Problem Description
John never knew he had a grand-uncle, until he received the notary’s letter. He learned that his late grand-uncle had gathered a lot of money, somewhere in South-America, and that John was the only inheritor.

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.

 
Input
The first line contains a single positive integer N which is the number of test cases. The test cases follow.

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.

 
Output
For each test case, output – on a separate line – the capital at the end of the period, after an optimal schedule of buying and selling.
 
Sample Input
1
10000 4
2
4000 400
3000 250
 
Sample Output
14050
 

题意:给出初始资金,还有年数,然后给出每个物品的购买价格与每年获得的利益,要求在给出的年份后所能得到的最大本利之和。

思路:因为每种物品可以多次购买,可以看做是完全背包的题目,但是要注意的是,由于本金可能会很大,所以我们要对背包的大小进行压缩,值得注意的是,题目已经说了本金与物品的购买价格都是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(完全背包)的更多相关文章

  1. POJ2063 Investment 【全然背包】

    Investment Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 8019   Accepted: 2747 Descri ...

  2. poj2063 Investment(多次完全背包)

    http://poj.org/problem?id=2063 多次完全背包~ #include <stdio.h> #include <string.h> #define MA ...

  3. POJ 2063 Investment 完全背包

    题目链接:http://poj.org/problem?id=2063 今天果然是卡题的一天.白天被hdu那道01背包的变形卡到现在还没想通就不说了,然后晚上又被这道有个不大也不小的坑的完全背包卡了好 ...

  4. hdu 1963 Investment 多重背包

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1963 //多重背包 #include <cstdio> #include <cstr ...

  5. poj2063 Investment

    http://poj.org/problem?id=2063 首先总结一下:总的来说通过这题我深深感觉到了自己的不足,比赛时思维很受限,...面对超时,没有想到好的解决方案. 题意:给出初始资金,还有 ...

  6. POJ2063【完全背包】

    题意: 给一个初始的钱,年数, 然后给出每个物品的购买价格 与 每年获得的利益, 求在给出的年份后手上有多少钱. 思路: 背包重量还是资金. dp[0]=初始资金: 重物的重量是他的价格,获利是价值. ...

  7. poj 2063 Investment ( zoj 2224 Investment ) 完全背包

    传送门: POJ:http://poj.org/problem?id=2063 ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...

  8. poj分类解题报告索引

    图论 图论解题报告索引 DFS poj1321 - 棋盘问题 poj1416 - Shredding Company poj2676 - Sudoku poj2488 - A Knight's Jou ...

  9. hdu1963 完全背包(数据压缩)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1963 注意:题中有一句话说债券的价钱都是1000的倍数,我之前没看到这句话,写的完全背包, ...

随机推荐

  1. JQuery window、document、 body

    我电脑屏幕分辨率:1440 * 900   最大化浏览器,刷新浏览器 alert($(window).width() + "---" + $(window).height()); ...

  2. mysql中timestamp,datetime,int类型的区别与优劣

    转载请注明来自 souldak,微博: @evagle 以下内容 整合筛选自互联网: int 1. 占用4个字节 2. 建立索引之后,查询速度快 3. 条件范围搜索可以使用使用between 4. 不 ...

  3. 有关UNICODE、ANSI字符集和相关字符串操作

    Q UNICODE字符串如何显示 A 如果程序定义了_UNICODE宏直接用 WCHAR *str=L"unicodestring"; TextOut(0,0,str); 否则就需 ...

  4. linux ssh 不用密码自动登录的几种方法

    1. 自动ssh/scp方法== A为本地主机(即用于控制其他主机的机器) ;B为远程主机(即被控制的机器Server), 假如ip为192.168.60.110;A和B的系统都是Linux 在A上运 ...

  5. kernal linear regression

  6. 经典排序算法(Java实现)

    以下程序均将数据封装于DataWrap数据包装类中,如下所示: //数据包装类 class DataWrap implements Comparable<DataWrap> { int d ...

  7. HTTP的头部

    if($this->GetHead("http-edition")=="HTTP/1.1") $httpv = "HTTP/1.1"; ...

  8. Swift应用开源项目推荐

    1. 风靡全球的2048 2014年出现了不少虐心的小游戏,除了名声大噪的Flappy Bird外,最风靡的应该就是2048了.一个看似简单的数字叠加游戏,却让玩的人根本停不下来,朋友圈还一度被晒分数 ...

  9. Java基础笔记-抽象,继承,多态

    抽象类: abstract修饰 抽象方法必须定义在抽象类中,抽象类不能创建对象. 在抽象方法中可以不定义抽象方法,作用是:让该类不能建立对象. 特点是: 1.定义在抽象类中 2.方法和类都用abstr ...

  10. JavaScript中的计时器原理

    理解John Resig 在 How JavaScript Timers Work. 原理分析 timer(setInterval,setTimeout)有一个很重要的概念,时间延迟的长短是不稳定的. ...