Poj2063 investment [full backpack]

Source: Internet
Author: User

Investment
Time limit:1000 ms   Memory limit:30000 K
Total submissions:8019   Accepted:2747

Description

John never knew he had a grand-uncle, until he got 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 wocould 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 wowould 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 cocould 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 launch 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

110000 424000 4003000 250

Sample output

14050
Assume that a backpack with weight capacity is given and the principal is weight at the beginning. Then, N items are given. The weight of each item is W [I], and the value is V [I]. unlimited quantity. These n items are packed in a backpack to maximize the value of the backpack. After one year, the principal will add the value in the backpack, and then re-allocate the backpack item, given the year m, calculate the principal amount in m years. Question: because the weight of each item is a multiple of 1000, you can set the size of each item and backpack to/= 1000 to reduce memory consumption. Because 1000000*1.1 ^ 40/1000 = more than 45000, therefore, it is enough to open the DP array to 50 thousand, and the rest is a complete backpack problem. Add the maximum value obtained every year to the Fund, and then output the principal. State transition equation: DP [I] [J] = max (DP [I-1] [J-K * W [I] + K * V [I]), 0 <= k <= totalweight/V [I]; compress the data into a one-dimensional array and then cyclically in the inner layer.

#include <stdio.h>#include <string.h>#define maxn 50000int 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;}


Poj2063 investment [full backpack]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.