CF730J 题解
第一问很显然直接贪心,按
对于第二问,问题答案就是没选中的瓶子里的水之和最小值,也就是求选中水的最大值,考虑 dp。
显然阶段为考虑到的瓶子数,状态为选的个数
dp 方程:如果选的话,
如果不选,
答案需要
要滚动数组。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <cctype>
#include <string>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <cctype>
#include <chrono>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#define int ll
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int MOD = 998244353;
const int mod = 1e9 + 7;
struct node{
int x, y;
bool operator<(const node & o) const {
return y > o.y;
}
} a[105];
int n, sma, smb, dp[2][105][10005], cnt, ans, now;
signed main(){
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i].x, sma += a[i].x;
for (int i = 1; i <= n; i++) cin >> a[i].y;
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; i++){
smb += a[i].y;
if (smb >= sma && cnt == 0) cnt = i;
}
memset(dp, -0x3f, sizeof(dp));
dp[1][0][0] = 0;
for (int i = 1; i <= n; i++){
for (int j = 0; j <= i; j++){
for (int s = 0; s <= smb; s++){
if (j && s >= a[i].y) dp[now][j][s] = max(dp[now ^ 1][j][s], dp[now ^ 1][j - 1][s - a[i].y] + a[i].x);
else dp[now][j][s] = dp[now ^ 1][j][s];
}
}
now ^= 1;
}
for (int i = sma; i <= smb; i++) ans = max(ans, dp[now ^ 1][cnt][i]);
cout << cnt << " " << sma - ans;
return 0;
}