题意:给定一个L*W的矩形和若干圆,求最少用多少圆能够将矩形区域全部覆盖。若无解输出-1,否则输出最小的数目。
思路:贪心思路,每次设定当前已覆盖的区域最右侧边界x,每次找l<=x,r>=x中r的最大值,计数加一,x更新为r。写的时候一直出错是一直当圆形看,其实最简单的方法就是求出每个圆占据的L*W大矩形区域的小矩形面积l*W,将n个圆转化成n个矩形。(若圆的直径小于等于W,怎该圆无法占据矩形区域,忽略此圆即可)
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstdlib>
#include<sstream>
#include<deque>
#include<stack>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-9;
const int maxn = 10000 + 20;
const int maxt = 300 + 10;
const int mod = 10;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, -1, 1};
const int Dis[] = {-1, 1, -5, 5};
const double inf = 0x3f3f3f3f;
const int MOD = 1000;
int n, m, k;
double L, W;
struct node{
double l;
double r;
bool operator < (const node &no) const{
if(l != no.l) return l < no.l;
return r < no.r;
}
}num[maxn];
int main(){
while(~scanf("%d%lf%lf", &n, &L, &W)){
int cnt = -1;
double p, r, x;
for(int i = 0; i < n; ++i){
scanf("%lf%lf", &p, &r);
if(2 * r <= W) continue;
x = sqrt(r * r - (W / 2.0) * (W / 2.0));
num[++cnt].l = p - x;//计算每个圆在大矩形中占得小矩形的左右边界
num[cnt].r = p + x;
}
sort(num, num + cnt + 1);
if(cnt == -1 || num[0].l > 0){
printf("-1\n"); continue;
}
x = 0;//x表示当前大矩形中已经覆盖好的矩形的最右侧边界
bool ok = true;
int ans = 0;
int cur = 0;
while(cur <= cnt){
int tmpi = -1;
double tmpm = -1;
for(int i = cur; i <= cnt; ++i){
if(num[i].l <= x && num[i].r >= x){
if(num[i].r > tmpm){
tmpm = num[i].r;//记录当前最右侧边界
tmpi = i;//对应第几个圆(小矩形)
}
}
}
if(tmpm == -1){
ok = false; break;
}
++ans;
x = tmpm;//更新最右侧边界
cur = tmpi + 1;
if(x >= L) break;
}
if(x >= L) printf("%d\n", ans);
else printf("-1\n");
}
return 0;
}