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 1Sample Output:
18
题解:先按deadline排序,把时间作为背包就行了。
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std; struct sion
{
int v, c, d;
}a[]; int cmp(sion a, sion b)
{
if(a.d!= b.d)
return a.d < b.d;
else
return a.c < b.c;
} int dp[][];
int main()
{
int n;
int sum ,ma ;
while(cin >> n)
{
sum = ;
for(int i = ; i <= n; i++)
{
cin >> a[i].v >> a[i].c>> a[i].d;
sum += a[i].c;
dp[i][] = ;
}
sort(a + , a + n + , cmp);
ma = ;
dp[][] = ;
for(int i = ; i <= n; i++ )
{
for(int j = ; j <= sum; j++)
{
if(j <= a[i].d)
{
dp[i][j] = max(dp[i-][j-a[i].c]+a[i].v, dp[i-][j]);
}
else
{
dp[i][j] = a[i].v;
}
ma = max(ma, dp[i][j]);
}
} printf("%d\n", ma); }
}