做法:dp
思路:
因为音量范围在0~maxx之间并且maxx<=1000,所以我们设dp[i][j]为第i次音量变化后此时音量为j的方案是否存在
在符合条件的音量内进行转移
和
代码
// Problem: [HAOI2012]音量调节 // Contest: NowCoder // URL: https://ac.nowcoder.com/acm/problem/19990 // Memory Limit: 524288 MB // Time Limit: 2000 ms // // Powered by CP Editor (https://cpeditor.org) #include <bits/stdc++.h> using namespace std; #define pb push_back #define mp(aa,bb) make_pair(aa,bb) #define _for(i,b) for(int i=(0);i<(b);i++) #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define per(i,b,a) for(int i=(b);i>=(a);i--) #define mst(abc,bca) memset(abc,bca,sizeof abc) #define X first #define Y second #define lowbit(a) (a&(-a)) #define debug(a) cout<<#a<<":"<<a<<"\n" typedef long long ll; typedef pair<int,int> pii; typedef unsigned long long ull; typedef long double ld; const int N=100010; const int INF=0x3f3f3f3f; const int mod=1e9+7; const double eps=1e-6; const double PI=acos(-1.0); int c[55]; bool dp[55][1005]; void solve(){ mst(dp,0); int n,begin,maxx;cin>>n>>begin>>maxx; rep(i,1,n) cin>>c[i]; dp[0][begin]=1; rep(i,1,n){ rep(j,0,maxx){ if(j-c[i]>=0&&dp[i-1][j-c[i]]) dp[i][j]=1; if(j+c[i]<=maxx&&dp[i-1][j+c[i]]) dp[i][j]=1; } } per(i,maxx,0){ if(dp[n][i]) return cout<<i<<"\n",void(0); } return cout<<"-1\n",void(0); } int main(){ ios::sync_with_stdio(0);cin.tie(0); // int t;cin>>t;while(t--) solve(); return 0; }