#include <bits/stdc++.h>
#define N 1000001
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll INF = 1e18, MOD = 1e9 + 7, MOD2 = 1e6 + 3;
ll n, k, median;
ll side = 0, lsum = 0, rsum = 0;
ll pr[N];
priority_queue <ll> l, r;
struct seg {
ll l, r;
bool operator < (const seg& b) {
return l + r < b.l + b.r;
}
};
vector <seg> v;
void insert (ll x) {
if (!l.size ()) {
median = x;
l.push (x);
lsum += x;
return;
}
if (x <= median) { l.push (x), lsum += x; }
else { r.push (-x), rsum += x; }
ll len = (l.size () + r.size () + 1) / 2;
if (l.size () > len) {
ll k = l.top ();
l.pop ();
r.push (-k);
lsum -= k, rsum += k;
}
if (l.size () < len) {
ll k = -r.top ();
r.pop ();
l.push (k);
lsum += k, rsum -= k;
}
median = l.top ();
}
int main () {
cin >> k >> n;
for (ll i = 0; i < n; i++) {
string a, b;
ll la, lb;
cin >> a >> la >> b >> lb;
if (a == b) side += abs (la - lb);
else v.push_back ({la, lb});
}
sort (v.begin (), v.end ());
for (ll i = 0; i < v.size (); i++) {
insert (v[i].l), insert (v[i].r);
pr[i] = rsum - lsum;
}
ll n = v.size ();
if (k == 2) {
lsum = 0, rsum = 0;
while (!l.empty ()) l.pop ();
while (!r.empty ()) r.pop ();
ll mx = pr[n-1];
for (ll i = n - 1; i >= 0; i--) {
insert (v[i].l), insert (v[i].r);
mx = min (mx, rsum - lsum + (i ? pr[i-1] : 0));
}
cout << mx + side + n;
}
else cout << pr[n-1] + side + n;
}
Compilation message
bridge.cpp: In function 'void insert(ll)':
bridge.cpp:43:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (l.size () > len) {
~~~~~~~~~~^~~~~
bridge.cpp:50:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (l.size () < len) {
~~~~~~~~~~^~~~~
bridge.cpp: In function 'int main()':
bridge.cpp:74:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (ll i = 0; i < v.size (); i++) {
~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
6 ms |
384 KB |
Output is correct |
5 |
Correct |
6 ms |
384 KB |
Output is correct |
6 |
Correct |
6 ms |
384 KB |
Output is correct |
7 |
Correct |
7 ms |
384 KB |
Output is correct |
8 |
Correct |
6 ms |
384 KB |
Output is correct |
9 |
Correct |
7 ms |
384 KB |
Output is correct |
10 |
Correct |
6 ms |
384 KB |
Output is correct |
11 |
Correct |
6 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
3 |
Correct |
6 ms |
384 KB |
Output is correct |
4 |
Correct |
6 ms |
384 KB |
Output is correct |
5 |
Correct |
6 ms |
384 KB |
Output is correct |
6 |
Correct |
6 ms |
384 KB |
Output is correct |
7 |
Correct |
7 ms |
384 KB |
Output is correct |
8 |
Correct |
6 ms |
384 KB |
Output is correct |
9 |
Correct |
7 ms |
384 KB |
Output is correct |
10 |
Correct |
6 ms |
384 KB |
Output is correct |
11 |
Correct |
6 ms |
384 KB |
Output is correct |
12 |
Correct |
108 ms |
6884 KB |
Output is correct |
13 |
Correct |
201 ms |
7732 KB |
Output is correct |
14 |
Correct |
135 ms |
6500 KB |
Output is correct |
15 |
Correct |
116 ms |
4972 KB |
Output is correct |
16 |
Correct |
152 ms |
7152 KB |
Output is correct |
17 |
Correct |
184 ms |
7996 KB |
Output is correct |
18 |
Correct |
164 ms |
7140 KB |
Output is correct |
19 |
Correct |
202 ms |
7908 KB |
Output is correct |
20 |
Correct |
159 ms |
7524 KB |
Output is correct |
21 |
Correct |
181 ms |
7652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
256 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
256 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
7 ms |
384 KB |
Output is correct |
14 |
Correct |
7 ms |
384 KB |
Output is correct |
15 |
Correct |
7 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
6 ms |
384 KB |
Output is correct |
20 |
Correct |
7 ms |
384 KB |
Output is correct |
21 |
Correct |
6 ms |
384 KB |
Output is correct |
22 |
Correct |
7 ms |
384 KB |
Output is correct |
23 |
Correct |
7 ms |
384 KB |
Output is correct |
24 |
Correct |
6 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
6 ms |
384 KB |
Output is correct |
14 |
Correct |
6 ms |
512 KB |
Output is correct |
15 |
Correct |
7 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
6 ms |
384 KB |
Output is correct |
20 |
Correct |
7 ms |
384 KB |
Output is correct |
21 |
Correct |
6 ms |
384 KB |
Output is correct |
22 |
Correct |
6 ms |
384 KB |
Output is correct |
23 |
Correct |
6 ms |
384 KB |
Output is correct |
24 |
Correct |
6 ms |
384 KB |
Output is correct |
25 |
Correct |
117 ms |
6376 KB |
Output is correct |
26 |
Correct |
155 ms |
6756 KB |
Output is correct |
27 |
Correct |
205 ms |
7504 KB |
Output is correct |
28 |
Correct |
230 ms |
8036 KB |
Output is correct |
29 |
Correct |
227 ms |
7528 KB |
Output is correct |
30 |
Correct |
125 ms |
4200 KB |
Output is correct |
31 |
Correct |
156 ms |
6884 KB |
Output is correct |
32 |
Correct |
209 ms |
7908 KB |
Output is correct |
33 |
Correct |
200 ms |
8036 KB |
Output is correct |
34 |
Correct |
210 ms |
8036 KB |
Output is correct |
35 |
Correct |
169 ms |
7496 KB |
Output is correct |
36 |
Correct |
198 ms |
7652 KB |
Output is correct |
37 |
Correct |
108 ms |
6500 KB |
Output is correct |