uva 1615 (区间选点)

来源:互联网 发布:unity3d在线培训 编辑:程序博客网 时间:2024/06/18 04:09

这是一个区间选点问题。

所谓的区间选点问题,就是给定一些区间让你选最少的点使任何一个区间都至少含有一个点。

这可以用贪心策略来求解。

#include <cstdio>#include <algorithm>#include <cmath>#define eps 1e-7using namespace std;const int maxn = 10000+10;struct village {  double o,l;  bool operator < (const village& b) const {    return l<b.l || fabs(l-b.l)<=eps && o > b.o;  } }a[maxn];int main(){int L, D;double u, v, t;int n;while(scanf("%d%d%d",&L,&D,&n) != EOF) {  for(int i(0); i < n; i++) {    scanf("%lf%lf",&u,&v);    t = sqrt(D*D - v*v);    a[i].o = max(u-t,0.0); a[i].l = min(u+t,(double)L);  }  sort(a,a+n);  //for(int o=0;o<n;o++) printf("(%lf,%lf) ",a[o].o,a[o].l);  int cnt(1); double pos = a[0].l;  for(int i(0); i < n; i++) {if(pos<a[i].o) {pos=a[i].l;cnt++;}  }  printf("%d\n",cnt);}}

0 0
原创粉丝点击