#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
using pii = pair<int,int>;
#define all(a) a.begin(),a.end()
#define sz(a) (int)a.size()
const int maxn = (int)1e5+10;
const int LINF = (int)1e18;
vector<int> v;
vector<pii> seg;
set<pii> bef,dur,aft;
int t, n, sum_bef, sum_dur, sum_aft, tot, ans=LINF;
int a[maxn], b[maxn];
int chk(int a[]){
priority_queue<int> p1; while(!p1.empty())p1.pop();
priority_queue<int,vector<int>,greater<int>> p2;
while(!p1.empty())p1.pop(); while(!p2.empty())p2.pop();
int sum = 0, x;
for(int i = 0; i < sz(seg); i++){
p1.push(seg[i].se), p1.push(seg[i].fi-seg[i].se);
sum-=seg[i].se, sum-=seg[i].fi-seg[i].se;
while(!p1.empty() and sz(p1)>sz(p2)) x=p1.top(), p1.pop(),p2.push(x), sum+=2*x;
while(!p2.empty() and sz(p1)<sz(p2)) x=p2.top(), p2.pop(),p1.push(x), sum-=2*x;
a[i]=sum;
}
return sum;
}
int32_t main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> t >> n; int ans = LINF;
for(int i = 0; i < n; i++){
char a, c; int b, d;
cin >> a >> b >> c >> d;
if(a==c) tot+=abs(b-d);
else seg.pb({b+d, min(b,d)}), tot++, v.pb(b), v.pb(d);
}
sort(all(v)); sort(all(seg));
if(!seg.empty()) ans = chk(a);
if(t==2){
chk(a),reverse(all(seg)),chk(b);
for(int i = 0; i < sz(seg); i++)
ans = min(ans, a[i]+b[sz(seg)-i-1]);
}
if(seg.empty()) ans=0;
cout << ans+tot;
}
Compilation message
bridge.cpp: In function 'long long int chk(long long int*)':
bridge.cpp:22:2: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
22 | while(!p1.empty())p1.pop(); while(!p2.empty())p2.pop();
| ^~~~~
bridge.cpp:22:30: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
22 | while(!p1.empty())p1.pop(); while(!p2.empty())p2.pop();
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
388 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |