【折半枚举】HDU_5616_Jam's balance

来源:互联网 发布:怎么查看数据库ip 编辑:程序博客网 时间:2024/06/08 12:08

Jam's balance

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


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
121 43245
 

Sample Output
NOYESYES
Hint
For the Case 1:Put the 4 weight aloneFor the Case 2:Put the 4 weight and 1 weight on both side
 

Source
BestCoder Round #70
 

Recommend
hujie
 
#include <bits/stdc++.h>using namespace std;const int maxn=31;int a[maxn];set<int>s1,s2,temp;set<int>::iterator it;int main(){    int t,n,q,x;    scanf("%d",&t);    while(t--){        s1.clear();s2.clear();        s1.insert(0);s2.insert(0);        scanf("%d",&n);        for(int i=0;i<n;i++)            scanf("%d",&a[i]);        for(int i=0;i<n/2;i++){            temp.clear();            for(it=s1.begin();it!=s1.end();it++){                temp.insert((*it)+a[i]);                if((*it)-a[i]>=0)                    temp.insert((*it)-a[i]);                else temp.insert(a[i]-(*it));            }            for(it=temp.begin();it!=temp.end();it++)                s1.insert((*it));        }        for(int i=n/2;i<n;i++){            temp.clear();            for(it=s2.begin();it!=s2.end();it++){                temp.insert((*it)+a[i]);                if((*it)-a[i]>=0)                    temp.insert((*it)-a[i]);                else temp.insert(a[i]-(*it));            }            for(it=temp.begin();it!=temp.end();it++)                s2.insert((*it));        }        scanf("%d",&q);        while(q--){            scanf("%d",&x);            if(x>2000){                puts("NO");                continue;            }            int f=0;            for(int i=0;i<=x;i++){                if(s1.find(i)!=s1.end()&&s2.find(x-i)!=s2.end()){                    f=1;                    break;                }            }            if(f){                puts("YES");                continue;            }            for(int i=0;i<=2000;i++){                if((s1.find(i)!=s1.end()&&s2.find(i+x)!=s2.end())                ||(s2.find(i)!=s2.end()&&s1.find(i+x)!=s1.end())){                    f=1;                    break;                }            }            if(f)                puts("YES");            else puts("NO");        }    }    return 0;}#include <bits/stdc++.h>using namespace std;const int maxn=31;int a[maxn];vector<int>v;bool vis[2010];int main(){    int t,n,q,x;    scanf("%d",&t);    while(t--){        v.clear();        memset(vis,false,sizeof(vis));        scanf("%d",&n);        for(int i=0;i<n;i++){            scanf("%d",&a[i]);            a[n+i]=-a[i];        }        v.push_back(0);        for(int i=0;i<2*n;i++){            int t=v.size();            for(int j=0;j<t;j++){                int temp=v[j]+a[i];                if(temp>=0&&!vis[temp]){                    vis[temp]=1;                    v.push_back(temp);                }            }        }        scanf("%d",&q);        while(q--){            scanf("%d",&x);            if(!vis[x])                printf("NO\n");            else printf("YES\n");        }    }}

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 手机螺丝拧圆了怎么办 小螺丝断在孔里怎么办 螺丝断在孔里怎么办 蟑螂咬过的衣服怎么办 遇到会飞的蟑螂怎么办 狗粘了老鼠胶怎么办 家有蟑螂怎么办小窍门 人感染狗的螨虫怎么办 棉拖鞋里面的臭怎么办 卧室老有酸臭味怎么办 养狗屋子有腥味怎么办 房间里进了壁虎怎么办 家中进了只老鼠怎么办 医保卡迟迟不发怎么办 医保断了一个月怎么办 没办离职的社保怎么办 家里吊顶上有老鼠怎么办 房间里进老鼠了怎么办 衣服上有老鼠屎怎么办 如果被老鼠咬了怎么办 儿童被老鼠咬了怎么办 蟑螂爬到衣柜里怎么办 床垫里有老鼠屎怎么办 汽车里进了老鼠怎么办 狗狗感染蜱虫怎么办 脸上有螨虫怎么办才能去除 老鼠被剪丁丁后怎么办 1楼下水道钻老鼠怎么办 月经来了奶水少了怎么办 孕37周霉菌严重怎么办 家里进了飞蚂蚁怎么办 家里进了大蜘蛛怎么办 衣服上有蟑螂卵怎么办 被子上有蟑螂卵怎么办 厨房里的小飞虫怎么办 水果生的小飞虫怎么办 家里的厕所有虫怎么办 人吃了蟑螂药怎么办 静电贴粘不住了怎么办 会飞的蚂蚁咬了怎么办 家里有白蚁怎么办能除根