HDU1963 && POJ2063:Investment(完全背包)
John did not need that much money for the moment. But he realized that it would be a good idea to store this capital in a safe place, and have it grow until he decided to retire. The bank convinced him that a certain kind of bond was interesting for him.
This kind of bond has a fixed value, and gives a fixed amount of yearly interest, payed to the owner at the end of each year. The bond has no fixed term. Bonds are available in different sizes. The larger ones usually give a better interest. Soon John realized that the optimal set of bonds to buy was not trivial to figure out. Moreover, after a few years his capital would have grown, and the schedule had to be re-evaluated.
Assume the following bonds are available:
Value Annual interest
4000 400
3000 250
With a capital of $10 000 one could buy two bonds of $4 000, giving a yearly interest of $800. Buying two bonds of $3 000, and one of $4 000 is a better idea, as it gives a yearly interest of $900. After two years the capital has grown to $11 800, and it makes sense to sell a $3 000 one and buy a $4 000 one, so the annual interest grows to $1 050. This is where this story grows unlikely: the bank does not charge for buying and selling bonds. Next year the total sum is $12 850, which allows for three times $4 000, giving a yearly interest of $1 200.
Here is your problem: given an amount to begin with, a number of years, and a set of bonds with their values and interests, find out how big the amount may grow in the given period, using the best schedule for buying and selling bonds.
The first line of a test case contains two positive integers: the amount to start with (at most $1 000 000), and the number of years the capital may grow (at most 40).
The following line contains a single number: the number d (1 <= d <= 10) of available bonds.
The next d lines each contain the description of a bond. The description of a bond consists of two positive integers: the value of the bond, and the yearly interest for that bond. The value of a bond is always a multiple of $1 000. The interest of a bond is never more than 10% of its value.
10000 4
2
4000 400
3000 250
题意:给出初始资金,还有年数,然后给出每个物品的购买价格与每年获得的利益,要求在给出的年份后所能得到的最大本利之和。
思路:因为每种物品可以多次购买,可以看做是完全背包的题目,但是要注意的是,由于本金可能会很大,所以我们要对背包的大小进行压缩,值得注意的是,题目已经说了本金与物品的购买价格都是1000的倍数,所以我们可以将他们都除以1000来进行压缩,然后就是一道完全背包模板题了。
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; struct node
{
int v,w;
}a[20]; int dp[100000]; int main()
{
int t,n,i,j,k,val,y;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&val,&y);
scanf("%d",&n);
for(i = 1;i<=n;i++)
{
scanf("%d%d",&a[i].v,&a[i].w);
a[i].v/=1000;//进行压缩
}
for(i = 1;i<=y;i++)
{
int s = val/1000;//每年本金都是上一年本金与利息之和
memset(dp,0,sizeof(dp));//每年都要重新存利息
for(j = 1;j<=n;j++)//完全背包
{
for(k = a[j].v;k<=s;k++)
{
dp[k]=max(dp[k],dp[k-a[j].v]+a[j].w);
}
}
val+=dp[s];//每年的最大本利和
}
printf("%d\n",val);
} return 0;
}
HDU1963 && POJ2063:Investment(完全背包)的更多相关文章
- POJ2063 Investment 【全然背包】
Investment Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8019 Accepted: 2747 Descri ...
- poj2063 Investment(多次完全背包)
http://poj.org/problem?id=2063 多次完全背包~ #include <stdio.h> #include <string.h> #define MA ...
- POJ 2063 Investment 完全背包
题目链接:http://poj.org/problem?id=2063 今天果然是卡题的一天.白天被hdu那道01背包的变形卡到现在还没想通就不说了,然后晚上又被这道有个不大也不小的坑的完全背包卡了好 ...
- hdu 1963 Investment 多重背包
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1963 //多重背包 #include <cstdio> #include <cstr ...
- poj2063 Investment
http://poj.org/problem?id=2063 首先总结一下:总的来说通过这题我深深感觉到了自己的不足,比赛时思维很受限,...面对超时,没有想到好的解决方案. 题意:给出初始资金,还有 ...
- POJ2063【完全背包】
题意: 给一个初始的钱,年数, 然后给出每个物品的购买价格 与 每年获得的利益, 求在给出的年份后手上有多少钱. 思路: 背包重量还是资金. dp[0]=初始资金: 重物的重量是他的价格,获利是价值. ...
- poj 2063 Investment ( zoj 2224 Investment ) 完全背包
传送门: POJ:http://poj.org/problem?id=2063 ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...
- poj分类解题报告索引
图论 图论解题报告索引 DFS poj1321 - 棋盘问题 poj1416 - Shredding Company poj2676 - Sudoku poj2488 - A Knight's Jou ...
- hdu1963 完全背包(数据压缩)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1963 注意:题中有一句话说债券的价钱都是1000的倍数,我之前没看到这句话,写的完全背包, ...
随机推荐
- Delphi 全面控制Windows任务栏
核心提示:使用Windows95/NT/98操作系统的用户知道:Windows正常启动后,在电脑屏幕下方出现一块 任务栏. 使用Windows95/NT/98操作系统的用户知道:Windows正常启动 ...
- docker 数据管理<1>
1. 挂载本地的目录到容器里: docker run -itd -v /data/:/data1 centos bash // -v 用来指定挂载目录, :前面的/data为本地目录,:后面的/dat ...
- CC++初学者编程教程(9) Windows8.1安装VS2013并捆绑QT与编程助手
我们在Windows8.1安装VS2013并捆绑QT与编程助手需要下列文件. 2. 在虚拟机中开启Windows8.1 3.然后选择VS2013的安装镜像. 4.将镜像复制到虚拟机. 5.我们装载这个 ...
- C++STL_max
template<class T> T max(T a,T b) { return a>b?a:b; }
- C4.5较ID3的改进
1.ID3选择最大化Information Gain的属性进行划分 C4.5选择最大化Gain Ratio的属性进行划分 规避问题:ID3偏好将数据分为很多份的属性 解决:将划分后数据集的个数考虑 ...
- Backward Digit Sums(暴力)
Backward Digit Sums Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5664 Accepted: 32 ...
- 关于写的Java书籍进展
大家好,去年说要写本Java书,近期就快出版了.眼下已经開始打印样书了,最快于本月中旬左右就能够在互动网www.china-pub.com上看到消息,其它各个站点何时会发售要看详细进货情况. 去年我预 ...
- 嵌入式OS入门笔记-以RTX为案例:六.RTX的任务调度
嵌入式OS入门笔记-以RTX为案例:六.RTX的任务调度 上一篇笔记介绍了一些绕开排程器(或调度程序,scheduler)来进行时间管理的一些小方法.这一篇详细介绍RTX的任务调度原理. RTX主要有 ...
- android 自定义ViewGroup和对view进行切图动画实现滑动菜单SlidingMenu[转]
http://blog.csdn.net/jj120522/article/details/8095852 示意图就不展示了,和上一节的一样,滑动菜单SlidingMenu效果如何大家都比较熟悉,在这 ...
- javascript高级知识分析——作为对象的函数
代码信息来自于http://ejohn.org/apps/learn/. 函数和对象是不是非常相似? var obj = {}; var fn = function(){}; console.log( ...