BZOJ2748(DP)

非常简单的DP题。类似背包的操作,按照音量改变值进行状态转移即可。

#include 

using namespace std;

#define REP(i,n)                for(int i(0); i <  (n); ++i)
#define rep(i,a,b)              for(int i(a); i <= (b); ++i)
#define dec(i,a,b)              for(int i(a); i >= (b); --i)
#define for_edge(i,x)           for(int i = H[x]; i; i = X[i])

#define LL      long long
#define ULL     unsigned long long
#define MP      make_pair
#define PB      push_back
#define FI      first
#define SE      second
#define INF     1 << 30

const int N     =    100000      +       10;
const int M     =    10000       +       10;
const int Q     =    1000        +       10;
const int A     =    30          +       1;

int a[N];
int f[A << 1][Q];
int n, init, up;
int x, ans;

int main(){
#ifndef ONLINE_JUDGE
	freopen("test.txt", "r", stdin);
	freopen("test.out", "w", stdout);
#endif

	memset(f, 0, sizeof f);
	scanf("%d%d%d", &n, &init, &up);
	rep(i, 1, n) scanf("%d", a + i);
	f[0][init] = 1;
	rep(i, 1, 50){
		rep(j, 0, up){
			x = j + a[i];
			if (x >= 0 && x <= up) f[i][j] |= f[i - 1][x];
			x = j - a[i];
			if (x >= 0 && x <= up) f[i][j] |= f[i - 1][x];
		}
	}

	ans = -1;
	dec(i, up, 0){
		if (f[n][i]){
			ans = i;
			break;
		}
	}
/*
	rep(i, 0, n){
		rep(j, 0, up) printf("%d", f[i][j]);
		putchar(10);
	}
*/
	printf("%d\n", ans);

	return 0;

}




转载于:https://www.cnblogs.com/cxhscst2/p/6648802.html

你可能感兴趣的:(BZOJ2748(DP))