#二分求最大值的最小值算法

先来一道例题:
Before the invention of book-printing, it was very hard to make a copy of a book. All the contents had to be re-written by hand by so called scribers. The scriber had been given a book and after several months he finished its copy. One of the most famous scribers lived in the 15th century and his name was Xaverius Endricus Remius Ontius Xendrianus (Xerox). Anyway, the work was very annoying and boring. And the only way to speed it up was to hire more scribers.
Once upon a time, there was a theater ensemble that wanted to play famous Antique Tragedies. The scripts of these plays were divided into many books and actors needed more copies of them, of course.So they hired many scribers to make copies of these books. Imagine you have m books (numbered1, 2, . . . , m) that may have different number of pages (p1, p2, . . . , pm) and you want to make one copy of each of them. Your task is to divide these books among k scribes, k ≤ m. Each book can be assigned to a single scriber only, and every scriber must get a continuous sequence of books. That means, there exists an increasing succession of numbers 0 = b0 < b1 < b2, . . . < bk−1 ≤ bk = m such that i-th scriber gets a sequence of books with numbers between bi−1 + 1 and bi. The time needed to make a copy of all the books is determined by the scriber who was assigned the most work. Therefore, our goal is to minimize the maximum number of pages assigned to a single scriber. Your task is to find the optimal assignment.
Input
The input consists of N cases. The first line of the input contains only positive integer N. Then follow
the cases. Each case consists of exactly two lines. At the first line, there are two integers m and k,
1 ≤ k ≤ m ≤ 500. At the second line, there are integers p1, p2, . . . , pm separated by spaces. All these
values are positive and less than 10000000.
Output
For each case, print exactly one line. The line must contain the input succession p1, p2, . . . pm divided
into exactly k parts such that the maximum sum of a single part should be as small as possible. Use
the slash character (‘/’) to separate the parts. There must be exactly one space character between any
two successive numbers and between the number and the slash.
If there is more than one solution, print the one that minimizes the work assigned to the first scriber,
then to the second scriber etc. But each scriber must be assigned at least one book.
Sample Input
2
9 3
100 200 300 400 500 600 700 800 900
5 4
100 100 100 100 100
Sample Output
100 200 300 400 500 / 600 700 / 800 900
100 / 100 / 100 / 100 100

题意理解:很显然就是求k+1组的最大值的最小值。思路是二分,因为设最大的数组的和为x,max是a[i]中最大的数,sum是a[1-n]的和,所以max<=x<=sum是必然的,那么我们就可以利用二分来大大减少时间复杂度,从左向右跑。一开始令x=max,y=sum。如果最大的s[i]>x了,。那么x=mid+1,反之y=mid。
再继续mid=x+(y-x)/2

AC代码:

#include
using namespace std;
const int maxn=600;
long long A[maxn],n,m,num[maxn];
bool vis[maxn];
bool libm(int x)
{
	long long t=0,s=0;
	bool ok=true;
	for(long long i=0;i<n;i++)
	{
		if(A[i]>x) 
		{
			ok=false;
			break;
		}
		if(s+A[i]>x)
		{
			t++;
			s=A[i];
			if(t>m-1)
			{
				ok=false;break;
			}
		}else
		{
			s+=A[i];
		}
	}
	return ok;
}
long long sum()
{
	long long s=0;
	for(long long i=0;i<n;i++) s+=A[i];
	return s;
}
 
 
int main()
{
	long long LIBM;
	cin>>LIBM;
	while(LIBM--){
		memset(vis,0,sizeof(vis));
		long long max=-1;
		cin>>n>>m;
		for(long long i=0;i<n;i++) 
		{
			cin>>A[i];
			if(A[i]>=max) max=A[i];
		}
		long long x=max,y=sum(),mid; 
		while(x<y)
		{
			mid=x+(y-x)/2;
			if(libm(mid)) y=mid;
			else x=mid+1;
		}
		long long res=x;
		long long sum=0,k=0;
		for(long long i=n-1;i>=0;i--){
			if(sum+A[i]<=res){
				sum+=A[i];
			}else{
				sum=A[i];
				k++;
				vis[i]=true;
			}
		}
		for(long long i=0;i<n&&k<m-1;i++){
			if(!vis[i]){
				vis[i]=true;
				k++;
			}
		}
		for(long long i=0;i<n-1;i++){
			cout<<A[i]<<" ";
			if(vis[i]) cout<<"/ ";
		}
		cout<<A[n-1]<<endl;
	}
	return 0;
}

你可能感兴趣的:(二分查找算法)