zoukankan      html  css  js  c++  java
  • bzoj1689[Usaco2005 Open] Muddy roads 泥泞的路*

    bzoj1689[Usaco2005 Open] Muddy roads 泥泞的路

    题意:

    数轴上n个互不覆盖的区间,问要用多少个长为L的线段覆盖。n≤10000

    题解:

    排序区间,然后从每个区间左端点开始铺木板,如果最后一块木板能够铺到下一个区间就铺,以此类推。

    代码:

     1 #include <cstdio>
     2 #include <algorithm>
     3 #include <cstring>
     4 #define maxn 10100
     5 #define inc(i,j,k) for(int i=j;i<=k;i++)
     6 using namespace std;
     7 
     8 inline int read(){
     9     char ch=getchar(); int f=1,x=0;
    10     while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
    11     while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    12     return f*x;
    13 }
    14 struct rg{int l,r;}; rg rgs[maxn]; int n,l,ans;
    15 bool cmp(rg a,rg b){return a.l<b.l;}
    16 int main(){
    17     n=read(); l=read(); 
    18     inc(i,1,n)rgs[i].l=read(),
    19     rgs[i].r=read()-1;
    20     sort(rgs+1,rgs+1+n,cmp); int i=1;
    21     while(i<=n){
    22         ans+=(rgs[i].r-rgs[i].l+l)/l; int rem=(rgs[i].r-rgs[i].l+1)%l; if(rem)rem=l-rem;
    23         while(i<n&&rem>=rgs[i+1].r-rgs[i].r)rem-=(rgs[i+1].r-rgs[i].r),i++;
    24         if(i==n)break; if(rem>=rgs[i+1].l-rgs[i].r)rgs[i+1].l+=rem-(rgs[i+1].l-rgs[i].r)+1;
    25         i++;
    26     }
    27     printf("%d",ans); return 0;
    28 }

    20160729

  • 相关阅读:
    tcp/心跳包
    TCP协议中的三次握手和四次挥手(图解)
    http 中get和post
    xmpp总结
    IOS中http请求使用cookie
    sdwebimage总结
    iOS断言
    Object-C自定义对象NSLog输入信息
    NSTimer你真的会用了吗
    ios中block中的探究
  • 原文地址:https://www.cnblogs.com/YuanZiming/p/5719828.html
Copyright © 2011-2022 走看看