Advertisement
Guest User

Untitled

a guest
Oct 21st, 2014
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cmath>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <set>
  7. #include <map>
  8. #include <stack>
  9. #include <queue>
  10. #include <cstdlib>
  11. #include <cstdio>
  12. #include <string>
  13. #include <cstring>
  14. #include <cassert>
  15. #include <utility>
  16. #include <iomanip>
  17.  
  18. using namespace std;
  19.  
  20. const int MAXN = 105000;
  21.  
  22. int n;
  23. int ans = 0;
  24. stack <int> st;
  25. stack <int> num;
  26. int a[MAXN];
  27.  
  28. int main() {
  29. //assert(freopen("input.txt","r",stdin));
  30. //assert(freopen("output.txt","w",stdout));
  31.  
  32. scanf("%d", &n);
  33. for (int i = 1; i <= n; i++) {
  34. scanf("%d", &a[i]);
  35. }
  36.  
  37. for (int i = 1; i <= n; i++) {
  38. if (st.empty()) {
  39. st.push(a[i]);
  40. num.push(1);
  41. }
  42. else if (st.top() == a[i]) {
  43. num.top() += 1;
  44. if (i + 1 > n || a[i + 1] != a[i]) {
  45. if (num.top() >= 3) {
  46. ans += num.top();
  47. st.pop();
  48. num.pop();
  49. }
  50. }
  51. }
  52. else {
  53. st.push(a[i]);
  54. num.push(1);
  55. }
  56. }
  57.  
  58. printf("%d\n", ans);
  59.  
  60. return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement