POJ2063 Investment 【完全背包】

Investment

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

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 $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

题意:给定一个容量为weight的背包而且一开始本金为weight,再给定n个物品,每种物品的重量是w[i],价值是v[i],数量无限。将这n种物品有选择的装入背包中,使背包价值最大,一年后本金会加上背包中的价值,然后重新分配背包物品,给定年份m,求m年后本金是多少。

题解:由于每种物品的重量都是1000的倍数,所以可以将每种物品和背包容量/=1000以减少内存消耗,由于1000000*1.1^40 / 1000 = 45000多,所以dp数组开到5万就足够了,剩下的就是完全背包问题了,将每年获得的最大价值加入本金中,最后再输出本金即可。状态转移方程:dp[i][j] = max(dp[i-1][j-k*w[i] + k*v[i]),0<=k<=totalWeight/v[i];压缩成一维数组后内层循环顺序。

#include <stdio.h>
#include <string.h>
#define maxn 50000

int dp[maxn], w[42], v[42];

int main()
{
    int t, totalWeight, years, i, j, capital, n;
    scanf("%d", &t);
    while(t--){
        scanf("%d%d", &totalWeight, &years);
        capital = totalWeight;
        scanf("%d", &n);
        for(i = 1; i <= n; ++i){
            scanf("%d%d", &w[i], &v[i]);
            w[i] /= 1000;
        }
        while(years--){
            totalWeight = capital / 1000;
            memset(dp, 0, sizeof(dp));
            for(i = 1; i <= n; ++i){
                for(j = w[i]; j <= totalWeight; ++j){
                    if(dp[j] < dp[j - w[i]] + v[i])
                        dp[j] = dp[j - w[i]] + v[i];
                }
            }
            capital += dp[totalWeight];
        }
        printf("%d\n", capital);
    }
    return 0;
}
时间: 2024-11-06 11:11:12

POJ2063 Investment 【完全背包】的相关文章

poj2063投资--完全背包

题意: 有d种股票,每种股票有一个购买钱数,和收益,你有本金C,year年之后,可以获得最大的投资收益是多少? 分析: 那么这里,我们可以知道每种股票可以购买无限次,那么这里可以看出是完全背包问题,可以把本金C看做背包.但是需要处理一下(等会说这个问题) 我们单独看看一年的收益,分析dp过程: dp[i][j] 表示考虑第i种股票,使用j 这么多钱的时候的最大收益.通过之前的 白话背包之完全背包 我们可以了解这里可以优化为一维数组 但是考虑到dp数组不能太大,同时股票售价都是1000的倍数,我们

hdu 1963 Investment 完全背包

hdu题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1963 poj题目链接:http://poj.org/problem?id=2063 完全背包 每过一年就重新做一次完全背包 注意到本钱非常大 不能直接暴力 看到基金的成本都是1000的倍数(注意它没说本钱什么的也是1000的倍数) 就要灵活对f[]进行处理了 最后一个问题是 f[]应该给多大 第一次我给了1010然后跪了 才发现只是说本金不超过一百万 注意到一个条件 利息不会超过10% 所以1

POJ 2063 Investment 完全背包

题目链接:http://poj.org/problem?id=2063 今天果然是卡题的一天.白天被hdu那道01背包的变形卡到现在还没想通就不说了,然后晚上又被这道有个不大也不小的坑的完全背包卡了好久.这题主要是说让你选择不同的债券(每种债券的费用和年利率的属性),然后n年后使得本金最大,如果仅仅是问一年的话就是个裸完全背包的题了,不过它是n年,每年得到的总利息都会加入到本金中变为下一年的本金,知道了这个后就很好处理了,在这道题里每年变化的本金就是背包容量,然后债券那两个属性就是物品的费用(o

poj2063 Investment

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 m

hdu 1963 Investment 多重背包

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1963 //多重背包 #include <cstdio> #include <cstring> #include <iostream> using namespace std; const int maxn = 1000000 + 10; #define N 15 long long dp[maxn], ans; int c[N], w[N], V; void Pack(in

poj分类解题报告索引

图论 图论解题报告索引 DFS poj1321 - 棋盘问题 poj1416 - Shredding Company poj2676 - Sudoku poj2488 - A Knight's Journey poj1724 - ROADS(邻接表+DFS) BFS poj3278 - Catch That Cow(空间BFS) poj2251 - Dungeon Master(空间BFS) poj3414 - Pots poj1915 - Knight Moves poj3126 - Prim

POJ 2063 Investment

POJ 2063 Investment 完全背包 fakeDescription: 吃土魔法少女经济不景气辣! 变出借来了为1000的倍数的钱打算去投资股票辣! (顺便吃土少女说她最多只能借来1000000元)告诉你吃土少女将会吃土投资几年以及每种股票的年收益和每一股的价格 现在吃土少女任命你为投资顾问制定每年的投资方案最大化收益.吃土少女不关心你怎么买的.只需要你写个程序告诉她她最后持有多少财富.吃土少女等着你来拯救! 以上题目描述都是我口胡的.233.不过意思对了就行了233 由于每年有一次

背包 [HDU 1963] Investment

由于HDU上这个题目显示有问题,不好复制,所以从虚拟OJ上复制了 Investment Time Limit:10000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Description John never knew he had a grand-uncle, until he received the notary??s letter. He learned that his late grand-uncle had

POJ 2063 Investment (完全背包)

A - Investment Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2063 Description John never knew he had a grand-uncle, until he received the notary's letter. He learned that his late grand-uncle