Light OJ 1147 Tug of War (背包+状压)
2016-03-30 15:37
288 查看
参考:点击打开链接
: [code]#include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long LL; const int maxn = 2e5+5; int n,m,a[105]; LL dp[maxn]; int mabs(int x){ return x<0?-x:x; } int main(){ int i,j,cas; scanf("%d",&cas); for(int T=1;T<=cas;T++){ scanf("%d",&n); m = 0; memset(dp,0,sizeof(dp)); for(i = 1;i <= n;i++){ scanf("%d",&a[i]); m += a[i]; } LL mask = (1LL<<(n/2+2))-1; dp[0] = 1; for(i = 1;i <= n;i++){ for(j = m/2;j >= a[i];j--){ dp[j] |= (dp[j-a[i]]<<1)&mask; } } int cnt;bool flag; for(j = m/2;j>=0;j--){ cnt = 0;flag = false; while(dp[j]){ if((dp[j]&1)&&(mabs(n-2*cnt)==1||mabs(n-2*cnt)==0)){ flag = true; } cnt++; dp[j]>>=1; } if(flag) break; } if(j > m-j) j = m-j; printf("Case %d: %d %d\n",T,j,m-j); } return 0; }
相关文章推荐
- python安装扩展”unable to find vcvarsall.bat“的解决办法
- JavaSe基础(11)-- static
- httpClient PostMethod
- 软件测试第三次作业
- 微信公众号开发
- 个人
- Nagle算法
- android shape的使用
- sublime 自动添加兼容前缀插件autoprefixer
- JAVA 发送是手机短信,一般是手机验证码较多
- SQL注入问题
- Google带WiFi的热气球或首次商用
- chrome控制台
- 【C语言】 单链表的实现 用malloc实现的动态内存管理
- SQL四种语言:DDL,DML,DCL,TCL
- FAQ02963]如何拦截来电,即在电话ring之前就有机会检查来电号码?
- 【poj 1080】Human Gene Functions 题意&题解&代码(C++)
- 【poj 1080】Human Gene Functions DP(类最长公共子序列*)
- JavaSe基础(10)-- final
- 顺序查找