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

dp(i)表示处理完[i,n]且i是放守卫塔的最小费用.

dp(i) = min{dp(j) + (j-i)(j-i-1)/2}+costi(i<j≤N)

然后斜率优化

-----------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>
 
using namespace std;
 
typedef long long ll;
#define f(x) (dp[x] * 2 + ll(x) * x)
 
const int maxn = 1000009;
 
ll dp[maxn];
int cost[maxn], N;
int q[maxn], qh = 0, qt = -1;
 
double K(int x, int y) {
return (double) (f(x) - f(y)) / (x - y);
}
 
int main() {
scanf("%d", &N);
for(int i = 1; i <= N; i++)
scanf("%d", cost + i);
cost[0] = 0;
dp[N] = cost[N];
q[++qt] = N;
for(int i = N; i--; ) {
while(qt - qh > 0 && K(q[qh], q[qh + 1]) > i * 2 + 1) qh++;
dp[i] = dp[q[qh]] + 1LL * (q[qh] - i) * (q[qh] - i - 1) / 2 + cost[i];
while(qt - qh > 0 && K(q[qt], i) > K(q[qt - 1], q[qt])) qt--;
q[++qt] = i;
}
printf("%lld\n", dp[0]);
return 0;
}

-----------------------------------------------------------

3156: 防御准备

Time Limit: 10 Sec   Memory Limit: 512 MB
Submit: 788   Solved: 375
[ 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

 

转载于:https://www.cnblogs.com/JSZX11556/p/4985658.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值