杭電 HDU ACM 1709 The Balance
阿新 • • 發佈:2019-02-12
The Balance
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6304 Accepted Submission(s): 2592
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 3 1 2 4 3 9 2 1
Sample Output 0 2 4 5這個題 丫的浪費好長時間 好幾個小時 ,唉 都怪自己笨 ,用母函式來做,母函式部分算出所有可能組合出來的值得情況。假設先把最大值的組合情況放在左盤,從1開始檢測所有組合情況,這個最大值的來源有兩種情況,case1:前面某些較小的砝碼組合,case2:本身那個砝碼就最大。對於case1:那個最大值組合減去其他某種組合情況之一相當於 左盤減 右盤加!!對於 case2:相當於左盤不變,從砝碼箱裡取出其他砝碼組合加入到右盤。最坑人的是 超時好幾次!稍微優化了一下,比規定時間少2毫秒!哈哈哈
#include<iostream> #include<stdio.h> #include<string.h> using namespace std; int cnt[100002],dic[100002]; int main() { int n,A[101]; while(scanf("%d",&n)!=EOF) { int Max=0; for(int i=0; i<n; i++) { scanf("%d",&A[i]); Max+=A[i]; } for(int j=0; j<=Max; j++) { cnt[j]=0; dic[j]=0; } cnt[0]=cnt[A[0]]=1; for(int t=2; t<=n; t++) { for(int k=0; k<=Max; k++) { if(cnt[k])//如果前一對括號中某個指數的係數為0,那麼即使乘以下一項,也不會給當前括號內某一項指數和為T的係數有效力 { for(int h=0; h+k<=Max&&h<=A[t-1]; h+=A[t-1]) { dic[k+h]+=cnt[k]; } } } for(int p=0; p<=Max; p++) { cnt[p]=dic[p]; dic[p]=0; } } int buff[Max+1]; memset(buff,0,sizeof(buff)); for(int q=Max; q>=1; q--) { if(cnt[q]) { for(int f=1; f<=q; f++) { if(cnt[f]) { if(f==q) buff[q]=1; else buff[q-f]=1; } } } } int ls[Max+1],r,count=0,t=0,flag=0; memset(ls,0,sizeof(ls)); for(int a=1; a<=Max; a++) { if(!buff[a]) { ls[t++]=a,count++; flag=1; } } if(!flag) { cout<<0<<endl; continue; } cout<<count<<endl; for( r=0; r<t-1; r++) cout<<ls[r]<<" "; cout<<ls[r]<<endl; } return 0; }