/// adil sultanov | vonat1us
#pragma GCC optimize("O3")
//#pragma comment(linker, "/STACK:36777216")
#include<bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define sz(x) (int) x.size()
#define all(z) (z).begin(), (z).end()
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 1e2;
void fin() {
#ifdef AM
freopen(".in", "r", stdin);
#endif
}
const bool flag = 0;
const int N = 1e5+10;
void ma1n() {
int k, n;
cin >> k >> n;
if (k == 2) return;
ll ans = 0;
vector<pii> v;
vector<int> all;
for (int i = 0; i < n; ++i) {
char c1, c2;
int l, r;
cin >> c1 >> l >> c2 >> r;
if (l > r) swap(l, r);
ans += r-l;
if (c1 != c2) {
ans++;
v.pb({l, r});
all.pb(l);
all.pb(r);
}
}
n = sz(v);
if (all.empty()) {
cout << ans, exit(0);
}
sort(all(all));
//all.erase(unique(all(all)), all.end());
ll mn = (1ll<<50);
int mid = all[sz(all)/2];
for (int i = max(0, mid-1000); i <= min((int)1e9, 1000+mid); ++i) {
ll res = 0;
for (auto [l, r] : v) {
if (i < l) {
res += (l-i)*2ll;
}
if (i > r) {
res += (i-r)*2ll;
}
}
mn = min(mn, res);
}
cout << ans + mn;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(nullptr), fin();
int ts = 1;
if (flag) {
cin >> ts;
}
while (ts--) {
ma1n();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
12 ms |
336 KB |
Output is correct |
5 |
Correct |
9 ms |
332 KB |
Output is correct |
6 |
Correct |
5 ms |
204 KB |
Output is correct |
7 |
Correct |
3 ms |
332 KB |
Output is correct |
8 |
Correct |
7 ms |
336 KB |
Output is correct |
9 |
Correct |
7 ms |
332 KB |
Output is correct |
10 |
Correct |
7 ms |
332 KB |
Output is correct |
11 |
Correct |
7 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
10 ms |
332 KB |
Output is correct |
5 |
Correct |
10 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
3 ms |
332 KB |
Output is correct |
8 |
Correct |
5 ms |
332 KB |
Output is correct |
9 |
Correct |
10 ms |
332 KB |
Output is correct |
10 |
Correct |
7 ms |
332 KB |
Output is correct |
11 |
Correct |
8 ms |
348 KB |
Output is correct |
12 |
Correct |
165 ms |
2488 KB |
Output is correct |
13 |
Correct |
1242 ms |
4380 KB |
Output is correct |
14 |
Correct |
1123 ms |
3104 KB |
Output is correct |
15 |
Correct |
705 ms |
2816 KB |
Output is correct |
16 |
Correct |
146 ms |
3756 KB |
Output is correct |
17 |
Correct |
229 ms |
4320 KB |
Output is correct |
18 |
Correct |
319 ms |
4016 KB |
Output is correct |
19 |
Correct |
1138 ms |
4408 KB |
Output is correct |
20 |
Correct |
897 ms |
3896 KB |
Output is correct |
21 |
Correct |
1157 ms |
4152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |