码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1709 The Balance

时间:2015-08-15 16:22:41      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

Description

Now you are asked to measure a dose of medicine with a balance and a number of weights. Certainly it is not always achievable. So you should find out the qualities which cannot be measured from the range [1,S]. S is the total quality of all the weights. 
 

Input

The 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 you have. Followed by N integers Ai (1<=i<=N), indicating the quality of each weight where 1<=Ai<=100. 
 

Output

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

Sample Input

3 1 2 4 3 9 2 1
 

Sample Output

0 2 4 5
 
题意:
题意:给定n个砝码的重量,总质量为sum,问在1~sum中有多少个重量不能被称出来。
每种砝码既可以放在右盘,又可以放在左盘,(若按左物右码来说),放在左盘那就取减号,放在右盘就取加号。
 
就是套用01背包的代码
 方程:
               c[j] |= b[j];//c【】,b【】,有一个为1 就成立,不加a【】的情况
               if(j+a[i]<=sum)
                   c[j+a[i]]+=b[j];//加a【】
               c[(int)abs(j-a[i])]+=b[j];//两个砝码的差
技术分享
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int main()
{
   int t,a[105],i,j,sum;
   int b[10005],c[10005];
   while(scanf("%d",&t)!=-1)
   {
       sum=0;
       for(i=1;i<=t;i++)
       {
           scanf("%d",&a[i]);
           sum+=a[i];
       }
       memset(b,0,sizeof(b));
       memset(c,0,sizeof(c));
       b[0]=1;
       b[a[1]]=1;
       for(i=2;i<=t;i++)
       {
           for(j=0;j<=sum;j++)
           {
               c[j] |= b[j];//c【】,b【】,有一个为1 就成立,不加a【】的情况
               if(j+a[i]<=sum)
                   c[j+a[i]]+=b[j];//加a【】
               c[(int)abs(j-a[i])]+=b[j];//两个砝码的差
           }
           memcpy(b,c,sizeof(c));
           memset(c,0,sizeof(c));
       }
       j=0;
       for(i=1;i<=sum;i++)
           if(b[i]==0)
               c[j++]=i;
           printf("%d\n",j);
           if(j)
           {
               printf("%d",c[0]);
               for(i=1;i<j;i++)
                   printf(" %d",c[i]);
               printf("\n");
           }
   }
   return 0;
}
View Code

 

HDU 1709 The Balance

标签:

原文地址:http://www.cnblogs.com/demodemo/p/4732591.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!