Algorithm: POJ 2063 investment (DP full backpack)

Source: Internet
Author: User

The main effect of the topic:

There are n kinds of bonds to buy, each price is a (A is a multiple of 1000), the annual interest is B. A person with a total of money tot (tot is a multiple of 1000), ask him in y years, the maximum can have how much money?

Ideas:

Since both tot and a are multiples of 1000, all can be reduced by 1000 times times in calculation, saving memory and time.

According to greedy thinking, every year with a full backpack to find the year's maximum available interest, and then the next year with the interest after the total money to continue to calculate ...

Code:

 #include <iostream> #include <queue> #include <stack> #include <cstdio> #include <cstr ing> #include <cmath> #include <map> #include <set> #include <string> #define MP Make_pai  
R #define SQ (x) ((x) * (x)) typedef int Int64;  
Const double PI = ACOs (-1.0);  
const int INF = 0X3F3F3F3F;  
      
using namespace Std;  
const int MOD = 1000000007;  
const int MAXN = 13;  
      
const int ADD = 1000*100;  
int tot, y,n;  
int C[MAXN], W[MAXN];  
      
      
int f[100030];  
    int main () {int ncase;  
    scanf ("%d", &ncase);  
        while (ncase--) {scanf ("%d%d%d", &tot, &y, &n);      
            for (int i=0; i<n; ++i) {scanf ("%d%d", &c[i], &w[i]);  
        C[i]/= 1000;  
            for (int i=0; i<y; ++i) {for (int j=0; j<=tot/1000; ++j) f[j] = 0; for (int j=0; j<n; ++j) {for (intV=C[J]; v<=tot/1000;  
            ++V) F[v] = max (F[v], f[v-c[j]]+w[j]);  
        } tot + = f[tot/1000];  
    printf ("%d\n", tot);  
return 0; }

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

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.