zoukankan      html  css  js  c++  java
  • UVA 10382 Watering Grass (区间覆盖,贪心)

    问题可以转化为草坪的边界被完全覆盖。这样一个圆形就换成一条线段。

    贪心,从中选尽量少的线段把区间覆盖,按照把线段按左端点排序,记录一个当前已经覆盖区间的位置cur,

    从左端点小于等于cur选一个右端点最大的作为这次选的区间,如果没有符合条件的,说明不可能完全覆盖。

    r*r会爆int...

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 1e4+5;
    int n,l,w;
    struct seg
    {
        double l,r;
        bool operator < (const seg& rh)const {
            return l < rh.l;
        }
    }L[maxn];
    
    
    int main()
    {
        //freopen("in.txt","r",stdin);
        while(~scanf("%d%d%d",&n,&l,&w)){
            double d = w*w/4.;
            int ss = 0;
            for(int i = 0; i < n; i++){
                int p,r; scanf("%d%d",&p,&r);
                if(2*r<=w) {
                    continue;
                }
                double dx = sqrt(1.*r*r-d);
                L[ss].l = p-dx; L[ss++].r = p+dx;
            }
            sort(L,L+ss);
            double cur = 0.; bool fg = false;
            int ans = 0;
    
            for(int i = 0; i < ss;){
                int j = i;
                double nxt = cur;
                while(j<ss && L[j].l <= cur) {
                    if(L[j].r > nxt) nxt = L[j].r;
                    j++;
                }
                if(j == i) break;
                ans++;
                cur = nxt;
                i = j;
                if(cur>=l){ fg = true; break; }
            }
    
            if(fg) printf("%d
    ",ans);
            else puts("-1");
        }
        return 0;
    }
  • 相关阅读:
    Graphic
    GUI编程实战
    Swing 混合布局
    运算符与数据库函数
    mysq基础操作
    mysql常见问题处理
    static 与final abstract关键字
    JAVA面试
    Swing
    AWT的应用
  • 原文地址:https://www.cnblogs.com/jerryRey/p/4809934.html
Copyright © 2011-2022 走看看