题目大意:

让每天都能吃到西瓜。

最少须要花多少钱。

思路分析:

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

给出这组数据是说,每次买西瓜的时候,都要和前一次的大小做比較,来表示西瓜在这里刚好吃完。

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <cstring>
  5. #define maxn 55555
  6. #define lson num<<1,s,mid
  7. #define rson num<<1|1,mid+1,e
  8. #define inf ((1LL)<<60)
  9. typedef long long LL;
  10. using namespace std;
  11.  
  12. LL dp[maxn<<2];
  13.  
  14. void pushdown(int num)
  15. {
  16. if(dp[num]!=inf)
  17. {
  18. dp[num<<1]=min(dp[num<<1],dp[num]);
  19. dp[num<<1|1]=min(dp[num<<1|1],dp[num]);
  20. dp[num]=inf;
  21. }
  22. }
  23.  
  24. void build(int num,int s,int e)
  25. {
  26. dp[num]=inf;
  27. if(s==e)
  28. {
  29. return;
  30. }
  31. int mid=(s+e)>>1;
  32. build(lson);
  33. build(rson);
  34. }
  35. void update(int num,int s,int e,int l,int r,LL val)
  36. {
  37. if(l<=s && r>=e)
  38. {
  39. dp[num]=min(val,dp[num]);
  40. return;
  41. }
  42. pushdown(num);
  43. int mid=(s+e)>>1;
  44. if(l<=mid)update(lson,l,r,val);
  45. if(r>mid)update(rson,l,r,val);
  46. }
  47.  
  48. LL query(int num,int s,int e,int pos)
  49. {
  50. if(s==e)
  51. {
  52. return dp[num];
  53. }
  54. pushdown(num);
  55. int mid=(s+e)>>1;
  56. if(pos<=mid)return query(lson,pos);
  57. else return query(rson,pos);
  58. }
  59.  
  60. int cost[maxn];
  61. int last[maxn];
  62.  
  63. int main()
  64. {
  65. int n;
  66. while(cin>>n)
  67. {
  68. for(int i=1;i<=n;i++)cin>>cost[i];
  69. for(int i=1;i<=n;i++)cin>>last[i];
  70.  
  71. build(1,1,n);
  72.  
  73. int pos=last[1];
  74. if(pos>n)pos=n;
  75.  
  76. update(1,1,n,1,pos,(LL)cost[1]);
  77.  
  78. for(int i=2;i<=n;i++)
  79. {
  80. LL cur=min(query(1,1,n,i),query(1,1,n,i-1));
  81.  
  82. pos=i+last[i]-1;
  83. if(pos>n)pos=n;
  84.  
  85. update(1,1,n,i,pos,cur+(LL)cost[i]);
  86. }
  87.  
  88. cout<<query(1,1,n,n)<<endl;
  89. }
  90. return 0;
  91. }
  92.  
  93. /*
  94. 5
  95. 1 2 3 4 5
  96. 1 2 2 2 2
  97. */

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. C-基础:关于预编译以及宏

    这是没有引入任何头文件时,如果使用"NULL",编译器会报错:没有定义NULL.此时可用下面代码定义. #undef NULL //#undef 是在后面取消以前定义的宏定义#if ...

  2. 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 ...

  3. HTML中 DOM操作的Document 对象详解(收藏)

    Document 对象Document 对象代表整个HTML 文档,可用来访问页面中的所有元素.Document 对象是 Window 对象的一个部分,可通过 window.document 属性来访 ...

  4. spring注解开发-IOC

    1. @Configuration, @Bean @Configuration该注解就是用来告诉spring这是配置类 @Bean该注解是用来注册一个bean.类型是返回值的类型,ID默认是用方法名作 ...

  5. Go:单元测试

    测试用的文件名必须以 _test.go 结尾: 测试用的函数名必须以 Test 开头,一般来说:Test+被测试的函数名(第一个字母必须大写): func TestXx(t *testing.T) { ...

  6. 杭电 4907 Task schedule ·

    Description 有一台机器,并且给你这台机器的工作表,工作表上有n个任务,机器在ti时间执行第i个任务,1秒即可完成1个任务. 有m个询问,每个询问有一个数字q,表示如果在q时间有一个工作表之 ...

  7. POJ 3660 Cow Contest(求图的传递性)

    题意: 给定n头牛, 然后有m个比较, 求出有多少头牛能确定自己的排名. 分析: 假设有一头牛a, 有ki头牛强于自己, kj头牛弱于自己, ki + kj == n-1时, 那么这头牛的排名就确定了 ...

  8. POJ 1161 Walls(Floyd , 建图)

    题意: 给定n个城市, 然后城市之间会有长城相连, 长城之间会围成M个区域, 有L个vip(每个vip会处于一个城市里)要找一个区域聚会, 问一共最少跨越多少个长城. 分析: 其实这题难就难在建图, ...

  9. Flask--Config研究

    导入Flask框架后,在项目跟目录下面会有一个Config.py 文件,里面的默认内容为: class Config(object): pass 可以这Config 类里面定义变量和其他对象 如: c ...

  10. iOS-runtime-objc_setAssociatedObject(关联对象以及传值)

    例子: static const char kRepresentedObject; - (IBAction)doSomething:(id)sender { UIAlertView *alert = ...