长沙理工大学第十二届ACM大赛-重现赛 D 小M和天平 (01背包)

链接:https://ac.nowcoder.com/acm/contest/1/D
来源:牛客网

小M和天平
时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld
题目描述
小M想知道某件物品的重量,但是摆在他面前的只有一个天平(没有游标)和一堆石子,石子可以放左边也可以放右边。他现在知道每个石子的重量。问能不能根据上述条件,能不能测出所问的重量。
输入描述:
多组数据,第一行一个数N,表示石子个数。(1<=N<=100) 接下来第二行N个数,表示石子的重量。(1<=Wi<=100) 接下来第三行一个数M,表示询问个数。(1<=M<=1000) 接下来M行每行一个数k(1<=k<=1e9),表示一个询问。
输出描述:
对于每组数据,输出"YES"或者"NO"
示例1
输入
复制
2
1 4
3
2
4
5
输出
复制
NO
YES
YES
题意:

思路:
暴力枚举每一个石头是加还是减即可,因为总重量是1e4 所以时间复杂度是 1e6 可以过

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
bool isok[10010];
int a[maxn];
int main()
{
    // freopen("D:\\code\\text\\input.txt","r",stdin);
    //freopen("D:\\code\\text\\output.txt","w",stdout);
    int n,m;
    while(~scanf("%d",&n))
    {
        int sum=0;
        repd(i,1,n)
        {
            gg(a[i]);
            // cin>>a[i];
            sum+=a[i];
        }
        MS0(isok);
        isok[0]=1;
        repd(i,1,n)
        {
            for(int j=sum;j>=a[i];j--)
            {
                isok[j]|=isok[j-a[i]];
            }
        }
        repd(i,1,n)
        {
            for(int j=1;j<=sum-a[i];j++)
            {
                isok[j]|=isok[j+a[i]];
            }
        }
        gg(m);
        // cin>>m;
        int num;
        while(m--)
        {
            gg(num);
            // cin>>num;
            if(num>sum||!isok[num])
            {
                puts("NO");
                // cout<<"NO"<<endl;
            }else
            {
                puts("YES");
                // cout<<"YES"<<endl;
            }
        }

    }
    
    
    
    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}

转载于:https://www.cnblogs.com/qieqiemin/p/10995217.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值