1210:f. Multiple parts and issues time limit: 1 Sec memory limit: up to MB
Submitted: Solution: Title Description
There are n different sizes of numbers, each of each. Determine whether some of these numbers can be selected to make their and exactly K.
Input
The first is a positive integer T (1<=t<=100)
Next is the T-group data
The first row of each set of data is a positive integer n (1<=n<=100), indicating that there are n different size numbers
The second line is n different-sized positive integer AI (1<=ai<=100000)
The third line is n positive integer mi (1<=mi<=100000), which indicates that each number has a MI
Line four is a positive integer K (1<=k<=100000)
Output
For each set of data, if you can select some of these numbers to make them and exactly k, then output "Yes", otherwise output "No", each output is a separate row
Sample input233 583 2 2 -21 21 14Sample outputYesNoOne-dimensional multi-backpack binary optimization thinking: First look at this problem, 100=1+2+4+8+16+32+37, observation can be concluded that any number within 100 can be obtained from the above 7 number selection combination, so the number of items is not from 0 to 100 times, but 0,1,2,4,5, 16,32,37 traversal, time is greatly optimized.
1#include <cstdio>2#include <iostream>3#include <cstring>4#include <algorithm>5#include <cmath>6 using namespacestd;7 intMain ()8 {9 intT,n,k,count;Ten inta[101],b[101]; One intf[100001]; A inti,j,m,p,g; -Cin>>T; - while(t--) the { -Memset (F,0,sizeof(f)); -Cin>>N; - for(i=1; i<=n;i++) +Cin>>A[i]; - for(i=1; i<=n;i++) +Cin>>B[i]; ACin>>K; at for(i=1; i<=n;i++) - { -p=0; -g=0; - while(b[i]>g) - { in for(j=k;j>=a[i]*g;j--) - { toF[j]=max (f[j],f[j-a[i]*g]+a[i]*g); + } -b[i]-=G; theG=pow (2, p); *p++; $ }Panax Notoginseng for(j=k;j>=a[i]*b[i];--j) - { theF[j]=max (f[j],f[j-a[i]*b[i]]+a[i]*b[i]); + } A } the if(f[k]==k) +cout<<"Yes"<<Endl; - Else $cout<<"No"<<Endl; $ } -}
Timeout code (not optimized)
1#include <cstdio>2#include <iostream>3#include <cstring>4#include <algorithm>5#include <cmath>6 using namespacestd;7 intMain ()8 {9 intT,n,k,count;Ten inta[101],b[101]; One intf[100001]; A inti,j,m; - intMax; -Cin>>T; the while(t--) - { -Memset (F,0,sizeof(f)); -Cin>>N; + for(i=1; i<=n;i++) -Cin>>A[i]; + for(i=1; i<=n;i++) ACin>>B[i]; atCin>>K; - for(i=1; i<=n;i++) - { - for(j=0; j<=b[i];j++) - { - for(m=k;m>=a[i]*j;m--) in { - if(f[m]< (f[m-j*a[i]]+j*A[i])) tof[m]=f[m-j*a[i]]+j*A[i]; + } - } the } *cout<<f[k]<<Endl; $ if(f[k]==k)Panax Notoginsengcout<<"Yes"<<Endl; - Else thecout<<"No"<<Endl; + } A}
14 Provincial---Multiple parts and problems (multiple packs + binary optimization)