#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 1e5 + 5;
int n, same, k, sumn, sumx, cur;
int pref[maxn];
vector<pair<int,int>> points;
priority_queue<int> mn;
priority_queue<int, vector<int>, greater<int>> mx;
inline void amn(int x)
{
mn.emplace(x);
sumn += x;
}
inline void amx(int x)
{
mx.emplace(x);
sumx += x;
}
inline void popn()
{
sumn -= mn.top();
mn.pop();
}
inline void popx()
{
sumx -= mx.top();
mx.pop();
}
inline void add(int x)
{
if (mn.size() == mx.size())
{
if (mn.empty())
amn(x);
else
{
vector<int> tmp = {x, mn.top(), mx.top()};
popn(); popx();
sort(tmp.begin(), tmp.end());
amn(tmp[0]);
amn(tmp[1]);
amx(tmp[2]);
}
}
else
{
if (mx.empty())
{
vector<int> tmp = {mn.top(), x};
popn();
sort(tmp.begin(), tmp.end());
amn(tmp[0]);
amx(tmp[1]);
}
else
{
vector<int> tmp = {x, mn.top(), mx.top()};
popn(); popx();
sort(tmp.begin(), tmp.end());
amn(tmp[0]);
amx(tmp[1]);
amx(tmp[2]);
}
}
if (mn.size() == mx.size())
{
int median = (mn.top() + mx.top()) / 2;
cur = sumx - sumn;
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
// freopen("test.inp", "r", stdin);
// freopen("test.out", "w", stdout);
cin >> k >> n;
for (int i = 1; i <= n; i++)
{
char side1, side2;
int x, y;
cin >> side1 >> x >> side2 >> y;
if (side1 == side2) same += abs(x - y);
else points.emplace_back(x, y);
}
n = points.size();
if (!n) return cout << same, 0;
for (int i = 0; i < n; i++)
{
add(points[i].first);
add(points[i].second);
pref[i] = cur;
}
if (k == 1) return cout << pref[n - 1] + n + same, 0;
int ans = pref[n - 1];
while (mn.size()) popn();
while (mx.size()) popx();
for (int i = n - 1; i >= 1; i--)
{
add(points[i].first);
add(points[i].second);
ans = min(ans, pref[i - 1] + cur);
}
cout << ans + n + same;
}
Compilation message
bridge.cpp: In function 'void add(long long int)':
bridge.cpp:74:13: warning: unused variable 'median' [-Wunused-variable]
74 | int median = (mn.top() + mx.top()) / 2;
| ^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 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 |
344 KB |
Output is correct |
10 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 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 |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
31 ms |
5588 KB |
Output is correct |
13 |
Correct |
58 ms |
5576 KB |
Output is correct |
14 |
Correct |
52 ms |
5320 KB |
Output is correct |
15 |
Correct |
33 ms |
2976 KB |
Output is correct |
16 |
Correct |
31 ms |
5676 KB |
Output is correct |
17 |
Correct |
55 ms |
5548 KB |
Output is correct |
18 |
Correct |
54 ms |
5596 KB |
Output is correct |
19 |
Correct |
59 ms |
5572 KB |
Output is correct |
20 |
Correct |
51 ms |
6040 KB |
Output is correct |
21 |
Correct |
58 ms |
5560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
- |