-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKattis-FreeWeights.java
More file actions
110 lines (96 loc) · 3.11 KB
/
Kattis-FreeWeights.java
File metadata and controls
110 lines (96 loc) · 3.11 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import java.util.*;
public class FreeWeights {
public static void main(String... Args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
ArrayList<Integer> r1 = new ArrayList<Integer>();
ArrayList<Integer> r2 = new ArrayList<Integer>();
HashSet<Integer> order1 = new HashSet<Integer>();
HashSet<Integer> order2 = new HashSet<Integer>();
ArrayList<Integer> r3 = new ArrayList<Integer>();
ArrayList<Integer> r4 = new ArrayList<Integer>();
HashSet<Integer> hs1 = new HashSet<Integer>();
int x =0 ;
// Read in
for(int i = 0; i<n;i++){
x = sc.nextInt();
if (hs1.contains(x)){
hs1.remove((Integer) x);
}else{
hs1.add((Integer) x);
}
r1.add((Integer) x);
}
// Read in
for(int i = 0; i<n;i++){
x = sc.nextInt();
r2.add((Integer)x);
}
// Largest Standalone
int standMax = 0;
if(hs1.size()!=0){
standMax = Collections.max(hs1);
}
// Find the binary search beginning point
int m1 = Collections.max(r1);
int m2 = Collections.max(r2);
int max = m2;
if(m1>m2){
max = m1;
}
int min = 0;
int searchPoint = (max-min)/2;
//ArrayList<Integer> temp = new ArrayList<Integer>();
//ArrayList<Integer> temp2 = new ArrayList<Integer>();
int temp3 = -1;
r3 = (ArrayList)r1.clone();
r4 = (ArrayList)r2.clone();
ArrayList<Integer> a = new ArrayList<Integer>();
while (temp3 != searchPoint){
temp3 = searchPoint;
//temp = rCD(r3,searchPoint);
//temp2 = rCD(r2,searchPoint);
if((rCD(r3,searchPoint).size() == 0) && (rCD(r4,searchPoint).size() == 0)){
max = searchPoint;
searchPoint = (max-min)/2 + min;
// System.out.println("low");
}else {
min = searchPoint+1;
searchPoint = max -(max-min)/2;
//System.out.println("high");
}
r3 = (ArrayList)r1.clone();
r4 = (ArrayList)r2.clone();
a = new ArrayList<Integer>();
//System.out.println(searchPoint);
}
System.out.println(temp3);
sc.close();
}
static ArrayList<Integer> rCD (ArrayList<Integer> a, Integer sp) {
ArrayList<Integer> a2b = new ArrayList<Integer>();
ArrayList<Integer> b = new ArrayList<Integer>();
String[] p = new String[0];
List<String> l = new ArrayList<String>();
for (Integer j:
a) {
if(j>sp){
a2b.add(j);
}
}
int i = 0;
int n = a2b.size();
while(i< n-1){
if(a2b.get(i).equals(a2b.get(i+1))){
i ++;
}else {
b.add(a2b.get(i));
}
i++;
}
if(i == n-1){
b.add(a2b.get(i));
}
return b;
}
}