P3040 [USACO12JAN]贝尔分享Bale Share

本文介绍了一种使用深度优先搜索(DFS)结合最优性剪枝策略解决将一组数字划分为三个子集,使得三个子集的最大值最小化的算法问题。通过预先排序和剪枝减少搜索空间,提高了算法效率。

想了一个二分 + 状压的思路, 嫌太麻烦了, 直接一个dfs + 最优性剪枝水了过去.
貌似比某些dp还快?

#include <cstdio>
#include <cstring>
#include <cassert>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 20 + 2;
inline int read(){
    char ch = getchar(); int x = 0;
    while(!isdigit(ch)) ch = getchar();
    while(isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
    return x;
}

int N;
int s[MAXN], sum;

int ans = (1 << 30);
void dfs(int u, int suma, int sumb, int sumc) {
    if(u == N + 1) return ans = min(ans, max(suma, max(sumb, sumc))), void();
    if(suma + s[u] < ans) dfs(u + 1, suma + s[u], sumb, sumc);
    if(sumb + s[u] < ans) dfs(u + 1, suma, sumb + s[u], sumc);
    if(sumc + s[u] < ans) dfs(u + 1, suma, sumb, sumc + s[u]);
    return ;
}

int main(){
    cin>>N;
    for(int i = 1; i <= N; i++) sum += (s[i] = read());
    sort(s + 1, s + N + 1, greater<int>());
    dfs(1, 0, 0, 0);
    printf("%d\n", ans);
    return 0;
}

转载于:https://www.cnblogs.com/wsmrxc/p/9829253.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值