#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define fr first
#define se second
#define rep(i,a,b) for(int i = a; i < (b); ++i)
#define rrep(i,a,b) for(int i = a; i > (b); --i)
#define sz(x) (int)(x).size()
#define all(x) (x).begin(),(x).end()
#define IN(i,l,r) (l<i&&i<r)
#define pb push_back
#define ones __builtin_popcountll
using namespace std;
using namespace __gnu_pbds;
template <class T>
using OSTree = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
typedef pair<int,int> pi;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef long long ll;
int k,n;
vi home,office,ord;
void fill_cost(vll &sol){
OSTree<pi> iset;
sol[0] = abs(office[ord[0]] - home[ord[0]]);
iset.insert({office[ord[0]],0});
iset.insert({home[ord[0]],1});
auto med = [&]() -> int {
return iset.find_by_order((sz(iset)-1)/2)->fr;
};
int c = 2;
rep(i,1,n){
int old = med();
iset.insert({home[ord[i]],c++});
int neo = med();
ll del = abs(home[ord[i]] - old) + (old - neo);
old = neo;
iset.insert({office[ord[i]],c++});
neo = med();
del += abs(office[ord[i]] - old);
sol[i] = sol[i-1] + del;
}
}
int main(){
cin.tie(0)->sync_with_stdio(false);
cin >> k >> n;
ll no_cross = 0;
rep(i,0,n){
char p,q; int s,t; cin >> p >> s >> q >> t;
if(p == q){
no_cross += abs(t - s);
}else{
home.pb(s);
office.pb(t);
}
}
n = sz(home);
ord = vi(n,0);
iota(all(ord),0);
sort(all(ord),[](int a,int b){return home[a] + office[a] < home[b] + office[b];});
vll pre(n,0), suf(n,0);
fill_cost(pre);
ll cross = pre[n-1];
if(k == 1){
cout << (no_cross + cross + n) << '\n';
return 0;
}
reverse(all(ord));
fill_cost(suf);
rep(i,1,n) cross = min(cross, pre[i-1] + suf[n-1-i]);
cout << (no_cross + cross + n) << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
600 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |