标签:bool sizeof target silver 分析 als names hang false
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 41707 | Accepted: 14125 |
Description
Input
Output
Sample Input
3 10
1 2 4 2 1 1
2 5
1 4 2 1
0 0
Sample Output
8 4
分析:很显然的多重背包,由数据范围可知需要进行拆分优化或者用单调队列优化。这里蒟蒻还不会单调队列优化,所以用的是二进制拆分。
Code:
1 //It is made by HolseLee on 17th May 2018 2 //POJ 1742 3 #include<cstdio> 4 #include<cstring> 5 #include<cstdlib> 6 #include<cmath> 7 #include<iostream> 8 #include<iomanip> 9 #include<algorithm> 10 #define Fi(i,a,b) for(int i=a;i<=b;i++) 11 #define Fx(i,a,b) for(int i=a;i>=b;i--) 12 using namespace std; 13 const int N=1e5+7;bool dp[N]; 14 int n,m,cnt,ans,k[N],a[101],c[101]; 15 int main() 16 { 17 ios::sync_with_stdio(false); 18 while(cin>>n>>m){ 19 if(!n||!m)break; 20 memset(dp,false,sizeof(dp)); 21 dp[0]=true;cnt=0;ans=0; 22 Fi(i,1,n)cin>>a[i];Fi(i,1,n)cin>>c[i]; 23 Fi(i,1,n){ 24 for(int j=1;j<=c[i];j<<=1){ 25 k[++cnt]=a[i]*j;c[i]-=j;} 26 if(c[i])k[++cnt]=a[i]*c[i];} 27 Fi(i,1,cnt)Fx(j,m,k[i]){ 28 dp[j]|=dp[j-k[i]];} 29 Fi(i,1,m)if(dp[i])ans++; 30 cout<<ans<<"\n";} 31 return 0; 32 }
标签:bool sizeof target silver 分析 als names hang false
原文地址:https://www.cnblogs.com/cytus/p/9053093.html