Submission #1149405

#TimeUsernameProblemLanguageResultExecution timeMemory
1149405KluydQCoin Collecting (JOI19_ho_t4)C++20
100 / 100
35 ms6216 KiB
#include <bits/stdc++.h>
 
#define respagold ios_base::sync_with_stdio(0), cin.tie(0);
#define ll long long
#define int long long
#define int2 __int128_t
#define FOR( i, x, n, d ) for( int i = x; i <= n; i += d )
#define FORR( i, x, n, d ) for( int i = x; i >= n; i -= d )
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x.size())
#define pb push_back
#define ins insert
#define lb lower_bound
#define ub upper_bound
#define pii pair <int, int>
#define mkp make_pair
 
using namespace std;
 
const int N = 1e6 + 123;
const int inf = 2e18;
const int MOD = 1e9 + 7;
const int MOD1 = 998244353;
const int P = 6547;
 
int a[N], b[N], c[N], n, m, x, y, z, w;
int mz[N], used[N][2], dolg1[N], dolg2[N];
 
mt19937 rng( chrono::steady_clock::now().time_since_epoch().count());
 
int rand( int l, int r )
{
    uniform_int_distribution <int> uid( l, r );
    return uid( rng );
}
void solve()
{
	cin >> n;
	int ans = 0;
	set <int> st1, st2;
	
	FOR( i, 1, n * 2, 1 )
	{
		cin >> x >> y;
		
		if( x < 1 ) ans += 1 - x, x = 1;
		if( x > n ) ans += x - n, x = n;
		if( y < 1 ) ans += 1 - y, y = 1;
		if( y > 2 ) ans += y - 2, y = 2;
		
		used[x][y] ++;
	}
	FOR( i, 1, n, 1 )
	{
		if( used[i][1] > 1 ) st1.ins(i);
		if( used[i][2] > 1 ) st2.ins(i);
		
		if( used[i][1] == 0 ) dolg1[i] = 1;
		if( used[i][2] == 0 ) dolg2[i] = 1;
		
		used[i][1] --, used[i][2] --;
	}
	int sos1 = 0, sos2 = 0;
	
	FOR( i, 1, n, 1 )
	{
		if( !dolg1[i] ) sos1 += used[i][1];
		else sos1 --;
		
		if( !dolg2[i] ) sos2 += used[i][2];
		else sos2 --;
		
		while(1)
		{
			if( min( sos1, sos2 ) >= 0 || max( sos1, sos2 ) <= 0 ) break;
			
			if( sos1 < sos2 ) sos1 ++, sos2 --, ans ++;
			if( sos2 < sos1 ) sos2 ++, sos1 --, ans ++;
			
			if( sos1 == 0 && sos2 == 0 ) break;
		}
		// dolg ili ostatok vpered
		if( sos1 <= 0 && sos2 <= 0 ) ans = ans - sos1 - sos2;
		if( sos1 >= 0 && sos2 >= 0 ) ans += sos1 + sos2;
	}
	cout << ans;
}
signed main() 
{
//	freopen("connect.in", "r", stdin);
//	freopen("connect.out", "w", stdout);
	    
    respagold
    
    int test = 1;
    
    if( !test ) cin >> test;
	
	while( test -- )
	{
		solve();
	}
}
// solved by KluydQ  
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...