zoukankan      html  css  js  c++  java
  • PAT (Top Level)1002. Business DP/背包

    As the manager of your company, you have to carefully consider, for each project, the time taken to finish it, the deadline, and the profit you can gain, in order to decide if your group should take this project. For example, given 3 projects as the following:

    • Project[1] takes 3 days, it must be finished in 3 days in order to gain 6 units of profit.
    • Project[2] takes 2 days, it must be finished in 2 days in order to gain 3 units of profit.
    • Project[3] takes 1 day only, it must be finished in 3 days in order to gain 4 units of profit.

      You may take Project[1] to gain 6 units of profit. But if you take Project[2] first, then you will have 1 day left to complete Project[3] just in time, and hence gain 7 units of profit in total. Notice that once you decide to work on a project, you have to do it from beginning to the end without any interruption.

      Input Specification:

      Each input file contains one test case. For each case, the first line gives a positive integer N(<=50), and then followed by N lines of projects, each contains three numbers P, L, and D where P is the profit, L the lasting days of the project, and D the deadline. It is guaranteed that L is never more than D, and all the numbers are non-negative integers.

      Output Specification:

      For each test case, output in a line the maximum profit you can gain.

      Sample Input:
      4
      7 1 3
      10 2 3
      6 1 2
      5 1 1
      
      Sample Output:
      18

    题解:先按deadline排序,把时间作为背包就行了。

     1 #include <stdio.h>
     2 #include <iostream>
     3 #include <string.h>
     4 #include <algorithm>
     5 using namespace std;
     6 
     7 struct sion
     8 {
     9     int v, c, d;
    10 }a[100];
    11 
    12 int cmp(sion a, sion b)
    13 {
    14     if(a.d!= b.d)
    15         return a.d < b.d;
    16     else 
    17         return a.c < b.c;
    18 }
    19 
    20 
    21 int dp[55][100000];
    22 int main()
    23 {
    24     int n;
    25     int sum ,ma ;
    26     while(cin >> n)
    27     {
    28         sum = 0;
    29         for(int i = 1; i <= n; i++)
    30         {
    31             cin >> a[i].v >> a[i].c>> a[i].d;
    32             sum += a[i].c;
    33             dp[i][0] = 0;    
    34         }
    35         sort(a + 1, a + n + 1, cmp);
    36         ma = 0;
    37         dp[0][0] = 0;
    38         for(int i = 1; i <= n; i++ )
    39         {
    40             for(int j = 1; j <= sum; j++)
    41             {
    42                 if(j <= a[i].d)
    43                 {
    44                     dp[i][j] = max(dp[i-1][j-a[i].c]+a[i].v, dp[i-1][j]);
    45                 }
    46                 else
    47                 {
    48                     dp[i][j] = a[i].v;
    49                 }
    50                 ma = max(ma, dp[i][j]);
    51             }
    52         }
    53         
    54         printf("%d
    ", ma);
    55         
    56     }
    57 }
  • 相关阅读:
    python 魔法方法
    wfst的compose算法
    文法和语言,理解克林闭包
    openfst常用命令
    Longest Substring Without Repeating Characters
    xgboost 实践
    决策树学习
    OPC UA的监控项、订阅、和通知
    限流及常用算法
    本体论与OWL
  • 原文地址:https://www.cnblogs.com/Yumesenya/p/5708411.html
Copyright © 2011-2022 走看看