#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 {
sort(v.begin(), v.end(), [](pii a, pii b) -> bool {
return a.fs + a.sc < b.fs + b.sc;
});
slt.init();
FOR(i, 0, v.size()) {
slt.add(v[i].fs);
slt.add(v[i].sc);
hl[i + 1] = slt.val;
}
slt.init();
for (int i = v.size() - 1; i >= 0; i--) {
slt.add(v[i].fs);
slt.add(v[i].sc);
hr[v.size() - i] = slt.val;
}
// FOR(i, 0, v.size() + 1) cout << hl[i] << ' ';
// cout << endl;
// FOR(i, 0, v.size() + 1) cout << hr[i] << ' ';
// cout << endl;
int ans = LLONG_MAX;
FOR(i, 0, v.size() + 1) ans = min(ans, hl[i] + hr[v.size() - i]);
// 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
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 |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
548 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
592 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
764 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
100 ms |
20684 KB |
Output is correct |
13 |
Correct |
164 ms |
20720 KB |
Output is correct |
14 |
Correct |
139 ms |
18628 KB |
Output is correct |
15 |
Correct |
68 ms |
12324 KB |
Output is correct |
16 |
Correct |
128 ms |
20676 KB |
Output is correct |
17 |
Correct |
134 ms |
20600 KB |
Output is correct |
18 |
Correct |
121 ms |
20660 KB |
Output is correct |
19 |
Correct |
124 ms |
20676 KB |
Output is correct |
20 |
Correct |
107 ms |
20680 KB |
Output is correct |
21 |
Correct |
111 ms |
20760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
600 KB |
Output is correct |
15 |
Correct |
2 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
476 KB |
Output is correct |
22 |
Correct |
2 ms |
604 KB |
Output is correct |
23 |
Correct |
2 ms |
604 KB |
Output is correct |
24 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
600 KB |
Output is correct |
15 |
Correct |
2 ms |
516 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
2 ms |
600 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
2 ms |
600 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
265 ms |
23100 KB |
Output is correct |
26 |
Correct |
305 ms |
23240 KB |
Output is correct |
27 |
Correct |
348 ms |
24004 KB |
Output is correct |
28 |
Correct |
359 ms |
24572 KB |
Output is correct |
29 |
Correct |
336 ms |
24568 KB |
Output is correct |
30 |
Correct |
124 ms |
12748 KB |
Output is correct |
31 |
Correct |
221 ms |
23492 KB |
Output is correct |
32 |
Correct |
218 ms |
24048 KB |
Output is correct |
33 |
Correct |
156 ms |
24000 KB |
Output is correct |
34 |
Correct |
201 ms |
24012 KB |
Output is correct |
35 |
Correct |
219 ms |
23764 KB |
Output is correct |
36 |
Correct |
212 ms |
24024 KB |
Output is correct |
37 |
Correct |
231 ms |
22876 KB |
Output is correct |