POJ2063 Investment (完全揹包)

Investment
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 9760 Accepted: 3398
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,givingayearlyinterestof 800. 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.
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 1000000),andthenumberofyearsthecapitalmaygrow(atmost40).Thefollowinglinecontainsasinglenumber:thenumberd(1<=d<=10)ofavailablebonds.Thenextdlineseachcontainthedescriptionofabond.Thedescriptionofabondconsistsoftwopositiveintegers:thevalueofthebond,andtheyearlyinterestforthatbond.Thevalueofabondisalwaysamultipleof 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

分析:
給了本金和不同種類基金的價格和能夠獲得的利息,求y年之後能夠獲得最大的本息和。
顯然是個完全揹包。
困難在於錢的總量大,空間不夠。
關鍵“The value of a bond is always a multiple of $1 000” 這個bond= =
是指本金和價格都是1000的倍數,所以可以除以1000再來計算。

唉英語學的不好就是傷。。
電腦沒電了就這樣吧(:з」∠)

//  Created by ZYD in 2015.
//  Copyright (c) 2015 ZYD. All rights reserved.
//

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <climits>
#include <string>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define Size 100000
#define ll long long
#define mk make_pair
#define pb push_back
#define mem(array) memset(array,0,sizeof(array))
typedef pair<int,int> P;
int n,y,x,k,sum;
int  v[20],c[20],m,mmax;
int dp[300000];
int main()
{
    freopen("in.txt","r",stdin);
    scanf("%d",&n);
    while(n--){
        scanf("%d %d",&m,&y);
        scanf("%d",&x);
        // cout<<x;
        sum=m;
        for(int i=1;i<=x;i++)
        {
            scanf("%d %d",&v[i],&c[i]);
            v[i]=v[i]/1000;
        }
        mmax=0;mem(dp);
        for(int i=1;i<=y;i++)
        {

            // cout<<m<<endl;
            m=sum/1000;
            // cout<<m<<endl;
            for(int k=1;k<=x;k++)
                for(int j=v[k];j<=m;j++){
                    dp[j]=max(dp[j],dp[j-v[k]]+c[k]);
                    // if(dp[j]>mmax) mmax=dp[j];
                }
            sum+=dp[m];

        }
        // cout<<m<<endl;
        printf("%d\n",sum);
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章