您的位置:首页 > 其它

POJ 2063 Investment

2016-08-13 20:16 274 查看
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

3000 400

250

With a capital of e10 000 one could buy two bonds of 4000,givingayearlyinterestof800. Buying two bonds of 3000,andoneof 4 000 is a better idea, as it gives a yearly interest of 900.Aftertwoyearsthecapitalhasgrownto 11 800, and it makes sense to sell a 3000oneandbuya 4 000 one, so the annual interest grows to 1050.Thisiswherethisstorygrowsunlikely:thebankdoesnotchargeforbuyingandsellingbonds.Nextyearthetotalsumis 12 850, which allows for three times 4000,givingayearlyinterestof 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.

【题目分析】

题目理解之后就是一道简单的完全背包。

【代码】

#include <cstdio>
#include <cstring>
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj