Advertisement
999ms

E10

Oct 23rd, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int arr[3000000];
  5. int pos[3000001];
  6. vector<int> ans;
  7. queue<int> free_pos;
  8. bitset<32000000> used;
  9. char res[32000000];
  10. // 32000000
  11. void Pop(int i) {
  12.     ans.push_back(i + 1);
  13. }
  14.  
  15. void Push() {
  16.     ans.push_back(0);
  17. }
  18.  
  19. int main() {
  20.     ios_base::sync_with_stdio(false);
  21.     ofstream cout("output.bin", std::ios::binary | std::ios::out);
  22.     ifstream cin("input.bin", std::ios::binary | std::ios::in);
  23.     cout.tie(nullptr);
  24.     cin.tie(nullptr);
  25.     int n = 0;
  26.     int maxx = 0;
  27.     cin.read((char*)&n, 4);
  28.     for (int i = 0; i < n; ++i) {
  29.         cin.read((char*)(arr + i), 4);
  30.         maxx = max(arr[i], maxx);
  31.     }
  32.     memset(pos, -1, sizeof(pos));
  33.     for (int i = 0; i < n; ++i) {
  34.         free_pos.push(i);
  35.         if (arr[i] == 0) {
  36.             continue;
  37.         }
  38.         if (arr[i] < i + 1) {
  39.             int minus1 = -1;
  40.             cout.write((char*)&minus1, 4);
  41.             cin.close();
  42.             cout.close();
  43.             return 0;
  44.         }
  45.         pos[arr[i]] = i;
  46.     }
  47.     ans.reserve(3 * n);
  48.     int last = 0;
  49.     for (int x = 1; x <= maxx; ++x) {
  50.         if (pos[x] == -1) {
  51.             if (last == n) {
  52.                 while (used[free_pos.front()]) free_pos.pop();
  53.                 Pop(free_pos.front());
  54.                 --last;
  55.             }
  56.             Push();
  57.         } else {
  58.             if (pos[x] > last) {
  59.                 int minus1 = -1;
  60.                 cout.write((char*)&minus1, 4);
  61.                 cin.close();
  62.                 cout.close();
  63.                 return 0;
  64.             }
  65.             if (pos[x] < last) {
  66.                 Pop(pos[x]);
  67.                 --last;
  68.             }
  69.             Push();
  70.             used[pos[x]] = true;
  71.         }
  72.         ++last;
  73.     }
  74.     for (int i = 0; i < last; ++i) {
  75.         if (arr[i] == 0) {
  76.             Pop(i);
  77.         }
  78.     }
  79.     int sz = ans.size();
  80.     char* ptr = (char*)&sz;
  81.     for (int i = 0; i < 4; ++i) {
  82.         res[i] = ptr[i];
  83.     }
  84.     int cur = 4;
  85.     for (int x : ans) {
  86.         ptr = (char*)&x;
  87.         for (int i = 0; i < 4; ++i) {
  88.             res[cur++] = ptr[i];
  89.         }
  90.     }
  91.     cout.write(res, cur);
  92.     cin.close();
  93.     cout.close();
  94.     return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement