ZSOI2012 选数排序 二分答案

考虑DP发现空间爆了,一般都是要反过来考虑二分答案的,于是显然

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#define LL long long
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
inline LL read()
{
	LL d=0,f=1;char s=getchar();
	while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
	while(s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();}
	return d*f;
}
#define N 500005
#define MAX 1000000001
int a[N];
int n,R,C;

bool check(int len)
{
	int sum=0,i=1;
	while(i+C-1<=n)
	{
		if(a[i+C-1]-a[i]<=len)
		{
			sum++;
			i+=C;
		}else i++;
	}
	if(sum<R)return 0;
	return 1;
}

int EF(int l,int r)
{
	int ret=-1;
	while(l<=r)
	{
		int mid=(l+r)>>1;
		if(check(mid))
		{
			ret=mid;
			r=mid-1;
		}else l=mid+1;
	}
	return ret;
}

int main()
{
	freopen("pick.in","r",stdin);
	freopen("pick.out","w",stdout);
	n=read(),R=read(),C=read();
	fo(i,1,n)a[i]=read();
	sort(a+1,a+n+1);
	int ans=EF(0,MAX);
	cout<<ans<<endl;
	return 0;
}


你可能感兴趣的:(二分答案)