#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define pii pair<int, int>
#define x first
#define y second
#define long long long
using namespace std;
const int N = 1e5+5;
int k, n, sz;
vector<pii> vec;
long ans;
long dp[2][N];
void solve_median(int z) {
priority_queue<int> Q1;
priority_queue<int, vector<int>, greater<int> > Q2;
long sum1 = 0, sum2 = 0;
for(int i = 0; i < sz; ++i) {
Q1.emplace(vec[i].x), sum1 += vec[i].x;
Q2.emplace(vec[i].y), sum2 += vec[i].y;
if(Q1.top() > Q2.top()) {
int u = Q1.top(), v = Q2.top();
Q1.pop(), Q2.pop(); sum1 += v - u, sum2 += u - v;
Q1.emplace(v), Q2.emplace(u);
}
dp[z][i] = ((i+1ll) * (Q1.top()) - sum1) + (sum2 - (i+1ll) * Q1.top());
}
}
int main() {
scanf("%d %d", &k, &n);
for(int i = 0; i < n; ++i) {
char a[3], b[3]; int c, d;
scanf("%s %d %s %d", a, &c, b, &d);
if(a[0] == b[0]) ans += abs(c - d);
else vec.emplace_back(c, d);
}
sz = vec.size();
if(!sz) return !printf("%lld\n", ans);
ans += sz;
sort(all(vec), [&](const pii &a, const pii &b) {
return a.x + a.y < b.x + b.y;
});
solve_median(0);
if(k == 1) return !printf("%lld\n", ans + dp[0][sz-1]);
reverse(all(vec)); solve_median(1);
long val = dp[0][sz-1];
for(int i = 0; i < sz-1; ++i) val = min(val, dp[0][i] + dp[1][sz-i-2]);
return !printf("%lld\n", ans + val);
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:34:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &k, &n);
~~~~~^~~~~~~~~~~~~~~~~
bridge.cpp:37:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s %d %s %d", a, &c, b, &d);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
356 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
412 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
300 KB |
Output is correct |
9 |
Correct |
3 ms |
424 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
45 ms |
3952 KB |
Output is correct |
13 |
Correct |
78 ms |
5520 KB |
Output is correct |
14 |
Correct |
61 ms |
4204 KB |
Output is correct |
15 |
Correct |
55 ms |
3568 KB |
Output is correct |
16 |
Correct |
41 ms |
4792 KB |
Output is correct |
17 |
Correct |
80 ms |
5484 KB |
Output is correct |
18 |
Correct |
50 ms |
5100 KB |
Output is correct |
19 |
Correct |
72 ms |
5472 KB |
Output is correct |
20 |
Correct |
64 ms |
4972 KB |
Output is correct |
21 |
Correct |
75 ms |
5224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
352 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
29 ms |
384 KB |
Output is correct |
4 |
Correct |
3 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 |
3 ms |
260 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
356 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 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 |
256 KB |
Output is correct |
6 |
Correct |
3 ms |
256 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
276 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
3 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 |
4 ms |
384 KB |
Output is correct |
22 |
Correct |
3 ms |
384 KB |
Output is correct |
23 |
Correct |
3 ms |
384 KB |
Output is correct |
24 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
356 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 |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
404 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
3 ms |
384 KB |
Output is correct |
18 |
Correct |
2 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 |
4 ms |
384 KB |
Output is correct |
22 |
Correct |
3 ms |
384 KB |
Output is correct |
23 |
Correct |
3 ms |
384 KB |
Output is correct |
24 |
Correct |
3 ms |
384 KB |
Output is correct |
25 |
Correct |
69 ms |
4748 KB |
Output is correct |
26 |
Correct |
68 ms |
4876 KB |
Output is correct |
27 |
Correct |
91 ms |
5668 KB |
Output is correct |
28 |
Correct |
111 ms |
6276 KB |
Output is correct |
29 |
Correct |
109 ms |
6408 KB |
Output is correct |
30 |
Correct |
52 ms |
3592 KB |
Output is correct |
31 |
Correct |
58 ms |
5644 KB |
Output is correct |
32 |
Correct |
100 ms |
6280 KB |
Output is correct |
33 |
Correct |
87 ms |
5920 KB |
Output is correct |
34 |
Correct |
96 ms |
6248 KB |
Output is correct |
35 |
Correct |
55 ms |
5800 KB |
Output is correct |
36 |
Correct |
96 ms |
6068 KB |
Output is correct |
37 |
Correct |
38 ms |
4748 KB |
Output is correct |