24h購物| | PChome| 登入
2013-01-03 20:55:20| 人氣1,055| 回應0 | 上一篇 | 下一篇

[UVA][線段交] 10902 - Pick-up Sticks

推薦 0 收藏 0 轉貼0 訂閱站台

Problem C: Pick-up sticks

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 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.

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.

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

Output for sample input

Top sticks: 2, 4, 5.
Top sticks: 1, 2, 3.


Piotr Rudnicki


#include <stdio.h>
#define min(x, y) ((x) < (y) ? (x) : (y))
#define max(x, y) ((x) > (y) ? (x) : (y))
struct Point {
    double x, y;
};
struct Segment {
    Point s, t;
};
int in(double a, double b, double c) {
    return c >= a && c <= b;
}
int onLine(Segment& a, Point& c) {
    static double minx, maxx, miny, maxy;
    minx = min(a.s.x, a.t.x);
    maxx = max(a.s.x, a.t.x);
    miny = min(a.s.y, a.t.y);
    maxy = max(a.s.y, a.t.y);
    if(in(minx, maxx, c.x) != 0 && in(miny, maxy, c.y) != 0) {
        if((a.s.x-a.t.x)*(c.y-a.s.y) == (a.s.y-a.t.y)*(c.x-a.s.x)) {
            return 1;
        }
    }
    return 0;
}
double cross(Point &o, Point &a, Point &b) {
    return (a.x-o.x)*(b.y-o.y)-(a.y-o.y)*(b.x-o.x);
}
int intersection(Segment& a, Segment& b) {
    if(onLine(a, b.s) || onLine(a, b.t) || onLine(b, a.s) || onLine(b, a.t))
        return 1;
    if(cross(a.s, a.t, b.s)*cross(a.s, a.t, b.t) < 0 &&
       cross(a.t, a.s, b.s)*cross(a.t, a.s, b.t) < 0 &&
       cross(b.s, b.t, a.s)*cross(b.s, b.t, a.t) < 0 &&
       cross(b.t, b.s, a.s)*cross(b.t, b.s, a.t) < 0
       )
        return 1;
    return 0;
}
Segment stk[1005];
int stkin[1005];
int main() {
    int n, i, j, k;
    while(scanf("%d", &n) == 1 && n) {
        int idx = 0;
        Segment T;
        for(i = 0; i < n; i++) {
            scanf("%lf %lf %lf %lf", &T.s.x, &T.s.y, &T.t.x, &T.t.y);
            for(j = 0, k = 0; j < idx; j++)
                if(!intersection(T, stk[j]))
                    stkin[k] = stkin[j], stk[k++] = stk[j];
            idx = k;
            stkin[idx] = i, stk[idx++] = T;
        }
        printf("Top sticks:");
        for(i = 0; i < idx; i++) {
            if(i)   putchar(',');
            printf(" %d", stkin[i]+1);
        }
        puts(".");
    }
    return 0;
}

台長: Morris
人氣(1,055) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][拓樸排序] 1119 - Project File Dependencies
此分類上一篇:[UVA] 782 - Contour Painting

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文