Colorful Rainbows(计算几何)
来源:互联网 发布:锦绣未央网络上映 编辑:程序博客网 时间:2024/06/05 04:23
Description
Evelyn likes drawing very much. Today, she draws lots of rainbows on white paper of infinite size, each using a different color. Since there're too many rainbows now, she wonders, how many of them can be seen?
For simplicity, each rainbow Li is represented as a non-vertical line specified by the equation: y=aix+bi. A rainbow Li can be seen if there exists some x-coordinate x0 at which, its y-coordinate is strictly greater than y-coordinates of any other rainbows: aix0+bi > ajx0+bjfor all j != i.
Now, your task is, given the set of rainbows drawn, figure out the number of rainbows that can be seen.
Input
Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 60) which is the number of test cases. And it will be followed by T consecutive test cases.
There's a blank line before every case. In each test case, there will first be an integer n (1 <= n <= 5000), which is the number of rainbows. Then n consecutive real number pairs follow. Each pair contains two real numbers, ai and bi, representing rainbow Li: y=aix+bi. No two rainbows will be the same, that is to say, have the same a and b.
Output
Results should be directed to standard output. The output of each test case should be a single integer, which is the number of rainbows that can be seen.
Sample Input
211 131 02 03 0
Sample Output
12
思路:
这题其实就是根据斜率大小来排序,之后就能根据两条线交点的x轴每次都要小于下一次就能不被以前的线段覆盖了,之后剩下的直线就是没被覆盖的了。
剩下在栈的元素都是X1<X2<X3....<Xn的,只有这样才能让所加直线不被覆盖掉。
AC代码:
#include<iostream>#include<algorithm>#include<cstring>#include<string>#include<cstdio>#include<stack>using namespace std;#define T 10000#define inf 0x3f3f3f3fstruct node{double k,b,x;bool operator<(const node& a)const{return k<a.k||(k==a.k&&b<a.b);}}a[T],t[T];int main(){#ifdef zscfreopen("input.txt","r",stdin);#endifint n,m,i;scanf("%d",&n);while(n--){scanf("%d",&m);for(i=0;i<m;++i){scanf("%lf%lf",&a[i].k,&a[i].b);}sort(a,a+m);int c = 0;for(i=0;i<m-1;++i){//去重if(a[i].k!=a[i+1].k)t[c++]=a[i];}if(t[c-1].k!=a[m-1].k){t[c++] = a[m-1];}else{t[c-1] = a[m-1];}stack<node> s;t[0].x = -inf;node tmp;s.push(t[0]);int top;for(i=1;i<c;++i){while(true){tmp = s.top();double x = (tmp.b-t[i].b)/(t[i].k-tmp.k);if(tmp.x<x){t[i].x = x;s.push(t[i]);break;}else{s.pop();}}}printf("%d\n",s.size());}return 0;}
- Colorful Rainbows(计算几何)
- 【计算几何】ZOJ 2967 Colorful Rainbows
- ZOJ 2967Colorful Rainbows
- 省赛Colorful Rainbows
- ZOJ2967-Colorful Rainbows
- zoj2967 Colorful Rainbows(凸包 排序 栈)
- C - Colorful Rainbows----(2015 summer training #4 (Qualifying))
- HDU5088 Colorful Toy(计算几何+ploya计数)
- HDU 5080 Colorful Toy(polya+计算几何)
- ZOJ 2967-C - Colorful Rainbows
- C - Colorful Rainbows 解题报告 (双向栈)
- zoj 2967 Colorful Rainbows (堆栈应用)
- ZOJ 2967——Colorful Rainbows
- hdu 5080 - Colorful Toy(2014 AnShan)几何+polya
- 【数论】hdu5080 Colorful Toy (polya计数+简单几何)
- zoj 2967 Colorful Rainbows求不被覆盖的直线数(贪心)
- POJ2318(计算几何)
- poj1939(计算几何)
- IOS 编程中引用第三方的方类库的方法及常见问题
- KSImageNamed 安装后无效解决方法(试过有效)
- Effective C++ 笔记 第三部分 资源管理
- EPM-linux-重启weblogic
- 转一个 Xcode 7 缺少 *.dylib库的解决方法
- Colorful Rainbows(计算几何)
- 一些排版的笔记
- ServiceSocket 和 ThreadPoolExecutor学习笔记
- 认识smack3.0.4基本对象 - Presence
- CSRF(跨站请求伪造)学习笔记
- spring security csrf 学习笔记
- 《三国演义》与“项目管理”——向刘备拜师
- 黑马程序员——java基础---IO(下)
- 编译原理(一) Chomsky文法的判断方法及C++代码实现