HDU 1709 the Balance (DP)

Source: Internet
Author: User

The Balance

Time limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 5956 Accepted Submission (s): 2427


Problem Descriptionnow You is asked to measure a dose of medicine with a balance and a number of weights. Certainly it isn't always achievable. So-should find out the qualities which cannot is measured from the range [1,s]. S is the total quality of the weights.

Inputthe input consists of multiple test cases, and each case begins with a single positive integer N (1<=n<=100) on A line by itself indicating the number of weights. followed by N integers Ai (1<=i<=n), indicating the quality of each weight where 1<=ai<=100.

Outputfor each input set, you should first print a line specifying the number of qualities which cannot is measured. Then print another line which consists all the irrealizable qualities if the number was not zero.

Sample Input31 2 439 2 1

Sample Output024 51 very interesting recursion. Just ask for a weight of N to wi. Ask what weight you can't weigh. BOOL Dp[i][j] represents the first I weight, can be weighed J. Initialize dp[0][0] = true; for a dp[i-1][j] = True, there must be dp[i][j+a[i]] = True, Dp[i][abs (J-a[i])] = True
#include <iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<vector>#include<queue>#include<map>#include<Set>#include<stack>#include<algorithm>using namespacestd;#defineRoot 1,n,1#defineLson l,mid,rt<<1#defineRson mid+1,r,rt<<1|1#defineLR rt<<1#defineRR rt<<1|1typedefLong LongLl;typedef pair<int,int>PII;#defineX First#defineY SecondConst intOO = 1e9+7;Const DoublePI = ACOs (-1.0);Const DoubleEPS = 1e-6 ;Const intN =10500;Const intMoD = 1e9+7;BOOLdp[ the][n];intN, M, A[n], b[n], tot;voidinit () {memset (DP,false,sizeofDP); dp[0][0] =true ;  for(inti =1; I <= N; ++i) {         for(intj =0; J <= tot; ++j) {            if(dp[i-1][J]) dp[i][j] = dp[i-1][j]; if(dp[i-1][j]) {dp[i][j+a[i]] =true; Dp[i][abs (j-a[i]) =true ; }        }    //For (int j = 0; J <= tot; ++j) cout << dp[i][j] << "; cout << Endl;    }}voidRun () {tot=0 ;  for(inti =1; I <= N; ++i) Cin>> A[i], tot + =A[i]; Init (); intCNT =0 ;  for(inti =1; I <= tot; ++i)if( !Dp[n][i]) {b[cnt++] =i; } cout<< CNT <<Endl;  for(inti =0; I < CNT; ++i) cout<< B[i] << (i+1==cnt?"\ n":" ");}intMain () {//freopen ("In.txt", "R", stdin);Ios::sync_with_stdio (false);  while(Cin >>N) Run ();
View Code

HDU 1709 the Balance (DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.