3315: [Usaco2013 Nov]Pogo-Cow

Time Limit: 3 Sec  Memory Limit: 128 MB
Submit: 143  Solved: 79
[Submit][Status]

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.

一个坐标轴有N个点,每跳到一个点会获得该点的分数,并只能朝同一个方向跳,但是每一次的跳跃的距离必须不小于前一次的跳跃距离,起始点任选,求能获得的最大分数。

Input

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

Output

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

Sample Input

6
5 6
1 1
10 5
7 6
4 8
8 10

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

Sample Output

25
OUTPUT DETAILS: 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).

从坐标为4的点,跳到坐标为5的,再到坐标为7和,再到坐标为10的。

HINT

 

Source

题解:
n^3的dp很好想,我们想一下如何把时间压缩成 n^2
n^3时间主要花费在寻找合法的下一个节点上,这样做了很多无用功
比如说  我们现在已经知道  i->j 之后 能到 k,那么 i->j 之后也一定能到k+1
所以我们用 f[j][i]来更新它能更新到的节点,
显然如果 s[k]-s[j]>=s[j]-s[i] 那么能转移到 k 的状态应该是 max(f[j][i],f[j][i+1],.......f[j][j])
而s[k]是递增的,也就是说能更新到 k,那么一定能更新到 k+1以及n。
所以我们维护一个区域最大值,枚举 j的前一个节点 i,tmp记录 f[j][i]..f[j][j]的最大值
然后 k 是一个递增的,这样每个节点的转移是可以做到O(n)的,整个算法的复杂度就是O(n^2)
注意还要倒着做一遍
说不太清楚,看代码更简单?有点儿单调队列的感觉?
代码:
 #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#define inf 1000000000
#define maxn 1500
#define maxm 500+100
#define eps 1e-10
#define ll long long
#define pa pair<int,int>
#define for0(i,n) for(int i=0;i<=(n);i++)
#define for1(i,n) for(int i=1;i<=(n);i++)
#define for2(i,x,y) for(int i=(x);i<=(y);i++)
#define for3(i,x,y) for(int i=(x);i>=(y);i--)
#define mod 1000000007
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
return x*f;
}
int n,ans=,f[maxn][maxn];
struct rec{int x,y;}a[maxn];
inline bool cmp(rec a,rec b)
{
return a.x<b.x;
}
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
n=read();
for1(i,n)a[i].x=read(),a[i].y=read();
sort(a+,a+n+,cmp);
a[].x=-inf;
for1(i,n)
{
f[i][i]=a[i].y;
int k=i+,tmp=f[i][i];
for3(j,i-,)
{
while(k<=n&&a[k].x-a[i].x<a[i].x-a[j].x)
{
f[k][i]=max(f[k][i],tmp+a[k].y);
//cout<<k<<' '<<i<<' '<<f[k][i]<<endl;
ans=max(ans,f[k][i]);
k++;
}
tmp=max(tmp,f[i][j]);
if(k>n)break;
}
}
//for1(i,n)for1(j,i-1)cout<<i<<' '<<j<<' '<<f[i][j]<<endl;
memset(f,,sizeof(f));
a[n+].x=inf;
for3(i,n,)
{
f[i][i]=a[i].y;
int k=i-,tmp=f[i][i];
for2(j,i+,n+)
{
while(k&&a[i].x-a[k].x<a[j].x-a[i].x)
{
f[k][i]=max(f[k][i],tmp+a[k].y);
//cout<<k<<' '<<i<<' '<<tmp<<' '<<f[k][i]<<endl;
ans=max(ans,f[k][i]);
k--;
}
tmp=max(tmp,f[i][j]);
if(!k)break;
}
}
printf("%d\n",ans);
return ;
}

BZOJ3315: [Usaco2013 Nov]Pogo-Cow的更多相关文章

  1. Bzoj3315 [Usaco2013 Nov]Pogo-Cow(luogu3089)

    3315: [Usaco2013 Nov]Pogo-Cow Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 352  Solved: 181[Submit ...

  2. bzoj1742[Usaco2005 nov]Grazing on the Run 边跑边吃草*&&bzoj3074[Usaco2013 Mar]The Cow Run*

    bzoj1742[Usaco2005 nov]Grazing on the Run 边跑边吃草 bzoj3074[Usaco2013 Mar]The Cow Run 题意: 数轴上有n棵草,牛初始在L ...

  3. BZOJ 3315: [Usaco2013 Nov]Pogo-Cow( dp )

    我真想吐槽USACO的数据弱..= = O(n^3)都能A....上面一个是O(n²), 一个是O(n^3) O(n^3)做法, 先排序, dp(i, j) = max{ dp(j, p) } + w ...

  4. BZOJ3314: [Usaco2013 Nov]Crowded Cows

    3314: [Usaco2013 Nov]Crowded Cows Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 86  Solved: 61[Subm ...

  5. BZOJ 3314: [Usaco2013 Nov]Crowded Cows( 单调队列 )

    从左到右扫一遍, 维护一个单调不递减队列. 然后再从右往左重复一遍然后就可以统计答案了. ------------------------------------------------------- ...

  6. 3314: [Usaco2013 Nov]Crowded Cows

    3314: [Usaco2013 Nov]Crowded Cows Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 111  Solved: 79[Sub ...

  7. BZOJ1640: [Usaco2007 Nov]Best Cow Line 队列变换

    1640: [Usaco2007 Nov]Best Cow Line 队列变换 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 493  Solved: 2 ...

  8. 1640: [Usaco2007 Nov]Best Cow Line 队列变换

    1640: [Usaco2007 Nov]Best Cow Line 队列变换 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 543  Solved: 2 ...

  9. 【BZOJ3312】[Usaco2013 Nov]No Change 状压DP+二分

    [BZOJ3312][Usaco2013 Nov]No Change Description Farmer John is at the market to purchase supplies for ...

随机推荐

  1. jquery animate

    $(".logo").animate( { opacity: .25, //将不透明度逐渐变成.25 height: 0 //高度逐渐变成0 }, { duration: 1000 ...

  2. 两个关于XML解析报错问题小记

    Caused by: org.xml.sax.SAXParseException: The string "--" is not permitted within comments ...

  3. 关于linux内存管理

     Linux的内存管理主要分为两部分:物理地址到虚拟地址的映射,内核内存分配管理(主要基于slab). 物理地址到虚拟地址之间的映射 1.概念 物理地址(physical address) 用于内存芯 ...

  4. 【酷Q插件制作】教大家做一个简单的签到插件

    酷Q插件已经有很多了,社区分享一大堆,不过还是自己写才有乐趣,哈哈.不得不吐槽一下,酷Q竟然不更新了,出了个酷Q pro,还收费!!诶.不过这也影响不了咱写插件的心情,今天教大家写一个酷Q签到插件,虽 ...

  5. Android组件间的数据传输

    组件我们有了,那么我们缺少一个组件之间传递信息的渠道.利用Intent做载体,这是一个王道的做法.还有呢,可以利用文件系统来做数据共享.也可以使用Application设置全局数据,利用组件来进行控制 ...

  6. ClickOnce发布后不能安装

    当在internet发布用ClickOnce打包的客户端程序时,遇到ClickOnce启动后出错,错误信息如下: + Downloading https://xxxxx/Deploy/pc/Boote ...

  7. CodeSMART for VS.NET插件工具

    今天无聊,想起以前看过的微软的Visual Studio的插件,所以就找了找. 微软的Visual Studio本身就非常强大了,但是仍然有不足的地方,比如下面要介绍的我喜欢的代码格式化功能的这个插件 ...

  8. MultipeerConnectivity

    Multipeer connectivity是一个使附近设备通过Wi-Fi网络.P2P Wi-Fi以及蓝牙个人局域网进行通信的框架.互相链接的节点可以安全地传递信息.流或是其他文件资源,而不用通过网络 ...

  9. CUICatalog: Invalid asset name supplied: (null)

    出现这个问题的根本原因是你调用了[UIImage imageNamed:name]这个方法 但是name = nil;所以报出该错误.   解决方法,在项目中搜索[UIImage imageNamed ...

  10. 外国高手画神级的linux 内核图,够详细!