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. oracle:变长数组varray,嵌套表,集合

    创建变长数组类型 ) );  这个变长数组最多可以容纳两个数据,数据的类型为 varchar2(50) 更改元素类型的大小或精度 可以更改变长数组类型和嵌套表类型 元素的大小. ALTER TYPE ...

  2. JAVA除去制定字符的方法

    只需调用replaceAll()方法: public class Test { public static void main(String[] args) { String s= "abc ...

  3. hibernate4.3.8整合struts2过程中遇到的问题

    1.遇到的异常: Exception in thread "main" org.hibernate.service.spi.ServiceException: Unable to ...

  4. shell脚本学习之if..else用法

    一 简介 1 字符串判断 str1 = str2 当两个串有相同内容.长度时为真  str1 != str2 当串str1和str2不等时为真  -n str1 当串的长度大于0时为真(串非空)  - ...

  5. 进程识别号(PID)的理解

    PID(Process Identification)操作系统里指进程识别号,也就是进程标识符.操作系统里每打开一个程序都会创建一个进程ID,即PID. PID(进程控制符)英文全称为Process ...

  6. iOS 获取通讯录里边的电话号码AddressBook

    1  首先导入库 <AddressBook/AddressBook.h> 2 然后在导入#import <AddressBook/AddressBook.h>文件 3 声明   ...

  7. iOS 数据持久性存储-对象归档

    对象归档是将对象归档以文件的形式保存到磁盘中(也称为序列化,持久化),使用的时候读取该文件的保存路径读取文件的内容(也称为解档,反序列化) 主要涉及两个类:NSKeyedArichiver.NSKey ...

  8. python - StringIO文本缓冲

    参考:http://pymotwcn.readthedocs.org/en/latest/documents/StringIO.html 类StringIO提供了一个在内存中方便处理文本的类文件(读, ...

  9. C# 跨线程访问控件

    this.BeginInvoke(new Action(() => { this.StatusProgressBar_ExecutingTaskStatus.Value = (int)value ...

  10. access_token的获取方式

      获取Access Token $appid = ""; $appsecret = ""; $url = "https://api.weixin.q ...