D - Digging 01背包问题

Description

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 theMaya 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 takesti 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 get t*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
**********************************************************************************************************************************************************************************************************************************************************************************************************************
 1 /*
 2 有公式 a.s*t+(t-a.t)*b.s  和
 3   b.s*t+(t-b.t)*a.s
 4   两者之差  a.s*b.t-b.s*a,t
 5   即单位密度,可据此作为排序条件判断排序
 6   其实到最后得出的就是一个01背包问题
 7  */
 8 #include<iostream>
 9 #include<string>
10 #include<cstring>
11 #include<cmath>
12 #include<cstdio>
13 #include<algorithm>
14 using namespace std;
15 struct coffin
16 {
17     int s,t;
18 }c[3001];
19 int n,t,dp[100001];
20 bool cmp(const coffin &a,const coffin &b)
21 {
22     return a.s*b.t<a.t*b.s;
23 }
24 int main()
25 {
26     int i,j,k;
27     while(scanf("%d %d",&n,&t)!=EOF)
28     {
29         for(i=0;i<n;i++)
30          scanf("%d %d",&c[i].t,&c[i].s);
31         sort(c,c+n,cmp);
32         memset(dp,0,sizeof(dp));
33         for(i=0;i<n;i++)
34         {
35             for(j=t;j-c[i].t>=0;j--)
36               if(dp[j]<dp[j-c[i].t]+j*c[i].s)
37                  dp[j]=dp[j-c[i].t]+j*c[i].s;
38         }
39         printf("%d\n",dp[t]);
40     }
41     return 0;
42 }
View Code

 

转载于:https://www.cnblogs.com/sdau--codeants/p/3445928.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值