标签:
链接:http://bak.vjudge.net/problem/UVA-1615
分析:以村庄为圆心,D为半径作圆,可选区间就是高速公路在圆内的部分(包括两个交点),按区间右端点从小到大,如若右端点相同再按左端点从大到小排好序,接下来就是很纯粹的区间选点问题。
1 #include <cstdio> 2 #include <algorithm> 3 #include <cmath> 4 using namespace std; 5 6 const int maxn = 1e5+5; 7 8 struct Interval { 9 int l, r; 10 bool operator < (const Interval& rhs) const { 11 return r < rhs.r || (r == rhs.r && l > rhs.l); 12 } 13 } itv[maxn]; 14 15 int L, D, N; 16 17 int main() { 18 while (scanf("%d%d%d", &L, &D, &N) == 3) { 19 double dis = D; 20 for (int i = 0; i < N; i++) { 21 double a, b; scanf("%lf%lf", &a, &b); 22 double d = sqrt(dis*dis - b*b); 23 itv[i].l = a - d; itv[i].r = a + d; 24 } 25 sort(itv, itv + N); 26 int ans = 0; 27 double curPos = -1e9; 28 for (int i = 0; i < N; i++) 29 if (curPos < itv[i].l) { 30 curPos = itv[i].r < L ? itv[i].r : L; 31 ans++; 32 } 33 printf("%d\n", ans); 34 } 35 return 0; 36 }
标签:
原文地址:http://www.cnblogs.com/XieWeida/p/5898482.html