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


stdout
6