poj 1004 Dividing
大意是,从输入六个数 。第i个数代表价值为i的有几个,平均分给两个人 ,明摆着的背包问题,本来以为把他转化为01背包。可是TLe,后来发现是12万的平方还多,所以妥妥的TLE,后来发现这是一个全然背包问题。然后即纠结了 ,没学过啊 。最后发现思想好i是蛮简单的,水水的A掉了。最后注意一下初始化问题和输入问题后就好了
#include <stdio.h>
#include <string.h>
int a[10];
int dp[120005];
int maxx(int a,int b)
{
return (a>b)?a:b;
}
int main()
{
int cases=0;
while(scanf("%d%d%d%d%d%d",&a[1],&a[2],&a[3],&a[4],&a[5],&a[6])!=EOF)
{
memset(dp,0,sizeof(dp));
int sum=0;
if(a[1]==0&&a[2]==0&&a[3]==0&&a[4]==0&&a[5]==0&&a[6]==0)
break;
for(int i=1;i<7;i++)
{
sum+=a[i]*i;
}
int mount;
int i,j,k;
// printf(" %d\n",sum);
if(sum%2)//当时奇数的时候肯定不能分开
{
printf("Collection #%d:\nCan't be divided.\n\n",++cases);
}
else
{
for(i=1;i<=6;i++)
{
// printf("%d %d ",sum,a[i]);
mount=a[i];
// printf("%d\n",mount);
dp[0]=1;//初始化为1。假设不初始化的话,由于dp【1】+=dp【0】。
for(k=1;k<=mount;k*=2)
{
for(j=sum/2;j>=k*i;j--)
dp[j]+=dp[j-k*i];
mount-=k;
}
if(mount)
for(j=sum/2;j>=mount*i;j--) //从sum/2開始 最后能不能有,有就一定是sum/2;
dp[j]+=dp[j-mount*i];
}
//for(int i=0;i<sum/2;i++)
// printf("%d\n",dp[i]);
if(dp[sum/2])
printf("Collection #%d:\nCan be divided.\n",++cases);
else
printf("Collection #%d:\nCan't be divided.\n",++cases);
printf("\n");
}
}
}
版权声明:都是兄弟,请重新发布。请说明谁是兄弟
poj 1004 Dividing的更多相关文章
- Financial Management POJ - 1004
Financial Management POJ - 1004 解题思路:水题. #include <iostream> #include <cstdio> #include ...
- POJ 2373 Dividing the Path(DP + 单调队列)
POJ 2373 Dividing the Path 描述 农夫约翰的牛发现,在他的田里沿着山脊生长的三叶草是特别好的.为了给三叶草浇水,农夫约翰在山脊上安装了喷水器. 为了使安装更容易,每个喷头必须 ...
- POJ 1004 解题报告
1.题目描述: http://poj.org/problem?id=1004 2.解题过程 这个题目咋一看很简单,虽然最终要解出来的确也不难,但是还是稍微有些小把戏在里面,其中最大的把戏就是float ...
- OpenJudge/Poj 1004 Financial Management
1.链接地址: http://poj.org/problem?id=1004 http://bailian.openjudge.cn/practice/1004 2.题目: 总时间限制: 1000ms ...
- POJ 1014 Dividing
Dividing Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 66032 Accepted: 17182 Descriptio ...
- POJ 1014 Dividing(多重背包)
Dividing Description Marsha and Bill own a collection of marbles. They want to split the collectio ...
- poj 2373 Dividing the Path
Dividing the Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2858 Accepted: 1064 ...
- POJ 1014 Dividing 多重背包
Dividing Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63980 Accepted: 16591 Descri ...
- POJ 1014 Dividing(多重背包+二进制优化)
http://poj.org/problem?id=1014 题意:6个物品,每个物品都有其价值和数量,判断是否能价值平分. 思路: 多重背包.利用二进制来转化成0-1背包求解. #include&l ...
随机推荐
- 通信协议:HTTP、TCP、UDP(转)
原文出处: 碧雪轩的博客 TCP HTTP UDP: 都是通信协议,也就是通信时所遵守的规则,只有双方按照这个规则“说话”,对方才能理解或为之服务. TCP HTTP UDP三者的关系 ...
- C#的c/s做出开灯关灯计算?
static void light(Boolean[] lights,int n) { if (n <= 1 || lights.Length<5) return; for ...
- Delphi F11 全屏
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- 内网port映射具体解释(花生壳)
关于怎样建立服务器的解答. 一.花生壳的作用 首先,我们先来了解一下花生壳的究竟有什么作用.由于ADSL每次拨号上网所获得的IP地址每次都是不同的,花生壳起到的作用就是方便用户訪问我们的server( ...
- Java EE (6) -- Java EE 5 Enterprise Architect Certified Master
Section 1: Application Design Concepts and Principles Explain the main advantages of an object-orien ...
- Vs2012在Linux应用程序开发(3):加入新平台hi3516
下面我们将VS2012添加一个新的平台支持,由于近来与哈斯hi3516.就选它吧! 1.1 复制平台文件 原来一直认为要让VS支持一个新的平台须要编写代码,某天在看MSBUILD文件夹的时候突 ...
- poj 1789 Truck History(kruskal算法)
主题链接:http://poj.org/problem?id=1789 思维:一个一个点,每两行之间不懂得字符个数就看做是权值.然后用kruskal算法计算出最小生成树 我写了两个代码一个是用优先队列 ...
- HDU 1856 More is better(并查集+离散化)
题目地址:HDU 1856 水题.因为标号范围太大,而数据数仅仅有10w,所以要先进行离散化.然后就是裸的并查集了. 代码例如以下: #include <iostream> #includ ...
- PHPCMS V9{loop subcat(0,0,0,$siteid) $r}怎么解释?
{loop subcat(0,0,0,$siteid) $r}{/loop} /** * 获取子栏目 * @param $parentid 父级id * @param $type 栏目类型 * ...
- opencv环境的搭建,并打开一个本地PC摄像头。
1.opencv环境结构 推荐连结 http://www.cnblogs.com/Anykong/archive/2011/04/06/Anykong_OpenCV1.html 2.以下是基本測试,和 ...