Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

详细二分题解吧,附代码

Posted by JRZ at 2018-04-24 11:16:44 on Problem 3258
我WA了三次没找到错误,结果居然是INF不够大
二分思路很明确,就是二分来贪婪的找最大值,C(x)来判断该值是否可行
详细注释在代码里了,希望可以帮到后面的同学
/*AC*/
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cmath>
#define INF 99999999999
#define MAX_N 51000
using namespace std;
typedef long long ll;
ll L,dis[MAX_N];
int M,N;
bool C(int x){
	int st=0,ed=st+1,sum=0;
	for(;ed<=N+1;ed++){//遍历所有区间 
		if((dis[ed]-dis[st])>=x){//如果这个区间长度符合要求 
			st=ed;//开始检测后面的区间 
			sum++;//记下这个区间 
		}
	}
	if(sum>=(2+N-M-1)) return true;//若区间数>=去掉M块石头后的区间数则x可行 
	else return false;
}
int main()
{
	while(cin>>L>>N>>M)
	{
		int lb=0,ub=INF;
		dis[0]=0;
		for(int i=1;i<N+1;i++){
			cin>>dis[i];
		}
		dis[N+1]=L;
		if(N==0&&M==0) cout<<L<<endl;
		else{
			sort(dis,dis+N+1);
			for(int i=0;i<100;i++){//二分法返回最大长度 
				int mid=(lb+ub)/2;
				if(C(mid)) lb=mid;
				else ub=mid;
			}
			cout<<lb<<endl;
		}
	}
	return 0;
}
如可读性不佳可移步UbuntuPastbin:https://paste.ubuntu.com/p/gKknxZcYxc/

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator