제출 #532924

#제출 시각아이디문제언어결과실행 시간메모리
532924amunduzbaevCoin Collecting (JOI19_ho_t4)C++17
0 / 100
1 ms204 KiB
#include "bits/stdc++.h"
using namespace std;

#define ar array
#define int long long

signed main(){
	ios::sync_with_stdio(0); cin.tie(0);
	
	int n; cin>>n; n <<= 1;
	vector<ar<int, 2>> a(n);
	for(int i=0;i<n;i++) cin>>a[i][0]>>a[i][1];
	long long res = 0;
	for(int i=0;i<n;i++){
		if(a[i][1] > 2) res += (a[i][1] - 2), a[i][1] = 2;
		if(a[i][1] < 1) res += (1 - a[i][1]), a[i][1] = 1;
	}
	
	vector<ar<int, 2>> b(n);
	for(int i=0;i<n;i++){
		b[i][0] = i / 2 + 1, b[i][1] = (i&1) + 1;
	}
	
	sort(a.begin(), a.end());
	sort(b.begin(), b.end());
	deque<int> tmp[2];
	for(int i=0;i<n;i++){
		if(a[i][1] == b[i][1]){
			res += abs(a[i][0] - b[i][0]);
		} else {
			tmp[a[i][1] - 1].push_back(i);
		}
	}
	
	while(!tmp[0].empty() && !tmp[1].empty()){
		int i = tmp[0].front(), j = tmp[1].front();
		if(a[i][0] > a[j][0]) swap(i, j);
		if(a[j][0] <= b[i][0] || b[j][0] <= a[i][0]){
			res += abs(a[i][0] - b[j][0]) + abs(a[j][0] - b[i][0]);
			tmp[0].pop_front(), tmp[1].pop_front();
		} else {
			res += abs(a[i][0] - b[i][0]) + 1;
			if(tmp[0].front() == i) tmp[0].pop_front();
			else tmp[1].pop_front();
		}
	}
	
	for(auto i : tmp[0]) res += abs(a[i][0] - b[i][0]) + 1;
	for(auto i : tmp[1]) res += abs(a[i][0] - b[i][0]) + 1;
	
	cout<<res<<"\n";
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...