3315: [usac2013 Nov] pogo-cowtime 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 und 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 has SS his farm. at various distinct positions on the path, he places n targets on which Bessie shoshould 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 ction, 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 has es credit for every target she touches (including the initial target on which she starts ). please compute the maximum number of points she can obtain.
There are N points in a coordinate axis. Each jump to a point will get the score of this point and only jump in the same direction. However, the distance between each hop must be no less than the previous hop distance, select the starting point and obtain the maximum score.
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 input6
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 ).
Jump from a point with the coordinate of 4 to a coordinate of 5, then to a coordinate of 7 and then to a coordinate of 10. Hint Source
Silver
Question: I really think about the DP of N ^ 3. We want to figure out how to compress the time to N ^ 2n ^ 3 and find a valid next node, this has done a lot of useless work. For example, we now know that after I-> J, we can go to K, then I-> J will certainly be able to reach k + 1, so we use F [J] [I] to update the node to which it can be updated, obviously, if s [k]-s [J]> = s [J]-s [I], the status that can be transferred to K should be max (F [J] [I], f [J] [I + 1],... f [J] [J]) and S [k] are incremental. That is to say, if K can be updated, it must be updated to k + 1 and N. Therefore, we maintain the maximum value of a region and enumerate the previous node I of J. tmp records f [J] [I] .. f [J] [J] has the maximum value and K is an incremental value, so that the transfer of each node can be O (n, the complexity of the entire algorithm is O (n ^ 2). Do you have to repeat it to make it clearer? A bit monotonous queue? Code:
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: [usaco 2013 Nov] pogo-cow