ZOJ2967-Colorful Rainbows
来源:互联网 发布:js删除节点的方法 编辑:程序博客网 时间:2024/05/24 05:30
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+bj for 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 Input211 131 02 03 0Sample Output
12
Author: DAI, Wenbin
Source: The 5th Zhejiang Provincial Collegiate Programming Contest
题意:给出n条y=ai*x+bi的直线。对于这些直线,如果存在x使得该直线y大于其他任意一直线,那么这条直线可以被看见,问有多少条直线可以被看见。
解题思路:首先去重,将那些a值相同的直线取其中b最大的那条保留下来,其他的全删掉。再将直线按照a值从小到大排序,因为斜率不同,所以任意两条直线都会相交。而这些直线是按照斜率从小到大进行排序,所以当x小于其交点x值时,斜率小的y值大。利用这一特性将,先让线入栈。若将入栈的线与栈顶线的交点x值小于栈顶两条线的的交点的x值,则将栈顶线出栈,继续进行上一次判断,直到所有线都判定过,第一条入栈的直线交点可以判断为-INF。
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <cmath>#include <algorithm>#include <stack>#include <queue>#include <climits>#include <functional>#include <vector>#include <map>#include <set>using namespace std;#define LL long longconst int INF=0x3f3f3f3f;struct node{ double a; double b;} s[5010],x[5010];bool cmp(node a1,node a2){ if(a1.a!=a2.a) return a1.a<a2.a; return a1.b<a2.b;}int main(){ int t,n,ans; double d,p; scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i=0; i<n; i++) scanf("%lf%lf",&s[i].a,&s[i].b); sort(s,s+n,cmp); int cnt=0; for(int i=0; i<n-1; i++) { if(s[i].a==s[i+1].a) continue; s[cnt++]=s[i]; } s[cnt++]=s[n-1]; x[0]=s[0]; d=-INF; ans=1; for(int i=1; i<cnt; i++) { p=(s[i].b-x[ans-1].b)*1.0/(x[ans-1].a-s[i].a); while(p<=d) { ans--; if(ans>1) { d=(x[ans-1].b-x[ans-2].b)*1.0/(x[ans-2].a-x[ans-1].a); p=(s[i].b-x[ans-1].b)*1.0/(x[ans-1].a-s[i].a); } else break; } d=(s[i].b-x[ans-1].b)*1.0/(x[ans-1].a-s[i].a); x[ans++]=s[i]; } printf("%d\n",ans); } return 0;}
- ZOJ2967-Colorful Rainbows
- zoj2967 Colorful Rainbows(凸包 排序 栈)
- ZOJ 2967Colorful Rainbows
- 省赛Colorful Rainbows
- ZOJ 2967-C - Colorful Rainbows
- Colorful Rainbows(计算几何)
- C - Colorful Rainbows 解题报告 (双向栈)
- 【计算几何】ZOJ 2967 Colorful Rainbows
- zoj 2967 Colorful Rainbows (堆栈应用)
- ZOJ 2967——Colorful Rainbows
- C - Colorful Rainbows----(2015 summer training #4 (Qualifying))
- zoj 2967 Colorful Rainbows求不被覆盖的直线数(贪心)
- zoj2967(堆栈应用)
- bzoj1759: Let there be rainbows!
- bzoj1759: Let there be rainbows!
- Colorful Tree
- colorful tree
- Colorful Tree
- android 学习(十四) 拍照捕获
- java cookie用法
- 动态 | 时隔一年卷土重来,强化版AlphaGo的制胜棋艺能再续辉煌吗?
- 名企笔试:网易2017内推笔试编(星际穿越) 2017-03-29 算法爱好者
- 拿到当前被调用代码所在的类名
- ZOJ2967-Colorful Rainbows
- Spring笔记——初识Spring
- 理解Python 中的 with 语句
- selenium webdriver学习 -怎么等待页面元素加载完成
- 高斯消元裸题(期望)——【HNOI2013】游走
- python学习笔记3
- Centos常用网络配置
- 高中老班(班主任)的经典语录
- TOMCAT的虚拟化配置