Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1881
Problem description there will be a large number of graduates initiating a carnival each year during the graduation season. Good friends will have a meal, which is called "BG" on the Internet ". Participating in BG in different groups may have different feelings. We can use a non-negative integer to define a "happiness" for each BG ". A bg list is given, which lists the happiness, duration, and departure time of each BG initiator. Schedule a series of BG times to maximize your happiness.
For example, there are four BG:
1st happy events are 5 and lasted for 1 hour. The initiator must leave after 1 hour;
2nd happy events are 10 and lasted for 2 hours. The initiator must leave after 3 hours;
3rd happy events are 6 and lasted for 1 hour. The initiator must leave after 2 hours;
4th happy events lasted for 1 hour, and the initiator must leave after 1 hour.
The maximum happiness level should be: first start 3rd games, get happiness level 6, end in 1st hours, the initiator can also leave; then start 2nd games, get happiness level 10, at the end of 3rd hours, the initiator just had time to leave. At this time, no other BG can be arranged because the initiators have left the school. Therefore, the maximum happiness is 16.
Note that BG must end before the initiator leaves. You cannot leave a bg or join a BG.
And because of your popularity, there may be up to 30 groups for you, so you need to write a program to solve the problem of this Schedule. The input test input contains several test cases. The first row of each test case contains an integer N (<= 30) followed by N rows. Each row contains a BG:
H l t
H indicates the degree of happiness, L indicates the duration (hours), and T indicates the time when the initiator leaves school. Data guarantee l is not greater than T, because if the initiator must leave after T hours, BG must end before the master leaves.
When N is negative, the input ends. Output: the output of each test case occupies one row, and the maximum output happiness is reached.
Sample Input
36 3 33 2 24 1 345 1 110 2 36 1 23 1 1-1
Sample output
716
The Code is as follows:
#include <iostream>#include <algorithm>using namespace std;int n, ans;struct bg{int h, l, t;}a[32];bool cmp(bg a, bg b){return a.t < b.t;}void dfs(int i, int hh, int tt){if(i == n){if(hh > ans)ans = hh;return;}if(tt+a[i].l <= a[i].t){dfs(i+1,hh+a[i].h,a[i].l+tt);}dfs(i+1,hh,tt);return;}int main(){while(cin>>n){if(n < 0)break;ans = 0;for(int i = 0; i < n; i++){cin>>a[i].h>>a[i].l>>a[i].t;}sort(a,a+n,cmp);dfs(0,0,0);cout<<ans<<endl;}return 0;}