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>
#define nl "\n"
#define no "NO"
#define yes "YES"
#define fi first
#define se second
#define vec vector
#define task "main"
#define _mp make_pair
#define ii pair<int, int>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define evoid(val) return void(std::cout << val)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FOD(i, b, a) for(int i = (b); i >= (a); --i)
#define unq(x) sort(all(x)); x.resize(unique(all(x)) - x.begin())
using namespace std;
template<typename U, typename V> bool maxi(U &a, V b) {
if (a < b) { a = b; return 1; } return 0;
}
template<typename U, typename V> bool mini(U &a, V b) {
if (a > b) { a = b; return 1; } return 0;
}
const int N = (int)2e5 + 9;
const int mod = (int)1e9 + 7;
void prepare(); void main_code();
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
const bool MULTITEST = 0; prepare();
int num_test = 1; if (MULTITEST) cin >> num_test;
while (num_test--) { main_code(); }
}
void prepare() {};
pair<int, int> c[N];
int k, n;
namespace SUB1 {
void sol() {
long long ans = 0;
vec<ii> px;
long long s1 = 0, s2 = 0;
for(int i = 1; i <= n; ++i) {
char a, b; int x, y;
cin >> a >> x >> b >> y;
if (a == b) {
ans += abs(x - y);
continue ;
}
if (x > y) swap(x, y);
c[i] = _mp(x, y);
++ans;
s2 += x + y;
px.push_back(_mp(x, i));
px.push_back(_mp(y, 0));
px.push_back(_mp(y + 1, -i));
}
sort(px.begin(), px.end());
int cl = 0, cr = sz(px) / 3;
long long s3 = 0;
long long res = LLONG_MAX;
if (sz(px) == 0) res = 0;
for(int i = 0, j = 0; i < sz(px); i = j + 1) {
while (j + 1 < sz(px) and px[j + 1].fi == px[i].fi) ++j;
for(int k = i; k <= j; ++k) {
if (px[k].second > 0) {
auto [l, r] = c[px[k].second];
s2 -= l + r;
s3 += r - l;
--cr;
} else if (px[k].second < 0) {
auto [l, r] = c[-px[k].second];
s3 -= r - l;
s1 += -l - r;
++cl;
}
}
int x = px[i].first;
res = min(res, 2LL * x * cl + s1 + s3 + s2 - 2LL * cr * x);
}
cout << ans + res;
}
};
long long ad = 0;
namespace SUB_FULL {
struct ctdl {
struct st {
int c = 0;
long long sum = 0;
multiset<int> s;
void ins(int x) {
s.insert(x);
sum += x;
++c;
}
void del(int x) {
s.erase(s.find(x));
sum -= x;
--c;
}
int mi() {
return *s.begin();
}
int mx() {
return *s.rbegin();
}
bool fx(int x) {
return s.find(x) != s.end();
}
};
st a, b;
void ins(int x) {
if (a.c <= b.c)
a.ins(x);
else
b.ins(x);
if (b.c > 0 and a.mx() > b.mi()) {
int x = a.mx(), y = b.mi();
a.ins(y), b.del(y);
a.del(x), b.ins(x);
}
}
void del(int x) {
if (a.fx(x)) {
a.del(x);
} else {
b.del(x);
}
if (a.c < b.c) {
int y = b.mi();
a.ins(y), b.del(y);
}
if (a.c - 2 == b.c) {
int x = a.mx();
a.del(x), b.ins(x);
}
}
long long cost() {
if (a.c == 0) return 0;
int j = a.mx();
return 1LL * j * a.c - a.sum + b.sum - 1LL * j * b.c;
}
};
void sol() {
int m = 0;
FOR(i, 1, n) if (c[i].fi != -1) {
c[++m] = c[i];
}
n = m;
sort(c + 1, c + n + 1, [&](ii x, ii y) {
return x.fi + x.se < y.fi + y.se;
});
long long ans = LLONG_MAX;
ctdl a, b;
FOR(i, 1, n) {
b.ins(c[i].fi);
b.ins(c[i].se);
}
ans = b.cost();
FOR(i, 1, n - 1) {
a.ins(c[i].fi);
a.ins(c[i].se);
b.del(c[i].fi);
b.del(c[i].se);
ans = min(ans, a.cost() + b.cost());
}
cout << ans + ad;
}
};
void main_code() {
cin >> k >> n;
if (k == 1)
return SUB1::sol(), void();
int cnt = 0;
FOR(i, 1, n) {
char a, b;
cin >> a >> c[i].fi >> b >> c[i].se;
if (c[i].fi > c[i].se) swap(c[i].fi, c[i].se);
if (a == b) {
ad += abs(c[i].fi - c[i].se);
++cnt;
c[i].fi = -1;
} else
++ad;
}
if (cnt == n) {
cout << ad;
return ;
}
SUB_FULL::sol();
}
/* Let the river flows naturally */
Compilation message (stderr)
bridge.cpp: In function 'void SUB1::sol()':
bridge.cpp:79:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
79 | auto [l, r] = c[px[k].second];
| ^
bridge.cpp:84:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
84 | auto [l, r] = c[-px[k].second];
| ^
bridge.cpp: In function 'int main()':
bridge.cpp:36:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:37:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |