#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define int long long
#define vi vector<int>
#define vvi vector<vector<int>>
#define pii pair<int, int>
#define vpii vector<pair<int, int>>
#define vc vector<char>
#define vb vector<bool>
#define mii map<int,int>
#define f0r(i,n) for(int i=0;i<n;i++)
#define FOR(i,k,n) for(int i=k;i<n;i++)
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define in(a) int a; cin>>a
#define in2(a,b) int a,b; cin>>a>>b
#define in3(a,b,c) int a,b,c; cin>>a>>b>>c
#define in4(a,b,c,d) int a,b,c,d; cin>>a>>b>>c>>d
#define vin(v,n); vi v(n); f0r(i,n){cin>>v[i];}
#define out(a) cout<<a<<'\n'
#define out2(a,b) cout<<a<<' '<<b<<'\n'
#define out3(a,b,c) cout<<a<<' '<<b<<' '<<c<<'\n'
#define out4(a,b,c,d) cout<<a<<' '<<b<<' '<<c<<' '<<d<<'\n'
#define pout(a) cout<<a.first<<' '<<a.second<<'\n'
#define vout(v) for(auto u : v){cout<<u<<' ';} cout<<endl
#define dout(a) cout<<a<<' '<<#a<<endl
#define dout2(a,b) cout<<a<<' '<<#a<<' '<<b<<' '<<#b<<endl
#define yn(x); if(x){cout<<"YES"<<'\n';}else{cout<<"NO"<<'\n';}
const int leg = 1e9 + 7;
const int mod = 998244353;
using namespace std;
struct median_set{
multiset<int>a, b; int sa, sb;
median_set(){
sa = 0; sb = 0;
}
void add(int x){
if(a.size() > 0 && *(--a.end()) >= x){
a.insert(x); sa += x;
if(a.size() - 2 == b.size()){
int y = *(--a.end());
a.erase(a.find(y)); sa -= y;
b.insert(y); sb += y;
}
}
else{
b.insert(x); sb += x;
if(b.size() > a.size()){
int y = *b.begin();
b.erase(b.find(y)); sb -= y;
a.insert(y); sa += y;
}
}
}
void remove(int x){
if(a.size() > 0 && *(--a.end()) >= x){
a.erase(a.find(x)); sa -= x;
if(b.size() > a.size()){
int y = *b.begin();
b.erase(b.find(y)); sb -= y;
a.insert(y); sa += y;
}
}
else{
b.erase(b.find(x)); sb -= x;
if(a.size() - 2 == b.size()){
int y = *(--a.end());
a.erase(a.find(y)); sa -= y;
b.insert(y); sb += y;
}
}
}
int cost(){
int med;
if(a.size() != 0)med = *(--a.end());
else if(b.size() != 0)med = *b.begin();
else return 0;
return med * a.size() - sa + sb - med * b.size();
}
};
signed main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
//ifstream cin(".in");
//ofstream cout(".out");
in2(k,n);
int ans = 0;
vi x; vi y;
f0r(i,n){
char a, c; int b,d;
cin>>a>>b>>c>>d;
if(a == c){
ans += abs(d - b);
}
else{
x.pb(b); y.pb(d);
}
}
n = x.size(); ans += n;
if(k == 1){
if(n != 0){
vi w;
f0r(i,n){
w.pb(x[i]); w.pb(y[i]);
}
sort(all(w)); int med = w[n];
f0r(i,n * 2){
ans += abs(med - w[i]);
}
}
out(ans);
}
else{
vpii pts;
f0r(i,n)pts.pb(mp(x[i] + y[i], i));
sort(all(pts));
median_set a, b;
f0r(i, n){
int dex = pts[i].second;
b.add(x[dex]); b.add(y[dex]);
// dout2(x[dex], y[dex]);
}
int ca = b.cost();
f0r(i,n){
int dex = pts[i].second;
b.remove(x[dex]); b.remove(y[dex]);
a.add(x[dex]); a.add(y[dex]);
ca = min(ca, b.cost() + a.cost());
}
out(ans + ca);
}
}
# | 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... |