Advertisement
Ritam_C

Create the jewellery

Apr 21st, 2021
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. #define pb push_back
  4. #define vll vector<ll>
  5. using namespace std;
  6.  
  7. int main(){
  8.     ios_base::sync_with_stdio(false);
  9.     cin.tie(NULL);
  10.     int n;
  11.     cin >> n;
  12.     vll v;
  13.     for(int i = 0; i < n; i++){
  14.         ll x;
  15.         cin >> x;
  16.         v.pb(x);
  17.     }
  18.     pair<int, int> dp[n];
  19.     int l = 0;
  20.     for(int i = 0; i < n; i++){
  21.         dp[i] = {1, 1};
  22.     }
  23.     for(int i = n-1; i >= 0; i--){
  24.         if(i == n-1){
  25.             dp[i] = {1, 1};
  26.             l = 1;
  27.         } else{
  28.             for(int j = i+1; j < n; j++){
  29.                 if(v[i] > v[j]){
  30.                     if(dp[i].first <= dp[j].first){
  31.                         dp[i].first = 1+dp[j].first;
  32.                     }
  33.                 } else{
  34.                     if(dp[i].second <= dp[j].second){
  35.                         dp[i].second = 1+dp[j].second;
  36.                     }
  37.                 }
  38.             }
  39.             l = max(l, dp[i].first+dp[i].second-1);
  40.         }
  41.     }
  42.     cout << l << "\n";
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement