BZOJ2748(DP)
非常简单的DP题。类似背包的操作,按照音量改变值进行状态转移即可。
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define REP(i,n) for(int i(0); i < (n); ++i) 6 #define rep(i,a,b) for(int i(a); i <= (b); ++i) 7 #define dec(i,a,b) for(int i(a); i >= (b); --i) 8 #define for_edge(i,x) for(int i = H[x]; i; i = X[i]) 9 10 #define LL long long 11 #define ULL unsigned long long 12 #define MP make_pair 13 #define PB push_back 14 #define FI first 15 #define SE second 16 #define INF 1 << 30 17 18 const int N = 100000 + 10; 19 const int M = 10000 + 10; 20 const int Q = 1000 + 10; 21 const int A = 30 + 1; 22 23 int a[N]; 24 int f[A << 1][Q]; 25 int n, init, up; 26 int x, ans; 27 28 int main(){ 29 #ifndef ONLINE_JUDGE 30 freopen("test.txt", "r", stdin); 31 freopen("test.out", "w", stdout); 32 #endif 33 34 memset(f, 0, sizeof f); 35 scanf("%d%d%d", &n, &init, &up); 36 rep(i, 1, n) scanf("%d", a + i); 37 f[0][init] = 1; 38 rep(i, 1, 50){ 39 rep(j, 0, up){ 40 x = j + a[i]; 41 if (x >= 0 && x <= up) f[i][j] |= f[i - 1][x]; 42 x = j - a[i]; 43 if (x >= 0 && x <= up) f[i][j] |= f[i - 1][x]; 44 } 45 } 46 47 ans = -1; 48 dec(i, up, 0){ 49 if (f[n][i]){ 50 ans = i; 51 break; 52 } 53 } 54 /* 55 rep(i, 0, n){ 56 rep(j, 0, up) printf("%d", f[i][j]); 57 putchar(10); 58 } 59 */ 60 printf("%d\n", ans); 61 62 return 0; 63 64 }