ZOJ - 2967 栈 计算几何

Colorful Rainbows

Time Limit: 2 Seconds       Memory Limit: 65536 KB

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

1
1 1

3
1 0
2 0
3 0
Sample Output
1

2

题意: 给你n 条直线 问你从上往下看 可以看到几条直线。

思路: 根据直线的 斜率和截距 排序, 然后单调栈解题。

代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#define N 5005
#define inf 0x3f3f3f3f

using namespace std;

struct node
{
	double k,b;
	double l,r;
}lin[N],s[N],a[N];

int n;

double eps=1e-8;

bool cmp(node a,node b){
	if(fabs(a.k-b.k)<eps) return a.b>b.b;
	return a.k<b.k;
}

int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		{
			scanf("%lf %lf",&lin[i].k,&lin[i].b);
		}
		
		sort(lin+1,lin+n+1,cmp);
		
		/*
		for(int i=1;i<=n;i++)
		{
			printf("%lf %lf \n",lin[i].k,lin[i].b);
		}
		*/
		int kk=0;
		a[++kk]=lin[1];
		for(int i=2;i<=n;i++)
		{
			if(lin[i].k==lin[i-1].k) continue;
			a[++kk]=lin[i];
		}
		n=kk;
		
		for(int i=1;i<=n;i++) lin[i]=a[i];
		/*
		printf(" n:  %d\n",n);
		*/
		
		int cc=1;
		s[1].l=-inf;  s[1].r=inf;
		s[1].k=lin[1].k; s[1].b=lin[1].b;
		
		for(int i=2;i<=n;i++)
		{
			int j,k;
			double x,k1,k2,b1,b2;
			j=1;  k=1;
			while(j<=cc)
			{
				k1=lin[i].k;  b1=lin[i].b;
				k2=s[j].k; b2=s[j].b;
				
				x=(b2-b1)/(k1-k2);
				
				if(x<=s[j].r){
					k=j;  break;
				}
				j++;
			}
			
			s[k].r=x;
			cc=k+1;
			s[cc].l=x;  s[cc].r=inf;   s[cc].k=k1;  s[cc].b=b1;
			
			
			
		}
		
		printf("%d\n",cc);
		
	}
	return 0;
}

/*

2
4
1 0
2 0
3 0
2 0


*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值