想了一个二分 + 状压的思路, 嫌太麻烦了, 直接一个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
时间: 2024-11-01 18:29:18