C - Colorful Rainbows ZOJ - 2967 (I have to say, the data is really strong, the samples are all integers, I didn't see the title saying it is 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+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 integerT(1 <=T<= 60) which is the number of test cases. And it will be followed byTconsecutive test cases.      

There's a blank line before every case. In each test case, there will first be an integern(1 <=n<= 5000), which is the number of rainbows. Thennconsecutive real number pairs follow. Each pair contains two real numbers,aiandbi, representing rainbow Li: y=aix+bi. No two rainbows will be the same, that is to say, have the sameaandb.            

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=dc[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;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325996823&siteId=291194637