pat考试1002 Business (35 分)

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 (≤), 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

做过类似的贪心题,但是这题好像不能贪心,于是我们动态规划,又由于题目并没有明确数据范围(又是坑爹),所以我们可以map来表示二维数组。  我们先做一个排序,按照截止时间排序。 然后dp[i][j]表示第i个任务在第j天刚好结束时的最大收益。转移是O(n)的。 

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define mp  make_pair
 4 #define fi  first
 5 #define se  second
 6 int const N=50+3;
 7 map<pair<int,int>,int> dp;
 8 int p[N],L[N],d[N],n;
 9 int main(){
10     scanf("%d",&n);
11     for(int i=1;i<=n;i++)
12         scanf("%d%d%d",&p[i],&L[i],&d[i]);
13     for(int i=1;i<=n;i++)
14         for(int j=i+1;j<=n;j++)
15             if(d[i]>d[j]){
16                 swap(p[i],p[j]);
17                 swap(L[i],L[j]);
18                 swap(d[i],d[j]);
19             }
20     int ans=0;
21     for(int i=1;i<=n;i++)
22         for(int j=L[i];j<=d[i];j++)
23             dp[mp(i,j)]=p[i],ans=max(ans,p[i]);
24     for(int i=1;i<n;i++)
25         for(int j=1;j<=d[i];j++){
26             if(dp.find(mp(i,j))==dp.end()) continue;
27             int x=dp[mp(i,j)];
28             for(int k=i+1;k<=n;k++)
29                 if(j+L[k]<=d[k]){
30                     int &t=dp[mp(k,j+L[k])];
31                     t=max(t,x+p[k]);
32                     ans=max(t,ans);
33                 }
34         }
35     printf("%d\n",ans);
36     return 0;
37 }

原文地址:https://www.cnblogs.com/ZJXXCN/p/11504876.html

时间: 2024-11-07 05:13:09

pat考试1002 Business (35 分)的相关文章

pat 1005 Programming Pattern (35 分)

Programmers often have a preference among program constructs. For example, some may prefer if(0==a), while others may prefer if(!a). Analyzing such patterns can help to narrow down a programmer's identity, which is useful for detecting plagiarism. No

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 str

PTA 7-1 畅通工程之局部最小花费问题(35 分)

7-1 畅通工程之局部最小花费问题(35 分) 某地区经过对城镇交通状况的调查,得到现有城镇间快速道路的统计数据,并提出"畅通工程"的目标:使整个地区任何两个城镇间都可以实现快速交通(但不一定有直接的快速道路相连,只要互相间接通过快速路可达即可).现得到城镇道路统计表,表中列出了任意两城镇间修建快速路的费用,以及该道路是否已经修通的状态.现请你编写程序,计算出全地区畅通需要的最低成本. 输入格式: 输入的第一行给出村庄数目N (1≤N≤100):随后的N(N?1)/2行对应村庄间道路的

2018/3/19 模拟赛 35分

T1 不会计算几何弃疗了. T2 写了个bitset结果还不如不优化(手动滑稽),因为测样例开小了空间忘了开回去所以0分. 正解是FFT,不会FFT.. T3 暴力35分,正解倍增floyd,学长还讲过但是还是错了,又多了一个要学的知识点. 原文地址:https://www.cnblogs.com/137shoebills/p/8602386.html

7-1 畅通工程之局部最小花费问题 (35 分)

7-1 畅通工程之局部最小花费问题 (35 分) 某地区经过对城镇交通状况的调查,得到现有城镇间快速道路的统计数据,并提出"畅通工程"的目标:使整个地区任何两个城镇间都可以实现快速交通(但不一定有直接的快速道路相连,只要互相间接通过快速路可达即可).现得到城镇道路统计表,表中列出了任意两城镇间修建快速路的费用,以及该道路是否已经修通的状态.现请你编写程序,计算出全地区畅通需要的最低成本. 输入格式: 输入的第一行给出村庄数目N (1≤N≤100):随后的N(N?1)/2行对应村庄间道路

pat 顶级 1001 Battle Over Cities - Hard Version (35 分)

It is vitally important to have all the cities connected by highways in a war. If a city is conquered by the enemy, all the highways from/toward that city will be closed. To keep the rest of the cities connected, we must repair some highways with the

pat 1003 Universal Travel Sites (35 分)

After finishing her tour around the Earth, CYLL is now planning a universal travel sites development project. After a careful investigation, she has a list of capacities of all the satellite transportation stations in hand. To estimate a budget, she

【PAT】B1041 考试座位号(15 分)

/* */ #include<stdio.h> #include<algorithm> using namespace std; struct stu{ char number[18]; int shi,kao; }arr[1005]; bool cmp(stu A,stu B){ return A.shi<B.shi; } int main(){ int N,N1,temp; scanf("%d",&N); for(int i=1;i<=N

PAT Advanced 1002 A+B for Polynomials (25分)

This time, you are supposed to find A+B where A and B are two polynomials. Input Specification: Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N?1?? a?N?1???? N?2?? a?N?2?