#include <bits/stdc++.h>
using namespace std;
#define oo 1000000000000000000ll
#define int long long
struct data{
char t1, t2;
int x1, x2;
};
signed main() {
int k, n; cin >> k >> n;
if (k == 1){
int ans = 0;
vector<int> arr;
for (int i = 0; i < n; ++i){
char t1, t2; int x1, x2;
cin >> t1 >> x1 >> t2 >> x2;
if (t1 == t2) ans += abs(x2 - x1);
else arr.push_back(x1), arr.push_back(x2);
}
if (arr.size() == 0){
cout << ans << "\n";
return 0;
}
sort(arr.begin(), arr.end());
vector<int> pref(arr.size()); pref[0] = arr[0];
for (int i = 1; i < arr.size(); ++i) pref[i] = pref[i-1] + arr[i];
vector<int> suf(arr.size()); suf.back() = arr.back();
for (int i = arr.size() - 2; i >= 0; --i) suf[i] = suf[i+1] + arr[i];
int mn = oo;
for (int i = 0; i < arr.size(); ++i){
mn = min(mn, arr[i] * (i+1) - pref[i] + suf[i] - arr[i] * ((int)arr.size() - i));
}
ans += (int)arr.size() / 2ll;
ans += mn;
cout << ans << "\n";
} else{
vector<int> arr;
vector<data> v(n);
for (int i = 0; i < n; ++i){
char t1, t2; int x1, x2;
cin >> t1 >> x1 >> t2 >> x2;
v[i] = {t1, t2, x1, x2};
arr.push_back(x1), arr.push_back(x2);
}
int ans = oo;
for (int i = 0; i < arr.size(); ++i){
for (int j = i+1; j < arr.size(); ++j){
int cur = 0;
for (int x = 0; x < n; ++x){
if (v[x].t1 == v[x].t2) cur += abs(v[x].x1 - v[x].x2);
else{
int c = abs(v[x].x1 - arr[i]) + abs(v[x].x2 - arr[i]) + 1ll;
c = min(c, abs(v[x].x1 - arr[j]) + abs(v[x].x2 - arr[j]) + 1ll);
cur += c;
}
}
ans = min(ans, cur);
}
}
cout << ans << "\n";
}
return 0;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:32:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for (int i = 1; i < arr.size(); ++i) pref[i] = pref[i-1] + arr[i];
| ~~^~~~~~~~~~~~
bridge.cpp:37:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for (int i = 0; i < arr.size(); ++i){
| ~~^~~~~~~~~~~~
bridge.cpp:55:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for (int i = 0; i < arr.size(); ++i){
| ~~^~~~~~~~~~~~
bridge.cpp:56:33: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for (int j = i+1; j < arr.size(); ++j){
| ~~^~~~~~~~~~~~
# |
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 |
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 |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 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 |
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 |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
312 KB |
Output is correct |
9 |
Correct |
3 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
2 ms |
340 KB |
Output is correct |
12 |
Correct |
70 ms |
5368 KB |
Output is correct |
13 |
Correct |
114 ms |
5320 KB |
Output is correct |
14 |
Correct |
82 ms |
4928 KB |
Output is correct |
15 |
Correct |
73 ms |
3388 KB |
Output is correct |
16 |
Correct |
86 ms |
5352 KB |
Output is correct |
17 |
Correct |
104 ms |
5296 KB |
Output is correct |
18 |
Correct |
92 ms |
5368 KB |
Output is correct |
19 |
Correct |
109 ms |
5276 KB |
Output is correct |
20 |
Correct |
109 ms |
5296 KB |
Output is correct |
21 |
Correct |
111 ms |
5256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
6 ms |
212 KB |
Output is correct |
4 |
Correct |
7 ms |
212 KB |
Output is correct |
5 |
Correct |
2 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
10 ms |
212 KB |
Output is correct |
8 |
Correct |
8 ms |
296 KB |
Output is correct |
9 |
Correct |
10 ms |
212 KB |
Output is correct |
10 |
Correct |
8 ms |
296 KB |
Output is correct |
11 |
Correct |
8 ms |
296 KB |
Output is correct |
12 |
Correct |
7 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 |
7 ms |
304 KB |
Output is correct |
4 |
Correct |
6 ms |
212 KB |
Output is correct |
5 |
Correct |
2 ms |
300 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
8 ms |
212 KB |
Output is correct |
8 |
Correct |
10 ms |
212 KB |
Output is correct |
9 |
Correct |
7 ms |
212 KB |
Output is correct |
10 |
Correct |
7 ms |
212 KB |
Output is correct |
11 |
Correct |
8 ms |
212 KB |
Output is correct |
12 |
Correct |
9 ms |
212 KB |
Output is correct |
13 |
Execution timed out |
2089 ms |
340 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
7 ms |
212 KB |
Output is correct |
4 |
Correct |
6 ms |
212 KB |
Output is correct |
5 |
Correct |
2 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
8 ms |
308 KB |
Output is correct |
8 |
Correct |
7 ms |
212 KB |
Output is correct |
9 |
Correct |
6 ms |
212 KB |
Output is correct |
10 |
Correct |
7 ms |
296 KB |
Output is correct |
11 |
Correct |
7 ms |
212 KB |
Output is correct |
12 |
Correct |
8 ms |
212 KB |
Output is correct |
13 |
Execution timed out |
2080 ms |
340 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |