zoj 2967 Colorful Rainbows求不被覆盖的直线数(贪心)

本文探讨了一种简化版的彩虹可见性问题,通过数学模型将彩虹表示为一系列非垂直线段,并提出一种算法来确定在二维平面上有多少条彩虹线可以被看到。输入包括多组测试案例,每组案例包含彩虹的数量及每条彩虹的参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1966

#include<iostream>
#include<algorithm>
#include<string>
#include<map>
#include<string.h>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<cstdio>
#define ll long long
using namespace std;
int comp(pair<double,double>a,pair<double,double>b){
	if(a.first!=b.first)
		return a.first<b.first;
	return a.second<b.second;
}
int main(){
	int t;
	cin>>t;
	while(t--){
		int n;
		cin>>n;
		pair<double,double> xian[6000];
		for(int i=0;i<n;i++){
			double a,b;
			cin>>a>>b;
			xian[i]=make_pair(a,b); //存每条线的斜率和 b 
		}
		sort(xian,xian+n,comp);
		pair<double,int>jiao;
		jiao=make_pair(10000007,n-1);  //从最后一个点开始 
		int sum=1;
		while(1){
			double maxn = -100000010;
			int biao=-1;
			int u=0;
			for(int i=0;i<n-1;i++){
				if(xian[i].first ==1.1&&xian[i].second ==1.1)
					continue;
				double x = (xian[i].second-xian[jiao.second].second)/(xian[jiao.second].first-xian[i].first);
			 	if(x<jiao.first&&x>maxn){ //x是两条线段交点的横坐标 
			 		maxn = x;
			 		biao = i;
			 		u=1;
			 	}
				else if(x==jiao.first&&xian[i].first<xian[jiao.second].first){  //x==jiao.first当前交点与上一次的交点横坐标一样时,去斜率较小的 
			 		maxn = x;
			 		biao = i;
			 		u=1;
			 	}
			}
			if(u==0)
				break;
			xian[jiao.second]=make_pair(1.1,1.1);  //表示该条直线移除 
			jiao = make_pair(maxn,biao);  //最大的横坐标 ,下一次的目标直线下标 
			sum++;
		}	
		cout<<sum<<endl;	 
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值