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的题目也不是那么难嘛!

 //rp++
//#include <bits/stdc++.h> #include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=;
long long f[maxn],s[maxn],a,b,c;
int q[maxn],st,ed;
long long Get_this(int j,int k)
{
return f[j]-f[k]+(a*(s[j]+s[k])-b)*(s[j]-s[k]);
}
int main()
{
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
int T;s[]=;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
scanf("%lld%lld%lld",&a,&b,&c);
for(int i=;i<=n;i++)
scanf("%lld",&s[i]); for(int i=;i<=n;i++)
s[i]+=s[i-]; st=ed=;
q[st]=;
for(int i=;i<=n;i++){
while(st<ed&&Get_this(q[st+],q[st])>=*a*s[i]*(s[q[st+]]-s[q[st]]))
st++; f[i]=f[q[st]]+a*(s[i]-s[q[st]])*(s[i]-s[q[st]])+b*(s[i]-s[q[st]])+c; while(st<ed&&Get_this(i,q[ed])*(s[q[ed]]-s[q[ed-]])>=Get_this(q[ed],q[ed-])*(s[i]-s[q[ed]]))
ed--; q[++ed]=i;
}
printf("%lld\n",f[n]);
}
return ;
}

动态规划(斜率优化):SPOJ Commando的更多相关文章

  1. 【学习笔记】动态规划—斜率优化DP(超详细)

    [学习笔记]动态规划-斜率优化DP(超详细) [前言] 第一次写这么长的文章. 写完后感觉对斜优的理解又加深了一些. 斜优通常与决策单调性同时出现.可以说决策单调性是斜率优化的前提. 斜率优化 \(D ...

  2. [bzoj1911][Apio2010特别行动队] (动态规划+斜率优化)

    Description Input Output Sample Input - - Sample Output HINT Solution 斜率优化动态规划 首先易得出这样的一个朴素状态转移方程 f[ ...

  3. [bzoj1597][usaco2008 mar]土地购买 (动态规划+斜率优化)

    Description 农夫John准备扩大他的农场,他正在考虑N (1 <= N <= 50,000) 块长方形的土地. 每块土地的长宽满足(1 <= 宽 <= 1,000, ...

  4. [luogu3648][bzoj3675][APIO2014]序列分割【动态规划+斜率优化】

    题目大意 让你把一个数列分成k+1个部分,使分成乘积分成各个段乘积和最大. 分析 首先肯定是无法开下n \(\times\) n的数组,那么来一个小技巧:因为我们知道k的状态肯定是从k-1的状态转移过 ...

  5. 动态规划(斜率优化):BZOJ 3675 [Apio2014]序列分割

    Description 小H最近迷上了一个分割序列的游戏.在这个游戏里,小H需要将一个长度为N的非负整数序列分割成k+l个非空的子序列.为了得到k+l个子序列, 小H将重复进行七次以下的步骤: 1.小 ...

  6. 动态规划(斜率优化):BZOJ 1010 【HNOI2008】 玩具装箱

    玩具装箱toy Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 8218  Solved: 3233[Submit] Description P 教授要去 ...

  7. BZOJ 1096: [ZJOI2007]仓库建设(动态规划+斜率优化)

    第一次写斜率优化,发现其实也没啥难的,没打过就随便找了一份代码借(chao)鉴(xi)下,不要介意= = 题解实在是懒得写了,贴代码吧= = CODE: #include<cstdio># ...

  8. UOJ#104. 【APIO2014】Split the sequence 动态规划 斜率优化

    原文链接www.cnblogs.com/zhouzhendong/p/UOJ104.html 题解 首先证明一个结论:对于一种分割方案,分割的顺序不影响最终结果. 证明:对于树 a[x] 和 a[y] ...

  9. [luogu4072][bzoj4518][SDOI2016]征途【动态规划+斜率优化】

    题目分析 Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成n段,相邻两段路的分界点设有休息站. Pine计划用m天到达T地.除第m天外,每一天晚上Pine都必须在休息站过夜.所以,一段路 ...

  10. [luogu3628][bzoj1911][APIO2010]特别行动队【动态规划+斜率优化DP】

    题目描述 给你一个数列,让你将这个数列分成若干段,使其每一段的和的\(a \times sum^2 + b \times sum + c\)的总和最大. 分析 算是一道斜率优化的入门题. 首先肯定是考 ...

随机推荐

  1. jQuery 获取文件后缀的方法

    var location=$("input[name='file']").val(); var point = location.lastIndexOf("." ...

  2. 兼容IE浏览器的placeholder【超不错】

    jQuery EnPlaceholder plug (兼容IE浏览器的placeholder)使用 >>>>>>>>>>>>&g ...

  3. 10.30 afternoon

    P76竞赛时间: ????年??月??日??:??-??:?? 题目名称 他 她 它 名称 he she it 输入 he.in she.in it.in 输出 he.out she.out it.o ...

  4. IE8下ckeditor无法正常使用,提示"例外被抛出且未被接住"的解决办法

    <script language="javascript" src="ckeditor/ckeditor.js"></script> & ...

  5. maven+jetty项目在tomcat部署

    步骤1:项目打包 clean install 步骤二:拷贝war 包到tomcat下 步骤三:修改server.xml文件的端口 步骤四:启动tomcat,注意jetty的项目是不需要带项目名的,To ...

  6. SGU 195. New Year Bonus Grant

    时间限制:0.75s 空间限制:4M 题意: 在一颗树(最多500000个节点)中,可以对节点染色,但是一个节点染了色后,它的父节点和兄弟节点都不能再染了,求最大的染色节点数,并输出所有染色节点. S ...

  7. 子元素的margin-top影响父元素原因和解决办法

    这个问题会出现在所有浏览器当中,原因是css2.1盒子模型中规定, In this specification, the expression collapsing margins means tha ...

  8. java.math.BigDecimal类

    BigDecimal类用于高精度计算.一般的float型和Double型数据只可以用来做科学计算或者是工程计算,由于在商业计算中,要求的数字精度比较高,所以要用到java.math.BigDecima ...

  9. dedecms模版制作活动的折叠菜单

    需要做成这种样式 url请求为这样: http://m03.com/plus/list.php?tid=19 这些菜单项都有对应的tid项,页面刷新后,应该将所有的菜单折叠起来,对于tid=19的菜单 ...

  10. javascript定义类的方法总结

    1.构造函数法 类是对象的模板,定义了对象共有的方法属性数据 等,在javascript中一个函数就是一个对象,也可以看做一个类的构造方法. 所以我们可以像以下方式定义类: //1.经典的构造方法 Q ...