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 FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define ph push
#define f first
#define s second
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ((ll)x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define btinpct(x) __builtin_popcountll((x))
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) { return (rng() % (y+1-x)) + x; } //inclusivesss
string to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){if(a>b)swap(a,b);if(a==0)return b;return gcd(b%a,a);}
#define ll long long int
#define ld long double
#define FOR(ii, ss, ee) for(ll ii = (ss); ii <= (ll)(ee); ++ii)
#define DEC(ii, ss, ee) for(ll ii = (ss); ii >= (ll)(ee); --ii)
typedef pair <ll, ll> pi; typedef pair <ll, pi> spi; typedef pair <pi, pi> dpi;
#define LLINF ((long long) 1e18)//1234567890987654321
#define INF 1234567890ll
// #define cerr if(0)cout
#define MAXN (200006)
ll n, ans;
pi A[MAXN];
int col[MAXN/2][3];
void add(ll a,ll b,ll c,ll d) {
ans += llabs(a-c) + llabs(b-d);
}
int main()
{
FAST
cin>>n;
FOR(i,1,2*n) cin>>A[i].f>>A[i].s;
FOR(i,1,2*n) {
if(1<=A[i].f&&A[i].f<=n&&1<=A[i].s&&A[i].s<=2) ++ col[A[i].f][A[i].s];
else if(1 <= A[i].f && A[i].f <= n) ++ col[A[i].f][(A[i].s>2?2:1)], add(A[i].f, A[i].s, A[i].f, (A[i].s>2?2:1));
else if(A[i].f < 1 && A[i].s >= 2) ++ col[1][2], add(A[i].f, A[i].s, 1, 2);
else if(A[i].f < 1 && A[i].s <= 1) ++ col[1][1], add(A[i].f, A[i].s, 1, 1);
else if(A[i].f > n && A[i].s >= 2) ++ col[n][2], add(A[i].f, A[i].s, n, 2);
else if(A[i].f > n && A[i].s <= 1) ++ col[n][1], add(A[i].f, A[i].s, n, 1);
else assert(0);
}
// cerr<<"ans: "<<ans<<"\n";
// FOR(j,1,2) FOR(i,1,n) cout<<col[i][j]<<" \n"[i==n];
int d1=0, d2=0;
FOR(i,1,n) {
d1 += col[i][1]-1;
d2 += col[i][2]-1;
if(d1 < 0 && d2 > 0) {
int tmp=min(d2, -d1);
ans += tmp, d1 += tmp, d2 -= tmp;
} else if(d1 > 0 && d2 < 0) {
int tmp=min(d1, -d2);
ans += tmp, d2 += tmp, d1 -= tmp;
}
ans += abs(d1);
ans += abs(d2);
}
cout<<ans<<'\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... |