#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/rope>
using namespace std;
// using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef long double ld;
template <class T>
using VV = vector<vector<T>>;
using VI = vector<int>;
using VVI = vector<vector<int>>;
using VL = vector<long long>;
using VVL = vector<vector<long long>>;
using VC = vector<char>;
using VVC = vector<vector<char>>;
using VB = vector<bool>;
using VVB = vector<vector<bool>>;
using VD = vector<double>;
using VVD = vector<vector<double>>;
using PII = pair<int, int>;
using PLL = pair<long long, long long>;
using PIL = pair<int, long long>;
using PLI = pair<long long, int>;
using VPII = vector<pair<int, int>>;
using VPLL = vector<pair<long long, long long>>;
#define LINE '\n'
#define SPACE ' '
#define PB push_back
#define FOR(i, a, b) for (int i = (a); i < (int(b)); i++)
#define ALL(x) x.begin(), x.end()
#define sz(x) ((int)x.size())
template <class T>
void mini(T &a, T b)
{
a = min(a, b);
}
priority_queue<int> lq;
priority_queue<int, VI, greater<int>> rq;
ll lsum, rsum;
void insert(int x)
{
int median = (sz(lq) ? lq.top() : 1e9 + 2137);
if(x <= median) {
lq.push(x);
lsum += x;
}
else {
rq.push(x);
rsum += x;
}
if(sz(lq) > sz(rq) + 1) {
int tmp = lq.top();
lq.pop();
rq.push(tmp);
lsum -= tmp;
rsum += tmp;
}
else if(sz(lq) < sz(rq)) {
int tmp = rq.top();
rq.pop();
lq.push(tmp);
rsum -= tmp;
lsum += tmp;
}
}
void solve()
{
int n, k;
cin >> k >> n;
ll same = 0;
VPII vec;
vec.PB({0, 0});
FOR(i, 0, n) {
char a, b;
int x, y;
cin >> a >> x >> b >> y;
if(a==b) same += abs(x-y);
else vec.PB({x, y});
}
sort(ALL(vec), [](const PII &a, const PII &b) {return a.first + a.second < b.first + b.second;});
n = sz(vec) - 1;
lsum = rsum = 0;
same += n; // because of bridge
VL pref(n + 1, 0);
FOR(i, 1, n + 1) {
auto [x, y] = vec[i];
insert(x);
insert(y);
cerr << lsum << SPACE << rsum << LINE;
pref[i] = rsum - lsum;
}
ll ans = pref[n];
if(k == 2) {
while(sz(lq)) lq.pop();
while(sz(rq)) rq.pop();
lsum = rsum = 0;
for(int i = n; i >= 1; i--) {
auto [x, y] = vec[i];
insert(x);
insert(y);
mini(ans, rsum - lsum + pref[i-1]);
}
}
cout << same + ans << LINE;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
// cin >> t;
while (t--)
solve();
}
# |
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 |
5 ms |
348 KB |
Output is correct |
4 |
Correct |
6 ms |
348 KB |
Output is correct |
5 |
Correct |
5 ms |
508 KB |
Output is correct |
6 |
Correct |
5 ms |
348 KB |
Output is correct |
7 |
Correct |
5 ms |
348 KB |
Output is correct |
8 |
Correct |
5 ms |
348 KB |
Output is correct |
9 |
Correct |
5 ms |
348 KB |
Output is correct |
10 |
Correct |
5 ms |
348 KB |
Output is correct |
11 |
Correct |
5 ms |
348 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 |
4 ms |
348 KB |
Output is correct |
4 |
Correct |
5 ms |
504 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
508 KB |
Output is correct |
7 |
Correct |
5 ms |
344 KB |
Output is correct |
8 |
Correct |
5 ms |
348 KB |
Output is correct |
9 |
Correct |
5 ms |
348 KB |
Output is correct |
10 |
Correct |
4 ms |
504 KB |
Output is correct |
11 |
Correct |
5 ms |
348 KB |
Output is correct |
12 |
Correct |
421 ms |
4556 KB |
Output is correct |
13 |
Correct |
459 ms |
8400 KB |
Output is correct |
14 |
Correct |
395 ms |
6348 KB |
Output is correct |
15 |
Correct |
278 ms |
5360 KB |
Output is correct |
16 |
Correct |
432 ms |
6688 KB |
Output is correct |
17 |
Correct |
472 ms |
8396 KB |
Output is correct |
18 |
Correct |
447 ms |
8164 KB |
Output is correct |
19 |
Correct |
455 ms |
8400 KB |
Output is correct |
20 |
Correct |
435 ms |
7116 KB |
Output is correct |
21 |
Correct |
467 ms |
7624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 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 |
456 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 |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
456 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 |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 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 |
12 |
Correct |
1 ms |
612 KB |
Output is correct |
13 |
Correct |
4 ms |
344 KB |
Output is correct |
14 |
Correct |
5 ms |
348 KB |
Output is correct |
15 |
Correct |
5 ms |
532 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
17 |
Correct |
3 ms |
464 KB |
Output is correct |
18 |
Correct |
2 ms |
348 KB |
Output is correct |
19 |
Correct |
5 ms |
348 KB |
Output is correct |
20 |
Correct |
5 ms |
348 KB |
Output is correct |
21 |
Correct |
5 ms |
468 KB |
Output is correct |
22 |
Correct |
5 ms |
348 KB |
Output is correct |
23 |
Correct |
5 ms |
348 KB |
Output is correct |
24 |
Correct |
5 ms |
348 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 |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 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 |
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 |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
4 ms |
348 KB |
Output is correct |
14 |
Correct |
5 ms |
348 KB |
Output is correct |
15 |
Correct |
5 ms |
496 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
17 |
Correct |
3 ms |
352 KB |
Output is correct |
18 |
Correct |
2 ms |
356 KB |
Output is correct |
19 |
Correct |
5 ms |
360 KB |
Output is correct |
20 |
Correct |
5 ms |
352 KB |
Output is correct |
21 |
Correct |
5 ms |
360 KB |
Output is correct |
22 |
Correct |
5 ms |
512 KB |
Output is correct |
23 |
Correct |
4 ms |
360 KB |
Output is correct |
24 |
Correct |
5 ms |
360 KB |
Output is correct |
25 |
Correct |
444 ms |
4572 KB |
Output is correct |
26 |
Correct |
456 ms |
5984 KB |
Output is correct |
27 |
Correct |
477 ms |
7360 KB |
Output is correct |
28 |
Correct |
502 ms |
8764 KB |
Output is correct |
29 |
Correct |
474 ms |
8396 KB |
Output is correct |
30 |
Correct |
252 ms |
4716 KB |
Output is correct |
31 |
Correct |
457 ms |
6332 KB |
Output is correct |
32 |
Correct |
456 ms |
8432 KB |
Output is correct |
33 |
Correct |
471 ms |
7488 KB |
Output is correct |
34 |
Correct |
471 ms |
8400 KB |
Output is correct |
35 |
Correct |
461 ms |
7116 KB |
Output is correct |
36 |
Correct |
472 ms |
7656 KB |
Output is correct |
37 |
Correct |
425 ms |
4236 KB |
Output is correct |