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. dom4j怎么获得指定名称的节点信息

    <?xml version="1.0" encoding="utf-8" ?> <MgUtil> <db_config> & ...

  2. Javascript时间以及格式化秒

    var now = new Date(); timer = $.timer(timeout, function () {     var sec_num = Math.ceil((now.getTim ...

  3. 理解Python命名机制

    理解Python命名机制 本文最初发表于恋花蝶的博客(http://blog.csdn.net/lanphaday),欢迎转载,但必须保留此声明且不得用于商业目的.谢谢. 引子 我热情地邀请大家猜测下 ...

  4. RuntimeError: Working outside of application context.

    flask执行错误: 问题:RuntimeError: Working outside of application context. 方法: from flask import Flask, cur ...

  5. OpenCV学习(32) 求轮廓的包围盒

    在OpenCV中,能够很方便的求轮廓包围盒.包括矩形,圆形,椭圆形以及倾斜的矩形(包围面积最小)集中包围盒.用到的四个函数是: Rect boundingRect(InputArray points) ...

  6. pat 1060 比较科学计数法

    trick: 1.前导0 如:000001,000.000001 2.出现0时也要按照科学计数法输出 e.g. 4 00000.00000 0001 NO 0.0000*10^0 0.1*10^1 3 ...

  7. 远程视频监控之应用篇(mjpg-streamer)

    这篇文章将主要结合源码介绍mjpg-streamer,使小伙伴们了解视频监控的实现. 一.移植 tar xvf mjpg-streamer-r63.tar.gz cd mjpg-streamer-r6 ...

  8. Android生成带图片的二维码

    一.问题描述 在开发中需要将信息转换为二维码存储并要求带有公司的logo,我们知道Google的Zxing开源项目就很好的帮助我们实现条形码.二维码的生成和解析,但带有logo的官网并没有提供demo ...

  9. 《iOS Human Interface Guidelines》——Search Bar

    搜索栏 搜索栏接收用户输入用于搜索的文本(例如以下,带有占位文本). API NOTE 查看UISearchBar学习怎样在你的代码中定义搜索栏.查看UISearchDisplayController ...

  10. POJ 1651 Multiplication Puzzle (区间DP)

    Description The multiplication puzzle is played with a row of cards, each containing a single positi ...