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 (numbered
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,
Output
For each case, print exactly one line. The line must contain the input succession
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
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
typedef long long ll;
using namespace std;
ll n, m, num[505], rec[505], sum;
int check(ll x) {
ll s = 0;
int cnt = 0;
for (int i = 0; i < n; i++) {
s += num[i];
if (num[i] > x) return 0;
if (s > x) {
s = num[i];
cnt++;
}
}
if (cnt >= m) {
return 0;
}
return 1;
}
int main() {
int T;
sum = 0;
scanf("%d", &T);
while (T--) {
memset(rec, 0, sizeof(rec));
scanf("%lld %lld", &n, &m);
for (int i = 0; i < n; i++) {
scanf("%lld", &num[i]);
sum += num[i];
}
ll l, r, mid;
l = 0, r = sum;
while (l < r) {
mid = (l + r) / 2;
if (!check(mid)) l = mid + 1;
else r = mid;
}
sum = 0;
int cnt2 = 0;
for (int i = n - 1; i >= 0; i--) {
sum += num[i];
if (sum > l || m - cnt2 - 1 == i + 1) {
sum = num[i];
rec[i] = 1;
cnt2++;
}
}
printf("%lld", num[0]);
if (rec[0]) printf(" /");
for (int i = 1 ; i < n; i++) {
printf(" %lld", num[i]);
if (rec[i]) printf(" /");
}
printf("\n");
}
return 0;
}
原文地址:http://blog.csdn.net/llx523113241/article/details/44160649