Submission #496865

#TimeUsernameProblemLanguageResultExecution timeMemory
496865NalrimetCoin Collecting (JOI19_ho_t4)C++17
100 / 100
62 ms3272 KiB
#include <bits/stdc++.h>

#define ll long long
#define pb push_back
#define F first
#define S second

using namespace std;

const int N = 1e5 + 5;

ll ans;
int n, x, y, a[N][5], x1, x2, change;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

	cin >> n;

	for(int i = 1, x, y; i <= n * 2; ++i){
        cin >> x >> y;
        if(x > n) {ans += x - n; x = n;}
        if(x < 1) {ans += 1 - x; x = 1;}
        if(y > 2) {ans += y - 2; y = 2;}
        if(y < 1) {ans += 1 - y; y = 1;}
        a[x][y]++;
	}
	
	for(int i = 1; i <= n; ++i){
        x1 += a[i][1];
        x2 += a[i][2];
        x1--;
        x2--;
        if((x1 > 0 && x2 < 0) || (x1 < 0 && x2 > 0)){
            change = min(abs(x1), abs(x2));
            ans += change;
            if(x1 < 0) x1 += change;
            else x1 -= change;
            if(x2 < 0) x2 += change;
            else x2 -= change;
        }
        ans += abs(x1) + abs(x2);
	}

	cout << ans;

	return 0;
}

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