This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <int, pii> pip;
typedef pair <pii, int> ppi;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
const int INF=0x3f3f3f3f;
const int OFF=(1<<17);
const int N=1e5+5;
int k, n;
ll sol=0;
vector <int> vec, saz;
vector <pii> v;
pli T[2*OFF];
ll desaz[N];
ll pref[N], suff[N];
int cmp(pii a, pii b) {
return a.X+a.Y<b.X+b.Y;
}
int sazmi(int x) {
int ret=lower_bound(saz.begin(), saz.end(), x)-saz.begin();
desaz[ret]=x;
return ret;
}
pli mrg(pli a, pli b) {
return mp(a.X+b.X, a.Y+b.Y);
}
pli query(int a, int b, int pos=1, int lo=0, int hi=OFF) {
if (lo>=a && hi<=b) return T[pos];
if (lo>=b || hi<=a) return mp(0, 0);
int mid=(lo+hi)/2;
return mrg(query(a, b, pos*2, lo, mid), query(a, b, pos*2+1, mid, hi));
}
int nadji(int val, int pos=1) {
if (pos>=OFF) return pos-OFF;
if (T[pos*2].Y>=val) return nadji(val, pos*2);
return nadji(val-T[pos*2].Y, pos*2+1);
}
void update(int pos) {
pos+=OFF;
T[pos].X+=desaz[pos-OFF]; T[pos].Y++;
pos/=2;
while (pos) {
T[pos]=mrg(T[pos*2], T[pos*2+1]);
pos/=2;
}
}
void dva() {
n=v.size();
sort(v.begin(), v.end(), cmp);
for (int i=0; i<n; ++i) saz.pb(v[i].X), saz.pb(v[i].Y);
sort(saz.begin(), saz.end());
saz.erase(unique(saz.begin(), saz.end()), saz.end());
for (int i=0; i<n; ++i) v[i].X=sazmi(v[i].X), v[i].Y=sazmi(v[i].Y);
for (int i=0; i<n; ++i) {
update(v[i].X); update(v[i].Y);
int med=nadji(i+1);
pli curr1=query(0, med), curr2=query(med+1, OFF);
pref[i]=desaz[med]*curr1.Y-curr1.X+curr2.X-desaz[med]*curr2.Y;
}
fill(T, T+2*OFF, mp(0, 0));
for (int i=n-1; i>=0; --i) {
update(v[i].X); update(v[i].Y);
int med=nadji(n-i);
pli curr1=query(0, med), curr2=query(med+1, OFF);
suff[i]=desaz[med]*curr1.Y-curr1.X + curr2.X-desaz[med]*curr2.Y;
}
ll res=suff[0];
for (int i=0; i<n; ++i) res=min(res, pref[i]+suff[i+1]);
printf("%lld\n", res+sol+n);
}
void jedinica() {
for (pii pp:v) vec.pb(pp.X), vec.pb(pp.Y);
sort(vec.begin(), vec.end());
int len=vec.size();
for (int i=0; i<len; ++i) sol+=abs(vec[i]-vec[len/2]);
printf("%lld\n", sol+len/2);
}
void load() {
scanf("%d %d", &k, &n);
for (int i=0; i<n; ++i) {
char ca, cb; int a, b;
scanf(" %c %d %c %d", &ca, &a, &cb, &b);
if (ca==cb) sol+=abs(a-b);
else v.pb(mp(min(a, b), max(a, b)));
}
}
int main() {
load();
if (k==1) jedinica();
else dva();
return 0;
}
Compilation message (stderr)
bridge.cpp: In function 'void load()':
bridge.cpp:102:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
102 | scanf("%d %d", &k, &n);
| ~~~~~^~~~~~~~~~~~~~~~~
bridge.cpp:105:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
105 | scanf(" %c %d %c %d", &ca, &a, &cb, &b);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |