[HNOI2008]玩具装箱toy(dp+斜率优化)
斜率优化问题一般都是决策单调问题。对于这题能够证明单调决策。
令sum[i]=sigma(c [k] ) 1<=k<=i , f[i]=sum[i]+i , c=L+1;
首先我们能够写出转移方程 dp[i] = min( dp[j] + (f[i]-f[j]-c)^2 ) 。令决策j1<j2。若决策j2更优有
dp[j2]+(f[i]-f[j2]-c)^2<=dp[j1]+(f[i]-f[j1]-c)^2
能够得带 ((dp[j2]+f[j2]^2)-(dp[j1]+f[j1]^2) )/(f[j2]-f[j1])<2*(f[i]-c)。
优于f[i]是递增的,所以对于t>i的点。决策j2总是比j1更优。那么j1实际上能够从决策集合中删除。后面的就能够用一个队列维护了。
<span style="font-size:14px;">#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 =50010;
LL C[mmax];
LL L,c;
LL sum[mmax],f[mmax],dp[mmax];
LL sqr(LL x)
{
return x*x;
}
double G(int x)
{
return 1.0*f[x]*f[x]+dp[x];
}
double S(int x)
{
return 2.0*f[x];
}
void calc(int i,int j)
{
dp[i]=dp[j]+sqr(f[i]-f[j]-c);
}
int Q[mmax];
int main()
{
int n;
while(cin>>n>>L)
{
c=L+1;
sum[0]=0;
f[0]=0;
for(int i=1;i<=n;i++)
{
scanf("%lld",&C[i]);
sum[i]=sum[i-1]+C[i];
f[i]=sum[i]+i;
}
int head=0,tail=-1;
dp[0]=0;
Q[++tail]=0;
for(int i=1;i<=n;i++)
{
while(head<tail)
{
double tmp=1.0*(G(Q[head+1])-G(Q[head]))/(S(Q[head+1])-S(Q[head]));
if(tmp<=f[i]-c)
head++;
else
break;
}
calc(i,Q[head]);
while(head<tail)
{
double tmp1=1.0*(G(Q[tail])-G(Q[tail-1]))/(S(Q[tail])-S(Q[tail-1]));
double tmp2=1.0*(G(i)-G(Q[tail]))/(S(i)-S(Q[tail]));
if(tmp1>=tmp2)
tail--;
else
break;
}
Q[++tail]=i;
} printf("%lld\n",dp[n]); }
return 0;
}
</span>
[HNOI2008]玩具装箱toy(dp+斜率优化)的更多相关文章
- BZOJ 1010: [HNOI2008]玩具装箱toy [DP 斜率优化]
1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 9812 Solved: 3978[Submit][St ...
- [HNOI2008]玩具装箱TOY --- DP + 斜率优化 / 决策单调性
[HNOI2008]玩具装箱TOY 题目描述: P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京. 他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器 ...
- 1010: [HNOI2008]玩具装箱toy [dp][斜率优化]
Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为1... ...
- BZOJ1010: [HNOI2008]玩具装箱toy(dp+斜率优化)
Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 12451 Solved: 5407[Submit][Status][Discuss] Descript ...
- BZOJ.1010.[HNOI2008]玩具装箱toy(DP 斜率优化/单调队列 决策单调性)
题目链接 斜率优化 不说了 网上很多 这的比较详细->Click Here or Here //1700kb 60ms #include<cstdio> #include<cc ...
- P3195 [HNOI2008]玩具装箱TOY(斜率优化dp)
P3195 [HNOI2008]玩具装箱TOY 设前缀和为$s[i]$ 那么显然可以得出方程 $f[i]=f[j]+(s[i]-s[j]+i-j-L-1)^{2}$ 换下顺序 $f[i]=f[j]+( ...
- 【BZOJ-1010】玩具装箱toy DP + 斜率优化
1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 8432 Solved: 3338[Submit][St ...
- 【BZOJ 1010】 [HNOI2008]玩具装箱toy (斜率优化)
1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 9330 Solved: 3739 Descriptio ...
- 1010: [HNOI2008]玩具装箱toy(斜率优化)
1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 12280 Solved: 5277[Submit][S ...
- [HNOI2008]玩具装箱toy(斜率优化dp)
前言 这是我写的第一道$dp$斜率优化的题目,$dp$一直都很菜,而且咖啡鸡都说了这是基础的东西,然而看别人对$dp$斜率优化一大堆公式又看不懂就老老实实做几道题目,这个比较实在 描述 给出$n$和$ ...
随机推荐
- CentOS 7 下nagios搭建记录
跟随 园子的文章搭建 http://www.cnblogs.com/mchina/archive/2013/02/20/2883404.html 1.遇 nagios插件地址迁移错误,记录解决. 2. ...
- 【博弈论】【SG函数】【找规律】Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) E. Game of Stones
打表找规律即可. 1,1,2,2,2,3,3,3,3,4,4,4,4,4... 注意打表的时候,sg值不只与剩下的石子数有关,也和之前取走的方案有关. //#include<cstdio> ...
- cojs.tk(所有题目来源) 树状数组专练
1.求和问题 ★ 输入文件:sum.in 输出文件:sum.out 简单对比时间限制:1.2 s 内存限制:128 MB [问题描述] 在一个长度为n的整数数列中取出连续的若干 ...
- Linux下CURL设置请求超时时间
使用CURL时,有两个超时时间:一个是连接超时时间,另一个是数据传输的最大允许时间. 连接超时时间用--connect-timeout参数来指定,数据传输的最大允许时间用-m参数来指定. 例如: cu ...
- 安装CentOS 6停在selinux-policy-targeted卡住的问题解决
在刚开始安装时,设置swap分区.设置swap分区.设置swap分区 参考: http://tieba.baidu.com/p/3817971339 http://blog.csdn.net/zhan ...
- 看懂ios命名规则
http://liangrui.blog.51cto.com/1510945/509289/ http://daniellee520.blog.51cto.com/372529/229615
- 【mybatis】mybatis执行一个update方法,返回值为1,但是数据库中数据并未更新,粘贴sql语句直接在数据库执行,等待好久报错:Lock wait timeout exceeded; try restarting transaction
今天使用mybatis和jpa的过程中,发现这样一个问题: mybatis执行一个update方法,返回值为1,但是数据库中数据并未更新,粘贴sql语句直接在数据库执行,等待好久报错:Lock wai ...
- 【jQuery】jquery中 使用$('#parentUid').attr(parentUid);报错jquery-1.11.3.min.js:5 Uncaught TypeError: Cannot read property 'nodeType' of undefined
jquery中 使用$('#parentUid').attr(parentUid);报错jquery-1.11.3.min.js:5 Uncaught TypeError: Cannot read p ...
- 【Git】GitHub for Windows使用(1)
目录 1.下载GitHub for windows 客户端 2.注册GitHub 3.启动windows端客户端,登录帐号 4.了解Git客户端,从而了解Git的大体功能 5.创建一个新的资源库 6 ...
- 重设Windows 7密码 z
Restart the computer to boot using the CD. Once the GUI loads, press SHIFT+F10 to bring up the comma ...