动态规划(斜率优化):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\)的总和最大. 分析 算是一道斜率优化的入门题. 首先肯定是考 ...
随机推荐
- MVVM之View和ViewModel的关联
概要: 将所有的VM在加载到Application的Static Resource中,然后在View中用标签指定. 实现: 1)采用特性指定要添加到StaticResource中的对象 public ...
- codevs 3119 高精度练习之大整数开根 (各种高精+压位)
/* codevs 3119 高精度练习之大整数开根 (各种高精+压位) 二分答案 然后高精判重 打了一个多小时..... 最后还超时了...压位就好了 测试点#1.in 结果:AC 内存使用量: 2 ...
- apk文件解析,学习笔记
Android 应用程序包文件 (APK) 是一种Android操作系统上的应用程序安装文件格式,其英文全称为 “application package file” . 如果懂得使用反编译工具,可以下 ...
- 关于百度 UEditor的使用
1.文件路径的配置: 注意:在页面上需要指定editor文件所在的路径,否则报错 后面有时间,再说说 kindEditor和 bootstrap3的summernote的 Editor, fck ...
- .Net 4.0 Convert Object to XDocument
将Object转换为XDocment对象 代码如下: C# – Object to XDocument using System; using System.Collections.Generic; ...
- eclipse build很慢的时候,有可能是js文件编译验证慢的问题
第一步: 去除eclipse的JS验证: 将windows->preference->Java Script->Validator->Errors/Warnings-> ...
- xml中报错,验证是否是xml报错
1.xml中写入sql有时报错,例如有大于号小于号,要用<![CDATA[ ]]>扩起来 2.验证xml有错的方式,以浏览器方式打开,如果正常打开,无错. ...
- UITableViewCell 左滑删除
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return Y ...
- HttpClient的get+post请求使用
啥都不说,先上代码 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReade ...
- SGU 224.Little Queens
时间限制:0.75s 空间限制:6M 题意 n*n(n<=10)的棋盘,求出放置m(m<=n*n)个皇后的方案数. Solution: 状态压缩+位运算 搜索. 首先我们从上往下逐行放置 ...