#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);
if(k == 1){
ll sm = 0, sum = 0;
int cnt = 0;
multiset<int> A;
for(int i = 1; i <= n; ++i){
char p, q;
int s, t;
scanf(" %c %d %c %d", &p, &s, &q, &t);
if (p == q)
sm += abs(s-t);
else{
cnt++;
A.insert(s);
A.insert(t);
sum -= (s+t);
}
}
ll answer = cnt+sm;
while(cnt--)
sum += (*--A.end()*2), A.erase(--A.end());
answer += sum;
printf("%lld\n", answer);
return 0;
}
// k=2
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;
}
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:29:12: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
29 | scanf(" %c %d %c %d", &p, &s, &q, &t);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:50:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | scanf(" %c %d %c %d", &a, &x, &b, &y);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
428 KB |
Output is correct |
4 |
Correct |
2 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 |
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 |
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 |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
480 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
39 ms |
9560 KB |
Output is correct |
13 |
Correct |
75 ms |
9812 KB |
Output is correct |
14 |
Correct |
76 ms |
8612 KB |
Output is correct |
15 |
Correct |
39 ms |
5980 KB |
Output is correct |
16 |
Correct |
63 ms |
9808 KB |
Output is correct |
17 |
Correct |
54 ms |
9804 KB |
Output is correct |
18 |
Correct |
53 ms |
9660 KB |
Output is correct |
19 |
Correct |
58 ms |
9740 KB |
Output is correct |
20 |
Correct |
59 ms |
9988 KB |
Output is correct |
21 |
Correct |
48 ms |
9812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
2 ms |
452 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 |
348 KB |
Output is correct |
9 |
Correct |
5 ms |
452 KB |
Output is correct |
10 |
Correct |
6 ms |
348 KB |
Output is correct |
11 |
Correct |
4 ms |
448 KB |
Output is correct |
12 |
Correct |
6 ms |
344 KB |
Output is correct |
# |
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 |
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 |
6 ms |
456 KB |
Output is correct |
9 |
Correct |
6 ms |
348 KB |
Output is correct |
10 |
Correct |
5 ms |
348 KB |
Output is correct |
11 |
Correct |
4 ms |
452 KB |
Output is correct |
12 |
Correct |
5 ms |
348 KB |
Output is correct |
13 |
Correct |
1831 ms |
440 KB |
Output is correct |
14 |
Execution timed out |
2074 ms |
348 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
7 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
448 KB |
Output is correct |
8 |
Correct |
5 ms |
348 KB |
Output is correct |
9 |
Correct |
7 ms |
348 KB |
Output is correct |
10 |
Correct |
5 ms |
348 KB |
Output is correct |
11 |
Correct |
4 ms |
348 KB |
Output is correct |
12 |
Correct |
6 ms |
456 KB |
Output is correct |
13 |
Correct |
1954 ms |
436 KB |
Output is correct |
14 |
Execution timed out |
2070 ms |
348 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |