多重背包二进制优化 && Hdu 1059

xiaoxiao2021-02-27  141

多重背包有时候遇到大数据的时候是特别慢的,我们这是可以用二进制进行优化,简单的来说,就是假如说有10个价值为2,重量为3的物品,我们可以简化成1个价值为2,重量为3,一个价值为4,重量为6,一个价值为8,重量为12,一个价值为6,重量为9的物品,其实也就相当于把10分成了1,2,4,3的状态,然后进行01背包的计算,因为这四个状态可以组合成从1到10的所有状态,在大数据的时候优化效果明显,附上我的板子:

#include<bits/stdc++.h> using namespace std; const int maxn=1e5+5; int value[maxn],weight[maxn],dp[maxn]; int main() { int T; cin>>T; while(T--) { int N,V,cnt=1,values,weights,num; cin>>V>>N; for(int i=1;i<=N;i++) { cin>>values>>weights>>num; for(int i=1;i<=num;i*=2) { value[cnt]=values*i; weight[cnt++]=weights*i; num-=i; } if(num!=0) { value[cnt]=num*values; weight[cnt++]=num*weights; } } memset(dp,0,sizeof(dp)); for(int i=1;i<cnt;i++) { for(int j=weight[i];j>=0;j--){ dp[j]=max(dp[j],dp[j-weight[i]]+value[i]); } } cout<<dp[V]<<endl; } return 0; }

这里用一道例题来练手:Hdu 1059

Dividing

Problem Description Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles. This would be easy if all the marbles had the same value, because then they could just split the collection in half. But unfortunately, some of the marbles are larger, or more beautiful than others. So, Marsha and Bill start by assigning a value, a natural number between one and six, to each marble. Now they want to divide the marbles so that each of them gets the same total value. Unfortunately, they realize that it might be impossible to divide the marbles in this way (even if the total value of all marbles is even). For example, if there are one marble of value 1, one of value 3 and two of value 4, then they cannot be split into sets of equal value. So, they ask you to write a program that checks whether there is a fair partition of the marbles.

Input Each line in the input describes one collection of marbles to be divided. The lines consist of six non-negative integers n1, n2, …, n6, where ni is the number of marbles of value i. So, the example from above would be described by the input-line “1 0 1 2 0 0”. The maximum total number of marbles will be 20000.

The last line of the input file will be “0 0 0 0 0 0”; do not process this line.

Output For each colletcion, output Collection #k:'', where k is the number of the test case, and then eitherCan be divided.” or “Can’t be divided.”.

Output a blank line after each test case.

Sample Input 1 0 1 2 0 0 1 0 0 0 1 1 0 0 0 0 0 0

Sample Output Collection #1: Can’t be divided.

Collection #2: Can be divided.

暴力很定不行,我们用二进制优化一下卡时间过

#include<bits/stdc++.h> using namespace std; using LL=int64_t; const int INF=0x3f3f3f3f; const int maxn=12e4+5; int dp[maxn]; int main() { ios::sync_with_stdio(0); cin.tie(0); int kase=1,num[10],ans[1000]={0}; while(cin>>num[1]>>num[2]>>num[3]>>num[4]>>num[5]>>num[6]) { int sum=0,flag=0; for(int i=1;i<=6;i++){ sum+=num[i]*i; } if(sum==0) break; if(sum%2==1) flag=1; else { int cnt=1; for(int i=1;i<=6;i++){ for(int j=1;j<=num[i];j*=2) { ans[cnt++]=j*i; num[i]-=j; } if(num[i]!=0)ans[cnt++]=num[i]*i; } memset(dp,0,sizeof(dp)); int mid=sum/2; for(int i=1;i<cnt;i++) { for(int j=mid;j>=0;j--) { if(dp[j]<=mid&&j>=ans[i]&&dp[j-ans[i]]+ans[i]<=mid) { dp[j]=max(dp[j],dp[j-ans[i]]+ans[i]); } if(dp[mid]==mid) break; } if(dp[mid]==mid) break; } if(dp[mid]!=mid) flag=1; } cout<<"Collection #"<<kase++<<":\n"; if(flag==0) cout<<"Can be divided.\n\n"; else cout<<"Can't be divided.\n\n"; } return 0; }
转载请注明原文地址: https://www.6miu.com/read-17092.html

最新回复(0)