Simple DP "p1934" seal

Source: Internet
Author: User

Description

Long ago, the demon World drought, wells all dried up, the temperature is also getting higher. In order to save the residents, the King Long hope to break the magic of the well, into the human realm "steal" juicy beads, to repair the earth water vein. But there are seals between the six realms, and the seal of the Well of the gods is controlled by Shushan and sealed. Long as a royal in the Demon Realm, Xi has the technique of passing through, and can walk to any place where there are gaps. But the seal doesn't leave any space! Long helpless under can only forcibly break the seal. Breaking the seal inevitably consumes a certain amount of vitality. In order to find juicy beads, Long must reduce physical exertion. He can use the cross-line while breaking the seal.

The seal of the Demon's well has a total of n layers, and each layer has a solid value. As a demon, the Long of a single layer of seal must be consumed by the sum of the solid value of the seal and the sum of the squares of the total layer N of the seal. However, when he used the cross-section, breaking the total energy consumption of the layer I to J Layer Seal (i<j) was the sum of the solid values of the J Layer seal and the first I, the J layer between all seal layers (package The sum of the sum of the solid values of the first and the J layers. Also, in order not to disturb the Shushan, the sum of the solid values of the J-layer seal must not be greater than a fixed value of T (the solid value of the layer can be larger than the value when broken separately).

Input

The first line contains two positive integers n and T, sequentially representing the number of seal layers and fixed values described in the title.

The second acts n positive integers \ (a_1,\dots a_n\), which represent the solid values of the 1th to nth-layer seals sequentially.

Output

A single row that contains a positive integer representing the minimum energy consumption.

Obviously, this is a dynamic plan.

Set \ (f[i]\) represents the minimum energy consumption of the front \ (i\) layer.

Consider that we can get through the previous layer directly, you can also choose a previous layer to get through.

So it's easy to think of a state transition equation
\[f[i]=min (F[i-1]+a[i] \times n ^2, f[j-1]+ (A[j]+a[i]) \times (sum[i]-sum[j-1])) \]
Note the \ (j\) enumeration has a range of \ (1\leq j <i\), and where \ (sum[i]\) maintains the prefix and from \ (1\ ) to \ (i\) .

代码

#include<cstdio>#include<cstdlib>#include<cctype>#include<iostream>#define int long long#define R registerusing namespace std;inline void in(int &x){    int f=1;x=0;char s=getchar();    while(!isdigit(s)){if(s=='-')f=-1;s=getchar();}    while(isdigit(s)){x=x*10+s-'0';s=getchar();}    x*=f;}int n,t,a[1008];int sum[1008],f[1008];signed main(){    in(n),in(t);    for(R int i=1;i<=n;i++)        in(a[i]),sum[i]=sum[i-1]+a[i],f[i]=214748364;    f[1]=a[1]*n*n;    for(R int i=2;i<=n;i++)    {        f[i]=(f[i-1]+a[i]*n*n);        for(R int k=1;k<i;k++)        {            if(a[i]+a[k]<=t)            f[i]=min(f[i],f[k-1]+(a[i]+a[k])*(sum[i]-sum[k-1]));        }    }    printf("%lld",f[n]);}

Simple DP "p1934" seal

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.