#include <cstdio>
#include <algorithm>
#include <functional>
using namespace std;
const greater<int> gr;
struct Median {
int sz;
int mn[100007];
int mx[100007];
void clear() { sz = 0; }
long long insert(int a, int b, long long d) { // a <= b
if (!sz) return sz++, d + (*mx = b) - (*mn = a);
if (b < *mn) {
d += *mn * 2 - a - b;
mx[sz] = *mn;
pop_heap(mn, mn + sz);
mn[sz - 1] = a; mn[sz] = b;
push_heap(mn, mn + sz);
} else if (a > *mx) {
d += a + b - *mx * 2;
mn[sz] = *mx;
pop_heap(mx, mx + sz, gr);
mx[sz - 1] = a; mx[sz] = b;
push_heap(mx, mx + sz, gr);
} else {
d += b - a;
mn[sz] = a; mx[sz] = b;
}
push_heap(mn, mn + ++sz);
push_heap(mx, mx + sz, gr);
return d;
}
} med;
struct Point {
int a, b;
bool operator<(const Point& pt) const { return a + b < pt.a + pt.b; }
} pt[100003];
int g[200003];
long long ansl[100003];
int main() {
int K, N;
scanf("%d%d", &K, &N);
char ca[3], cb[3];
if (K == 1) {
int a, b, tN = 0;
long long ans = 0;
for (int i = 0; i < N; i++) {
scanf("%s%d%s%d", ca, &a, cb, &b);
if (*ca == *cb) ans += abs(b - a);
else g[tN++] = a, g[tN++] = b, ans++;
}
nth_element(g, g + (tN / 2), g + tN);
for (int i = 0; i < tN / 2; i++) ans += g[tN / 2] - g[i];
for (int i = tN / 2; i < tN; i++) ans += g[i] - g[tN / 2];
printf("%lld", ans);
return 0;
}
int tN = 0;
long long pre = 0;
for (int i = 0; i < N; i++) {
auto& z = pt[tN];
scanf("%s%d%s%d", ca, &z.a, cb, &z.b);
if (z.a > z.b) swap(z.a, z.b);
if (*ca == *cb) pre += z.b - z.a;
else pre++, tN++;
}
if (tN) {
sort(pt, pt + tN);
med.clear();
for (int i = 0; i < tN; i++) ansl[i + 1] = med.insert(pt[i].a, pt[i].b, ansl[i]);
long long ans = ansl[tN], now = 0;
med.clear();
for (int i = tN - 1; i >= 0; i--) {
now = med.insert(pt[i].a, pt[i].b, now);
if (now + ansl[i] < ans) ans = now + ansl[i];
}
printf("%lld", ans + pre);
} else printf("%lld", pre);
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:48:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &K, &N);
~~~~~^~~~~~~~~~~~~~~~
bridge.cpp:54:12: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s%d%s%d", ca, &a, cb, &b);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:69:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s%d%s%d", ca, &z.a, cb, &z.b);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
256 KB |
Output is correct |
9 |
Correct |
2 ms |
256 KB |
Output is correct |
10 |
Correct |
3 ms |
256 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
320 KB |
Output is correct |
3 |
Correct |
3 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
256 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
26 ms |
1152 KB |
Output is correct |
13 |
Correct |
46 ms |
1144 KB |
Output is correct |
14 |
Correct |
31 ms |
1024 KB |
Output is correct |
15 |
Correct |
27 ms |
768 KB |
Output is correct |
16 |
Correct |
32 ms |
1024 KB |
Output is correct |
17 |
Correct |
56 ms |
1016 KB |
Output is correct |
18 |
Correct |
38 ms |
1016 KB |
Output is correct |
19 |
Correct |
44 ms |
1144 KB |
Output is correct |
20 |
Correct |
37 ms |
1124 KB |
Output is correct |
21 |
Correct |
48 ms |
1016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
3 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
256 KB |
Output is correct |
9 |
Correct |
2 ms |
256 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
256 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
256 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
3 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
384 KB |
Output is correct |
20 |
Correct |
4 ms |
384 KB |
Output is correct |
21 |
Correct |
3 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
256 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
256 KB |
Output is correct |
18 |
Correct |
2 ms |
256 KB |
Output is correct |
19 |
Correct |
3 ms |
384 KB |
Output is correct |
20 |
Correct |
4 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
3 ms |
384 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
3 ms |
384 KB |
Output is correct |
25 |
Correct |
38 ms |
2680 KB |
Output is correct |
26 |
Correct |
54 ms |
2808 KB |
Output is correct |
27 |
Correct |
100 ms |
2680 KB |
Output is correct |
28 |
Correct |
74 ms |
2680 KB |
Output is correct |
29 |
Correct |
75 ms |
2680 KB |
Output is correct |
30 |
Correct |
53 ms |
1528 KB |
Output is correct |
31 |
Correct |
62 ms |
2680 KB |
Output is correct |
32 |
Correct |
92 ms |
2680 KB |
Output is correct |
33 |
Correct |
45 ms |
2652 KB |
Output is correct |
34 |
Correct |
76 ms |
2808 KB |
Output is correct |
35 |
Correct |
46 ms |
2680 KB |
Output is correct |
36 |
Correct |
68 ms |
2652 KB |
Output is correct |
37 |
Correct |
28 ms |
2680 KB |
Output is correct |