Accept: 2669 Submit: 22797
Time Limit: 3000 mSec
Problem 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 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. Onceuponatime,therewasatheaterensemblethatwantedtoplayfamousAntiqueTragedies. 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 (numbered 1,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
Sample Input
Sample Output
100 200 300 400 500 / 600 700 / 800 900
100 / 100 / 100 / 100 100
题解:最大化最小值,目前遇到的题不是贪心就是二分,这个题明显二分答案。输出格式需要注意一下,因为要字典序最小所以尽量让后面的数凑在一起,这样前面的数就能尽量分散。如果当前剩余分块数等于剩余数字个数,直接break输出即可。
#include <bits/stdc++.h> using namespace std;
typedef long long LL; const int maxn = + ;
const LL INF = 1e15; int n, m;
LL num[maxn]; int Check(LL x) {
LL tmp = ;
int cnt = ;
for (int i = ; i < n; i++) {
if (tmp + num[i] <= x) tmp += num[i];
else tmp = num[i], cnt++;
} return cnt;
} bool should_put[maxn]; void output(LL x) {
memset(should_put, false, sizeof(should_put));
LL tmp = ;
int i, cnt = ;
for (i = n - ; i >= ; i--) {
if (tmp + num[i] <= x) tmp += num[i];
else {
should_put[i] = true;
tmp = num[i];
cnt++;
}
if (i == m - cnt) break;
} if (i != -) {
for (int j = ; j < i; j++) {
should_put[j] = true;
}
}
for (int i = ; i < n - ; i++) {
printf("%lld ", num[i]);
if (should_put[i]) printf("/ ");
}
printf("%lld\n", num[n - ]);
} int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int iCase;
scanf("%d", &iCase);
while (iCase--) {
scanf("%d%d", &n, &m);
LL sum = , Max = -INF;
for (int i = ; i < n; i++) {
scanf("%lld", &num[i]);
sum += num[i];
Max = Max > num[i] ? Max : num[i];
}
LL ans = ;
LL l = Max, r = sum;
while (l <= r) {
LL mid = (l + r) / ;
if (Check(mid) <= m) {
ans = mid;
r = mid - ;
}
else l = mid + ;
} //printf("%lld\n", l); output(l);
}
return ;
}