Skip to content

Commit fea6373

Browse files
authored
[20250825] BOJ / P4 / 가희의 수열놀이 (Large) / 권혁준
1 parent a87c182 commit fea6373

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
```cpp
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
int Q, MOD;
6+
vector<int> pos[1000001]{};
7+
stack<int> st;
8+
set<int> s;
9+
10+
int main(){
11+
cin.tie(0)->sync_with_stdio(0);
12+
13+
cin>>Q>>MOD;
14+
if(MOD > Q) {
15+
for(int o,a;Q--;) {
16+
cin>>o;
17+
if(o == 1) cin>>a;
18+
if(o == 3) cout<<"-1\n";
19+
}
20+
return 0;
21+
}
22+
23+
for(int i=0,o,a;i<Q;i++) {
24+
cin>>o;
25+
if(o == 1) {
26+
cin>>a;
27+
a %= MOD;
28+
if(!pos[a].empty()) s.erase(pos[a].back());
29+
s.insert(st.size());
30+
pos[a].push_back(st.size());
31+
st.push(a);
32+
}
33+
else if(o == 2) {
34+
if(st.empty()) continue;
35+
a = st.top(); st.pop();
36+
s.erase(pos[a].back());
37+
pos[a].pop_back();
38+
if(!pos[a].empty()) s.insert(pos[a].back());
39+
}
40+
else {
41+
if(s.size() != MOD) cout<<"-1\n";
42+
else cout<<st.size()-(*s.begin())<<'\n';
43+
}
44+
}
45+
46+
}
47+
```

0 commit comments

Comments
 (0)