POJ Pick-up sticks

                                                                                                      Pick-up sticks
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 7550   Accepted: 2789

Description

Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishing throwing, Stan tries to find the top sticks, that is these sticks such that there is no stick on top of them. Stan has noticed that the last thrown stick is always on top but he wants to know all the sticks that are on top. Stan sticks are very, very thin such that their thickness can be neglected.

Input

Input consists of a number of cases. The data for each case start with 1 <= n <= 100000, the number of sticks for this case. The following n lines contain four numbers each, these numbers are the planar coordinates of the endpoints of one stick. The sticks are listed in the order in which Stan has thrown them. You may assume that there are no more than 1000 top sticks. The input is ended by the case with n=0. This case should not be processed.

Output

For each input case, print one line of output listing the top sticks in the format given in the sample. The top sticks should be listed in order in which they were thrown.

The picture to the right below illustrates the first case from input. POJ Pick-up sticks

Sample Input

5

1 1 4 2

2 3 3 1

1 -2.0 8 4

1 4 8 2

3 3 6 -2.0

3

0 0 1 1

1 0 2 1

2 0 3 1

0

Sample Output

Top sticks: 2, 4, 5.

Top sticks: 1, 2, 3.

Hint

Huge input,scanf is recommended.
#include <iostream>

#include <cstdio>

#include <cstring>

#include <cstdlib>

#include <algorithm>

#include <vector>

//Accepted	4116K	641MS	C++	1841B	2013-05-22 16:46:02

using namespace std;

const int maxn = 100010;



struct Point {

    double x;

    double y;

};



struct V {

    bool mark;

    Point st;    ///start;

    Point et;    ///end

    V() {

        mark = true;

    }

};

V v[maxn];

int n;



double det(Point p1, Point p2, Point p0) {

    return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);

}



int Across(struct V v1, struct V v2) {

    if(max(v1.st.x, v1.et.x)>= min(v2.st.x, v2.et.x) &&

       max(v2.st.x, v2.et.x)>= min(v1.st.x, v1.et.x) &&

       max(v1.st.y, v1.et.y)>= min(v2.st.y, v2.et.y) &&

       max(v2.st.y, v2.et.y)>= min(v1.st.y, v1.et.y) &&

       det(v2.st, v1.et, v1.st)*det(v1.et, v2.et, v1.st)>=0 &&

       det(v1.st, v2.et, v2.st)*det(v2.et, v1.et, v2.st)>=0

       )

       return 1;

       return 0;

}



void work() {

    for(int i = 1; i <= n-1; i++) {

        for(int j = i+1; j <= n; j++) {

            if(Across(v[i], v[j])) {

                v[i].mark = false;

                break;

            }

        }

    }

    vector<int> tmp;

    tmp.clear();

    for(int i = 1; i <= n; i++) {

        if(v[i].mark) {

            tmp.push_back(i);

        }

    }

    printf("Top sticks: ");

    int maxsize = tmp.size();

    for(int i = 0; i < maxsize-1; i++) {

        printf("%d, ", tmp[i]);

    }

    printf("%d.\n", tmp[maxsize-1]);

}



void init() {

    for(int i = 1; i <= n; i++) {

        v[i].mark = true;

    }

}



int main()

{

    while(scanf("%d", &n) != EOF) {

        if(n==0) break;

        init();

        for(int i = 1; i <= n; i++) {

            scanf("%lf%lf%lf%lf", &v[i].st.x, &v[i].st.y, &v[i].et.x, &v[i].et.y);

        }

        work();

    }

    return 0;

}


 

你可能感兴趣的:(poj)