hdu 1059 Dividing(dp+二进制优化)

解决Marsha和Bill如何公平地分配不同价值的大理石集合的问题。通过二进制优化的背包算法检查是否存在一种分配方式使得两人的总价值相等。

Dividing

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10438    Accepted Submission(s): 2918


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 either ``Can 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.


 

Source


 

Recommend
JGShining

 

思路:直接背包的时间是6*N*N毫无疑问TLE。所以需要二进制优化。

        for(int i=1;i<=6;i++)
        for(int j=1;j<=v[i];j++)
        for(int k=sum-i*j;k>=0;k--)
        if(dp[k])dp[k+j*i]=1;   

但仔细考虑这个过程,手动模拟一下你会发现很多的重复标记。

因此可以使用二进制来减小这些重复标记 的过程。1 2, 4 8 16...

具体手动算一下,就明白了。

for(int i=1;i<=6;i++)
    { for(int j=1;j<=v[i];j*=2)
      { v[i]-=j;
        for(int k=sum-i*j;k>=0;k--)
        {
        if(dp[k])dp[k+j*i]=1;
        }
     }
     if(v[i])
     {
       for(int k=sum-i*v[i];k>=0;k--)
       if(dp[k])dp[k+i*v[i]]=1;
     }
    }                                           

 

#include<iostream>
#include<cstring>
using namespace std;
const int mm=620010;
int dp[mm];
int v[7];
int main()
{ int sum;
  int cas=0;
  while(1)
  { sum=0;++cas;
    for(int i=1;i<=6;i++)
    {
      cin>>v[i];
      sum+=v[i]*i;
    }
    if(!sum)break;
    cout<<"Collection #"<<cas<<":\n";
    if(sum&1)
    {cout<<"Can't be divided.\n\n";
     continue;
    }
    sum/=2;
    memset(dp,0,sizeof(dp));
    dp[0]=1;
    for(int i=1;i<=6;i++)
    { for(int j=1;j<=v[i];j*=2)
      { v[i]-=j;
        for(int k=sum-i*j;k>=0;k--)
        {
        if(dp[k])dp[k+j*i]=1;
        }
     }
     if(v[i])
     {
       for(int k=sum-i*v[i];k>=0;k--)
       if(dp[k])dp[k+i*v[i]]=1;
     }
    }
    if(dp[sum])cout<<"Can be divided.\n";
    else cout<<"Can't be divided.\n";
    cout<<"\n";
  }
}


 

 

 

基于粒子群优化算法的p-Hub选址优化(Matlab代码实现)内容概要:本文介绍了基于粒子群优化算法(PSO)的p-Hub选址优化问题的研究与实现,重点利用Matlab进行算法编程和仿真。p-Hub选址是物流与交通网络中的关键问题,旨在通过确定最优的枢纽节点位置和非枢纽节点的分配方式,最小化网络总成本。文章详细阐述了粒子群算法的基本原理及其在解决组合优化问题中的适应性改进,结合p-Hub中转网络的特点构建数学模型,并通过Matlab代码实现算法流程,包括初始化、适应度计算、粒子更新与收敛判断等环节。同时可能涉及对算法参数设置、收敛性能及不同规模案例的仿真结果分析,以验证方法的有效性和鲁棒性。; 适合人群:具备一定Matlab编程基础和优化算法理论知识的高校研究生、科研人员及从事物流网络规划、交通系统设计等相关领域的工程技术人员。; 使用场景及目标:①解决物流、航空、通信等网络中的枢纽选址与路径优化问题;②学习并掌握粒子群算法在复杂组合优化问题中的建模与实现方法;③为相关科研项目或实际工程应用提供算法支持与代码参考。; 阅读建议:建议读者结合Matlab代码逐段理解算法实现逻辑,重点关注目标函数建模、粒子编码方式及约束处理策略,并尝试调整参数或拓展模型以加深对算法性能的理解。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值