[HAOI2012]音量调节
题目地址:
基本思路:
比较简单的,用表示到第首歌时音量为的情况是否存在.
转移方程如下:
参考代码:
/* Author: sunsetcolors */ #pragma GCC optimize(2) #pragma GCC optimize(3) #include <bits/stdc++.h> using namespace std; #define IO std::ios::sync_with_stdio(false); cin.tie(0) #define ll long long #define ull unsigned long long #define SZ(x) ((int)(x).size()) #define all(x) (x).begin(), (x).end() #define rep(i, l, r) for (int (i) = l; (i) <= (r); ++(i)) #define per(i, l, r) for (int (i) = l; (i) >= (r); --(i)) #define mset(s, _) memset(s, _, sizeof(s)) #define pb push_back #define pii pair <int, int> #define mp(a, b) make_pair(a, b) #define debug(x) cerr << #x << " = " << (x) << '\n'; #define pll pair <ll, ll> #define fir firsts #define sec second #define INF 0x3f3f3f3f #define int ll template<typename T1, typename T2> int gcd(T1 a, T2 b) { return b == 0 ? a : gcd(b,a % b); } template<typename T1, typename T2> void ckmin(T1 &a, T2 b) { if (a > b) a = b; } template<typename T1, typename T2> void ckmax(T1 &a, T2 b) { if (a < b) a = b; } inline int read() { int x = 0, f = 0; char ch = getchar(); while (!isdigit(ch)) f |= ch == '-', ch = getchar(); while (isdigit(ch)) x = 10 * x + ch - '0', ch = getchar(); return f ? -x : x; } template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x >= 10) print(x / 10); putchar(x % 10 + '0'); } template<typename T> inline void print(T x, char let) { print(x), putchar(let); } const int maxn = 55; int n,s,t,a[maxn]; bool dp[maxn][1010]; signed main() { IO; cin >> n >> s >> t; rep(i,1,n) cin >> a[i]; dp[0][s] = true; rep(i,1,n) rep(j,0,t) { if (j + a[i] <= t) dp[i][j + a[i]] |= dp[i - 1][j]; if (j - a[i] >= 0) dp[i][j - a[i]] |= dp[i - 1][j]; } int ans = -1; per(j,t,0) if(dp[n][j]) { ans = j; break; } cout << ans << '\n'; return 0; }