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 ...
随机推荐
- 从PCI上读取数据 线程和定时器效率
从PCI上读取数据 线程和定时器效率 线程: mythread=AfxBeginThread(StartContinuous,(LPVOID)1,THREAD_PRIORITY_NORMAL,0,CR ...
- 利用jsoup爬取百度网盘资源分享连接(多线程)
突然有一天就想说能不能用某种方法把百度网盘上分享的资源连接抓取下来,于是就动手了.知乎上有人说过最好的方法就是http://pan.baidu.com/wap抓取,一看果然链接后面的uk值是一串数字, ...
- Android在发送带有附件的邮件
准备好工作了-下载最新的版本号JMail https://java.net/projects/javamail/pages/Home#Download_JavaMail_1.5.2_Release h ...
- vs2015 安装问题汇总
1. The product version that you are trying to set up is earlier than the version already installed o ...
- The app references non-public selectors in payload With Xcode6.1
猴子原创,欢迎转载.转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com,谢谢! 原文地址: p=591" style="color: rgb(255, 97, 0 ...
- win8.1 64位环境建设android开发环境
1.下载JDK,http://www.oracle.com/technetwork/java/javase/downloads/index.html,选择版本号 2.安装刚刚下载的JDK 3.环境变量 ...
- response.setHeader各种使用方法
一秒刷新页面一次 response.setHeader("refresh","1"); 二秒跳到其它页面 response.setHeader("re ...
- Lua 服务器与客户端实例(转)
=============================================================== 服务器,main.lua ======================= ...
- Spring的文件上传
Spring在发现包括multipart的请求后,会使用MultipartResolver的实现bean处理文件上传操作,现有採用Servlet3的 org.springframework.web.m ...
- NYOJ 118 路方案(第二小的跨越)
修路方案 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描写叙述 南将军率领着很多部队,它们分别驻扎在N个不同的城市里,这些城市分别编号1~N.因为交通不太便利,南将军准备修 ...