能够非常好的证明单调决策性质。用   记sum[i]=sigma(C[1],C[2].....C[k]);f[i]=sum[i]+i;  c=l-1;

有转移dp[i]=min( dp[j]+(f[i]-f[jk]-c)^2);  假死 有2个决策j<k, 对于i点。k决策更优秀 于是能够得到

dp[k]+(f[i]-f[k]-c)^2<dp[j]+(f[i]-f[j]-c)^2;

对于一个x>i  如果f[x]=f[i]+v;对于决策j,k。若决策k优于决策j  ,必定

dp[k]+(f[x]-f[k]-c)^2<dp[j]+(f[x]-f[j]-c)^2;

于是dp[k]+(f[i]+v-f[k]-c)^2<dp[j]+(f[i]-v-f[j]-c)^2;

仅仅要2v(f[i]-f[k]-c)+v^2<2v(f[i]-f[j]-c)

优于v>0   f[k]>f[j]  这是必定成立的  ,所以能够非常好的证明单调决策性质。然后能够依据《1D/1D动态规划初步》论文的写法做。

#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <string>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int inf = 0x3fffffff;
const int mmax = 500010;
LL C[mmax];
LL dp[mmax];
LL sum[mmax];
int n;
LL L;
struct node
{
int l,r;
int d;
node() {}
node(int l,int r,int d):l(l),r(r),d(d) {}
void print()
{
printf("%d %d %d\n",l,r,d);
}
}Q[mmax];
LL sqr(LL x)
{
return x*x;
}
void up(int i,int j)
{
dp[i]=dp[j]+sqr(sum[i]-sum[j]+i-j-1-L);
}
bool ok(int i,int j,int d)
{
return dp[d]+sqr(sum[i]-sum[d]+i-d-1-L)>=dp[j]+sqr(sum[i]-sum[j]+i-j-1-L);
}
int find(int l,int r,int j,int d)
{
int mid;
r++;
while(l<r)
{
mid=(l+r)>>1;
if(ok(mid,j,d))
r=mid;
else
l=mid+1;
}
return r;
}
void make()
{
int head=0,tail=0;
dp[0]=0;
Q[tail++]=node(0,n,0);
for(int i=1;i<=n;i++)
{
while(Q[head].r<i)
head++;
if(Q[head].l<i)
Q[head].l=i;
up(i,Q[head].d);
int tmp=0;
while(head<tail)
{
if(ok(Q[tail-1].l,i,Q[tail-1].d))
{
tmp=Q[tail-1].l;
tail--;
}
else
{
tmp=find(Q[tail-1].l,Q[tail-1].r,i,Q[tail-1].d);
Q[tail-1].r=tmp-1;
break;
}
}
if(tmp<=n)
Q[tail++]=node(tmp,n,i);
}
}
int main()
{ while(cin>>n>>L)
{
sum[0]=0;
for(int i=1;i<=n;i++)
{
scanf("%lld",&C[i]);
sum[i]=sum[i-1]+C[i];
}
make();
printf("%lld\n",dp[n]);
}
return 0;
}

bzoj 1010 (单调决策优化)的更多相关文章

  1. bzoj 3126 单调队列优化dp

    能转移的最左是其左边完整区间的最右左端点,最右是能覆盖它的最左左端点-1 #pragma GCC optimize ("O3") #include<cstdio> #i ...

  2. BZOJ 1010 [HNOI2008]玩具装箱 (斜率优化DP)

    题目链接 http://www.lydsy.com/JudgeOnline/problem.php?id=1010 思路 [斜率优化DP] 我们知道,有些DP方程可以转化成DP[i]=f[j]+x[i ...

  3. 【BZOJ 1010】 [HNOI2008]玩具装箱toy (斜率优化)

    1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 9330  Solved: 3739 Descriptio ...

  4. BZOJ 1499 [NOI2005] 瑰丽华尔兹 | 单调队列优化DP

    BZOJ 1499 瑰丽华尔兹 | 单调队列优化DP 题意 有一块\(n \times m\)的矩形地面,上面有一些障碍(用'#'表示),其余的是空地(用'.'表示).每时每刻,地面都会向某个方向倾斜 ...

  5. BZOJ 1855 股票交易 - 单调队列优化dp

    传送门 题目分析: \(f[i][j]\)表示第i天,手中拥有j份股票的最优利润. 如果不买也不卖,那么\[f[i][j] = f[i-1][j]\] 如果买入,那么\[f[i][j] = max\{ ...

  6. BZOJ 2806: [Ctsc2012]Cheat [广义后缀自动机 单调队列优化DP 二分]

    2806: [Ctsc2012]Cheat 题意: 多个主串和多个询问串,每次询问将询问串分成多个连续子串,如果一个子串长度>=L且在主串中出现过就是熟悉的 如果熟悉的字符串长度>=询问串 ...

  7. bzoj 2806 [Ctsc2012]Cheat——广义后缀自动机+单调队列优化DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2806 只想着怎么用后缀数据结构做,其实应该考虑结合其他算法. 可以二分那个长度 L .设当前 ...

  8. bzoj 1855 dp + 单调队列优化

    思路:很容易写出dp方程,很容易看出能用单调队列优化.. #include<bits/stdc++.h> #define LL long long #define fi first #de ...

  9. bzoj 1499 [NOI2005]瑰丽华尔兹——单调队列优化dp

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1499 简单的单调队列优化dp.(然而当时却WA得不行.今天总算填了坑) 注意滚动数组赋初值应 ...

随机推荐

  1. MarkDown写作之嵌入LaTeX和HTML

    本系列文章由 @YhL_Leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/49788741 Markdown 是一种 ...

  2. 洛谷 P3102 [USACO14FEB]秘密代码Secret Code

    P3102 [USACO14FEB]秘密代码Secret Code 题目描述 Farmer John has secret message that he wants to hide from his ...

  3. [Webpack + React] Import CSS Modules with TypeScript and webpack

    If you try to use CSS Modules in TypeScript the same way you would use them in JavaScript, with webp ...

  4. 广东省知名P2P平台资料

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYXNrYmFpNjY2ODg4/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  5. Majority Element:主元素

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  6. struts2入门(搭建环境、配置、示例)

    转自:https://blog.csdn.net/u012862311/article/details/53412716 1.下载Struts2的jar包 下载地址:http://archive.ap ...

  7. jsp输出当前时间

    在jsp页面中输出完整的时间,格式为"年 月 日  时:分:秒" <% Date date = new Date(); SimpleDateFormat t = new Si ...

  8. php生成无限栏目树

    栏目数组:$arr=Array( Array('cid' => 2,'cname' => '新闻','pid' => 0),    Array('cid' => 4,'cnam ...

  9. Python有了asyncio和aiohttp在爬虫这类型IO任务中多线程/多进程还有存在的必要吗?

    最近正在学习Python中的异步编程,看了一些博客后做了一些小测验:对比asyncio+aiohttp的爬虫和asyncio+aiohttp+concurrent.futures(线程池/进程池)在效 ...

  10. JavaScript总结(3)

    第3章 获取用户的输入 <script>10 intA=prompt("请输入第一个数字","");11 intB=prompt("请输入 ...