"Slope optimization" Special Ops Team _ optimization

Source: Internet
Author: User

Special Operations Team
"Problem description"
You have an army of N reservists, soldiers numbered from 1 to N, to split them up.
into a number of special operations teams into the battlefield. For tacit consideration, the number of the members of the same special ops team
Should be continuous, that is, the sequence of the form (I,i + 1, ..., i + k).
The initial battle capacity of the soldier numbered I is XI, and the initial battle capacity of a special ops team X for the team
The sum of the soldiers ' initial fighting capacity, namely x= XI + xi+1 + ... + xi+k.
After a long period of observation, you have summed up the initial battle effectiveness of a special Ops team X will be as follows experience public
Type is modified to X ': X ' = ax
2
+BX + C, where a,b, C is the known coefficient (a< 0).
As commander of the Force, now you have to make a formation for this unit, so that all the Special Ops teams are amended
The greatest of battle effectiveness. Try to find out the biggest and.
For example, you have 4 soldiers, x1= 2, x2 = 2, x3 = 3, x4 = 4. The parameters in the empirical formula are a=–1,
b=, C =–20. At this point, the best plan is to make the soldiers into 3 Special operations teams: The first team contains soldiers
1 and Soldier 2, second team contains soldiers 3, third team contains soldier 4. The initial combat effectiveness of the Special ops team
Do not be 4, 3, 4, after the revised fighting capacity of 4, 1, 4. Corrected the combat effectiveness and for 9, no other
The program can make the modified combat effectiveness and greater.
"Input Format"
The input is made up of three rows. The first line contains an integer n, representing the total number of soldiers. The second line contains three

Integer a,b, C, the coefficients of each of the empirical formulas. The third row contains n a space-delimited integer x1,

X2,..., xn, respectively, for 1,2, ..., N soldiers ' initial combat effectiveness.

"Output Format"
Outputs an integer representing the maximum number of combat effectiveness for all special Ops teams.
"Sample Input"
4
-110-20
2 2 3 4
"Sample Output"
9
"Data Range"
20% of the data, n≤1000;
50% of the data, n≤10,000;
In 100% of the data, 1≤n≤1,000,000,–5≤a≤–1,|b|≤10,000,000,|c|≤
10,000,000,1≤x≤100.



Exercises

F[i] is used to represent the maximum fighting power that can be obtained from 1 to I in several groups.

F[i]=max{f[j]+g (S[i]-s[j])}. (0<=j<i)

Wherein S[i] is the sum of the combat effectiveness of 1 to I, G (x) =a*x^2+b*x+c, Direct motion gauge O (n^2), and is expected to be 40~50 divided.

This 1d/1d equation can be optimized with a slope.

A decision J,k is provided. Where j is the optimal decision of I, K is any decision except J.

Then f[j]+a* (S[i]-s[j]) ^2+b* (S[i]-s[j]) +c>=f[k]+a* (s[i]-s[k)) ^2+b* (s[i]-s[k)) +c

=>f[j]-f[k]+a*sj*sj-a*sk*sk-b*sj+b*sk>=2a*si* (Sj-sk)

When J<k, (F[j]-f[k]+a*sj*sj-a*sk*sk-b*sj+b*sk)/(SJ-SK) <=2a*si

Order h[j,k]= (F[j]-f[k]+a*sj*sj-a*sk*sk-b*sj+b*sk)/(S[j]-s[k]).

According to the comparison between this function and 2a*s[i], we can know the pros and cons of two decisions.

Then maintain a decision value sequence D, where D1<D2<D3<......<DK. Meet H[D1,D2]>=H[D2,D3]>=......>=H[DK-1,DK].

Each time the team head to eliminate H[dl,dl+1]>2*a*si decision, until H[dl,dl+1] is just less than equal to 2a*si, at this time the DL is the optimal decision.

As long as you do this kind of questions on it.

Division is extremely inefficient and takes dozens of times times more time to multiply, and it is recommended that you change all division to multiplication.

Code according to the program (No, too much food)

#include <iostream>
#include <cstdio>

using namespace std;

const  int maxn=1000000+5;
Long long F[MAXN],S[MAXN];
int Q[MAXN];
int n;
Long long a,b,c;

Long Long calc (int j,int k)
{return
     f[j]-f[k]+a* (S[j]*s[j]-s[k]*s[k])-b* (S[j]-s[k]);

int main ()
{
    freopen ("commando.in", "R", stdin);
    Freopen ("Commando.out", "w", stdout);
    scanf ("%d\n", &n);
    scanf ("%i64d%i64d%i64d", &a,&b,&c);
    for (int i=1;i<=n;++i)
    {
        scanf ("%d", &s[i]);
        S[I]+=S[I-1];
    }
    Long long X;
    int y,l=0,r=0;
    for (int i=1;i<=n;++i)
    {while
        (L<r&&calc (q[l],q[l+1)) <=2*a*s[i]* (s[q[l]]-s[q[l+1])) L ++;
        Y=Q[L];
        X=s[i]-s[y];
        F[i]=f[y]+a*x*x+b*x+c;
        while (L<r&&calc (Q[r-1],q[r]) * (S[q[r]]-s[i]) <= (s[q[r-1]]-s[q[r)] *calc (q[r],i)) r--;
        q[++r]=i;
    }
    printf ("%i64d", F[n]);
    return 0;
}

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.