日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

發(fā)布時間:2025/3/20 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 BZOJ 3156: 防御准备( dp + 斜率优化 ) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

dp(i)表示處理完[i,n]且i是放守衛(wèi)塔的最小費用.

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

然后斜率優(yōu)化

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

#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

第一行為一個整數(shù)N表示戰(zhàn)線的總長度。

第二行N個整數(shù),第i個整數(shù)表示在位置i放置守衛(wèi)塔的花費Ai

Output

共一個整數(shù),表示最小的戰(zhàn)線花費值。

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

Katharon+#1

?

轉載于:https://www.cnblogs.com/JSZX11556/p/4985658.html

總結

以上是生活随笔為你收集整理的BZOJ 3156: 防御准备( dp + 斜率优化 )的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。