#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define db double
#define pairll pair<ll,ll>
#define lpairll pair<ll,pairll>
#define repp(i,a,b) for (ll i = a; i <= b; i++)
#define repz(i,a,b) for (ll i = a; i < b; i++)
#define repm(i,a,b) for (ll i = a; i >= b; i--)
#define fr first
#define sc second
#define mp make_pair
#define pb push_back
const ll N = 2e5+5, MOD = 1e9+7, M = 1e5+5;
ll tc = 1, n, m, sf[N], pr[N], R, L;
ll x, y, k, otsum, dg, dsum, idx;
string s, s1, s2, ye = "YES", no = "NO";
pairll ar[N];
priority_queue<ll> le;
priority_queue<ll,vector<ll>,greater<ll> > ri;
bool cmp(pairll a, pairll b){
return(a.fr+a.sc < b.fr+b.sc);
}
void add(ll val){
if (le.empty() || le.top() >= val) le.push(val), L += val;
else ri.push(val), R += val;
ll ls = le.size(), rs = ri.size();
//move from left to right
if (ls > rs+1){
ll tt = le.top();
le.pop();
ri.push(tt);
L -= tt;
R += tt;
}
//move from right to left
else if (rs > ls){
ll tt = ri.top();
ri.pop();
le.push(tt);
L += tt;
R -= tt;
}
}
void input(){
cin >> k >> n;
repp(i,1,n){
char A, B;
ll L, R;
cin >> A >> L >> B >> R;
if (L > R) swap(L,R);
if (A == B) otsum += abs(R-L);
else{
otsum++;
idx++;
ar[idx] = mp(L,R);
}
}
}
void solve(){
sort(ar+1,ar+idx+1,cmp);
//build prefix
L = R = 0;
repp(i,1,idx){
add(ar[i].fr);
add(ar[i].sc);
pr[i] = R-L;
}
//build suffix
while(le.size()) le.pop();
while(ri.size()) ri.pop();
L = R = 0;
repm(i,idx,1){
add(ar[i].fr);
add(ar[i].sc);
sf[i] = R-L;
}
ll ans = LLONG_MAX;
pr[0] = pr[idx+1] = 0;
if (k == 1) ans = pr[idx];
else{
repp(i,1,idx+1){
ans = min(ans,pr[i-1]+sf[i]);
//cout << i << " " << pr[i-1] << " " << sf[i] << endl;
}
}
cout << ans+otsum << endl;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
//cin >> tc;
while(tc--){
input();
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
324 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 |
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 |
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 |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 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 |
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 |
32 ms |
5152 KB |
Output is correct |
13 |
Correct |
82 ms |
5148 KB |
Output is correct |
14 |
Correct |
76 ms |
4612 KB |
Output is correct |
15 |
Correct |
42 ms |
3160 KB |
Output is correct |
16 |
Correct |
31 ms |
5148 KB |
Output is correct |
17 |
Correct |
63 ms |
5192 KB |
Output is correct |
18 |
Correct |
45 ms |
5048 KB |
Output is correct |
19 |
Correct |
65 ms |
5156 KB |
Output is correct |
20 |
Correct |
43 ms |
5196 KB |
Output is correct |
21 |
Correct |
67 ms |
5240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 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 |
344 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 |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 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 |
348 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 |
352 KB |
Output is correct |
20 |
Correct |
1 ms |
340 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 |
356 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 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 |
336 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 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 |
368 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 |
2 ms |
340 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 |
25 |
Correct |
32 ms |
5984 KB |
Output is correct |
26 |
Correct |
57 ms |
6080 KB |
Output is correct |
27 |
Correct |
77 ms |
6904 KB |
Output is correct |
28 |
Correct |
78 ms |
7424 KB |
Output is correct |
29 |
Correct |
80 ms |
7492 KB |
Output is correct |
30 |
Correct |
45 ms |
4148 KB |
Output is correct |
31 |
Correct |
34 ms |
6916 KB |
Output is correct |
32 |
Correct |
63 ms |
7500 KB |
Output is correct |
33 |
Correct |
47 ms |
7048 KB |
Output is correct |
34 |
Correct |
72 ms |
7548 KB |
Output is correct |
35 |
Correct |
47 ms |
7120 KB |
Output is correct |
36 |
Correct |
70 ms |
7248 KB |
Output is correct |
37 |
Correct |
28 ms |
5956 KB |
Output is correct |