#include <bits/stdc++.h>
using namespace std;
using ll = long long; using pii = pair<ll,ll>;
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
ll K,N; cin >> K >> N;
vector<pii> v;
ll ans = 0;
for (ll i=0;i<N;i++) {
string p,q; ll s,t;
cin >> p >> s >> q >> t;
if (p==q) {
ans += abs(s-t);
} else {
ans++;
v.push_back({min(s,t),max(s,t)});
}
}
vector<ll> v2;
for (pii p0: v) {
v2.push_back(p0.first);
v2.push_back(p0.second);
}
if (K==1) {
sort(v2.begin(),v2.end());
for (ll x: v2) {
ans += abs(x-v2[v2.size()/2]);
}
cout << ans; exit(0);
}
ll emin = 1e18;
for (ll a: v2) {
ll ec = 0;
priority_queue<pii> PQ; //-pos,sign(+1/-1)
for (pii p0: v) {
ll x = p0.first; ll y = p0.second;
ec += abs(a-x)+abs(a-y);
if (a<x || a>y) {
PQ.push({-a,-1});
PQ.push({-x,1});
PQ.push({-y,1});
PQ.push({-x-y+a,-1});
}
}
ll xmin = 1e18;
ll sp=0; ll sn=0;
while (!PQ.empty()) {
pii p0 = PQ.top(); PQ.pop();
ll p = -p0.first; ll n = p0.second;
xmin = min(xmin,(p*sn-sp));
sp += p*n; sn += n;
}
emin = min(emin,2*xmin+ec);
}
cout << (emin+ans);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
356 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
488 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
404 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
19 ms |
5844 KB |
Output is correct |
13 |
Correct |
31 ms |
7372 KB |
Output is correct |
14 |
Correct |
27 ms |
6096 KB |
Output is correct |
15 |
Correct |
23 ms |
4044 KB |
Output is correct |
16 |
Correct |
20 ms |
6612 KB |
Output is correct |
17 |
Correct |
23 ms |
7416 KB |
Output is correct |
18 |
Correct |
25 ms |
6864 KB |
Output is correct |
19 |
Correct |
48 ms |
7368 KB |
Output is correct |
20 |
Correct |
24 ms |
6856 KB |
Output is correct |
21 |
Correct |
47 ms |
7040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |