经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C++ » 查看文章
[luogu1486][郁闷的出纳员]
来源:cnblogs  作者:wxyww  时间:2018/12/3 10:13:30  对本文有异议

题目链接

思路

这个题其实就是对于treap中的删除操作进行一些修改。自己yy了一种做法。就是在删除的时候,如果要删除的数比这棵子树的根大,那么就把根变成根的右孩子,这样就相当于删除了整棵左子树和根节点。然后重新维护一下siz,并且维护一下平衡性就行了。

竟然把rotate函数写错了。调了30min。又没正确理解

如果某个员工的初始工资低于最低工资标准,那么将不计入最后的答案内

这句话的含义,又调了30min。23333

代码

  1. //每次修改操作之后都进行一次删除,并且更改删除函数
  2. /*
  3. * @Author: wxyww
  4. * @Date: 2018-12-02 08:41:38
  5. * @Last Modified time: 2018-12-02 10:02:49
  6. */
  7. #include<cstdio>
  8. #include<iostream>
  9. #include<cstdlib>
  10. #include<cmath>
  11. #include<ctime>
  12. #include<bitset>
  13. using namespace std;
  14. #define ls TR[cur].ch[0]
  15. #define rs TR[cur].ch[1]
  16. typedef long long ll;
  17. const int N = 100000 + 100,INF = 1e9 + 7;
  18. ll read() {
  19. ll x=0,f=1;char c=getchar();
  20. while(c<'0'||c>'9') {
  21. if(c=='-') f=-1;
  22. c=getchar();
  23. }
  24. while(c>='0'&&c<='9') {
  25. x=x*10+c-'0';
  26. c=getchar();
  27. }
  28. return x*f;
  29. }
  30. struct node {
  31. int ch[2],id,val,siz,cnt;
  32. }TR[N];
  33. int MIN;
  34. void up(int cur) {
  35. TR[cur].siz = TR[ls].siz + TR[rs].siz + TR[cur].cnt;
  36. }
  37. void rotate(int &cur,int f) {
  38. int son = TR[cur].ch[f];
  39. TR[cur].ch[f] = TR[son].ch[f ^ 1];
  40. TR[son].ch[f ^ 1] = cur;
  41. up(cur);
  42. cur = son;
  43. up(cur);
  44. }
  45. int tot;
  46. void insert(int &cur,int val) {
  47. if(!cur) {
  48. cur = ++tot;
  49. TR[cur].id = rand();
  50. TR[cur].val = val;
  51. TR[cur].siz = TR[cur].cnt = 1;
  52. return;
  53. }
  54. TR[cur].siz++;
  55. if(val == TR[cur].val) {TR[cur].cnt++;return;}
  56. int d = val > TR[cur].val;
  57. insert(TR[cur].ch[d],val);
  58. if(TR[TR[cur].ch[d]].id < TR[cur].id) rotate(cur,d);
  59. }
  60. void del(int &cur,int val) {
  61. if(!cur) return;
  62. if(val <= TR[cur].val) del(ls,val);
  63. else {
  64. del(rs,val);
  65. cur = rs;
  66. }
  67. up(cur);
  68. if(TR[ls].id < TR[cur].id && ls) rotate(cur,0);
  69. if(TR[rs].id < TR[cur].id && rs) rotate(cur,1);
  70. }
  71. int kth(int cur,int now) {
  72. while(1) {
  73. if(now > TR[rs].siz + TR[cur].cnt) now -= TR[rs].siz + TR[cur].cnt,cur = ls;
  74. else if(now <= TR[rs].siz) cur = rs;
  75. else return TR[cur].val;
  76. }
  77. }
  78. int main() {
  79. int rt = 0;
  80. int n = read(),change = 0,num = 0;
  81. MIN = read();
  82. char c;
  83. while(n--) {
  84. int x;
  85. scanf("\n%c %d",&c,&x);
  86. if(c == 'I') {
  87. x -= change;
  88. if(x >= MIN) num++,insert(rt,x);
  89. }
  90. if(c == 'A') MIN -= x,change += x;
  91. if(c == 'S') {
  92. MIN += x;
  93. del(rt,MIN);
  94. change -= x;
  95. }
  96. if(c == 'F') {
  97. if(x > TR[rt].siz) puts("-1");
  98. else printf("%d\n",kth(rt,x) + change);
  99. }
  100. }
  101. cout<<num - TR[rt].siz<<endl;
  102. return 0;
  103. }

一言

时间会把你欠下的对不起,变成还不起,又会把很多对不起,变成来不及。 ——乖,摸摸头

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号