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居然可以为负数....是不是很伤不起呀...哈哈

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. const int maxn=;
  5. struct price
  6. {
  7. int val;
  8. int num;
  9. };
  10. price sta[maxn];
  11. int dp[];
  12. int main()
  13. {
  14. int n,m,i,j,ans;
  15. while(scanf("%d%d",&n,&m),n+m)
  16. {
  17.  
  18. for(i=;i<n;i++)
  19. scanf("%d",&sta[i].val);
  20. for(ans=i=;i<n ;i++)
  21. {
  22. scanf("%d",&sta[i].num);
  23. ans+=sta[i].val*sta[i].num ;
  24. }
  25. int tem=ans<m?ans:m;
  26. if(tem<)
  27. {
  28. printf("0\n");
  29. continue;
  30. }
  31. memset(dp,-,sizeof(dp[])*(tem+));
  32. dp[]=;
  33. for(i=;i<n;i++)
  34. {
  35. if(sta[i].num*sta[i].val>=tem) //完全背包
  36. {
  37. for(j=sta[i].val ;j<=tem;j++)
  38. {
  39. if(dp[j-sta[i].val]>-&&dp[j]<dp[j-sta[i].val]+sta[i].num)
  40. dp[j]=dp[j-sta[i].val]+sta[i].num;
  41. }
  42. }
  43. else
  44. {
  45. int k=;
  46. while(sta[i].num>=k)
  47. {
  48. for(j=tem ;j>=k*sta[i].val ; j--)
  49. {
  50. if(dp[j-k*sta[i].val]>-&&dp[j]<dp[j-k*sta[i].val]+k)
  51. dp[j]=dp[j-k*sta[i].val]+k;
  52. }
  53. sta[i].num-=k;
  54. k<<=;
  55. }
  56.  
  57. for(j=tem ;j>=sta[i].num ;j--)
  58. {
  59. if(j<sta[i].num*sta[i].val) break;
  60. if(dp[j-sta[i].num*sta[i].val]>-&&dp[j]<dp[j-sta[i].num*sta[i].val]+sta[i].num)
  61. dp[j]=dp[j-sta[i].num*sta[i].val]+sta[i].num;
  62. }
  63.  
  64. }
  65. }
  66. ans=;
  67. for(i=;i<=tem;i++)
  68. {
  69. if(dp[i]!=-)ans++;
  70. }
  71. printf("%d\n",ans);
  72. }
  73. return ;
  74. }

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. java类与继承的执行顺序

    1.子类 public class TestClassLife extends TestClassLifeSuper { public TestClassLife() { System.out.pri ...

  2. Android Binder 设计与实现 - 设计篇

    关键词 Binder Android IPC Linux 内核 驱动 摘要 Binder是Android系统进程间通信(IPC)方式之一.Linux已经拥有管道,system V IPC,socket ...

  3. Java网络编程技术2

    3. UDP数据报通信 UDP通信中,需要建立一个DatagramSocket,与Socket不同,它不存在“连接”的概念,取而代之的是一个数据报包——DatagramPacket.这个数据报包必须知 ...

  4. mybatis映射文件遇到的小问题

    mybatis的映射文件插入操作时: 如果对应的属性是String类型的,那么一定要做空串的判断. 比如注册的时候,如果需要向数据库中插入一条记录时,对应的字段没有给他赋值,这个String类型的值传 ...

  5. std::tr1::function

    转自:https://www.cnblogs.com/qlee/archive/2011/07/04/2097594.html 在C++的TR1中(Technology Report)中包含一个fun ...

  6. 第十一章 自己实现一致性hash算法

    关于一致性hash算法的意义以及其相对于简单求余法(除数求余法)的好处,查看第六章 memcached剖析 注意:真实的hash环的数据结构是二叉树,这里为了简便使用了列表List 1.一致性hash ...

  7. java.net.URI 简介 文档 API

    URI 简介 文档地址:http://tool.oschina.net/apidocs/apidoc?api=jdk-zh public final class java.net.URI extend ...

  8. HDU 1495 很可乐 (DFS)

    题目链接:很可乐 解析:一个瓶子,容量为s.两个杯子,容量分别为n和m,问最少多少次倾倒才干将一瓶可乐均分为两份. 直接模拟每次的倾倒.然后递归求解. 能够加个预判的条件,要是s是奇数的时候,不管怎样 ...

  9. LINUX设备驱动程序笔记(五)中断处理

         <一> 中断处理流程例如以下: 1.发生中断时,CPU运行异常向量vector_irq的代码. 2.在vector_irq里面.终于会调用中断处理的总入口函数asm_do_IRQ ...

  10. 将Tp-link无线路由器桥接到Dlink无线路由器上

    笔者家中原有两台笔记本和两台IPad,通过一台Dlink无线路由器(型号DIR-612,以下简称Dlink)上网,Dlink以PPPOE方式连到小区宽带.一直还可以. 后来为了练习Linux,启用了一 ...