Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

新人16MS代码水一贴

Posted by scissorsy at 2016-01-05 22:00:59 on Problem 1328 and last updated at 2016-01-05 22:04:06
#include <cstdio>
#include <utility>
#include <algorithm>
#include <cmath>
using namespace std;
typedef pair<double, double> Pair;
const int MAX_N = 1000;
bool getRange(Pair& range, Pair coor, double d){
	if(d <= 0 || coor.second < 0 || coor.second > d)
		return false;
	range.first = coor.first - sqrt(d*d - coor.second * coor.second);
	range.second = coor.first + sqrt(d*d - coor.second * coor.second);
	return true;
}
int main() {
	setvbuf(stdin, new char[1 << 20], _IOFBF, 1 << 20);
    setvbuf(stdout, new char[1 << 20], _IOFBF, 1 << 20);
	int n, case_cnt = 0;
	double d;
	Pair coors[MAX_N];
	while(scanf("%d%lf", &n, &d), n||d){
		case_cnt++;
		for(int i = 0;i<n;i++)
			scanf("%lf%lf", &coors[i].first, &coors[i].second);
		getchar();
		sort(coors, coors+n);
		Pair range[MAX_N];
		bool flag = false;
		for(int i = 0;i<n;i++)
			if(!getRange(range[i], coors[i], d)){
				printf("Case %d: %d\n", case_cnt, -1);
				flag = true;
				break;
			}		
		if(flag)
			continue;
		int cnt = 1, i = 0;
		while(i < n - 1){
			if(range[i].second >= range[i+1].first){
				if(range[i].second < range[i+1].second)
					range[i+1].second = range[i].second;
			}
			else
				cnt++;
			i++;
		}
		printf("Case %d: %d\n", case_cnt, cnt);
	}
	return 0;
}
一直超时,后来看到一篇帖子恍然大悟是自己的算法错了,这道题事实上是区间问题,每一个岛屿都确定一个雷达可辐射的区间,如果两个岛屿的区间有交集,将雷达安置在交集处则可以覆盖两个岛屿,如果没有交集则说明不可能覆盖,至少需要两个雷达

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator