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; }