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种颜色 ...
随机推荐
- QT_7_资源文件_对话框_QMessageBox_界面布局_常用控件
资源文件 1.1. 将资源导入到项目下 1.2. 添加文件—>Qt -->Qt Resource File 1.3. 起名称 res ,生成res.qrc文件 1.4. 右键 open i ...
- web前端中的一些注释表达法
1.HTML注释 <!--注释的内容--> 注释的地方(根据个人习惯可能有所不同): 结束标签的后面,这一切都是为了程序在嵌套的时候更加方便.明了,如: <div class=&qu ...
- web中的$多种意思
$符号在php中是表示变量的特征字符, 在js中它也有很多作用, 一般我们用来命名一个函数名称,获取id的1.首先可以用来表示变量, 比如变量 var s='asdsd'或var $s='asdasd ...
- JS应用之正则表达式
定义 正则表达式是用于匹配字符串中字符组合的模式. 创建正则表达式 两种方式: 1.new RegExp() let pattern1 = new RegExp('cat'); //第一个参数字符串 ...
- (二十)python 3 匿名函数
匿名函数lambda Python使用lambda关键字创造匿名函数.所谓匿名,意即不再使用def语句这样标准的形式定义一个函数.这种语句的目的是由于性能的原因,在调用时绕过函数的栈分配.其语法是: ...
- Saving James Bond - Hard Version
07-图5 Saving James Bond - Hard Version(30 分) This time let us consider the situation in the movie &q ...
- 【C#】【数据结构】006-栈:链栈
C#数据结构:链栈 1.自定义链栈结构: 链栈节点类 using System.Collections; using System.Collections.Generic; using UnityEn ...
- kafka flumn sparkstreaming java实现监听文件夹内容保存到Phoenix中
ps:具体Kafka Flumn SparkStreaming的使用 参考前几篇博客 2.4.6.4.1 配置启动Kafka (1) 在slave机器上配置broker 1) 点击CDH上的kafk ...
- iframe in ipad safari
http://developer.apple.com/library/safari/#documentation/appleapplications/reference/safariwebconten ...
- Oracle常用内置数据表查询
Oracle 查询库中所有表名.字段名.字段名说明,查询表的数据条数.表名.中文表名. 查询所有表名:select t.table_name from user_tables t;查询所有字段名:se ...