# BZOJ 2748: [HAOI2012]音量调节 (dp)

Posted by 111qqz on Sunday, April 2, 2017

## 2748: [HAOI2012]音量调节

Time Limit: 3 Sec  Memory Limit: 128 MB
Submit: 1814  Solved: 1148
[Submit][Status][Discuss]

3 5 10
5 3 7

10

## HINT

1<=N<=50,1<=Ci<=Maxlevel 1<=maxlevel<=1000

0<=beginlevel<=maxlevel

dp[i][j]表示经过i次调整后能否达到音量j.

``````/* ***********************************************
Author :111qqz
Created Time :2017年04月02日 星期日 14时36分58秒
File Name :code/bzoj/2748.cpp
************************************************ */

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#define PB push_back
#define fst first
#define sec second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ms(a,x) memset(a,x,sizeof(a))
typedef long long LL;
#define pi pair < int ,int >
#define MP make_pair

using namespace std;
const double eps = 1E-8;
const int dx4[4]={1,0,0,-1};
const int dy4[4]={0,-1,1,0};
const int inf = 0x3f3f3f3f;
bool dp[51][1005]; //dp[i][j]表示经过i次操作,能否达到音量j
int n,L,R;
int a[1005];
int main()
{
#ifndef  ONLINE_JUDGE
freopen("code/in.txt","r",stdin);
#endif
ms(dp,0);
cin>>n>>L>>R;
for ( int i = 1 ; i <= n ; i++ ) cin>>a[i];
dp[0][L] = true;
for ( int i = 1 ; i <= n ; i++)
{
for (int j = 0 ; j <= R ; j++)
{
if (dp[i-1][j])
{
if (j-a[i]>=0) dp[i][j-a[i]] = true;
if (j+a[i]<=R) dp[i][j+a[i]] = true;
}
}
}
int ans = -1 ; //无解输出-1.
for ( int i = 0 ; i <= R ; i++)if (dp[n][i]) ans = max(i,ans);
cout<<ans<<endl;
#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
return 0;
}
``````

「真诚赞赏，手留余香」