# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
955116 | hihihihaw | Palembang Bridges (APIO15_bridge) | C++17 | 0 ms | 436 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define pii pair<int,int>
#define sz(v) (int)v.size()
#define fi first
#define se second
#define INF 1223372036854775807
#define MOD 1000000007
#define cint(x) int x;cin>>x;
#define cinarr(a,n) int a[n];for (int i=0;i<n;i++) cin>>a[i];
#define coutarr(a) for (auto d:a) cout<<d<<" "; cout<<endl;
#define coutarrD(a) for (auto d:a) cout<<d.fi<<","<<d.se<<" "; cout<<endl;
#define AYBERK_SARICA ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define endl '\n'
#define ld long double
int t=1;
int usaco=0;
void solve(){
int k,n;
cin>>k>>n;
vector<vector<int>> v;
int ans=0;
for (int i=0;i<n;i++){
char c1,c2;
int x1,x2;
cin>>c1>>x1>>c2>>x2;
if (c1==c2) ans+=abs(x1-x2);
else{
ans++;
v.pb({x1+x2,x1,x2});
}
}
sort(v.begin(),v.end());
int pfMed[sz(v)];
multiset<int> s1;multiset<int> s2;
int sm1=0,sm2=0;
for (int i=0;i<sz(v);i++){
if (i==0){
if (v[i][1]<v[i][2]){
s1.insert(v[i][1]);sm1+=v[i][1];
s2.insert(v[i][2]);sm2+=v[i][2];
}
else{
s2.insert(v[i][1]);sm2+=v[i][1];
s1.insert(v[i][2]);sm1+=v[i][1];
}
}
else{
if (v[i][1]>=*s2.begin()){ s2.insert(v[i][1]);sm2+=v[i][1];}
else{sm1+=v[i][1];s1.insert(v[i][1]);}
if (v[i][2]>=*s2.begin()){ s2.insert(v[i][2]);sm2+=v[i][2];}
else{sm1+=v[i][2];s1.insert(v[i][2]);}
if (sz(s1)>sz(s2)){
sm2+=*s1.rbegin();
s2.insert(*s1.rbegin());
sm1-=*(--s1.end());
s1.erase(--s1.end());
}
else if (sz(s2)>sz(s1)){
sm1+=*s2.begin();
s1.insert(*s2.begin());
sm2-=*s2.begin();
s2.erase(s2.begin());
}
}
pfMed[i]=sm2-sm1;
}
int sfMed[sz(v)];
s1.clear();s2.clear();
sm1=0,sm2=0;
for (int i=sz(v)-1;i>=0;i--){
if (i==sz(v)-1){
if (v[i][1]<v[i][2]){
s1.insert(v[i][1]);sm1+=v[i][1];
s2.insert(v[i][2]);sm2+=v[i][2];
}
else{
s2.insert(v[i][1]);sm2+=v[i][1];
s1.insert(v[i][2]);sm1+=v[i][1];
}
}
else{
if (v[i][1]>=*s2.begin()){ s2.insert(v[i][1]);sm2+=v[i][1];}
else{sm1+=v[i][1];s1.insert(v[i][1]);}
if (v[i][2]>=*s2.begin()){ s2.insert(v[i][2]);sm2+=v[i][2];}
else{sm1+=v[i][2];s1.insert(v[i][2]);}
if (sz(s1)>sz(s2)){
sm2+=*s1.rbegin();
s2.insert(*s1.rbegin());
sm1-=*(--s1.end());
s1.erase(--s1.end());
}
else if (sz(s2)>sz(s1)){
sm1+=*s2.begin();
s1.insert(*s2.begin());
sm2-=*s2.begin();
s2.erase(s2.begin());
}
}
sfMed[i]=sm2-sm1;
}
int mn=INF;
for (int i=0;i<sz(v)-1;i++){
mn=min(mn,sfMed[i+1]+pfMed[i]);
}
cout<<mn+ans<<endl;
}
int32_t main(){
//AYBERK_SARICA;
if (usaco){
freopen("fortmoo.in", "r", stdin);
freopen("fortmoo.out", "w", stdout);
}
#ifdef Local
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
//freopen("wormsort.out", "w", stdout);
#endif
if (t==1) solve();
else{
cin>>t;
while (t--) solve();
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |