// TST
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e5+10;
vector<pair<ll, ll>> points;
ll pref[MAXN];
priority_queue<ll> pqLeft;
priority_queue<ll, vector<ll>, greater<ll>> pqRight;
ll sumLeft = 0, sumRight = 0;
void ins(ll x) {
if (pqLeft.empty() || pqLeft.top() >= x) {
pqLeft.push(x);
sumLeft += x;
} else {
pqRight.push(x);
sumRight += x;
}
if (pqRight.size() > pqLeft.size()) {
ll t = pqRight.top();
pqRight.pop();
sumRight -= t;
pqLeft.push(t);
sumLeft += t;
} else if (pqLeft.size() > pqRight.size() + 1) {
ll t = pqLeft.top();
pqLeft.pop();
sumLeft -= t;
pqRight.push(t);
sumRight += t;
}
}
ll getSumOfDistances() {
return sumRight - sumLeft;
}
void clearPq() {
while(pqLeft.size() > 0) {pqLeft.pop();}
while(pqRight.size() > 0) {pqRight.pop();}
sumLeft = sumRight = 0;
}
int main() {
int k, n;
cin >> k >> n;
ll sumOfSame = 0;
for (int i = 0; i < n; i++) {
char p, q;
ll s, t;
cin >> p >> s >> q >> t;
if(p == q) sumOfSame += abs(s - t);
else points.push_back({s, t});
}
sort(points.begin(), points.end(), [](pair<ll, ll> a, pair<ll, ll> b) {
return a.first + a.second < b.first + b.second;
});
int a = points.size();
for (int i = 0; i < a; i++) {
auto p = points[i];
ll s = p.first;
ll t = p.second;
ins(s);
ins(t);
pref[i] = getSumOfDistances();
}
if (k == 1) {
ll ats;
if (a == 0) {
ats = sumOfSame;
} else {
ll ats = sumOfSame + pref[a-1] + a;
}
cout << ats << endl;
return 0;
}
ll ats = a > 0 ? pref[a-1] : 0;
clearPq();
for (int i = a-1; i > 0; i--) {
auto p = points[i];
ll s = p.first;
ll t = p.second;
ins(s);
ins(t);
ll dabAts = getSumOfDistances() + pref[i-1];
ats = min(dabAts, ats);
}
ats += a + sumOfSame;
cout << ats << endl;
return 0;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:86:16: warning: unused variable 'ats' [-Wunused-variable]
86 | ll ats = sumOfSame + pref[a-1] + a;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
0 ms |
348 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 |
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 |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 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 |
# |
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 |
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 |
600 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 |
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 |
1 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 |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
372 KB |
Output is correct |
8 |
Correct |
1 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 |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 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 |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
35 ms |
5476 KB |
Output is correct |
26 |
Correct |
59 ms |
6772 KB |
Output is correct |
27 |
Correct |
85 ms |
7424 KB |
Output is correct |
28 |
Correct |
96 ms |
8040 KB |
Output is correct |
29 |
Correct |
84 ms |
7360 KB |
Output is correct |
30 |
Correct |
68 ms |
4172 KB |
Output is correct |
31 |
Correct |
49 ms |
6920 KB |
Output is correct |
32 |
Correct |
71 ms |
7996 KB |
Output is correct |
33 |
Correct |
68 ms |
8028 KB |
Output is correct |
34 |
Correct |
75 ms |
7868 KB |
Output is correct |
35 |
Correct |
54 ms |
7368 KB |
Output is correct |
36 |
Correct |
70 ms |
7624 KB |
Output is correct |
37 |
Correct |
31 ms |
6348 KB |
Output is correct |