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)的更多相关文章

  1. PAT-Top1002. Business (35)

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

  2. PAT (Top Level)1002. Business DP/背包

    As the manager of your company, you have to carefully consider, for each project, the time taken to ...

  3. 【PAT】1002. 写出这个数 (20)

    1002. 写出这个数 (20) 读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式:每个测试输入包含1个测试用例,即给出自然数n的值.这里保证n小于10100. 输出格式 ...

  4. PAT乙级 1002. 写出这个数 (20)

    1002. 写出这个数 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 读入一个自然数n,计算其各位数字 ...

  5. [C++]PAT乙级1002.写出这个数(20/20)

    /* 1002. 写出这个数 (20) 读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式:每个测试输入包含1个测试用例,即给出自然数n的值.这里保证n小于10^100. ...

  6. PAT Basic 1002

    1002 写出这个数 (20 分) 读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式: 每个测试输入包含 1 个测试用例,即给出自然数 n 的值.这里保证 n 小于 1 ...

  7. 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 ...

  8. PAT乙级1002

    1002 写出这个数 (20 分)   读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式: 每个测试输入包含 1 个测试用例,即给出自然数 n 的值.这里保证 n 小于 ...

  9. pat甲级1002

    1002. A+B for Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue T ...

随机推荐

  1. NOIp2013 货车运输 By cellur925

    题目传送门 A 国有 n 座城市,编号从 1 到 n ,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重. 现在有 q 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情况下 ...

  2. 面试基础试题 一 http和HTTPS的区别

    作为老生常谈的问题,我主要给出我自己的理解和结合大牛的叙述的综合看法,来检验自己学习 超文本传输协议HTTP协议被用于在Web浏览器和网站服务器之间传递信息,HTTP协议以明文方式发送内容,不提供任何 ...

  3. C++中的显式构造函数

    有如下一个简单的复数类: class ClxComplex { public: ClxComplex(double dReal = 0.0, double dImage = 0.0) { m_dRea ...

  4. django urls路由匹配分发

    django urls路由匹配分发 一.urls配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表:你就是以这种方式告诉Django,对 ...

  5. LBP特征 学习笔记

    这几天一直在做人脸识别的项目,有用到LBP特征,但是毫无头绪,师姐这几天也比较忙,没有时间来指导我,随自己找相应的介绍LBP的博文来看,现在总算有了一个大体的思路了,就写下来吧 注:参考博文: 目标检 ...

  6. 题解报告:hdu 1235 统计同成绩学生人数

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1235 Problem Description 读入N名学生的成绩,将获得某一给定分数的学生人数输出. ...

  7. 08 H5新增input元素

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  8. Web自动化测试框架-PO模式

    Web自动化测试框架(WebTestFramework)是基于Selenium框架且采用PageObject设计模式进行二次开发形成的框架. 一.适用范围:传统Web功能自动化测试.H5功能自动化测试 ...

  9. Android插件开发

    插件开发的概念: 对于一个功能特别多,代码量特别大的App比如支付宝.360手机助手来说,如果把所有的功能和代码都写在一个App中,就会造成App体积过于庞大,用户下载体验差,不方便测试,业务.模块耦 ...

  10. WinForm 对话框,流

    private void button1_Click(object sender, EventArgs e) { //显示颜色选择器 colorDialog1.ShowDialog(); //把取到的 ...