UVA 1615 Highway 区间选点 经典贪心 (SEERC 2005)

来源:互联网 发布:linux切换oracle实例 编辑:程序博客网 时间:2024/05/22 06:54

一定要根据右端点从小到大排序,因为为了保证一次扫过满足每个区间,那么被包含区间应该排在包含它的区间前面。


#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>#include<vector>using namespace std;#define all(x) (x).begin(), (x).end()#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)#define sqr(x) ((x)*(x))typedef long long ll;typedef pair<int, int> pii;const int INF =0x3f3f3f3f;const int maxn= 100000   ;double L,D;int n;struct Seg{    double le,ri;    Seg(){}    Seg(double le,double ri):le(le),ri(ri){}    bool operator<(const Seg g)const    {        return ri<g.ri;    }}a[maxn+10];void cope(int ind,double & x,double &y){     double delta=sqrt(sqr(D)-sqr(y));     a[ind]=Seg( max(0.0,x-delta),min(L,x+delta)    );}void work(){    int ans=0;    double now=-1;    for1(i,n)    {       if(a[i].le<=now)       {           continue;       }       else       {           now=a[i].ri;           ans++;       }    }    printf("%d\n",ans);}int main(){    while(~scanf("%lf%lf",&L,&D))    {        scanf("%d",&n);        double x,y;        for1(i,n)        {            scanf("%lf%lf",&x,&y);            cope(i,x,y);        }        sort(a+1,a+1+n);        work();    }   return 0;}


0 0