动态规划(斜率优化):SPOJ Commando
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的更多相关文章
- 【学习笔记】动态规划—斜率优化DP(超详细)
[学习笔记]动态规划-斜率优化DP(超详细) [前言] 第一次写这么长的文章. 写完后感觉对斜优的理解又加深了一些. 斜优通常与决策单调性同时出现.可以说决策单调性是斜率优化的前提. 斜率优化 \(D ...
- [bzoj1911][Apio2010特别行动队] (动态规划+斜率优化)
Description Input Output Sample Input - - Sample Output HINT Solution 斜率优化动态规划 首先易得出这样的一个朴素状态转移方程 f[ ...
- [bzoj1597][usaco2008 mar]土地购买 (动态规划+斜率优化)
Description 农夫John准备扩大他的农场,他正在考虑N (1 <= N <= 50,000) 块长方形的土地. 每块土地的长宽满足(1 <= 宽 <= 1,000, ...
- [luogu3648][bzoj3675][APIO2014]序列分割【动态规划+斜率优化】
题目大意 让你把一个数列分成k+1个部分,使分成乘积分成各个段乘积和最大. 分析 首先肯定是无法开下n \(\times\) n的数组,那么来一个小技巧:因为我们知道k的状态肯定是从k-1的状态转移过 ...
- 动态规划(斜率优化):BZOJ 3675 [Apio2014]序列分割
Description 小H最近迷上了一个分割序列的游戏.在这个游戏里,小H需要将一个长度为N的非负整数序列分割成k+l个非空的子序列.为了得到k+l个子序列, 小H将重复进行七次以下的步骤: 1.小 ...
- 动态规划(斜率优化):BZOJ 1010 【HNOI2008】 玩具装箱
玩具装箱toy Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 8218 Solved: 3233[Submit] Description P 教授要去 ...
- BZOJ 1096: [ZJOI2007]仓库建设(动态规划+斜率优化)
第一次写斜率优化,发现其实也没啥难的,没打过就随便找了一份代码借(chao)鉴(xi)下,不要介意= = 题解实在是懒得写了,贴代码吧= = CODE: #include<cstdio># ...
- UOJ#104. 【APIO2014】Split the sequence 动态规划 斜率优化
原文链接www.cnblogs.com/zhouzhendong/p/UOJ104.html 题解 首先证明一个结论:对于一种分割方案,分割的顺序不影响最终结果. 证明:对于树 a[x] 和 a[y] ...
- [luogu4072][bzoj4518][SDOI2016]征途【动态规划+斜率优化】
题目分析 Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成n段,相邻两段路的分界点设有休息站. Pine计划用m天到达T地.除第m天外,每一天晚上Pine都必须在休息站过夜.所以,一段路 ...
- [luogu3628][bzoj1911][APIO2010]特别行动队【动态规划+斜率优化DP】
题目描述 给你一个数列,让你将这个数列分成若干段,使其每一段的和的\(a \times sum^2 + b \times sum + c\)的总和最大. 分析 算是一道斜率优化的入门题. 首先肯定是考 ...
随机推荐
- python----------进程、线程、协程
进程与线程 什么是进程(process)? An executing instance of a program is called a process. Each process provides ...
- <legend>标签
健康信息身高: 体重: 如果表单周围没有边框,说明您的浏览器太老了. <!DOCTYPE HTML> <html> <body> <form> < ...
- (转)兼容主流浏览器的CSS透明代码
透明往往能产生不错的网页视觉效果下面是兼容主流浏览器的CSS透明代码.transparent_class { filter:alpha(opacity=50); -moz-opacity:0.5; - ...
- C++ 简单的入门语法
入门的hello world using namespace std; 是使用命名空间,有点像java里面的引入包main 方法和java一样是主入口,有且只有一个,因为是int ,所以还必须返回一个 ...
- 为Angular-UEditor增加工具栏属性
感谢胡大大分享的的开源项目 Angular 的 UEditor 插件 Angular-UEditor 本文只是修改了angular-ueditor.js,加入了对工具栏的定制,方便项目使用 1 (fu ...
- javascript的选项卡
主要用的索引值 首先 写三个按钮 <input type="button" > <input type="button" > <i ...
- SGU 160.Magic Multiplying Machine
时间限制:0.5s 空间限制6M 题意: 给出n个(1<=n<=10000)1~m(2<m<1000)范围内的数,选择其中任意个数,使它们的 乘积 模m 最大,输 ...
- SGU 276 Andrew's Troubles
简单的题.直接找题意来就好了. #include <iostream> #include <cstdio> using namespace std; int s, n, ans ...
- APP被Rejected 的各种原因翻译(转)
原文:http://www.cnblogs.com/sell/archive/2013/02/16/2913341.html Terms and conditions(法律与条款) 1.1 As a ...
- 一个基于nodejs,支持http/https的中间人(MITM)代理,便于渗透测试和开发调试。
源码地址:https://github.com/wuchangming/node-mitmproxy node-mitmproxy node-mitmproxy是一个基于nodejs,支持http/h ...