-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKattis_pairingsocks.cpp
More file actions
36 lines (28 loc) · 887 Bytes
/
Kattis_pairingsocks.cpp
File metadata and controls
36 lines (28 loc) · 887 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <bits/stdc++.h> // here we have all the STL we need, including istringstream and ostringstream
#define ALL(x) x.begin(), x.end()
#define FAST std::cin.tie(0); ios::sync_with_stdio(false); std::cout.tie(0);
using namespace std;
#define min(a,b) (a < b) ? (a) : (b)
#define max(a,b) (a > b) ? (a) : (b)
#define vii vector<pair<int,int>>
int main() {
FAST
int n;
cin >> n;
int k = 2*n;
queue<int> pile1;
for (int i = 0; i < k; i++) {
int num;
cin >> num;
pile1.push(num);
}
int ans = 0;
stack<int> pile2;
while (!pile1.empty()) {
ans++;
if (!pile2.empty() && pile2.top() == pile1.front()) { pile1.pop(); pile2.pop(); }
else { pile2.push(pile1.front()); pile1.pop(); }
}
if (pile1.empty() && pile2.empty()) cout << ans << endl;
else cout << "impossible" << endl;
}