#include <bits/stdc++.h>
#define int long long
using namespace std;
int pref[100005], l, r, k, n, num, ans=1e18;
priority_queue<int> pql, pqr;
vector<pair<int, int> > vec;
void f(int x)
{
int median=pql.size()?pql.top():1e9;
if (x<=median)
{
pql.push(x);
l+=x;
}
else
{
pqr.push(-x);
r+=x;
}
if (pql.size()>=pqr.size()+2)
{
x=pql.top();
pql.pop();
pqr.push(-x);
l-=x;
r+=x;
}
else if (pql.size()<pqr.size())
{
x=-pqr.top();
pqr.pop();
pql.push(x);
r-=x;
l+=x;
}
}
bool cmp(pair<int, int> x, pair<int, int> y)
{
return x.first+x.second<y.first+y.second;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> k >> n;
for (int i=0; i<n; i++)
{
char p, q;
int x, y;
cin >> p >> x >> q >> y;
if (p==q)
num+=abs(x-y);
else
vec.push_back({x, y});
}
sort(vec.begin(), vec.end(), cmp);
n=vec.size();
num+=n;
for (int i=0; i<n; i++)
{
f(vec[i].first);
f(vec[i].second);
pref[i]=r-l;
}
if (!n || k==1)
{
cout << num+pref[n-1];
return 0;
}
while (pql.size())
pql.pop();
while (pqr.size())
pqr.pop();
l=r=0;
for (int i=n-1; i>=0; i--)
{
ans=min(ans, r-l+pref[i]);
f(vec[i].first);
f(vec[i].second);
}
cout << num+ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
25 ms |
6040 KB |
Output is correct |
13 |
Correct |
51 ms |
5348 KB |
Output is correct |
14 |
Correct |
39 ms |
5152 KB |
Output is correct |
15 |
Correct |
30 ms |
3608 KB |
Output is correct |
16 |
Correct |
31 ms |
5352 KB |
Output is correct |
17 |
Correct |
41 ms |
5500 KB |
Output is correct |
18 |
Correct |
38 ms |
5152 KB |
Output is correct |
19 |
Correct |
46 ms |
5676 KB |
Output is correct |
20 |
Correct |
32 ms |
5568 KB |
Output is correct |
21 |
Correct |
45 ms |
5560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
296 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
368 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
376 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
35 ms |
6296 KB |
Output is correct |
26 |
Correct |
61 ms |
6688 KB |
Output is correct |
27 |
Correct |
83 ms |
7308 KB |
Output is correct |
28 |
Correct |
85 ms |
7916 KB |
Output is correct |
29 |
Correct |
88 ms |
7436 KB |
Output is correct |
30 |
Correct |
45 ms |
4204 KB |
Output is correct |
31 |
Correct |
42 ms |
6864 KB |
Output is correct |
32 |
Correct |
66 ms |
7820 KB |
Output is correct |
33 |
Correct |
66 ms |
7872 KB |
Output is correct |
34 |
Correct |
71 ms |
7832 KB |
Output is correct |
35 |
Correct |
44 ms |
7488 KB |
Output is correct |
36 |
Correct |
70 ms |
7800 KB |
Output is correct |
37 |
Correct |
28 ms |
6296 KB |
Output is correct |