sicily 1004 I Conduit!

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

分析:

本题比较水,主要要注意浮点数的相等判断处理,以及输入数据存储顺序的整理,然后排序统计即可。特别注意的是,本题的数据是线段,不是直线,所以能否合并要判断两条共线线段的相邻端点;还有点斜式无法表示垂直于x轴的直线,这里不妨设它斜率为正无穷(赋个很大的数即可),同时端点判断也要特殊处理。

代码:

 1 // Problem#: 1004
 2 // Submission#: 1792749
 3 // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
 4 // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
 5 // All Copyright reserved by Informatic Lab of Sun Yat-sen University
 6 #include <iostream>
 7 #include <algorithm>
 8 #include <cmath>
 9 using namespace std;
10 
11 #define MAX 10000
12 #define INF 1000000
13 #define ESP 1e-6
14 
15 struct node{
16     double x1,y1,x2,y2;
17     double k,b;
18 }buffer[MAX];
19 
20 inline bool dcmp( double a, double b ){
21     return ( fabs(a-b)<=ESP );
22 }
23 
24 inline double max( double a, double b ){
25     return a>b?a:b;
26 }
27 
28 inline void swap( double &a, double &b ){
29     double t = a;
30     a = b;
31     b = t;
32 }
33 
34 inline bool cmp( node a, node b ){
35     if( !dcmp(a.k,b.k) ) return a.k<b.k;
36     if( !dcmp(a.b,b.b) ) return a.b<b.b;
37     if( !dcmp(a.x1,b.x1) ) return a.x1<b.x1;
38     if( !dcmp(a.y1,b.y1) ) return a.y1<b.y1;
39     if( !dcmp(a.x2,b.x2) ) return a.x2<b.x2;
40     return a.y2<b.y2;
41 }
42 
43 inline bool judge( node a, node b ){
44     if( dcmp(a.k,b.k) && dcmp(a.b,b.b) ){
45         if( dcmp(a.k,INF) ) return dcmp(b.y1,a.y2) || b.y1<a.y2;
46         else return  dcmp(a.x2,b.x1) || a.x2>b.x1;
47     }
48     return false;
49 }
50 
51 int main(){
52     int n,re;
53     while( cin>>n && n ){
54         re = n;
55         for( int i=0 ; i<n ; i++ ){
56             cin >> buffer[i].x1 >> buffer[i].y1 >> buffer[i].x2 >> buffer[i].y2;
57             bool flag = dcmp(buffer[i].x1,buffer[i].x2);
58             if( flag && buffer[i].y1>buffer[i].y2 ) swap(buffer[i].y1,buffer[i].y2);
59             else if( !flag && buffer[i].x1>buffer[i].x2 ){
60                 swap(buffer[i].x1,buffer[i].x2);
61                 swap(buffer[i].y1,buffer[i].y2);
62             }
63             buffer[i].k = flag ? INF : (buffer[i].y2-buffer[i].y1) / (buffer[i].x2-buffer[i].x1);
64             buffer[i].b = flag ? buffer[i].x1 : buffer[i].y1-buffer[i].k*buffer[i].x1;
65         }
66         sort(buffer,buffer+n,cmp);
67         for( int i=0 ; i<n-1 ; i++ ){
68             if( judge(buffer[i],buffer[i+1]) ){
69                 re--;
70                 if( dcmp(buffer[i].k,INF) ) buffer[i+1].y2 = max(buffer[i].y2,buffer[i+1].y2);
71                 else
72                     buffer[i+1].x2 = max(buffer[i].x2,buffer[i+1].x2);
73             }
74         }
75         cout << re << endl;
76     }
77     return 0;
78 }                                 

 

转载于:https://www.cnblogs.com/ciel/archive/2013/01/25/2876770.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
大学生参加学科竞赛有着诸多好处,不仅有助于个人综合素质的提升,还能为未来职业发展奠定良好基础。以下是一些分析: 首先,学科竞赛是提高专业知识和技能水平的有效途径。通过参与竞赛,学生不仅能够深入学习相关专业知识,还能够接触到最新的科研成果和技术发展趋势。这有助于拓展学生的学科视野,使其对专业领域有更深刻的理解。在竞赛过程中,学生通常需要解决实际问题,这锻炼了他们独立思考和解决问题的能力。 其次,学科竞赛培养了学生的团队合作精神。许多竞赛项目需要团队协作来完成,这促使学生学会有效地与他人合作、协调分工。在团队合作中,学生们能够学到如何有效沟通、共同制定目标和分工合作,这对于日后进入职场具有重要意义。 此外,学科竞赛是提高学生综合能力的一种途径。竞赛项目通常会涉及到理论知识、实际操作和创新思维等多个方面,要求参赛者具备全面的素质。在竞赛过程中,学生不仅需要展现自己的专业知识,还需要具备创新意识和解决问题的能力。这种全面的综合能力培养对于未来从事各类职业都具有积极作用。 此外,学科竞赛可以为学生提供展示自我、树立信心的机会。通过比赛的舞台,学生有机会展现自己在专业领域的优势,得到他人的认可和赞誉。这对于培养学生的自信心和自我价值感非常重要,有助于他们更加积极主动地投入学习和未来的职业生涯。 最后,学科竞赛对于个人职业发展具有积极的助推作用。在竞赛中脱颖而出的学生通常能够引起企业、研究机构等用人单位的关注。获得竞赛奖项不仅可以作为个人履历的亮点,还可以为进入理想的工作岗位提供有力的支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值