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. Java_Activiti5_菜鸟也来学Activiti5工作流_之初识BPMN2.0的简单结构(五)

    <?xml version="1.0" encoding="UTF-8"?> <definitions xmlns="http:// ...

  2. 怎样使用svn开发项目

    那么首先什么是svn呢?官方有很好的解释,我说一下个人简单的理解,svn就是开源的版本控制软件, 那么什么是版本呢?简单的说版本就是标记,比如你买了一本书,同样的书名,但是版本不一定一样, 因为里面可 ...

  3. WPF TextElement内容模型简介(转)

    本内容模型概述描述了 TextElement 支持的内容. Paragraph 类是 TextElement 的类型. 内容模型描述哪些对象/元素可以包含在其他对象/元素中. 本概述汇总了派生自 Te ...

  4. Android--WebView控件

    WebView 一 简介: WebView一般用于将Android页面已HTML的形式展现,我们一般叫它HTML5开发: WebView可以使得网页轻松的内嵌到app里,还可以直接跟js相互调用,通过 ...

  5. redis 服务器端命令

    redis 127.0.0.1:6380> time ,显示服务器时间, 时间戳(秒), 微秒数 1) "1375270361" 2) "504511" ...

  6. Python开发【第一章】:Python简介和入门

    Python简介 Python的创始人为Guido van Rossum.1989年圣诞节期间,在阿姆斯特丹,Guido为了打发圣诞节的无趣,决心开发一个新的脚本解释程序,做为ABC 语言的一种继承. ...

  7. php开发入门教程

    LAMP window:WAMP(windows,apache,mysql,php) LAMP是 Linux,Apache,MySQL和PHP的缩写,是我们提供 Web 服务的软件基础. 对于 Lin ...

  8. java导入导出excel常用操作小结及简单示例

    POI中常用设置EXCEL的操作小结: 操作excel如下 HSSFWorkbook wb = new HSSFWorkbook();  //创建一个webbook,对应一个Excel文件 HSSFS ...

  9. 转:jQuery常用插件

    原文来自于:http://download.csdn.net/album/detail/369 jquery.cycle.all.js 上传者:itmyhome      上传时间:2014-06-1 ...

  10. Fastreport怎么样在同一页上下部分打印相同内容

    使用FastReport遇到个难题,不知道怎么解决 分组打印之后,需要同一页上下部分打印相同内容,就是一式两份的联单打印. 例如: 送货单 ********** A 这里上半页,地区分组之后的内容 * ...