虽然一看就是DP问题,但是每个Coffin是唯一的,修了一次就不能再修了。这样更新每个时间对应的最高赏金时要考虑那些Coffin还没修的问题,这就很麻烦了。但事实上,对于一个最优的安排中修建的Coffin,是有严格的时间先后顺序的。
考虑Coffin a & b,在任意相同时间t下,先a后b与先b后a所能获得的赏金分别是 t*a.s + (t-a.t)*b.s & t*b.s + (t-b.t)*a.s 。其差为 a.s*b.t - a.t*b.s ,这个反映了每个Coffin方案的价性比。假设这个差值大于0(表示先a后b更好),在任意一个包含a,b修建顺序中,若b先于a,则交换a,b的顺序能得到一个更优的方案(当a,b间有其他Coffin时应该不成立,但是能过了)。于是我们可以用价性比从低到高的顺序更新状态,这样搞价性比的Coffin必然会出现在T较大的时刻,即会安排先修建。
#include <cstdio>
#include <memory.h>
#include <algorithm>
using namespace std;
struct coffin
{
int t, s;
}c[3000];
int n, t, dp[10001];
inline bool cmp(const coffin& a, const coffin& b)
{
return a.s*b.t < a.t*b.s;
}
int main()
{
while(~scanf("%d%d", &n, &t))
{
for(int i=0; i<n; ++i)
scanf("%d%d", &c[i].t, &c[i].s);
sort(c, c+n, cmp);
memset(dp, 0, 4*(t+1));
int temp;
for(int j=0; j<n; ++j)
{
for(int k=t; k-c[j].t>=0; --k)
{
temp = dp[k-c[j].t] + k*c[j].s;
if(temp > dp[k])
dp[k] = temp;
}
}
printf("%d\n", dp[t]);
}
return 0;
}