分析:其实初始音量给出,并且每首歌要调节的音量也给出后,每首歌对应的音量的个数也就确定了
例如样例:
//第一次调节 只能从 5开始 调小的话就是0 调大的话就是10
//第二次调节 先分析0 调小不可以 调大只能是3 再分析10 调小是7 调大不可以
//第三次调节 先分析3 调小不可以 调大是10 再分析7 调小是0 调大是不可以
所以只需要记录每首歌能到达的音量即可,即dp[i][j]表示第i首个能否到达音量j,见代码
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<cmath>
#include<string>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
ll n,str,maxn;
ll dp[100][1000];
int main(){
cin>>n>>str>>maxn;
ll c;
dp[0][str]=1;
for(ll i=1;i<=n;i++){
cin>>c;
for(ll j=0;j<=maxn;j++){
if(dp[i-1][j]){//第i首个是由第i-1首歌能达到的音量推出来的
if(j-c>=0){//调小
dp[i][j-c]=1;
}
if(j+c<=maxn){//调大
dp[i][j+c]=1;
}
}
}
}
ll max_s=-1;//记录最大的音量
for(ll i=maxn;i>=0;i--){
if(dp[n][i]){
max_s=i;
break;
}
}
cout<<max_s<<endl;
return 0;
}
// /\ | / |**、
// / \ | / | \
// / \ |/ | / _____ ____ | /
// /------\ |\ |__/ / \ \ /\ / / \ | /
// / \ | \ | / \ \ / \ / /______\ |/
// / \ | \ | \ / \ / \ / \ |
// / \ | \ | \_____/ \/ \/ \_____ |
/**
* ┏┓ ┏┓
* ┏┛┗━━━━━━━┛┗━━━┓
* ┃ ┃
* ┃ ━ ┃
* ┃ > < ┃
* ┃ ┃
* ┃... ⌒ ... ┃
* ┃ ┃
* ┗━┓ ┏━┛
* ┃ ┃ Code is far away from bug with the animal protecting
* ┃ ┃ 神兽保佑,代码无bug
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
*/
// warm heart, wagging tail,and a smile just for you!
//
// _ooOoo_
// o8888888o
// 88" . "88
// (| -_- |)
// O\ = /O
// ____/`---'\____
// .' \| |// `.
// / \||| : |||// \
// / _||||| -:- |||||- \
// | | \\ - /// | |
// | \_| ''\---/'' | |
// \ .-\__ `-` ___/-. /
// ___`. .' /--.--\ `. . __
// ."" '< `.___\_<|>_/___.' >'"".
// | | : `- \`.;`\ _ /`;.`/ - ` : | |
// \ \ `-. \_ __\ /__ _/ .-` / /
// ======`-.____`-.___\_____/___.-`____.-'======
// `=---='
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//