CF730J 题解

· · 个人记录

第一问很显然直接贪心,按 b_i 从大到小排序,统计个 sum,到了 break 掉。

对于第二问,问题答案就是没选中的瓶子里的水之和最小值,也就是求选中水的最大值,考虑 dp。

显然阶段为考虑到的瓶子数,状态为选的个数 c,选的 a_i 和,选的 b_i 和,可以设计 dp_{i,c,j,k},而 c 越小越好,a_i 越大越好,b_i 越小越好,于是可以把 a_i 扔到 dp 值里,设计状态 dp_{i,c,j}。

dp 方程:如果选的话,dp_{i,c,j}=dp_{i-1,c-1,j-b_i}+a_i

如果不选,dp_{i,c,j}=dp_{i-1,c,j}

答案需要 j 大于 a_i 的和,初始化负无穷,dp_{0,0,0}=0。

要滚动数组。

#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;
}