Code
#include <bits/stdc++.h>
using namespace std;
void solve(vector<int> &type, vector<int> &x) {
int n = x.size();
vector<vector<int>> occ_index(n + 1);
vector<int> taken_count(n + 1);
vector<bool> picked(n);
bool possible = true;
for (int i = 0; i < n; i++) {
// Do a lazy pickup.
if (type[i] == 1) {
occ_index[x[i]].push_back(i);
}
if (type[i] == 2) {
vector<int> &occurrences = occ_index[x[i]];
if (occurrences.empty()) {
possible = false;
break;
}
int idx = occurrences.back();
occurrences.pop_back();
picked[idx] = true;
// Add 1 to the range [idx, i]
taken_count[idx] += 1;
taken_count[i + 1] -= 1;
}
}
// Convert the difference array to a prefix sum array.
// This will retrieve the array after range queries.
for (int i = 1; i < n; i++) {
taken_count[i] += taken_count[i - 1];
}
if (!possible) {
cout << "-1"
<< "\n";
return;
}
int mx = -1;
for (int i = 0; i < n; i++) {
mx = max(mx, taken_count[i]);
}
cout << mx << "\n";
for (int i = 0; i < n; i++) {
if (type[i] == 1) {
cout << picked[i] << " ";
}
}
cout << "\n";
}
int main() {
int n;
cin >> n;
vector<int> type(n), x(n);
for (int i = 0; i < n; i++) {
cin >> type[i] >> x[i];
}
solve(type, x);
return 0;
}