Advertisement
Guest User

Untitled

a guest
Nov 16th, 2019
4,904
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int borne = (int)(1e6) + 5;
  5. const int WAIT=0, ENTERED=1, LEFT=2;
  6. int state[borne];
  7.  
  8. bool solve()
  9. {
  10.     ios::sync_with_stdio(false);
  11.     int nbEvents;
  12.     cin >> nbEvents;
  13.     vector<int> res, cur;
  14.     int ofs = 0;
  15.  
  16.     for (int ind = 0; ind < nbEvents; ++ind) {
  17.         int ev; cin >> ev;
  18.         int guy = abs(ev);
  19.         cur.push_back(guy);
  20.         if (ev > 0) {
  21.             if (state[guy] != WAIT) return false;
  22.             state[guy] = ENTERED; ++ofs;
  23.         } else {
  24.             if (state[guy] != ENTERED) return false;
  25.             state[guy] = LEFT; --ofs;
  26.         }
  27.         if (ofs == 0) {
  28.             res.push_back(cur.size());
  29.             for (int x : cur) state[x] = WAIT;
  30.             cur.clear();
  31.         }
  32.     }
  33.  
  34.     if (! cur.empty()) return false;
  35.  
  36.     int nbDays = res.size();
  37.     cout << nbDays << "\n";
  38.     for (int i = 0; i < nbDays; ++i) {
  39.         cout << res[i];
  40.         if (i+1 < nbDays) cout << " ";
  41.         else cout << "\n";
  42.     }
  43.     return true;
  44. }
  45.  
  46. int main()
  47. {
  48.     if (!solve()) cout << "-1\n";
  49.     return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement