HDU 5616 Jam's balance
背包。dp[i]=1表示i这种差值能被组合出来,差值有负数,所以用sum表示0,0表示-sum,2*sum表示sum。
询问X的时候,只需看dp[sum+X]或者dp[sum-X]是否有一个为1,注意RE。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std; int dp[];
int w[];
int flag[]; int main()
{
int T,N,M;
scanf("%d",&T);
while(T--)
{
scanf("%d",&N);
memset(dp,,sizeof dp);
for(int i=;i<=N;i++) scanf("%d",&w[i]);
int sum=;
for(int i=;i<=N;i++) sum=sum+w[i];
dp[sum]=; for(int i=;i<=N;i++)
{
memset(flag,,sizeof flag);
for(int j=*sum;j>=;j--)
{
if(dp[j])
{
if(j+w[i]<=*sum) flag[j+w[i]]=;
if(j-w[i]>=) flag[(j-w[i])]=;
}
}
for(int i=;i<=*sum;i++) if(flag[i]) dp[i]=;
} scanf("%d",&M);
for(int i=;i<=M;i++)
{
int x;
scanf("%d",&x);
if(x>sum||x<) printf("NO\n");
else
{
if(dp[sum+x]==||(sum-x>=&&dp[sum-x]==)) printf("YES\n");
else printf("NO\n");
}
}
}
return ;
}
HDU 5616 Jam's balance的更多相关文章
- HDU 5616 Jam's balance(Jam的天平)
HDU 5616 Jam's balance(Jam的天平) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K ...
- HDU 5616 Jam's balance(DP)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=5616 题目: Jam's balance Time Limit: 2000/1000 MS (Java ...
- HDU 5616 Jam's balance(01背包)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=5616 题目: Jam's balance Time Limit: 2000/1000 MS (Java ...
- HDU 5616 Jam's balance 背包DP
Jam's balance Problem Description Jim has a balance and N weights. (1≤N≤20)The balance can only tell ...
- hdu 5616 Jam's balance 正反背包+转换
http://acm.hdu.edu.cn/showproblem.php?pid=5616 思路 题目中蕴含着两种需要计算的重量 1. 从所有的砝码中挑出任意种2.(转换的思想)在天平的两端都挑出这 ...
- hdu 5616 Jam's balance(dp 正反01背包)
来自官方题解: AC代码: #pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream ...
- cdq分治(hdu 5618 Jam's problem again[陌上花开]、CQOI 2011 动态逆序对、hdu 4742 Pinball Game、hdu 4456 Crowd、[HEOI2016/TJOI2016]序列、[NOI2007]货币兑换 )
hdu 5618 Jam's problem again #include <bits/stdc++.h> #define MAXN 100010 using namespace std; ...
- hdu 5616
Jam's balance Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- HDU 5616:Jam's balance(背包DP)
http://acm.hdu.edu.cn/showproblem.php?pid=5616 题意:有n个物品,每个重量为w[i],有一个天平,你可以把物品放在天平的左边或者右边,接下来m个询问,问是 ...
随机推荐
- mongodb分片认证
启动configsvr 1. 确保mongdb的configsvr是采用service模式启动的,即从/etc/init.d下的脚本启动的,其用户是mongod. 2. 确保mongod的配置文件完全 ...
- Unable to chmod /system/build.prop.: Read-only file system
Unable to chmod /system/build.prop.: Read-only file system 只读文件系统 所以需要更改 使用下面的命令 mount -o remount,rw ...
- 笨方法学python--安装和准备
1 下载并安装python http://python.org/download 下载python2.7. python2.7并不是python3.5的旧版本. python2现在应用较广,网上资料较 ...
- c++判断一个字符串是否是数字
bool isNum(const string& str) { bool bRet = false; bool point = false; ) { return bRet; } ]) &am ...
- win8.1下安装ubuntu 14.0 4LTS
1.前奏 电脑上已经安装了win8.1系统 2.准备工作 关闭win8.1的快速启动 步骤: 控制面板->电源选项->选择电源按钮的功能->更改不可用的设置,然后把"启用快 ...
- jQuery仿百度帖吧头部固定不随滚动条滚动效果
<style> *{margin:0px;padding:0px;} div.nav{background:#000000;height:57px;line-height:57px;col ...
- THOUGHTS: programming in linux... with third_party open sources... methods
Actually I do not have experiences in programming with open sources/third party libs.. in linux.. I ...
- const与static的区别
const就是只读的意思,只在声明中使用;const修饰的数据类型是指常类型,常类型的变量或对象的值是不能被更新的. const的作用: (1)可以定义const常量,具有不可变性. (2)便于进行类 ...
- poj 2594 Treasure Exploration(最小路径覆盖,可重点)
题意:选出最小路径覆盖图中所有点,路径可以交叉,也就是允许路径有重复的点. 分析:这个题的难点在于如何解决有重复点的问题-方法就是使用Floyd求闭包,就是把间接相连的点直接连上边,然后就是求最小路径 ...
- 博弈论最简单例子TacTicToe
博弈论是人工智能中的一个分支.顾名思义就是下棋的算法.当然引申出来的应用可能不止用来下棋,也可以用来做游戏或者模拟战争策略等. 博弈的基本算法也是模拟人的思维,比如当自己下子时遍历所有可能寻求最有利步 ...