西南民族大學第十一屆程序設計競賽(同步賽)-C Guard the empire

地址:https://ac.nowcoder.com/acm/contest/3570/C

思路:思維+貪心

這題直接將點按x軸排序貪心是不對的,這樣並不能保證前一個點的最右邊包含的圓能夠包含後一個點

例如包含(0,1)的最右邊的圓不能夠包含(1,2)。而可以考慮將點轉換爲包含該點的圓心的範圍,即轉換爲一條線段,在對其排序貪心即可。

Code:

#include<iostream>
#include<algorithm>
using namespace std;
typedef pair<double,double> pr;

const int MAX_N=1e3+5;
int n,m;
pr a[MAX_N];

int main()
{
	ios::sync_with_stdio(false);
	int t=1,res;
	while(cin>>n>>m&&n+m!=0){
		res=1;
		int x,y;
		for(int i=0;i<n;++i)
		{
			cin>>x>>y;
			if(y>m)	res=-1;
			if(res!=-1){
				a[i].first=x-sqrt(m*m-y*y);
				a[i].second=x+sqrt(m*m-y*y);
			}
		}
		if(res!=-1){
			sort(a,a+n);
			double l=a[0].first,r=a[0].second;
			for(int i=0;i<n;++i)
			{
				l=max(l,a[i].first);
				r=min(r,a[i].second);
				if(l>r){
					++res;	l=a[i].first;	r=a[i].second;
				}
			}
		}
		cout<<"Case "<<t++<<": "<<res<<endl;
	}
	
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章