poj 1742(好题,楼天城男人八题,混合背包)
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 33269 | Accepted: 11295 |
Description
You are to write a program which reads n,m,A1,A2,A3...An and
C1,C2,C3...Cn corresponding to the number of Tony's coins of value
A1,A2,A3...An then calculate how many prices(form 1 to m) Tony can pay
use these coins.
Input
input contains several test cases. The first line of each test case
contains two integers n(1<=n<=100),m(m<=100000).The second line
contains 2n integers, denoting A1,A2,A3...An,C1,C2,C3...Cn
(1<=Ai<=100000,1<=Ci<=1000). The last test case is followed
by two zeros.
Output
Sample Input
3 10
1 2 4 2 1 1
2 5
1 4 2 1
0 0
Sample Output
8
4
题意:题目给定 n种 硬币,第i种价值为 A[i],数量为C[i] ,问这些硬币能够组成1-m中哪些硬币。 分析:这题去年在hdu 2844上面过了,利用分组背包每次将每种物品分割,分割完一次马上进行就进行 01 背包,这样处理的速度就比所有的都分割完后再进行01背包要快许多。。但是没想到poj的数据如此变态。。然后看了网上的题解,恍然大悟。。原来当A[i]*C[i]>=m时,我们就可以将第i件物品看成无穷件了。。然后只要进行完全背包就OK。 hdu 2844 AC代码:
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
using namespace std; int a[],b[],value[],f[];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF&&n>)
{
int wi=;
memset(f,,sizeof(f));
for(int i=; i<n; i++)
{
scanf("%d",&a[i]);
}
for(int i=; i<n; i++)
{
scanf("%d",&b[i]);
}
for(int i=; i<n; i++)
{
int sum=;
int w=b[i],v=a[i];
while(w>)
{
if(w>=sum)
{
value[wi]=sum*v;
w=w-sum;
sum=sum*;
}
else
{
value[wi]=w*v;
w-=w;
} for(int j=m; j>=value[wi]; j--)
f[j]=max(f[j],f[j-value[wi]]+value[wi]);
wi++;
} }
int ans=;
for(int i=; i<=m; i++)
{
if(f[i]==i) ans++;
}
printf("%d\n",ans); }
return ;
}
poj 1742 AC代码:
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
#define N 1005
#define M 100005
using namespace std; int A[],C[],W[N];
bool dp[M];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF,n+m)
{
memset(dp,,sizeof(dp));
for(int i=; i<=n; i++)
{
scanf("%d",&A[i]);
}
for(int i=; i<=n; i++)
{
scanf("%d",&C[i]);
}
int k = ;
dp[]=; ///初始化0元是存在的
int ans = ;
for(int i=; i<=n; i++)
{
if(C[i]*A[i]>=m) { ///如果A[i]*C[i] >= m,我们可以将其当成完全背包问题处理,因为k*A[i]不能超过
///m,超过了就可以看成"无限"了.
for(int v = A[i];v<=m;v++){
if(!dp[v]&&dp[v-A[i]]) {
dp[v]=true;
ans++;
}
}
}else{
int t = ;
while(C[i]>){
if(C[i]>=t){
W[k]=A[i]*t;
C[i]-=t;
t = t<<;
}else{W[k]=A[i]*C[i];C[i]=;}
for(int v=m;v>=W[k];v--){
if(!dp[v]&&dp[v-W[k]]) { ///当dp[v-W[k]]存在时才能够推导出dp[v]
dp[v]=true;
ans++;
}
}
k++;
}
}
}
printf("%d\n",ans); }
return ;
}
poj 1742(好题,楼天城男人八题,混合背包)的更多相关文章
- poj 1737男人八题之一 orz ltc
这是楼教主的男人八题之一.很高兴我能做八分之一的男人了. 题目大意:求有n个顶点的连通图有多少个. 解法: 1. 用总数减去不联通的图(网上说可以,我觉得时间悬) 2. 用动态规划(数学递推) ...
- poj 1741 楼教主男人八题之中的一个:树分治
http://poj.org/problem? id=1741 Description Give a tree with n vertices,each edge has a length(posit ...
- POJ1742 Coins(男人八题之一)
前言 大名鼎鼎的男人八题,终于见识了... 题面 http://poj.org/problem?id=1742 分析 § 1 多重背包 这很显然是一个完全背包问题,考虑转移方程: DP[i][j]表示 ...
- Cogs 1714. [POJ1741][男人八题]树上的点对(点分治)
[POJ1741][男人八题]树上的点对 ★★★ 输入文件:poj1741_tree.in 输出文件:poj1741_tree.out 简单对比 时间限制:1 s 内存限制:256 MB [题目描述] ...
- poj 1743 男人八题之后缀数组求最长不可重叠最长重复子串
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14874 Accepted: 5118 De ...
- 博弈论(男人八题):POJ 1740 A New Stone Game
A New Stone Game Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5694 Accepted: 3119 ...
- nyoj137 取石子(三) 楼教主男人八题之一
思路:一堆时,N态.两堆时,当两堆数量相同,P态,不同为N态.三堆时,先手可以变成两堆一样的,必胜N态. 此时可以总结规律:堆数为偶数可能且石子数都是两两相同的,为P态.分析四堆时,当四堆中两两数量一 ...
- 新男人八题---AStringGame
终于完成进度男人1/8,为了这题学了sam= = 题意先有一个串,n个子串,两个人轮流每次在子串上加字符,要求加完后还是原串的子串,最后不能加的就是输者,求赢的人 解法:sam之后在构造的状态图上跑s ...
- codeforces水题100道 第十八题 Codeforces Round #289 (Div. 2, ACM ICPC Rules) A. Maximum in Table (brute force)
题目链接:http://www.codeforces.com/problemset/problem/509/A题意:f[i][1]=f[1][i]=1,f[i][j]=f[i-1][j]+f[i][j ...
随机推荐
- MyBatis之二级缓存
二级缓存与一级缓存区别:二级缓存的范围更大,多个sqlSession可以共享一个UserMapper的二级缓存区域. 每一个mapper都有一个自己的二缓存区域(按namespace区分),两个map ...
- [LOJ10186]任务安排
link 试题分析 一道斜率优化的dp 易得方程$f[j]=(S+t[i])\times c[j]+f[i]-t[i]\times c[i]+s\times c[n]$,为什么要写成这要,因为这样其实 ...
- django error: DisallowedHost: Invalid HTTP_HOST header: ''. You may need to add u'' to ALLOWED_HOST
测试环境: [root@nanx-lli ~]# lsb_release -aLSB Version: :core-4.1-amd64:core-4.1-noarchDistributor ID: C ...
- Android线程池ThreadPoolExecutor
阿里巴巴Android开发手册[强制]新建线程时,必须通过线程池提供(AsyncTask 或者 ThreadPoolExecutor或者其他形式自定义的线程池),不允许在应用中自行显式创建线程说明:使 ...
- stout代码分析之十一:hashmap和multihashmap
hashmap是std::unordered_map的子类,前者对后者的接口做了进一步封装. hashmap的移动构造函数: hashmap(std::map<Key, Value>&am ...
- idea 多模块引用
roma-server 引用common-utils的类,所以在roma-server 的pom中配置 <dependency> <groupId>org.springfram ...
- 本地更新代码同步至github仓库
昨晚在家里写了一个demo放到github上,然后今天晚上来公司准备搞一下,但是git pull下来在本地修改之后push不到github上,然后发现公司电脑上并没有access权限,然后想起来还没配 ...
- java通过各种类型驱动连接数据库
常见数据库驱动实现类:JDBC-ODBC:sun.jdbc.odbc.JdbcOdbcDriver Oracle:oracle.jdbc.driver.OracleDriver MySQL:com.m ...
- JS把内容动态插入到DIV
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DT ...
- asp.net SimpleImpersonation使用身份模拟访问局域网共享目录
mvc中默认账户的权限很低,缺省情况下,ASP.NET应用程序以本机的ASPNET帐号运行,该帐号属于普通用户组,权限受到一定的限制,以保障ASP.NET应用程序运行的安全.但是有时需要某个ASP.N ...