每个测试用例的输出占一行,输出最大快乐度。
3 6 3 3 3 2 2 4 1 3 4 5 1 1 10 2 3 6 1 2 3 1 1 -1
7 16
//m[i][j]表示前i个bg在进行了j时间时的最大快乐度
//m[i][j]=max{ m[i-1][j], m[i-1][j-bg[i].l]+bg[i].h }, j-bg[i].l>=0 && j<=bg[i].t
//m[0][j]=m[i][0]=0;
//最后要求的是max{m[n][0]~m[n][mmax]}
//bg的选择特点是发起人离开时间更早的bg会更先被举行,所以先对离开时间 t 排序
//bg进行的时间总和不大于发起人中的最晚离开时间mmax
#include <iostream>
#include <algorithm>
using namespace std;
int m[31][1000];
struct bg_typ
{
int h,l,t;
};
bg_typ bg[31];
bool cmp(const bg_typ a,const bg_typ b)
{
return a.t<b.t;
}
int max(int x,int y)
{
if(x>y)
return x;
else
return y;
}
int main()
{
int n,i,j,t;
while(cin>>n&&n>=0)
{
int mmax=0;
for(t=1;t<=n;++t)
{
cin>>bg[t].h>>bg[t].l>>bg[t].t;
if(bg[t].t>mmax)
mmax=bg[t].t;
}
sort(bg+1,bg+n+1,cmp);
for(i=0;i<=n;++i)
m[i][0]=m[0][i]=0;
for(i=1;i<=n;++i)
{
for(j=0;j<=mmax;++j)
{
if(j<=bg[i].t&&j-bg[i].l>=0)
m[i][j]=max(m[i-1][j],m[i-1][j-bg[i].l]+bg[i].h);
else m[i][j]=m[i-1][j];
}
}
int result=m[n][mmax];
for(j=mmax;j>=0;--j)
if(result<m[n][j])
result=m[n][j];
cout<<result<<endl;
}
return 0;
}
/**************************************************************
Problem: 1030
User: vhreal
Language: C++
Result: Accepted
Time:0 ms
Memory:1640 kb
****************************************************************/原文地址:http://blog.csdn.net/wtyvhreal/article/details/42076485