#include "bits/stdc++.h"
using namespace std;
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define wr puts("----------------")
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
const int MOD = 1e9+7;
const int INF = 1e9;
const int N = 1e5+5;
int s[N], t[N];
char p[N], q[N];
int main(){
int k, n;
scanf("%d%d", &k ,&n);
for(int i = 1; i <= n; ++i){
char a, b;
int x, y;
scanf(" %c %d %c %d", &a, &x, &b, &y);
p[i] = a, s[i] = x, q[i] = b, t[i] = y;
}
if(k == 1){
ll answer = 1e18;
for(int k = 1; k <= n; ++k){
ll sm = 0;
int br = s[k];
for(int i = 1; i <= n; ++i){
if(p[i] == q[i]){
sm += abs(s[i]-t[i]);
continue;
}
// p[i] != q[i]
sm++; // this is for bridge crossing
sm += abs(br-s[i]);
sm += abs(br-t[i]);
}
umin(answer, sm);
sm = 0;
br = t[k];
for(int i = 1; i <= n; ++i){
if(p[i] == q[i]){
sm += abs(s[i]-t[i]);
continue;
}
sm++;
sm += abs(br-s[i]);
sm += abs(br-t[i]);
}
umin(answer, sm);
}
printf("%lld\n", answer);
return 0;
}
// k = 2
ll answer = 1e18;
for(int x = 1; x <= n; ++x)
for(int y = 1; y <= n; ++y){
if(s[x] == t[y])
continue; // two bridge cannot be in same place
ll sm = 0;
int br = s[x], br_ = t[y];
for(int i = 1; i <= n; ++i){
if(p[i] == q[i]){
sm += abs(s[i]-t[i]);
continue;
}
sm++;
ll A = abs(br-s[i])+abs(br-t[i]);
ll B = abs(br_-s[i])+abs(br_-t[i]);
sm += min(A, B);
}
umin(answer, sm);
sm = 0;
if(s[y] == t[x])
continue;
br = s[y], br_ = t[x];
for(int i = 1; i <= n; ++i){
if(p[i] == q[i]){
sm += abs(s[i]-t[i]);
continue;
}
sm++;
ll A = abs(br-s[i])+abs(br-t[i]);
ll B = abs(br_-s[i])+abs(br_-t[i]);
sm += min(A, B);
}
umin(answer, sm);
}
printf("%lld\n", answer);
return 0;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:21:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
21 | scanf("%d%d", &k ,&n);
| ~~~~~^~~~~~~~~~~~~~~~
bridge.cpp:25:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
25 | scanf(" %c %d %c %d", &a, &x, &b, &y);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
348 KB |
Output is correct |
4 |
Correct |
3 ms |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
348 KB |
Output is correct |
6 |
Correct |
3 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
480 KB |
Output is correct |
8 |
Correct |
4 ms |
348 KB |
Output is correct |
9 |
Correct |
3 ms |
348 KB |
Output is correct |
10 |
Correct |
4 ms |
344 KB |
Output is correct |
11 |
Correct |
4 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
348 KB |
Output is correct |
5 |
Correct |
3 ms |
492 KB |
Output is correct |
6 |
Correct |
4 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
344 KB |
Output is correct |
8 |
Correct |
3 ms |
344 KB |
Output is correct |
9 |
Correct |
3 ms |
348 KB |
Output is correct |
10 |
Correct |
3 ms |
404 KB |
Output is correct |
11 |
Correct |
4 ms |
348 KB |
Output is correct |
12 |
Execution timed out |
2096 ms |
2140 KB |
Time limit exceeded |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
344 KB |
Output is correct |
4 |
Correct |
5 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
348 KB |
Output is correct |
8 |
Correct |
6 ms |
348 KB |
Output is correct |
9 |
Correct |
6 ms |
344 KB |
Output is correct |
10 |
Correct |
6 ms |
344 KB |
Output is correct |
11 |
Correct |
4 ms |
344 KB |
Output is correct |
12 |
Correct |
7 ms |
456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
5 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
348 KB |
Output is correct |
8 |
Correct |
5 ms |
456 KB |
Output is correct |
9 |
Correct |
6 ms |
348 KB |
Output is correct |
10 |
Correct |
6 ms |
348 KB |
Output is correct |
11 |
Correct |
4 ms |
344 KB |
Output is correct |
12 |
Correct |
6 ms |
348 KB |
Output is correct |
13 |
Correct |
1815 ms |
444 KB |
Output is correct |
14 |
Execution timed out |
2023 ms |
344 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
6 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
348 KB |
Output is correct |
8 |
Correct |
7 ms |
348 KB |
Output is correct |
9 |
Correct |
5 ms |
344 KB |
Output is correct |
10 |
Correct |
5 ms |
348 KB |
Output is correct |
11 |
Correct |
4 ms |
344 KB |
Output is correct |
12 |
Correct |
6 ms |
348 KB |
Output is correct |
13 |
Correct |
1883 ms |
444 KB |
Output is correct |
14 |
Execution timed out |
2035 ms |
344 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |