标签:
Commando
You are 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 will consist of a contiguous sequence of
soldiers of the form (i, i+1, . . . , i+k).
Each soldier i has 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 battle effectiveness of the soldiers in the unit. In other words, x = xi + xi+1 + · · · + xi+k .
However, years of glorious victories
have led you to conclude that the battle effectiveness of a unit should
be adjusted as follows: the adjusted effectiveness x is computed by
using the equation x = ax2 + bx + c, where a,b, c are known coefficients(a < 0), x is the original effectiveness of the unit.
Your task as commander is to divide your soldiers into commando units in order to maximize the sum of the adjusted effectiveness 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 be a = −1, b = 10, c = −20. In this case,
the best solution is to divide the soldiers into three commando units:
The first unit contains soldiers 1 and 2, the second unit contains
soldier 3, and the third unit contains soldier 4. The battle
effectiveness of the three units are 4, 3, 4 respectively, and the
adjusted
effectiveness are 4, 1, 4 respectively. The total adjusted
effectiveness for this grouping is 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, 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 soldiers 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
这道题又是一如既往的推公式,推出来后又水过了。
原来APIO的题目也不是那么难嘛!
1 //rp++ 2 //#include <bits/stdc++.h> 3 4 #include <iostream> 5 #include <cstring> 6 #include <cstdio> 7 using namespace std; 8 const int maxn=1000010; 9 long long f[maxn],s[maxn],a,b,c; 10 int q[maxn],st,ed; 11 long long Get_this(int j,int k) 12 { 13 return f[j]-f[k]+(a*(s[j]+s[k])-b)*(s[j]-s[k]); 14 } 15 int main() 16 { 17 //freopen(".in","r",stdin); 18 //freopen(".out","w",stdout); 19 int T;s[0]=0; 20 scanf("%d",&T); 21 while(T--) 22 { 23 int n; 24 scanf("%d",&n); 25 scanf("%lld%lld%lld",&a,&b,&c); 26 for(int i=1;i<=n;i++) 27 scanf("%lld",&s[i]); 28 29 for(int i=2;i<=n;i++) 30 s[i]+=s[i-1]; 31 32 st=ed=1; 33 q[st]=0; 34 for(int i=1;i<=n;i++){ 35 while(st<ed&&Get_this(q[st+1],q[st])>=2*a*s[i]*(s[q[st+1]]-s[q[st]])) 36 st++; 37 38 f[i]=f[q[st]]+a*(s[i]-s[q[st]])*(s[i]-s[q[st]])+b*(s[i]-s[q[st]])+c; 39 40 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]])) 41 ed--; 42 43 q[++ed]=i; 44 } 45 printf("%lld\n",f[n]); 46 } 47 return 0; 48 }
标签:
原文地址:http://www.cnblogs.com/TenderRun/p/5266698.html