poj 2063 Investment(dp背包)

【题目大意】:开始时有amount数量的钱,现在要去买d种股票,每一种股票有一个价值和年收益(这里注意(The value of a bond is always a multiple of $1 000.),即股票的价格是1000的倍数),投资时间为year年,如何使得投资的收益最大.

【解题思路】:完全背包。注意除个1000~~一开始没除,后来发现了,结果原来数组开太大又忘记改,结果因为memset还是TLE了一次...中招!!

【代码】:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <string>
#include <cctype>
#include <map>
#include <iomanip>
                   
using namespace std;
                   
#define eps 1e-8
#define pi acos(-1.0)
#define inf 1<<30
#define pb push_back
#define lc(x) (x << 1)
#define rc(x) (x << 1 | 1)
#define lowbit(x) (x & (-x))
#define ll long long

int w[12],v[12],dp[50000];
int n,m,y;

int solve_dp(int tmp){
    tmp=n/1000;
    memset(dp,0,sizeof(dp));
    for(int i=1; i<=m; i++)
        for(int j=w[i]; j<=tmp; j++)
            dp[j]=max(dp[j],dp[j-w[i]]+v[i]);
    return dp[tmp];
}

int main(){
    int T;
    cin >> T;
    while(T--){
         scanf("%d%d",&n,&y);
         scanf("%d",&m);
         for(int i=1; i<=m; i++){
             scanf("%d%d",&w[i],&v[i]);
             w[i]/=1000;
         }
         for(int i=1; i<=y; i++)
            n+=solve_dp(n);
         printf("%d\n",n);
    }
    return 0;
}   


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值