PKU——1328 Radar Installation
2009-04-26 18:25 Logic0 阅读(219) 评论(0) 编辑 收藏 举报题目链接:http://acm.pku.edu.cn/JudgeOnline/problem?id=1328
方法:贪心。
规则:
- 按照岛的x坐标从小到达排序
- 依次计算可以探测到小岛的雷达可以放置的x坐标范围
- 如果两个小岛的雷达有交集,则共用一个雷达,否则,新开一个雷达
- 依次进行,直至所有岛屿都可探测
CODE:
#include <iostream> #include <vector> #include <algorithm> #include <utility> #include <cmath> using namespace std; typedef struct { int x,y; double from,to; }NODE; bool compare(NODE a,NODE b) { return a.x < b.x; } int main() { vector<NODE> island; long n,i,j,k,d; cin>>n>>d; int case_num = 1; while(n&&d) { long res = 0; NODE tmp; int flag = 1; for(i=1 ; i<=n ; i++) { cin>>tmp.x>>tmp.y; if(tmp.y > d) { flag = -1; } else { tmp.from = (double)tmp.x - sqrt((double)d*d - (double)tmp.y*tmp.y); tmp.to = (double)tmp.x + sqrt((double)d*d - (double)tmp.y*tmp.y); island.push_back(tmp); } } if(flag == -1) { flag = 1; cout<<"Case "<<case_num<<": -1"<<endl; island.clear(); } else { sort(island.begin(),island.end(),compare); tmp = island[0]; res = 1; for(vector<NODE>::iterator i=island.begin()+1 ; i!=island.end() ; i++) { if((*i).from <= tmp.to) { if(tmp.from < (*i).from) tmp.from = (*i).from; if(tmp.to > (*i).to) tmp.to = (*i).to; } else { res += 1; tmp = *i; } } cout<<"Case "<<case_num<<": "<<res<<endl; } cin>>n>>d; island.clear(); case_num += 1; } return 0; }