郁闷的出纳员

思路:

  设工资下限为ko,然后ko--(因为要小于工资下限);

  设cur为记录工资增长,降低;

  设第i个人的工资为pos;

  对应的四种操作:

    插入:cur-pos-ko;

    增长:cur-=pos;

    降低:cur+=pos;

      每个降低操作都要进行一次删除节点;

      把小于等于cur的节点全部删掉;

    排名:输出rank()-cur+ko;

  splay支持以上全部操作;

  还有一点,如果一开始这个人的工资小于ko,则不算在离去的人里;

来,上代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define maxn 1000005 struct TreeNodeType {
int w,key,opi,size,ch[]; void destroy()
{
w=key=opi=size=ch[]=ch[]=;
} void create(int x)
{
key=x;
w=size=;
opi=ch[]=ch[]=;
}
};
struct TreeNodeType tree[maxn<<]; int n,ko,root,tot,cur,ans,tot_; inline void in(int &now)
{
register char Cget=getchar();now=;
while(Cget>''||Cget<'') Cget=getchar();
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
} inline int getson(int now)
{
return tree[tree[now].opi].ch[]==now;
} inline void updata(int now)
{
tree[now].size=tree[now].w;
if(tree[now].ch[]) tree[now].size+=tree[tree[now].ch[]].size;
if(tree[now].ch[]) tree[now].size+=tree[tree[now].ch[]].size;
} inline void rotate(int now)
{
int opi=tree[now].opi,fopi=tree[opi].opi,pos=getson(now);
if(tree[tree[now].ch[pos^]].opi)tree[tree[now].ch[pos^]].opi=opi;
tree[opi].ch[pos]=tree[now].ch[pos^];
if(fopi) tree[fopi].ch[getson(opi)]=now;
tree[opi].opi=now;tree[now].opi=fopi;
tree[now].ch[pos^]=opi;
updata(opi),updata(now);
} void splay(int now)
{
for(int opi;opi=tree[now].opi;rotate(now))
{
if(tree[opi].opi) rotate(getson(now)==getson(opi)?opi:now);
}
root=now;
} void insert(int x)
{
if(!root) tree[++tot].create(x),root=tot;
else
{
int now=root,opi=;
while()
{
if(tree[now].key==x)
{
tree[now].w++;
tree[now].size++;
splay(now);
break;
}
opi=now,now=tree[now].ch[x>tree[now].key];
if(!now)
{
tot++;
tree[tot].create(x);
tree[tot].opi=opi;
tree[opi].ch[x>tree[opi].key]=tot;
splay(tot);break;
}
}
}
} void del()
{
ans+=tree[root].size-;
if(!tree[root].ch[]) tree[].destroy(),root=;
else
{
int tmp=root;
root=tree[root].ch[];
tree[tmp].destroy();
tree[root].opi=;
ans-=tree[root].size;
}
} int rank(int x)
{
int now=root;
while()
{
if(tree[now].ch[])
{
if(x>tree[tree[now].ch[]].size) x-=tree[tree[now].ch[]].size;
else
{
now=tree[now].ch[];
continue;
}
}
if(x<=tree[now].w)
{
splay(now);
return tree[now].key;
}
else
{
x-=tree[now].w;
now=tree[now].ch[];
}
}
} int main()
{
in(n),in(ko),ko--;
char ch[];int pos;
while(n--)
{
scanf("%s",ch);in(pos);
if(ch[]=='I') if(pos>ko) insert(pos+cur-ko),tot_++;
if(ch[]=='A') cur-=pos;
if(ch[]=='S') cur+=pos,insert(cur),del();
if(ch[]=='F')
{
if(tot_-ans>=pos) printf("%d\n",rank(tot_-ans-pos+)-cur+ko);
else printf("-1\n");
}
}
printf("%d\n",ans);
return ;
}

AC日记——郁闷的出纳员 codevs 1286的更多相关文章

  1. AC日记——接龙游戏 codevs 1051

    1051 接龙游戏  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond       题目描述 Description 给出了N个单词,已经按长度排好了序.如果 ...

  2. AC日记——地鼠游戏 codevs 1052

    1052 地鼠游戏  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description 王钢是一名学习成绩优异的学生,在平 ...

  3. AC日记——鬼谷子的钱袋 codevs 2998

    2998 鬼谷子的钱袋 2006年省队选拔赛湖南  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master    题目描述 Description 鬼谷子非常聪明,正 ...

  4. AC日记——舒适的路线 codevs 1001 (并查集+乱搞)

    1001 舒适的路线 2006年  时间限制: 2 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Description Z小镇是 ...

  5. AC日记——逃跑的拉尔夫 codevs 1026 (搜索)

    1026 逃跑的拉尔夫  时间限制: 1 s    空间限制: 128000 KB    题目等级 : 黄金 Gold 题解       题目描述 Description   年轻的拉尔夫开玩笑地从一 ...

  6. AC日记——自然数和分解 codevs 2549

    自然数和分解 思路: 水题: 代码: #include <bits/stdc++.h> using namespace std; ][]; int main() { cin>> ...

  7. AC日记——营业额统计 1296 codevs

    1296 营业额统计 2002年  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master 题解  查看运行结果     题目描述 Description Tiger ...

  8. AC日记——花店橱窗布置 codevs 1028

    题目描述 Description 假设以最美观的方式布置花店的橱窗,有F束花,V个花瓶,我们用美学值(一个整数)表示每束花放入每个花瓶所产生的美学效果.为了取得最佳的美学效果,必须使花的摆放取得最大的 ...

  9. AC日记——最优贸易 codevs 1173

    题目描述 Description [问题描述]C 国有n 个大城市和m 条道路,每条道路连接这n 个城市中的某两个城市.任意两个城市之间最多只有一条道路直接相连.这m 条道路中有一部分为单向通行的道路 ...

随机推荐

  1. Codeforces Round #271 (Div. 2) D Flowers【计数dp】

    D. Flowers time limit per test 1.5 seconds memory limit per test 256 megabytes input standard input ...

  2. Detecting iOS

    Detecting iOS I am not a fan of User Agent sniffing, but here is how you would do it: var iOS = /iPa ...

  3. Spring MVC 使用 HttpServletResponseWrapper 修改返回结果

    HttpServletResponseWrapper 是什么? ServletResponse 的包装类,相关设计模式 装饰者模式. 运行环境 jdk 1.7 spring boot 整合的web环境 ...

  4. 相当牛X的java版星际游戏

    分享一款牛人用java写的经典游戏,目录结构如下: 虽然只能算一个Demo,但是用到了很多Java基础技术和算法: Java2D,双缓冲,A星寻路,粒子系统,动画效果,处理图片,Swing ui ,U ...

  5. vue知识

    https://juejin.im/post/5af16a2cf265da0b8636353b

  6. js跨域post请求

    function funPostBack(srvMethod){ /* var contentNR=$(document.getElementById("reportFrame") ...

  7. [python][django学习篇][2]创建django app

    推荐学校django博客:http://pythonzh.cn/post/8/ django app 可以理解为一个文件夹: 里面包含了相关功能的代码.通过manage.py来创建 web app 激 ...

  8. [转]Docker容器内不能联网的6种解决方案

    注: 下面的方法是在容器内能ping通公网IP的解决方案,如果连公网IP都ping不通,那主机可能也上不了网(尝试ping 8.8.8.8) 1.使用--net:host选项 sudo docker ...

  9. NSURLSession使用模板和AFNetworking使用模板(REST风格)

    1.NSURLSession使用模板 NSURLSession是苹果ios7后提供的api,用来替换 NSURLConnection会话指的是程序和服务器的通信对象//一.简单会话不可以配合会话(ge ...

  10. HDU 1043 & POJ 1077 Eight(康托展开+BFS | IDA*)

    Eight Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30176   Accepted: 13119   Special ...