제출 #497964

#제출 시각아이디문제언어결과실행 시간메모리
497964Nuraly_SerikbayCoin Collecting (JOI19_ho_t4)C++14
100 / 100
54 ms5148 KiB
#include <bits/stdc++.h>
  
#define endl "\n"
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define sz size()
#define rep(i,k,n) for(int i = k ; i <= n ; ++i)
#define per(i,k,n) for(int i = k ; i >= n ; --i)
#define YOSIK() ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define all(x) x.begin(),x.end()
#define fr(x) freopen(x".in","r",stdin),freopen(x".out","w",stdout)
#define toqta return 0
#define PERMUTE next_permutation
#define no cout<<"No"<<endl;
#define yes cout<<"Yes"<<endl;
#define a() a + 1, a + n + 1  

using namespace std;
 
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse,-fgcse-lm")
#pragma GCC optimize("-ftree-pre,-ftree-vrp")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("unroll-loops")

   
typedef long long ll;
typedef unsigned long long ull;
typedef string S;
typedef double ld;
typedef long double lld;
  
const int N = 1e6 + 17;
const int modd = 1e9 + 7;
const ll INF = 1e18 - 19;
const int P = 37;
const ll NN = 1e7 + 17;
const ld eps = 1e-19;
const double pi = 3.141592653589793238462643383279 ;
   
 
bool sortbysec(const pair<int,int> &a, const pair<int,int> &b){
    return (a.second < b.second);
}
 
void pre (ll a) {
    cout << fixed << setprecision(a);
    return;
}

ll n;
ll in[3][100100];
ll totalans;


inline void Solution () {
	cin >> n;
	for (int i = 1; i <= n * 2; ++ i) {
		ll x, y;
		cin >> x >> y;
		if (1 <= x && x <= n && 1 <= y && y <= 2) {
			in[y][x] ++;
			continue;
		}
		if (1 <= x && x <= n) {
			if (y < 1) totalans += 1 - y, in[1][x] ++;
			else totalans += y - 2, in[2][x] ++;
		}
		else if (1 <= y && y <= 2) {
			if (x < 1) totalans += 1 - x, in[y][1] ++;
			else totalans += x - n, in[y][n] ++;

		}
		else {
			if (x < 1 && y < 1) {
				totalans += 1 - x + 1 - y;
				in[1][1] ++;
			}
			if (x > n && y > 2) {
				totalans += x - n + y - 2;
				in[2][n] ++;
			}
			if (x > n && y < 1) {
				totalans += x - n + 1 - y;
				in[1][n] ++;
			}
			if (x < 1 && y > 2) {
				totalans += 1 - x + y - 2;
				in[2][1] ++;
			}
		}	
	}
	ll a, b = 0;
	a = b;
	for (int i = 1; i <= n; ++ i) {
		a += in[1][i] - 1;
		b += in[2][i] - 1;
		ll mn = min (abs(a), abs(b));
		if (a < 0 && b > 0) {
			totalans += mn;
			b -= mn;
			a += mn;	
		}
		else if (a > 0 && b < 0) {
			totalans += mn;
			b += mn;
			a -= mn;
		}
		totalans += abs(a) + abs(b);
	}
	cout << totalans;
	return;
}
 
 
int main () {
	YOSIK();
	Solution ();
	exit (0);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...