#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);
}
sort(v2.begin(),v2.end());
if (K==1) {
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);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 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 |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
468 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 |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
18 ms |
5652 KB |
Output is correct |
13 |
Correct |
49 ms |
7372 KB |
Output is correct |
14 |
Correct |
37 ms |
5952 KB |
Output is correct |
15 |
Correct |
21 ms |
4076 KB |
Output is correct |
16 |
Correct |
21 ms |
6608 KB |
Output is correct |
17 |
Correct |
23 ms |
7372 KB |
Output is correct |
18 |
Correct |
26 ms |
6836 KB |
Output is correct |
19 |
Correct |
38 ms |
7212 KB |
Output is correct |
20 |
Correct |
29 ms |
6868 KB |
Output is correct |
21 |
Correct |
30 ms |
7120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |