P2990 [USACO10OPEN]牛跳房子Cow Hopscotch

题目描述

The cows have reverted to their childhood and are playing a game similar to human hopscotch. Their hopscotch game features a line of N (3 <= N <= 250,000) squares conveniently labeled 1..N that are chalked onto the grass.

Like any good game, this version of hopscotch has prizes! Square i is labeled with some integer monetary value V_i (-2,000,000,000 <= V_i <= 2,000,000,000). The cows play the game to see who can earn the most money.

The rules are fairly simple:

* A cow starts at square '0' (located just before square 1; it has no monetary value).

* She then executes a potentially empty sequence of jumps toward square N. Each square she lands on can be a maximum of K (2 <= K <= N) squares from its predecessor square (i.e., from square 1, she can jump outbound to squares 2 or 3 if K==2).

* Whenever she wishes, the cow turns around and jumps back towards square 0, stopping when she arrives there. In addition to the restrictions above (including the K limit), two

additional restrictions apply:

* She is not allowed to land on any square she touched on her outbound trip (except square 0, of course).

* Except for square 0, the squares she lands on during the return trip must directly precede squares she landed on

during the outbound trip (though she might make some larger leaps that skip potential return squares altogether).

She earns an amount of money equal to the sum of the monetary values of all the squares she jumped on. Find the largest amount of cash a cow can earn.

By way of example, consider this six-box cow-hopscotch course where K has the value 3:

Square Num: 0 1 2 3 4 5 6 

+---+ +---+ +---+ +---+ +---+ +---+ +---+ 

|///|--| |--| |--| |--| |--| |--| | 

+---++---+ +---+ +---+ +---+ +---+ +---+ 

Value: - 0 1 2 -3 4 5 

One (optimal) sequence Bessie could jump (shown with respective bracketed monetary values) is: 1[0], 3[2], 6[5], 5[4], 2[1], 0[0] would yield a monetary total of 0+2+5+4+1+0=12.

If Bessie jumped a sequence beginning with 0, 1, 2, 3, 4, ... then she would be unable to return since she could not legally jump back to an untouched square.

奶牛们正在回味童年,玩一个类似跳格子的游戏,在这个游戏里,奶

牛们在草地上画了一行N个格子,(3 <=N <= 250,000),编号为1..N。

就像任何一个好游戏一样,这样的跳格子游戏也有奖励!第i个格子标有一个数字V_i(-2,000,000,000 <=V_i <= 2,000,000,000)表示这个格子的钱。奶牛们想看看最后谁能得到最多的钱。

规则很简单:

* 每个奶牛从0号格子出发。(0号格子在1号之前,那里没钱)

* 她向N号格子进行一系列的跳跃(也可以不跳),每次她跳到的格子最多可以和前一个落脚的格子差K格(1 <= K <= N)(比方说,当前在1号格,K=2, 可以跳到2号和3号格子)

*在任何时候,她都可以选择回头往0号格子跳,直到跳到0号格子。另外,除了以上规则之外,

回头跳的时候还有两条规则:

*不可以跳到之前停留的格子。

*除了0号格子之外,她在回来的时候,停留的格子必须是恰巧过去的时候停留的某个格子的前一格(当然,也可以跳过某些过去…

输入输出格式

输入格式:

* Line 1: Two space separated integers: N and K

* Lines 2..N+1: Line i+1 contains a single integer: V_i

输出格式:

* Line 1: A single line with a single integer that is the maximum amount of money a cow can earn

英文看着头大,实际上就是在坐标轴上跳,每次最多跳$k$步,还要跳回来,而跳回来时踩的点必须是跳过去时经过过的点的前一个,求这样跳过去跳回原点能获得的最大价值。

而很容易想到,因为回来时的路径选择是依赖过去的路径,我们可以DP过去的路径,同时为回来的路径预留位置,所以我们的DP方程$f[i]$表示过去时跳到$i$位置此时能获得的最大价值,转移就是从$f[i-k]-f[i-2]$中取max(就是选最优决策点),再加上选的最有决策点到当前位置$i$中区间正数的和(因为k距离以内可以随便跳,我们贪心选择这段区间价值大于0的跳就行了),最后加上$a[i]$和$a[i-1]$就行了。

因为要找的实际上就是在k范围内的最大$f[j]-pre[j]$,很容易想到用单调队列来优化。然后问题就解决了,主要是各种细节。

首先要保证队列中能更新$i$的决策点都是在$i-k$到$i-2$之间的,所以随着$i$的加入顺势加入当前新的决策点$j$就可以了。

优先队列一般都是先删掉劣的尾,加入当前的新值,再删过期的头,最后更新要求得的$dp$方程。删尾时要保证队列内不为空,删头时除过期的还要满足题目要求的限定条件,比如队列内必须至少存在几个元素。

这道题最后统计答案时不能忘了,一步跳过去和一步跳回来也可能是一个答案,要先定$ans$初值。

#include<bits/stdc++.h>
#define LL long long
using namespace std; int n, k;
int a[];
LL q[], pre[], f[]; int main() {
scanf("%d%d", &n, &k);
for(int i = ; i <= n; i ++) {
scanf("%d", &a[i]);
pre[i] = pre[i-] + (a[i] > ? a[i] : );
}
int h = , t = ;
q[++t] = ; f[] = a[];
for(int i = ,j = ; i <= n; i ++,j ++) {//因为队列中位置必须小于等于i-2才能更新i 所以定义j来直接表示
LL tmp = f[j] - pre[j];
while(t - h > && tmp >= f[q[t]] - pre[q[t]]) t --;
q[++t] = j;
while(i - q[h+] > k && t - h > ) h ++; ///////是由i来判断队列中点是否过期
f[i] = f[q[h+]] - pre[q[h+]] + a[i] + a[i-] + pre[i-];
}
LL ans = pre[min(n, k)];//跳k步过去再直接跳回来
for(int i = ; i <= n; i ++) {
int to = min(n, i - + k);
ans = max(ans, f[i] + pre[to] - pre[i]);
}
printf("%lld", ans);
return ;
}

【洛谷】2990:[USACO10OPEN]牛跳房子Cow Hopscotch【单调队列优化DP】的更多相关文章

  1. 洛谷P3195 [HNOI2008]玩具装箱TOY(单调队列优化DP)

    题目描述 P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为1...N的N件玩具, ...

  2. [USACO10OPEN]牛跳房子Cow Hopscotch

    题目描述 奶牛们正在回味童年,玩一个类似跳格子的游戏,在这个游戏里,奶 牛们在草地上画了一行N个格子,(3 <=N <= 250,000),编号为1..N. 就像任何一个好游戏一样,这样的 ...

  3. 2018.09.26洛谷P3957 跳房子(二分+单调队列优化dp)

    传送门 表示去年考普及组的时候失了智,现在看来并不是很难啊. 直接二分答案然后单调队列优化dp检验就行了. 注意入队和出队的条件. 代码: #include<bits/stdc++.h> ...

  4. 洛谷 P3957 跳房子 —— 二分答案+单调队列优化DP

    题目:https://www.luogu.org/problemnew/show/P3957 先二分一个 g,然后判断: 由于转移的范围是一个区间,也就是滑动窗口,所以单调队列优化: 可以先令队尾为 ...

  5. 洛谷P2627 [USACO11OPEN]Mowing the Lawn G (单调队列优化DP)

    一道单调队列优化DP的入门题. f[i]表示到第i头牛时获得的最大效率. 状态转移方程:f[i]=max(f[j-1]-sum[j])+sum[i] ,i-k<=j<=i.j的意义表示断点 ...

  6. 洛谷P2216: [HAOI2007]理想的正方形 单调队列优化DP

    洛谷P2216 )逼着自己写DP 题意: 给定一个带有数字的矩阵,找出一个大小为n*n的矩阵,这个矩阵中最大值减最小值最小. 思路: 先处理出每一行每个格子到前面n个格子中的最大值和最小值.然后对每一 ...

  7. P3957 跳房子(二分答案+单调队列优化DP)

    题目链接:https://www.luogu.org/contestnew/show/4468 题目大意:跳房子,也叫跳飞机,是一种世界性的儿童游戏,也是中国民间传统的体育游戏之一. 跳房子的游戏规则 ...

  8. [NOIP2017普及组]跳房子(二分,单调队列优化dp)

    [NOIP2017普及组]跳房子 题目描述 跳房子,也叫跳飞机,是一种世界性的儿童游戏,也是中国民间传统的体育游戏之一. 跳房子的游戏规则如下: 在地面上确定一个起点,然后在起点右侧画 nn 个格子, ...

  9. 洛谷 P3120 [USACO15FEB]牛跳房子(金)Cow Hopscotch (Gold)

    P3120 [USACO15FEB]牛跳房子(金)Cow Hopscotch (Gold) 就像人类喜欢跳格子游戏一样,FJ的奶牛们发明了一种新的跳格子游戏.虽然这种接近一吨的笨拙的动物玩跳格子游戏几 ...

随机推荐

  1. cookie知识点概述

    cookie是什么 这个讲起来很简单,了解http的同学,肯定知道,http是一个不保存状态的协议,什么叫不保存状态,就是一个服务器是不清楚是不是同一个浏览器在访问他,在cookie之前,有另外的技术 ...

  2. nesC编程入门

    1.接口 NesC程序主要由各式组件(component)构成,组件和组件之间通过特定的接口(interface)互相沟通.一个接口内声明了提供相关服务的方法(C语言函数).例如数据读取接口(Read ...

  3. module 'pytest' has no attribute 'allure'问题解决

    安装allure后执行命令后报错module 'pytest' has no attribute 'allure' C:\Users\Desktop\xin>pytest -s -q --all ...

  4. nginx升级至1.12.1版本

    nginx升级至1.12.1 编号 名称 说明 1 nginx-1.12.1.tar.gz nginx安装程序 2 nginx_upstream_check_module-master.zip 实现后 ...

  5. Entity Framework 5.0 Code First全面学习 (转)

    原文地址:感谢原文作者 http://blog.csdn.net/gentle_wolf/article/details/14004345 不贴图片了,太累. Code First 约定 借助 Cod ...

  6. AspNet Core 发布到Linux系统和发布IIS 注意项

    AspNet Core 发布到Linux系统和发布IIS 注意项 1.发布时需要注意的 2.Windows Server 2012 api-ms-win-crt-runtime-l1-1-0.dll ...

  7. 一键去除网页BOM属性【解决乱码,头部空白,&#65279问题】

    几个常出现的问题: 1.网站打开空白 2.页面头部出现多余的空白 3.网站出现乱码,如“锘�” 解决方法可以是: 1.选用专业的编辑器,例如notepad++,sublime,editplus这样不会 ...

  8. 为什么需要学UML建模

    今天在看<设计模式>的时候,看到了许多的UML模型图,案例中作者用极少的代码却能讲清楚讲好设计模式的背景和思想,抽象成一张张的UML图就能很好的review和复盘,这对于在工作中习惯用代码 ...

  9. Java容器---Collection接口中的共有方法

    1.Collection 接口 (1)Collection的超级接口是Iterable (2)Collection常用的子对象有:Map.List.Set.Queue. 右图中实现黑框的ArrayLi ...

  10. interesting Integers(数学暴力||数论扩展欧几里得)

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAwwAAAHwCAIAAACE0n9nAAAgAElEQVR4nOydfUBT1f/Hbw9202m0r8