PAT顶级 1002. Business (35)
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(<=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
设dp[i][j]表示考虑到第i个项目,前一个项目结束时间为j的最大收益,那么可得DP方程为:
dp[i][j+a[i].l]=max(dp[i][j+a[i].l],dp[k][j]) if(j+a[i].l<=a[i].d)
由于没有说明数据范围,只能使用map表示二维dp数组了
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
struct node
{
int p,l,d;
node() {}
node(int tp,int tl,int td)
{
p=tp;
l=tl;
d=td;
}
bool operator < (const node &an) const
{
return d<an.d;
}
} a[55];
typedef long long ll;
map<ll,map<ll,ll> >dp;
int main()
{
int n,p,l,d;
while(cin>>n)
{
for(int i=1; i<=n; i++)
{
cin>>p>>l>>d;
a[i]=node(p,l,d);
}
a[0]=node(0,0,0);
sort(a+1,a+1+n);
dp.clear();
ll ans=0;
for(int i=1; i<=n; i++)
{
for(int j=0; j<=a[i-1].d; j++)
{
if(j+a[i].l<=a[i].d)
{
for(int k=0;k<i;k++){
dp[i][j+a[i].l]=max(dp[i][j+a[i].l],dp[k][j]+a[i].p);
ans=max(ans,dp[i][j+a[i].l]);
}
}
}
}
cout<<ans<<endl;
}
return 0;
}
PAT顶级 1002. Business (35)的更多相关文章
- PAT-Top1002. Business (35)
在一个项目的截止日期之前,如果工期有空闲则可能可以开展其他项目,提高效益.本题考查动态规划.数组dp[i][t]表示在截止时间为t时,前i个项目工作安排能够产生的最大收益,而前i个项目的截止时间都不大 ...
- PAT (Top Level)1002. Business DP/背包
As the manager of your company, you have to carefully consider, for each project, the time taken to ...
- 【PAT】1002. 写出这个数 (20)
1002. 写出这个数 (20) 读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式:每个测试输入包含1个测试用例,即给出自然数n的值.这里保证n小于10100. 输出格式 ...
- PAT乙级 1002. 写出这个数 (20)
1002. 写出这个数 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 读入一个自然数n,计算其各位数字 ...
- [C++]PAT乙级1002.写出这个数(20/20)
/* 1002. 写出这个数 (20) 读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式:每个测试输入包含1个测试用例,即给出自然数n的值.这里保证n小于10^100. ...
- PAT Basic 1002
1002 写出这个数 (20 分) 读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式: 每个测试输入包含 1 个测试用例,即给出自然数 n 的值.这里保证 n 小于 1 ...
- PAT 甲级 1002 A+B for Polynomials (25 分)
1002 A+B for Polynomials (25 分) This time, you are supposed to find A+B where A and B are two polyno ...
- PAT乙级1002
1002 写出这个数 (20 分) 读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式: 每个测试输入包含 1 个测试用例,即给出自然数 n 的值.这里保证 n 小于 ...
- pat甲级1002
1002. A+B for Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue T ...
随机推荐
- 题解报告:poj 2533 Longest Ordered Subsequence(最长上升子序列LIS)
Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence ...
- Hibernate3中重复引用hbm文件错误信息记录
Hibernate3中重复引用hbm文件错误信息记录. 八月 ::, ERROR - Context initialization failed org.springframework.beans.f ...
- Android Dialogs(2)最好用DialogFragment创建Dialog
Creating a Dialog Fragment You can accomplish a wide variety of dialog designs—including custom layo ...
- 不通过getElementByName实现获取表单数据 (document.form表单的name值.input输入框的name值)
function update() { //document.form表单的name值.input输入框的name值 var username = document.form1.username; v ...
- 214 Shortest Palindrome 最短回文串
给一个字符串 S, 你可以通过在字符串前面添加字符将其转换为回文串.找到并返回可以用这种方式转换的最短回文串.例如:给出 "aacecaaa",返回 "aaacecaaa ...
- D. Artsem and Saunders 数学题
http://codeforces.com/contest/765/problem/D 这题的化简,不能乱带入,因为复合函数的带入,往往要严格根据他们的定义域的 题目要求出下面两个函数 g[h(x)] ...
- rsync常见错误
rsync使用时的常见问题: 错误1: rsync: read error: Connection reset by peer (104) rsync error: error in rsync pr ...
- 树莓派 离线安装 apt-get offline
有网络的地方 sudo cp /var/cache/apt/archives/YOUR_PACK_NAME ~/swap/deb/ 没网络的地方 sudo dpkg -i ~/swap/deb/*
- AJPFX关于Java NIO的概述总结
Java NIO 由以下几个核心部分组成: Channels Buffers Selectors 虽然Java NIO 中除此之外还有很多类和组件,但在我看来,Channel,Buffer 和 Sel ...
- 解决 图片在div中等比例缩放问题 (未解决:图片比例小于盒子模型时不会自动填充)
如题,该方案仅支持对图片等比例缩放.本文附件地址:https://files.cnblogs.com/files/john69-/background-Img.rar <!DOCTYPE htm ...