P3089 [USACO13NOV]POGO的牛Pogo-Cow

题目描述

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.

FJ给奶牛贝西的脚安装上了弹簧,使它可以在农场里快速地跳跃,但是它还没有学会如何降低速度。

FJ觉得让贝西在一条直线的一维线路上进行练习,他在不同的目标点放置了N (1 <= N <= 1000)个目标点,目标点i在目标点x(i),该点得分为p(i)。贝西开始时可以选择站在一个目标点上,只允许朝一个方向跳跃,从一目标点跳到另外一个目标点,每次跳跃的距离至少和上一次跳跃的距离相等,并且必须跳到一个目标点。

每跳到一个目标点,贝西可以拿到该点的得分,请计算他的最大可能得分。

输入输出格式

输入格式:

  • Line 1: The integer N.

  • Lines 2..1+N: Line i+1 contains x(i) and p(i), each an integer in the range 0..1,000,000.

输出格式:

  • Line 1: The maximum number of points Bessie can receive.

输入输出样例

输入样例#1: 复制

6

5 6

1 1

10 5

7 6

4 8

8 10

输出样例#1: 复制

25

说明

There are 6 targets. The first is at position x=5 and is worth 6 points, and so on.

Bessie hops from position x=4 (8 points) to position x=5 (6 points) to position x=7 (6 points) to position x=10 (5 points).

题解

比较神奇的单调队列优化。

貌似是利用了单调性并没有利用单调队列?

先来考虑\(O(n^3)\)

\(f[i][j]=max(f[j][k]+ch[i].p)\)

三重循坏枚举点再判断是否可以转移。

好现在我们来看一下怎么优化。

对于一个中间点 \(j\) ,它的左边 \(i\) 和 右边 \(k\) 分别满足

当 \(i\) 从 \(j+1\) 到 \(n\) 的时候,\(k\) 的 \(j-1\) 到 \(k\) 的范围是共用的。

\(why?\)因为我们的距离一开始已经排序了。所以 \(k\) 和 \(i\) 的总转移加起来为\(O(n)\)。

这时候我们就只要记录一下当前状态的最大值就可以了。

Code

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
const int N=1005;
int f[N][N];
int n,ans;
struct node{
int x,p;
}ch[N];
int read(){
int x=0,w=1;char ch=getchar();
while(ch>'9'||ch<'0'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*w;
} bool cmp(node a,node b){
return a.x<b.x;
} int main(){
n=read();
for(int i=1;i<=n;i++)ch[i].x=read(),ch[i].p=read();
sort(ch+1,ch+n+1,cmp);
for(int j=1;j<=n;j++){
int k=j-1,sum=ch[j].p;
for(int i=j+1;i<=n;i++){
while(k&&(ch[i].x-ch[j].x>=ch[j].x-ch[k].x))
sum=max(sum,f[j][k]),k--;
f[i][j]=max(f[i][j],sum+ch[i].p);
ans=max(ans,f[i][j]);
}
}
for(int j=n;j>=1;j--){
int k=j+1,sum=ch[j].p;
for(int i=j-1;i>=1;i--){
while(k<=n&&(ch[j].x-ch[i].x>=ch[k].x-ch[j].x))
sum=max(sum,f[j][k]),k++;
f[i][j]=max(f[i][j],sum+ch[i].p);
ans=max(ans,f[i][j]);
}
}
cout<<ans<<endl;
return 0;
}

[luogu] P3089 [USACO13NOV]POGO的牛Pogo-Cow的更多相关文章

  1. P3089 [USACO13NOV]POGO的牛Pogo-Cow

    P3089 [USACO13NOV]POGO的牛Pogo-Cow FJ给奶牛贝西的脚安装上了弹簧,使它可以在农场里快速地跳跃,但是它还没有学会如何降低速度. FJ觉得让贝西在一条直线的一维线路上进行练 ...

  2. DP【洛谷P3089】 [USACO13NOV]POGO的牛Pogo-Cow

    [洛谷P3089] [USACO13NOV]POGO的牛Pogo-Cow FJ给奶牛贝西的脚安装上了弹簧,使它可以在农场里快速地跳跃,但是它还没有学会如何降低速度. FJ觉得让贝西在一条直线的一维线路 ...

  3. P2877 [USACO07JAN]牛校Cow School(01分数规划+决策单调性分治)

    P2877 [USACO07JAN]牛校Cow School 01分数规划是啥(转) 决策单调性分治,可以解决(不限于)一些你知道要用斜率优化却不会写的问题 怎么证明?可以暴力打表 我们用$ask(l ...

  4. bzoj1638 / P2883 [USACO07MAR]牛交通Cow Traffic

    P2883 [USACO07MAR]牛交通Cow Traffic 对于每一条边$(u,v)$ 设入度为0的点到$u$有$f[u]$种走法 点$n$到$v$(通过反向边)有$f2[v]$种走法 显然经过 ...

  5. P3014 [USACO11FEB]牛线Cow Line && 康托展开

    康托展开 康托展开为全排列到一个自然数的映射, 空间压缩效率很高. 简单来说, 康托展开就是一个全排列在所有此序列全排列字典序中的第 \(k\) 大, 这个 \(k\) 即是次全排列的康托展开. 康托 ...

  6. bzoj1612 / P2419 [USACO08JAN]牛大赛Cow Contest(Floyd)

    P2419 [USACO08JAN]牛大赛Cow Contest Floyd不仅可以算最短路,还可以处理点之间的关系. 跑一遍Floyd,处理出每个点之间是否有直接或间接的关系. 如果某个点和其他$n ...

  7. 【洛谷】2990:[USACO10OPEN]牛跳房子Cow Hopscotch【单调队列优化DP】

    P2990 [USACO10OPEN]牛跳房子Cow Hopscotch 题目描述 The cows have reverted to their childhood and are playing ...

  8. 洛谷——P2952 [USACO09OPEN]牛线Cow Line

    P2952 [USACO09OPEN]牛线Cow Line 题目描述 Farmer John's N cows (conveniently numbered 1..N) are forming a l ...

  9. P2419 [USACO08JAN]牛大赛Cow Contest

    P2419 [USACO08JAN]牛大赛Cow Contest 题目背景 [Usaco2008 Jan] 题目描述 N (1 ≤ N ≤ 100) cows, conveniently number ...

随机推荐

  1. Spring管理流程部署——Activiti

    pom.xml <!-- activit jar 包 --> <dependency> <groupId>org.activiti</groupId> ...

  2. Myeclipse学习总结(6)——MyEclipse断点调试

    当程序写好之后,如何调试呢? 我们在MyEclipse中jav添加断点,运行debug as-->open debug Dialog,然后在对话框中选类后--> Run在debug视图下. ...

  3. annotation配置springMVC的方法了事务不起作用

    Spring MVC 和spring context 父子容器关系http://www.121ask.com/thread-5471-1.html 父上下文容器中保存数据源.服务层.DAO层.事务的B ...

  4. BA-siemens-ppm模块调试

    第一部分:现场接线 1. 拨码:朝向数字那一端为0,远离数字那一端为1,PPM的地址设定方法就是将拨码器拨为跟系统架构表一样的数字,比如一个1U32的编号为77020,那么它的编号就是20,将4和16 ...

  5. C/C++知识要点5——智能指针原理及自己定义实现

    智能指针概述: 智能指针用来管理动态对象.其行为类似于常规指针,重要的差别是:它负责自己主动释放所指向的对象. C++ 11标准库提供两种智能指针:shared_ptr.unique_ptr 差别是: ...

  6. flume採集数据导入elasticsearch 配置

    Flume启动通常会报两种错,一种是log4j没有配置,第二种就是缺少各种jar包.SO: [root@laiym ~]# cp /usr/local/elasticsearch/lib/*/usr/ ...

  7. Fatal error: Incompatible file format: The encoded file has format major ID 1...解决方式

    申请好域名和空间后.将站点源代码上传到空间,解析好域名后.在地址栏输入域名出现以下错误: Fatal error: Incompatible file format: The encoded file ...

  8. NFS的搭建(sudo apt-get install nfs-kernel-server),TFTP服务器(sudo apt-get install tftpd-hpa tftp-hpa)

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/Osean_li/article/details/53240705 ***************** ...

  9. 【BZOJ 2038】小Z的袜子

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2038 [算法] 莫队算法 [代码] #include<bits/stdc++. ...

  10. DB-SQL-MySQL-杂项-调优:Mysql千万以上数据优化、SQL优化方法

    ylbtech-DB-SQL-MySQL-杂项-调优:Mysql千万以上数据优化.SQL优化方法 1.返回顶部 1. 1,单库表别太多,一般保持在200以下为宜 2,尽量避免SQL中出现运算,例如se ...