zoukankan      html  css  js  c++  java
  • Jam's balance

    Jam's balance

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 2915    Accepted Submission(s): 1121


    Problem Description
    Jim has a balance and N weights. (1N20)
    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(1T5), means T test cases.
    For each test case :
    The first line is N, means the number of weights.
    The second line are N number, i'th number wi(1wi100) means the i'th weight's weight is wi.
    The third line is a number MM is the weight of the object being measured.
     
    Output
    You should output the "YES"or"NO".
     
    Sample Input
    1 2 1 4 3 2 4 5
     
    Sample Output
    NO YES YES
    Hint
    For the Case 1:Put the 4 weight alone For the Case 2:Put the 4 weight and 1 weight on both side
    #include <bits/stdc++.h>
    
    using namespace std;
    typedef long long ll;
    const int maxn = 1e5 + 10;
    const int bit = (1 << 10);
    const int oo = 6e3 + 10;
    
    int T;
    int n, Query, c[maxn], dp[maxn];
    
    int main() {
    #ifndef ONLINE_JUDGE
        freopen("1.txt", "r", stdin);
    #endif
        scanf("%d", &T);
        while (T--) {
            scanf("%d", &n);
            for (register int i = 1; i <= n; ++i) {
                scanf("%d", &c[i]);
            }
            sort(c + 1, c + 1 + n);
            memset(dp, 0, sizeof(dp));
            dp[0] = 1;
            for (register int i = 1; i <= n; ++i) {
                for (register int j = oo; j >= c[i]; --j) {
                    dp[j] = dp[j - c[i]];
                }
            }
            for (register int i = 1; i <= n; ++i) {
                for (register int j = c[i]; j <= oo; ++j) {
                    dp[j - c[i]] |= dp[j];
                }
            }
            scanf("%d", &Query);
            int cur;
            while (Query--) {
                scanf("%d", &cur);
                if (dp[cur]) {
                    puts("YES");
                } else {
                    puts("NO");
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    [多线程]使用信号量进行同步
    [多线程]互斥锁与信号量的区别
    [多线程]环形缓冲区以及多线程条件同步
    [多线程]LINUX c多线程编程-线程初始化与建立
    [STL]bitset使用
    [算法]败者树
    【Rollo的Python之路】Python:字符串内置函数
    【Rollo的Python之路】Python:字典的学习笔记
    【Rollo的Python之路】 购物车程序练习
    【Rollo的Python之路】Python 元组的学习
  • 原文地址:https://www.cnblogs.com/czy-power/p/11674967.html
Copyright © 2011-2022 走看看