Josif_tepe

Untitled

Jul 23rd, 2025
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4. int t,n,k,s;
  5. const int maxs=1001;
  6. int x[maxs];
  7. int dp[maxs][maxs][11];
  8. int rec(int at,int movesleft,int pos){
  9.  if(at==s){
  10.     return 0;
  11.  }
  12.  int res=0;
  13.     if(at != -1 and dp[at][movesleft][pos] != -1) {
  14.         return dp[at][movesleft][pos];
  15.     }
  16.  if(at != -1 and pos==x[at]){
  17.     res=max(res,rec(at+1,movesleft,pos)+1);
  18.     if(movesleft>0){
  19.         if(pos>0){
  20.            res=max(res,rec(at+1,movesleft-1,pos-1)+1);
  21.         }
  22.         if(pos<n){
  23.            res=max(res,rec(at+1,movesleft-1,pos+1)+1);
  24.         }
  25.     }
  26.  }
  27.    
  28.     if(movesleft>0){
  29.         if(pos>0){
  30.            res=max(res,rec(at+1,movesleft-1,pos-1));
  31.         }
  32.         if(pos<n){
  33.            res=max(res,rec(at+1,movesleft-1,pos+1));
  34.         }
  35.    
  36.  }
  37.     res=max(res,rec(at+1,movesleft,pos));
  38.     if(at != -1) {
  39.         dp[at][movesleft][pos] = res;
  40.     }
  41.     return res;
  42. }
  43.  
  44. int main()
  45. {
  46.     cin>>t;
  47.     for(int i = 0;i<t;i++){
  48.         memset(dp, -1, sizeof dp);
  49.         cin>>n>>k>>s;
  50.         for(int j = 0;j<s;j++){
  51.           cin>>x[j];
  52.             x[j]--;
  53.         }
  54.         int rez=rec(-1,k, 0);
  55.         cout<<rez<<endl;
  56.     }
  57.  
  58.     return 0;
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment