Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description
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 calledscribers. 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 havem books (numbered ) that may have different number of pages ( ) and you want to make one copy of each of them. Your task is to divide these books among k scribes, . 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 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, . At the second line, there are integers 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 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
Miguel A. Revilla
2000-02-15
题意:给定m个数,划分为k组数,使得每组数之和的最大值最小,如果有多种解,输出第一组尽量小的解,如果仍有多组解,第二组应尽量小
思路:最大值最小或者最小值最大类的问题,我们常常采用二分答案的方法,对于给定的一个分组之和S,我们很容易去判断是否存在解能够将m个数划分为k个数使得每组之和小于S,因此我们只需要去二分S,需找最优解即可
那么如何去判断当前S是否有解呢?我们只需要采用贪心策略,尽量将一组分到最接近S,超出S后再划分新的一组,计算分组数和k做比较即可
确定最优解S之和,我们就需要采用贪心策略去构造解,因为要求第一组尽量小,所以我们从后往前贪心,使得最后几组尽量大,这里要注意的是这种贪心策略可能使得分组小于k,如题目给出的第二组数据,所以我们需要判断现在剩余未划分的数字个数和已分组个数与k的关系,当剩下的元素必须单独成为一组的时候就停止贪心,将剩下的每个元素单独分到一组,我们在代码中用一个辅助数组P,P[i]为1表示我们需要在第i个数后面输出一个分组斜杠
代码如下
#include<cstdio>
#include<cstring>
#include<cmath>
typedef long long ll;
const int maxn=505;
int A[maxn];
bool P[maxn];
int main()
{
int N,m,k,i,j;
int cnt,sum;
ll MIN,MAX,M;
scanf("%d",&N);
while(N--)
{
scanf("%d%d",&m,&k);
MAX=0;
memset(P,false,sizeof P);
MIN=-1;
for(i=0;i<m;i++)
{
scanf("%d",&A[i]);
if(MIN<A[i])
MIN=A[i];
MAX+=A[i];
}
while(MIN<MAX)
{
M=(MAX+MIN)>>1;
cnt=1;
sum=0;
for(i=0;i<m;i++)
{
if(sum+A[i]>M)
{
sum=A[i];
cnt++;
}
else
sum+=A[i];
}
if(cnt>k)
{
MIN=M+1;
}
else
{
MAX=M;
}
}
sum=0;
cnt=0;
for(i=m-1;i>=0;i--)
{
if(sum+A[i]>MIN)
{
P[i]=true;
cnt++;
sum=A[i];
}
else
{
sum+=A[i];
}
if(i<=k-1-cnt)
break;
}
for(i=0;i<k-1-cnt;i++)
P[i]=true;
for(i=0;i<m;i++)
{
if(i==0)
printf("%d",A[i]);
else
printf(" %d",A[i]);
if(P[i])
{
printf(" /");
}
}
puts("");
}
return 0;
}
Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description
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 calledscribers. 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 havem books (numbered ) that may have different number of pages ( ) and you want to make one copy of each of them. Your task is to divide these books among k scribes, . 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 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, . At the second line, there are integers 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 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
Miguel A. Revilla
2000-02-15