2019独角兽企业重金招聘Python工程师标准>>>

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轴的直线,这里不妨设它斜率为正无穷(赋个很大的数即可),同时端点判断也要特殊处理。

代码:

// Problem#: 1004
// Submission#: 1792749
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;#define MAX 10000
#define INF 1000000
#define ESP 1e-6struct node{double x1,y1,x2,y2;double k,b;
}buffer[MAX];inline bool dcmp( double a, double b ){return ( fabs(a-b)<=ESP );
}inline double max( double a, double b ){return a>b?a:b;
}inline void swap( double &a, double &b ){double t = a;a = b;b = t;
}inline bool cmp( node a, node b ){if( !dcmp(a.k,b.k) ) return a.k<b.k;if( !dcmp(a.b,b.b) ) return a.b<b.b;if( !dcmp(a.x1,b.x1) ) return a.x1<b.x1;if( !dcmp(a.y1,b.y1) ) return a.y1<b.y1;if( !dcmp(a.x2,b.x2) ) return a.x2<b.x2;return a.y2<b.y2;
}inline bool judge( node a, node b ){if( dcmp(a.k,b.k) && dcmp(a.b,b.b) ){if( dcmp(a.k,INF) ) return dcmp(b.y1,a.y2) || b.y1<a.y2;else return  dcmp(a.x2,b.x1) || a.x2>b.x1;}return false;
}int main(){int n,re;while( cin>>n && n ){re = n;for( int i=0 ; i<n ; i++ ){cin >> buffer[i].x1 >> buffer[i].y1 >> buffer[i].x2 >> buffer[i].y2;bool flag = dcmp(buffer[i].x1,buffer[i].x2);if( flag && buffer[i].y1>buffer[i].y2 ) swap(buffer[i].y1,buffer[i].y2);else if( !flag && buffer[i].x1>buffer[i].x2 ){swap(buffer[i].x1,buffer[i].x2);swap(buffer[i].y1,buffer[i].y2);}buffer[i].k = flag ? INF : (buffer[i].y2-buffer[i].y1) / (buffer[i].x2-buffer[i].x1);buffer[i].b = flag ? buffer[i].x1 : buffer[i].y1-buffer[i].k*buffer[i].x1;}sort(buffer,buffer+n,cmp);for( int i=0 ; i<n-1 ; i++ ){if( judge(buffer[i],buffer[i+1]) ){re--;if( dcmp(buffer[i].k,INF) ) buffer[i+1].y2 = max(buffer[i].y2,buffer[i+1].y2);elsebuffer[i+1].x2 = max(buffer[i].x2,buffer[i+1].x2);}}cout << re << endl;}return 0;
}

转载于:https://my.oschina.net/u/235391/blog/96504

sicily 1004 I Conduit!相关推荐

  1. 【ACM】杭电OJ 1004

     题目链接:杭电OJ 1004 运行环境:Dev-C++ 5.11 思路: 先把先把num数组全部赋值为1:第一个颜色单独输入,从第二个开始,需要与前面的进行比较,如果前面有相同的颜色,则在目前的nu ...

  2. 百度之星2014资格赛 1004 - Labyrinth

    先上题目: Labyrinth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. Letters比赛第六场1004 Unit Fraction Partition解题报告

    1004 Unit Fraction Partition(POJ 1980) 解题思路:DFS + 剪枝.这题的剪枝条件还是比较严格的,很容易超时,我想到的需要剪枝的情况有以下几点:①前几项的和超过了 ...

  4. PAT甲级1004 Counting Leaves (30分):[C++题解]树、邻接表存储树、dfs遍历树

    文章目录 题目分析 题目链接 题目分析 题意重述:一棵树,求每一层的叶子节点数目. 分析 构造树,使用邻接表来存(相当于存储有向图). 需要一个头结点数组h[N],然后每个头节点往外形成一个单链表e[ ...

  5. lightoj 1004 dp:数字三角形

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1004 #include <cstdio> #include <cst ...

  6. 题目 1004: [递归]母牛的故事

    题目 1004: [递归]母牛的故事 idea 1 2 3 4 6 9 13 第4年母牛的数量=第1年母牛的数量+第3年母牛的数量 第5年母牛的数量=第2年母牛的数量+第4年母牛的数量 第6年母牛的数 ...

  7. leetcode 485,487,1004. Max Consecutive Ones I ,II, III(最大连续1的个数问题合集)

    485. Max Consecutive Ones https://leetcode.com/problems/max-consecutive-ones/ easy 题,思路不说了,直接上代码. cl ...

  8. 【BZOJ】1004: [HNOI2008]Cards(置换群+polya+burnside)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1004 学习了下polya计数和burnside引理,最好的资料就是:<Pólya 计数法的应用 ...

  9. 百炼OJ - 1004 - 财务管理

    题目链接:http://bailian.openjudge.cn/practice/1004/ 思路 求和取平均... #include <stdio.h>int main() {floa ...

最新文章

  1. Offer是否具有法律效力?
  2. mysql 单例模式好处_PHP单例模式的优点分析
  3. EasyExcel(笔记)
  4. 线程Blocked--SynchronizedDemo
  5. python编写arcgis脚本教程_ArcGIS使用Python脚本工具
  6. 基础知识之什么是I/O
  7. tomcat启动脚本
  8. TortoiseGit-创建分支、合并分支
  9. pycharm 改成中文亲测好用
  10. Unity 日志管理系统
  11. 如何用数据驱动的广告效果
  12. CondaValueError : prefix already exists: /**/anaconda3
  13. ajax的三种传参方式
  14. 2022-2028年中国网络直播行业商业模式创新与投资机会深度研究报告
  15. 在下载 chromium 源码时错误汇总
  16. 定时任务ScheduledExecutorService
  17. 各个认证记录及说明SRRC与CTA认证
  18. 如何对待每逢佳节被逼婚
  19. 如何往hive直接插入自定义数据values
  20. Vue3实现打字机效果

热门文章

  1. hive数据导入导出
  2. linux路由介绍,Linux的路由表详细介绍
  3. vue页面数据不显示_PHP7中session_start 使用注意事项,会导致浏览器刷时页面数据不更新...
  4. elasticsearch的父子_elasticsearch父子关系(官方)实际使用中的一些建议
  5. 张仰彪第二排序法_十大排序之冒泡和选择排序
  6. 了解下Lua 环境安装
  7. mysql/mariadb命令如何获取帮助
  8. 【Java】数据结构之 顺序表(MyArrayList)
  9. HDU1269(强连通分量)
  10. P5357 【模板】AC自动机(二次加强版)(AC自动机建fail树dfs求模式串出现次数)