bzoj1689: [Usaco2005 Open] Muddy roads 泥泞的路

把水坑按初始位置排序·,然后简单模拟,具体方式看代码。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct water
{
    int from;
    int to;
}a[100005];
int cmp(water x,water y)
{
    if(x.from!=y.from)
    return x.from<y.from;
    return x.to<y.to;
}
int main()
{
    int n,l,ans=0;
    cin>>n>>l;
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&a[i].from,&a[i].to);

    }
    sort(a+1,a+n+1,cmp);
    int now=0;
    for(int i=1;i<=n;i++)
    {
        if(now>a[i].to)
        continue;
        now=max(now,a[i].from);
        if((a[i].to-now)%l==0)
        {ans+=(a[i].to-now)/l;
        now=a[i].to;
        }
        else
        {
            ans+=(a[i].to-now)/l+1;
            now+=((a[i].to-now)/l+1)*l;
        }
        //cout<<now<<" ";
    }
    cout<<ans;
}
相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页