-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy path1505CopyingBooks.cpp
More file actions
69 lines (68 loc) · 1.38 KB
/
1505CopyingBooks.cpp
File metadata and controls
69 lines (68 loc) · 1.38 KB
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
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<utility>
#include<queue>
#include<limits.h>
using namespace std;
typedef long long LL;
LL p[500];
int k,m;
inline bool check(LL len){
//greedy, cnt<=m
LL cur = 0;
int cnt = 0;
for(int i=0;i<k;i++){
if(p[i]>len) return false;
if(cur+p[i]>len){
cur = p[i];
cnt++;
}else cur+=p[i];
}
cnt++;
return cnt<=m;
}
int cut[500];
void print_answer(LL len){
LL cur = 0;
int cnt = 0;
cut[k-1] = 1;
for(int i=k-1;i>=0;i--){
if(cur+p[i]>len){
cur = p[i];
cut[i] = 1;
cnt++;
}else cur+=p[i];
}
cnt++;
//assign one book
for(int i=0;i<k && cnt<m; i++)
if(!cut[i]) cut[i] = 1, cnt++;
for(int i=0;i<k;i++){
printf("%lld",p[i]);
if(i!=k-1)printf(" ");
else printf("\n");
if(cut[i] && i!=k-1) printf("/ ");
}
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&k,&m);
LL l = 0, r = 0;
for(int i=0;i<k;i++)
scanf("%lld",p+i),r+=p[i];
LL mid;
while(l+1<r){
mid = (l+r)>>1;
if(check(mid)) r = mid;
else l = mid;
}
//printf("%lld\n",r);
memset(cut,0,sizeof(cut));
print_answer(r);
}
return 0;
}