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 ...
随机推荐
- ubuntu 64 14.04 共享文件夹问题
转自http://blog.csdn.net/gongyuan073/article/details/46604233 原文链接: http://blog.csdn.NET/chinley/artic ...
- laravel实现多对多的分析
在实际开发中多对多的开发还是比较常见的 1.1首先由migrate来创建表(文章表) 1.2同理创建标签表 1.3这是 我会的到如下结果: 2.1在数据迁移表contents中添加几个字段 publi ...
- glib hash库GHashTable的使用实例
前言 hash表是一种key-value访问的数据结构,hash表存储的数据能够很快捷和方便的去查询.在很多工程项目都需要使用到hash表来存储数据.对于hash表的详细说明这里就不进行阐述了,不了解 ...
- eclipse——Maven创建JavaWeb工程
打包方式改为war 问题:webapp目录下缺少web.xml文件 先勾选掉Dynamic Web Services 点击Applay 再勾选上Dynamic Web Services ,目的是为了产 ...
- C# 把本地文件上传到服务器上,和从服务器上下载文件
方法一.通过Ajax方式上传文件(input file),使用FormData进行Ajax请求 <div > <input type="file" name=& ...
- HttpUploader7-授权码配置
1.1. 七牛云存储 配置方式: 1.配置授权码 2.配置云存储 3.配置空间名称 4.配置上传地址 1.2. 阿里云存储 配置方式: 1.填写授权码 2.配置云存储为阿里云 ...
- POJ3295 Tautology(栈+枚举)
Description WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some ...
- JAVA的String 类【转】
String类 1.String对象的初始化 由于String对象特别常用,所以在对String对象进行初始化时,Java提供了一种简化的特殊语法,格式如下: String s = “abc”; s ...
- window中启动vs后鼠标无法移动
你停止wisptis.exe这个进程,在c:\Windows\System32下删除wispitis.exe就可以了!
- [转]Marshaling a SAFEARRAY of Managed Structures by P/Invoke Part 6.
1. Introduction. 1.1 Starting from part 4 I have started to discuss how to interop marshal a managed ...