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. struts2中<welcome-file>index.action</welcome-file>直接设置action,404的解决方案

    这几天的项目页面的访问全部改为.action访问,在修改首页时遇到了问题.将web.xml文件中<welcome-file>index.action</welcome-file> ...

  2. tomcat配置manager

    tomcat-users.xml配置 <role rolename="manager-gui"/><user username="tomcat" ...

  3. Unity3d-Particle System 5.x系统的学习(四)

    Unity3d-Particle System 5.x系统的学习(四) 今天,我们来聊聊unity5.x的粒子系统和unity4.x粒子系统的区别. 我大致看了下,区别还是蛮多的,但是总体的粒子制作思 ...

  4. Ubuntu 16.04 重置密码

    忘记了你的Ubuntu用户密码,登录不了系统:不要紧,在Ubuntu上重置密码是很简单的,即使你忘记了用户名. #1 进入Recovery Mode Recovery Mode即恢复模式:在Grub启 ...

  5. 【BZOJ】【3437】小P的牧场

    DP/斜率优化 斜率优化基本题……等等,好像就没啥变化啊= = 嗯目测这题跟仓库建设差不多?写题的时候倒是没想这么多……直接推了公式. $$f[i]=min\{f[j]+cal(j,i)+a[i]\} ...

  6. Objective-C:字符串的反转Reverse

    OC中字符串的反转方式可以用两种方式来处理: 第一种:从头到尾取出字符串的每一个字符,然后将其从尾到头添加到可变的字符串中,最后输出即可. 第二种:将OC内部的字符串转换为C语言中的字符串,然后动态分 ...

  7. 理清Processor, Processor Sockets, Processor Cores, Logical Processors, Hyperthreading这些概念吧

    如果你只知道CPU这么一个概念,那么是无法理解CPU的拓扑的.事实上,在NUMA架构下,CPU的概念从大到小依次是:Node.Socket.Core.Logical Processor. 随着多核技术 ...

  8. Android中Dialog对话框的调用及监听

    Android中经常会需要在Android界面上弹出一些对话框提示用户,比如App的退出的时候都会有各种框来挽留你的心,支付宝的时候输入密码的密码框,非常常见及其实用的功能,类似于JS中的alter, ...

  9. ubuntu添加默认路由才可以访问网络

  10. iOS_2_button控制物体形变

    终于效果图: BeyondViewController.h // // BeyondViewController.h // 02_button控制物体形变 // // Created by beyon ...