BZOJ 1010 [HNOI2008]玩具装箱toy (斜率优化DP)
优化 DP BZOJ 装箱 玩具 斜率
2023-09-11 14:17:18 时间
1010: [HNOI2008]玩具装箱toy
Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 11699 Solved: 4956
[Submit][Status][Discuss]
Description
P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京。他使用自己的压缩器进行压
缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中。P教授有编号为1...N的N件玩具,第i件玩具经过
压缩后变成一维长度为Ci.为了方便整理,P教授要求在一个一维容器中的玩具编号是连续的。同时如果一个一维容
器中有多个玩具,那么两件玩具之间要加入一个单位长度的填充物,形式地说如果将第i件玩具到第j个玩具放到一
个容器中,那么容器的长度将为 x=j-i+Sigma(Ck) i<=K<=j 制作容器的费用与容器的长度有关,根据教授研究,
如果容器长度为x,其制作费用为(X-L)^2.其中L是一个常量。P教授不关心容器的数目,他可以制作出任意长度的容
器,甚至超过L。但他希望费用最小.
Input
第一行输入两个整数N,L.接下来N行输入Ci.1<=N<=50000,1<=L,Ci<=10^7
Output
输出最小费用
Sample Input
5 4
3
4
2
1
4
3
4
2
1
4
Sample Output
1
HINT
析:很容易看出来是DP,而且方程也很好写,dp[i] = min{ dp[j] + (sum[i] - sum[j] + i - j - 1 - L)^2 },然而复杂度是O(n^2),肯定不行,但可以用斜率进行优化,先设f[i] = sum[i] + i,m = L + 1 ,那么这个方程都简单多了dp[j] + (f(i)-f(j)-m)^2,然后使用斜率优化就好。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <sstream> #include <list> #include <assert.h> #include <bitset> #include <numeric> #define debug() puts("++++") #define gcd(a, b) __gcd(a, b) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define fi first #define se second #define pb push_back #define sqr(x) ((x)*(x)) #define ms(a,b) memset(a, b, sizeof a) #define sz size() #define pu push_up #define pd push_down #define cl clear() #define all 1,n,1 #define FOR(i,x,n) for(int i = (x); i < (n); ++i) #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const LL LNF = 1e17; const double inf = 1e20; const double PI = acos(-1.0); const double eps = 1e-3; const int maxn = 5e4 + 10; const int maxm = 3e5 + 10; const int mod = 100003; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, -1, 0, 1}; const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline bool is_in(int r, int c) { return r >= 0 && r < n && c >= 0 && c < m; } int a[maxn]; LL sum[maxn], dp[maxn]; int q[maxn]; LL f(int i){ return sum[i] + i; } LL UP(int i, int j){ return dp[i]-dp[j] + sqr(f(i)) - sqr(f(j)); } LL DOWN(int i, int j){ return 2 * (f(i) - f(j)); } LL DP(int i, int j){ return dp[j] + sqr(f(i)-f(j)-m); } int main(){ scanf("%d %d", &n, &m); ++m; for(int i = 1; i <= n; ++i){ int x; scanf("%d", &x); sum[i] = sum[i-1] + x; } dp[0] = 0; int fro = 0, rear = 0; q[++rear] = 0; for(int i = 1; i <= n; ++i){ while(fro + 1 < rear && UP(q[fro+2], q[fro+1]) <= DOWN(q[fro+2], q[fro+1]) * (f(i) - m)) ++fro; dp[i] = DP(i, q[fro+1]); while(fro + 1 < rear && UP(i, q[rear]) * DOWN(i, q[rear-1]) < UP(i, q[rear-1]) * DOWN(i, q[rear])) --rear; q[++rear] = i; } printf("%lld\n", dp[n]); return 0; }
相关文章
- SQL Server-聚焦存储过程性能优化、数据压缩和页压缩提高IO性能(一)
- 关于Unity渲染优化,你可能遇到这些问题
- 优化if...else...语句
- 【状态设计优化DP】Atcoder Beginner Contest E - Work or Rest
- Atcoder 前缀和优化DP Candies
- 学渣乱搞系列之dp斜率优化
- 【46】SSD硬盘(上):如何完成性能优化的KPI?
- 《流量的秘密: Google Analytics网站分析与优化技巧(第3版)》一2.5 总结
- 《Oracle性能优化与诊断案例精选》——2.3 衣带渐宽终不悔
- iOS应用的启动流程和优化详解
- UITableView优化那点事
- sql优化实战:从1353秒到135秒(删除索引+修改数据+重建索引)
- JDBC优化策略总结
- 18.linux基础优化
- MySQL 性能优化的最佳20多条经验分享(一)(转)
- mysql最佳优化经验
- 168、【动态规划】leetcode ——121. 买卖股票的最佳时机:dp数组+变量优化 (C++版本)
- android 性能优化 -- 启动过程 冷启动 热启动
- hdu 5411 CRB and Puzzle (矩阵高速幂优化dp)
- MySQL性能优化的21个最佳实践 和 mysql使用索引
- 【bzoj1097】[POI2007]旅游景点atr 状压dp+堆优化Dijkstra
- 【bzoj2806】[Ctsc2012]Cheat 广义后缀自动机+二分+单调队列优化dp
- 【bzoj4518】[Sdoi2016]征途 斜率优化dp
- 【bzoj1096】[ZJOI2007]仓库建设 斜率优化dp
- 【bzoj3156】防御准备 斜率优化dp