Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- int t,n,k,s;
- const int maxs=1001;
- int x[maxs];
- int dp[maxs][maxs][11];
- int rec(int at,int movesleft,int pos){
- if(at==s){
- return 0;
- }
- int res=0;
- if(at != -1 and dp[at][movesleft][pos] != -1) {
- return dp[at][movesleft][pos];
- }
- if(at != -1 and pos==x[at]){
- res=max(res,rec(at+1,movesleft,pos)+1);
- if(movesleft>0){
- if(pos>0){
- res=max(res,rec(at+1,movesleft-1,pos-1)+1);
- }
- if(pos<n){
- res=max(res,rec(at+1,movesleft-1,pos+1)+1);
- }
- }
- }
- if(movesleft>0){
- if(pos>0){
- res=max(res,rec(at+1,movesleft-1,pos-1));
- }
- if(pos<n){
- res=max(res,rec(at+1,movesleft-1,pos+1));
- }
- }
- res=max(res,rec(at+1,movesleft,pos));
- if(at != -1) {
- dp[at][movesleft][pos] = res;
- }
- return res;
- }
- int main()
- {
- cin>>t;
- for(int i = 0;i<t;i++){
- memset(dp, -1, sizeof dp);
- cin>>n>>k>>s;
- for(int j = 0;j<s;j++){
- cin>>x[j];
- x[j]--;
- }
- int rez=rec(-1,k, 0);
- cout<<rez<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment