题目大意:

让每天都能吃到西瓜。

最少须要花多少钱。

思路分析:

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)的更多相关文章

  1. ZOJ 1610 Count the Colors (线段树区间更新)

    题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头 ...

  2. ZOJ - 1610 Count the Colors(线段树区间更新,单点查询)

    1.给了每条线段的颜色,存在颜色覆盖,求表面上能够看到的颜色种类以及每种颜色的段数. 2.线段树区间更新,单点查询. 但是有点细节,比如: 输入: 2 0 1 1 2 3 1 输出: 1 2 这种情况 ...

  3. ZOJ 1610 Count the Colors (线段树区间更新与统计)

    Painting some colored segments on a line, some previously painted segments may be covered by some th ...

  4. Zoj 1610 Count the Colors (线段树+区间更新+暴力计数)

    题目大意: 有n次操作,每次都是对一根线中的一段区间进行染色(颜色并不相同),有时候后面的颜色有可能覆盖前面的颜色,问最后涂完色,能看到的颜色有几种,每种颜色有几部分? 解题思路: 这个题目建树的时候 ...

  5. ZOJ - 1610 Count the Colors(线段树区间更新)

    https://cn.vjudge.net/problem/ZOJ-1610 题意 给一个n,代表n次操作,接下来每次操作表示把[l,r]区间的线段涂成k的颜色其中,l,r,k的范围都是0到8000. ...

  6. ZOJ 1610 Count the Color(线段树区间更新)

    描述Painting some colored segments on a line, some previously painted segments may be covered by some ...

  7. HDU 1556 Color the ball(线段树区间更新)

    Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...

  8. hihoCoder 1080 : 更为复杂的买卖房屋姿势 线段树区间更新

    #1080 : 更为复杂的买卖房屋姿势 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho都是游戏迷,“模拟都市”是他们非常喜欢的一个游戏,在这个游戏里面他们 ...

  9. HDU 5023 A Corrupt Mayor's Performance Art(线段树区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023 解题报告:一面墙长度为n,有N个单元,每个单元编号从1到n,墙的初始的颜色是2,一共有30种颜色 ...

随机推荐

  1. Microsoft Windows Server 部署

    Microsoft Windows Server 部署 多重引导 计算机可以被设置多重引导,即在一台计算机上安装多个操作系统..在安装多重引导的操作系统时,还要注意版本的类型,一般应先安装版本低的,再 ...

  2. 【收藏】史上最全的浏览器 CSS & JS Hack 手册

    浏览器渲染页面的方式各不相同,甚至同一浏览器的不同版本(“杰出代表”是 IE)也有差异.因此,浏览器兼容成为前端开发人员的必备技能.如果有一份浏览器 Hack 手册,那查询起来就方便多了.这篇文章就向 ...

  3. ajax从入门到深入精通

    前言 ajax全称Asynchronous Javascript and XML.其中Asynchronous代表异步.同步于异步是描述通信模式的概念,同步机制:发送方发生请求后,需要等待接收到接收方 ...

  4. 手动编译openslide

    1.下载openslide源代码, 2.转到openslide代码目录: ./configure 3.安装依赖库: sudo apt-get update sudo apt-get install l ...

  5. 条款17:以独立语句将newed对象置入智能指针(Store newed objects in smart pointers in standalone statements)

    NOTE: 1.以独立语句将newed对象存储于智能指针内.如果不这样做,一旦异常被抛出,有可能导致难以察觉的资源泄漏.

  6. mysql多字段组合删除重复行

    DELETEFROM boll_paramWHERE id in ( SELECT a.id FROM ( SELECT id FROM boll_param WHERE (symbol, time_ ...

  7. POJ 1905 Expanding Rods (求直杆弯曲拱起的高度)(二分法,相交弦定理)

    Description When a thin rod of length L is heated n degrees, it expands to a new length L' = (1+n*C) ...

  8. 为什么在属性中设置private set

    引言: 属性的引入来自C#的封装机制,也就是说对象的内部数据不应该由对象实例来直接访问,我们可以使用传统的Get和Set方法,来封装字段,C#为我们提供了语法糖,也就是属性.属性包括get和set,分 ...

  9. C#基于引用创建单链表

    在C语言,单链表的实现依赖指针,指针用来指向节点,那么,用C#实现,自然就想到引用,节点的引用不就类似于指向Node的指针嘛

  10. python之tkinter变量设置 2014-4-9

    python 可以自己定义变量以及变量类型mystring = StringVar(ticked_yes = BooleanVoption1 = IntVar()volume = DoubleVar( ...