#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long int lld;
#define INF 1000000000000000
lld dist(lld x, lld y){
if(y>x)return y-x;
return x-y;
}
int main(){
int k,n;
cin>>k>>n;
int arr[n][4];
for(int i=0;i<n;i++){
char a,b;
cin>>a>>arr[i][1]>>b>>arr[i][3];
arr[i][0]=a;
arr[i][2]=b;
}
if(k==1){
lld ans=0;
vector<lld> v;
for(int i=0;i<n;i++){
if(arr[i][0]==arr[i][2])ans+=dist(arr[i][1],arr[i][3]);
else {
v.push_back(arr[i][1]);
v.push_back(arr[i][3]);
}
}//cout<<ans<<endl;
sort(v.begin(),v.end());
for(int i=0;i<v.size();i++){
ans+=dist(v[i],v[v.size()/2-1]);
//cout<<v[i]<<" "<<v[v.size()/2-1]<<" "<<dist(v[i],v[v.size()/2-1])<<endl;
}cout<<ans+v.size()/2<<endl;
return 0;
}
lld ans=INF;
for(int i=0;i<2*n;i++){
for(int j=0;j<2*n;j++){
lld pos1=arr[i/2][2*(i%2)+1];
lld pos2=arr[j/2][2*(j%2)+1];
lld can=0;
for(int k=0;k<n;k++){
if(arr[k][0]==arr[k][2])can+=dist(arr[k][1],arr[k][3]);
else{
can+=min(dist(arr[k][1],pos1)+dist(arr[k][3],pos1)+1,dist(arr[k][1],pos2)+dist(arr[k][3],pos2)+1);
}
}//cout<<can<<endl;
ans=min(ans,can);
}
}cout<<ans<<endl;
return 0;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:32:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<v.size();i++){
~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
484 KB |
Output is correct |
3 |
Correct |
4 ms |
484 KB |
Output is correct |
4 |
Correct |
5 ms |
484 KB |
Output is correct |
5 |
Correct |
4 ms |
484 KB |
Output is correct |
6 |
Correct |
4 ms |
484 KB |
Output is correct |
7 |
Correct |
6 ms |
596 KB |
Output is correct |
8 |
Correct |
4 ms |
596 KB |
Output is correct |
9 |
Correct |
4 ms |
596 KB |
Output is correct |
10 |
Correct |
5 ms |
596 KB |
Output is correct |
11 |
Correct |
5 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
596 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
3 ms |
596 KB |
Output is correct |
4 |
Correct |
5 ms |
596 KB |
Output is correct |
5 |
Correct |
5 ms |
596 KB |
Output is correct |
6 |
Correct |
5 ms |
596 KB |
Output is correct |
7 |
Correct |
4 ms |
596 KB |
Output is correct |
8 |
Correct |
4 ms |
624 KB |
Output is correct |
9 |
Correct |
4 ms |
624 KB |
Output is correct |
10 |
Correct |
3 ms |
624 KB |
Output is correct |
11 |
Correct |
3 ms |
624 KB |
Output is correct |
12 |
Correct |
86 ms |
4280 KB |
Output is correct |
13 |
Correct |
159 ms |
4320 KB |
Output is correct |
14 |
Correct |
99 ms |
4320 KB |
Output is correct |
15 |
Correct |
96 ms |
4320 KB |
Output is correct |
16 |
Correct |
124 ms |
4320 KB |
Output is correct |
17 |
Correct |
159 ms |
4320 KB |
Output is correct |
18 |
Correct |
136 ms |
4320 KB |
Output is correct |
19 |
Correct |
159 ms |
4320 KB |
Output is correct |
20 |
Correct |
142 ms |
4320 KB |
Output is correct |
21 |
Correct |
195 ms |
4320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4320 KB |
Output is correct |
2 |
Correct |
3 ms |
4320 KB |
Output is correct |
3 |
Correct |
34 ms |
4320 KB |
Output is correct |
4 |
Correct |
32 ms |
4320 KB |
Output is correct |
5 |
Correct |
8 ms |
4320 KB |
Output is correct |
6 |
Correct |
3 ms |
4320 KB |
Output is correct |
7 |
Correct |
27 ms |
4320 KB |
Output is correct |
8 |
Correct |
37 ms |
4320 KB |
Output is correct |
9 |
Correct |
25 ms |
4320 KB |
Output is correct |
10 |
Correct |
27 ms |
4320 KB |
Output is correct |
11 |
Correct |
27 ms |
4320 KB |
Output is correct |
12 |
Correct |
25 ms |
4320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4320 KB |
Output is correct |
2 |
Correct |
2 ms |
4320 KB |
Output is correct |
3 |
Correct |
22 ms |
4320 KB |
Output is correct |
4 |
Correct |
22 ms |
4320 KB |
Output is correct |
5 |
Correct |
8 ms |
4320 KB |
Output is correct |
6 |
Correct |
4 ms |
4320 KB |
Output is correct |
7 |
Correct |
35 ms |
4320 KB |
Output is correct |
8 |
Correct |
24 ms |
4320 KB |
Output is correct |
9 |
Correct |
27 ms |
4320 KB |
Output is correct |
10 |
Correct |
35 ms |
4320 KB |
Output is correct |
11 |
Correct |
25 ms |
4320 KB |
Output is correct |
12 |
Correct |
26 ms |
4320 KB |
Output is correct |
13 |
Execution timed out |
2040 ms |
4320 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4320 KB |
Output is correct |
2 |
Correct |
3 ms |
4320 KB |
Output is correct |
3 |
Correct |
22 ms |
4320 KB |
Output is correct |
4 |
Correct |
21 ms |
4320 KB |
Output is correct |
5 |
Correct |
7 ms |
4320 KB |
Output is correct |
6 |
Correct |
4 ms |
4320 KB |
Output is correct |
7 |
Correct |
22 ms |
4320 KB |
Output is correct |
8 |
Correct |
23 ms |
4320 KB |
Output is correct |
9 |
Correct |
25 ms |
4320 KB |
Output is correct |
10 |
Correct |
26 ms |
4320 KB |
Output is correct |
11 |
Correct |
26 ms |
4320 KB |
Output is correct |
12 |
Correct |
23 ms |
4320 KB |
Output is correct |
13 |
Execution timed out |
2066 ms |
4320 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |