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 has 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) in a row of cows numbered 1... n. The efficiency of each cow is different. The efficiency of the 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 for a party :). Therefore, FJ now needs your help to calculate the maximum efficiency that FJ can get, and this solution does not continuously exceed K cows. 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.
Question:
Define f [I] as the maximum efficiency of the former I-1 for cows.
Sum [I] is the prefix and value of EI.
F [I] = max {f [J] + sum [I-1]-sum [J]} (I-j <K)
You can use a monotonic queue to maintain f [J]-sum [J].
Code:
#include<cstdio>using namespace std;long long sum[100005];long long f[100005];long long n,k;struct N{ int x;long long w; N(int a=0,long long b=0){ x=a,w=b; }};struct dddl{ int h,t; N q[100005]; void insert(N a){ while(h!=t&&q[t-1].w<=a.w) t--; q[t++]=a; } void pop(int x){ if(q[h].x==x) h++; } long long ask(){ return q[h].w; }}dddl;int main(){ scanf("%d%d",&n,&k); for(int i=1;i<=n;i++){ scanf("%lld",&sum[i]); } k++; for(int i=1;i<=n;i++) sum[i]+=sum[i-1]; dddl.insert(N(0,0)); for(int i=1;i<=n+1;i++){ dddl.pop(i-k-1); f[i]=dddl.ask()+sum[i-1]; dddl.insert(N(i,f[i]-sum[i])); } printf("%lld\n",f[n+1]); return 0;}
Bzoj 2442: [usaco open] lawn trimming