Coins

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5936    Accepted Submission(s): 2458

Problem Description
Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One day Hibix opened purse and found there were some coins. He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact price(without change) and he known the price would not more than m.But he didn't know the exact price of the watch.

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
The 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
For each test case output the answer on a single line.
 
Sample Input
3 10
1 2 4 2 1 1
2 5
1 4 2 1
0 0
 
Sample Output
8
4
 
Source

代码: 简单的多重背包,但是恶心的地方是m居然可以为负数....是不是很伤不起呀...哈哈

 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
const int maxn=;
struct price
{
int val;
int num;
};
price sta[maxn];
int dp[];
int main()
{
int n,m,i,j,ans;
while(scanf("%d%d",&n,&m),n+m)
{ for(i=;i<n;i++)
scanf("%d",&sta[i].val);
for(ans=i=;i<n ;i++)
{
scanf("%d",&sta[i].num);
ans+=sta[i].val*sta[i].num ;
}
int tem=ans<m?ans:m;
if(tem<)
{
printf("0\n");
continue;
}
memset(dp,-,sizeof(dp[])*(tem+));
dp[]=;
for(i=;i<n;i++)
{
if(sta[i].num*sta[i].val>=tem) //完全背包
{
for(j=sta[i].val ;j<=tem;j++)
{
if(dp[j-sta[i].val]>-&&dp[j]<dp[j-sta[i].val]+sta[i].num)
dp[j]=dp[j-sta[i].val]+sta[i].num;
}
}
else
{
int k=;
while(sta[i].num>=k)
{
for(j=tem ;j>=k*sta[i].val ; j--)
{
if(dp[j-k*sta[i].val]>-&&dp[j]<dp[j-k*sta[i].val]+k)
dp[j]=dp[j-k*sta[i].val]+k;
}
sta[i].num-=k;
k<<=;
} for(j=tem ;j>=sta[i].num ;j--)
{
if(j<sta[i].num*sta[i].val) break;
if(dp[j-sta[i].num*sta[i].val]>-&&dp[j]<dp[j-sta[i].num*sta[i].val]+sta[i].num)
dp[j]=dp[j-sta[i].num*sta[i].val]+sta[i].num;
} }
}
ans=;
for(i=;i<=tem;i++)
{
if(dp[i]!=-)ans++;
}
printf("%d\n",ans);
}
return ;
}

HDUOJ-------2844Coins的更多相关文章

  1. hduoj 1455 && uva 243 E - Sticks

    http://acm.hdu.edu.cn/showproblem.php?pid=1455 http://uva.onlinejudge.org/index.php?option=com_onlin ...

  2. hduoj 4712 Hamming Distance 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...

  3. hduoj 4706 Herding 2013 ACM/ICPC Asia Regional Online —— Warmup

    hduoj 4706 Children's Day 2013 ACM/ICPC Asia Regional Online —— Warmup Herding Time Limit: 2000/1000 ...

  4. hdu-oj 1874 畅通工程续

    最短路基础 这个题目hdu-oj 1874可以用来练习最短路的一些算法. Dijkstra 无优化版本 #include<cstdio> #include<iostream> ...

  5. C#版 - HDUoj 5391 - Zball in Tina Town(素数) - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. HDUoj 5 ...

  6. C++版 - HDUoj 2010 3阶的水仙花数 - 牛客网

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C++版 - ...

  7. HDUOJ题目HTML的爬取

    HDUOJ题目HTML的爬取 封装好的exe/app的GitHub地址:https://github.com/Rhythmicc/HDUHTML 按照系统选择即可. 其实没什么难度,先爬下来一个题目的 ...

  8. hduoj 1251 统计难题

    http://acm.hdu.edu.cn/showproblem.php?pid=1251 统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory ...

  9. hduoj 1286 找新朋友

    http://acm.hdu.edu.cn/showproblem.php?pid=1286 找新朋友 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

  10. hduoj 1285 确定比赛名次

    http://acm.hdu.edu.cn/showproblem.php?pid=1285 确定比赛名次 Time Limit: 2000/1000 MS (Java/Others) Memory ...

随机推荐

  1. 如何搭建 LNMP环境

    和LAMP不同的是LNMP中的N指的是是Nginx(类似于Apache的一种web服务软件)其他都一样.目前这种环境应用的也是非常之多. Nginx设计的初衷是提供一种快速高效多并发的web服务软件. ...

  2. matlab 使用OPENCV

    +cv就是 测试: h=cv.imread('c:\h.jpg');

  3. Solution Explorer 和 Source Control Explorer 的 View History 异同

    如果查看别人对代码的修改,你可能会非常烦恼与在 Solution Explorer 中看历史版本看不全,如下: 实际上,你想看到的是对于整个解决方案,全部的历史版本,那应该跑去 Source Cont ...

  4. Objective-C:动态绑定

    // Complex.h // 03-动态绑定 // // Created by ma c on 15/8/11. // Copyright (c) 2015年. All rights reserve ...

  5. 3D有向包围盒与球体碰撞的算法

    之前发的小游戏滚蛋躲方块中,用它来判断球体与立方体是否发生了碰撞. http://www.cnblogs.com/WhyEngine/p/3350012.html 现在发布下该算法: 有向包围盒OBB ...

  6. OTL使用指南

    1 OTL简介 OTL 是 Oracle, Odbcand DB2-CLI Template Library 的缩写,是一个C++编译中操控关系数据库的模板库,它目前几乎支持当前所有的各种主流数据库, ...

  7. Android动画-帧动画

    Android 平台提供了两种动画一种是 Frame动画,即顺序的播放事先做好的图像,与gif图片或者说跟放电影的原理相似,另一种是Tween动画,就是对场景里的对象不断的进行图像变化来产生动画效果( ...

  8. JavaScript操作XML (一)

    JavaScript操作XML是通过XML DOM来完成的.那么什么是XML DOM呢?XML DOM 是: 用于 XML 的标准对象模型 用于 XML 的标准编程接口 中立于平台和语言 W3C 的标 ...

  9. vim学习笔记(2)——vim配置

    记录vim的配置,随时更新 MacVim 安装: homebrew,安装位置:/usr/local/Cellar brew linkapps macvim--将macvim.app加入到Applica ...

  10. python 分词计算文档TF-IDF值并排序

    文章来自于我的个人博客:python 分词计算文档TF-IDF值并排序 该程序实现的功能是:首先读取一些文档,然后通过jieba来分词,将分词存入文件,然后通过sklearn计算每一个分词文档中的tf ...