#include <bits/stdc++.h>
#define FOR(i,j,k) for(int i=j; i<=k; ++i)
#define ROF(i,j,k) for(int i=j; i>=k; --i)
inline int read (void) {
int x = 0, f = 1, ch = getchar();
while(!isdigit(ch)) { if(ch == '-') f = -f; ch = getchar(); }
while(isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); }
return x * f;
}
using ll = long long;
using pii = std::pair <int, int>;
const int maxn = 100005;
inline bool cmp (pii p, pii q) {
return p.first + p.second < q.first + q.second;
}
char chp[5], chq[5];
pii p[maxn];
int a[maxn<<1];
ll pre[maxn], suf[maxn];
struct Node1 {
ll tot; std::priority_queue <int> q;
inline int top (void) { return q.top(); }
inline void push (int v) { tot += v; q.push(v); }
inline void pop (void) { tot -= q.top(); q.pop(); }
inline void clear (void) { tot = 0; while(!q.empty()) q.pop(); }
} q1;
struct Node2 {
ll tot; std::priority_queue <int, std::vector <int>, std::greater <int> > q;
inline int top (void) { return q.top(); }
inline void push (int v) { tot += v; q.push(v); }
inline void pop (void) { tot -= q.top(); q.pop(); }
inline void clear (void) { tot = 0; while(!q.empty()) q.pop(); }
} q2;
int main (void) {
int k = read(), N = read(), n = 0;
ll ans = 0;
FOR(i,1,N) {
scanf("%s", chp+1);
int s = read();
scanf("%s", chq+1);
int t = read();
if(s > t) std::swap (s, t);
if(chp[1] == chq[1])
ans += t - s;
else {
++ ans;
p[++ n] = {s, t};
}
}
if(k == 1) {
FOR(i,1,n) a[i*2-1] = p[i].first, a[i*2] = p[i].second;
std::sort(a+1, a+n*2+1);
FOR(i,1,n) ans -= a[i];
FOR(i,n+1,n*2) ans += a[i];
} else {
std::sort(p+1, p+n+1, cmp);
ll mn = 1ll << 60;
FOR(i,1,n) {
q1.push(p[i].first);
q2.push(p[i].second);
while(q1.top() > q2.top()) {
q2.push(q1.top()); q1.pop();
q1.push(q2.top()); q2.pop();
}
pre[i] = q2.tot - q1.tot;
}
q1.clear (); q2.clear ();
ROF(i,n,1) {
q1.push(p[i].first);
q2.push(p[i].second);
while(q1.top() > q2.top()) {
q2.push(q1.top()); q1.pop();
q1.push(q2.top()); q2.pop();
}
suf[i] = q2.tot - q1.tot;
}
FOR(i,0,n) mn = std::min(mn, pre[i] + suf[i+1]);
ans += mn;
}
printf("%lld\n", ans);
return 0;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:38:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | scanf("%s", chp+1);
| ~~~~~^~~~~~~~~~~~~
bridge.cpp:40:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | scanf("%s", chq+1);
| ~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
312 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
308 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
320 KB |
Output is correct |
6 |
Correct |
2 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
16 ms |
2560 KB |
Output is correct |
13 |
Correct |
35 ms |
4164 KB |
Output is correct |
14 |
Correct |
30 ms |
2892 KB |
Output is correct |
15 |
Correct |
20 ms |
2604 KB |
Output is correct |
16 |
Correct |
21 ms |
3404 KB |
Output is correct |
17 |
Correct |
23 ms |
4176 KB |
Output is correct |
18 |
Correct |
29 ms |
3784 KB |
Output is correct |
19 |
Correct |
35 ms |
4248 KB |
Output is correct |
20 |
Correct |
23 ms |
3644 KB |
Output is correct |
21 |
Correct |
31 ms |
3872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
312 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
308 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
312 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
324 KB |
Output is correct |
20 |
Correct |
1 ms |
324 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
448 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
312 KB |
Output is correct |
8 |
Correct |
1 ms |
312 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
308 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
436 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
328 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
324 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
328 KB |
Output is correct |
25 |
Correct |
31 ms |
4416 KB |
Output is correct |
26 |
Correct |
69 ms |
4520 KB |
Output is correct |
27 |
Correct |
84 ms |
5192 KB |
Output is correct |
28 |
Correct |
78 ms |
5824 KB |
Output is correct |
29 |
Correct |
85 ms |
5768 KB |
Output is correct |
30 |
Correct |
42 ms |
3316 KB |
Output is correct |
31 |
Correct |
31 ms |
5232 KB |
Output is correct |
32 |
Correct |
73 ms |
5968 KB |
Output is correct |
33 |
Correct |
40 ms |
5652 KB |
Output is correct |
34 |
Correct |
79 ms |
5932 KB |
Output is correct |
35 |
Correct |
58 ms |
5428 KB |
Output is correct |
36 |
Correct |
88 ms |
5676 KB |
Output is correct |
37 |
Correct |
23 ms |
4412 KB |
Output is correct |