#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], suf[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];
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++) {
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
492 KB |
Output is correct |
7 |
Correct |
0 ms |
476 KB |
Output is correct |
8 |
Correct |
0 ms |
480 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
520 KB |
Output is correct |
# |
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 |
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 |
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 |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
15 ms |
6840 KB |
Output is correct |
13 |
Correct |
31 ms |
7880 KB |
Output is correct |
14 |
Correct |
26 ms |
6348 KB |
Output is correct |
15 |
Correct |
18 ms |
4956 KB |
Output is correct |
16 |
Correct |
18 ms |
7124 KB |
Output is correct |
17 |
Correct |
24 ms |
7880 KB |
Output is correct |
18 |
Correct |
23 ms |
7112 KB |
Output is correct |
19 |
Correct |
27 ms |
7884 KB |
Output is correct |
20 |
Correct |
20 ms |
7636 KB |
Output is correct |
21 |
Correct |
26 ms |
7624 KB |
Output is correct |
# |
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 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |