• PAT-Top1002. Business (35)


      在一个项目的截止日期之前,如果工期有空闲则可能可以开展其他项目,提高效益。本题考查动态规划。数组dp[i][t]表示在截止时间为t时,前i个项目工作安排能够产生的最大收益,而前i个项目的截止时间都不大于t。

     1 //#include "stdafx.h"
     2 #include <iostream>
     3 #include <algorithm>
     4 #include <memory.h>
     5 
     6 using namespace std;
     7 
     8 struct node { // project struct
     9     int p, l, d; // p is the profit, l is the lasting days of the project, d is the deadline
    10 }pro[51];
    11 
    12 int cmp(node a, node b) { // sort rule
    13     return a.d < b.d;
    14 }
    15 
    16 int main() {
    17     int n;
    18     scanf("%d", &n);
    19 
    20     int i, maxd = 0;
    21     for (i = 1; i <= n; i++) {
    22         scanf("%d%d%d", &pro[i].p, &pro[i].l, &pro[i].d);
    23 
    24         if (pro[i].d > maxd) { // get the max deadline
    25             maxd = pro[i].d;
    26         }
    27     }
    28 
    29     sort(pro + 1, pro + n + 1, cmp);
    30 
    31     int** dp = new int*[n + 1];
    32     for (i = 0; i <= n; i++) {
    33         dp[i] = new int[maxd + 1];
    34         memset(dp[i], 0, sizeof(dp[i])); // initialization : set value zero
    35     }
    36 
    37     //printf("%d
    ", dp[0][0]);
    38     int j, t;
    39     for (i = 1; i <= n; i++) {
    40         for (j = 1; j <= maxd; j++) {
    41             t = min(j, pro[i].d) - pro[i].l; 
    42             // get the max profit
    43             if (t >= 0) { // if can plus current project to compare the profit
    44                 dp[i][j] = max(pro[i].p + dp[i - 1][t], dp[i - 1][j]);
    45             } else { // otherwise
    46                 dp[i][j] = dp[i - 1][j];
    47             }
    48         }
    49     }
    50 
    51     printf("%d
    ", dp[n][maxd]);
    52 
    53     for (i = 0; i <= n; i++) {
    54         delete[] dp[i];
    55     }
    56     delete[] dp;
    57 
    58     system("pause");
    59     return 0;
    60 }
    View Code

    参考资料

    PAT. 1002. Business (35)

  • 相关阅读:
    CrawlSpiders
    从抓取Tencent中学习Scrapy
    对象返回规范的url的两种方式的两种方式
    多对多关系的额外字段
    Django定时任务
    Scripy学习(一)
    Django开发博客一(搭建模型和准备数据)
    求并集
    求子集、交集
    java数学函数Math类中常用的方法
  • 原文地址:https://www.cnblogs.com/WJQ2017/p/7569199.html
Copyright © 2020-2023  润新知