This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
#pragma GCC optimize("O2")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2,fma,tune=native")
//*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int ,int > pii;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll maxn = 1e5 + 100;
const ll mod =1e9+7;
const ld PI = acos((ld)-1);
#define pb push_back
#define endl '\n'
#define dokme(x) cout << x , exit(0)
#define migmig ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ms(x , y) memset(x , y , sizeof x)
ll pw(ll a, ll b, ll md = mod){ll res = 1;while(b){if(b&1){res=(a*res)%md;}a=(a*a)%md;b>>=1;}return(res);}
int n , k;
bool H[maxn] , W[maxn];
ll h[maxn] , w[maxn];
vector < ll > vec;
struct sub1{
ll solve(ll x){
ll ans = 0;
for(int i = 0 ; i < n ; i ++){
if(H[i] == W[i])ans += abs(h[i] - w[i]);
else
ans += abs(h[i] - x) + abs(w[i] - x) + 1;
}
return(ans);
}
void sub(){
ll ans = 1e18;
for(auto x : vec)
ans = min(ans , solve(x));
dokme(ans);
}
}s1;
struct sub3{
ll solve(ll x , ll y){
ll ans = 0;
for(int i = 0 ; i < n ; i ++){
if(H[i] == W[i])ans += abs(h[i] - w[i]);
else
ans += min(abs(h[i] - x) + abs(w[i] - x) ,abs(h[i] - y) + abs(w[i] - y)) + 1;
}
return(ans);
}
void sub(){
ll ans = 1e18;
for(auto x : vec)
for(auto y : vec)
ans = min(ans , solve(x , y));
dokme(ans);
}
}s3;
bool cmp(pii a , pii b){
return(a.first + a.second < b.first + b.second);
}
struct sub2{
void sub(){
ll ans = 0;
vec.clear();
for(int i = 0 ; i < n ; i ++)
if(H[i] == W[i])
ans += abs(w[i] - h[i]);
else
ans ++,
vec.pb(h[i]) , vec.pb(w[i]);
ll mn = 5e18;
sort(vec.begin() , vec.end());
int N = vec.size();
vector < ll > l(N , 0) , r(N , 0);
for(int i = 1 ; i < N ; i ++){
l[i] = (vec[i] - vec[i - 1])*ll(i) + l[i - 1];
}
for(int i = N - 2 , j = 1 ; i >= 0 ; i -- , j ++ ){
r[i] = (vec[i + 1] - vec[i]) * ll(j) + r[i + 1];
}
for(int i = 0 ; i < N ; i ++)
mn = min(mn , l[i] + r[i]);
dokme(ans + mn);
}
}s2;
vector < pii > v;
ll l[maxn] , r[maxn];
ll mnsum = 0 , mxsum = 0;
priority_queue < ll > mx;
priority_queue < ll ,vector < ll > , greater < ll > > mn;
void add(pii x){
mx.push(x.first);
mx.push(x.second);
mxsum += x.first;
mxsum+=x.second;
while((int)mx.size() - (int)mn.size() > 0)
mn.push(mx.top()) ,mxsum-=mx.top() , mnsum += mx.top(), mx.pop();
while((int)mn.size() - (int)mx.size() > 0)
mx.push(mn.top()) ,mxsum+=mn.top() , mnsum -= mn.top(), mn.pop();
}
ll calc(){
ll med = mn.top();
ll ans = med * ll(mn.size()) - mnsum;
ans += mxsum - med*ll(mx.size());
return(ans);
}
int32_t main(){
migmig;
cin >> k >> n;
for(int i = 0 ; i < n ; i ++){
string s;
cin >> s;
H[i] = (s == "A");
cin >> h[i];
cin >> s;
W[i] = (s == "A");
cin >> w[i];
vec.pb(h[i]);
vec.pb(w[i]);
}
sort(vec.begin() , vec.end());
vec.resize(unique(vec.begin() , vec.end()) - vec.begin());
if(k == 1 and n <= 1000){
s1.sub();
}
if(k == 2 and n <= 100){
s3.sub();
}
if(k == 1){
s2.sub();
}
assert(k == 2);
assert(n <= 1e5);
ll ans = 0;
for(int i = 0 ; i < n ; i ++){
if(H[i] == W[i])
ans += abs(h[i] - w[i]);
else
ans ++, v.pb({min(h[i] , w[i]) , max(h[i] , w[i])});
}
ll Ans = 5e18;
sort(v.begin() , v.end() , cmp);
n = v.size();
if(n == 0)
dokme(ans);
for(int i = 0 ; i < n ; i ++){
add(v[i]);
l[i] = calc();
}
reverse(v.begin() , v.end());
mxsum = mnsum = 0;
while(mx.size())mx.pop();
while(mn.size())mn.pop();
for(int i = 0 ; i < n ; i ++){
add(v[i]);
r[i] = calc();
Ans = min(Ans , r[i] + l[n - i - 2]);
}
cout << ans + Ans;
return(0);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |