2442: [usaco 128 open] lawn trimming time limit: 10 sec memory limit: MB
Submit: 500 solved: 244
[Submit] [Status] Description
After winning the town's best lawn match a year ago, FJ became lazy and never trimmed the lawn any more. Now,
The next round of the best lawn competition started again, and FJ hopes to win the championship again.
However, FJ's lawn is very messy, so FJ can only let his cows do the job. FJ has n
(1 <= n <= 100,000) Only a row of cows numbered 1... n. The efficiency of each cow is different,
The efficiency of cow I is e_ I (0 <= e_ I <= 1,000,000,000 ).
The cows nearby are familiar with this, so if FJ has arranged more Than k continuous cows, these cows will go on strike.
Go to the party :). Therefore, FJ needs your help to calculate the maximum efficiency that FJ can get.
No more than K cows in a row.
Input
* First line: two integers N and K separated by Spaces
* Row 2 to n + 1: Row I + 1 has an integer e_ I
Output
* First line: A value that indicates the maximum efficiency value that FJ can obtain.
Sample input5 2
1
2
3
4
5
Input explanation:
FJ has 5 cows, which are efficient at 1, 2, 3, 4, and 5. They want to select the cows with the highest total efficiency,
He cannot select more than two consecutive cows.
Sample output
12
FJ can select other cows than the third. The total efficiency is 1 + 2 + 4 + 5 = 12.
Hintsource
Gold
Question:
At the beginning, I saw a rush of lines and trees. Later I found the situation a little complicated...
Why is the code in status so short? So it must not be a line segment tree.
Consider DP
F [I] indicates the maximum sum obtained by selecting a [I ].
G [I] indicates the maximum sum that can be obtained without selecting a [I ].
Then f [I] = max (G [J] + s [I]-s [J]) = max (G [J]-s [J]) + s [I] I-j <= K
G [I] = max (G [I-1], F [I-1])
Then we find that the J range that can be updated to I is monotonous, And we require the maximum value in a range.
This reminds us of monotonous queues, and then we can abuse them casually.
Code;
1 #include<cstdio> 2 3 #include<cstdlib> 4 5 #include<cmath> 6 7 #include<cstring> 8 9 #include<algorithm>10 11 #include<iostream>12 13 #include<vector>14 15 #include<map>16 17 #include<set>18 19 #include<queue>20 21 #include<string>22 23 #define inf 100000000024 25 #define maxn 100000+1026 27 #define maxm 500+10028 29 #define eps 1e-1030 31 #define ll long long32 33 #define pa pair<int,int>34 35 #define for0(i,n) for(int i=0;i<=(n);i++)36 37 #define for1(i,n) for(int i=1;i<=(n);i++)38 39 #define for2(i,x,y) for(int i=(x);i<=(y);i++)40 41 #define for3(i,x,y) for(int i=(x);i>=(y);i--)42 43 #define mod 100000000744 45 using namespace std;46 47 inline int read()48 49 {50 51 int x=0,f=1;char ch=getchar();52 53 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}54 55 while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}56 57 return x*f;58 59 }60 ll f[maxn],g[maxn],s[maxn];61 int n,k,q[maxn];62 63 int main()64 65 {66 67 freopen("input.txt","r",stdin);68 69 freopen("output.txt","w",stdout);70 71 n=read();k=read();72 for1(i,n)s[i]=s[i-1]+read();73 int l=1,r=1;74 for1(i,n)75 {76 while(l<r&&i-q[l]>k)l++;77 f[i]=g[q[l]]-s[q[l]]+s[i];78 g[i]=max(f[i-1],g[i-1]);79 while(l<=r&&g[q[r]]-s[q[r]]<=g[i]-s[i])r--;80 q[++r]=i;81 }82 printf("%lld\n",max(f[n],g[n]));83 84 return 0;85 86 }View code
Bzoj2442: [usaco2011 open] lawn trimming