Advertisement
Centipede18

ACM ProPTIT: B. Xayah.PROPTIT

Apr 6th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. int main() {
  6.     ios_base::sync_with_stdio(0);
  7.     cin.tie(NULL); cout.tie(NULL);
  8.  
  9.     int n; cin >> n; vector<int> a(n);
  10.     for (int i=0; i<n; i++) cin >> a[i];
  11.  
  12.     int MinIndex = 0, MaxIndex = 0;
  13.     int CurrMin = 0, CurrMax = 0;
  14.  
  15.     for (int i=0; i<n; i++) {
  16.         if (a[i] == 0) {
  17.             CurrMin++; CurrMax++;
  18.             CurrMin = min(CurrMin, 0);
  19.         }
  20.         else {
  21.             CurrMin--; CurrMax--;
  22.             CurrMax = max(CurrMax, 0);
  23.         }
  24.         MinIndex = min(MinIndex, CurrMin);
  25.         MaxIndex = max(MaxIndex, CurrMax);
  26.     }
  27.  
  28.     cout << (MaxIndex - MinIndex + 1);
  29.     return 0;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement