#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
using namespace std;
typedef long long int ll;
const int maxn = 1e6 + 5;
const int INF = 1e9;
const ll inf = 1e18;
ll k, n;
vector<pair<ll, ll>> pos;
ll pre[maxn], suf[maxn];
priority_queue<ll> ql;
priority_queue<ll, vector<ll>, greater<ll>> qr;
ll suml = 0, sumr = 0;
void insert_queue(ll x)
{
ll med;
if(ql.size())
med = ql.top();
else
med = inf;
if(x <= med)
ql.push(x), suml += x;
else
qr.push(x), sumr += x;
if(ql.size() > qr.size() + 1)
{
ll t = ql.top();
ql.pop(), suml -= t;
qr.push(t), sumr += t;
}
else if(qr.size() > ql.size())
{
ll t = qr.top();
qr.pop(), sumr -= t;
ql.push(t), suml += t;
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> k >> n;
ll tol_dis = 0;
for(int i = 1; i <= n; i++)
{
char a, b;
ll x, y;
cin >> a >> x >> b >> y;
if(x > y)
swap(x, y);
if(a == b)
tol_dis += (y - x);
else
pos.push_back({x, y});
}
tol_dis += (int)pos.size();
sort(pos.begin(), pos.end(), [&](const pair<ll, ll> &a, const pair<ll, ll> &b){
if((a.first + a.second) == (b.first + b.second))
return a < b;
return (a.first + a.second) < (b.first + b.second);
});
for(int i = 0; i < (int)pos.size(); i++)
{
insert_queue(pos[i].first);
insert_queue(pos[i].second);
pre[i] = sumr - suml;
}
while(!ql.empty())
ql.pop();
while(!qr.empty())
qr.pop();
sumr = suml = 0;
for(int i = (int)pos.size() - 1; i >= 0; i--)
{
insert_queue(pos[i].first);
insert_queue(pos[i].second);
suf[i] = sumr - suml;
}
ll ans = pre[(int)pos.size() - 1];
if(k == 2)
{
for(int i = 0; i < (int)pos.size(); i++)
ans = min(ans, pre[i] + suf[i + 1]);
}
cout << ans + tol_dis;
// for(int i = 0; i < (int)pos.size(); i++)
// cout << pre[i] << ' ';
// for(int i = 0; i < (int)pos.size(); i++)
// {
// cout << pos[i].first << ' ' << pos[i].second << '\n';
// }
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2572 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
2 ms |
2544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
464 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2576 KB |
Output is correct |
10 |
Correct |
1 ms |
2648 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
29 ms |
12188 KB |
Output is correct |
13 |
Correct |
69 ms |
13416 KB |
Output is correct |
14 |
Correct |
54 ms |
12736 KB |
Output is correct |
15 |
Correct |
37 ms |
8908 KB |
Output is correct |
16 |
Correct |
25 ms |
12772 KB |
Output is correct |
17 |
Correct |
53 ms |
13460 KB |
Output is correct |
18 |
Correct |
47 ms |
12996 KB |
Output is correct |
19 |
Correct |
62 ms |
13620 KB |
Output is correct |
20 |
Correct |
44 ms |
13184 KB |
Output is correct |
21 |
Correct |
57 ms |
13216 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 |
2516 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
2512 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 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 |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2412 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 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 |
1 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 |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2516 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 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 |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 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 |
2392 KB |
Output is correct |
23 |
Correct |
1 ms |
2404 KB |
Output is correct |
24 |
Correct |
1 ms |
2400 KB |
Output is correct |
25 |
Correct |
29 ms |
11976 KB |
Output is correct |
26 |
Correct |
46 ms |
12224 KB |
Output is correct |
27 |
Correct |
62 ms |
13424 KB |
Output is correct |
28 |
Correct |
67 ms |
14280 KB |
Output is correct |
29 |
Correct |
65 ms |
14040 KB |
Output is correct |
30 |
Correct |
34 ms |
8396 KB |
Output is correct |
31 |
Correct |
25 ms |
12828 KB |
Output is correct |
32 |
Correct |
63 ms |
13512 KB |
Output is correct |
33 |
Correct |
51 ms |
13020 KB |
Output is correct |
34 |
Correct |
58 ms |
13440 KB |
Output is correct |
35 |
Correct |
39 ms |
13004 KB |
Output is correct |
36 |
Correct |
56 ms |
13248 KB |
Output is correct |
37 |
Correct |
24 ms |
11980 KB |
Output is correct |