Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int arr[3000000];
- int pos[3000001];
- vector<int> ans;
- queue<int> free_pos;
- bitset<32000000> used;
- char res[32000000];
- // 32000000
- void Pop(int i) {
- ans.push_back(i + 1);
- }
- void Push() {
- ans.push_back(0);
- }
- int main() {
- ios_base::sync_with_stdio(false);
- ofstream cout("output.bin", std::ios::binary | std::ios::out);
- ifstream cin("input.bin", std::ios::binary | std::ios::in);
- cout.tie(nullptr);
- cin.tie(nullptr);
- int n = 0;
- int maxx = 0;
- cin.read((char*)&n, 4);
- for (int i = 0; i < n; ++i) {
- cin.read((char*)(arr + i), 4);
- maxx = max(arr[i], maxx);
- }
- memset(pos, -1, sizeof(pos));
- for (int i = 0; i < n; ++i) {
- free_pos.push(i);
- if (arr[i] == 0) {
- continue;
- }
- if (arr[i] < i + 1) {
- int minus1 = -1;
- cout.write((char*)&minus1, 4);
- cin.close();
- cout.close();
- return 0;
- }
- pos[arr[i]] = i;
- }
- ans.reserve(3 * n);
- int last = 0;
- for (int x = 1; x <= maxx; ++x) {
- if (pos[x] == -1) {
- if (last == n) {
- while (used[free_pos.front()]) free_pos.pop();
- Pop(free_pos.front());
- --last;
- }
- Push();
- } else {
- if (pos[x] > last) {
- int minus1 = -1;
- cout.write((char*)&minus1, 4);
- cin.close();
- cout.close();
- return 0;
- }
- if (pos[x] < last) {
- Pop(pos[x]);
- --last;
- }
- Push();
- used[pos[x]] = true;
- }
- ++last;
- }
- for (int i = 0; i < last; ++i) {
- if (arr[i] == 0) {
- Pop(i);
- }
- }
- int sz = ans.size();
- char* ptr = (char*)&sz;
- for (int i = 0; i < 4; ++i) {
- res[i] = ptr[i];
- }
- int cur = 4;
- for (int x : ans) {
- ptr = (char*)&x;
- for (int i = 0; i < 4; ++i) {
- res[cur++] = ptr[i];
- }
- }
- cout.write(res, cur);
- cin.close();
- cout.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement