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;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
const ll mod=1e9+7;
const ll maxn=1e5+5;
const ll INF=1e18;
#define ok ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define fi first
#define se second
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define endl '\n'
int n, k, arr[maxn][2], cnt, mid, lmid;
char c[maxn][2];
ll tmp, pos1, pos2;
vi isi;
int main() {
cin >> k >> n;
for(int i=0; i<n; i++) {
for(int j=0; j<2; j++) cin >> c[i][j] >> arr[i][j];
}
for(int i=0; i<n; i++) {
if(c[i][0]==c[i][1]) tmp+=abs(arr[i][0]-arr[i][1]);
else {
cnt++;
isi.pb(arr[i][0]);
isi.pb(arr[i][1]);
}
}
tmp+=cnt;
for(auto it : isi) {
cnt--;
if(cnt<=0) {
mid=it;
break;
}
lmid=it;
}
for(auto it : isi) {
pos1+=abs(it-mid);
pos2+=abs(it-lmid);
}
cout << min(tmp+pos1, tmp+pos2);
}
# | 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... |