#include <bits/stdc++.h>
using namespace std;
#define PrintDec(d, c) cout << fixed << setprecision(c) << d
#define MultiTest(q) long long q; cin >> q; while (q--)
#define rall(v) v.rbegin(), v.rend()
#define all(v) v.begin(), v.end()
#define endl "\n"
#define NAME "BRIDGES"
#define ull unsigned long long int
#define ll long long int
#define pii pair <int, int>
#define pll pair <ll, ll>
bool minimize (ll &a, ll b) {
if (a > b) {
return a = b, true;
}
return false;
}
bool maximize (ll &a, ll b) {
if (a < b) {
return a = b, true;
}
return false;
}
struct Pedestrian {
int s, t;
bool operator< (const Pedestrian &X) const {
return s + t < X.s + X.t;
}
};
const int N = 2E5 + 25;
priority_queue <int, vector<int>> L;
priority_queue <int, vector<int>, greater<int>> R;
int n, m, s, t, k;
vector <int> V;
vector <Pedestrian> A;
char c1, c2;
ll D, T, v1, v2, F[N], G[N];
signed main () {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
if (fopen(NAME".INP", "r")) {
freopen(NAME".INP", "r", stdin);
freopen(NAME".OUT", "w", stdout);
}
cin >> k >> n;
V.push_back(0);
A.push_back({0, 0});
for (int i = 1; i <= n; i++) {
cin >> c1 >> s >> c2 >> t;
if (c1 == c2) D += llabs(s - t);
else {
if (s > t) swap(s, t);
A.push_back({s, t});
++m;
V.push_back(s);
V.push_back(t);
}
}
D += m;
if (k == 1) {
v1 = v2 = 0;
sort(V.begin(), V.end());
for (int i = 1; i <= 2 * m; i++) {
v1 += abs(V[m] - V[i]);
v2 += abs(V[m + 1] - V[i]);
}
cout << D + min(v1, v2);
}
else {
sort(A.begin(), A.end());
v1 = v2 = 0;
for (int i = 1; i <= m; i++) {
L.push(A[i].s); R.push(A[i].t);
v1 += A[i].s; v2 += A[i].t;
if (L.top() > R.top()) {
int u = L.top(), v = R.top();
v1 += v - u;
v2 += u - v;
L.pop(); R.pop();
L.push(v); R.push(u);
}
F[i] = v2 - v1;
}
while (L.size()) L.pop();
while (R.size()) R.pop();
v1 = v2 = 0;
for (int i = m; i >= 1; i--) {
L.push(A[i].s); R.push(A[i].t);
v1 += A[i].s; v2 += A[i].t;
if (L.top() > R.top()) {
int u = L.top(), v = R.top();
v1 += v - u;
v2 += u - v;
L.pop(); R.pop();
L.push(v); R.push(u);
}
G[i] = v2 - v1;
}
T = F[1] + G[2];
for (int i = 2; i < m; i++)
minimize(T, F[i] + G[i + 1]);
cout << D + T;
}
return 0;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:49:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | freopen(NAME".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bridge.cpp:50:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | freopen(NAME".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
12 ms |
3016 KB |
Output is correct |
13 |
Correct |
24 ms |
4552 KB |
Output is correct |
14 |
Correct |
18 ms |
3280 KB |
Output is correct |
15 |
Correct |
14 ms |
2944 KB |
Output is correct |
16 |
Correct |
16 ms |
3932 KB |
Output is correct |
17 |
Correct |
17 ms |
4404 KB |
Output is correct |
18 |
Correct |
19 ms |
4040 KB |
Output is correct |
19 |
Correct |
23 ms |
4552 KB |
Output is correct |
20 |
Correct |
15 ms |
4040 KB |
Output is correct |
21 |
Correct |
21 ms |
4296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2648 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 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 |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2516 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
0 ms |
2472 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 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 |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2516 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2648 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
0 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2524 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2648 KB |
Output is correct |
25 |
Correct |
17 ms |
6336 KB |
Output is correct |
26 |
Correct |
41 ms |
6588 KB |
Output is correct |
27 |
Correct |
49 ms |
7360 KB |
Output is correct |
28 |
Correct |
51 ms |
7872 KB |
Output is correct |
29 |
Correct |
53 ms |
7944 KB |
Output is correct |
30 |
Correct |
31 ms |
5320 KB |
Output is correct |
31 |
Correct |
17 ms |
7292 KB |
Output is correct |
32 |
Correct |
48 ms |
7872 KB |
Output is correct |
33 |
Correct |
26 ms |
7368 KB |
Output is correct |
34 |
Correct |
51 ms |
7920 KB |
Output is correct |
35 |
Correct |
28 ms |
7368 KB |
Output is correct |
36 |
Correct |
49 ms |
7616 KB |
Output is correct |
37 |
Correct |
14 ms |
6232 KB |
Output is correct |