bzoj 1689: [Usaco2005 Open] Muddy roads 泥泞的路【贪心】

按左端点排序,贪心的选即可

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=10005;
int n,l,p,ans;
struct qwe
{
	int x,y;
}a[N];
bool cmp(const qwe &a,const qwe &b)
{
	return a.x<b.x;
}
int read()
{
	int r=0,f=1;
	char p=getchar();
	while(p>'9'||p<'0')
	{
		if(p=='-')
			f=-1;
		p=getchar();
	}
	while(p>='0'&&p<='9')
	{
		r=r*10+p-48;
		p=getchar();
	}
	return r*f;
}
int main()
{
	n=read(),l=read();
	for(int i=1;i<=n;i++)
	   a[i].x=read(),a[i].y=read();
    sort(a+1,a+n+1,cmp);
    for(int i=1;i<=n;i++)
    	if(p<a[i].y)
		{
			p=max(p,a[i].x);
			while(p<a[i].y)
				ans++,p+=l;
		}
    printf("%d",ans);
	return 0;
}
posted @ 2018-05-09 16:10  lokiii  阅读(105)  评论(0编辑  收藏  举报