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 fi first
#define se second
#define eb emplace_back
#define mt make_tuple
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define MOD 1000000007
typedef long long ll;
typedef pair <int, int> ii;
typedef pair <ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef long double ld;
bool DEBUG=0;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
int k,n;
vll coor;
vector<pll>str;
cin >> k >> n;
ll ans = 0ll;
for(int i=0; i<n; i++){
char p; ll s; char q; ll t;
cin >> p >> s >> q >> t;
if(p==q){
ans += abs(s-t);
}else{
if(s>t)swap(s,t);
ans+=t-s+1ll;
str.eb(s,t);
}
}
n=sz(str);
//k=1
for(ii x:str){
coor.eb(x.fi);
coor.eb(x.se);
}
sort(all(coor));
ll bridge1=coor[n-1],bridge2=coor[n];
ll res1=0ll,res2=0ll;
for(int i=0; i<n; i++){
if(bridge1<str[i].fi){
res1 += 2ll*(str[i].fi-bridge1);
}else if(bridge1>str[i].se){
res1 += 2ll*(bridge1-str[i].se);
}
if(bridge2<str[i].fi){
res2 += 2ll*(str[i].fi-bridge2);
}else if(bridge2>str[i].se){
res2 += 2ll*(bridge2-str[i].se);
}
}
ans += min(res1,res2);
cout << ans << '\n';
return 0;
}
# | 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... |