hdu 1709 The Balance
来源:互联网 发布:金山在线数据恢复收费 编辑:程序博客网 时间:2024/06/06 08:24
The Balance
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8118 Accepted Submission(s): 3393
Problem 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
31 2 439 2 1
Sample Output
024 5
这道题与常规的凑数字不同,各个数字之间不仅仅是加起来了,相减也是可以的,范围是1到所有数的总和,在这个范围里面求出所有凑不到的数字,只要在原有的基础上,加上一个相减的情况就可以了,就是加上now[abs(k-j)]+=ans[j];
#include<iostream>#include<cstring>#include<cmath>#include<cstdio>#include<algorithm>using namespace std;int main(){ int n; while(scanf("%d",&n)!=EOF) { int a[11111]; int a1[111111]; int a2[111111]; memset(a1,0,sizeof(a1)); memset(a2,0,sizeof(a2)); int sum=0; int i,j,k; for(i=1;i<=n;i++) { cin>>a[i]; sum+=a[i]; } a1[a[1]]=1; a1[0]=1; for(i=2;i<=n;i++) { for(j=0;j<=sum;j++) { for(k=0;k<=a[i];k+=a[i]) { a2[k+j]+=a1[j]; a2[abs(k-j)]+=a1[j]; } } for(j=0;j<=sum;j++) { a1[j]=a2[j]; a2[j]=0; } } int ans[1111]; int l=0; int ans1=0; for(i=0;i<=sum;i++) { if(a1[i]==0) { ans1++; ans[l++]=i; } } if(ans1==0)cout<<"0"<<endl; else { int q=0; cout<<ans1<<endl; for(i=0;i<l;i++) { if(q++)cout<<" "; cout<<ans[i]; } cout<<endl; } } return 0;}
阅读全文
0 0
- hdu 1709 The Balance
- hdu 1709 The Balance
- HDU 1709 The Balance
- hdu 1709-The Balance
- Hdu 1709 The Balance
- hdu 1709 The Balance
- hdu 1709 The Balance
- hdu 1709 The Balance
- hdu 1709 The Balance
- HDU 1709 The Balance
- hdu 1709 The Balance
- HDU 1709 The Balance
- HDU 1709 The Balance
- hdu 1709 The Balance
- hdu 1709 The Balance
- hdu 1709 The Balance (dp)
- hdu 1709 The Balance (母函数)
- hdu 1709——The Balance
- 我的操盘机器人变成联合收割机了
- 运行SVM模型出错Error using svmtrain. Y must be a vector or a character array.
- (77)Collections类:sort、max、binarySearch、fill、replaceAll、reverse
- (VS2010)MFC程序中F5后有些断点无效
- 电信联通ADSL共享上网检测的原理与实现
- hdu 1709 The Balance
- VM克隆后找不到网卡的问题解决
- Java基础之String,StringBuffer与StringBuilder的区别浅谈
- eclipse调试断点
- ES6基础教程(3)
- Ubuntu开机时光标一直在左上角闪烁无法进入系统的原因及解决办法
- spring @Scheduled注解执行定时任务
- Invalid character found in method name. HTTP method names must be tokens
- 深入理解JavaScript中的箭头函数