这里只需要从一半开始枚举就可以了
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAXN 101
#define MAXV 50001
int v[MAXN], dp[MAXV];
int dynamic_programming(const int &n, const int &max_v)
{
memset(dp, 0, sizeof(int)*(max_v>>1)+1); dp[0] = 1;
for(int i = 0; i < n; i ++) {
for(int j = max_v>>1; j >= v[i]; j --) {
if( dp[j-v[i]] ) {
dp[j] = 1;
}
}
}
for(int i = max_v>>1; i >= 0; i --) {
if( dp[i] ) {
return (i+i > max_v)? i+i-max_v : max_v-i-i;
}
}
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin);
#endif
int n, max_v, m;
scanf("%d", &n);
for( ; n; n --) {
scanf("%d", &m); max_v = 0;
for(int i = 0; i < m; i ++) {
scanf("%d", &v[i]); max_v += v[i];
}
printf("%d\n", dynamic_programming(m, max_v));
}
return 0;
}
uva_562 Dividing coins
最新推荐文章于 2021-02-24 15:52:41 发布