#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <functional>
#include <stdlib.h>
using namespace std;
#define int long long
priority_queue<int>l;
priority_queue<int,vector<int>,greater<int>>r;
int lc=0;
void ins(int n)
{
int ml=l.size()?l.top():(1<<30);
if(n<ml)
{
lc+=n;
l.push(n);
if(l.size()-1>r.size())
{
lc-=l.top();
r.push(l.top());
l.pop();
}
}
else
{
r.push(n);
if(r.size()>l.size())
{
lc+=r.top();
l.push(r.top());
r.pop();
}
}
}
bool cmp(pair<int,int>a,pair<int,int>b)
{
return a.first+a.second<b.first+b.second;
}
signed main()
{
int N,K;
cin >> K >> N;
int i;
int ans=0;
vector<pair<int,int>>x;
for(i=0;i<N;i++)
{
char a,b;
int c,d;
cin >> a >> c >> b >> d;
if(a==b)
ans+=max(c,d)-min(c,d);
else
x.push_back({min(c,d),max(c,d)});
}
N=x.size();
sort(x.begin(),x.end(),cmp);
vector<int>la;
vector<int>ra;
la.push_back(0);
int totsu=0;
for(i=0;i<N;i++)
{
ins(x[i].first);
ins(x[i].second);
totsu+=x[i].first+x[i].second;
la.push_back(totsu-2*lc);
}
ra.push_back(0);
totsu=0;
lc=0;
while(l.size())
l.pop();
while(r.size())
r.pop();
for(i=N-1;i>=0;i--)
{
ins(x[i].first);
ins(x[i].second);
totsu+=x[i].first+x[i].second;
int mid=l.top();
ra.push_back(totsu-2*lc);
}
int an=1LL<<60;
for(i=0;i<=N;i++)
{
if(K==2)
an=min(an,la[i]+ra[N-i]);
else
an=la[N];
}
cout << ans+an+N;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:83:13: warning: unused variable 'mid' [-Wunused-variable]
83 | int mid=l.top();
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
2 ms |
364 KB |
Output is correct |
4 |
Correct |
2 ms |
364 KB |
Output is correct |
5 |
Correct |
2 ms |
364 KB |
Output is correct |
6 |
Correct |
2 ms |
364 KB |
Output is correct |
7 |
Correct |
2 ms |
364 KB |
Output is correct |
8 |
Correct |
2 ms |
364 KB |
Output is correct |
9 |
Correct |
3 ms |
364 KB |
Output is correct |
10 |
Correct |
2 ms |
364 KB |
Output is correct |
11 |
Correct |
2 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
2 ms |
364 KB |
Output is correct |
4 |
Correct |
2 ms |
364 KB |
Output is correct |
5 |
Correct |
2 ms |
364 KB |
Output is correct |
6 |
Correct |
2 ms |
364 KB |
Output is correct |
7 |
Correct |
2 ms |
364 KB |
Output is correct |
8 |
Correct |
2 ms |
364 KB |
Output is correct |
9 |
Correct |
3 ms |
364 KB |
Output is correct |
10 |
Correct |
2 ms |
364 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
75 ms |
7132 KB |
Output is correct |
13 |
Correct |
164 ms |
8668 KB |
Output is correct |
14 |
Correct |
118 ms |
7132 KB |
Output is correct |
15 |
Correct |
95 ms |
5036 KB |
Output is correct |
16 |
Correct |
110 ms |
8048 KB |
Output is correct |
17 |
Correct |
147 ms |
8668 KB |
Output is correct |
18 |
Correct |
115 ms |
8284 KB |
Output is correct |
19 |
Correct |
154 ms |
8668 KB |
Output is correct |
20 |
Correct |
117 ms |
8156 KB |
Output is correct |
21 |
Correct |
145 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
2 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
2 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
2 ms |
364 KB |
Output is correct |
14 |
Correct |
2 ms |
364 KB |
Output is correct |
15 |
Correct |
2 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
2 ms |
364 KB |
Output is correct |
18 |
Correct |
2 ms |
364 KB |
Output is correct |
19 |
Correct |
2 ms |
364 KB |
Output is correct |
20 |
Correct |
3 ms |
364 KB |
Output is correct |
21 |
Correct |
2 ms |
364 KB |
Output is correct |
22 |
Correct |
3 ms |
364 KB |
Output is correct |
23 |
Correct |
2 ms |
364 KB |
Output is correct |
24 |
Correct |
2 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
368 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
2 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
2 ms |
364 KB |
Output is correct |
14 |
Correct |
2 ms |
364 KB |
Output is correct |
15 |
Correct |
2 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
2 ms |
364 KB |
Output is correct |
18 |
Correct |
2 ms |
364 KB |
Output is correct |
19 |
Correct |
2 ms |
364 KB |
Output is correct |
20 |
Correct |
3 ms |
364 KB |
Output is correct |
21 |
Correct |
2 ms |
364 KB |
Output is correct |
22 |
Correct |
3 ms |
364 KB |
Output is correct |
23 |
Correct |
3 ms |
364 KB |
Output is correct |
24 |
Correct |
2 ms |
364 KB |
Output is correct |
25 |
Correct |
75 ms |
7132 KB |
Output is correct |
26 |
Correct |
103 ms |
7396 KB |
Output is correct |
27 |
Correct |
147 ms |
8156 KB |
Output is correct |
28 |
Correct |
190 ms |
8828 KB |
Output is correct |
29 |
Correct |
168 ms |
8668 KB |
Output is correct |
30 |
Correct |
85 ms |
4704 KB |
Output is correct |
31 |
Correct |
111 ms |
8028 KB |
Output is correct |
32 |
Correct |
146 ms |
8668 KB |
Output is correct |
33 |
Correct |
117 ms |
8228 KB |
Output is correct |
34 |
Correct |
151 ms |
8668 KB |
Output is correct |
35 |
Correct |
117 ms |
8156 KB |
Output is correct |
36 |
Correct |
149 ms |
8596 KB |
Output is correct |
37 |
Correct |
75 ms |
7100 KB |
Output is correct |