题目地址:http://codeforces.com/contest/474/problem/E

第一次遇到这样的用线段树来维护DP的题目。ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是那题有简单方法,于是就没写。这次最终写出来了。。

这题的DP思想跟求最长上升子序列的思想是一样的。仅仅只是这里的找前面最大值时会超时,所以能够用线段树来维护这个最大值,然后因为还要输出路径,所以要用线段树再来维护一个每一个数在序列中所在的位置信息。

手残了好多地方,最终调试出来了。。。

代码例如以下:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstring>
  5. #include <stdlib.h>
  6. #include <math.h>
  7. #include <ctype.h>
  8. #include <queue>
  9. #include <map>
  10. #include <set>
  11. #include <algorithm>
  12.  
  13. using namespace std;
  14. #define LL __int64
  15. #define lson l, mid, rt<<1
  16. #define rson mid+1, r, rt<<1|1
  17. const int INF=0x3f3f3f3f;
  18. const int MAXN=100000;
  19. int maxv[MAXN<<2], cnt, pre[MAXN+10], f[MAXN+10], q_maxp, maxp[MAXN<<2], q_maxv;
  20. LL a[MAXN+10], c[MAXN+10], d[MAXN+10];
  21. void PushUp(int rt)
  22. {
  23. maxv[rt]=max(maxv[rt<<1],maxv[rt<<1|1]);
  24. if(maxv[rt<<1]>=maxv[rt<<1|1])
  25. maxp[rt]=maxp[rt<<1];
  26. else
  27. maxp[rt]=maxp[rt<<1|1];
  28. }
  29. void update(int p, int x, int i, int l, int r, int rt)
  30. {
  31. if(l==r)
  32. {
  33. maxv[rt]=x;
  34. maxp[rt]=i;
  35. return ;
  36. }
  37. int mid=l+r>>1;
  38. if(p<=mid) update(p,x,i,lson);
  39. else update(p,x,i,rson);
  40. PushUp(rt);
  41. }
  42. void query(int ll, int rr, int l, int r, int rt)
  43. {
  44. if(ll<=l&&rr>=r)
  45. {
  46. if(q_maxv<maxv[rt])
  47. {
  48. q_maxv=maxv[rt];
  49. q_maxp=maxp[rt];
  50. }
  51. return ;
  52. }
  53. int mid=l+r>>1, ans=0;
  54. if(ll<=mid) query(ll,rr,lson);
  55. if(rr>mid) query(ll,rr,rson);
  56. }
  57. int bin_seach(LL x)
  58. {
  59. int low=0, high=cnt-1, mid;
  60. while(low<=high)
  61. {
  62. mid=low+high>>1;
  63. if(d[mid]==x) return mid;
  64. else if(d[mid]>x) high=mid-1;
  65. else low=mid+1;
  66. }
  67. }
  68. int l_seach(LL x)
  69. {
  70. int low=0, high=cnt-1, mid, ans=-1;
  71. while(low<=high)
  72. {
  73. mid=low+high>>1;
  74. if(d[mid]<=x)
  75. {
  76. ans=mid;
  77. low=mid+1;
  78. }
  79. else high=mid-1;
  80. }
  81. return ans;
  82. }
  83. int r_seach(LL x)
  84. {
  85. int low=0, high=cnt-1, mid, ans=-1;
  86. while(low<=high)
  87. {
  88. mid=low+high>>1;
  89. if(d[mid]>=x)
  90. {
  91. ans=mid;
  92. high=mid-1;
  93. }
  94. else low=mid+1;
  95. }
  96. return ans;
  97. }
  98. void print(int x)
  99. {
  100. if(x==-1) return ;
  101. print(pre[x]);
  102. printf("%d ",x+1);
  103. }
  104. int main()
  105. {
  106. int n, dd, i, x, ans, y, z, max1=-1, pos, tot;
  107. scanf("%d%d",&n,&dd);
  108. for(i=0; i<n; i++)
  109. {
  110. scanf("%I64d",&a[i]);
  111. c[i]=a[i];
  112. }
  113. sort(c,c+n);
  114. d[0]=c[0];
  115. cnt=1;
  116. for(i=1; i<n; i++)
  117. {
  118. if(c[i]!=c[i-1])
  119. {
  120. d[cnt++]=c[i];
  121. }
  122. }
  123. /*for(i=0;i<cnt;i++)
  124. {
  125. printf("%d ",c[i]);
  126. }
  127. puts("");*/
  128. memset(maxv,0,sizeof(maxv));
  129. memset(pre,-1,sizeof(pre));
  130. for(i=0; i<n; i++)
  131. {
  132. x=bin_seach(a[i]);
  133. y=l_seach(a[i]-dd);
  134. z=r_seach(a[i]+dd);
  135. //printf("%d %d %d\n",x,y,z);
  136. q_maxp=-1;
  137. q_maxv=-1;
  138. if(y!=-1)
  139. query(0,y,0,cnt-1,1);
  140. if(z!=-1)
  141. query(z,cnt-1,0,cnt-1,1);
  142. update(x,q_maxv+1,i,0,cnt-1,1);
  143. pre[i]=q_maxp;
  144. if(q_maxv==0)
  145. pre[i]=-1;
  146. if(max1<q_maxv+1)
  147. {
  148. max1=q_maxv+1;
  149. pos=i;
  150. }
  151. }
  152. printf("%d\n",max1);
  153. print(pos);
  154. return 0;
  155. }

Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)的更多相关文章

  1. Codeforces Round #271 (Div. 2) F. Ant colony 线段树

    F. Ant colony time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  2. Codeforces Round #305 (Div. 2) D题 (线段树+RMQ)

    D. Mike and Feet time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  3. Codeforces Round #426 (Div. 2) D. The Bakery 线段树优化DP

    D. The Bakery   Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought req ...

  4. Codeforces Round #271 (Div. 2) F题 Ant colony(线段树)

    题目地址:http://codeforces.com/contest/474/problem/F 由题意可知,最后能够留下来的一定是区间最小gcd. 那就转化成了该区间内与区间最小gcd数相等的个数. ...

  5. Codeforces Round #603 (Div. 2) E. Editor(线段树)

    链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...

  6. Codeforces Round #222 (Div. 1) D. Developing Game 线段树有效区间合并

    D. Developing Game   Pavel is going to make a game of his dream. However, he knows that he can't mak ...

  7. Codeforces Round #244 (Div. 2) B. Prison Transfer 线段树rmq

    B. Prison Transfer Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/pro ...

  8. Codeforces Round #546 (Div. 2) E 推公式 + 线段树

    https://codeforces.com/contest/1136/problem/E 题意 给你一个有n个数字的a数组,一个有n-1个数字的k数组,两种操作: 1.将a[i]+x,假如a[i]+ ...

  9. Codeforces Round #275 Div.1 B Interesting Array --线段树

    题意: 构造一个序列,满足m个形如:[l,r,c] 的条件. [l,r,c]表示[l,r]中的元素按位与(&)的和为c. 解法: 线段树维护,sum[rt]表示要满足到现在为止的条件时该子树的 ...

随机推荐

  1. OGG-01008 Extract displays Discarding bad record (discard recs=1) when using filter or where clause

    因为在extract參数文件里使用了where语句,而where后面的的条件列又不是主键,没有为update.delete操作记录日志,因此会报1008错误. Applies to: Oracle G ...

  2. HDU1163 Eddy&#39;s digital Roots【九剩余定理】

    Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  3. Swift基础--使用TableViewController自己定义列表

    首先建立一个swift项目,把storyboard的内容删掉,加入一个Navigation Controller.然后设置storyboard相应界面的class,在Navigation Contro ...

  4. [改变自己wordpress.2]至wordpress再加上简单的debug sql调试.

    或者说,同事. 需要帮助她打印出来sql 调试输出到页面sql 在这里,我们使用插件或一个的方式来启动配置文件wordpress的debug 在插件文件夹 wordpress/wp-content/p ...

  5. setInterval定时和ajax请求

    fnSetMarkPoint = function (param) { $.ajax({ success: function (returnValue) { window.setInterval(&q ...

  6. LNMP 免安装包

    LNMP(Linux-Nginx-Mysql-PHP)可爱的黄金搭档,不过配置并不轻易,而我平常用于测试环境又经常用到,所以打包了这么一个免安装的LNMP包,内置常用库和模块,以及基本的优化设置,这样 ...

  7. jquery处理页面元素

    处理父级页面中的元素 $(parent.document).find('#hidSendPerson').val(val);$(parent.document).find('#btnGo').clic ...

  8. 查看.a架构文件

    苹果公司现在要求所有新提交的评论app,我们必须支持64位架构.而我们的在线项目编制,操作员做了一堆SDK在需要访问,我们发现,在这个过程中,有些SDK的.a文件进入后,链接错误,如提示 Undefi ...

  9. vuejs 相关资料

    官网 http://vuejs.org/ 中文网站 http://cn.vuejs.org/ Vue.js——60分钟快速入门 http://www.cnblogs.com/keepfool/p/56 ...

  10. TCP连接建立过程中为什么需要“三次握手”(转)

    传输控制协议(Transmission Control Protocol, TCP)是一种面向连接的.可靠的.基于字节流的运输层(Transport layer)通信协议.是专门为了在不可靠的互联网络 ...