This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define F first
#define S second
#define pii pair<int,int>
#define piii pair<pair<int,int>,int>
const int N = 3e5+10;
const int M = 3e2+10;
const LL INF = 1e9;
const LL LINF = 2e18;
const LL MOD = 1e9+7;
const double PI = 3.141592653589793;
vector<LL>l,r;
LL pr[N],pr1[N];
vector<pair<LL,LL>>vec;
LL calc(LL p){
LL ret = 0;
LL idx = upper_bound(r.begin(),r.end(),p)-r.begin()-1;
if(idx>=0)
ret += p*(idx+1)-pr1[idx];
idx = upper_bound(l.begin(),l.end(),p)-l.begin();
if(idx!=l.size())
ret += pr[idx]-p*(l.size()-idx);
return ret;
}
LL get_dis(pair<LL,LL>p,LL p1){
if(p1<p.F)
return p.F-p1;
if(p1>p.S)
return p1-p.S;
return 0;
}
LL calc(LL p,LL p1){
LL ret = 0;
for(auto x:vec)
ret += min(get_dis(x,p),get_dis(x,p1));
return ret;
}
void ans1(LL ans){
sort(l.begin(),l.end());
sort(r.begin(),r.end());
for(int i=l.size()-1;i>=0;i--)
pr[i] = pr[i+1]+l[i];
for(int i=0;i<r.size();i++)
pr1[i] = (i?pr1[i-1]:0)+r[i];
LL mn = l.size()?LINF:0;
for(auto x:l)
mn = min(mn,calc(x));
for(auto x:r)
mn = min(mn,calc(x));
printf("%lld\n",ans+mn*2);
}
void ans2(LL ans){
vector<LL>points;
for(auto x:vec)
points.push_back(x.F),points.push_back(x.S);
sort(points.begin(),points.end());
LL mn = points.size()?LINF:0;
for(int i=0;i<points.size();i++)
for(int j=i;j<points.size();j++)
mn = min(mn,calc(points[i],points[j]));
printf("%lld\n",ans+mn*2);
}
int main(){
//freopen("out.txt","w",stdout);
int k,n;
scanf("%d%d",&k,&n);
LL ans = 0;
for(int i=1;i<=n;i++){
char c1,c2;
LL d1,d2;
cin>>c1>>d1>>c2>>d2;
ans += abs(d1-d2);
if(c1==c2)continue;
ans++;
l.push_back({min(d1,d2)});
r.push_back(max(d1,d2));
vec.push_back({min(d1,d2),max(d1,d2)});
}
if(k==1)ans1(ans);
else ans2(ans);
}
Compilation message (stderr)
bridge.cpp: In function 'long long int calc(long long int)':
bridge.cpp:29:11: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | if(idx!=l.size())
| ~~~^~~~~~~~~~
bridge.cpp: In function 'void ans1(long long int)':
bridge.cpp:62:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for(int i=0;i<r.size();i++)
| ~^~~~~~~~~
bridge.cpp: In function 'void ans2(long long int)':
bridge.cpp:83:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | for(int i=0;i<points.size();i++)
| ~^~~~~~~~~~~~~~
bridge.cpp:84:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | for(int j=i;j<points.size();j++)
| ~^~~~~~~~~~~~~~
bridge.cpp: In function 'int main()':
bridge.cpp:95:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
95 | scanf("%d%d",&k,&n);
| ~~~~~^~~~~~~~~~~~~~
# | 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... |