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种颜色 ...
随机推荐
- C-基础:关于预编译以及宏
这是没有引入任何头文件时,如果使用"NULL",编译器会报错:没有定义NULL.此时可用下面代码定义. #undef NULL //#undef 是在后面取消以前定义的宏定义#if ...
- linux 安装nginx 集成emq
1:下载nginx-1.12.2.tar.gz http://nginx.org/en/download.html 2:解压 tar -zxvf nginx-1.12.2.tar.gz 3:进行co ...
- HTML中 DOM操作的Document 对象详解(收藏)
Document 对象Document 对象代表整个HTML 文档,可用来访问页面中的所有元素.Document 对象是 Window 对象的一个部分,可通过 window.document 属性来访 ...
- spring注解开发-IOC
1. @Configuration, @Bean @Configuration该注解就是用来告诉spring这是配置类 @Bean该注解是用来注册一个bean.类型是返回值的类型,ID默认是用方法名作 ...
- Go:单元测试
测试用的文件名必须以 _test.go 结尾: 测试用的函数名必须以 Test 开头,一般来说:Test+被测试的函数名(第一个字母必须大写): func TestXx(t *testing.T) { ...
- 杭电 4907 Task schedule ·
Description 有一台机器,并且给你这台机器的工作表,工作表上有n个任务,机器在ti时间执行第i个任务,1秒即可完成1个任务. 有m个询问,每个询问有一个数字q,表示如果在q时间有一个工作表之 ...
- POJ 3660 Cow Contest(求图的传递性)
题意: 给定n头牛, 然后有m个比较, 求出有多少头牛能确定自己的排名. 分析: 假设有一头牛a, 有ki头牛强于自己, kj头牛弱于自己, ki + kj == n-1时, 那么这头牛的排名就确定了 ...
- POJ 1161 Walls(Floyd , 建图)
题意: 给定n个城市, 然后城市之间会有长城相连, 长城之间会围成M个区域, 有L个vip(每个vip会处于一个城市里)要找一个区域聚会, 问一共最少跨越多少个长城. 分析: 其实这题难就难在建图, ...
- Flask--Config研究
导入Flask框架后,在项目跟目录下面会有一个Config.py 文件,里面的默认内容为: class Config(object): pass 可以这Config 类里面定义变量和其他对象 如: c ...
- iOS-runtime-objc_setAssociatedObject(关联对象以及传值)
例子: static const char kRepresentedObject; - (IBAction)doSomething:(id)sender { UIAlertView *alert = ...