제출 #249661

#제출 시각아이디문제언어결과실행 시간메모리
249661groeneprofCoin Collecting (JOI19_ho_t4)C++14
100 / 100
74 ms6632 KiB
#include <bits/stdc++.h>
#define int long long
#define P(x) do {if(debug) cout << x << endl;} while(false)
#define H(x) P(#x << ": " << x)
#define FR(i, a, b) for(int i = (a); i < (b); ++i)
#define F(i, n) FR(i, 0, n)
#define DR(i, a, b) for(int i = (b); i --> (a);)
#define D(i, n) DR(i, 0, n)
#define S(s) (int)(s).size()
#define ALL(x) (x).begin(), (x).end()
#define MI(x, a) (x) = min(x, a)
#define MA(x, a) (x) = max(x, a)
#define V vector
#define pb push_back
#define mp make_pair
#define a coins[0][i]
#define b coins[1][i]
using namespace std;
const bool debug = 1;
const int inf = 1e18;
signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int N;
	cin>>N;
	int res = 0;
	vector<vector<int> > coins(2,vector<int> (N+1,-1));
	F(i,2*N){
		int x,y;
		cin>>x>>y;
		int X = min(N,max(1ll,x));
		int Y = min(2ll,max(1ll,y));
		res += abs(X-x) + abs(Y-y);
		coins[Y-1][X-1]++;

	}

	F(i,N){
		if(a>b){
			swap(coins[0],coins[1]);
		}
		if(a<0&&b>0){
			if(abs(a)>abs(b)){
				swap(coins[0],coins[1]);
			}
			res+=abs(a);
			b = a+b;
			a = 0;
		}
		res+=abs(a)+abs(b);
		coins[0][i+1] += a;
		coins[1][i+1] += b;
	}
	cout<<res<<endl;
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...