Glass Half Spilled
作者:互联网
\(dp\)
- \(f[k][j]表示选k个杯子,总体积为j的条件下装水的最大值\)
- \(转移方程:f[k][j]=max(f[k][j],f[k-1][j-v[i]]+w[i])\)
- \(注意初始化,很多方案是不合法的\)
- \(对于每一个f[k][j],它的答案是0.5*(sumw-f[k][j]]+f[k][j]),\\ 由于加水可能导致溢出,还要于j取min\)
#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define ull unsigned long long
#define pb push_back
#define PII pair<int, int>
#define x first
#define y second
#define inf 0x3f3f3f3f
const int N = 110, M = 30010;
int v[N], w[N];
int f[N][N * N];
int main() {
IO;
int n;
cin >> n;
int sumv = 0, sumw = 0;
for (int i = 1; i <= n; ++i) {
cin >> v[i] >> w[i];
sumv += v[i];
sumw += w[i];
}
memset(f, -0x3f, sizeof f);
f[0][0] = 0;
for (int i = 1; i <= n; ++i)
for (int k = i; k >= 1; --k)
for (int j = sumv; j >= v[i]; --j)
f[k][j] = max(f[k][j], f[k - 1][j - v[i]] + w[i]);
for (int k = 1; k <= n; ++k) {
double ans = 0;
for (int j = 0; j <= sumv; ++j) ans = max(ans, min(1.0 * j, 0.5 * (sumw + f[k][j])));
cout << fixed << setprecision(10) << ans << ' ';
}
return 0;
}
标签:int,sumv,Spilled,sumw,long,Glass,IO,Half,define 来源: https://www.cnblogs.com/phr2000/p/14433572.html