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

——————————————————————————————————————————————————————————————————————————————————————————————

转:http://hi.baidu.com/oldjunyi/item/8391d3e63ae1493986d9decd?qq-pf-to=pcqq.c2c

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

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

先不考虑总天数 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 部分是一个类似于背包的写法,照着题目里给的计算方式去算就行了

——————————————————————————————————————————————————————————————————————————————————————————————

至于排序后的DP和没排序的DP有什么不一样,那是因为根据背包DP的性质,回想一下工作原理可以发现,序号小的物品一定会放在序号大的物品前面,那么就能够符合上述要求了。

ZOJ 3689 Digging(DP)的更多相关文章

  1. ZOJ 3689 Digging(贪心+dp)

    Digging Time Limit: 2 Seconds      Memory Limit: 65536 KB When it comes to the Maya Civilization, we ...

  2. Digging(DP)

    ZOJ Problem Set - 3689 Digging Time Limit: 2 Seconds      Memory Limit: 65536 KB When it comes to th ...

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

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

  4. ZOJ - 3822 Domination (DP)

    Edward is the headmaster of Marjar University. He is enthusiastic about chess and often plays chess ...

  5. ZOJ 3769-Diablo III(DP)

    描述 Diablo III is an action role-playing video game. A few days ago, Reaper of Souls (ROS), the new e ...

  6. zoj 3706 Break Standard Weight(dp)

    Break Standard Weight Time Limit: 2 Seconds                                     Memory Limit: 65536 ...

  7. ZOJ 4027 Sequence Swapping(DP)题解

    题意:一串括号,每个括号代表一个值,当有相邻括号组成()时,可以交换他们两个并得到他们值的乘积,问你最大能得到多少 思路:DP题,注定想得掉头发. 显然一个左括号( 的最远交换距离由他右边的左括号的最 ...

  8. ZOJ Problem Set - 3822Domination(DP)

    ZOJ Problem Set - 3822Domination(DP) problemCode=3822">题目链接 题目大意: 给你一个n * m的棋盘,每天都在棋盘上面放一颗棋子 ...

  9. 2014百度之星资格赛 1004:Labyrinth(DP)

    Labyrinth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

随机推荐

  1. SSH整合(一)

    一.ssh原始整合方式 不需要任何整合包,就是简单的将三个框架集合到一起 hibernate        导入jar包:            hibernate-release-5.0.7.Fin ...

  2. 『ACM C++』HDU杭电OJ | 1416 - Gizilch (DFS - 深度优先搜索入门)

    从周三课开始总算轻松了点,下午能在宿舍研究点题目啥的打一打,还好,刚开学的课程还算跟得上,刚开学的这些课程也是复习以前学过的知识,下半学期也不敢太划水了,被各种人寄予厚望之后瑟瑟发抖,只能努力前行了~ ...

  3. 在Java中用正则表达式判断一个字符串是否是数字的方法

    package chengyujia; import java.util.regex.Pattern; public class NumberUtil { /** * 判断一个字符串是否是数字. * ...

  4. HTML a的连接

    QQ电脑端 <a target="_blank" href="http://wpa.qq.com/msgrd?v=3&uin=XXXXXX&site ...

  5. ethereum(以太坊)(实例)--"简单的公开竞拍"

    说真的,刚开始接触这个竞拍案例--“简单的公开竞拍”,我就抱着简单的心态去查看这个实例,但是自我感觉并不简单.应该是我实力不到家的原因吧!!!233333...不过经过大半天的努力,自己理解完之后,觉 ...

  6. linux 操作系统下简单的命令行操作

    一: 配置linux 操作系统虚拟主机 首先安装一个虚拟机(百度上面有很多哦) , 我主要使用的是VMware workstation 然后 下载一个centos镜像6..5到7都可以; 然后用VMw ...

  7. openWrt libubox组件之uloop原理分析

    1.    libubox概述 libubox是openwrt新版本中的一个基础库,有很多应用是基于libubox开发的,如uhttpd,netifd,ubusd等. libubox主要提供以下两种功 ...

  8. 面试乐融集团Python开发工程师有感

    这是笔者第一次面试,,乐融集团位于朝阳区朝阳公园的乐融大厦.是下午两点的笔面试,笔者是一点半到的,然后在里面等了会,开始笔试 笔试题并不是太难,就是考的比较宽,因为笔者是校招,所以笔试题出来了数据结构 ...

  9. Stream的顺序流与并行流

    /** * @auther hhh * @date 2019/1/2 22:52 * @description */ public class StreamAPI2 { /** * 流的特性:支持并行 ...

  10. 路由追踪:traceroute/tcptraceroute

    一.工作原理 traceroute:IP路由过程中对数据包TTL(Time to Live,存活时间)进行处理.当路由器收到一个IP包时,会修改IP包的TTL(及由此造成的头部检验和checksum变 ...