"BZOJ2442" [Usaco2011 Open] Mowing lawn slope optimization dp

Source: Internet
Author: User
Tags cmath

First-time slope optimization.

There are basically two ways of thinking:

  1.f[i] indicates that the optimal condition (minimum loss and) of the I-f[i]=f[j]+e[i] is obviously n^2 T, but it can be found that the movement of F can be optimized to O (n) using the previous monotone queue optimization.

2.f[i] Represents the first choice, the j+1 not selected optimal conditions (maximum efficiency and) f[i]=f[j]+sum[i]-sum[j+1] (i-k-1<=j<=i-1), also can be monotonic queue optimization to O (n).

  PS: The first method needs to enumerate the case of the last K number, but the initial value of min 0x7fffffff (max_longint) is a point of WA ....

1#include <iostream>2#include <cstdio>3#include <cmath>4#include <cstring>5#include <cmath>6 #defineINF 999999999999999999LL7 #defineN 100000+10008 #definell Long Long9 using namespacestd;Ten structData One { A     intp; - ll v; - }q[n]; the intA[n]; - intN,k,l,r; - ll Minn,ans,f[n]; -InlineintRead () + { -     intf=1, ans=0; +     CharC; A      while(!isdigit (C=getchar ()))if(c=='-') f=-1; atans=c-'0'; -      while(IsDigit (C=getchar ())) ans=ans*Ten+c-'0'; -     returnans*F; - } - intMain () - { inN=read (); k=read (); -      for(intI=1; i<=n;i++) to     { +a[i]=read (); -ans+=A[i]; the     } *     //l=; r=0; $      for(intI=1; i<=n;i++)Panax Notoginseng     { -f[i]=q[l].v+A[i]; the          while(L<=r && q[r].v>f[i]) r--; +q[++r].v=F[i]; Aq[r].p=i; the          while(q[l].p<i-k) l++; +     } -minn=inf; $      for(inti=n-k;i<=n;i++) minn=min (minn,f[i]); $printf"%lld\n", ans-Minn);  -     return 0; -}
The first approach Description


After winning the best lawn game in town a year ago, FJ became very lazy and never trimmed the lawn. Right now
The new round of the best lawn competition began again, FJ hope to win again.

However, FJ's lawn is very messy, so FJ can only get his cows to do the work. 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).

Close to the cows are very familiar, so if FJ arranged more than k continuous cows, then these cows will strike
To party:). So now FJ needs your help to calculate the maximum efficiency that FJ can get, and in that scenario
No more than k cows in succession.

Input


* First line: Two integers separated by spaces N and K

* Second to N+1 line: line i+1 has an integer e_i


Output


* First line: A value that represents the maximum efficiency value that the FJ can get.

Sample Input5 2
1
2
3
4
5

Input explanation:

FJ has 5 cows, and their efficiency is 1,2,3,4,5. They want to pick the cows with the greatest efficiency, but
He can't pick more than 2 consecutive cows.

Sample Output
12

FJ can choose a cow other than the third one, with a total efficiency of 1+2+4+5=12.

HINT Source

Gold

"BZOJ2442" [Usaco2011 Open] Mowing lawn slope optimization dp

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.