이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//{ <defines>
#ifndef _LOCAL
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")
#endif
#include <bits/stdc++.h>
using namespace std;
#define fr(i, n) for(int i = 0; i < n; ++i)
#define fo(n) fr(i, n)
#define re return
#define ef else if
#define ifn(x) if(!(x))
#define _ << ' ' <<
#define ft first
#define sd second
#define ve vector
#define pb push_back
#define eb emplace_back
#define sz(x) int(x.size())
#define pw(x) (1 << (x))
#define PW(x) (1ll << (x))
#define bnd(x) x.begin(), x.end()
#define clr(x, y) memset(x, y, sizeof x)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef ve<int> vi;
const int oo = 2e9;
const ll OO = 4e18;
//const ld pi = arg(complex<ld>(-1, 0));
//const ld pi2 = pi + pi;
const int md = 0x3b800001;
const int MD = 1e9 + 7;
inline ll time() {re chrono :: system_clock().now().time_since_epoch().count();}
mt19937 rnd(time());
mt19937_64 RND(time());
template<typename t> inline void umin(t &a, t b) {a = min(a, b);}
template<typename t> inline void umax(t &a, t b) {a = max(a, b);}
//} </defines>
const int N = 1e5 + 5;
int n, m;
ll pl[N], pr[N], l[N], r[N];
ll constant;
ll all_to(ll x, ll *a, ll *p) {
int t = lower_bound(a, a + m, x) - a;
re (t ? x * t - p[t - 1] : 0) +
(p[m - 1] - (t ? p[t - 1] : 0) - x * (m - t));
}
ll all_to(ll x) {
re all_to(x, r, pr) + all_to(x, l, pl);
}
ll solve1() {
ifn(m) re 0;
sort(l, l + m); partial_sum(l, l + m, pl);
sort(r, r + m); partial_sum(r, r + m, pr);
ll ans = OO;
fo(m) umin(ans, all_to(l[i]));
fo(m) umin(ans, all_to(r[i]));
re ans + m;
}
ll get(ll x0, ll x1) {
ll ans = 0;
fo(m) ans += min(abs(l[i] - x0) + abs(r[i] - x0),
abs(l[i] - x1) + abs(r[i] - x1));
re ans;
}
ll solve2() {
ifn(m) re 0;
vi x; fo(m) x.pb(l[i]), x.pb(r[i]);
ll ans = OO;
fo(sz(x)) fr(j, i + 1) umin(ans, get(x[i], x[j]));
re ans + m;
}
int main() {
#ifdef _LOCAL
freopen("in.txt", "r", stdin);
#endif
int tp;
cin >> tp >> n;
fo(n) {
char a, b;
int x, y;
cin >> a >> x >> b >> y;
if(x > y) swap(x, y);
if(a == b) constant += y - x;
else l[m] = x, r[m] = y, m++;
}
cout << constant + (tp & 1 ? solve1() : solve2()) << endl;
}
# | 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... |