【题解】 bzoj2748 [HAOI2012]音量调节 (动态规划)

懒得复制,戳我戳我

Solution:

  • 傻逼题目,直接dp就可以了,他是求最后一次的最大值

Code:

//It is coded by Ning_Mew on 4.17
#include<bits/stdc++.h>
using namespace std;

const int maxn=55,maxl=1000+7;

int n,sl,xl,ans=-1;
int c,dp[maxn][maxl];

int main(){
  freopen("changingsounds.in","r",stdin);
  freopen("changingsounds.out","w",stdout);
  scanf("%d%d%d",&n,&sl,&xl);
  memset(dp,-1,sizeof(dp));
  scanf("%d",&c);
  if(sl+c<=xl)dp[1][sl+c]=1;//,ans=max(ans,sl+c);
  if(sl-c>=0)dp[1][sl-c]=1;//,ans=max(ans,sl-c);
  for(int i=2;i<=n;i++){
    scanf("%d",&c);
    //cout<<"---------"<<i<<endl;
    for(int j=0;j<=xl;j++){
      if(j-c>=0)dp[i][j]=max(dp[i][j],dp[i-1][j-c]);
      if(j+c<=xl)dp[i][j]=max(dp[i][j],dp[i-1][j+c]);
      if(dp[i][j]==1&&i==n)ans=max(ans,j);
      //cout<<j<<' '<<j-c<<' '<<j+c<<endl;
    }
  }
  printf("%d\n",ans);
  return 0;
}

posted @ 2018-04-29 15:14  Ning_Mew  阅读(158)  评论(0编辑  收藏  举报