#include "bits/stdc++.h"
#define ll long long int
#define pb push_back
#define pii pair<ll,ll>
#define ff first
#define ss second
#define sz size()
const int N = 2e5 + 1;
using namespace std;
ll n, k, x[N], y[N], ans, p[N], q[N], mn = 1e15, cnt, cnt1, sum, sum1;
char s[N], d[N];
vector <pii> v;
multiset <int> l, r;
bool cmp(pii a, pii b) {
return a.ff + a.ss < b.ff + b.ss;
}
void med(int a){
if(l.empty()) {
l.insert(a);
sum += a;
}
else if((int)l.sz <= (int)r.sz){
auto t = r.begin();
ll aa = *t;
if (a > aa) {
sum += aa;
l.insert(aa);
sum1 += a;
sum1 -= aa;
r.erase(t);
r.insert(a);
}
else {
l.insert(a);
sum += a;
}
}
else{
auto t = l.end();
t--;
ll aa = *t;
if (a < aa){
sum -= (ll)aa;
sum += a;
sum1 += aa;
r.insert(aa);
l.erase(t);
l.insert(a);
}
else{
r.insert(a);
sum1 += a;
}
}
}
int main() {
// freopen ("input.txt", "r", stdin);
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> k >> n;
for(int i = 1; i <= n; i++){
cin >> s[i] >> x[i] >> d[i] >> y[i];
if(s[i] != d[i]){
ans++;
v.pb({x[i],y[i]});
}
else ans += abs(x[i]-y[i]);
}
sort(v.begin(), v.end(), cmp);
for(int i = 0; i < (int)v.sz; i++){
med(v[i].ff);
med(v[i].ss);
auto t = l.end();
t--;
ll med1 = *t;
cnt = (int)l.sz;cnt1 = (int)r.sz;
p[i] = (cnt * med1 - sum) + (sum1 - cnt1 * med1);
}
cnt = sum = cnt1 = sum1 = 0;
l.clear();r.clear();
for(int i = (int)v.sz-1; i >= 0; i--){
med(v[i].ff);
med(v[i].ss);
auto t = l.end();
t--;
ll med1 = *t;
cnt = (int)l.sz;cnt1 = (int)r.sz;
q[i] = (cnt * med1 - sum) + (sum1 - cnt1 * med1);
}
for(int i = 0; i < (int)v.sz; i++){
mn = min(mn, p[i] + q[i+1]);
}
mn = min(mn,q[0]);
if(k == 1) mn = q[0];
cout << mn + ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6616 KB |
Output is correct |
5 |
Correct |
2 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6748 KB |
Output is correct |
7 |
Correct |
2 ms |
6632 KB |
Output is correct |
8 |
Correct |
2 ms |
6620 KB |
Output is correct |
9 |
Correct |
2 ms |
6620 KB |
Output is correct |
10 |
Correct |
2 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6744 KB |
Output is correct |
7 |
Correct |
2 ms |
6744 KB |
Output is correct |
8 |
Correct |
2 ms |
6744 KB |
Output is correct |
9 |
Correct |
2 ms |
6744 KB |
Output is correct |
10 |
Correct |
2 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
82 ms |
18412 KB |
Output is correct |
13 |
Correct |
158 ms |
19696 KB |
Output is correct |
14 |
Correct |
134 ms |
17692 KB |
Output is correct |
15 |
Correct |
80 ms |
14284 KB |
Output is correct |
16 |
Correct |
89 ms |
19216 KB |
Output is correct |
17 |
Correct |
96 ms |
19676 KB |
Output is correct |
18 |
Correct |
78 ms |
19416 KB |
Output is correct |
19 |
Correct |
102 ms |
19912 KB |
Output is correct |
20 |
Correct |
94 ms |
19292 KB |
Output is correct |
21 |
Correct |
99 ms |
19400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6744 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6604 KB |
Output is correct |
10 |
Correct |
1 ms |
6744 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
2 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
6616 KB |
Output is correct |
15 |
Correct |
2 ms |
6748 KB |
Output is correct |
16 |
Correct |
1 ms |
6624 KB |
Output is correct |
17 |
Correct |
2 ms |
6492 KB |
Output is correct |
18 |
Correct |
2 ms |
6492 KB |
Output is correct |
19 |
Correct |
2 ms |
6748 KB |
Output is correct |
20 |
Correct |
2 ms |
6616 KB |
Output is correct |
21 |
Correct |
2 ms |
6748 KB |
Output is correct |
22 |
Correct |
2 ms |
6492 KB |
Output is correct |
23 |
Correct |
2 ms |
6748 KB |
Output is correct |
24 |
Correct |
2 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
2 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
2 ms |
6628 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6488 KB |
Output is correct |
19 |
Correct |
2 ms |
6748 KB |
Output is correct |
20 |
Correct |
2 ms |
6748 KB |
Output is correct |
21 |
Correct |
2 ms |
6748 KB |
Output is correct |
22 |
Correct |
2 ms |
6492 KB |
Output is correct |
23 |
Correct |
2 ms |
6748 KB |
Output is correct |
24 |
Correct |
2 ms |
6748 KB |
Output is correct |
25 |
Correct |
82 ms |
18376 KB |
Output is correct |
26 |
Correct |
131 ms |
18416 KB |
Output is correct |
27 |
Correct |
148 ms |
19144 KB |
Output is correct |
28 |
Correct |
181 ms |
19656 KB |
Output is correct |
29 |
Correct |
147 ms |
19940 KB |
Output is correct |
30 |
Correct |
69 ms |
13252 KB |
Output is correct |
31 |
Correct |
92 ms |
18884 KB |
Output is correct |
32 |
Correct |
97 ms |
19396 KB |
Output is correct |
33 |
Correct |
81 ms |
19140 KB |
Output is correct |
34 |
Correct |
99 ms |
19400 KB |
Output is correct |
35 |
Correct |
92 ms |
18848 KB |
Output is correct |
36 |
Correct |
109 ms |
19344 KB |
Output is correct |
37 |
Correct |
72 ms |
18116 KB |
Output is correct |