http://www.lydsy.com/JudgeOnline/problem.php?id=1010

题意:

思路:

容易得到朴素的递归方程:$dp(i)=min(dp(i),dp(k)+(i-k-1+sum[i]-sum[k]-l)^{2})$,$sum[i]$表示前i个玩具的$c_{i}$之和。$f(k)$表示前k个玩具的最小费用。

如果设$f(i)=sum[i]+i$,那么上式就可以改写为$dp(i)=min(dp(i),dp(k)+(f(i)-f(k)-l-1)^{2})$。

所以这道题目是很明显的斜率优化dp。

如果k决策比j决策更优的话,那么(c=l+1)

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,ll> pll;
const int INF = 0x3f3f3f3f;
const int maxn=+; ll n, l;
ll c[maxn];
ll dp[maxn];
ll sum[maxn];
ll Q[maxn]; ll dy(ll k, ll j)
{
return dp[k]+(sum[k]+l)*(sum[k]+l)-dp[j]-(sum[j]+l)*(sum[j]+l);
} ll dx(ll k, ll j)
{
return *(sum[k]-sum[j]);
} int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%lld%lld",&n,&l))
{
l+=;
sum[]=;
for(int i=;i<=n;i++)
{
scanf("%I64d",&c[i]);
sum[i]=sum[i-]+c[i];
}
for(int i=;i<=n;i++) sum[i]+=i;
Q[]=;
int frt=,rear=;
for(int i=;i<=n;i++)
{
while(frt<rear && dy(Q[frt+],Q[frt])<=sum[i]*dx(Q[frt+],Q[frt])) frt++;
int tmp=Q[frt];
dp[i]=dp[tmp]+(sum[i]-sum[tmp]-l)*(sum[i]-sum[tmp]-l);
while(frt<rear && dy(Q[rear],Q[rear-])*dx(i,Q[rear])>=dy(i,Q[rear])*dx(Q[rear],Q[rear-])) rear--;
Q[++rear]=i;
}
printf("%lld\n",dp[n]);
}
return ;
}

BZOJ 1010: [HNOI2008]玩具装箱toy(斜率优化dp)的更多相关文章

  1. BZOJ 1010: [HNOI2008]玩具装箱toy 斜率优化DP

    1010: [HNOI2008]玩具装箱toy Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再 ...

  2. Bzoj 1010: [HNOI2008]玩具装箱toy(斜率优化)

    1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec Memory Limit: 162 MB Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定 ...

  3. bzoj1010[HNOI2008]玩具装箱toy 斜率优化dp

    1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 11893  Solved: 5061[Submit][S ...

  4. 【bzoj1010】[HNOI2008]玩具装箱toy 斜率优化dp

    题目描述 P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为1...N的N件玩具, ...

  5. [luogu3195 HNOI2008] 玩具装箱TOY (斜率优化dp)

    题目描述 P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为1...N的N件玩具, ...

  6. P3195 [HNOI2008]玩具装箱TOY 斜率优化dp

    传送门:https://www.luogu.org/problem/P3195 题目描述 P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任 ...

  7. 洛谷P3195 [HNOI2008]玩具装箱TOY——斜率优化DP

    题目:https://www.luogu.org/problemnew/show/P3195 第一次用斜率优化...其实还是有点云里雾里的: 网上的题解都很详细,我的理解就是通过把式子变形,假定一个最 ...

  8. BZOJ 1010: [HNOI2008]玩具装箱toy [DP 斜率优化]

    1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 9812  Solved: 3978[Submit][St ...

  9. bzoj 1010 [HNOI2008]玩具装箱toy(DP的斜率优化)

    1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 7874  Solved: 3047[Submit][St ...

  10. BZOJ 1010 [HNOI2008]玩具装箱toy

    1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 7184  Solved: 2724[Submit][St ...

随机推荐

  1. caffe学习记录

    结论: caffe网络的prototxt训练与测试的时候用的是不同的,训练的时候用的prototxt里面有test只是为了测试网络的训练程度,里面的测试集是验证集,并不是真正我们测试的时候用的网络定义 ...

  2. Spring boot Security 用于权限管理,用户添加等。

    1:添加依赖: <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thy ...

  3. HDU1010:Tempter of the Bone(dfs+剪枝)

    http://acm.hdu.edu.cn/showproblem.php?pid=1010   //题目链接 http://ycool.com/post/ymsvd2s//一个很好理解剪枝思想的博客 ...

  4. Oracle多关键字模糊查询

    以前写SQL时,知道MySQL多字段模糊查询可以使用[charlist] 通配符,如: SELECT * FROM Persons WHERE City LIKE '[ALN]%'但是在Oracle中 ...

  5. [LeetCode] 532. K-diff Pairs in an Array_Easy tag: Hash Table

    Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in t ...

  6. selenium webdriver窗口切换(上)

    selenium webdriver窗口切换,有时候在做自动化的时候需要打开很多很多的页面, 当在操作不同的页面的时候需要切换窗口,下面是如何切换到前后页面窗口的操作: package test201 ...

  7. Trove系列(七)——Trove的Mysql的复制功能介绍

    描述提供各种复制功能的支持对于Trove来说是很关键的.本章节将描述各种使用案例和相关的用户需求.并依次提出了MySQL的初始阶段的实现.Mysql的复制功能介绍概述先介绍一下MySQL的复制功能原理 ...

  8. Object-C-Foundation-NSDate

    NSDate 表达日期表达时间的方法 NSDate *now=[NSDate date]; 获得当前日期 NSDate *tomrrow=[now dateByAddingTimeInterval:2 ...

  9. 527D Clique Problem 判断一维线段没有两辆相交的最大线段数量

    这题说的是给了n个位置 在x轴上 每个位置有一个权值为wi,然后将|xi - xj|>=wi+wj ,满足这个条件的点建一条边,计算着整张图中有多少多少个点构成的子图,使得这个子图的节点数尽量的 ...

  10. ORTP编译为静态库的问题

    项目中需要用到ORTP,我采用的编译环境是 VC2013,当我在项目设置中将设置为静态库是,发现没有导出函数,比如在需要连接 oRTP.lib库时提示 找不到 ORTP_init; 解决办法是 :在O ...