#include <bits/stdc++.h>
#define st first
#define nd second
using namespace std;
const long long INF = 1e7;
const int MOD = 1e9+7;
const int MAXN = 30000;
long long n, k, ans, cnt;
vector < pair<int,int> > a;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
cin >> k >> n;
vector < long long > v, v1, v2;
for(int i = 1; i <= n; i++) {
long long x1, x2;
char c1, c2;
cin >> c1 >> x1 >> c2 >> x2;
if(c1 == c2) ans += abs(x2-x1);
else {
v.push_back(x1);
v.push_back(x2);
v1.push_back(x1);
v2.push_back(x2);
cnt++;
}
}
ans += cnt;
sort(v.begin(),v.end());
//for(int i = 0; i < v.size(); i++)
// cout << v[i] << " ";
//cout << endl;
if(k == 2) {
v.push_back(INF);
long long num = INF;
for(int i = 0; i < v.size(); i++){
for(int j = i+1; j < v.size(); j++){
long long b1, b2;
b1 = v[i]; b2 = v[j];
if(b1 == b2) continue;
long long sum = 0;
// if(b1 == 2 && b2 == 6){
for(int k = 0; k < v1.size(); k++)//{
sum += min(abs(b1-v1[k])+abs(b1-v2[k]),
abs(b2-v1[k])+abs(b2-v2[k]));
//cout << k<<" - "<< v1[k] << " " << v2[k] <<" : "<<sum << endl;
//}
// }
//cout << b1 << " " << b2 <<" - "<<sum << endl;
num = min(num,sum);
}
}
cout << ans+num << endl;
return 0;
}
if(v.size() > 1){
int x = v[(v.size()+1)/2 - 1];
for(int i = 0; i < v.size(); i++)
ans += abs(x-v[i]);
}
cout << ans;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:47:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < v.size(); i++){
^
bridge.cpp:48:32: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = i+1; j < v.size(); j++){
^
bridge.cpp:54:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int k = 0; k < v1.size(); k++)//{
^
bridge.cpp:71:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < v.size(); i++)
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
1 ms |
352 KB |
Output is correct |
3 |
Correct |
2 ms |
404 KB |
Output is correct |
4 |
Correct |
2 ms |
532 KB |
Output is correct |
5 |
Correct |
3 ms |
532 KB |
Output is correct |
6 |
Correct |
2 ms |
532 KB |
Output is correct |
7 |
Correct |
2 ms |
532 KB |
Output is correct |
8 |
Correct |
2 ms |
532 KB |
Output is correct |
9 |
Correct |
2 ms |
532 KB |
Output is correct |
10 |
Correct |
2 ms |
532 KB |
Output is correct |
11 |
Correct |
2 ms |
616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
660 KB |
Output is correct |
2 |
Correct |
1 ms |
660 KB |
Output is correct |
3 |
Correct |
2 ms |
660 KB |
Output is correct |
4 |
Correct |
2 ms |
660 KB |
Output is correct |
5 |
Correct |
2 ms |
660 KB |
Output is correct |
6 |
Correct |
2 ms |
660 KB |
Output is correct |
7 |
Correct |
2 ms |
660 KB |
Output is correct |
8 |
Correct |
2 ms |
660 KB |
Output is correct |
9 |
Correct |
2 ms |
660 KB |
Output is correct |
10 |
Correct |
3 ms |
660 KB |
Output is correct |
11 |
Correct |
2 ms |
660 KB |
Output is correct |
12 |
Correct |
29 ms |
5048 KB |
Output is correct |
13 |
Correct |
58 ms |
5048 KB |
Output is correct |
14 |
Correct |
39 ms |
5048 KB |
Output is correct |
15 |
Correct |
30 ms |
5048 KB |
Output is correct |
16 |
Correct |
34 ms |
5048 KB |
Output is correct |
17 |
Correct |
37 ms |
5048 KB |
Output is correct |
18 |
Correct |
46 ms |
5048 KB |
Output is correct |
19 |
Correct |
50 ms |
5048 KB |
Output is correct |
20 |
Correct |
36 ms |
5048 KB |
Output is correct |
21 |
Correct |
46 ms |
5048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
5048 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
5048 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
5048 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |