Dynamic planning (Slope optimization): Spoj Commando

Source: Internet
Author: User

Commando


You is the commander of a troop of N soldiers, numbered from 1 to N. For the battle ahead, you plan to divide these n soldiers into several Com-mando units. To promote unity and boost morale, each unit would consist of a contiguous sequence of soldiers of the form (I, i+1, ... , I+k).

Each soldier I have a battle effectiveness rating XI. Originally, the battle effectiveness X of a commando unit (I, i+1, ..., i+k) was computed by adding up the individual B Attle effectiveness of the soldiers in the unit. In other words, X = xi + xi+1 + + xi+k.


However, years of glorious victories has led you to conclude so the battle effectiveness of a unit should be adjusted a s follows:the adjusted effectiveness x is computed by using the equation x = ax2 + bx + C, where a, B, C is known Coeffic Ients (A < 0), X is the original effectiveness of the unit.

Your task as Commander is-divide Your soldiers into commando units in order to maximize the sum of the adjusted EFFECTI Veness of all the units.

For instance, suppose you have 4 soldiers, x1 = 2, x2 = 2, x3 = 3, x4 = 4. Further, let the coefficients for the equation to adjust the battle effectiveness of a unit is a =−1, B = ten, C =−20. The the best solution are to divide the soldiers into three commando Units:the first unit contains soldiers 1 an D 2, the second unit contains soldier 3, and the third unit contains soldier 4. The battle effectiveness of the three units is 4, 3, 4 respectively, and the
Adjusted effectiveness is 4, 1, 4 respectively. The total adjusted effectiveness for this grouping are 9 and it can be checked that no better solution is possible.

Input format:

First line of input consists number of cases T.

Each case consists of three lines. The first line contains a positive integer n and the total number of soldiers. The second line contains 3 integers a, B, and C, the coefficients for the equation to adjust the battle effectiveness of a Commando Unit. The last line contains n integers x1, x2, ..., xn, sepa-rated by spaces, representing the battle effectiveness of Ldiers 1, 2, ..., n, respectively.

Constraints:

T<=3

N≤1, 000, 000,

−5≤a≤−1

|b| ≤10, 000, 000

|c| ≤10, 000, 000

1≤xi≤100.


Output format:

Output each answer in a single line.

Input:

3
4
-1 10-20
2 2 3 4
5
-1 10-20
1 2 3) 4 5
8
-2 4 3
100 12 3 4 5 2 4 2

Output:

9
13
-19884

This problem is the same as the formula, pushed out after the water.

The original Apio problem is not so difficult!

1 //rp++2 //#include <bits/stdc++.h>3 4#include <iostream>5#include <cstring>6#include <cstdio>7 using namespacestd;8 Const intmaxn=1000010;9 Long Longf[maxn],s[maxn],a,b,c;Ten intq[maxn],st,ed; One Long LongGet_this (intJintk) A { -     returnf[j]-f[k]+ (* (S[j]+s[k])-B) * (s[j]-s[k]); - } the intMain () - { -     //freopen (". In", "R", stdin); -     //freopen (". Out", "w", stdout); +     intt;s[0]=0; -scanf"%d",&T); +      while(t--) A     { at         intN; -scanf"%d",&n); -scanf"%lld%lld%lld",&a,&b,&c); -          for(intI=1; i<=n;i++) -scanf"%lld",&s[i]); -          in          for(intI=2; i<=n;i++) -s[i]+=s[i-1]; to          +St=ed=1; -q[st]=0; the          for(intI=1; i<=n;i++){ *              while(St<ed&&get_this (q[st+1],Q[ST]) >=2*a*s[i]* (s[q[st+1]]-s[q[st])) $st++;Panax Notoginseng              -F[i]=f[q[st]]+a* (S[i]-s[q[st]]) * (S[i]-s[q[st]]) +b* (S[i]-s[q[st]]) +C; the              +              while(St<ed&&get_this (i,q[ed]) * (s[q[ed]]-s[q[ed-1]]) >=get_this (q[ed],q[ed-1]) * (s[i]-s[q[ed])) Aed--; the              +q[++ed]=i; -         } $printf"%lld\n", F[n]); $     } -     return 0; -}

Dynamic planning (Slope optimization): Spoj Commando

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.