tungggg

Count_LIS_DP_LastIsINFINITE

Feb 12th, 2022 (edited)
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. using namespace std;
  4.  
  5. int main(){
  6.     int n;
  7.     cin>>n;
  8.     int *a = new int [n+2];
  9.     a[0]=INT_MIN;
  10.     a[n+1]=INT_MAX;
  11.     for(int i=1;i<=n;i++) cin>>a[i];
  12.     int dp[n+2]={0};
  13.     dp[0]=1; dp[1]=1;
  14.     int F[n+2]={0};
  15.     int vet[n+2];
  16.     vet[1]=0;
  17.     F[0]=1; F[1]=2;
  18.     for(int i=2;i<=n+1;i++){
  19.         for(int j=0;j<i;j++){
  20.             if (a[j]<a[i]){
  21.                 if(F[i]<F[j]+1){
  22.                     F[i]=F[j]+1;
  23.                     vet[i]=j;
  24.                     dp[i]=dp[j];
  25.                 }
  26.                 else if (F[i]==F[j]+1){
  27.                     dp[i]=dp[i]+dp[j];
  28.                 }
  29.             }
  30.         }
  31.     }
  32.     vector < int > res;
  33.     cout<<F[n+1]-2<<endl;
  34.     int i= vet[n+1];
  35.     while(i >= 0){
  36.         if (i==0){
  37.             res.push_back(a[i]);
  38.             break;
  39.         }
  40.         else {
  41.             res.push_back(a[i]);
  42.             i=vet[i];
  43.         }
  44.     }
  45.     for(int i=res.size()-1;i>=0;i--){
  46.         if (res[i]!=INT_MAX  && res[i]!= INT_MIN){
  47.             cout<<res[i]<<" ";
  48.         }
  49.     }
  50.     cout<<endl<<"Co tat ca "<< dp[n+1]<< " day con tang dai nhat khong lien tiep "<<endl;
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment