区间贪心·POJ 1328·Radar Installation
2018-03-15 13:35
477 查看
题目大意:
坐标系中有n个小岛,在x轴上设置雷达,雷达范围是d,要求用最少雷达能覆盖所有小岛;
解题思路:
如果直接用x轴上点到岛的距离进行计算,不仅麻烦而且很容易打乱思路,毕竟圆形范围不是很好处理。
我们可以把它转化为x轴上的区间问题,既然岛到雷达距离不能超过d,那么在x轴上必定对应一个范围使得雷达必须在区间内才可以覆盖到岛。
AC代码:#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <bitset>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <fstream>
#include <cstdlib>
#include <sstream>
#include <cstring>
#include <iostream>
#include <algorithm>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
#define maxn 1010
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ms(x,y) memset(x,y,sizeof(x))
#define rep(i,n) for(int i=0;i<(n);i++)
#define repf(i,a,b) for(int i=(a);i<=(b);i++)
#define pii pair<int,int>
//#define mp make_pair
#define FI first
#define SE second
#define IT iterator
#define PB push_back
#define Times 10
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int ,int > P;
const double eps = 1e-10;
const double pi = acos(-1.0);
const ll mod = 1e9+7;
const int inf = 0x3f3f3f3f;
const ll INF = (ll)1e18+300;
const int maxd = 1000 + 10;
int n;
double d;
struct node{
double l, r;
node(double xx = 0, double yy = 0): l(xx), r(yy){}
};
node ac[maxd];
bool cmp(node a, node b) {
if(a.l != b.l) {
return a.l < b.l;
}
else return a.r > b.r;
}
int main() {
int kase = 0;
while(cin >> n >> d && (n +d)) {
kase ++;
double max_d = -1;
int flag = 1;
for (int i = 0; i < n; i++){
double xx, yy;
cin >> xx >> yy;
if(flag == 0) {
continue;
}
if(yy > d) {
flag = 0;
continue;
}
ac[i].l = xx - sqrt(d*d - yy*yy);
ac[i].r = xx + sqrt(d*d - yy*yy);
}
if(!flag) {
cout << "Case " << kase << ": ";
cout << "-1" << endl;
continue;
}
sort(ac, ac + n, cmp);
double max_r = ac[0].r;
int f = 0;
int ans = 1;
for (int i = 1; i < n; i++) {
if(ac[i].l <= max_r) {
if(ac[i].r < max_r) {
max_r = ac[i].r;
}
}
else{
max_r = ac[i].r;
ans ++;
}
}
cout<<"Case "<<kase<<": "<<ans&
4000
lt;<endl;
}
}
坐标系中有n个小岛,在x轴上设置雷达,雷达范围是d,要求用最少雷达能覆盖所有小岛;
解题思路:
如果直接用x轴上点到岛的距离进行计算,不仅麻烦而且很容易打乱思路,毕竟圆形范围不是很好处理。
我们可以把它转化为x轴上的区间问题,既然岛到雷达距离不能超过d,那么在x轴上必定对应一个范围使得雷达必须在区间内才可以覆盖到岛。
AC代码:#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <bitset>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <fstream>
#include <cstdlib>
#include <sstream>
#include <cstring>
#include <iostream>
#include <algorithm>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
#define maxn 1010
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ms(x,y) memset(x,y,sizeof(x))
#define rep(i,n) for(int i=0;i<(n);i++)
#define repf(i,a,b) for(int i=(a);i<=(b);i++)
#define pii pair<int,int>
//#define mp make_pair
#define FI first
#define SE second
#define IT iterator
#define PB push_back
#define Times 10
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int ,int > P;
const double eps = 1e-10;
const double pi = acos(-1.0);
const ll mod = 1e9+7;
const int inf = 0x3f3f3f3f;
const ll INF = (ll)1e18+300;
const int maxd = 1000 + 10;
int n;
double d;
struct node{
double l, r;
node(double xx = 0, double yy = 0): l(xx), r(yy){}
};
node ac[maxd];
bool cmp(node a, node b) {
if(a.l != b.l) {
return a.l < b.l;
}
else return a.r > b.r;
}
int main() {
int kase = 0;
while(cin >> n >> d && (n +d)) {
kase ++;
double max_d = -1;
int flag = 1;
for (int i = 0; i < n; i++){
double xx, yy;
cin >> xx >> yy;
if(flag == 0) {
continue;
}
if(yy > d) {
flag = 0;
continue;
}
ac[i].l = xx - sqrt(d*d - yy*yy);
ac[i].r = xx + sqrt(d*d - yy*yy);
}
if(!flag) {
cout << "Case " << kase << ": ";
cout << "-1" << endl;
continue;
}
sort(ac, ac + n, cmp);
double max_r = ac[0].r;
int f = 0;
int ans = 1;
for (int i = 1; i < n; i++) {
if(ac[i].l <= max_r) {
if(ac[i].r < max_r) {
max_r = ac[i].r;
}
}
else{
max_r = ac[i].r;
ans ++;
}
}
cout<<"Case "<<kase<<": "<<ans&
4000
lt;<endl;
}
}
相关文章推荐
- POJ 1328 Radar Installation (贪心,区间选点问题)
- POJ 1328 Radar Installation-放置雷达(贪心,区间,二维转一维)
- poj1328 区间贪心 雷达覆盖岛屿
- poj-1328 Radar Installation(贪心+区间选点)
- poj_1328_Radar Installation(区间贪心)
- (一般)POJ-1328 区间贪心,几何
- POJ - 1328-贪心(区间)-Radar Installation
- A - Radar Installation POJ - 1328 (区间贪心)
- POJ1328 Radar Installation (区间选点问题,贪心)
- [ACM] POJ 1328 Radar Installation (贪心,区间选点问题)
- POJ1328 区间选点问题(贪心)
- [POJ 1328] Radar Installation (区间贪心)
- 贪心问题:区间覆盖 POJ 1328 Rader Installation
- Radar Installation POJ - 1328 贪心(区间覆盖)
- [ACM] POJ 1328 Radar Installation (贪心,区间选点问题)
- Radar Installation(POJ 1328 区间贪心)
- Poj 1328 贪心之区间选点+勾股定理**
- POJ - 1328 Radar Installation 安装雷达 贪心+区间选点 重庆一中高2018级竞赛班第八次测试 2016.8.7 Problem 3
- POJ 1328 Radar Installation 区间贪心
- 【贪心专题】POJ 1328 G - Radar Installation (区间覆盖)