zoukankan      html  css  js  c++  java
  • Pick-up sticks[HDU1147]

    Pick-up sticks
    Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 2950 Accepted Submission(s): 1108


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

     

    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.

      虽然总的边数很多,但是他说了位于最上面的边不超过1000个。所以只要维护一个长度不超过1000的链表,从前往后计算。一开始链表为空,对于每一条边,将它与链表中的每个元素比较是否相交,若相交则将该边移除,比较完成后将当前边加入到链表末尾。全部处理结束后,剩下的边就是最上面的边。

    #include <stdio.h>
    #include <iostream>
    #include <list>
    using namespace std;
    // ALGORITHM OF GEOMETRY ->
    
    struct Geometry_vertex {
        double x, y;
    };
    struct Geometry_vector {
        double x, y;
    };
    struct Geometry_line {
        Geometry_vertex A, B;
        int i;
    };
    Geometry_vector MakeGeometry_vector(Geometry_vertex v1, Geometry_vertex v2) {
        Geometry_vector v;
        v.x = v2.x - v1.x;
        v.y = v2.y - v1.y;
        return v;
    }
    double DotProduct(Geometry_vector v1, Geometry_vector v2) {
        return ((v1.x) * (v2.x) + (v1.y) * (v2.y));
    }
    double CrossProduct(Geometry_vector v1, Geometry_vector v2) {
        return ((v1.x) * (v2.y) - (v2.x) * (v1.y));
    }
    bool IsLineCrossed(Geometry_line l1, Geometry_line l2) {
        Geometry_vector v1, v2;
        double c1, c2;
        v1 = MakeGeometry_vector(l1.A, l1.B);
        v2 = MakeGeometry_vector(l1.A, l2.A);
        c1 = CrossProduct(v1, v2);
        v2 = MakeGeometry_vector(l1.A, l2.B);
        c2 = CrossProduct(v1, v2);
        if(c1 * c2 >= 0) {
            return false;
        }
        v1 = MakeGeometry_vector(l2.A, l2.B);
        v2 = MakeGeometry_vector(l2.A, l1.A);
        c1 = CrossProduct(v1, v2);
        v2 = MakeGeometry_vector(l2.A, l1.B);
        c2 = CrossProduct(v1, v2);
        if(c1 * c2 >= 0) {
            return false;
        }
        return true;
    }
    // <- ALGORITHM OF GEOMETRY
    list<Geometry_line> G;
    int main() {
        int n;
        while(scanf("%d", &n) != EOF) {
            if(n == 0) {
                break;
            }
            G.clear();
            Geometry_line l;
            for(int i = 1; i <= n; i++) {
                scanf("%lf%lf%lf%lf", &l.A.x, &l.A.y, &l.B.x, &l.B.y);
                l.i = i;
                for(list<Geometry_line>::iterator it = G.begin(); it != G.end();) {
                    if(IsLineCrossed(*it, l)) {
                        it = G.erase(it);
                    } else {
                        it++;
                    }
                }
                G.push_back(l);
            }
            printf("Top sticks: ");
            for(list<Geometry_line>::iterator it = G.begin(); it != G.end(); it++) {
                list<Geometry_line>::iterator j = it;
                j++;
                if(j == G.end()) {
                    printf("%d.
    ", (*it).i);
                } else {
                    printf("%d, ", (*it).i);
                }
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    关于word开发中字体大小
    WPF学习笔记
    C#各种配置文件使用,操作方法总结
    web.config和app.config使用
    微软 WordXML格式初步分析
    面向对象—C#高级编程(第10版)学习笔记8
    C#编程的推荐规则和约定—C#高级编程(第10版)学习笔记7
    C#基础—C#高级编程(第10版)学习笔记6
    .Net 应用程序体系结构—C#高级编程(第10版)学习笔记5
    通俗易懂说编程:.Net Core是什么、有何用?
  • 原文地址:https://www.cnblogs.com/dramstadt/p/6091037.html
Copyright © 2011-2022 走看看