把水坑按初始位置排序·,然后简单模拟,具体方式看代码。
#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;
}