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 ...
随机推荐
- Luogu P1083 借教室【二分答案/差分】By cellur925
题目描述 Description 在大学期间,经常需要租借教室.大到院系举办活动,小到学习小组自习讨论,都需要 向学校申请借教室.教室的大小功能不同,借教室人的身份不同,借教室的手续也不一样. 面对海 ...
- hibernate简单实现连接数据库,并实现数据的操作
1:创建实体类 package com.yinfu.entity; public class User { private int id; private String username; priva ...
- maven学习-搭建环境
1.Maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具. 2.下载: maven.apache.org 3.bin目录包含mvn的运行脚本: ...
- 51nod 1134最长递增子序列
1134 最长递增子序列 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出长度为N的数组,找出这个数组的最长递增子序列.(递增子序列是指,子序列的元素 ...
- 题解报告:hdu1202The calculation of GPA(算绩点问题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1202 Problem Description 每学期的期末,大家都会忙于计算自己的平均成绩,这个成绩对 ...
- Ubuntu Server 上安装pip后pip命令报错的解决办法
Installation Do I need to install pip? pip is already installed if you are using Python 2 >=2.7.9 ...
- AJPFX简述可变参数概述和使用
A:可变参数概述 定义方法的时候不知道该定义多少个参数 B:格式 修饰符 返回值类型 方法名(数据类型… 变量名){} C:注意事项: 这里的变量其实是一个数组 如果一个方法有可变参数,并且有多个参数 ...
- C#过时方法标记
1.当遇到过时或废弃的方式 函数怎么办 [Obsolete]特性解决你的困惑 1.1:当方法已经完成相关兼容 可以保留时
- hihocoder offer收割编程练习赛12 C 矩形分割
思路: 模拟,深搜. 实现: #include <iostream> #include <cstdio> #include <string> using names ...
- Android习惯——给全部Activity添加集合管理
title: 2017-3-23 Android习惯--给全部Activity添加集合管理 public void ActivityCollector{ public static List<A ...