zoukankan      html  css  js  c++  java
  • POJ 2063 Investment(完全背包)

    Q: 每年一个完全背包?

    A: YES

    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

    思路:

    1. 每年一个完全背包, 背包的总容量每年一变

    2. 投资的钱数总是 1000 的倍数, 所以可以让背包容量和投资钱数都除以 1000 以减小数据规模

    代码:

    #include <iostream>
    using namespace std;
    int stbond, year;
    int d;
    int w[11], v[11];
    int dp[150000];
    int solve_dp() {	// 完全背包
    	for(int i = 0; i < year; i ++) {	// 遍历年
    		int V = stbond/1000;
    		memset(dp, 0, sizeof(int)*(V+10));
    		for(int j = 0; j < d; j ++) {	// 遍历物品
    			for(int k = w[j]; k <= V; k ++) {
    				dp[k] = max(dp[k], dp[k-w[j]]+v[j]);
    			}
    		}
    		stbond = dp[V] + stbond;
    	}
    	return stbond;
    }
    
    int main() {
        freopen("E:\Copy\ACM\测试用例\in.txt", "r", stdin);
    	
    	int testcase;
    	cin >> testcase;
    	while(testcase--) {
    		scanf("%d%d%d", &stbond, &year, &d);
    		for(int i = 0; i < d; i ++) {
    			scanf("%d%d", &w[i], &v[i]);
    			w[i] /= 1000;
    		}
    		// mainfunc
    		cout << solve_dp() << endl;
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    git提交代码五部曲
    菜单树jstree.js插件几个主要事件汇总
    创维电视加mac过滤后连不了wifi问题(路由器为TP-link)
    Mysql和Navicat for MySQL本地运行.sql文件
    MySQL下载与安装
    js一个区域拖拽到另一个区域,拖拽后还可以排序。知识点:js插件Jquery-UI:拖拽组件draggable,放置组件droppable,排序组件sortable
    echarts基本应用-更改坐标轴文字样式、轴名称、轴刻度、轴线、轴网格、曲线(折线图)、柱体上面显示值(柱状图),鼠标悬浮提示
    boostrap的时间插件daterangepicker.js之单日期,精确到秒
    echarts用法之点击事件(圆柱体例子)
    注册界面和功能
  • 原文地址:https://www.cnblogs.com/xinsheng/p/3463623.html
Copyright © 2011-2022 走看看