#include <bits/stdc++.h>
using namespace std;
int k, n;
int findmedian(vector<int> j){
j.erase(remove(j.begin(), j.end(), -1), j.end());
sort (j.begin(), j.end());
if (j.size() == 0) return 0;
return (j[j.size() / 2] + j[j.size() / 2 - 1]) / 2;
}
int main(){
long long ans = 0;
priority_queue <int> q;
int k, n;
cin >> k >> n;
long long a[n*2];
vector <int> v;
char b[n], d[n];
for (int i = 0; i < n; i++){
cin >> b[i] >> a[i*2] >> d[i] >> a[i*2+1];
if (b[i] == d[i]){
v.push_back(-1);
v.push_back(-1);
}
else {
v.push_back(a[i*2]);
v.push_back(a[i*2+1]);
}
if (a[i*2] > a[i*2+1]){
int temp = a[i*2];
a[i*2] = a[i*2+1];
a[i*2+1] = temp;
}
}
int f;
if (k == 1){
while (k--){
f = findmedian(v);
for (int i = 0; i < n; i++){
if (f >= a[i*2] && f <= a[i*2+1]){
v[i*2] = -1;
v[i*2+1] = -1;
}
}
}
for (int i = 0; i < n; i++){
if (b[i] == d[i]){
ans += a[i*2+1] - a[i*2];
}
else{
if (v[i*2] == -1){
ans += a[i*2+1] - a[i*2] + 1;
}
else{
if (f < a[i*2]) ans += 1 + a[i*2+1] - a[i*2] + 2 * (a[i*2] - f);
if (f > a[i*2+1]) ans += 1 + a[i*2+1] - a[i*2] + 2 * (f - a[i*2+1]);
}
}
}
}
else{
ans = LLONG_MAX;
for (int z = 0; z < 2 * n; z++){
for (int y = 0; y < 2 * n; y++){
long long curans = 0;
int f = a[z];
int g = a[y];
vector <int> h = v;
for (int i = 0; i < n; i++){
if (f >= a[i*2] && f <= a[i*2+1]){
h[i*2] = -1;
h[i*2+1] = -1;
}
}
for (int i = 0; i < n; i++){
if (g >= a[i*2] && g <= a[i*2+1]){
h[i*2] = -1;
h[i*2+1] = -1;
}
}
for (int i = 0; i < n; i++){
if (b[i] == d[i]){
curans += a[i*2+1] - a[i*2];
}
else{
if (h[i*2] == -1){
curans += a[i*2+1] - a[i*2] + 1;
}
else{
long long c = LLONG_MAX;
if (f < a[i*2]) c = min(1 + a[i*2+1] - a[i*2] + 2 * (a[i*2] - f), c);
if (f > a[i*2+1]) c = min(1 + a[i*2+1] - a[i*2] + 2 * (f - a[i*2+1]), c);
if (g < a[i*2]) c = min(1 + a[i*2+1] - a[i*2] + 2 * (a[i*2] - g), c);
if (g > a[i*2+1]) c = min(1 + a[i*2+1] - a[i*2] + 2 * (g - a[i*2+1]), c);
curans += c;
}
}
}
ans = min(curans, ans);
}
}
}
cout << ans;
}
# |
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 |
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 |
0 ms |
452 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
428 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 |
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 |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
25 ms |
3840 KB |
Output is correct |
13 |
Correct |
63 ms |
3756 KB |
Output is correct |
14 |
Correct |
59 ms |
3524 KB |
Output is correct |
15 |
Correct |
32 ms |
2504 KB |
Output is correct |
16 |
Correct |
75 ms |
3744 KB |
Output is correct |
17 |
Correct |
82 ms |
3784 KB |
Output is correct |
18 |
Correct |
46 ms |
3784 KB |
Output is correct |
19 |
Correct |
54 ms |
3796 KB |
Output is correct |
20 |
Correct |
42 ms |
3780 KB |
Output is correct |
21 |
Correct |
49 ms |
3780 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 |
11 ms |
436 KB |
Output is correct |
4 |
Correct |
16 ms |
436 KB |
Output is correct |
5 |
Correct |
4 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
15 ms |
348 KB |
Output is correct |
8 |
Correct |
14 ms |
344 KB |
Output is correct |
9 |
Correct |
24 ms |
348 KB |
Output is correct |
10 |
Correct |
16 ms |
344 KB |
Output is correct |
11 |
Correct |
13 ms |
348 KB |
Output is correct |
12 |
Correct |
15 ms |
436 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 |
11 ms |
440 KB |
Output is correct |
4 |
Correct |
16 ms |
344 KB |
Output is correct |
5 |
Correct |
4 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
11 ms |
436 KB |
Output is correct |
8 |
Correct |
14 ms |
348 KB |
Output is correct |
9 |
Correct |
15 ms |
436 KB |
Output is correct |
10 |
Correct |
17 ms |
348 KB |
Output is correct |
11 |
Correct |
12 ms |
440 KB |
Output is correct |
12 |
Correct |
15 ms |
348 KB |
Output is correct |
13 |
Execution timed out |
2094 ms |
348 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
12 ms |
432 KB |
Output is correct |
4 |
Correct |
18 ms |
432 KB |
Output is correct |
5 |
Correct |
7 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
21 ms |
436 KB |
Output is correct |
8 |
Correct |
14 ms |
348 KB |
Output is correct |
9 |
Correct |
13 ms |
436 KB |
Output is correct |
10 |
Correct |
24 ms |
344 KB |
Output is correct |
11 |
Correct |
27 ms |
436 KB |
Output is correct |
12 |
Correct |
16 ms |
436 KB |
Output is correct |
13 |
Execution timed out |
2063 ms |
348 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |