#include <bits/stdc++.h>
using namespace std;
int k, n;
long long curans;
vector <int> h;
vector <int> q;
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){
for (int i = 0; i < j.size(); i++){
q.push_back(j[i].first);
q.push_back(j[i].second);
}
q.erase(remove(q.begin(), q.end(), -1), q.end());
sort (q.begin(), q.end());
if (q.size() == 0) return 0;
return (q[q.size() / 2] + q[q.size() / 2 - 1]) / 2;
}
int main(){
long long ans = 0, dans = 0;
int k, n;
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> k >> n;
int 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 {
dans++;
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]);
}
dans += a[i*2+1] - a[i*2];
}
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;
long long xans;
int f, g, c;
sort(v.begin(), v.end(), sorting);
vector <pair<int, int>>start(v.begin(), v.begin() + 1);
vector <pair<int, int>>ends(v.begin() + 1, v.end());
for (int N = 1; N <= n; N++){
h = z;
curans = dans;
if (N != 1){
start.push_back(v[N-1]);
ends.erase(ends.begin());
}
f = findmedian(start);
q.clear();
g = findmedian(ends);
q.clear();
for (int i = 0; i < n; i++){
if ((f >= a[i*2] && f <= a[i*2+1]) || (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 (h[i*2] != -1){
if (f < a[i*2]) c = 2 * (a[i*2] - f);
else c = 2 * (f - a[i*2+1]);
if (g < a[i*2]) c = min(2 * (a[i*2] - g), c);
else c = min(2 * (g - a[i*2+1]), c);
curans += c;
}
}
ans = min(curans, ans);
}
}
cout << ans;
}
Compilation message
bridge.cpp: In function 'int findmedian(std::vector<std::pair<int, int> >)':
bridge.cpp:12:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
12 | for (int i = 0; i < j.size(); i++){
| ~~^~~~~~~~~~
bridge.cpp: In function 'int main()':
bridge.cpp:77:23: warning: unused variable 'xans' [-Wunused-variable]
77 | long long xans;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
600 KB |
Output is correct |
5 |
Correct |
0 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
20 ms |
5268 KB |
Output is correct |
13 |
Correct |
26 ms |
5320 KB |
Output is correct |
14 |
Correct |
19 ms |
4804 KB |
Output is correct |
15 |
Correct |
15 ms |
3036 KB |
Output is correct |
16 |
Correct |
15 ms |
5256 KB |
Output is correct |
17 |
Correct |
17 ms |
5316 KB |
Output is correct |
18 |
Correct |
20 ms |
5320 KB |
Output is correct |
19 |
Correct |
25 ms |
5332 KB |
Output is correct |
20 |
Correct |
17 ms |
5320 KB |
Output is correct |
21 |
Correct |
22 ms |
5268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
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 |
344 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 |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
16 ms |
568 KB |
Output is correct |
14 |
Correct |
72 ms |
540 KB |
Output is correct |
15 |
Correct |
66 ms |
552 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
17 |
Correct |
16 ms |
540 KB |
Output is correct |
18 |
Correct |
10 ms |
348 KB |
Output is correct |
19 |
Correct |
33 ms |
564 KB |
Output is correct |
20 |
Correct |
26 ms |
572 KB |
Output is correct |
21 |
Correct |
62 ms |
344 KB |
Output is correct |
22 |
Correct |
21 ms |
348 KB |
Output is correct |
23 |
Correct |
16 ms |
348 KB |
Output is correct |
24 |
Correct |
21 ms |
572 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 |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 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 |
388 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
18 ms |
556 KB |
Output is correct |
14 |
Correct |
77 ms |
556 KB |
Output is correct |
15 |
Correct |
83 ms |
556 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
17 |
Correct |
17 ms |
540 KB |
Output is correct |
18 |
Correct |
10 ms |
348 KB |
Output is correct |
19 |
Correct |
25 ms |
556 KB |
Output is correct |
20 |
Correct |
21 ms |
344 KB |
Output is correct |
21 |
Correct |
57 ms |
344 KB |
Output is correct |
22 |
Correct |
23 ms |
348 KB |
Output is correct |
23 |
Correct |
16 ms |
344 KB |
Output is correct |
24 |
Correct |
23 ms |
572 KB |
Output is correct |
25 |
Execution timed out |
2092 ms |
7448 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |