题意:买瓜,每天的瓜有不同的价格和xu命时间,要求能苟到第n天的最小代价

定义DP方程\(dp[i]\),指苟到第\(i\)天的最小代价,所求即为\(dp[n]\)

那么怎么转移就是问题,这里的状态表示显然不能转移,因为哪一天买的瓜苟到哪一刻都不知道,空间太大不足以维护

再定义一个st表\(st[i]\),表示【能够】苟到第\(i\)天的最小代价,那么转移就是dp[i]=min(dp[i-1]+瓜,st[i...n]),后者表示不买瓜直接xu命

st表肯定是一个每一位都单调非增序列,应该可以用单调队列优化(然而不会),所以还是放到线段树上更新了..

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<bitset>
#include<set>
#include<map>
#define rep(i,j,k) for(register int i=j;i<=k;i++)
#define rrep(i,j,k) for(register int i=j;i>=k;i--)
#define erep(i,u) for(register int i=head[u];~i;i=nxt[i])
#define iin(a) scanf("%d",&a)
#define lin(a) scanf("%lld",&a)
#define din(a) scanf("%lf",&a)
#define s0(a) scanf("%s",a)
#define s1(a) scanf("%s",a+1)
#define print(a) printf("%lld",(ll)a)
#define enter putchar('\n')
#define blank putchar(' ')
#define println(a) printf("%lld\n",(ll)a)
#define IOS ios::sync_with_stdio(0)
using namespace std;
const int maxn = 1e5+11;
const int MOD = 2520;
const double eps = 1e-10;
typedef long long ll;
const ll oo = 1ll<<60;
ll read(){
ll x=0,f=1;register char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
ll dp[maxn],a[maxn],b[maxn],n;
struct ST{
ll mn[maxn<<2];
#define lc o<<1
#define rc o<<1|1
void build(int o,int l,int r){
mn[o]=oo;
if(l==r)return;
int m=l+r>>1;
build(lc,l,m);
build(rc,m+1,r);
}
void pu(int o){
mn[o]=min(mn[lc],mn[rc]);
}
void update(int o,int l,int r,int k,ll v){
if(l==r){
mn[o]=min(v,mn[o]);
return;
}
int m=l+r>>1;
if(k<=m) update(lc,l,m,k,v);
else update(rc,m+1,r,k,v);
pu(o);
}
ll query(int o,int l,int r,int L,int R){
if(L<=l&&r<=R) return mn[o];
int m=l+r>>1;
ll ans=oo;
if(L<=m) ans=min(ans,query(lc,l,m,L,R));
if(R>m) ans=min(ans,query(rc,m+1,r,L,R));
return ans;
}
}st;
int main(){
while(cin>>n){
rep(i,1,n) a[i]=read();
rep(i,1,n) b[i]=min(read(),n);
st.build(1,1,n);
dp[0]=0;
dp[1]=a[1];st.update(1,1,n,b[1],dp[1]);
rep(i,2,n){
dp[i]=dp[i-1]+a[i];
st.update(1,1,n,min(i+b[i]-1,n),dp[i]);//mai gua
ll t=st.query(1,1,n,i,n);//xu ming
if(t<dp[i]) dp[i]=t;
}
println(min(dp[n],st.query(1,1,n,n,n)));
}
return 0;
}

ZOJ - 3632 DP 单调优化的更多相关文章

  1. codeforces 1101F Trucks and Cities 区间dp+单调优化 好题

    题目传送门 题意简述:(来自洛谷) 有n个城市坐落在一条数轴上,第ii个城市位于位置ai​. 城市之间有m辆卡车穿行.每辆卡车有四个参数:si​为起点编号,fi​为终点编号,ci​表示每行驶1个单位长 ...

  2. ZOJ 3632 ----dp+优先队列

    上个礼拜学长讲了优先队列的说.... emmmmmm.... 看着题解敲了一题...先m下. #include<cstring> #include<algorithm> #in ...

  3. ZOJ 3632 K - Watermelon Full of Water 优先队列优化DP

    K - Watermelon Full of Water Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%lld &am ...

  4. [poj3017] Cut the Sequence (DP + 单调队列优化 + 平衡树优化)

    DP + 单调队列优化 + 平衡树 好题 Description Given an integer sequence { an } of length N, you are to cut the se ...

  5. 【Codeforces 321E / BZOJ 5311】【DP凸优化】【单调队列】贞鱼

    目录 题意: 输入格式 输出格式 思路: DP凸优化的部分 单调队列转移的部分 坑点 代码 题意: 有n条超级大佬贞鱼站成一行,现在你需要使用恰好k辆车把它们全都运走.要求每辆车上的贞鱼在序列中都是连 ...

  6. Newnode's NOI(P?)模拟赛 第二题 dp决策单调优化

    其实直接暴力O(n3)DP+O2O(n^3)DP+O_2O(n3)DP+O2​优化能过- CODE O(n3)O(n^3)O(n3) 先来个O(n3)O(n^3)O(n3)暴力DP(开了O2O_2O2 ...

  7. 3622 假期(DP+单调队列优化)

    3622 假期 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 黄金 Gold 题目描述 Description 经过几个月辛勤的工作,FJ决定让奶牛放假.假期可以在1-N天内任意选择 ...

  8. 【BZOJ-1010】玩具装箱toy DP + 斜率优化

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

  9. DP+单调队列 codevs 1748 瑰丽华尔兹(还不是很懂具体的代码实现)

    codevs 1748 瑰丽华尔兹 2005年NOI全国竞赛  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master 题解       题目描述 Descripti ...

随机推荐

  1. Mips下交叉编译dropbear

    1. 编译zlib-1.2.8 在编译dropbear的时候,会遇到“configure: error: *** zlib missing - install first or check confi ...

  2. Luogu 4254 [JSOI2008]Blue Mary开公司

    BZOJ 1568 学习了一波李超线段树. 大佬blog 这个东西专门用来维护插入一条线段和区间/单点的最大/最小值. 插入的时候讨论: 1.如果当前结点上没有线段,那么直接插入. 2.如果当前结点上 ...

  3. Flask框架 之 上下文管理前戏

    偏函数 自动传递参数 import functools def index(a1,a2): return a1 + a2 # 原来的调用方式 # ret = index(1,23) # print(r ...

  4. Oracle数据库之单表查询

    接着上一篇的分享,今天主要给大家分享的是关于数据中的单表查询,单表查询很基础,也很重要,但是任何一个初学者必须要掌握的姿势,单表查询就是对单个表进行操作,查询我们想要的数据.单表查询里面的内容也是比较 ...

  5. HTML <area> 标签区域map标签

    1.距形:(左上角顶点坐标为(x1,y1),右下角顶点坐标为(x2,y2)) <area shape="rect" coords="x1,y1,x2,y2" ...

  6. Task async await

    暇之余,究多Task.async.await. using System; using System.Collections.Generic; using System.Linq; using Sys ...

  7. go语言 defer 你不知道的秘密!

    go 语言的defer功能强大,对于资源管理非常方便,但是如果没用好,也会有陷阱哦.我们先来看几个例子. 例一: defer 是先进后出 这个很自然,后面的语句会依赖前面的资源,因此如果先前面的资源先 ...

  8. Insus.NET最近想更换一部手机

    Insus.NET曾经使用过好几部手机.给Insus.NET工作与生活上带来了方便.最近想更换一部新手机,因此记念一下以前使用过的手机.当时Insus.NET没有相机,下面图片是网上找的(前四部): ...

  9. linux系统上安装mysql5.6(详细步骤)

    为了学习mycat 尝试在虚拟机上装mysql(看了别人的博客比划着安装),但装了两次都没成功.因此总结了如下步骤 有需要的朋友可以试下(linux需要联网) mysql-5.6.26.tar.gz百 ...

  10. 201621123012 《java程序设计》第3周学习总结

    1. 本章学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 答:关键词:继承 多态 抽象类 abstract 覆盖 object siper 1.2 尝试使用思维导图将这些关键词组织起来. ...