Submission #831198

#TimeUsernameProblemLanguageResultExecution timeMemory
831198NK_Towers (NOI22_towers)C++17
12 / 100
2033 ms186640 KiB
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>

using namespace std;
 
#define nl '\n'
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define sz(x) int(x.size())
 
template<class T> using V = vector<T>;
using pi = pair<int, int>;
using vi = V<int>;
using vpi = V<pi>;

using ll = long long;
using pl = pair<ll, ll>;
using vpl = V<pl>;
using vl = V<ll>;

using db = long double;

template<class T> using pq = priority_queue<T, V<T>, greater<T>>;

const int MOD = 1e9 + 7;
const ll INFL = ll(1e16) + 10;

int main() {
	cin.tie(0)->sync_with_stdio(0);
	
	int N; cin >> N;
	vpi A(N); for(auto& x : A) cin >> x.f >> x.s;


	for(int t = 0; t < 2; t++) {
		map<int, int> mp; int cur = 0;
		for(auto& x : A) mp[x.f]++;
		for(auto& x : mp) x.s = cur++;
		for(auto& x : A) x.f = mp[x.f], swap(x.f, x.s);
	}

	map<pi, int> IDX; for(int i = 0; i < N; i++) {
		// cerr << A[i].f << " " << A[i].s << " is " << i << endl;
		IDX[A[i]] = i;
	}

	V<vi> C(N);
	for(int i = 0; i < N; i++) C[A[i].s].pb(A[i].f);

	for(int i = 0; i < N; i++) sort(begin(C[i]), end(C[i]));

	V<vpi> R(N); 

	vi POS(2 * N, -1);
	function<void(int)> update = [&](int x) {
		// cout << "UPDATE " << x << endl;
		if (POS[x] == -1) return;
		// 2*y+c => y is col # and c is dir (0 - dwn (+1), 1 - up (-1));
		while(1) {
			int y = x / 2, dir = (x % 2 ? -1 : +1);
			if (POS[x] < 0 || POS[x] >= sz(C[y])) {
				POS[x] = -1; return;
			}

			if (POS[2*y] == POS[2*y+1]) {
				POS[x] = -1;
				return; // stop because same as other one
			}

			int r = C[y][POS[x]]; // row
			// cerr << x << " " << y << " " << dir << " " << r << endl;

			// check if you can put it in
			if (sz(R[r]) <= 1) {
				// cerr << "ADDED TO " << r << endl;
				R[r].pb(mp(y, x)); return;
			} else if (R[r].back().f < y) { // sz(R[r]) == 2
				// cerr << "ADDED2 TO " << r << endl;
				// cerr << "MUST UPDATE: " << i << endl;
				int i = R[r].back().s; 
				R[r].pop_back(); R[r].pb(mp(y, x));
				return update(i);
			}	

			POS[x] += dir;
		}
	};

	vi cols; for(int i = 0; i < N; i++) {
		cols.pb(A[i].s);
	}

	sort(begin(cols), end(cols)); cols.erase(unique(begin(cols), end(cols)), end(cols));

	for(auto& c : cols) { 
		POS[2 * c] = 0; 
		update(2 * c); 

		POS[2 * c + 1] = sz(C[c]) - 1;
		update(2 * c + 1);
	}

	vpi take;
	for(int i = 0; i < 2 * N; i++) {
		int c = i / 2; 
		if (0 <= POS[i] && POS[i] < sz(C[c])) {
			take.pb(mp(C[c][POS[i]], c));
			// cerr << i << " " << c << " " << C[c][POS[i]] << endl;
		}
	}

	string ans(N, '0');
	for(auto& p : take) ans[IDX[p]] = '1';

	cout << ans << nl;

    return 0;
} 	
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...