USACO 2013 Nov Silver Pogo-Cow
最近因为闲的蛋疼(停课了),所以开始做一些 USACO 的银组题。被完虐啊 TAT
貌似 Pogo-Cow 这题是 2013 Nov Silver 唯一一道可说的题目?
Pogo-Cow
- Description
(大意是一条直线上有一些带权值的点,可以选择一个点作为出发点,选好一个前进方向(左或右)然后不断地向前跳到另一个点,得分为这个点的权值,要求每一跳的跳跃距离不小于前一跳,求能获得的最大得分)
In an ill-conceived attempt to enhance the mobility of his prize cow
Bessie, Farmer John has attached a pogo stick to each of Bessie's legs.
Bessie can now hop around quickly throughout the farm, but she has not yet
learned how to slow down.
To help train Bessie to hop with greater control, Farmer John sets up a
practice course for her along a straight one-dimensional path across his
farm. At various distinct positions on the path, he places N targets on
which Bessie should try to land (1 <= N <= 1000). Target i is located at
position x(i), and is worth p(i) points if Bessie lands on it. Bessie
starts at the location of any target of her choosing and is allowed to move
in only one direction, hopping from target to target. Each hop must cover
at least as much distance as the previous hop, and must land on a target.
Bessie receives credit for every target she touches (including the initial
target on which she starts). Please compute the maximum number of points
she can obtain.
- Solution
想了好一会儿,到晚自习下课的时候想到了算法,然后回寝室冷静了一个晚上,第二天实现了一下。
先只考虑一个方向上的。(另一个方向的话只要反向做一遍就好了)
首先,肯定要先按照横坐标将这些点排序。
其实 O(N^3) 的 DP 是不难想到的:用 f(i, j) 表示从 i 跳到 j 再往后跳的最大得分(因为只考虑单方向所以假设 j > i),则
f(i, j) = w(i)+max{ f(j, k) }, k>j 且 x(k)-x(j) >= x(j)-x(i)
w(i) 表示第 i 个点的权值。
然后优化:
对于上面方程中的 k,我们只要找到第一个满足条件的 k 就会发现 k+1, k+2, ..., n 都是满足条件的。
那么我们干脆维护一个区间最大值:用 m(i, j) 表示 max{ f(i, j), f(i, j+1), ..., f(i, n) }
于是上面的方程就可以改写成:
f(i, j) = w(i)+m(j, k), k>j 且 k 是满足条件 x(k)-x(j) >= x(j)-x(i) 的第一个点
显然我们可以用二分查找来找到这个 k。
至于 m(i, j) 的维护,因为 k 在 j 的后面所以我们是倒着来 DP(i 和 j 逆序枚举),那么得到一个 f(i, j) 之后就可以更新 m(i, j) 的值了:
m(i, j) = max{ f(i, j), m(i, j+1) }
至此,问题的复杂度降为 O(N2logN)。
这种方法其实是很普遍的,比如下面这道同样是 USACO Silver 题:
POJ 3616 Milking Time
Description
Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that she decides to schedule her next N (1 ≤ N ≤ 1,000,000) hours (conveniently labeled 0..N-1) so that she produces as much milk as possible.
Farmer John has a list of M (1 ≤ M ≤ 1,000) possibly overlapping intervals in which he is available for milking. Each interval i has a starting hour (0 ≤ starting_houri ≤ N), an ending hour (starting_houri < ending_houri ≤ N), and a corresponding efficiency (1 ≤ efficiencyi ≤ 1,000,000) which indicates how many gallons of milk that he can get out of Bessie in that interval. Farmer John starts and stops milking at the beginning of the starting hour and ending hour, respectively. When being milked, Bessie must be milked through an entire interval.
Even Bessie has her limitations, though. After being milked during any interval, she must rest R (1 ≤ R ≤ N) hours before she can start milking again. Given Farmer Johns list of intervals, determine the maximum amount of milk that Bessie can produce in the N hours.
Input
* Line 1: Three space-separated integers: N, M, and R
* Lines 2..M+1: Line i+1 describes FJ's ith milking interval withthree space-separated integers: starting_houri , ending_houri , and efficiencyi
Output
* Line 1: The maximum number of gallons of milk that Bessie can product in the N hours
Sample Input
12 4 2
1 2 8
10 12 19
3 6 24
7 10 31
Sample Output
43
Source
题目大意是有 M 个可能有重叠的时间段,每个时间段有不同的收益,选出一些时间段满足前一个时间段的结束时刻到后一个时间段的开始时刻至少有 R 的间隔时间,求能达到的最大收益。
其实这道题完全可以用平方级的算法操过去,但是本着闲着蛋疼就来折腾 POJ 服务器的原则,我们可以用上述方法优化到 O(NlogN):(当然先把区间按照结束时间排序)用 f(i) 表示最后一个区间是第 i 个的时候所能得到的最大收益,则
f(i) = w(i)+max{ f(k) }, k<i 且 end_time(k)+R<=start_time(i)
然后用上面的方法二分找到第一个满足条件的 k,并维护一个连续区间的最大值。注意这次应该正着来(因为要找的 k 在i 前面嘛)。
应 LZW 大神要求写得更详细一些:
令 m(i) = max{ f(1), f(2), ..., f(i-1), f(i) },则动规方程可以改为:
f(i) = w(i)+m(k), k<i 且 k 是满足条件 end_time(k)+R<=start_time(i) 的最靠后的时间段
同样地,m(i) 在得到一个 f(i) 之后维护:
m(i) = max{ f(i), m(i-1) }
(但是时间上和 LZW 大神的平方算法一样啊,给常数小得人神共怒的 LZW 大牛跪了)
(估计 ZBT 大神会吐槽我:「就这两个破题目你也要写篇题解?」)
USACO 2013 Nov Silver Pogo-Cow的更多相关文章
- USACO翻译:USACO 2013 NOV Silver三题
USACO 2013 NOV SILVER 一.题目概览 中文题目名称 未有的奶牛 拥挤的奶牛 弹簧牛 英文题目名称 nocow crowded pogocow 可执行文件名 nocow crowde ...
- USACO翻译:USACO 2013 DEC Silver三题
USACO 2013 DEC SILVER 一.题目概览 中文题目名称 挤奶调度 农场航线 贝西洗牌 英文题目名称 msched vacation shuffle 可执行文件名 msched vaca ...
- USACO 2007 February Silver The Cow Lexicon /// DP oj24258
题目大意: 输入w,l: w是接下来的字典内的单词个数,l为目标字符串长度 输入目标字符串 接下来w行,输入字典内的各个单词 输出目标字符串最少删除多少个字母就能变成只由字典内的单词组成的字符串 Sa ...
- BZOJ2017[USACO 2009 Nov Silver 1.A Coin Game]——DP+博弈论
题目描述 农夫约翰的奶牛喜欢玩硬币游戏,因此他发明了一种称为“Xoinc”的两人硬币游戏. 初始时,一个有N(5 <= N <= 2,000)枚硬币的堆栈放在地上,从堆顶数起的第I枚硬币的 ...
- USACO 2007 November Silver Best Cow Line /// oj21653
题目大意: 输入n 接下来n行字母 在队头和队尾中选出较小的放入新的队列 Sample Input 6ACDBCB Sample Output ABCBCD 注意相同的情况 先判断内层的大小 输出 ...
- USACO 2013 January Silver Painting the Fence /// oj23695
题目大意: 输入n,k :n次操作 找到覆盖次数在k及以上的段的总长 一开始位置在0 左右活动范围为1-1000000000 接下来n行描述每次操作的步数和方向 Sample Input 6 22 R ...
- usaco No Change, 2013 Nov 不找零(二分查找+状压dp)
Description 约翰带着 N 头奶牛在超市买东西,现在他们正在排队付钱,排在第 i 个位置的奶牛需要支付 Ci 元.今天说好所有东西都是约翰请客的,但直到付账的时候,约翰才意识到自己没带钱,身 ...
- USACO翻译:USACO 2013 JAN三题(1)
USACO 2013 JAN 一.题目概览 中文题目名称 镜子 栅栏油漆 奶牛排队 英文题目名称 mirrors paint lineup 可执行文件名 mirrors paint lineup 输入 ...
- NC25025 [USACO 2007 Nov G]Sunscreen
NC25025 [USACO 2007 Nov G]Sunscreen 题目 题目描述 To avoid unsightly burns while tanning, each of the \(C\ ...
随机推荐
- POJ 2041
#include <iostream> #include <string> #include <algorithm> using namespace std; st ...
- POJ 1547
#include<iostream> #include<string> using namespace std; int main() { int length; int hi ...
- php截取字符串中的关键字,并高亮显示
<?php $str = "hahaceshi测试一下关键字高亮显示,以及长字符串截取的问题!"; $key = "关键字"; $r = sub_key_ ...
- C++ std命名空间
1.命名空间是一种特殊的作用域,它包含了处于该作用域中所有标示符.命名空间使用namespace 来声明,并使用{}来界定命名空间的作用域,例如: namespace func{ int val=0; ...
- iOS多线程 GCD
iOS多线程 GCD Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法. dispatch queue分成以下三种: 1)运行在主线程的Main que ...
- 305. Number of Islands II
题目: A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand ...
- Linux SHELL脚本
在Linux系统中,虽然有各种各样的图形化接口工具,但是sell仍然是一个非常灵活的工具.Shell不仅仅是命令的收集,而且是一门非常棒的编程语言.可以通过使用shell使大量的任务自动化,shell ...
- dojo 四 类的构造函数和父方法的调用
与java类一样,在Dojo里也可以定义constructor 构造函数,在创建一个实例时可以对需要的属性进行初始化.//定义一个类Mqsy_yj var Mqsy_YJ = declare(null ...
- struts配置。泪奔...
说多了都是泪啊,配置一个环境一天才搞定.不错the requested resource (/login) is not available in struts,就是找不到什么什么class.亦或th ...
- hdu - 1728逃离迷宫 && hdu - 1175 连连看 (普通bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1728 这两道题花了一下午的时候调试,因为以前做过类似的题,但是判断方向的方法是错的,一直没发现啊,真无语. 每个 ...