#include <bits/stdc++.h>
using namespace std;
int k, n;
bool sorting(pair <int, int> a, pair <int, int> b){
return a.first + a.second < b.first + b.second;
}
int findmedian(vector<pair<int, int>> j){
vector <int> d;
for (int i = 0; i < j.size(); i++){
d.push_back(j[i].first);
d.push_back(j[i].second);
}
d.erase(remove(d.begin(), d.end(), -1), d.end());
sort (d.begin(), d.end());
if (d.size() == 0) return 0;
return (d[d.size() / 2] + d[d.size() / 2 - 1]) / 2;
}
int main(){
long long ans = 0;
priority_queue <int> q;
int k, n;
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> k >> n;
long long a[n*2];
vector <int> z;
vector <pair<int,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, -1});
z.push_back(-1);
z.push_back(-1);
}
else {
v.push_back({a[i*2], a[i*2+1]});
z.push_back(a[i*2]);
z.push_back(a[i*2+1]);
}
if (a[i*2] > a[i*2+1]){
swap(a[i*2], a[i*2+1]);
}
}
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].first = -1;
v[i].second = -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].first == -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; N++){
vector <int> h = z;
sort(v.begin(), v.end(), sorting);
long long curans = 0;
vector <pair<int, int>> start(v.begin(), v.begin() + N);
vector <pair<int, int>> ends(v.begin() + N, v.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] - 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);
if (c == LLONG_MAX){
}
curans += c;
}
}
}
ans = min(curans, ans);
}
}
cout << ans;
}
Compilation message
bridge.cpp: In function 'int findmedian(std::vector<std::pair<int, int> >)':
bridge.cpp:10:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
10 | for (int i = 0; i < j.size(); i++){
| ~~^~~~~~~~~~
# |
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 |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 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 |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
392 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
13 ms |
6088 KB |
Output is correct |
13 |
Correct |
33 ms |
6088 KB |
Output is correct |
14 |
Correct |
23 ms |
5576 KB |
Output is correct |
15 |
Correct |
15 ms |
3536 KB |
Output is correct |
16 |
Correct |
20 ms |
5940 KB |
Output is correct |
17 |
Correct |
19 ms |
6084 KB |
Output is correct |
18 |
Correct |
28 ms |
5904 KB |
Output is correct |
19 |
Correct |
28 ms |
6000 KB |
Output is correct |
20 |
Correct |
18 ms |
6088 KB |
Output is correct |
21 |
Correct |
24 ms |
6104 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 |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 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 |
484 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 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 |
480 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
500 KB |
Output is correct |
13 |
Correct |
37 ms |
348 KB |
Output is correct |
14 |
Correct |
78 ms |
344 KB |
Output is correct |
15 |
Correct |
94 ms |
348 KB |
Output is correct |
16 |
Correct |
5 ms |
348 KB |
Output is correct |
17 |
Correct |
23 ms |
516 KB |
Output is correct |
18 |
Correct |
13 ms |
348 KB |
Output is correct |
19 |
Correct |
37 ms |
572 KB |
Output is correct |
20 |
Correct |
46 ms |
348 KB |
Output is correct |
21 |
Correct |
69 ms |
568 KB |
Output is correct |
22 |
Correct |
43 ms |
344 KB |
Output is correct |
23 |
Correct |
35 ms |
348 KB |
Output is correct |
24 |
Correct |
43 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
38 ms |
348 KB |
Output is correct |
14 |
Correct |
79 ms |
344 KB |
Output is correct |
15 |
Correct |
88 ms |
344 KB |
Output is correct |
16 |
Correct |
4 ms |
360 KB |
Output is correct |
17 |
Correct |
23 ms |
564 KB |
Output is correct |
18 |
Correct |
12 ms |
348 KB |
Output is correct |
19 |
Correct |
40 ms |
348 KB |
Output is correct |
20 |
Correct |
42 ms |
344 KB |
Output is correct |
21 |
Correct |
69 ms |
348 KB |
Output is correct |
22 |
Correct |
43 ms |
348 KB |
Output is correct |
23 |
Correct |
35 ms |
596 KB |
Output is correct |
24 |
Correct |
43 ms |
348 KB |
Output is correct |
25 |
Execution timed out |
2057 ms |
8436 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |