ZOJ - 3689 Digging(贪心 + 01背包)

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 Babylonianhas, Ancient 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 N, T (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 ti, si (1 ≤ ti, si ≤ 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天 每个任务有si 与 ti 分别代表次任务花费时间和房间大小 计算任务报酬的方式为剩余天数 * 房间大小 求在T天内的最大报酬

解题思路:这题是一个背包 但是需要先贪心排序一下 

证明贪心思路: 设S1, W1, S2, W2分别为第一个任务花费的时间,第一个任务的报酬

, 第二个任务花费的时间, 第二个任务花费的报酬, T为总时间,那么T * W1 + (T - S1) * W2为先完成第一个任务的报酬, T * W2 + (T - S2) * W1为先完成第二个任务的报酬  自然需要将总值最高的顺序放前面  化简可得 S1 * W2 > S2 * W1 这就是贪心的排序公式  排序完成后就是一个裸的01背包了


 

#include <bits/stdc++.h>
#define met(a, b) memset(a, b, sizeof(a))
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e6 + 10;
ll dp[MAXN];
using namespace std;

struct Node
{
    int w, val;//时间  价值
}node[MAXN];

bool cmp(Node x, Node y)
{
    return x.w * y.val > x.val * y.w;//贪心公式
}

int main()
{
    int n, t;
    while (cin >> n >> t)
    {
        for (int i = 0; i < n; i++)
            cin >> node[i].w >> node[i].val;
        sort(node, node + n, cmp);
        met(dp, 0);
        for (int i = 0; i < n; i++)
        {
            for (int j = t; j >= node[i].w; j--)
                dp[j] = max(dp[j], dp[j - node[i].w] + j * node[i].val);//01背包
        }
        cout << dp[t] << endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值