码迷,mamicode.com
首页 > 其他好文 > 详细

PAT-Top1002. Business (35)

时间:2017-09-21 17:59:21      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:delete   view   print   node   数组   play   .com   detail   pat   

  在一个项目的截止日期之前,如果工期有空闲则可能可以开展其他项目,提高效益。本题考查动态规划。数组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\n", 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\n", 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)

PAT-Top1002. Business (35)

标签:delete   view   print   node   数组   play   .com   detail   pat   

原文地址:http://www.cnblogs.com/WJQ2017/p/7569199.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!