i conduct

转载 2015年11月20日 22:46:01
  1. I Conduit!

Constraints

Time Limit: 3 secs, Memory Limit: 32 MB

Description

Irv Kenneth Diggit works for a company that excavates trenches, digs holes and generally tears up people’s yards. Irv’s job is to make sure that no underground pipe or cable is underneath where excavation is planned. He has several different maps, one for each utility company, showing where their conduits lie, and he needs to draw one large, consolidated map combining them all. One approach would be to simply draw each of the smaller maps one at a time onto the large map. However, this often wastes time, not to mention ink for the pen-plotter in the office, since in many cases portions of the conduits overlap with each other (albeit at different depths underground). What Irv wants is a way to determine the minimum number of line segments to draw given all the line segments from the separate maps.

Input

Input will consist of multiple input sets. Each set will start with a single line containing a positive integer n indicating the total number of line segments from all the smaller maps. Each of the next n lines will contain a description of one segment in the format x1 y1 x2 y2 where (x1,y1) are the coordinates of one endpoint and (x2,y2) are the coordinates of the other. Coordi- nate values are floating point values in the range 0… 1000 specified to at most two decimal places. The maximum number of line segments will be 10000 and all segments will have non-zero length. Following the last input set there will be a line containing a 0 indicating end of input; it should not be processed.

Output

For each input set, output on a single line the minimum number of line segments that need to be drawn on the larger, consolidated map.

Sample Input

3
1.0 10.0 3.0 14.0
0.0 0.0 20.0 20.0
10.0 28.0 2.0 12.0
2
0.0 0.0 1.0 1.0
1.0 1.0 2.15 2.15
2
0.0 0.0 1.0 1.0
1.0 1.0 2.15 2.16
0
Sample Output

2
1
2

CPP

#include <iostream>
#include <cmath>
#include <algorithm>

double const MAX = 99999;

using namespace std;

struct seg{//结构体的标签
    double k;
    double b;
    double leftx;
    double downy;
    double rightx;
    double upy;
}l[10001];//一个数组;记住如何定义结构体

void swap(double& a, double& b) {
    double tmp = a;
    a = b;
    b = tmp;
}
/*void swap(double *a, double *b) {
    double tmp = *a;
    a = b;
    b = &tmp;
    用c来表示swap函数
}*/


bool equal(double a, double b) {
    return fabs(a-b) < 1e-6;
}//浮点数有误差,当误差小于某个数时,认为这两个数是相等的。

bool comseg(const seg& a, const seg& b) {
    if(equal(a.k, b.k)) {
        if(a.k == MAX) {
            if(equal(a.b, b.b)) {
                if(equal(a.downy, b.upy)) {
                    return a.upy < b.upy;
                }
                return a.downy < b.downy;
            }
            return a.b < b.b;
        }
        else if(equal(a.b, b.b)) {
            if(equal(a.leftx, b.leftx)) {
                return a.rightx < b.rightx;
            }
            return a.leftx < b.leftx;
        }
        return a.b < b.b;
    }
    return a.k < b.k;
}//用于排序!

int main() {
    int n;
    while(cin >> n && n != 0) {
        for(int i = 0; i < n; i++) {
            cin >> l[i].leftx >> l[i].downy >> l[i].rightx >> l[i].upy;
            if(l[i].leftx == l[i].rightx) {
                l[i].k = MAX;
                l[i].b = l[i].leftx;
                if(l[i].downy > l[i].upy) {
                    swap(l[i].downy, l[i].upy);//让up和right为大的数。
                }
            }
            else if(l[i].leftx > l[i].rightx) {
                l[i].k = (l[i].upy - l[i].downy) / (l[i].rightx - l[i].leftx);
                l[i].b = l[i].downy - l[i].leftx  * l[i].k;
                swap(l[i].leftx, l[i].rightx);
                swap(l[i].downy, l[i].upy);
            }
            else {
                l[i].k = (l[i].upy - l[i].downy) / (l[i].rightx - l[i].leftx);
                l[i].b = l[i].downy - l[i].leftx  * l[i].k;
            }
        }
        sort(l, l+n, comseg);
        int count = 1;
        double x = l[0].rightx;
        double y = l[0].upy;
        for(int i = 1; i < n; i++) {
            if(!(equal(l[i].k, l[i-1].k) && equal(l[i].b, l[i-1].b))){//注意!的作用域!
                count++;
            }
            else if((l[i].k == MAX && l[i].downy > y) || (l[i].k != MAX && l[i].leftx > x)) {
                count++;
            }
            else if((l[i].k == MAX && y > l[i].upy) || (l[i].k != MAX && x >= l[i].rightx)) {
                continue;
            }
            x = l[i].rightx;
            y = l[i].upy;
        }
        cout << count << endl;
    }
    return 0;
}

此题思路:
1一定要用排序!可以减少很多的问题,只要比较相邻两条线就可以了。
2就算k和d相等,也有需要比较的部分。
3注意浮点数的误差问题

Andrey and Problem

 Description Andrey needs one more problem to conduct a programming contest. He has n frie...
  • qq_19327307
  • qq_19327307
  • 2014年12月14日 20:10
  • 282

when conduct the instruction“cmake...”

I have installed the ‘debug’ version of opencv in a path. then I want to install the ‘release’ vers...
  • thefighteran
  • thefighteran
  • 2016年03月25日 14:55
  • 222

Problem

E - Problem Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit...
  • Dandelioning
  • Dandelioning
  • 2014年08月21日 19:24
  • 454

百词斩

1. 具体名词 countess:伯爵夫人,女伯爵 Count: (法、德、南非)伯爵(欧洲贵族头衔) The Count of Monte Cristo:基督山伯爵; tent:帐篷 2. 抽象名...
  • lanchunhui
  • lanchunhui
  • 2016年10月29日 23:09
  • 497

IGFEL CODE OF CONDUCT

Intergroup Far East Ltd.  Ethical Policy.  -IGFEL CODE OF CONDUCT-I)PURPOSE AND SCOPE.   We, Intergr...
  • sharpening
  • sharpening
  • 2005年04月07日 22:44
  • 1484

Super Tool: Conduct Global Ping on One Site

http://just-ping.com alias:  http://cloudmonitor.ca.com/ TO BE CONTINUED
  • Derek_Zhang_
  • Derek_Zhang_
  • 2015年01月28日 18:41
  • 543

Ubuntu麒麟社区的行为准则(Code of Conduct)

     4月14日,我进入UbuntuKylin中文社区(www.ubuntukylin.com)的技术论坛,有感。 UbuntuKylin社区的官方口号是:“做更有中国味的操作系...
  • yuanmeng001
  • yuanmeng001
  • 2013年04月14日 02:34
  • 1446

Akka是个什么鬼?

What is Akka?Akka是什么? Scalable real-time transaction processing 可伸缩实时事务处理 We believe that writin...
  • jeruen
  • jeruen
  • 2016年03月20日 10:24
  • 13460

(++i) + (++i) + (++i) + (++i)

输出: i=1;  (++i) + (++i) + (++i) + (++i) =  3 + 3 + 4 + 5 = 15 前两个自增统一取值,后面依次自增立刻取值。 (i...
  • chenbang110
  • chenbang110
  • 2013年06月27日 23:09
  • 633

i=i++; i=++i

  程序1:public class iaaa { public static void main(String[] args) { int i = 4; i = i++; System.out...
  • fanhj__2007
  • fanhj__2007
  • 2010年05月10日 14:26
  • 736
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:i conduct
举报原因:
原因补充:

(最多只允许输入30个字)