Bzoj 3675 [Apio2014] Sequence segmentation (slope dp)

Source: Internet
Author: User

"Topic link"

http://www.lydsy.com/JudgeOnline/problem.php?id=3675

Test instructions

The sequence of n number is divided k times, the benefit of each time is divided into two parts of the value and the product, to seek maximum benefit.

Ideas

F[I][J] indicates that the maximum benefit of the first I-Partition J is transferred:

f[i][j]=max{f[k][j-1]+ (S (i)-S (k)) *s (k)}

A<b, if B is better than a decision-making:

(S[b]^2-s[a]^2+f[a][j-1]-f[b][j-1])/(S[b]-s[a]) <s[i]

The monotonic queue maintains the upper convex hull.

Code

1#include <Set>2#include <cmath>3#include <queue>4#include <vector>5#include <cstdio>6#include <cstring>7#include <iostream>8#include <algorithm>9 #defineTrav (u,i) for (int i=front[u];i;i=e[i].nxt)Ten #definefor (A,B,C) for (int a= (b); a<= (c); a++) One using namespacestd; A  -typedefLong Longll; - Const intN = 2e5+Ten; the  - ll Read () { -     CharC=GetChar (); -ll f=1, x=0; +      while(!IsDigit (c)) { -         if(c=='-') f=-1; C=GetChar (); +     } A      while(IsDigit (c)) atx=x*Ten+c-'0', c=GetChar (); -     returnx*F; - } -  - intN,k,tot; -ll f[n][2],cur,q[n],qh,qt,a[n],s[n]; in /* - Double Slop (ll A,ll b) { to return (double) (s[b]*s[b]-s[a]*s[a]+f[a][cur^1]-f[b][cur^1])/(double) (S[b]-s[a]); + } - */ the ll up (ll A,ll b) * { $     return(s[b]*s[b]-s[a]*s[a]+f[a][cur^1]-f[b][cur^1]);Panax Notoginseng } - ll Down (ll A,ll b) the { +     return(s[b]-S[a]); A } the  + intMain () - { $N=read (), k=read (); $for (I,1, N) { -a[++tot]=read (); -         if(!a[tot])--tot; the     } -n=tot;Wuyifor (I,1, N) s[i]=s[i-1]+A[i]; theFor (round,1, K) { -cur^=1; WuQh=1; qt=0; - For (i,round,n) { About              while(Qh<qt&&up (q[qt-1],Q[QT]) *down (q[qt-1],i-1) >up (q[qt-1],i-1) *down (q[qt-1],Q[QT]) qt--; $q[++qt]=i-1; -              while(Qh<qt&up (q[qh],q[qh+1]) <s[i]*down (q[qh],q[qh+1])) qh++; -             intt=Q[QH]; -f[i][cur]=f[t][cur^1]+ (S[i]-s[t]) *S[t]; A         } +     } theprintf"%lld\n", F[n][cur]); -     return 0; $}

Bzoj 3675 [Apio2014] Sequence segmentation (slope 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.