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 = 3e4+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<int>l,r;
LL pr[N],pr1[N];
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;
}
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;
int 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));
}
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] = (1?pr1[i-1]:0)+r[i];
LL mn = LINF;
for(auto x:l)
mn = min(mn,calc(x));
for(auto x:r)
mn = min(mn,calc(x));
printf("%lld\n",ans+mn*2);
}
Compilation message (stderr)
bridge.cpp: In function 'long long int calc(long long int)':
bridge.cpp:28:11: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | if(idx!=l.size())
| ~~~^~~~~~~~~~
bridge.cpp: In function 'int main()':
bridge.cpp:59:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for(int i=0;i<r.size();i++)
| ~^~~~~~~~~
bridge.cpp:39:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
39 | 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... |