#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, k, sm, cnt;
vector<pair<ll, ll>> vec;
vector<ll> l, r, both;
int main(){
cin >> k >> n;
for (ll i = 0; i < n; i ++){
char p, q;
ll s, t;
cin >> p >> s >> q >> t;
if (s > t) swap(s, t);
if (p == q){
cnt++;
sm += t - s;
}
else{
vec.push_back({s, t});
l.push_back(s);
r.push_back(t);
both.push_back(s);
both.push_back(t);
}
}
sort(vec.begin(), vec.end());
sort(l.begin(), l.end());
sort(r.begin(), r.end());
sort(both.begin(), both.end());
ll ans = 1e18;
if (k == 1){
ll prev = 0;
ll after = 0;
for (ll i = 1; i < both.size(); i ++)
after += both[i] - both[0];
ans = min(ans, sm + after + n - cnt);
for (ll i = 1; i < both.size(); i ++){
after -= (both[i] - both[i - 1]) * (both.size() - i);
prev += (both[i] - both[i - 1]) * i;
ans = min(ans, sm + prev + after + n - cnt);
}
cout << ans << endl;
}
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:43:26: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for (ll i = 1; i < both.size(); i ++)
| ~~^~~~~~~~~~~~~
bridge.cpp:47:26: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for (ll i = 1; i < both.size(); i ++){
| ~~^~~~~~~~~~~~~
# |
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 |
1 ms |
344 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 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 |
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 |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
40 ms |
6344 KB |
Output is correct |
13 |
Correct |
87 ms |
6484 KB |
Output is correct |
14 |
Correct |
62 ms |
5944 KB |
Output is correct |
15 |
Correct |
49 ms |
3580 KB |
Output is correct |
16 |
Correct |
50 ms |
6448 KB |
Output is correct |
17 |
Correct |
62 ms |
6368 KB |
Output is correct |
18 |
Correct |
59 ms |
6448 KB |
Output is correct |
19 |
Correct |
85 ms |
6480 KB |
Output is correct |
20 |
Correct |
58 ms |
6452 KB |
Output is correct |
21 |
Correct |
83 ms |
6364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |