首页 > 代码库 > BZOJ3315: [Usaco2013 Nov]Pogo-Cow

BZOJ3315: [Usaco2013 Nov]Pogo-Cow

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

Silver

题解:
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)
注意还要倒着做一遍
说不太清楚,看代码更简单?有点儿单调队列的感觉?
代码:
 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<iostream> 7 #include<vector> 8 #include<map> 9 #include<set>10 #include<queue>11 #include<string>12 #define inf 100000000013 #define maxn 150014 #define maxm 500+10015 #define eps 1e-1016 #define ll long long17 #define pa pair<int,int>18 #define for0(i,n) for(int i=0;i<=(n);i++)19 #define for1(i,n) for(int i=1;i<=(n);i++)20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)22 #define mod 100000000723 using namespace std;24 inline int read()25 {26     int x=0,f=1;char ch=getchar();27     while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}28     while(ch>=0&&ch<=9){x=10*x+ch-0;ch=getchar();}29     return x*f;30 }31 int n,ans=0,f[maxn][maxn];32 struct rec{int x,y;}a[maxn];33 inline bool cmp(rec a,rec b)34 {35     return a.x<b.x;36 }37 int main()38 {39     freopen("input.txt","r",stdin);40     freopen("output.txt","w",stdout);41     n=read();42     for1(i,n)a[i].x=read(),a[i].y=read();43     sort(a+1,a+n+1,cmp);44     a[0].x=-inf;45     for1(i,n)46      {47          f[i][i]=a[i].y;48          int k=i+1,tmp=f[i][i];49          for3(j,i-1,0)50           {51               while(k<=n&&a[k].x-a[i].x<a[i].x-a[j].x)52                {53                    f[k][i]=max(f[k][i],tmp+a[k].y);54                    //cout<<k<<‘ ‘<<i<<‘ ‘<<f[k][i]<<endl;55                    ans=max(ans,f[k][i]);56                    k++;57                }58               tmp=max(tmp,f[i][j]);59               if(k>n)break;60          }61     }62     //for1(i,n)for1(j,i-1)cout<<i<<‘ ‘<<j<<‘ ‘<<f[i][j]<<endl;63     memset(f,0,sizeof(f));64     a[n+1].x=inf;65     for3(i,n,1)66      {67          f[i][i]=a[i].y;68          int k=i-1,tmp=f[i][i];69          for2(j,i+1,n+1)70           {71               while(k&&a[i].x-a[k].x<a[j].x-a[i].x)72                {73                    f[k][i]=max(f[k][i],tmp+a[k].y);74                    //cout<<k<<‘ ‘<<i<<‘ ‘<<tmp<<‘ ‘<<f[k][i]<<endl;75                    ans=max(ans,f[k][i]);76                    k--;77                }78               tmp=max(tmp,f[i][j]);79               if(!k)break;80          }81      }82     printf("%d\n",ans); 83     return 0;84 }
View Code

 

BZOJ3315: [Usaco2013 Nov]Pogo-Cow