forked from sherxon/AlgoDS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPractice4.java
More file actions
72 lines (59 loc) · 1.84 KB
/
Copy pathPractice4.java
File metadata and controls
72 lines (59 loc) · 1.84 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
70
71
72
package codejam2018;
import java.util.*;
/**
* Why Did you create this class? what does it do?
*/
public class Practice4 {
static long k = 0;
static long y = 0;
static long z = 0;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < t; i++) {
builder.append("Case #").append(i + 1).append(": ");
long n = in.nextLong();
k = in.nextLong();
y = z = 0;
go(new Pair(1, n));
builder.append(y).append(" ").append(z).append("\n");
}
System.out.println(builder);
}
private static class Pair implements Comparable<Pair> {
long from, to;
public Pair(long from, long to) {
this.from = from;
this.to = to;
}
@Override public int compareTo(Pair p) {
long diff = (to - from) - (p.to - p.from);
if (diff > 0)
return -1;
else if (diff < 0)
return 1;
return (p.from - from) < 0 ? 1 : -1;
}
}
private static void go(Pair pair1) {
if (k <= 0)
return;
Queue<Pair> lf = new PriorityQueue<>();
lf.add(pair1);
while (!lf.isEmpty()) {
k--;
Pair pair = lf.remove();
long mid = pair.from + (pair.to - pair.from) / 2;
if (k == 0 || pair.to < pair.from) {
y = Math.max(mid - pair.from, pair.to - mid);
z = Math.min(mid - pair.from, pair.to - mid);
return;
}
Pair left = new Pair(pair.from, mid - 1);
Pair right = new Pair(mid + 1, pair.to);
lf.add(left);
lf.add(right);
}
}
}