#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e5 + 5;
struct type {
char c1; int x1;
char c2; int x2;
} person[maxN];
bool cmp(type a, type b) {
return (a.x1 + a.x2) < (b.x1 + b.x2);
}
int k, n;
long long ans = 0;
void subtask1() {
vector<int> val;
for (int i = 1; i <= n; i++) {
val.push_back(person[i].x1);
val.push_back(person[i].x2);
}
sort(val.begin(), val.end());
int median = val[int(val.size() - 1) / 2];
//cout << median << '\n';
for (int i = 1; i <= n; i++)
ans += abs(person[i].x1 - median) + abs(person[i].x2 - median) + 1;
cout << ans;
return ;
}
void subtask2() {
sort(person + 1, person + n + 1, cmp);
long long res = 1e18;
vector<int> val1, val2;
for (int i = 1; i <= n - 1; i++) {
val1.clear();
val2.clear();
for (int j = 1; j <= i; j++) {
val1.push_back(person[j].x1);
val1.push_back(person[j].x2);
}
for (int j = i + 1; j <= n; j++) {
val2.push_back(person[j].x1);
val2.push_back(person[j].x2);
}
sort(val1.begin(), val1.end());
sort(val2.begin(), val2.end());
long long cal = 0;
int med1 = val1[int(val1.size() - 1) / 2], med2 = val2[int(val2.size() - 1) / 2];
for (auto v: val1)
cal = cal + abs(v - med1);
for (auto v: val2)
cal = cal + abs(v - med2);
res = min(res, cal);
}
cout << res + ans + n;
return ;
}
priority_queue<int> lhs;
priority_queue<int, vector<int>, greater<int>> rhs;
long long pre[maxN], lsum = 0, rsum = 0;
void add(int x) {
int median = (lhs.empty() ? 1000000007 : lhs.top());
if (x <= median) {
lhs.push(x);
lsum += x;
}
else {
rhs.push(x);
rsum += x;
}
if (lhs.size() > rhs.size() + 1) {
rhs.push(lhs.top());
rsum += lhs.top();
lsum -= lhs.top();
lhs.pop();
}
else if (lhs.size() < rhs.size()) {
lhs.push(rhs.top());
lsum += rhs.top();
rsum -= rhs.top();
rhs.pop();
}
}
void subtask3() {
sort(person + 1, person + n + 1, cmp);
for (int i = 1; i <= n; i++) {
add(person[i].x1);
add(person[i].x2);
pre[i] = rsum - lsum;
}
while (!lhs.empty()) lhs.pop();
while (!rhs.empty()) rhs.pop();
lsum = 0; rsum = 0;
long long res = pre[n];
for (int i = n; i >= 1; i--) {
add(person[i].x1);
add(person[i].x2);
res = min(res, pre[i - 1] + rsum - lsum);
}
cout << res + ans + n;
return ;
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> k >> n;
for (int i = 1; i <= n; i++) {
char c1, c2;
int x1, x2;
cin >> c1 >> x1 >> c2 >> x2;
if (c1 == c2) {
ans = ans + abs(x1 - x2);
n--;
i--;
continue;
}
person[i] = {c1, x1, c2, x2};
}
if (n == 0) {
cout << ans;
return 0;
}
if (k == 1) subtask1();
else if (n <= 1000) subtask2();
else subtask3();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2648 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2512 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
15 ms |
4572 KB |
Output is correct |
13 |
Correct |
32 ms |
4956 KB |
Output is correct |
14 |
Correct |
23 ms |
4316 KB |
Output is correct |
15 |
Correct |
20 ms |
4092 KB |
Output is correct |
16 |
Correct |
23 ms |
4824 KB |
Output is correct |
17 |
Correct |
23 ms |
4848 KB |
Output is correct |
18 |
Correct |
26 ms |
4976 KB |
Output is correct |
19 |
Correct |
30 ms |
4820 KB |
Output is correct |
20 |
Correct |
21 ms |
5084 KB |
Output is correct |
21 |
Correct |
27 ms |
5084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2516 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2516 KB |
Output is correct |
4 |
Correct |
1 ms |
2524 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2516 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2512 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
20 ms |
2396 KB |
Output is correct |
14 |
Correct |
66 ms |
2540 KB |
Output is correct |
15 |
Correct |
73 ms |
2392 KB |
Output is correct |
16 |
Correct |
2 ms |
2392 KB |
Output is correct |
17 |
Correct |
19 ms |
2396 KB |
Output is correct |
18 |
Correct |
10 ms |
2396 KB |
Output is correct |
19 |
Correct |
23 ms |
2392 KB |
Output is correct |
20 |
Correct |
24 ms |
2396 KB |
Output is correct |
21 |
Correct |
60 ms |
2532 KB |
Output is correct |
22 |
Correct |
24 ms |
2392 KB |
Output is correct |
23 |
Correct |
18 ms |
2396 KB |
Output is correct |
24 |
Correct |
25 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2488 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2520 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
20 ms |
2396 KB |
Output is correct |
14 |
Correct |
65 ms |
2396 KB |
Output is correct |
15 |
Correct |
77 ms |
2652 KB |
Output is correct |
16 |
Correct |
3 ms |
2392 KB |
Output is correct |
17 |
Correct |
20 ms |
2396 KB |
Output is correct |
18 |
Correct |
11 ms |
2396 KB |
Output is correct |
19 |
Correct |
23 ms |
2396 KB |
Output is correct |
20 |
Correct |
24 ms |
2408 KB |
Output is correct |
21 |
Correct |
61 ms |
2532 KB |
Output is correct |
22 |
Correct |
24 ms |
2396 KB |
Output is correct |
23 |
Correct |
17 ms |
2548 KB |
Output is correct |
24 |
Correct |
24 ms |
2396 KB |
Output is correct |
25 |
Correct |
28 ms |
4568 KB |
Output is correct |
26 |
Correct |
53 ms |
4568 KB |
Output is correct |
27 |
Correct |
69 ms |
5356 KB |
Output is correct |
28 |
Correct |
71 ms |
5940 KB |
Output is correct |
29 |
Correct |
72 ms |
6088 KB |
Output is correct |
30 |
Correct |
36 ms |
4068 KB |
Output is correct |
31 |
Correct |
31 ms |
4860 KB |
Output is correct |
32 |
Correct |
62 ms |
5576 KB |
Output is correct |
33 |
Correct |
53 ms |
5240 KB |
Output is correct |
34 |
Correct |
67 ms |
5752 KB |
Output is correct |
35 |
Correct |
37 ms |
5072 KB |
Output is correct |
36 |
Correct |
57 ms |
5580 KB |
Output is correct |
37 |
Correct |
29 ms |
4300 KB |
Output is correct |