forked from t3nsor/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 2
/
books1.cpp
81 lines (81 loc) · 1.26 KB
/
books1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// 2008-08-20
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
int N,M;
int pages[500];
bool ok(long long x)
{
int next=0;
int i;
for (i=0; i<M; i++)
{
long long sum=0;
do
{
sum+=pages[next];
if (sum<=x)
next++;
}
while (next<N&&sum<=x);
if (next==N&&sum<=x)
return true;
}
return false;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("books.in","r",stdin);
freopen("books.out","w",stdout);
#endif
int t,i,j;
scanf("%d",&t);
int books[500];
int work[500][500];
while (t--)
{
scanf("%d %d",&N,&M);
memset(books,0,sizeof(books));
long long total=0;
int _m=0;
for (i=0; i<N; i++)
{
scanf("%d",pages+i);
total+=pages[i];
_m=max(_m,pages[i]);
}
long long l=max((total+M-1)/M,(long long)_m);
long long u=total;
while (u>l)
{
long long m=(l+u)/2;
if (ok(m))
u=m;
else
l=m+1;
}
int next=N-1;
for (i=M-1; i>=0; i--)
{
long long sum=0;
for(;;)
{
sum+=pages[next];
if (sum<=l&&next>=i)
work[i][books[i]++]=pages[next--];
else
break;
}
}
for (i=0; i<M-1; i++)
{
for (j=books[i]-1; j>=0; j--)
printf("%d ",work[i][j]);
printf("/ ");
}
for (i=books[M-1]-1; i; i--)
printf("%d ",work[M-1][i]);
printf("%d\n",work[M-1][0]);
}
}