Jam's balance HDU - 5616 (01背包基础题)
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.
Input
The first line is a integer T(1≤T≤5), 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(1≤wi≤100) means the i'th weight's weight is wi.
The third line is a number M. M 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
題意:
給你n個(gè)砝碼,q個(gè)詢(xún)問(wèn),對(duì)于每一個(gè)詢(xún)問(wèn),你需要回答這n個(gè)瑪法能否選取一些瑪法的組合稱(chēng)量出x的重量。砝碼可以放在天平的左側(cè)和右側(cè)。
思路:
01背包基礎(chǔ)題。
定義dp[x] =1 表示可以測(cè)量x重量。
正著掃一遍,反著掃一遍即可,代碼有注釋。
細(xì)節(jié)見(jiàn)代碼:
#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 dll(x) scanf("%I64d",&x) #define xll(x) printf("%I64d\n",x) #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 chu(x) cout<<"["<<#x<<" "<<(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 ***/ int dp[maxn]; int a[maxn]; int main() {//freopen("D:\\code\\text\\input.txt","r",stdin);//freopen("D:\\code\\text\\output.txt","w",stdout);int t;gbtb;cin>>t;while(t--){int n,q,w;cin>>n;int sum=0;repd(i,1,n){cin>>a[i];sum+=a[i];}MS0(dp);dp[0]=1;repd(i,1,n){for(int j=sum;j>=a[i];--j)// 一定要反著dp,,因?yàn)轫来a的個(gè)數(shù)1 個(gè),{// 從后向前可以避免當(dāng)前修改的dp[j]在本次過(guò)程中的后續(xù)中有影響。if(dp[j-a[i]]==1){dp[j]=1;}}}repd(i,1,n){for(int j=1;j<=sum;++j){if(dp[j+a[i]]==1)// 把a(bǔ)[i]放在天平的反側(cè),就可以測(cè)量出 j的重量。{dp[j]=1;}}}cin>>q;while(q--){cin>>w;if(dp[w]){cout<<"YES"<<endl;}else{cout<<"NO"<<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';}} }轉(zhuǎn)載于:https://www.cnblogs.com/qieqiemin/p/11216002.html
總結(jié)
以上是生活随笔為你收集整理的Jam's balance HDU - 5616 (01背包基础题)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 茴香豆的n种写法之②——Sql据多个条件
- 下一篇: 2019长安大学ACM校赛网络同步赛