D - Digging

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Submit Status

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

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

题意:古代某统治者要修建一些棺材,其中第 i 个棺材大小为 s[i],修建需要花费 t[i] 天,如果在剩余 x 天的时候开始修建并且能够及时完成,则能获得 x * s[i] 的报酬,总共有 T 天可用,问最大能获得的报酬为多少,注意可用天数T并不一定会大于或等于所有棺材所需天数的总和;

先不考虑总天数 T 的限制,假设他们全都能修建完成。对于某个修建顺序 a[1], a[2], .., a[N],考虑其中任意相邻的两个任务 l = a[i] 和 r = a[i + 1],它们能获得的报酬为:
x * s[l] + (x - t[l]) * s[r] = x * (s[l] + s[r]) - t[l] * s[r]
如果交换它们的顺序,则明显不影响其他任务(因为它们的总耗时不变),而交换后的报酬为:
x * s[r] + (x - t[r]) * s[l] = x * (s[l] + s[r]) - t[r] * s[l]
可以发现,这两个式子变换后,前面的部分都一样,后面的部分一个是 -t[l] * s[r],一个是 -t[r] * s[l]

既然交换相邻的任务不会影响其他任务,但会改变的总报酬,那么我们就可以通过对 {1, 2, 3, .., N} 这个序列进行一定的交换,得到一个报酬最大的修建顺序,换句话说,对这些任务进行一个排序即可得到一个最优的工作顺序:

bool cmp(int l, int r){

return -t[l] * s[r] > -t[r] * s[l];

}

现 在,有了总天数 T 的限制后,必须在这个基础上进行一个 DP,做法就是从排好序的工作中选择一部分工作去执行(上面的 cmp 函数中的表达式能转换 成 t[l] / s[l] < t[r] / s[r],可以发现它有传递性,因此它的任意子序列也是最优的顺序),

于是剩下的 DP 部分是一个类似于背包的写法,照着题目里给的计算方式去算就行了

此题应该是先进行贪心,找出修建序列,然后再根据贪心后的顺序进行01背包

代码:

 #include <cstdio>
#include<iostream>
#include <cstring>
#include <algorithm>
using namespace std; int t[],s[],a[],dp[]; bool cmp(int l, int r){
return -t[l]*s[r]>-t[r]*s[l];
} int main(){
// freopen("input.txt","r",stdin);
int n,m;
while(scanf("%d%d",&n,&m)==){
memset(dp,,sizeof(dp));
dp[]=;
for(int i=;i<n;i++){
scanf("%d%d",t+i,s+i);
a[i]=i;
}
sort(a,a+n,cmp);//进行贪心排序,
for(int o=;o<n;o++){//利用一位数组进行01背包计算
int i=a[o];
for(int j=m;j>=t[i];j--)
dp[j]=max(dp[j],dp[j-t[i]]+(m-j+t[i])*s[i]);
}
long long maxsum=;
for(int i=;i<=m;i++){//求最大值
if(dp[i]>maxsum){
maxsum=dp[i];
}
}
printf("%d\n",maxsum);
}
}

网上那个有比较好的AC代码:

代码:http://ideone.com/eYK6u8

比较好的题解:http://blog.csdn.net/makaihong123/article/details/8780692

D - Digging(01背包,贪心)的更多相关文章

  1. HDU -2546饭卡(01背包+贪心)

    这道题有个小小的坎,就是低于5块不能选,大于5块,可以任意选,所以就在初始条件判断一下剩余钱数,然后如果大于5的话,这时候就要用到贪心的思想,只要大于等于5,先找最大的那个,然后剩下的再去用背包去选择 ...

  2. HDU--3466(0-1背包+贪心/后效性)

    题意是: 给你一些钱 m ,然后在这个国家买东西, 共有 n 件物品,每件物品有  价格 P    价值 V    还有一个很特别的属性 Q, Q 指 你如过想买这件物品 你的手中至少有这钱Q . 虽 ...

  3. Proud Merchants HDU - 3466 01背包&&贪心

    最近,我去了一个古老的国家.在很长一段时间里,它是世界上最富有.最强大的王国.结果,这个国家的人民仍然非常自豪,即使他们的国家不再那么富有.商人是最典型的,他们每个人只卖一件商品,价格是Pi,但是如果 ...

  4. ZOJ3689 Digging(01背包)

    #include <iostream> #include <cstdio> #include<cmath> #include<algorithm> #i ...

  5. 0-1背包的动态规划算法,部分背包的贪心算法和DP算法------算法导论

    一.问题描述 0-1背包问题,部分背包问题.分别实现0-1背包的DP算法,部分背包的贪心算法和DP算法. 二.算法原理 (1)0-1背包的DP算法 0-1背包问题:有n件物品和一个容量为W的背包.第i ...

  6. TopCoder SRM502 Div1 500 贪心 01背包

    原文链接https://www.cnblogs.com/zhouzhendong/p/SRM502-500.html SRM502 Div1 500 好题. 首先,如果已经确定了解决所有问题的优先级, ...

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

    Digging Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID: 36 ...

  8. Codeforces1203F2. Complete the Projects (hard version) (贪心+贪心+01背包)

    题目链接:传送门 思路: 对于对rating有提升的项目,肯定做越多越好,所以把$b_{i} >= 0$的项目按rating要求从小到大贪心地都做掉,得到最高的rating记为r. 对于剩余的$ ...

  9. 2018.09.22 ZJOI2005午餐(贪心+01背包)

    描述 上午的训练结束了,THU ACM小组集体去吃午餐,他们一行N人来到了著名的十食堂.这里有两个打饭的窗口,每个窗口同一时刻只能给一个人打饭.由于每个人的口味(以及胃口)不同,所以他们要吃的菜各有不 ...

随机推荐

  1. Python学习笔记——基础篇【第六周】——logging模块

    常用模块之logging 用于便捷记录日志且线程安全的模块 import logging logging.basicConfig(filename='log.log', format='%(ascti ...

  2. 面试题-Java基础-开发环境

    1.什么是Java虚拟机?为什么Java被称作是“平台无关的编程语言”? Java虚拟机是一个可以执行Java字节码的虚拟机进程.Java源文件被编译成能被Java虚拟机执行的字节码文件.Java被设 ...

  3. CVE-2016-0143 漏洞分析(2016.4)

    CVE-2016-0143漏洞分析 0x00 背景 4月20日,Nils Sommer在exploitdb上爆出了一枚新的Windows内核漏洞PoC.该漏洞影响所有版本的Windows操作系统,攻击 ...

  4. python自动化Traceback (most recent call last):报错

    今天使用python.然而遇见了Traceback (most recent call last):的报错.抓狂的一笔.有说path写错的,有说是...网上查到的资料也是很少.后来突然发现,页面上我暂 ...

  5. redis11--java_jedis-test

    使用Java开发项目的时候使用Redis的话,目前有一些开源API可以使用.最常用的就是jedis,它提供了许多基于Java的对象和方法来调用Redis的指令.jedis的jar包下载地址http:/ ...

  6. 【.NET】发送Email

    首先,在web.config的appSettings配置一下. <appSettings> <add key="FromMail" value="xxx ...

  7. 虚拟机Q&A

    Q1:虚拟机运行一段时间后,各个版本的第一台虚拟机容量变大,增大容量可以接受 原因:不详 解决方法:用备份镜像替换当前镜像 1.1G Jun 24 17:07 win7_sp1_32_0.qcow2 ...

  8. vim全局替换命令

    vim全局替换命令   语法为 :[addr]s/源字符串/目的字符串/[option]全局替换命令为::%s/源字符串/目的字符串/g [addr] 表示检索范围,省略时表示当前行.如:“1,20” ...

  9. HTTP Basic Authentication认证的各种语言 后台用的

    访问需要HTTP Basic Authentication认证的资源的各种语言的实现 无聊想调用下嘀咕的api的时候,发现需要HTTP Basic Authentication,就看了下. 什么是HT ...

  10. NodeJS学习指南

    七天学会NodeJS NodeJS基础 什么是NodeJS 有啥用处 如何安装 安装程序 编译安装 如何运行 权限问题 模块 require exports module 模块初始化 主模块 完整示例 ...