BZOJ1650: [Usaco2006 Dec]River Hopscotch 跳石子

  题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1650

  水题,二分枚举答案,和NOIP2015d2t1一模一样……,bzoj上要权限这题#奥妙重重

  贴代码

#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
int L,R,len,ans,mid,n,m;
int f[50005];
bool check(int x){
	int sum=0,last=1;
	for (int i=2;i<=n;i++){
		if (f[i]-f[last]<x)sum++;
		else last=i;
	}
	return sum<=m;
}
void swap(int &x,int &y){
	int t;
	t=x;x=y;y=t;
}
void qsort(int L,int R){
	int i,j,mid;
	i=L;j=R;mid=f[rand()%(R-L+1)+L];
	do{
		while (f[i]<mid) i++;
		while (f[j]>mid) j--;
		if (i<=j) swap(f[i],f[j]),i++,j--;
	} while (i<=j);
	if (i<R) qsort(i,R);
	if (L<j) qsort(L,j);
}
int main(){
//	freopen("1650.in","r",stdin);
//	freopen("1650.out","w",stdout);
	scanf("%d%d%d",&len,&n,&m);
	int bo=0;
	for (int i=1;i<=n;i++) {
		scanf("%d",&f[i]);
		if (f[i]==0) bo=1;
	}
	if (bo==0) f[++n]=0;
	qsort(1,n);
	if (f[n]!=len)f[++n]=len; 
	L=0;R=len;ans=0;
	while (L<=R){
		mid=(L+R)>>1;
		if (check(mid)) ans=mid,L=mid+1;
		else R=mid-1;
	}
	printf("%d",ans);
	return 0;
} 
【写的有漏洞的,欢迎路过大神吐槽】

  2017/1/4 22:11:48

  Ending.



相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页