POJ 2063 Investment (完全背包)
Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u
Description
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 3000 |
400 250 |
With
a capital of e10 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 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
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 算法思考:
很有意思的一道完全背包问题,只要想到是背包问题,基本就能解决了!
#include <stdio.h>
#include <string.h> #define N 50006 int dp[N], w[N], p[N]; int max(int a, int b)
{
return a > b ? a : b;
} int main()
{
int t;
int aa, yy, d;
int n, v, i, k; scanf("%d", &t); while(t--)
{
scanf("%d %d", &aa, &yy);
scanf("%d", &d); for(i=1; i<=d; i++)
{
scanf("%d %d", &p[i], &w[i] );//花费 收入
p[i] = p[i]/1000;
}
n = d;//种类 while(yy--)
{
memset(dp, 0, sizeof(dp));
v = aa/1000;
for(i=1; i<=n; i++)
{
for(k=0; k<=v; k++)
{
if( k>=p[i] )
dp[k] = max (dp[k], dp[k-p[i]]+w[i] );
}
} aa = aa+dp[v]; }
printf("%d\n", aa );
}
}
POJ 2063 Investment (完全背包)的更多相关文章
- POJ 2063 Investment 完全背包
题目链接:http://poj.org/problem?id=2063 今天果然是卡题的一天.白天被hdu那道01背包的变形卡到现在还没想通就不说了,然后晚上又被这道有个不大也不小的坑的完全背包卡了好 ...
- POJ 2063 Investment 滚动数组+完全背包
题目链接: http://poj.org/problem?id=2063 题意: 你现在有现金m元,你要做n年的存款投资,给你k种投资方式,每种需要现金vi元,能获得xi元的理论,一年到期后你要利用拿 ...
- poj 2063 Investment ( zoj 2224 Investment ) 完全背包
传送门: POJ:http://poj.org/problem?id=2063 ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...
- poj 2063 Investmen 完全背包
这个题的想法不难,两个点: 1 是完全背包 2 是考虑/1000,降低复杂度 但是提交的时候反复的wa,最后找问题原来是dp开小了,可是dp本来开1005,后来开到100030过了.哎,如果没有时 ...
- [POJ 2063] Investment (动态规划)
题目链接:http://poj.org/problem?id=2063 题意:银行每年提供d种债券,每种债券需要付出p[i]块钱,然后一年的收入是v[i],到期后我们把本金+收入取出来作为下一年度本金 ...
- poj 2063 Investment
题意:给定一个初始资金capital,然后给定d种投资方案,每种投资方案中有投资额value[i](是1000的倍数)和利息interest[i],每年的投资就可以拿到全部利息,然后累加起来继续投资利 ...
- 专题复习--背包问题+例题(HDU 2602 、POJ 2063、 POJ 1787、 UVA 674 、UVA 147)
*注 虽然没什么人看我的博客但我还是要认认真真写给自己看 背包问题应用场景给定 n 种物品和一个背包.物品 i 的重量是 w i ,其价值为 v i ,背包的容量为C.应该如何选择装入背包中的物品,使 ...
- poj 2063完全背包
题意:给出总资金和投资年份 ,n个股票 给出股票价格和其一年的利润.问如何选择能获得最大利润. 思路:股票可以重复选择,完全背包问题,完全背包也是从01背包衍生而行的,其主要区别在于中间那层循环的次序 ...
- POJ 1155 (树形DP+背包+优化)
题目链接: http://poj.org/problem?id=1155 题目大意:电视台转播节目.对于每个根,其子结点可能是用户,也可能是中转站.但是用户肯定是叶子结点.传到中转站或是用户都要花钱, ...
随机推荐
- TortoiseGit在github上创建工程
一.前期准备 TortoiseGit官网下载地址:http://code.google.com/p/tortoisegit/ git下载地址:https://git-scm.com/download/ ...
- C++ 关于类与对象在虚函数表上唯一性问题 浅析
[摘要] 非常多教材上都有介绍到虚指针.虚函数与虚函数表.有的说类对象共享一个虚函数表,有的说,一个类对象拥有一个虚函数表.还有的说,不管用户声明了多少个类对象,可是,这个VTABLE虚函数表仅仅有一 ...
- 微信小程序 - 非Form数据怎么发送到后端?
通过设置异步缓存,就可以做到 wx.setStorageSync('imgs',imglist); 最后的提交信息:
- 全国车辆违章查询API文档及demo
简介 聚合数据全国车辆违章API,目前已经支持300个左右的城市违章查询,已连接上万个APP.方便有车一族随时了解自己是否有过交通违章,避免因遗忘或逾期处理违章罚单而造成的不必要损失. API参考文档 ...
- 关于郭天祥51开发板无法烧敲代码问题的解决(Prolific USB-to-Serial Comm Port)
1. 事件背景: 因为使用了win8系统,之前购买的郭天祥C51开发板在通过一个两头都是usb口的下载线下载程序时出现了问题:下载工具stc isp无法连接到开发板上的串口,所以无法下载程序到c51开 ...
- socket连接和TCP连接的关系
我们在数据传输时,能够仅仅使用(传输层)TCP/IP协议,可是那样的话,假设没有应用层.便无法识别数据内容,假设想要使传输的数据有意义.则必须使用到应用层协议,应用层协议有非常多,比方HTTP.FTP ...
- 【转载】viewState详解
作者:Infinities Loop 概述 ViewState是一个被误解很深的动物了.我希望通过此文章来澄清人们对 ViewState的一些错误认识.为了达到这个目的,我决定从头到尾详细的描述一下整 ...
- PX4学习之-uORB简单体验
一.前言 最近项目使用到 CPU2 与 CPU0 之间的通信, 使用定时器传递消息到 CPU0 后, CPU0 需要将消息分发到不同的应用程序里面. PX4 里面使用的是 uORB 多线程/进程通信机 ...
- c# emit 实现类的代理
using System; using System.Linq; using System.Reflection; using System.Reflection.Emit; namespace Em ...
- Java异常 - Exception总结
这篇blog总结的比较详细了. 如下图所示,在Java中所有异常的基类均为Throwable类.有两个子类,分别为Exception和Error.其中Error主要由JVM处理,比如OutOfMemo ...