import java.io.*;
import java.util.*;
public class bridge {
public static PriorityQueue<Long> left; public static PriorityQueue<Long> right;
public static long lSum; public static long rSum; //We have distance = \sum_{i} |S_i - bridge| + |T_i-bridge|
//If we pick the median as the bridge (which is optimal), then there are an equal amount of S_i or T_i > bridge as < bridge
//(if odd, the bridge = median, so difference = 0). Thus, the -x and +x all cancel, leaving distance = rSum - lSum.
public static void main(String[] args) throws IOException {
BufferedReader scan = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer l1 = new StringTokenizer(scan.readLine());
int k = Integer.parseInt(l1.nextToken()); int tot = Integer.parseInt(l1.nextToken()); int num = 0; ArrayList<Integer> idxs = new ArrayList<>();
long[][] arrTemp = new long[tot][4]; long constant = 0; //Distance for same shore house-work pairs.
for (int i = 0;i<tot;i++) {
StringTokenizer st = new StringTokenizer(scan.readLine());
arrTemp[i][0] = st.nextToken().charAt(0)-'A'; arrTemp[i][1] = Long.parseLong(st.nextToken()); arrTemp[i][2] = st.nextToken().charAt(0)-'A'; arrTemp[i][3] = Long.parseLong(st.nextToken());
if (arrTemp[i][0]==arrTemp[i][2]) {
constant += Math.abs(arrTemp[i][1]-arrTemp[i][3]);
} else {
num++; idxs.add(i);
}
}
long[][] arr = new long[num][2]; //Filtered to only the house-work pairs that are not on the same bank.
int idx = 0;
for (int i : idxs) {
arr[idx][0] = arrTemp[i][1]; arr[idx][1] = arrTemp[i][3]; idx++;
}
Arrays.sort(arr,(i,j)-> (int) ((i[0]+i[1])-(j[0]+j[1])));
/*for (int i = 0;i<num;i++) {
System.out.println(arr[i][0]+" "+arr[i][1]);
}*/
clean(); long ans;
long[] prefix = new long[num+1]; //Prefix of all the answers if we have cows 0 to i
for (int i = 0;i<num;i++) {
insert(arr[i][0]); insert(arr[i][1]);
//System.out.println(left+" "+right);
prefix[i+1] = rSum - lSum;
}
ans = prefix[num];
if (k==2) { //Iterate through all possible partitioning spots.
clean();
for (int i = num; i>0; i--) {
insert(arr[i-1][0]); insert(arr[i-1][1]);
ans = Math.min(ans, rSum - lSum + prefix[i - 1]);
}
}
System.out.println(ans+constant+num);
}
public static void insert (long x) {
long median = (left.size()>0 ? left.peek() : 1000000001);
if (x<=median) { //Insert to left
left.add(x); lSum += x;
} else { //Insert to right
right.add(x); rSum += x;
}
//Check sizes. We want left.size = right.size + (0 or 1)
if (right.size()+1<left.size()) { //Add to right
long temp = left.peek();
left.poll(); right.add(temp);
lSum -= temp; rSum += temp;
} else if (left.size()<right.size()) { //Add to left
long temp = right.peek();
right.poll(); left.add(temp);
rSum -= temp; lSum += temp;
}
}
public static void clean () {
left = new PriorityQueue<>((a,b)-> (int) (b-a)); right = new PriorityQueue<>((a,b)-> (int) (a-b));
lSum = 0; rSum = 0;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
26992 KB |
Output is correct |
2 |
Correct |
58 ms |
24952 KB |
Output is correct |
3 |
Correct |
100 ms |
22572 KB |
Output is correct |
4 |
Correct |
141 ms |
25796 KB |
Output is correct |
5 |
Correct |
137 ms |
25984 KB |
Output is correct |
6 |
Correct |
146 ms |
29152 KB |
Output is correct |
7 |
Correct |
136 ms |
25920 KB |
Output is correct |
8 |
Correct |
158 ms |
29524 KB |
Output is correct |
9 |
Correct |
142 ms |
25724 KB |
Output is correct |
10 |
Correct |
132 ms |
25752 KB |
Output is correct |
11 |
Correct |
137 ms |
25976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
24352 KB |
Output is correct |
2 |
Correct |
55 ms |
24808 KB |
Output is correct |
3 |
Correct |
88 ms |
26564 KB |
Output is correct |
4 |
Correct |
139 ms |
25812 KB |
Output is correct |
5 |
Correct |
139 ms |
25804 KB |
Output is correct |
6 |
Correct |
146 ms |
25480 KB |
Output is correct |
7 |
Correct |
139 ms |
25540 KB |
Output is correct |
8 |
Correct |
171 ms |
25536 KB |
Output is correct |
9 |
Correct |
138 ms |
25968 KB |
Output is correct |
10 |
Correct |
131 ms |
31668 KB |
Output is correct |
11 |
Correct |
141 ms |
29604 KB |
Output is correct |
12 |
Correct |
463 ms |
43760 KB |
Output is correct |
13 |
Correct |
846 ms |
52840 KB |
Output is correct |
14 |
Correct |
836 ms |
55548 KB |
Output is correct |
15 |
Correct |
730 ms |
42144 KB |
Output is correct |
16 |
Correct |
352 ms |
50532 KB |
Output is correct |
17 |
Correct |
358 ms |
51008 KB |
Output is correct |
18 |
Correct |
372 ms |
50812 KB |
Output is correct |
19 |
Correct |
867 ms |
52304 KB |
Output is correct |
20 |
Correct |
545 ms |
57924 KB |
Output is correct |
21 |
Correct |
440 ms |
50844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
22968 KB |
Output is correct |
2 |
Correct |
55 ms |
24432 KB |
Output is correct |
3 |
Correct |
61 ms |
23080 KB |
Output is correct |
4 |
Correct |
63 ms |
22924 KB |
Output is correct |
5 |
Correct |
59 ms |
22636 KB |
Output is correct |
6 |
Correct |
67 ms |
22220 KB |
Output is correct |
7 |
Correct |
65 ms |
22504 KB |
Output is correct |
8 |
Correct |
64 ms |
22808 KB |
Output is correct |
9 |
Correct |
65 ms |
29204 KB |
Output is correct |
10 |
Correct |
62 ms |
23132 KB |
Output is correct |
11 |
Correct |
65 ms |
22720 KB |
Output is correct |
12 |
Correct |
62 ms |
22712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
22692 KB |
Output is correct |
2 |
Correct |
55 ms |
22748 KB |
Output is correct |
3 |
Correct |
59 ms |
22876 KB |
Output is correct |
4 |
Correct |
63 ms |
22836 KB |
Output is correct |
5 |
Correct |
61 ms |
22536 KB |
Output is correct |
6 |
Correct |
60 ms |
23160 KB |
Output is correct |
7 |
Correct |
67 ms |
27024 KB |
Output is correct |
8 |
Correct |
65 ms |
22416 KB |
Output is correct |
9 |
Correct |
71 ms |
22700 KB |
Output is correct |
10 |
Correct |
62 ms |
23328 KB |
Output is correct |
11 |
Correct |
61 ms |
24600 KB |
Output is correct |
12 |
Correct |
64 ms |
22424 KB |
Output is correct |
13 |
Correct |
89 ms |
22836 KB |
Output is correct |
14 |
Correct |
123 ms |
26548 KB |
Output is correct |
15 |
Correct |
168 ms |
26000 KB |
Output is correct |
16 |
Correct |
74 ms |
22448 KB |
Output is correct |
17 |
Correct |
84 ms |
22676 KB |
Output is correct |
18 |
Correct |
77 ms |
29016 KB |
Output is correct |
19 |
Correct |
143 ms |
25832 KB |
Output is correct |
20 |
Correct |
150 ms |
26036 KB |
Output is correct |
21 |
Correct |
165 ms |
27516 KB |
Output is correct |
22 |
Correct |
152 ms |
25752 KB |
Output is correct |
23 |
Correct |
152 ms |
25336 KB |
Output is correct |
24 |
Correct |
150 ms |
26076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
23168 KB |
Output is correct |
2 |
Correct |
55 ms |
22356 KB |
Output is correct |
3 |
Correct |
62 ms |
22764 KB |
Output is correct |
4 |
Correct |
63 ms |
23296 KB |
Output is correct |
5 |
Correct |
59 ms |
25296 KB |
Output is correct |
6 |
Correct |
57 ms |
22968 KB |
Output is correct |
7 |
Correct |
61 ms |
22916 KB |
Output is correct |
8 |
Correct |
63 ms |
24756 KB |
Output is correct |
9 |
Correct |
66 ms |
29048 KB |
Output is correct |
10 |
Correct |
68 ms |
29176 KB |
Output is correct |
11 |
Correct |
61 ms |
22756 KB |
Output is correct |
12 |
Correct |
60 ms |
23328 KB |
Output is correct |
13 |
Correct |
91 ms |
23056 KB |
Output is correct |
14 |
Correct |
126 ms |
28628 KB |
Output is correct |
15 |
Correct |
165 ms |
25896 KB |
Output is correct |
16 |
Correct |
69 ms |
24252 KB |
Output is correct |
17 |
Correct |
81 ms |
22192 KB |
Output is correct |
18 |
Correct |
78 ms |
28932 KB |
Output is correct |
19 |
Correct |
137 ms |
25580 KB |
Output is correct |
20 |
Correct |
147 ms |
25632 KB |
Output is correct |
21 |
Correct |
164 ms |
25668 KB |
Output is correct |
22 |
Correct |
150 ms |
27652 KB |
Output is correct |
23 |
Correct |
146 ms |
29804 KB |
Output is correct |
24 |
Correct |
165 ms |
25712 KB |
Output is correct |
25 |
Correct |
568 ms |
49724 KB |
Output is correct |
26 |
Correct |
1348 ms |
46696 KB |
Output is correct |
27 |
Correct |
913 ms |
56928 KB |
Output is correct |
28 |
Correct |
997 ms |
61052 KB |
Output is correct |
29 |
Correct |
1095 ms |
59036 KB |
Output is correct |
30 |
Correct |
875 ms |
47616 KB |
Output is correct |
31 |
Correct |
360 ms |
56964 KB |
Output is correct |
32 |
Correct |
441 ms |
58372 KB |
Output is correct |
33 |
Correct |
442 ms |
54188 KB |
Output is correct |
34 |
Correct |
944 ms |
55968 KB |
Output is correct |
35 |
Correct |
583 ms |
58496 KB |
Output is correct |
36 |
Correct |
527 ms |
56776 KB |
Output is correct |
37 |
Correct |
367 ms |
51052 KB |
Output is correct |