1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
| #include "bits/stdc++.h" #include "atcoder/all" using namespace std; #define fi first #define se second #define vi vector<int> #define pb push_back #define eb emplace_back #define pii pair<int,int> #define mp make_pair #define rep( i , a , b ) for( int i = (a) , i##end = b ; i <= i##end ; ++ i ) #define per( i , a , b ) for( int i = (a) , i##end = b ; i >= i##end ; -- i ) #define mem( a ) memset( a , 0 , sizeof (a) ) #define all( x ) x.begin() , x.end()
typedef long long ll; const int MAXN = 1e6 + 10; const int P = 1e9 + 7; int n;
vector<pii> pois; map<int,int> idxx , idxy; int ret[MAXN]; int cx , cy; int cnx[MAXN] , cny[MAXN] , idx[MAXN]; int sol() { vector<pii> poi[3]; rep( i , 0 , 2 ) poi[i].clear(); for( auto& t : pois ) { poi[t.se % 3].pb( t ); } int ans = 1e9; int cur[3] , bd[3]; map<int,int> S; rep( rem , 0 , 2 ) { idxx.clear() , idxy.clear(); cx = cy = 0; auto &po = poi[rem]; for( auto& t : po ) { if( !idxx.count( t.fi ) ) idxx[t.fi] = ++ cx; if( !idxy.count( t.se ) ) idxy[t.se] = ++ cy; cnx[idxx[t.fi]] ++ , cny[idxy[t.se]] ++; } int res = 0 , rx = 0 , ry = 0; rep( i , 1 , cx ) if( cnx[i] & 1 ) ++ rx; rep( i , 1 , cy ) if( cny[i] & 1 ) ++ ry; bd[rem] = ry , cur[rem] = rx; for( auto& t : idxx ) if( cnx[t.se] & 1 ) { S[t.fi] |= ( 1 << rem ); } rep( i , 1 , cx ) cnx[i] = 0; rep( i , 1 , cy ) cny[i] = 0; } int ops[12]; mem( ops );
for( auto t : S ) ++ ops[t.se]; int fcur[3];
auto chkans = [&]( ) { int ret = 0; rep( i , 0 , 2 ) ret = ret + max( fcur[i] - bd[i] , 0 ); ans = min( ans , ret ); };
int tim = 0; auto reset = [&] () { tim = 0; memcpy( fcur , cur , sizeof fcur ); }; auto doop = [&]( int x ) { if( x & 1 ) -- fcur[0]; else ++ fcur[0]; if( x & 2 ) -- fcur[1]; else ++ fcur[1]; if( x & 4 ) -- fcur[2]; else ++ fcur[2]; ++ tim; if( ~tim & 1 ) chkans(); }; auto rollback = [&] ( int x ) { if( x & 1 ) ++ fcur[0]; else -- fcur[0]; if( x & 2 ) ++ fcur[1]; else -- fcur[1]; if( x & 4 ) ++ fcur[2]; else -- fcur[2]; -- tim; };
reset(); chkans();
rep( i , 0 , 2 ) rep( j , 0 , 2 ) if( j != i ) { int t = i ^ j ^ 3; int si = ( 1 << i ) , sj = ( 1 << j ) , st = ( 1 << t ); reset(); rep( k , 1 , ops[7] ) doop( 7 ); rep( k , 1 , ops[sj | st] ) doop( sj | st ); rep( k , 1 , ops[si | st] ) doop( si | st ); rep( k , 1 , ops[si | sj] ) doop( si | sj ); } rep( i , 0 , 2 ) rep( j , 0 , 2 ) if( j != i ) { int t = i ^ j ^ 3; int si = ( 1 << i ) , sj = ( 1 << j ) , st = ( 1 << t ); reset(); rep( k , 1 , ops[7] ) doop( 7 ); rep( k , 1 , ops[sj | st] ) doop( sj | st ); rep( k , 1 , ops[si | st] ) doop( si | st ); rep( k , 1 , ops[st] ) doop( st ); } return ans + bd[0] + bd[1] + bd[2]; }
void solve() { cin >> n; rep( i , 1 , n ) { int x , y; scanf("%d%d",&x,&y); pois.eb( x , y ); } int res = sol(); cout << res << endl; }
signed main() {
solve(); }
|