#include <bits/stdc++.h>
#define int long long
#define iris 998244353
#define pii pair<int,int>
using namespace std;
vector<int> ouo;
vector<pii> arr;
priority_queue<int> ll;
priority_queue<int,vector<int>,greater<int> > rr;
int lsum,rsum;
int ans[100010];
bool cmp(pii a,pii b)
{
return a.first+a.second<b.first+b.second;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
char x,y;
int k,n,i,a,b,sum,sagiri,l,r,m;
sum=0;
cin>>k>>n;
for(i=0;i<n;i++)
{
cin>>x>>a>>y>>b;
if(x==y)
{
sum+=abs(a-b);
}
else
{
sum++;
if(k==1)
{
ouo.emplace_back(a);
ouo.emplace_back(b);
}
else
{
arr.emplace_back(make_pair(a,b));
}
}
}
if(ouo.empty() && arr.empty())
{
cout<<sum<<'\n';
return 0;
}
if(k==1)
{
sort(ouo.begin(), ouo.end());
m=ouo[ouo.size()/2];
for(int aoi:ouo)
{
sum+=abs(aoi-m);
}
cout<<sum<<'\n';
}
else
{
sort(arr.begin(), arr.end(), cmp);
lsum=rsum=0;
for(i=0;i<arr.size();i++)
{
l=arr[i].first;
r=arr[i].second;
ll.push(l);
ll.push(r);
lsum+=l+r;
while(!ll.empty() && !rr.empty() && ll.top()>rr.top())
{
rsum+=ll.top();
lsum-=ll.top();
rr.push(ll.top());
ll.pop();
}
while(ll.size()+1 < rr.size())
{
lsum+=rr.top();
rsum-=rr.top();
ll.push(rr.top());
rr.pop();
}
while(ll.size() > rr.size())
{
rsum+=ll.top();
lsum-=ll.top();
rr.push(ll.top());
ll.pop();
}
m=ll.top();
ans[i+1]=m*ll.size()-lsum+rsum-m*rr.size();
}
while(!ll.empty())
ll.pop();
while(!rr.empty())
rr.pop();
sagiri=1e18;
lsum=rsum=0;
for(i=arr.size()-1;i>=0;i--)
{
l=arr[i].first;
r=arr[i].second;
ll.push(l);
ll.push(r);
lsum+=l+r;
while(!ll.empty() && !rr.empty() && ll.top()>rr.top())
{
rsum+=ll.top();
lsum-=ll.top();
rr.push(ll.top());
ll.pop();
}
while(ll.size()+1 < rr.size())
{
lsum+=rr.top();
rsum-=rr.top();
ll.push(rr.top());
rr.pop();
}
while(ll.size() > rr.size())
{
rsum+=ll.top();
lsum-=ll.top();
rr.push(ll.top());
ll.pop();
}
m=ll.top();
ans[i]+=m*ll.size()-lsum+rsum-m*rr.size();
sagiri=min(sagiri, ans[i]);
}
cout<<sagiri+sum<<'\n';
}
return 0;
}
Compilation message
bridge.cpp: In function 'int main()':
bridge.cpp:69:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0;i<arr.size();i++)
~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
26 ms |
2552 KB |
Output is correct |
13 |
Correct |
56 ms |
2552 KB |
Output is correct |
14 |
Correct |
37 ms |
2552 KB |
Output is correct |
15 |
Correct |
33 ms |
1532 KB |
Output is correct |
16 |
Correct |
35 ms |
2552 KB |
Output is correct |
17 |
Correct |
44 ms |
2544 KB |
Output is correct |
18 |
Correct |
48 ms |
2672 KB |
Output is correct |
19 |
Correct |
54 ms |
2552 KB |
Output is correct |
20 |
Correct |
36 ms |
2552 KB |
Output is correct |
21 |
Correct |
48 ms |
2552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
256 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
6 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
6 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
6 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
512 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
6 ms |
384 KB |
Output is correct |
15 |
Correct |
6 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
6 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
6 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
512 KB |
Output is correct |
24 |
Correct |
6 ms |
512 KB |
Output is correct |
25 |
Correct |
52 ms |
6508 KB |
Output is correct |
26 |
Correct |
87 ms |
6636 KB |
Output is correct |
27 |
Correct |
113 ms |
7400 KB |
Output is correct |
28 |
Correct |
118 ms |
8040 KB |
Output is correct |
29 |
Correct |
118 ms |
8040 KB |
Output is correct |
30 |
Correct |
63 ms |
4080 KB |
Output is correct |
31 |
Correct |
57 ms |
7404 KB |
Output is correct |
32 |
Correct |
103 ms |
8040 KB |
Output is correct |
33 |
Correct |
81 ms |
7656 KB |
Output is correct |
34 |
Correct |
107 ms |
8040 KB |
Output is correct |
35 |
Correct |
67 ms |
7528 KB |
Output is correct |
36 |
Correct |
105 ms |
7784 KB |
Output is correct |
37 |
Correct |
41 ms |
6508 KB |
Output is correct |