#include<bits/stdc++.h>
#define pb push_back
#define sz(x) (int)(x.size())
#define all(x) x.begin(), x.end()
#define int long long
#define pii pair<int, int>
#define inf 1e9
#define mod 1000000007
#define F first
#define S second
#define mkp(x, y) make_pair(x, y)
#define wopen(x) freopen((x),"w",stdout)
#define ropen(x) freopen((x),"r",stdin)
#define de(x) cout << #x << " = " << x << ' '
#define IO ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
const int mxN = 1e6 + 5;
int k, n, dis, su, sd, s[mxN], t[mxN], pre[mxN], suf[mxN];
char p[mxN], q[mxN];
priority_queue<int, vector<int>, greater<int>> up;
priority_queue<int> dn;
void keep() {
while(sz(dn) == sz(up) and dn.top() > up.top()) {
int t = up.top(); up.pop();
int tt = dn.top(); dn.pop();
su = su - t + tt;
sd = sd - tt + t;
up.push(tt); dn.push(t);
}
}
void insert(int x, bool o) {
if(o) {
dn.push(x);
sd += x;
} else {
up.push(x);
su += x;
}
}
void clear() {
while(!up.empty()) up.pop();
while(!dn.empty()) dn.pop();
su = sd = 0;
}
int get_mid() { return dn.top(); }
signed main() {
IO;
cin >> k >> n;
vector<int> v;
vector<pii> g;
int ans = 0;
for(int i = 0; i < n; i++) {
cin >> p[i] >> s[i] >> q[i] >> t[i];
if(p[i] != q[i]) {
v.pb(s[i]); v.pb(t[i]);
g.pb({s[i] + t[i], i});
ans++;
}
else ans += abs(s[i] - t[i]);
}
if(k == 1) {
sort(all(v));
if(sz(v) == 0) {
cout << ans << '\n';
return 0;
}
int mid = v[(sz(v)) / 2];
for(int i = 0; i < sz(v); i++) ans += abs(v[i] - mid);
cout << ans << '\n';
} else {
sort(all(g));
dis = 0;
for(int i = 0; i < sz(g); i++) {
insert(s[g[i].S], 0);
insert(t[g[i].S], 1);
keep();
pre[i] = get_mid() * sz(dn) - sd + su - get_mid() * sz(up);
}
clear();
for(int i = sz(g) - 1; i >= 0; i--) {
insert(s[g[i].S], 1);
insert(t[g[i].S], 0);
keep();
suf[i] = get_mid() * sz(dn) - sd + su - get_mid() * sz(up);
}
int mx = suf[0] + ans;
for(int i = 0; i < sz(g); i++) mx = min(mx, ans + pre[i] + suf[i + 1]);
cout << mx << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
2 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
2 ms |
492 KB |
Output is correct |
12 |
Correct |
26 ms |
5456 KB |
Output is correct |
13 |
Correct |
48 ms |
5456 KB |
Output is correct |
14 |
Correct |
37 ms |
4944 KB |
Output is correct |
15 |
Correct |
28 ms |
3416 KB |
Output is correct |
16 |
Correct |
33 ms |
5456 KB |
Output is correct |
17 |
Correct |
42 ms |
5604 KB |
Output is correct |
18 |
Correct |
43 ms |
5456 KB |
Output is correct |
19 |
Correct |
51 ms |
5584 KB |
Output is correct |
20 |
Correct |
34 ms |
5456 KB |
Output is correct |
21 |
Correct |
44 ms |
5456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
2 ms |
492 KB |
Output is correct |
15 |
Correct |
2 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
2 ms |
492 KB |
Output is correct |
21 |
Correct |
1 ms |
492 KB |
Output is correct |
22 |
Correct |
2 ms |
492 KB |
Output is correct |
23 |
Correct |
2 ms |
492 KB |
Output is correct |
24 |
Correct |
2 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
2 ms |
492 KB |
Output is correct |
15 |
Correct |
2 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
2 ms |
492 KB |
Output is correct |
21 |
Correct |
1 ms |
492 KB |
Output is correct |
22 |
Correct |
2 ms |
492 KB |
Output is correct |
23 |
Correct |
1 ms |
492 KB |
Output is correct |
24 |
Correct |
2 ms |
492 KB |
Output is correct |
25 |
Correct |
59 ms |
10192 KB |
Output is correct |
26 |
Correct |
99 ms |
10192 KB |
Output is correct |
27 |
Correct |
125 ms |
10320 KB |
Output is correct |
28 |
Correct |
134 ms |
10140 KB |
Output is correct |
29 |
Correct |
132 ms |
10192 KB |
Output is correct |
30 |
Correct |
63 ms |
5464 KB |
Output is correct |
31 |
Correct |
63 ms |
10192 KB |
Output is correct |
32 |
Correct |
100 ms |
10192 KB |
Output is correct |
33 |
Correct |
80 ms |
10192 KB |
Output is correct |
34 |
Correct |
110 ms |
10240 KB |
Output is correct |
35 |
Correct |
85 ms |
10192 KB |
Output is correct |
36 |
Correct |
103 ms |
10192 KB |
Output is correct |
37 |
Correct |
46 ms |
10212 KB |
Output is correct |