#include <bits/stdc++.h>
#define FAST_IO ios_base::sync_with_stdio(false); cin.tie(nullptr)
#define FOR(i, begin, end) for(int i=(begin); i<(end); i++)
#define pb push_back
#define s second
#define f first
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
const int N=1e5+10;
int k, n;
ll ans, suff[N];
vector<int> a;
vector<pii> inf;
bool cmp(pii a, pii b) {
return (a.f+a.s)<(b.f+b.s);
}
ll s[4];
void fix(priority_queue<int> &l, priority_queue<int, vector<int>, greater<int>> &r) {
while(l.size()>r.size()) {
int val=l.top(); l.pop();
r.push(val);
s[0]-=val; s[1]+=val;
}
while(r.size()>l.size()) {
int val=r.top(); r.pop();
l.push(val);
s[0]+=val; s[1]-=val;
}
}
ll calc() {
return s[1]-s[0];
}
int main() {
FAST_IO;
cin >> k >> n;
FOR(i, 0, n) {
char c1, c2; int x1, x2;
cin >> c1 >> x1 >> c2 >> x2;
if(c1==c2) {
ans+=abs(x1-x2);
}
else {
inf.pb({x1,x2});
a.pb(x1);
a.pb(x2);
}
}
sort(a.begin(), a.end());
n=a.size();
ans+=n/2;
if(n==0) {
cout << ans;
return 0;
}
sort(inf.begin(), inf.end(), cmp);
priority_queue<int> l;
priority_queue<int, vector<int>, greater<int>> r;
for(int i=(int)inf.size()-1; i>=0; i--) {
int val=inf[i].f;
if(l.empty()) l.push(val), s[0]+=val;
else {
int md=l.top();
if(val>=md) r.push(val), s[1]+=val;
else l.push(val), s[0]+=val;
}
val=inf[i].s;
int md=l.top();
if(val>=md) r.push(val), s[1]+=val;
else l.push(val), s[0]+=val;
fix(l, r);
suff[i]=calc();
}
if(k==1) {
cout << suff[0]+ans;
return 0;
}
while(!l.empty()) l.pop();
while(!r.empty()) r.pop();
s[0]=s[1]=0;
ll ret=suff[0]+ans;
FOR(i, 0, inf.size()) {
int val=inf[i].f;
if(l.empty()) l.push(val), s[0]+=val;
else {
int md=l.top();
if(val>=md) r.push(val), s[1]+=val;
else l.push(val), s[0]+=val;
}
val=inf[i].s;
int md=l.top();
if(val>=md) r.push(val), s[1]+=val;
else l.push(val), s[0]+=val;
fix(l, r);
// cout << i << ": " << calc() << " " << suff[i+1] << endl;
ret=min(ret, calc()+ans+suff[i+1]);
}
cout << ret;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:3:48: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
3 | #define FOR(i, begin, end) for(int i=(begin); i<(end); i++)
| ^
bridge.cpp:97:5: note: in expansion of macro 'FOR'
97 | FOR(i, 0, inf.size()) {
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
17 ms |
4320 KB |
Output is correct |
13 |
Correct |
54 ms |
4280 KB |
Output is correct |
14 |
Correct |
31 ms |
3772 KB |
Output is correct |
15 |
Correct |
23 ms |
2508 KB |
Output is correct |
16 |
Correct |
20 ms |
4040 KB |
Output is correct |
17 |
Correct |
26 ms |
4056 KB |
Output is correct |
18 |
Correct |
24 ms |
4064 KB |
Output is correct |
19 |
Correct |
36 ms |
3940 KB |
Output is correct |
20 |
Correct |
23 ms |
4296 KB |
Output is correct |
21 |
Correct |
34 ms |
4032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
472 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
472 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
472 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
0 ms |
516 KB |
Output is correct |
21 |
Correct |
1 ms |
488 KB |
Output is correct |
22 |
Correct |
1 ms |
512 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
464 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
356 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
352 KB |
Output is correct |
21 |
Correct |
0 ms |
352 KB |
Output is correct |
22 |
Correct |
1 ms |
352 KB |
Output is correct |
23 |
Correct |
0 ms |
352 KB |
Output is correct |
24 |
Correct |
0 ms |
352 KB |
Output is correct |
25 |
Correct |
23 ms |
5272 KB |
Output is correct |
26 |
Correct |
43 ms |
5124 KB |
Output is correct |
27 |
Correct |
58 ms |
5828 KB |
Output is correct |
28 |
Correct |
61 ms |
6592 KB |
Output is correct |
29 |
Correct |
59 ms |
6632 KB |
Output is correct |
30 |
Correct |
31 ms |
3540 KB |
Output is correct |
31 |
Correct |
27 ms |
5576 KB |
Output is correct |
32 |
Correct |
43 ms |
6388 KB |
Output is correct |
33 |
Correct |
37 ms |
6264 KB |
Output is correct |
34 |
Correct |
58 ms |
6300 KB |
Output is correct |
35 |
Correct |
30 ms |
6344 KB |
Output is correct |
36 |
Correct |
50 ms |
6080 KB |
Output is correct |
37 |
Correct |
20 ms |
4804 KB |
Output is correct |