#include <bits/stdc++.h>
using namespace std;
vector < pair <long long int, int> > v;
long long int pos [20010][2];
int main (){
int n, k; cin >> k >> n; long long int sol=0; int ind=0;
for (int i=0; i<n; i++){
long long int a, b; char x, y; cin >> x >> a >> y >> b;
if (x==y){
sol+=abs (a-b); continue;
}
v.push_back ({a, 0});
v.push_back ({b, 1}); pos [ind][0]=a; pos [ind][1]=b; ind++;
}
long long int ans=1e18;
for (int i=0; i<v.size(); i++){
int po=v [i].first; long long int curr=0;
for (int j=0; j<ind; j++){
if (!(po<=pos [j][1] && po>=pos [j][0])) curr+=2*min (abs (pos [j][0]-po), abs (pos [j][1]-po));
curr+=(abs (pos [j][0]-pos [j][1])+1);
}
ans=min (ans, curr);
po=v [i].first-1; curr=0;
for (int j=0; j<ind; j++){
if (!(po<=pos [j][1] && po>=pos [j][0])) curr+=2*min (abs (pos [j][0]-po), abs (pos [j][1]-po));
curr+=(abs (pos [j][0]-pos [j][1])+1);
}
ans=min (ans, curr);
po=v [i].first+1; curr=0;
for (int j=0; j<ind; j++){
if (!(po<=pos [j][1] && po>=pos [j][0])) curr+=2*min (abs (pos [j][0]-po), abs (pos [j][1]-po));
curr+=(abs (pos [j][0]-pos [j][1])+1);
}
ans=min (ans, curr);
//cout << ans << endl;
}
cout << sol+ans << endl;
return 0;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:16:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | for (int i=0; i<v.size(); i++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |