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;
#ifdef MIKU
string dbmc = "\033[1;38;2;57;197;187m", dbrs = "\033[0m";
#define debug(x...) cout << dbmc << "[" << #x << "]: ", dout(x)
void dout() { cout << dbrs << endl; }
template <typename T, typename ...U>
void dout(T t, U ...u) { cout << t << (sizeof...(u) ? ", " : ""); dout(u...); }
#else
#define debug(...) 39
#endif
#define int long long
#define fs first
#define sc second
#define mp make_pair
#define FOR(i, j, k) for (int i = j, Z = k; i < Z; i++)
using ll = long long;
typedef pair<int, int> pii;
const int MXN = 100005;
int n, k;
vector<pii> v;
ll sum;
ll hl[MXN], hr[MXN];
struct SLOPE_TRICK {
int val, now;
multiset<int> L, R;
void init() {
val = 0;
now = INT_MAX;
L.clear();
R.clear();
L.insert(INT_MIN);
R.insert(INT_MAX);
}
void add(int x) {
val += abs(now - x);
if (L.lower_bound(x) == L.end()) {
if (x <= *R.begin()) {
L.insert(x);
R.insert(x);
val -= abs(now - x);
now = x;
} else {
R.insert(x);
R.insert(x);
L.insert(*R.begin());
R.erase(R.begin());
now = *R.begin();
}
} else {
L.insert(x);
L.insert(x);
R.insert(*L.rbegin());
L.erase(prev(L.end()));
int cur = *R.begin();
val -= (now - cur);
now = cur;
}
}
void COUT() {
debug(now, val);
for (auto i : L) cout << i << ' ';
cout << endl;
for (auto i : R) cout << i << ' ';
cout << endl;
}
} slt;
void miku() {
cin >> k >> n;
FOR(i, 0, n) {
int a, b;
char c, d;
cin >> c >> a >> d >> b;
if (a > b) swap(a, b);
if (c == d) {
sum += b - a;
} else {
sum += 1;
v.push_back(mp(a, b));
}
}
if (k == 1) {
slt.init();
for (auto &[a, b] : v) {
slt.add(a);
slt.add(b);
}
cout << sum + slt.val << '\n';
} else {
slt.init();
sort(v.begin(), v.end(), [](pii a, pii b) -> bool {
return a.fs + a.sc < b.fs + b.sc;
});
FOR(i, 0, v.size()) {
slt.add(v[i].fs);
slt.add(v[i].sc);
hl[i] = slt.val;
}
slt.init();
for (int i = v.size() - 1; i >= 0; i--) {
slt.add(v[i].fs);
slt.add(v[i].sc);
hr[i] = slt.val;
}
// FOR(i, 0, v.size()) cout << hl[i] << ' ';
// cout << endl;
// FOR(i, 0, v.size()) cout << hr[i] << ' ';
// cout << endl;
int ans = LLONG_MAX;
FOR(i, 0, v.size()) ans = min(ans, hl[i] + hr[i + 1]);
cout << ans + sum << '\n';
}
}
int32_t main() {
cin.tie(0) -> sync_with_stdio(false);
cin.exceptions(cin.failbit);
miku();
return 0;
}
Compilation message (stderr)
bridge.cpp: In member function 'void SLOPE_TRICK::COUT()':
bridge.cpp:11:20: warning: statement has no effect [-Wunused-value]
11 | #define debug(...) 39
| ^~
bridge.cpp:66:9: note: in expansion of macro 'debug'
66 | debug(now, val);
| ^~~~~
# | 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... |