码迷,mamicode.com
首页 > 其他好文 > 详细

bnu 28890 &zoj 3689——Digging——————【要求物品次序的01背包】

时间:2015-07-21 01:15:22      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

Digging

Time Limit: 2000ms
Memory Limit: 65536KB
This problem will be judged on ZJU. Original ID: 3689
64-bit integer IO format: %lld      Java class name: Main
Type: 
None
 
 

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

 
 
 
题目大意:给你n个物品,T天。每个物品又ti,si。表示需要ti天去造,而获得的酬劳是si*t。t是开始造该物品时的时间。
解题思路:比赛的时候用01背包写了,但是一直推不出来样例。比赛完后,看别人说是对物品的放入背包顺序有要求。应该选择性价比由低到高的顺序去dp。这样的好处是,对于性价比高的物品,不会被性价比低的覆盖,性价比越高,越可能被保留下来。从而保证了获得的价值更大。
 
/*用价性比从低到高的顺序更新状态,这样高性价比的Coffin必
 然会出现在T较大的时刻,即会安排先修建。如果性价比高的放在
 前面进行dp更新,可能高性价比的会被覆盖掉,造成最优解丢失。
 */

#include<bits/stdc++.h>
using namespace std;
const int maxv=1e4+100;
int dp[maxv];
struct coffin{
    int t,s;
}goods[3200];
int m;
bool cmp(coffin a,coffin b){
    return a.s*1.0/a.t<b.s*1.0/b.t;
}
void ZeroOnePack(coffin x){
    int i;
    for(i=m;i>=x.t;i--){
        dp[i]=max(dp[i],dp[i-x.t]+i*x.s);
    } 
}
int main(){
    int n,i,j,res;
    while(scanf("%d%d",&n,&m)!=EOF){
        memset(dp,0,sizeof(dp));
        for(i=0;i<n;i++){
            scanf("%d%d",&goods[i].t,&goods[i].s);
        }
        sort(goods,goods+n,cmp);
        for(i=0;i<n;i++){
            ZeroOnePack(goods[i]);
        }
        res=0;
        for(i=0;i<=m;i++){
            res=max(res,dp[i]);
        }
        printf("%d\n",res);
    }
    return 0;
}

  

bnu 28890 &zoj 3689——Digging——————【要求物品次序的01背包】

标签:

原文地址:http://www.cnblogs.com/chengsheng/p/4663137.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!