bzoj 3156: 防御准备(斜率DP)

来源:互联网 发布:带着淘宝去古代txt 编辑:程序博客网 时间:2024/06/17 18:28

3156: 防御准备

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


和http://blog.csdn.net/jaihk662/article/details/78434514一模一样

就是相当于b[]全为1

#include<stdio.h>#include<algorithm>using namespace std;#define LL long longLL a[1000005], b[1000005], dp[1000005], st[1000005];double Jud(int j, int k){return 1.0*(dp[j]-dp[k])/(k-j);}int main(void){LL i, n, ans, bet, l, r;scanf("%lld", &n);for(i=1;i<=n;i++)scanf("%lld", &a[i]);ans = a[n];for(i=1;i<=n;i++)ans += n-i;l = 1, r = 1;st[1] = n;bet = 0;for(i=n-1;i>=1;i--){while(r>l && Jud(st[l], st[l+1])>i)l++;dp[i] = dp[st[l]]+i*(st[l]-i)-a[i];bet = max(bet, dp[i]);while(r>l && Jud(st[r], i)>Jud(st[r-1], st[r]))r--;st[++r] = i;}printf("%lld\n", ans-bet);return 0;}


原创粉丝点击