#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> ii;
#define f(i,a,b) for(ll i = a; i < b; i++)
#define fa(i,a,b) for(ll i = a; i >= b; i--)
const int N = 30005;
const ll inf = 1e9, INF = 1e18;
int k, n;
ll ans, mini;
set <ll> pos;
vector <ii> v;
ll get(ll i, ll j){
ll res = 0;
for(auto p: v){
ll a = p.first, b = p.second;
res += 1 + min(abs(a-i)+abs(b-i), abs(a-j)+abs(b-j));
}
return res;
}
ll ge(ll i){
ll res = 0;
for(auto p: v){
ll a = p.first, b = p.second;
if(i < a) res += a + b + 1 - 2*i;
else if(b < i) res += 2*i + 1 - a - b;
else res += b - a + 1;
}
return res;
}
int main(){
cin >> k >> n;
f(i,1,n+1){
char x, y;
ll a, b;
cin >> x >> a >> y >> b;
if(x == y){
ans += abs(b-a);
}
else{
v.push_back({min(a,b), max(a, b)});
pos.insert(a), pos.insert(b);
}
}
ll mini = INF;
ll l = 0, r = inf;
while(l < r){
ll m = (l+r)>>1;
if(ge(m) < ge(m+1)) r = m;
else l = m+1;
}
mini = ge(l);
if(k == 2){
vector <ll> ra;
for(ll x: pos) ra.push_back(x);
int le = ra.size();
f(i,0,le-1){
ll x = ra[i];
ll l = i+1, r = le-1;
while(l < r){
ll m = (l+r)>>1;
if(get(x, ra[m]) < get(x, ra[m+1])) r = m;
else l = m+1;
}
mini = min(mini, get(x, ra[l]));
}
}
cout << ans + mini << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
3 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
212 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
55 ms |
2368 KB |
Output is correct |
13 |
Correct |
216 ms |
11280 KB |
Output is correct |
14 |
Correct |
86 ms |
2752 KB |
Output is correct |
15 |
Correct |
147 ms |
6624 KB |
Output is correct |
16 |
Correct |
74 ms |
2420 KB |
Output is correct |
17 |
Correct |
133 ms |
11244 KB |
Output is correct |
18 |
Correct |
123 ms |
11236 KB |
Output is correct |
19 |
Correct |
212 ms |
10800 KB |
Output is correct |
20 |
Correct |
104 ms |
2476 KB |
Output is correct |
21 |
Correct |
146 ms |
11248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
3 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
2 ms |
308 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
2 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
4 ms |
212 KB |
Output is correct |
15 |
Correct |
99 ms |
420 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
2 ms |
212 KB |
Output is correct |
18 |
Correct |
15 ms |
352 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
103 ms |
416 KB |
Output is correct |
21 |
Correct |
99 ms |
432 KB |
Output is correct |
22 |
Correct |
99 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
212 KB |
Output is correct |
24 |
Incorrect |
106 ms |
416 KB |
Output isn't correct |
25 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
4 ms |
212 KB |
Output is correct |
15 |
Correct |
103 ms |
420 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
2 ms |
212 KB |
Output is correct |
18 |
Correct |
13 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
103 ms |
340 KB |
Output is correct |
21 |
Correct |
101 ms |
340 KB |
Output is correct |
22 |
Correct |
99 ms |
412 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Incorrect |
104 ms |
416 KB |
Output isn't correct |
25 |
Halted |
0 ms |
0 KB |
- |