fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n, move = 0;
  6. cin >> n;
  7. stack<int> ori, aux;
  8. for(int i = 0; i < 2*n; i++) {
  9. int tmp;
  10. cin >> tmp;
  11. ori.push(tmp);
  12. }
  13. while(!ori.empty()) {
  14. move++;
  15. if(!aux.empty() && aux.top() == ori.top()) {
  16. aux.pop();
  17. ori.pop();
  18. continue;
  19. }
  20. aux.push(ori.top());
  21. ori.pop();
  22. }
  23. if(!aux.empty())
  24. cout << "impossible" << endl;
  25. else
  26. cout << move << endl;
  27. return 0;
  28. }
Success #stdin #stdout 0.01s 5280KB
stdin
3
3 2 1 1 3 2
stdout
impossible