3156: 防御准备

Time Limit: 10 Sec  Memory Limit: 512 MB
Submit: 837  Solved: 395
[Submit][Status][Discuss]

Description

 

Input

第一行为一个整数N表示战线的总长度。

第二行N个整数,第i个整数表示在位置i放置守卫塔的花费Ai。

Output

共一个整数,表示最小的战线花费值。

Sample Input

10
2 3 1 5 4 5 6 3 1 2

Sample Output

18

HINT

1<=N<=10^6,1<=Ai<=10^9

Source

【思路】

斜率优化DP。

唯一需要注意的是过程中数据超范围,需要特别声明一下。

  为毛斜率DP都长一个样<_<

【代码1】

 #include<cstdio>
#include<iostream>
using namespace std; typedef long long LL;
const int N = 1e6+;
struct point { LL x,y;
}q[N],now;
int n,L,R,a[N]; LL cross(point a,point b,point c) {
return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}
void read(int& x) {
char c=getchar(); while(!isdigit(c)) c=getchar();
x=; while(isdigit(c)) x=x*+c-'' , c=getchar();
}
int main() {
//freopen("in.in","r",stdin);
//freopen("out.out","w",stdout);
read(n);
for(int i=;i<=n;i++) read(a[i]);
for(int i=;i<=n;i++) {
while(L<R && q[L].y-q[L].x*i >= q[L+].y-q[L+].x*i) L++;
now.x=i;
now.y=q[L].y-(LL)q[L].x*i+(LL)i*i+a[i];
while(L<R && cross(q[R-],q[R],now)<=) R--;
q[++R]=now;
}
printf("%lld",q[R].y-(LL)n*(n+)/); // (LL) n*(n+1)/2
return ;
}

Folding 1

【代码2】

 #include<cstdio>
#include<cstring>
#include<iostream>
using namespace std; typedef long long LL;
const int N = 1e6+; int n,L,R,a[N],q[N]; LL f[N]; double slop(int k,int j) {
return (double)(f[j]-f[k]+(LL)j*(j+)/-(LL)k*(k+)/)/(j-k);
}
void read(int& x) {
char c=getchar(); while(!isdigit(c)) c=getchar();
x=; while(isdigit(c)) x=x*+c-'' , c=getchar();
}
int main() {
//freopen("in.in","r",stdin);
//freopen("out.out","w",stdout);
read(n);
for(int i=;i<=n;i++) read(a[i]);
for(int i=;i<=n;i++) {
while(L<R && slop(q[L],q[L+])<i) L++;
int t=q[L];
f[i]=f[t]+(LL)i*(i-t)+(LL)t*(t+)/-(LL)i*(i+)/+a[i];
while(L<R && slop(q[R-],q[R])>slop(q[R],i)) R--;
q[++R]=i;
}
printf("%lld",f[n]);
return ;
}

Folding 2

bzoj 3156 防御准备(斜率DP)的更多相关文章

  1. BZOJ 3156: 防御准备 斜率优化DP

    3156: 防御准备 Description   Input 第一行为一个整数N表示战线的总长度. 第二行N个整数,第i个整数表示在位置i放置守卫塔的花费Ai. Output 共一个整数,表示最小的战 ...

  2. BZOJ 3156: 防御准备( dp + 斜率优化 )

    dp(i)表示处理完[i,n]且i是放守卫塔的最小费用. dp(i) = min{dp(j) + (j-i)(j-i-1)/2}+costi(i<j≤N) 然后斜率优化 ------------ ...

  3. bzoj 3156: 防御准备【斜率优化dp】

    就是套路咯,设s[i]为1+2+...i 首先列出dp方程\( f[i]=min(f[j]+a[i]+(i-j)*i-(s[i]-s[j])) \) 然后推一推 \[ f[i]=f[j]+a[i]+( ...

  4. BZOJ 3156 防御准备

    也是斜率优化....推下式子就好了. #include<iostream> #include<cstdio> #include<cstring> #include& ...

  5. bzoj3156防御准备 斜率优化dp

    3156: 防御准备 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 2279  Solved: 959[Submit][Status][Discuss ...

  6. bzoj 1597 斜率DP

    1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 5115  Solved: 1897[Submit] ...

  7. bzoj4518: [Sdoi2016]征途--斜率DP

    题目大意:把一个数列分成m段,计算每段的和sum,求所有的sum的方差,使其最小. 由方差*m可以化简得ans=m*sigma(ki^2)-sum[n]^2 很容易得出f[i][j]=min{f[i- ...

  8. hdu 3507 斜率dp

    不好理解,先多做几个再看 此题是很基础的斜率DP的入门题. 题意很清楚,就是输出序列a[n],每连续输出的费用是连续输出的数字和的平方加上常数M 让我们求这个费用的最小值. 设dp[i]表示输出前i个 ...

  9. 斜率dp cdq 分治

    f[i] = min { f[j] + sqr(a[i] - a[j]) } f[i]= min { -2 * a[i] * a[j] + a[j] * a[j] + f[j] } + a[i] * ...

随机推荐

  1. SQL几个有点偏的语句

    SQL语句是一种集合操作,就是批量操作,它的速度要比其他的语言快,所以在设计的时候很多的逻辑都会放在sql语句或者存储过程中来实现,这个是一种设计思想.但是今天我们来讨论另外一个话题.Sql页提供了丰 ...

  2. $(obj).data() 绑定和获取数据的应用

    1.解说 data() 方法向被选元素附加数据,或者从被选元素获取数据. 例如:$("#id").data("name","xiao"); ...

  3. Object-C Init

    上一篇为Object-C类实现 我们可以创建一个init方法用来给我们的实例变量设置初始化值: - (id)init { if(self = [super init]) { [self setCapt ...

  4. CSS Positioning(定位)

    Positioning(定位) CSS定位属性允许你为一个元素定位.它也可以将一个元素放在另一个元素后面,并指定一个元素的内容太大时,应该发生什么. 元素可以使用的顶部,底部,左侧和右侧属性定位.然而 ...

  5. [LeetCode OJ] Roman to Integer

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  6. 读书笔记之 - javascript 设计模式 - 适配器模式

    适配器模式可以用来在现在接口和不兼容的类之间进行适配. 使用这种模式的对象又叫包装器,因为他们是在用一个新接口包装另一个对象. 在设计类的时候往往遇到有些接口不能与现有api一同使用的情况,借助于适配 ...

  7. Oracle replace函数使用

    需求是要修改Oracle某列表中把这一列中全部的100换成200: update b_nodes a set a.childs=replace((select childs from b_nodes ...

  8. ubuntu 14.04安装amd omega 驱动

    驱动共分4部分: fglrx_14.501-0ubuntu1_amd64_UB_14.01.deb      52.0MB fglrx-core_14.501-0ubuntu1_amd64_UB_14 ...

  9. EF+WCF怎样更新数据?

    public virtual void Update(T entity) { T current = this.Where(m => m.Id.Equals(entity.Id)) .Singl ...

  10. 看es6 字符串新方法有感

    'x'.repeat(3) // "xxx" 'hello'.repeat(2) // "hellohello" 'na'.repeat(0) // " ...