-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathbooks1.java
83 lines (73 loc) · 2.26 KB
/
books1.java
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
82
83
import java.io.*;
import java.util.*;
import static java.lang.Integer.parseInt;
public class books1
{
private static BufferedReader f;
public static void main(String[] args) throws IOException {
f = new BufferedReader(new InputStreamReader(System.in));
int T = parseInt(f.readLine());
StringTokenizer st;
int[] books, splits; int num, s, i, j, lo, hi, mid;
while(T-- > 0)
{
st = new StringTokenizer(f.readLine());
num = parseInt(st.nextToken());
s = parseInt(st.nextToken());
st = new StringTokenizer(f.readLine());
books = new int[num];
lo = hi = 0;
for(i = 0; i < num; i++)
{
books[i] = parseInt(st.nextToken());
hi += books[i];
lo = Math.max(lo, books[i]);
}
splits = new int[s-1];
while(lo < hi)
{
mid = (lo+hi)>>1;
if(assign(mid, splits, books)) hi = mid;
else lo = mid+1;
}
assign(lo, splits, books);
System.out.print(books[0]);
j = 0;
for(i = 1; i < num; i++)
{
if(j < splits.length && i == splits[j])
{
System.out.print(" /");
j++;
}
System.out.print(" " + books[i]);
}
System.out.println();
System.out.flush();
}
System.exit(0);
}
private static boolean assign(int num, int[] splits, int[] books)
{
int s = splits.length;
int sum = 0;
for(int i = books.length-1; i >= 0; i--)
{
sum += books[i];
if(sum >= num)
{
if(sum > num) i++;
sum = 0;
if(s <= 0) return false;
splits[--s] = i;
}
if(i == s)
{// assign all books to remaining scribes
for(; i > 0; i--)
splits[--s] = i;
break;
}
}
return s == 0 && sum < num;
}
}