foj 1689

http://acm.fzu.edu.cn/problem.php?pid=1689

dp[j]表示杀了j个怪以后剩下p的最大值;

接下来我们不断枚举每一种怪杀掉的数目;得:

dp[j]=max{dp[j],dp[j-k]-tmp};

k就是某一种怪被杀掉的数目,tmp就是杀掉该些怪花费的p值;

代码:

#include<iostream>
#include<algorithm>
using namespace std;
const int N = 605 ;
int   n,m,p;
int   base[N],num[N],data[N][N],dp[N],flag[N]; 
int main()
{
      while(scanf("%d%d%d",&n,&m,&p)!=EOF)
      {
         for(int i=1;i<=m;i++) 
                scanf("%d",&base[i]);
         int egy,cc;
         memset(num,0,sizeof(num));
         for(int i=0;i<n;i++)
         {
              scanf("%d%d",&egy,&cc);
              data[cc][num[cc]++]=egy;       
         }                 
         for(int i=1;i<=m;i++)
              sort(data[i],data[i]+num[i]);
         memset(dp,0,sizeof(dp));
         memset(flag,0,sizeof(flag));
         dp[0]=p,flag[0]=1;
         for(int i=1;i<=m;i++)
         {
               for(int j=n;j>0;j--)
               {
                         int tmp=base[i];
                         for(int k=1;k<=num[i];k++)
                         {
                                  if(j-k<0) break;
                                  tmp+=data[i][k-1];
                                  if(flag[j-k]&&dp[j-k]>=tmp)
                                  {
                                         if(dp[j]<dp[j-k]-tmp)
                                                  dp[j]=dp[j-k]-tmp;
                                         flag[j]=1;                      
                                                           
                                  }                       
                         }
               }
         }
         int j;
         for(j=n;j>0;j--)
             if(flag[j]) break;
         printf("%d/n",j);     
      }                     
      return 0;
}

你可能感兴趣的:(foj 1689)