#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 100;
const ll INF = 1e13;
vector<pair<ll, ll>> a;
priority_queue<ll> lowerHalf;
priority_queue<ll, vector<ll>, greater<ll>> upperHalf;
ll lowerSum, upperSum;
void ins(ll x) {
if (lowerHalf.empty() || x <= lowerHalf.top()) {
lowerHalf.push(x);
lowerSum += x;
}
else {
upperHalf.push(x);
upperSum += x;
}
if (upperHalf.size() + 1 < lowerHalf.size()) {
ll y = lowerHalf.top();
lowerHalf.pop();
upperHalf.push(y);
lowerSum -= y;
upperSum += y;
} else if (lowerHalf.size() < upperHalf.size()) {
ll y = upperHalf.top();
upperHalf.pop();
lowerHalf.push(y);
upperSum -= y;
lowerSum += y;
}
}
ll getSumOfDistances() {
return upperSum - lowerSum; // works since there are always an even number of elements in priority queues combined
}
ll pref[MAXN];
int main() {
ios_base::sync_with_stdio(0); cin.tie(nullptr);
ll sumInSameSide = 0;
int k, n; cin >> k >> n;
for (int i = 0; i < n; i++) {
char p, q;
ll s, t;
cin >> p >> s >> q >> t;
if (p == q)
sumInSameSide += abs(s - t);
else
a.push_back({s, t});
}
if (a.size() == 0) {
cout << sumInSameSide << "\n";
return 0;
}
sort(a.begin(), a.end(), [](pair<ll, ll> f, pair<ll, ll> s) {
return (f.first + f.second) < (s.first + s.second);
});
for (int i = 0; i < a.size(); i++) {
auto p = a[i];
ins(p.first);
ins(p.second);
pref[i] = getSumOfDistances();
}
ll ans = pref[a.size()-1];
if (k == 2) {
while (lowerHalf.size()) {lowerHalf.pop();}
while (upperHalf.size()) {upperHalf.pop();}
lowerSum = upperSum = 0;
for (int i = a.size() - 1; i >= 0; i--) {
auto p = a[i];
ins(p.first);
ins(p.second);
ll curVal = getSumOfDistances() + (i > 0 ? pref[i-1] : 0);
ans = min(ans, curVal);
}
}
cout << sumInSameSide + ans + a.size() << "\n";
return 0;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:72:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for (int i = 0; i < a.size(); i++) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
352 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 |
432 KB |
Output is correct |
5 |
Correct |
1 ms |
520 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
476 KB |
Output is correct |
8 |
Correct |
0 ms |
516 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 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 |
464 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 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 |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
15 ms |
6824 KB |
Output is correct |
13 |
Correct |
34 ms |
7704 KB |
Output is correct |
14 |
Correct |
25 ms |
6348 KB |
Output is correct |
15 |
Correct |
20 ms |
4916 KB |
Output is correct |
16 |
Correct |
23 ms |
7120 KB |
Output is correct |
17 |
Correct |
32 ms |
7876 KB |
Output is correct |
18 |
Correct |
27 ms |
7116 KB |
Output is correct |
19 |
Correct |
34 ms |
8040 KB |
Output is correct |
20 |
Correct |
35 ms |
7480 KB |
Output is correct |
21 |
Correct |
32 ms |
7752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
464 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 |
608 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
352 KB |
Output is correct |
8 |
Correct |
0 ms |
412 KB |
Output is correct |
9 |
Correct |
1 ms |
352 KB |
Output is correct |
10 |
Correct |
0 ms |
356 KB |
Output is correct |
11 |
Correct |
0 ms |
360 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
360 KB |
Output is correct |
2 |
Correct |
1 ms |
356 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
468 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
520 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
460 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
352 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
352 KB |
Output is correct |
16 |
Correct |
0 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
480 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
23 ms |
6552 KB |
Output is correct |
26 |
Correct |
46 ms |
6868 KB |
Output is correct |
27 |
Correct |
60 ms |
7512 KB |
Output is correct |
28 |
Correct |
59 ms |
8104 KB |
Output is correct |
29 |
Correct |
55 ms |
7424 KB |
Output is correct |
30 |
Correct |
43 ms |
4312 KB |
Output is correct |
31 |
Correct |
24 ms |
7024 KB |
Output is correct |
32 |
Correct |
41 ms |
8072 KB |
Output is correct |
33 |
Correct |
51 ms |
7844 KB |
Output is correct |
34 |
Correct |
47 ms |
8012 KB |
Output is correct |
35 |
Correct |
29 ms |
7392 KB |
Output is correct |
36 |
Correct |
55 ms |
7664 KB |
Output is correct |
37 |
Correct |
19 ms |
6600 KB |
Output is correct |