P3040 [USACO12JAN]贝尔分享Bale Share

Posted wsmrxc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了P3040 [USACO12JAN]贝尔分享Bale Share相关的知识,希望对你有一定的参考价值。

想了一个二分 + 状压的思路, 嫌太麻烦了, 直接一个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
", ans);
    return 0;
}

以上是关于P3040 [USACO12JAN]贝尔分享Bale Share的主要内容,如果未能解决你的问题,请参考以下文章

P1561 [USACO12JAN]爬山Mountain Climbing

洛谷P3043 [USACO12JAN]牛联盟Bovine Alliance

P3043 [USACO12JAN]牛联盟Bovine Alliance(并查集)

洛谷—— P1561 [USACO12JAN]爬山Mountain Climbing

洛谷 P3041 [USACO12JAN] Video Game Combos

P3041 [USACO12JAN]视频游戏的连击Video Game Combos