HDU 5616 Jam's balance
来源:互联网 发布:子域名怎么看 编辑:程序博客网 时间:2024/06/05 06:47
Problem Description
Jim has a balance and N weights. (1≤N≤20)
The balance can only tell whether things on different side are the same weight.
Weights can be put on left side or right side arbitrarily.
Please tell whether the balance can measure an object of weight M.
The balance can only tell whether things on different side are the same weight.
Weights can be put on left side or right side arbitrarily.
Please tell whether the balance can measure an object of weight M.
Input
The first line is a integer T(1≤T≤5) , means T test cases.
For each test case :
The first line isN , means the number of weights.
The second line areN number, i'th number wi(1≤wi≤100) means the i'th weight's weight is wi .
The third line is a numberM . M is the weight of the object being measured.
For each test case :
The first line is
The second line are
The third line is a number
Output
You should output the "YES"or"NO".
Sample Input
121 43245
Sample Output
NOYESYESHintFor the Case 1:Put the 4 weight aloneFor the Case 2:Put the 4 weight and 1 weight on both side
题意:有一个没有游标的天平,和n个秤砣,m个询问, 每次一个k,
问可否秤出k这个重量。 秤砣可以放两边。
dp[n][sum]存储可达与不可达....
dp[i][j] 表示放前i个秤砣能否称出j重量
因为相减可能为负 所以把全部值加上sum 避免出现负数
因此初始为 dp[0][sum] = 1
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <queue>#include <vector>#include <cmath>#include <stack>#include <string>#include <map>#include <set>#define pi acos(-1)#define LL long long#define ULL unsigned long long#define inf 0x3f3f3f3f#define INF 1e18#define lson l,mid,rt<<1#define rson mid+1,r,rt<<1|1using namespace std;typedef pair<int, int> P;const int maxn = 1e5 + 5;const int mod = 1e8;int dp[25][maxn];int a[25];int main(void){//freopen("C:\\Users\\wave\\Desktop\\NULL.exe\\NULL\\in.txt","r", stdin); int T, i, j, n, m, sum, t; cin >> T; while (T--) { sum = 0; cin >> n; for (i = 1; i <= n; i++){ cin >> a[i]; sum += a[i]; } memset(dp, 0, sizeof(dp)); dp[0][sum] = 1; for (i = 1; i <= n; i++){ for (j = 1; j <= 2*sum; j++){ if (dp[i-1][j]){ dp[i][j] = 1; dp[i][j - a[i]] = 1; dp[i][j + a[i]] = 1; } } } cin >> m; while (m--){ cin >> t; if (t > sum) puts("NO"); else if (dp[n][t + sum]) puts("YES"); else puts("NO"); } }return 0;}
0 0
- HDU 5616 Jam's balance
- hdu 5616 Jam's balance
- HDU 5616 Jam's balance
- HDU 5616 Jam's balance
- HDU 5616Jam's balance
- HDU 5616 Jam's balance
- HDU 5616 Jam's balance(dp)
- hdu 5616 Jam's balance ( 动态规划 )
- hdu 5616 Jam's balance(简单dp)
- HDU 5616 Jam's balance(dp)
- HDU 5616 Jam's balance(01背包)
- HDU 5616 Jam's balance dp, bitset
- HDU-5616 Jam's balance(母函数)
- hdoj-5616-Jam's balance
- HDU 5616 Jam's balance(01背包)
- HDU 5616 Jam's balance(暴力枚举子集)
- 杭电 hdu 5616 Jam's balance【思维】【暴力枚举】
- HDU 5616 Jam's balance(01背包)
- hdu3698 Let the light guide us
- Servlet的一个简单调用
- UVA - 10167 Birthday Cake
- hdu 5855 Less Time, More profit【二分+最大权闭包--最大流Dinic】
- bzoj2555: SubString
- HDU 5616 Jam's balance
- UVA - 146 ID Codes
- Qt在Windows下的三种编程环境搭建
- UVA - 10098 Generating Fast
- UVa 1400
- Power String poj2406(后缀数组+DC3算法)
- 弟子规详解
- 蓝鸥 8.16
- Dijkstra、Bellman-Ford及Spfa算法思想对比