-
Notifications
You must be signed in to change notification settings - Fork 34
/
D.java
47 lines (43 loc) · 1.4 KB
/
D.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
import java.io.PrintStream;
import java.util.*;
/**
* APAC 2015 Round A Problem D: Cut Tiles
* Check README.md for explanation.
*/
public class Main {
public String solve(Scanner scanner) {
int n=scanner.nextInt(), m=scanner.nextInt();
int[] size=new int[32];
for (int i=0;i<n;i++)
size[scanner.nextInt()]++;
int matched=0, ans=0;
while (matched<n) {
ans++;
matched+=find(size, m, m);
}
return String.valueOf(ans);
}
private int find(int[] size, int a, int b) {
if (a<b) {int tmp=a;a=b;b=tmp;}
int index=31;
for (;index>=0;index--) {
if (b<(1<<index) || size[index]==0) continue;
break;
}
if (index==-1) return 0;
size[index]--;
int edge=(1<<index);
return 1+find(size, a-edge, b)+find(size, edge, b-edge);
}
public static void main(String[] args) throws Exception {
System.setOut(new PrintStream("output.txt"));
Scanner scanner=new Scanner(System.in);
int times=scanner.nextInt();
long start=System.currentTimeMillis();
for (int t=1;t<=times;t++) {
System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner)));
}
long end=System.currentTimeMillis();
System.err.println(String.format("Time used: %.3fs", (end-start)/1000.));
}
}