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 1988
#include<iostream> #include<stdio.h> #include<algorithm> #define MAXN 30005 using ...
- 历代诗词咏宁夏注释1----常星景:< 六盘>
六盘 常星景 关中形势甲天下,四岳分峙西太华.[1] 中有汭泾经纬之,六盘嵚崎历历落.[2] □□□□其流亚,终年峰头雪不消. 弟畜太白兒美高,眼底培缕纷纷何足数,呼吸想通天尺五.[3] 西北堆镇一切 ...
- centOS学习part2:安装JDK及tomcat
0 上一篇(http://www.cnblogs.com/souvenir/p/3875424.html)给大家介绍了centOS操作系统的安装,接下来我们来介绍centOS常用软件的安装以及配置,希 ...
- python_pycharm介绍1
1. 常用设置 修改编程风格 File-Setting中,Editor下Colors&Fonts修改即可调整风格. 修改字体大小 pycharm默认字体太小,需调整些,Settings--&g ...
- C++运算符重载——重载一元运算符
0.重载一元操作符的方法 一元运算符即只需要一个操作用的运算符,如取地址运算符(&).复数(-).自减(--).自加(++)等. 运算符重载可以分为3种方式:类的非静态成员函数.类的友元函数. ...
- Android核心分析 之九Zygote Service
Zygote Service 在本章我们会接触到这两个单词: Zygote [生物] 受精卵, 接合子, 接合体 Spawn:产卵 通过这两个单词,我们就可以大体知道Zygote是干什么的了,就是叫老 ...
- Delphi对于控件的SuperClassing(封装并扩展Button,使之变成TButton)
写博客写了这么久,但是一直不知道应该怎么样写函数之间的调用关系和执行顺序,因为不停的跳来跳去的,但是写的时候却只能顺序写调用关系,直到今天发现这种写法很不错: TButton创建窗口是在CreateW ...
- C++:异常的处理
6.4 异常处理 程序中常见的错误分为两大类:编译时期的错误和运行时期的错误. 编译时期的错误比较简单容易发现:主要是语法错误,如关键字拼写错误.缺分号.括号不匹配等 运行时期的错误比较难发现,甚至是 ...
- Netty4.x中文教程系列(三) Hello World !详解
Netty 中文教程 (二) Hello World !详解 上一篇文章,笔者提供了一个Hello World 的Netty示例. 时间过去了这么久,准备解释一下示例代码. 1.HelloServer ...
- USACO Section 3.1: Score Inflation
完全背包问题 /* ID: yingzho1 LANG: C++ TASK: inflate */ #include <iostream> #include <fstream> ...