Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <cmath>
- #include <algorithm>
- #include <vector>
- #include <set>
- #include <map>
- #include <stack>
- #include <queue>
- #include <cstdlib>
- #include <cstdio>
- #include <string>
- #include <cstring>
- #include <cassert>
- #include <utility>
- #include <iomanip>
- using namespace std;
- const int MAXN = 105000;
- int n;
- int ans = 0;
- stack <int> st;
- stack <int> num;
- int a[MAXN];
- int main() {
- //assert(freopen("input.txt","r",stdin));
- //assert(freopen("output.txt","w",stdout));
- scanf("%d", &n);
- for (int i = 1; i <= n; i++) {
- scanf("%d", &a[i]);
- }
- for (int i = 1; i <= n; i++) {
- if (st.empty()) {
- st.push(a[i]);
- num.push(1);
- }
- else if (st.top() == a[i]) {
- num.top() += 1;
- if (i + 1 > n || a[i + 1] != a[i]) {
- if (num.top() >= 3) {
- ans += num.top();
- st.pop();
- num.pop();
- }
- }
- }
- else {
- st.push(a[i]);
- num.push(1);
- }
- }
- printf("%d\n", ans);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement