ZOJ 3632 Watermelon Full of Water (线段树 区间更新 + dp)
题目大意:
让每天都能吃到西瓜。
最少须要花多少钱。
思路分析:
dp[pos] 就表示 要让 前i天每天都有西瓜吃。最少须要花多少钱。
那么假设你买这个西瓜的话。
那么这个西瓜能吃的持续时间都要更新一下。
然后再在每一个西瓜的更新部分取最小的,就能够是这个点所能得到的最小值。
事实上就是 dp[i] = min (dp[i] , dp[ j - k +1] + a[j]);
可是枚举前面的时候会超时,就用线段树维护。
5
1 2 3 4 5
1 2 2 2 2
给出这组数据是说,每次买西瓜的时候,都要和前一次的大小做比較,来表示西瓜在这里刚好吃完。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define maxn 55555
#define lson num<<1,s,mid
#define rson num<<1|1,mid+1,e
#define inf ((1LL)<<60)
typedef long long LL;
using namespace std; LL dp[maxn<<2]; void pushdown(int num)
{
if(dp[num]!=inf)
{
dp[num<<1]=min(dp[num<<1],dp[num]);
dp[num<<1|1]=min(dp[num<<1|1],dp[num]);
dp[num]=inf;
}
} void build(int num,int s,int e)
{
dp[num]=inf;
if(s==e)
{
return;
}
int mid=(s+e)>>1;
build(lson);
build(rson);
}
void update(int num,int s,int e,int l,int r,LL val)
{
if(l<=s && r>=e)
{
dp[num]=min(val,dp[num]);
return;
}
pushdown(num);
int mid=(s+e)>>1;
if(l<=mid)update(lson,l,r,val);
if(r>mid)update(rson,l,r,val);
} LL query(int num,int s,int e,int pos)
{
if(s==e)
{
return dp[num];
}
pushdown(num);
int mid=(s+e)>>1;
if(pos<=mid)return query(lson,pos);
else return query(rson,pos);
} int cost[maxn];
int last[maxn]; int main()
{
int n;
while(cin>>n)
{
for(int i=1;i<=n;i++)cin>>cost[i];
for(int i=1;i<=n;i++)cin>>last[i]; build(1,1,n); int pos=last[1];
if(pos>n)pos=n; update(1,1,n,1,pos,(LL)cost[1]); for(int i=2;i<=n;i++)
{
LL cur=min(query(1,1,n,i),query(1,1,n,i-1)); pos=i+last[i]-1;
if(pos>n)pos=n; update(1,1,n,i,pos,cur+(LL)cost[i]);
} cout<<query(1,1,n,n)<<endl;
}
return 0;
} /*
5
1 2 3 4 5
1 2 2 2 2
*/
ZOJ 3632 Watermelon Full of Water (线段树 区间更新 + dp)的更多相关文章
- ZOJ 1610 Count the Colors (线段树区间更新)
题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头 ...
- ZOJ - 1610 Count the Colors(线段树区间更新,单点查询)
1.给了每条线段的颜色,存在颜色覆盖,求表面上能够看到的颜色种类以及每种颜色的段数. 2.线段树区间更新,单点查询. 但是有点细节,比如: 输入: 2 0 1 1 2 3 1 输出: 1 2 这种情况 ...
- ZOJ 1610 Count the Colors (线段树区间更新与统计)
Painting some colored segments on a line, some previously painted segments may be covered by some th ...
- Zoj 1610 Count the Colors (线段树+区间更新+暴力计数)
题目大意: 有n次操作,每次都是对一根线中的一段区间进行染色(颜色并不相同),有时候后面的颜色有可能覆盖前面的颜色,问最后涂完色,能看到的颜色有几种,每种颜色有几部分? 解题思路: 这个题目建树的时候 ...
- ZOJ - 1610 Count the Colors(线段树区间更新)
https://cn.vjudge.net/problem/ZOJ-1610 题意 给一个n,代表n次操作,接下来每次操作表示把[l,r]区间的线段涂成k的颜色其中,l,r,k的范围都是0到8000. ...
- ZOJ 1610 Count the Color(线段树区间更新)
描述Painting some colored segments on a line, some previously painted segments may be covered by some ...
- HDU 1556 Color the ball(线段树区间更新)
Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...
- hihoCoder 1080 : 更为复杂的买卖房屋姿势 线段树区间更新
#1080 : 更为复杂的买卖房屋姿势 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho都是游戏迷,“模拟都市”是他们非常喜欢的一个游戏,在这个游戏里面他们 ...
- HDU 5023 A Corrupt Mayor's Performance Art(线段树区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023 解题报告:一面墙长度为n,有N个单元,每个单元编号从1到n,墙的初始的颜色是2,一共有30种颜色 ...
随机推荐
- manjaro利用docker使用MySQL
使用docker安装MySQL并使用 安装docker: sudo yaourt -S docker 使用docker安装mysql: systemctl start docker # 启动docke ...
- Java 编程下 Eclipse/myeclipse 如何设置单行代码显示的最大宽度
http://www.cnblogs.com/sunzn/archive/2013/03/30/2990191.html 或 http://zhidao.baidu.com/link?url=67uy ...
- nginx 集群
Nginx是什么? Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器.一直纳闷这个X是怎么来 ...
- Python3--中括号"[]"与冒号":"在列表中的作用
先来定义两个列表: liststr = ["helloworld","hahahh","123456"] listnum = [1,2,3, ...
- Crossed Ladders 求街道宽度 (二分法)
Description A narrow street is lined with tall buildings. An x foot long ladder is rested at the bas ...
- 分享14个很酷的jQuery导航菜单插件
导航按钮是网站的非常重要的一部分,因其将网站的所有部分而集中一处,jQuery导航菜单插件在其中扮演重要的角色. 本文介绍了14个很酷的jQuery导航菜单插件,它们够漂亮.简单,并且完全兼容各种类型 ...
- robotframework使用requestsLibrary进行接口测试
一.定义 接口测试:接口测试通常是系统之间交互的接口,或者某个系统对外提供的一些接口服务 分类:RESTful.webservice接口 二.安装 进入C:\Pyhon27\scripts 先要安装r ...
- jmeter-添加断言(检查点)-实例
方法/步骤 打开 jmeter的图形界面工具,然后打开之前保存的脚本(之前经验中用到的),demo-baidu.jmx 先点击运行,查看运行结果. 第一次请求返回302,然后跳转到第二次请 ...
- 字典树模板题 POJ 2503
#include <cstdio> #include <cstring> ],fr[]; int st; struct Tire{ ]; ]; }node[]; void in ...
- 第k小整数(树状数组)
洛谷传送门 入门难度.. 没错,但是我并不是要暴力做. 而是用树状数组来做. 先离散化,然后随便搞一搞就可以了.(晕.比暴力还慢) 如果要查找某一区间的的话可以把区间取出重新建树,然后再求.(更暴力) ...