Given a balance and n weights, each weight is weighed to find the weight that cannot be represented by the balance from 1 to the sum of the weights. Balances can be placed at both ends.
Dynamic planning practices, see online are the mother function, really do not understand.
Dp[i] If 0 means that these weights can not weigh I weight, if is 1 the indication can weigh I weight.
For each weight, assuming that the current weight is b[i], then J starts from the maximum and decreases to b[i],
If there is a dp[j-b[i]] is 1, the current J can be composed.
The reason for the J Inverse cycle is that each weight has only one, similar to the 01 backpack with a full backpack look,
If the forward loop causes each item to be used more than once, the reverse loop does not occur.
In fact, for example, can be seen.
Because the balance can be placed on both sides, it is possible to subtract the small number from the large number by means of a weight of b[i],
Traversal in the DP array is true and B[i] "Those j, then Dp[j-b[i]" must also be true, this at first glance is not reliable, but after analysis seems to be right.
What if B[i] is reused? That dp[j] has used the b[i], you have to subtract b[i], but it has only one AH.
At this time if DP[J] has been used b[i], then Dp[j-b[i]] Early has been true, that is, by other weights can be formed early dp[j-b[i]].
What if there are multiple weights minus 2 weights? Won't you miss it? In fact, in the first weight of the time recorded a number of weights minus the results of the first weight,
This result is used when the second weight is reached. This will not leak.
In fact, this is also DP, the DP of J to proceed, the reverse will lead to the use of weights reused.
#include <bitset> #include <map> #include <vector> #include <cstdio> #include <iostream># include<cstring> #include <string> #include <algorithm> #include <cmath> #include <stack > #include <queue> #include <set> #define INF 0x3f3f3f3f#define mem (a,x) memset (A,x,sizeof (a)) using Namespace Std;typedef Long long ll;typedef pair<int,int> pii;inline int in () {int res=0; char c; while (C=getchar ()) < ' 0 ' | | c> ' 9 '); while (c>= ' 0 ' && c<= ' 9 ') res=res*10+c-' 0 ', C=getchar (); return res;} const int N=100006;int dp[n];int b[2006];int ans[20000];int main () {int N; while (~SCANF ("%d", &n)) {mem (dp,0); int mx=0; for (int i=0;i<n;i++) {b[i]=in (); Mx+=b[i]; } dp[0]=1; for (int i=0;i<n;i++) {for (int j=mx;j>=b[i];j--) {if (Dp[j-b[i]) dp[j]= 1; }} for (int i=0;i<n; i++) {for (int j=b[i];j<=mx;j++) {if (dp[j]) dp[j-b[i]]=1; Each larger than the current weight, minus the weights of the current weights. }} int p=0; for (int i=1;i<=mx;i++) if (!dp[i]) ans[p++]=i; printf ("%d\n", p); for (int i=0;i<p-1;i++) {printf ("%d", ans[i]); } if (p>0) printf ("%d\n", ans[p-1]); The p>0} return 0 was omitted;
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 1709 to calculate the weight of the balance can not weigh dynamic planning