#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// Ofast, O0, O1, O2, O3, unroll-loops, fast-math, trapv
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define Mp make_pair
#define sep ' '
#define endl '\n'
#define F first
#define S second
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define kill(res) cout << res << '\n', exit(0);
#define set_dec(x) cout << fixed << setprecision(x);
#define fast_io ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("input.txt", "r", stdin) ; freopen("output.txt", "w", stdout);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll N = 2e5 + 50;
const ll Mod = 1e9 + 7;
ll n, m, k, pre[N], nxt[N], val[N], cnt, med, ans, pref[N], suf[N];
set<pii> ms;
vector<pii> vec;
bool cmp(pii i, pii j){
return (i.F + i.S) < (j.F + j.S);
}
void add(int x){
int aft = ms.lower_bound(Mp(x, ++cnt)) -> S;
ms.insert(Mp(x, cnt));
val[cnt] = x; pre[cnt] = pre[aft]; nxt[pre[cnt]] = cnt; nxt[cnt] = aft; pre[aft] = cnt;
ans += abs(val[med] - x);
if(cnt % 2 == 0 && x < val[med]){
med = pre[med];
}
if(cnt % 2 == 1 && x >= val[med]){
ans -= val[nxt[med]] - val[med];
med = nxt[med];
}
}
int main(){
fast_io;
cin >> k >> n;
ll sum = 0;
for(int i = 1; i <= n; i++){
char p, q; int s, t;
cin >> p >> s >> q >> t;
if(p != q){
vec.pb(Mp(s, t)); sum++;
}
else sum += abs(s - t);
}
if(vec.empty()) kill(sum);
vec.pb(Mp(-1, -1));
sort(all(vec), cmp);
cnt = 2;
nxt[1] = 2; pre[2] = 1;
val[1] = -1; val[2] = Mod;
ms.insert(Mp(val[1], 1));
ms.insert(Mp(val[2], 2));
med = 1; ans = 0;
m = vec.size() - 1; pref[0] = 0;
for(int i = 1; i <= m; i++){
add(vec[i].F); add(vec[i].S);
pref[i] = ans;
}
fill(pre, pre + N, 0);
fill(nxt, nxt + N, 0);
ms.clear();
cnt = 2;
nxt[1] = 2; pre[2] = 1;
val[1] = -1; val[2] = Mod;
ms.insert(Mp(val[1], 1));
ms.insert(Mp(val[2], 2));
med = 1; ans = 0;
suf[m+1] = 0;
for(int i = m; i > 0; i--){
add(vec[i].F); add(vec[i].S);
suf[i] = ans;
}
ll res = pref[m];
if(k == 1) kill(res + sum);
for(int i = 1; i < m; i++) res = min(res, pref[i] + suf[i+1]);
kill(res + sum);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
8028 KB |
Output is correct |
4 |
Correct |
3 ms |
8028 KB |
Output is correct |
5 |
Correct |
3 ms |
8160 KB |
Output is correct |
6 |
Correct |
2 ms |
8028 KB |
Output is correct |
7 |
Correct |
2 ms |
8028 KB |
Output is correct |
8 |
Correct |
2 ms |
8028 KB |
Output is correct |
9 |
Correct |
2 ms |
8028 KB |
Output is correct |
10 |
Correct |
3 ms |
8156 KB |
Output is correct |
11 |
Correct |
2 ms |
8028 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 |
2 ms |
8028 KB |
Output is correct |
4 |
Correct |
3 ms |
8028 KB |
Output is correct |
5 |
Correct |
3 ms |
8028 KB |
Output is correct |
6 |
Correct |
2 ms |
8184 KB |
Output is correct |
7 |
Correct |
2 ms |
8028 KB |
Output is correct |
8 |
Correct |
3 ms |
8196 KB |
Output is correct |
9 |
Correct |
2 ms |
8028 KB |
Output is correct |
10 |
Correct |
2 ms |
8028 KB |
Output is correct |
11 |
Correct |
2 ms |
8028 KB |
Output is correct |
12 |
Correct |
106 ms |
19072 KB |
Output is correct |
13 |
Correct |
191 ms |
20616 KB |
Output is correct |
14 |
Correct |
192 ms |
18476 KB |
Output is correct |
15 |
Correct |
104 ms |
15476 KB |
Output is correct |
16 |
Correct |
103 ms |
19956 KB |
Output is correct |
17 |
Correct |
109 ms |
20656 KB |
Output is correct |
18 |
Correct |
102 ms |
20236 KB |
Output is correct |
19 |
Correct |
97 ms |
20688 KB |
Output is correct |
20 |
Correct |
113 ms |
20172 KB |
Output is correct |
21 |
Correct |
108 ms |
20424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
8028 KB |
Output is correct |
4 |
Correct |
2 ms |
8028 KB |
Output is correct |
5 |
Correct |
2 ms |
8028 KB |
Output is correct |
6 |
Correct |
2 ms |
8028 KB |
Output is correct |
7 |
Correct |
2 ms |
8028 KB |
Output is correct |
8 |
Correct |
2 ms |
7892 KB |
Output is correct |
9 |
Correct |
2 ms |
8028 KB |
Output is correct |
10 |
Correct |
2 ms |
8028 KB |
Output is correct |
11 |
Correct |
2 ms |
8028 KB |
Output is correct |
12 |
Correct |
2 ms |
7892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
8028 KB |
Output is correct |
4 |
Correct |
2 ms |
8024 KB |
Output is correct |
5 |
Correct |
2 ms |
8028 KB |
Output is correct |
6 |
Correct |
1 ms |
8024 KB |
Output is correct |
7 |
Correct |
2 ms |
8028 KB |
Output is correct |
8 |
Correct |
2 ms |
8028 KB |
Output is correct |
9 |
Correct |
1 ms |
8028 KB |
Output is correct |
10 |
Correct |
2 ms |
8028 KB |
Output is correct |
11 |
Correct |
2 ms |
7904 KB |
Output is correct |
12 |
Correct |
2 ms |
7884 KB |
Output is correct |
13 |
Correct |
2 ms |
8028 KB |
Output is correct |
14 |
Correct |
2 ms |
8028 KB |
Output is correct |
15 |
Correct |
2 ms |
8028 KB |
Output is correct |
16 |
Correct |
2 ms |
8028 KB |
Output is correct |
17 |
Correct |
2 ms |
8028 KB |
Output is correct |
18 |
Correct |
2 ms |
8028 KB |
Output is correct |
19 |
Correct |
2 ms |
8028 KB |
Output is correct |
20 |
Correct |
3 ms |
8028 KB |
Output is correct |
21 |
Correct |
2 ms |
8028 KB |
Output is correct |
22 |
Correct |
2 ms |
8024 KB |
Output is correct |
23 |
Correct |
2 ms |
8028 KB |
Output is correct |
24 |
Correct |
2 ms |
8028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
8028 KB |
Output is correct |
4 |
Correct |
2 ms |
8024 KB |
Output is correct |
5 |
Correct |
2 ms |
8072 KB |
Output is correct |
6 |
Correct |
2 ms |
8028 KB |
Output is correct |
7 |
Correct |
2 ms |
8028 KB |
Output is correct |
8 |
Correct |
1 ms |
8028 KB |
Output is correct |
9 |
Correct |
2 ms |
7892 KB |
Output is correct |
10 |
Correct |
1 ms |
8024 KB |
Output is correct |
11 |
Correct |
2 ms |
8028 KB |
Output is correct |
12 |
Correct |
2 ms |
8024 KB |
Output is correct |
13 |
Correct |
3 ms |
8028 KB |
Output is correct |
14 |
Correct |
3 ms |
8028 KB |
Output is correct |
15 |
Correct |
2 ms |
8028 KB |
Output is correct |
16 |
Correct |
2 ms |
8028 KB |
Output is correct |
17 |
Correct |
2 ms |
8028 KB |
Output is correct |
18 |
Correct |
2 ms |
8028 KB |
Output is correct |
19 |
Correct |
3 ms |
8080 KB |
Output is correct |
20 |
Correct |
2 ms |
8280 KB |
Output is correct |
21 |
Correct |
2 ms |
8028 KB |
Output is correct |
22 |
Correct |
2 ms |
8024 KB |
Output is correct |
23 |
Correct |
3 ms |
8028 KB |
Output is correct |
24 |
Correct |
2 ms |
8028 KB |
Output is correct |
25 |
Correct |
106 ms |
19068 KB |
Output is correct |
26 |
Correct |
158 ms |
19400 KB |
Output is correct |
27 |
Correct |
199 ms |
20020 KB |
Output is correct |
28 |
Correct |
194 ms |
20568 KB |
Output is correct |
29 |
Correct |
203 ms |
20812 KB |
Output is correct |
30 |
Correct |
86 ms |
14804 KB |
Output is correct |
31 |
Correct |
106 ms |
19956 KB |
Output is correct |
32 |
Correct |
104 ms |
20756 KB |
Output is correct |
33 |
Correct |
98 ms |
20172 KB |
Output is correct |
34 |
Correct |
79 ms |
20684 KB |
Output is correct |
35 |
Correct |
109 ms |
20164 KB |
Output is correct |
36 |
Correct |
100 ms |
20424 KB |
Output is correct |
37 |
Correct |
110 ms |
19152 KB |
Output is correct |