제출 #1168406

#제출 시각아이디문제언어결과실행 시간메모리
1168406yellowtoadCoin Collecting (JOI19_ho_t4)C++20
0 / 100
0 ms328 KiB
#include <iostream>
#include <queue>
using namespace std;

long long n, x, y, xx, yy, a[100010][3], ans;
queue<int> q[3];

int main() {
	cin >> n;
	for (int i = 1; i <= 2*n; i++) {
		cin >> x >> y;
		if (y <= 1) yy = 1;
		else yy = 2;
		if (x < 1) xx = 1;
		else if (x > n) xx = n;
		else xx = x;
		a[xx][yy]++;
		ans += abs(x-xx)+abs(y-yy);
	}
	for (int i = 1; i <= n; i++) for (int j = 1; j <= 2; j++) for (int k = 2; k <= a[i][j]; k++) q[j].push(i);
	for (int i = 1; i <= n; i++) {
		if (a[i][1] == 0) {
			if ((q[2].empty()) || ((q[1].size()) && (abs(i-q[1].front()) <= abs(i-q[2].front())+1))) {
				ans += abs(i-q[1].front());
				q[1].pop();
			} else {
				ans += abs(i-q[2].front())+1;
				q[2].pop();
			}
		}
		if (a[i][2] == 0) {
			if ((q[1].empty()) || ((q[2].size()) && (abs(i-q[2].front()) <= abs(i-q[1].front())+1))) {
				ans += abs(i-q[2].front());
				q[2].pop();
			} else {
				ans += abs(i-q[1].front())+1;
				q[1].pop();
			}
		}
	}
	cout << ans << endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...