ZOJ - 3632 DP 单调优化
题意:买瓜,每天的瓜有不同的价格和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 单调优化的更多相关文章
- codeforces 1101F Trucks and Cities 区间dp+单调优化 好题
题目传送门 题意简述:(来自洛谷) 有n个城市坐落在一条数轴上,第ii个城市位于位置ai. 城市之间有m辆卡车穿行.每辆卡车有四个参数:si为起点编号,fi为终点编号,ci表示每行驶1个单位长 ...
- ZOJ 3632 ----dp+优先队列
上个礼拜学长讲了优先队列的说.... emmmmmm.... 看着题解敲了一题...先m下. #include<cstring> #include<algorithm> #in ...
- ZOJ 3632 K - Watermelon Full of Water 优先队列优化DP
K - Watermelon Full of Water Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%lld &am ...
- [poj3017] Cut the Sequence (DP + 单调队列优化 + 平衡树优化)
DP + 单调队列优化 + 平衡树 好题 Description Given an integer sequence { an } of length N, you are to cut the se ...
- 【Codeforces 321E / BZOJ 5311】【DP凸优化】【单调队列】贞鱼
目录 题意: 输入格式 输出格式 思路: DP凸优化的部分 单调队列转移的部分 坑点 代码 题意: 有n条超级大佬贞鱼站成一行,现在你需要使用恰好k辆车把它们全都运走.要求每辆车上的贞鱼在序列中都是连 ...
- 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 ...
- 3622 假期(DP+单调队列优化)
3622 假期 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 黄金 Gold 题目描述 Description 经过几个月辛勤的工作,FJ决定让奶牛放假.假期可以在1-N天内任意选择 ...
- 【BZOJ-1010】玩具装箱toy DP + 斜率优化
1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 8432 Solved: 3338[Submit][St ...
- DP+单调队列 codevs 1748 瑰丽华尔兹(还不是很懂具体的代码实现)
codevs 1748 瑰丽华尔兹 2005年NOI全国竞赛 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题解 题目描述 Descripti ...
随机推荐
- <c:out>标签中有一个escapeXml属性 如果为escapeXml="false",则将其中的html、xml解析出来。
<td><c:out value="${s.name}" escapeXml="false"></c:out></td ...
- 【摘自大型网站技术架构书】负载均衡时session如何共享
由于负载均衡服务器可能会将请求分发到集群任何一台服务器上,所以保证每次请求能够获得正确的session比单机时复杂. 集群环境下,session管理的主要几种手段 1.session复制 sessio ...
- Spring Boot 启动失败,描述/Description: Cannot determine embedded database driver class for database type NONE
异常截图====> 快速解决方式==> 在SpringBoot的启动类上面添加注解:@EnableAutoConfiguration(exclude={DataSourceAutoConf ...
- c语言split的实现代码
我们知道在其他语言中有split函数可以把一个字符串按你自己想要的分隔符分割成多个字符串并以列表的形式返回.但是对于c语言来说,是没有这样一个函数接口可以直接调用的.但是有时候在项目工作中,又会用到这 ...
- 2.python IP/DNS地址处理之IPy/Dnspython模块
1.IPy模块 在IP地址规划中,涉及到计算大量的IP地址,包括网段.网络掩码.广播地址.子网数.IP类型等,即便是专业的网络人员也要进行繁琐的计算,而IPy模块提供了专门针对IPV4地址与IPV6 ...
- docker入门及安装
Docker简介 Docker是一个开源的引擎,可以轻松的为任何应用创建一个轻量级的.可移植的.自给自足的容器.开发者在笔记本上编译测试通过的容器可以批量地在生产环境中部署,包括VMs(虚拟机).ba ...
- C++ 中 dynamic_cast 浅析
简述:dynamic_cast 操作符,将基类的指针或引用安全的转换为派生类的指针或引用.主要讲解,dynamic_cast操作符的原理.使用方式.编译器设置.返回值等相关知识. dynamic_ca ...
- Linq学习<二>
http://www.cnblogs.com/wyqlijin/archive/2011/02/25/1964934.html 这位仁兄写的比较高深,建议大家看看 一: 这一篇以一个数据类为例,操作数 ...
- brk/sbrk和mmap行为分析程序
#include <stdio.h> #include <stdlib.h> #include <unistd.h> // #include <malloc. ...
- Linq 左连接 left join
Suppose you have a tblRoom and tblUserInfo. Now, you need to select all the rooms regardless of whet ...