UVA - 672 Gangsters(简单dp,正解应该先离散化)

#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 30100;
const int N    = 105;
int d[maxn][105];
int c[maxn][105],t[N],p[N],s[N],n,k,T;
int vis[maxn][105];
int dp(int i,int j){
  if(vis[i][j]) return d[i][j];
  vis[i][j] = true;
  if(i==T+1){
     return d[i][j] = 0;
  }
  d[i][j] = 0;
  if(j>0)d[i][j]=max(d[i][j],dp(i+1,j-1)+c[i][j]);
         d[i][j]=max(d[i][j],dp(i+1,j  )+c[i][j]);
  if(j<k)d[i][j]=max(d[i][j],dp(i+1,j+1)+c[i][j]);
  return d[i][j];
}
int main()
{
    int Case;
    scanf("%d",&Case);
    while(Case--){
         scanf("%d %d %d",&n,&k,&T);
         for(int i=0;i<=T+1;i++)
            for(int j=0;j<=k;j++)
                vis[i][j] = c[i][j] = 0;
         for(int i=1;i<=n;i++)
             scanf("%d",&t[i]);
         for(int i=1;i<=n;i++)
             scanf("%d",&p[i]);
         for(int i=1;i<=n;i++){
             scanf("%d",&s[i]);
             c[t[i]][s[i]]+=p[i];
         }
         printf("%d\n",dp(0,0));
         if(Case) printf("\n");
    }
    return 0;
}

你可能感兴趣的:(UVA - 672 Gangsters(简单dp,正解应该先离散化))