题意:有一个矩形,n个圆。已知矩形的长宽和圆的半径,问最少需多少个圆将矩形完全覆盖。
分析:
1、首先求圆与矩形的长的交点,若无交点,则一定不能对用最少的圆覆盖矩形有贡献。
2、如果两个圆与矩形相交所得的线段重合,那这两个圆一定能把矩形在两线段并集的那部分所覆盖。问题转化为用圆与矩形相交所得的线段覆盖矩形的长。
3、按线段左端点排序,对于某个已选择的线段a,求它后面满足b.L <= a.R的线段b的b.R的最大值,依次类推。
#include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<iostream> #include<sstream> #include<iterator> #include<algorithm> #include<string> #include<vector> #include<set> #include<map> #include<stack> #include<deque> #include<queue> #include<list> #define lowbit(x) (x & (-x)) const double eps = 1e-8; inline int dcmp(double a, double b){ if(fabs(a - b) < eps) return 0; return a > b ? 1 : -1; } typedef long long LL; typedef unsigned long long ULL; const int INT_INF = 0x3f3f3f3f; const int INT_M_INF = 0x7f7f7f7f; const LL LL_INF = 0x3f3f3f3f3f3f3f3f; const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f; const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1}; const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1}; const int MOD = 1e9 + 7; const double pi = acos(-1.0); const int MAXN = 10000 + 10; const int MAXT = 10000 + 10; using namespace std; struct Node{ double pos, r; double L, R; bool operator < (const Node& rhs)const{ return L < rhs.L; } }num[MAXN]; int main(){ int n; double l, w; while(scanf("%d%lf%lf", &n, &l, &w) == 3){ double pos, r; int cnt = 0; for(int i = 0; i < n; ++i){ scanf("%lf%lf", &pos, &r); double tmpl = r * r - w * w / 4.0; if(dcmp(tmpl, 0.0) <= 0) continue; ++cnt; num[cnt].pos = pos; num[cnt].r = r; num[cnt].L = pos - sqrt(tmpl); num[cnt].R = pos + sqrt(tmpl); } sort(num + 1, num + cnt + 1); int ans = 0; double st = 0.0; bool ok = false; for(int i = 1; i <= cnt; ++i){ if(dcmp(st, l) >= 0){ ok = true; break; } ++ans; if(num[i].L > st){ ok = false; break; } double ma = -1.0; int j; for(j = i; j <= cnt; ++j){ if(dcmp(num[j].L, st) <= 0){ ma = max(ma, num[j].R); } else break; } st = ma; i = j - 1; } if(dcmp(st, l) >= 0){ ok = true; } if(ok){ printf("%d\n", ans); } else{ printf("-1\n"); } } return 0; }
时间: 2024-10-05 09:39:31