#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 N = 1; N < n - 1; N++){
vector <int> h = v;
vector <int> x;
for (int i = 0; i < n; i++){
x.push_back((v[i*2]+v[i*2+1]) / 2);
}
sort(x.begin(), x.end());
long long curans = 0;
vector <int> start(x.begin(), x.begin() + N);
vector <int> ends(x.begin() + N, x.end());
int f = findmedian(start);
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;
}
}
int g = findmedian(ends);
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] - f), c);
if (g > a[i*2+1]) c = min(1 + a[i*2+1] - a[i*2] + 2 * (f - a[i*2+1]), c);
if (c == LLONG_MAX){
}
curans += c;
}
}
}
ans = min(curans, ans);
}
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
0 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 |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
452 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 |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
452 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 |
348 KB |
Output is correct |
12 |
Correct |
29 ms |
3760 KB |
Output is correct |
13 |
Correct |
58 ms |
3716 KB |
Output is correct |
14 |
Correct |
36 ms |
3528 KB |
Output is correct |
15 |
Correct |
34 ms |
2384 KB |
Output is correct |
16 |
Correct |
38 ms |
3784 KB |
Output is correct |
17 |
Correct |
49 ms |
3780 KB |
Output is correct |
18 |
Correct |
48 ms |
3776 KB |
Output is correct |
19 |
Correct |
56 ms |
3784 KB |
Output is correct |
20 |
Correct |
43 ms |
3724 KB |
Output is correct |
21 |
Correct |
52 ms |
3672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |