跳到正文

个人记录

CF730J 题解

cyrel_awa约 4 分钟
原文
文章信息与版本

文章信息

发布
2025年9月6日
收录
2025年12月12日
更新
2026年11月21日
篇幅
约 4 分钟
获赞
1

版本

  1. v2当前
  2. v1

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

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

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

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

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

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

要滚动数组。

C++
#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;
}
原文