C - Colorful Rainbows ZOJ - 2967 (不得不说,出的数据是真的强,样例都是整数,没看见题目说是real number)

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 x0at 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 Input
2

1
1 1

3
1 0
2 0
3 0
Sample Output
1
2


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

typedef long long ll;
int n, m;

const int maxn=5010;
const double eps=1e-9;

struct node{
	double a,b;
}pp[maxn],p[maxn],stk[maxn];

int check(node x1,node x2,node x3){
	double tmp1=(x3.b-x2.b)/(x2.a-x3.a);
	double tmp2=(x2.b-x1.b)/(x1.a-x2.a);
	//printf("%.2f %.2f\n",tmp1,tmp2);
	if(tmp2+eps>=tmp1) return 1;
	return 0;
}

int cmp(node x1,node x2){
	if(x1.a==x2.a) return x1.b>x2.b;
	return x1.a<x2.a;
}

int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		int n;
		scanf("%d",&n);
		for(int i=1;i<=n;i++){
			scanf("%lf %lf",&p[i].a,&p[i].b);
		}
		sort(p+1,p+n+1,cmp);
		int cnt=0;
		pp[++cnt]=p[1];
		for(int i=2;i<=n;i++){
			if(fabs(p[i].a-p[i-1].a)<eps)continue;
			pp[++cnt]=p[i];
		}
		int top=-1;
		for(int i=1;i<=cnt;i++){
			if(top<=0){
				stk[++top]=pp[i];
			}
			else{
				node c=pp[i];
				while(top>0){
					node a=stk[top-1];
					node b=stk[top];
					if(check(a,b,c))//x2<=x1
						top--;
					else
						break;
				}
				stk[++top]=pp[i];
			}
		}
		printf("%d\n",top+1);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36424540/article/details/80226607