#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define pb push_back
#define sz(x) int((x).size())
int dx[4]{-1,0,1,0};
int dy[4]{0,1,0,-1};
void setIO(string filename=""){
ios_base::sync_with_stdio(0); cin.tie(0);
if(filename.size()){
freopen((filename + ".in").c_str(), "r", stdin);
freopen((filename + ".out").c_str(), "w", stdout);
}
}
struct dsu {
vector<int> e;
dsu(int N) { e=vector<int>(N,-1); }
int get(int x) { return e[x]<0 ? x : e[x]=get(e[x]); }
bool same_set(int a, int b) { return get(a)==get(b); }
int size(int x) { return -e[get(x)]; }
bool unite(int x, int y){
x=get(x),y=get(y);
if(x==y) return false;
if(e[x] > e[y]) swap(x,y);
e[x]+=e[y]; e[y]=x; return true;
}
};
const int INF=(int) 1e9;
const int M= 1e9+7;
const ll MAXD = 1e18;
const int P = 1e6;
//
const int MAXN = 1e5+5;
ll pref[MAXN];
ll rsum,lsum;
priority_queue<int> lpq;
priority_queue<int,vector<int>,greater<int>> rpq;
bool cmp(pair<int,int> a, pair<int,int> b) {
return a.fi + a.se < b.fi + b.se;
}
void insert(int x){
int median=(lpq.size() ? lpq.top() : INF);
if(x <= median) {
lpq.push(x);
lsum+=x;
} else {
rpq.push(x);
rsum+=x;
}
if(rpq.size() + 1 < lpq.size()){
int nxt=lpq.top();
lpq.pop();
rpq.push(nxt);
lsum-=nxt;
rsum+=nxt;
} else if(lpq.size() < rpq.size()){
int nxt=rpq.top();
rpq.pop();
lpq.push(nxt);
rsum-=nxt;
lsum+=nxt;
}
}
int main() {
setIO();
int k,n;
cin >> k >> n;
ll same_side=0;
vector<pair<int,int>> v={{0,0}};
for(int i=0;i<n;++i){
char a,b;
int x,y;
cin >> a >> x >> b >> y;
if(a==b)
same_side += abs(x-y);
else
v.push_back({x,y});
}
sort(v.begin(),v.end(),cmp);
n=v.size()-1;
same_side+=n;
lsum = rsum = 0;
for(int i=1;i<=n;++i){
insert(v[i].fi);
insert(v[i].se);
pref[i] = rsum - lsum;
}
ll ans=pref[n];
if(k==2){
while(lpq.size())
lpq.pop();
while(rpq.size())
rpq.pop();
lsum=rsum=0;
for(int i=n;i;--i){
insert(v[i].fi);
insert(v[i].se);
ans=min(ans, rsum-lsum + pref[i-1]);
}
}
cout << same_side + ans << "\n";
}
Compilation message
bridge.cpp: In function 'void setIO(std::string)':
bridge.cpp:17:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | freopen((filename + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:18:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | freopen((filename + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
360 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
344 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 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 |
344 KB |
Output is correct |
12 |
Correct |
31 ms |
3924 KB |
Output is correct |
13 |
Correct |
50 ms |
5556 KB |
Output is correct |
14 |
Correct |
48 ms |
4588 KB |
Output is correct |
15 |
Correct |
32 ms |
3532 KB |
Output is correct |
16 |
Correct |
39 ms |
4852 KB |
Output is correct |
17 |
Correct |
47 ms |
5452 KB |
Output is correct |
18 |
Correct |
41 ms |
5488 KB |
Output is correct |
19 |
Correct |
46 ms |
5516 KB |
Output is correct |
20 |
Correct |
33 ms |
5332 KB |
Output is correct |
21 |
Correct |
44 ms |
5192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 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 |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
340 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 |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
340 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 |
344 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 |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
368 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
328 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
212 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 |
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 |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
35 ms |
3832 KB |
Output is correct |
26 |
Correct |
61 ms |
4556 KB |
Output is correct |
27 |
Correct |
80 ms |
5032 KB |
Output is correct |
28 |
Correct |
85 ms |
5664 KB |
Output is correct |
29 |
Correct |
85 ms |
5356 KB |
Output is correct |
30 |
Correct |
43 ms |
3100 KB |
Output is correct |
31 |
Correct |
41 ms |
4688 KB |
Output is correct |
32 |
Correct |
61 ms |
5408 KB |
Output is correct |
33 |
Correct |
65 ms |
5044 KB |
Output is correct |
34 |
Correct |
66 ms |
5352 KB |
Output is correct |
35 |
Correct |
44 ms |
5320 KB |
Output is correct |
36 |
Correct |
65 ms |
5148 KB |
Output is correct |
37 |
Correct |
29 ms |
3888 KB |
Output is correct |