Advertisement
amine99

Untitled

May 25th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define io ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  4. #define ALL(x) x.begin(),x.end()
  5. #define SZ(x) x.size()
  6. #define PB push_back
  7. #define MP make_pair
  8. #define F first
  9. #define S second
  10. typedef long long LL;
  11. typedef vector<int> VI;
  12. typedef pair<int,int> PI;
  13.  
  14. int n,a[5000],ans,Z[5000][2];
  15.  
  16. int solve() {
  17.    Z[0][0] = 1;
  18.    Z[0][1] = 1;
  19.    int cnt = 0;
  20.    for(int i = 1; i < n; i++) {
  21.       for(int j = i-1; j >= 0; j--){
  22.          if(a[j] <= a[i]) Z[i][0] = max(Z[j][1]+1,Z[i][0]);
  23.          if(a[j] >= a[i]) Z[i][1] = max(Z[j][0]+1,Z[i][1]);
  24.       }
  25.       cnt = max(cnt,max(Z[i][0],Z[i][1]));
  26.    }
  27.    return cnt;
  28. }
  29.  
  30. int main() {
  31.    io
  32.    cin >> n;
  33.    for(int i = 0; i < n; i++)
  34.       cin >> a[i];
  35.    cout << solve();
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement