题意:有n个泥水沟,要你用L的板子把所有沟覆盖。
分析:贪心。每次把当前的沟覆盖,若之前的木板已把当前的沟覆盖则忽略,否则覆盖部分或完全没覆盖,需要考虑的是剩下沟的长度如何覆盖完全。
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct node{
int st,ed;
bool operator<(const node &p)const{
return st<p.st;
}
};
const int MAX=10005;
node p[MAX];
int main(){
int n,L,i,lastpos,ans,len,tmp;
scanf("%d%d",&n,&L);
for(i=0;i<n;i++)
scanf("%d%d",&p[i].st,&p[i].ed);
sort(p,p+n);
for(lastpos=-1,ans=0,i=0;i<n;i++){//起始时上一块木板的末坐标很小。
if(lastpos>=p[i].ed)
continue;
if(lastpos>p[i].st){
len=p[i].ed-lastpos;
tmp=(len+L-1)/L;//向上取整,不够L的部分要加一,即把当前的完全覆盖。
lastpos=lastpos+tmp*L;
ans+=tmp;
}
else{
len=p[i].ed-p[i].st;
tmp=(len+L-1)/L;
lastpos=p[i].st+tmp*L;
ans+=tmp;
}
}
printf("%d\n",ans);
return 0;
}