hdu 4104
先排序,再动态规划。须要优化
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<map> #include<algorithm> using namespace std; const int maxn = 1e3+10; const int maxm = 1e6+10; int a[maxn]; int f[maxm]; int main() { int N, k, i, j, m; while(scanf("%d", &N)!=EOF) { memset(f, 0, sizeof(f)); for(i = 0; i < N; i ++) scanf("%d", &a[i]); sort(a, a+N); m = 0; f[0] = 1; k = 1; //K是不能组合的最小值。即临时的答案 for(i = 0; i < N; i ++) { m += a[i]; //前i个数之和,组合的最大值 if(k < a[i]) break; //假设此时的a[i]已经比K值大了。那么就再不可以组合得到K了,K即终于答案了 for(int j = m; j >= k; j --) { if(f[j-a[i]] == 1) f[j] = 1; } while(f[k] == 1) k ++; } printf("%d\n", k); } return 0; }