Digging(DP)

ZOJ Problem Set - 3689
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 tidays 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

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

可以转化为01背包问题解决,物品为要修建的墓地,要修建的墓地数N为物品数,总天数T是背包容量,物品价值为t*si,物品体积为ti。注意传统的01背包问题中,物品的价值是固定的,但是该题中物品的价值t*si受t影响,是不固定的,换言之,物品的价值是可分配的,因此要先确定物品价值分配的最优策略,在此基础上再用01背包问题求解。

 方案:按照si/ti对物品进行排序,在进行01背包算法时按此顺序遍历物品,能保证所取物品中,si/ti越大,分配的剩余时间t也越大,那么si*t/ti越大,si*t/ti就是物品i单位体积的价值,一个背包中物品的单位体积的平均价值越大,在背包容量固定为T的情况下,自然能获得最优解。

AC Code:

 1 #include <iostream>
 2 #include <queue>
 3 #include <vector>
 4 #include <map>
 5 #include <string>
 6 #include <algorithm>
 7 #include <cstdio>
 8 #include <cstring>
 9 #include <cmath>
10 
11 using namespace std;
12 
13 const int SZ = 30005, MAXT = 10003;
14 const double eps = 10e-5;
15 struct Coffin
16 {
17     int t, s;
18     double r;
19 }co[SZ];
20 int T, N;
21 int re[MAXT];
22 
23 bool CMP(const Coffin& x, const Coffin& y)
24 {
25     if(fabs(x.r - y.r) > eps) return x.r > y.r;
26     return x.t < y.t;
27 }
28 
29 int Max(int a, int b)
30 {
31     return a > b ? a : b;
32 }
33 
34 int main()
35 {
36     while(scanf("%d %d", &N, &T) != EOF)
37     {
38         for(int i = 0; i < N; i++)
39         {
40             scanf("%d %d", &co[i].t, &co[i].s);
41             co[i].r = 1.0 * co[i].s / co[i].t;
42         }
43         sort(co, co + N, CMP);
44         memset(re, 0, sizeof(re));
45         int maxRe = 0;
46         for(int i = 0; i < N; i++)
47         {
48             for(int j = T; j >= co[i].t; j--)
49             {
50                 re[j] = Max(re[j], re[j - co[i].t] + co[i].s * (T - j + co[i].t));
51                 if(maxRe < re[j]) maxRe = re[j];
52             }
53         }
54         printf("%d\n", maxRe);
55     }
56     return 0;
57 }

 

posted on 2013-07-30 11:49  铁树银花  阅读(308)  评论(0编辑  收藏  举报

导航