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

ZOJ 3689 Digging(贪心+dp)

时间:2017-06-16 10:12:22      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:tle   cee   bsp   article   space   mina   case   时间   最大   

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 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

题意:n个任务,剩余t的时间,完毕每一个任务会消耗时间ti,同一时候也会得到t*si的价值,求怎么安排任务得到的价值最大
题解:一開始仅仅想用背包,出不来例子。排个序后就能够了。

至于为什么。我也不知道,(看来还是没參透dp的精髓。

。),想来大概是背包得先背性价比高的来保证贪心的思想吧。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;

const int M=10010;
const int N=3030;

int n,T;
ll dp[M];
struct node {
    int t,v;
} a[N];

bool cmp(node a,node b) {
    return a.v*b.t<a.t*b.v;
}

int main() {
   // freopen("test.in","r",stdin);
    while(~scanf("%d%d",&n,&T)) {
        for(int i=0; i<n; i++)
            scanf("%d%d",&a[i].t,&a[i].v);
        sort(a,a+n,cmp);
        memset(dp,0,sizeof dp);
        for(int i=0; i<n; i++) {
            for(int j=T; j>=a[i].t; j--) {
                dp[j]=max(dp[j],dp[j-a[i].t]+a[i].v*j);
            }
        }
        printf("%lld\n",dp[T]);
    }
    return 0;
}


ZOJ 3689 Digging(贪心+dp)

标签:tle   cee   bsp   article   space   mina   case   时间   最大   

原文地址:http://www.cnblogs.com/lytwajue/p/7025672.html

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