abc204D 双烤箱烹饪的最短时间

需要烹饪n道菜,第i道菜耗时t[i],有两个相同的烤箱,一个烤箱同一时间只能烹饪同一道菜。求烹饪完所有菜所需的最短时间?
1<=n<=100, 1<=t[i]<=1000

01背包变形,需要选出部分菜用烤箱1,其他用烤箱2,两者的最大值为结果,取所有可能的结果的最小值,即为答案。这里dp[i]表示总耗时为i的方案是否可行。

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,a,b) for(int i=a; i<=b; i++)
#define per(i,a,b) for(int i=b; i>=a; i--)

const int N = 100000;
int n, a[101], dp[N+1], tot;
void solve() {
    cin >> n;
    rep(i,1,n) cin >> a[i], tot += a[i];
    dp[0] = 1;
    rep(i,1,n) per(j,a[i],N) {
        dp[j] |= dp[j-a[i]];
    }
    int ans = 1E8;
    rep(i,1,N) if (dp[i]) ans = min(ans, max(i, tot-i));
    cout << ans << "\n";
}

signed main() {
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    while (t--) solve();
    return 0;
}
posted @ 2024-03-12 21:02  chenfy27  阅读(3)  评论(0编辑  收藏  举报