当前位置:网站首页>Partition marble (multiple knapsack + binary optimization)
Partition marble (multiple knapsack + binary optimization)
2022-06-30 18:07:00 【The most handsome man】
Divide the marble ( Multiple backpack + Binary optimization )
Description
The values are 1…6 Of marble a[1…6] block , Now I want to divide them into two parts , Make the sum of the two parts equal , Ask if you can achieve . The total number of marble is not more than 20000.
Input
There are multiple sets of data !
So how could it be
If there is 0 0 0 0 0 0 Indicates the end of the input file
The rest of the action 6 It's an integer
Output
How many lines of feasible data are output
If the division succeeds , Output Can, otherwise Can’t
Samples
Input Copy
4 7 4 5 9 1
9 8 1 7 2 4
6 6 8 5 9 2
1 6 6 1 0 7
5 9 3 8 8 4
0 0 0 0 0 0
Output
Can’t
Can
Can’t
Can’t
Can
Ideas : This problem is multiple backpacks . Ask if it can be divided into two equal parts , It can be transformed into sum/2 Whether there is , If there is , Then the rest must be equal to sum/2;
General multiple backpacks :( Simple algorithm )
//n Items , Capacity of m The backpack , Each item has a maximum of s[i] individual , The volume is v[i], Value is w[i], ask
// Package maximum value
for(int i=1;i<=n;i++){
for(int j=m;j>=v[i];j--){
for(int k=0;k*v[i]<=j&&k<=s[i];k++){
f[j]=max(f[j],f[j-k*v[i]]+k*w[i]);
}
}
}
Binary optimization
//n Items , Capacity of m The backpack , Each item has a maximum of s[i] individual , The volume is v[i], Value is w[i], ask
// Package maximum value
// hypothesis 50 An apple , take n individual , If the naive algorithm is one by one , But binary is to 50 An apple is divided into several parts (1,2,4,8,16,29( Because the rest is not satisfied 2*16) Add these ==50) These can be made up of anything 50 Any number within , hypothesis n yes 6, The naive algorithm should 6 Time , Binary must 2 Time (2,4)
int vv[maxn],ww[maxn],tot=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=s[i];j*=2){
vv[++tot]=v[i]*j;
ww[tot]=w[i]*j;
s[i]-=j;
}
if(s[i]){
vv[++tot]=v[i]*s[i];
ww[tot]=w[i]*s[i];
}
}
for(int i=1;i<=tot;i++){
for(int j=m;j>=vv[i];j--){
f[j]=max(f[j],f[j-vv[i]]+w[i]);
}
}
AC Code :
int a[7];
int w[maxn];
int f[maxn];
int main(){
while(1){
int sum=0,aa=0;
int num=0;
for(int i=1;i<=6;i++) {
cin>>a[i],sum+=i*a[i];
if(!a[i]) aa++;
for(int j=1;j<=a[i];j*=2){
w[++num]=i*j;
a[i]-=j;
}
if(a[i]) w[++num]=a[i]*i;
}
if(aa==6) return 0;
if(sum&1) {
puts("Can't");
continue;
}
memset(f,0,sizeof(f));
f[0]=1;
for(int i=1;i<=num;i++){
for(int j=sum/2;j>=w[i];j--){
// Here the volume is replaced by the value , Whether there is !!! miao
f[j]=max(f[j],f[j]+f[j-w[i]]);
}
}
if(f[sum/2]) cout<<"Can\n";
else puts("Can't");
}
return 0;
}
边栏推荐
- [machine learning] K-means clustering analysis
- 广电5G正式启航,黄金频段将如何应用引关注
- 联想“双平台”运维解决方案 助力智慧医疗行业智慧管理能力全面提升
- A tough battle for Tencent cloud
- MySQL reports that the column timestamp field cannot be null
- Redis (III) - transaction
- Design of online shopping mall based on SSH
- [binary tree] preorder traversal to construct binary search tree
- 分布式机器学习:模型平均MA与弹性平均EASGD(PySpark)
- Nft: unlimited possibilities to open the era of encryption Art
猜你喜欢

广电5G正式启航,黄金频段将如何应用引关注

Booking UI effect implemented by svg

Redis (IV) - delete policy

Development: how to install offline MySQL in Linux system?

【架构】1366- 如何画出一张优秀的架构图

TFTP download kernel, NFS mount file system

Apache 解析漏洞(CVE-2017-15715)_漏洞复现

Redis (V) - advanced data types
![[zero basic IOT pwn] environment construction](/img/3b/a0689a1570fcc40bb9a5a4e9cdc63c.png)
[zero basic IOT pwn] environment construction

Daily interview 1 question - how to prevent CDN protection from being bypassed
随机推荐
Add code block in word (Reprint)
4 years of working experience, and you can't tell the five communication modes between multithreads. Can you believe it?
vue3 响应式数据库—— reactive
Horizontal visual error effect JS special effect code
Building a basic buildreoot file system
Simulation of campus network design based on ENSP
Mo Tianlun salon | Tsinghua qiaojialin: Apache iotdb, originated from Tsinghua, is building an open source ecological road
splitting. JS password display hidden JS effect
Several points in MySQL that are easy to ignore and forget
Canvas mouse control gravity JS effect
基于SSM的新闻管理系统
MySQL reports that the column timestamp field cannot be null
基于eNSP的校园网设计的仿真模拟
TFTP下载kernel,nfs挂载文件系统
Combination of applet container and Internet of things
【架构】1366- 如何画出一张优秀的架构图
Development: how to install offline MySQL in Linux system?
自旋锁探秘
Share 5 commonly used feature selection methods, and you must see them when you get started with machine learning!!!
Redis (V) - advanced data types