bzoj 1623: [Usaco2008 Open]Cow Cars 奶牛飞车【排序+贪心】

从小到大排个序,然后能选就选

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=50005;
int n,m,d,l,ans,a[N];
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(),m=read(),d=read(),l=read();
    for(int i=1;i<=n;i++) 
		a[i]=read();
    sort(a+1,a+n+1);
    for(int i=1;i<=n;i++)
		if(a[i]-ans/m*d>=l) 
			ans++;
    printf("%d\n",ans);
    return 0;
 }
posted @ 2018-05-05 11:48  lokiii  阅读(84)  评论(0编辑  收藏  举报