BZOJ3675: [Apio2014] Sequence Segmentation

Source: Internet
Author: User

3675: [Apio2014] sequence split time limit:40 Sec Memory limit:128 MB
submit:218 solved:82
[Submit] [Status] Description

Little H has recently been fascinated by a split-sequence game. In this game, small H takes a long
A non-negative integer sequence of degrees N is divided into k+l non-empty subsequence sequences. In order to get the K+l subsequence,
The small H will repeat the following steps seven times:
1. Small h first selects a sequence with a length of more than 1 (at the beginning of the small h only a length of n
Sequence one by one is the whole sequence obtained at the beginning);
2. Select a location and divide the sequence into contiguous two non-empty new
Sequence.
After each of these steps, small h will get a certain score. This is a score of two new sequences
The product of the elements in the column. Small h wants to choose an optimal partitioning scheme, so that after K-wheel (TH),
The total score for small h is the largest.

Input

The first line of the input file contains two integers n and Guineas (K+1≤n).
The second line contains n non-negative integers a1,n2 .... , an (0≤ai≤10^4), which represents the beginning of a small h
to the sequence.

Output


A row contains an integer that is the maximum score a small h can get.

Sample Input7 3
4 1 3 4 0 2 3
Sample Output108
HINT



"Sample description"

In the example, small H can get 108 points with the following 3-wheel operation:

1. -Start small H has a sequence (4,1,3,4,0,2,3). Small H selects the position after the 1th number

Divide the sequence into two parts and get the 4x (1+3+4+0+2+3) =52 points.

2. At the beginning of this round, small H has two sequences: (4), (1,3,4,0,2,3). Small h selection in 3rd number

The position after the word divides the second sequence into two parts and gets (1+3) x (4+0+2+

3) =36 points.

3. At the beginning of this round, small H has three sequences: (4), (1,3), (4,0,2,3). Small h selection in 5th

The position after the number divides the third sequence into two parts and gets (4+0) x (2+3) =

20 points.

After the three-wheeled operation, the small H will get four sub-sequences: (4), (1,3), (4,0), (2,3) and a total of 52+36+20=108 points.

"Data size and scoring"

: Data meets 2≤n≤100000,1≤k≤min (N-1,200).

Source

Exercises

This is a clever conversion: The final score is only related to the last number of sequences, and the order of the points is irrelevant. (Thought for a long time to come out t_t)

Then we can DP, F[i]=min (f[j]+ (S[i]-s[j]) * (N-s[i]+s[j]) slightly deformed, this can be optimized with slope, so the problem is solved.

Concrete deformation can be http://trinklee.blog.163.com/blog/static/238158060201462310236608/

Because the write slope does not have the special enigmatic grading mother ==0 WA 2 times ...

Code:

1#include <cstdio>2  3#include <cstdlib>4  5#include <cmath>6  7#include <cstring>8  9#include <algorithm>Ten   One#include <iostream> A   -#include <vector> -   the#include <map> -   -#include <Set> -   +#include <queue> -   +#include <string> A   at #defineINF 1000000000 -   - #defineMAXN 100000+1000 -   - #defineMAXM 201 -   in #defineEPS 1e-10 -   to #definell Long Long +   - #definePA pair<int,int> the   * #defineFor0 (i,n) for (int i=0;i<= (n); i++) $  Panax Notoginseng #defineFor1 (i,n) for (int i=1;i<= (n); i++) -   the #defineFor2 (i,x,y) for (int i= (x); i<= (y); i++) +   A #defineFor3 (i,x,y) for (int i= (x); i>= (y); i--) the   + #defineMoD 1000000007 -   $ using namespacestd; $   -InlineintRead () -   the { -  Wuyi     intx=0, f=1;CharCh=GetChar (); the   -      while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} Wu   -      while(ch>='0'&&ch<='9') {x=Ten*x+ch-'0'; ch=GetChar ();} About   $     returnx*F; -   - } - intn,m,t=0, Q[MAXN]; All f[2][MAXN],S[MAXN]; +InlineDoubleKintIintj) the { -     if(s[i]-s[j]<eps)returninf; $     Else return(Double) (f[1-t][i]-s[i]*s[i]-f[1-T][J]+S[J]*S[J])/(Double) (s[i]-s[j]); the } the   the intMain () the   - { inFreopen ("Input.txt","R", stdin); theFreopen ("output.txt","W", stdout); theN=read (); m=read (); Abouts[0]=0; theFor1 (i,n) s[i]=s[i-1]+read (); theFor1 (i,n) f[0][i]=s[i]* (s[n]-s[i]); the For1 (j,m) +     { -t=1-T; the         intL=1, r=0;Bayi For1 (i,n) the         { the              while(L<r&&k (q[l+1],Q[L]) >s[n]-2*s[i]) l++; -f[t][i]=f[1-t][q[l]]+ (S[i]-s[q[l]) * (s[n]-s[i]+S[q[l]]); -             if(i<j+1) f[t][i]=0; the             if(i==j+1) f[t][i]=f[1-t][i-1]+ (s[i]-s[i-1]) * (s[n]-s[i]+s[i-1]); the              while(L<r&&k (I,q[r]) >k (q[r],q[r-1])) r--; theq[++r]=i; the         } -     } theprintf"%lld\n",f[t][n]>>1); the   the     return 0;94   the}
View Code

Why are thieves slow? Should it be a question of real numbers?

BZOJ3675: [Apio2014] Sequence Segmentation

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.