zoj 3689 Digging (dp)

Digging

Time Limit: 2 Seconds       Memory Limit: 65536 KB

When it comes to the Maya Civilization, we can quickly remind of a term called the end of the world. It's not difficult to understand why we choose to believe the prophecy (or we just assume it is true to entertain ourselves) if you know the other prophecies appeared in the Maya Calendar. For instance, it has accurately predicted a solar eclipse on July 22, 2009.

The ancient civilization, such as Old BabylonianhasAncient Egypt and etc, some features in common. One of them is the tomb because of the influence of the religion. At that time, the symbol of the tomb is the pyramid. Many of these structures featured a top platform upon which a smaller dedicatory building was constructed, associated with a particular Maya deity. Maya pyramid-like structures were also erected to serve as a place of interment for powerful rulers.

Now there are N coffin chambers in the pyramid waiting for building and the ruler has recruited some workers to work for T days. It takes ti days to complete the ith coffin chamber. The size of the ith coffin chamber is si. They use a very special method to calculate the reward for workers. If starting to build the ith coffin chamber when there are t days left, they can gett*si units of gold. If they have finished a coffin chamber, then they can choose another coffin chamber to build (if they decide to build the ith coffin chamber at the time t, then they can decide next coffin chamber at the time t-ti).

At the beginning, there are T days left. If they start the last work at the time t and the finishing time t-ti < 0, they will not get the last pay.

Input

There are few test cases.

The first line contains NT (1 ≤ N ≤ 3000,1 ≤ T ≤ 10000), indicating there are N coffin chambers to be built, and there are T days for workers working. Next N lines contains tisi (1 ≤ tisi≤ 500).

All numbers are integers and the answer will not exceed 2^31-1.

Output

For each test case, output an integer in a single line indicating the maxminal units of gold the workers will get.

Sample Input
3 10
3 4
1 2
2 1
Sample Output
62
Hint

Start the second task at the time 10
Start the first task at the time 9
Start the third task at the time 6
The answer is 10*2+9*4+6*1=62


思路:

先不考虑总天数 T 的限制,假设他们全都能修建完成。对于某个修建顺序 a[1], a[2], .., a[N],考虑其中任意相邻的两个任务 l = a[i] 和 r = a[i + 1],它们能获得的报酬为:
x * s[l] + (x - t[l]) * s[r] = x * (s[l] + s[r]) - t[l] * s[r]
如果交换它们的顺序,则明显不影响其他任务(因为它们的总耗时不变),而交换后的报酬为:
x * s[r] + (x - t[r]) * s[l] = x * (s[l] + s[r]) - t[r] * s[l]
可以发现,这两个式子变换后,前面的部分都一样,后面的部分一个是 -t[l] * s[r],一个是 -t[r] * s[l]

既然交换相邻的任务不会影响其他任务,但会改变的总报酬,那么我们就可以通过对 {1, 2, 3, .., N} 这个序列进行一定的交换,得到一个报酬最大的修建顺序,换句话说,对这些任务进行一个排序即可得到一个最优的工作顺序:

bool cmp(int l, int r){

    return -t[l] * s[r] > -t[r] * s[l];

}

现 在,有了总天数 T 的限制后,必须在这个基础上进行一个 DP,做法就是从排好序的工作中选择一部分工作去执行(上面的 cmp 函数中的表达式能转换 成 t[l] / s[l] < t[r] / s[r],可以发现它有传递性,因此它的任意子序列也是最优的顺序),

于是剩下的 DP 部分是一个类似于背包的写法,照着题目里给的计算方式去算就行了




#include <cstdio>
#include <cstring>
#include <algorithm>
const long M=11000;
const long N=3100;
struct node{
	long s,t;
}a[N];
long dp[M],n,T,ans;
long cmp(node x,node y){
	return y.s*x.t<x.s*y.t;
}
int main(){
	while (~scanf("%d%d",&n,&T)){
		for (long i=1;i<=n;++i)
			scanf("%d%d",&a[i].t,&a[i].s);
		std::sort(a+1,a+1+n,cmp);
		memset(dp,0,sizeof(dp));
		ans=0;
		for (long i=1;i<=n;++i)
			for (long j=T;j>=a[i].t;--j){
				dp[j]=std::max(dp[j],dp[j-a[i].t]+a[i].s*(T-j+a[i].t));
				ans=std::max(dp[j],ans);
			}
		printf("%d\n",ans);
	}
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值