题目链接:http://codeforces.com/gym/100989/problem/D

In this cafeteria, the N tables are all ordered in one line, where table number 1 is the closest to the window and table number N is the closest to the door.

Each time a group of X people enter the cafeteria, one of the cafeteria staff escorts the guests to the table with the smallest number of chairs greater than or equal to X chairs among all available tables. If there’s more than one such table, the guests are escorted to the table that is closest to the window. If there isn't any suitable table available, the group will leave the cafeteria immediately. A table is considered available if no one sits around it.

Eyad likes to help others and also likes to prove that he has learned something from the training of Master Hasan. Therefore, he decided to write a program that helps the cafeteria staff choose the right table for a newly arriving group.

Given the log file of who entered and who left the cafeteria, find the table at which a given group should sit.

Input

The first line of input contains two integers N and Q (1 ≤ N, Q ≤ 105), the number of tables in the cafeteria and the number of events in the log file.

The second line contains N integers, each represents the size of a table (number of chairs around it). The tables are given in order from 1 to N. The size of each table is at least 1 and at most 105.

Each of the following Q lines describes an event in one of the following formats:

- in X: means a group of X (1 ≤ X ≤ 105) people entered the cafeteria.

- out T: means the group on table number T (1 ≤ T ≤ N) just left the cafeteria, the table is now available. It is guaranteed that a group was sitting on this table (input is valid).

Initially, all tables are empty, and the log lists the events in the same order they occurred (in chronological order).

Output

For each event of the first type, print the number of the table on which the coming group should sit. If for any event a group cannot be escorted to any table, print  - 1 for that event.

Example

Input
4 7
1 2 6 7
in 4
in 1
in 3
in 5
out 1
out 4
in 7
Output
3
1
4
-1
4
这道题在比赛的时候感觉蛮容易是,但是没想到用我那种方法会在第十组数据超时,然后用二分法优化之后还是在第四十组数据那超时了,实在想不到什么优化的方法之后看别人的博客才知道这题是要用set来做的,果然还是学的太少了。
附上我比赛时的代码,以便以后看看可不可以优化出来。
代码1:
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int ot[];
struct tb{
int nb;
int sum;
int lg;
bool operator <(const tb& t)const{
if(sum!=t.sum)
return sum<t.sum;
else
return nb<t.nb;
}
}s[];
int bin(int l,int r,int sum){ while (l <= r) {
int mid = (l + r) / ;
if (s[mid].sum == sum) {
if(s[mid].nb==)
return mid;
else
r=mid-;
}
else if (s[mid].sum < sum) {
l = mid + ;
}
else {
r = mid - ;
}
} return l;
}
int main(){
int n,t;
int i,j;
scanf("%d%d",&n,&t);
for(i=;i<n;i++){
scanf("%d",&s[i].sum);
s[i].nb=i+;
s[i].lg=;
}
sort(s,s+n);
char c[];
int a;
while(t--){
getchar();
scanf("%s",&c);
scanf("%d",&a);
int lg1;
if(c[]=='i'){
lg1=;
i=bin(,n-,a);
for(;i<n&&lg1;i++){
if((s[i].lg==||ot[s[i].nb]==)&&s[i].sum>=a){
printf("%d\n",s[i].nb);
s[i].lg=;
ot[s[i].nb]=;
lg1=;
//printf("%d %d\n",s[i].lg,ot[s[i].nb]);
}
}
if(lg1==)
printf("-1\n");
}
else if(c[]=='o'){
ot[a]=;
}
}
return ;
}

用set后写的代码:

#include<cstdio>
#include<set>
using namespace std;
set<pair<int,int> > st1;
int s[100050];
int main(){
int n,q;
scanf("%d%d",&n,&q);
int i,j;
int a,b;
for(i=1;i<=n;i++){
scanf("%d",&s[i]);
st1.insert(make_pair(s[i],i) );
}
char c[5];
while(q--){
getchar();
scanf("%s%d",c,&a);
if(c[0]=='i'){
set<pair<int,int> >::iterator it;
it=st1.lower_bound(make_pair(a,0));
if(it==st1.end()){
printf("-1\n");
}
else{
printf("%d\n",it->second);
st1.erase(it);
}
}
else if(c[0]=='o'){
st1.insert(make_pair(s[a],a));
}
}
return 0;
}

set的主要用法(参考自https://www.cnblogs.com/omelet/p/6627667.html)

set的特性是,所有元素都会根据元素的键值自动排序,set的元素不像map那样可以同时拥有实值(value)和键值(key),set元素的键值就是实值,实值就是键值。set不允许两个元素有相同的键值。

set的各成员函数列表如下:

1. begin()--返回指向第一个元素的迭代器

2. clear()--清除所有元素

3. count()--返回某个值元素的个数

4. empty()--如果集合为空,返回true

5. end()--返回指向最后一个元素的迭代器

6. equal_range()--返回集合中与给定值相等的上下限的两个迭代器

7. erase(迭代器)--删除集合中的元素

8. find()--返回一个指向被查找到元素的迭代器

9. get_allocator()--返回集合的分配器

10. insert(pair类型数据)--在集合中插入元素

11. lower_bound(pair类型数据)--返回指向大于(或等于)某值的第一个元素的迭代器

12. key_comp()--返回一个用于元素间值比较的函数

13. max_size()--返回集合能容纳的元素的最大限值

14. rbegin()--返回指向集合中最后一个元素的反向迭代器

15. rend()--返回指向集合中第一个元素的反向迭代器

16. size()--集合中元素的数目

17. swap()--交换两个集合变量

18. upper_bound()--返回大于某个值元素的迭代器

19. value_comp()--返回一个用于比较元素间的值的函数

set,pair容器使用方法的更多相关文章

  1. [笔记]使用Go语言Redigo包在Docker容器内连接Redis容器的方法

    Docker容器之间的连接可以带来不少方便,下面记录下如何在自己容器内通过环境变量连接与之连接的Redis容器的方法. 先起一个Redis的Docker容器,命名为 redis,再起一个自己的Dock ...

  2. .net core 2.0下的容器注册方法

    https://www.cnblogs.com/Wddpct/p/5764511.html 自带的容器注册方法真的很好用

  3. STL中pair容器的用法

    1.定义pair容器 1 pair <int, int> p, p1; 2 //定义 [int,int] 型容器 //直接初始化了p的内容 pair<string,int>p( ...

  4. 关联容器 // append方法

    关联容器和顺序容器的差别在于:关联容器通过键(key)存储和读取元素,而顺序容器则通过元素在容器中的位置顺序存储和访问元素. 1.关联容器支持通过键来高效地查找和读取元素.两个基本的关联容器类型是ma ...

  5. C++中 pair 的使用方法

    #include<iostream> #include<string> #include<map> using namespace std; // pair简单讲就 ...

  6. 两种进入容器的方法 - 每天5分钟玩转 Docker 容器技术(23)

    我们经常需要进到容器里去做一些工作,比如查看日志.调试.启动其他进程等.有两种方法进入容器:attach 和 exec. docker attach 通过 docker attach 可以 attac ...

  7. c++ 中 pair 的 使用方法

    原转载地址:点击打开链接 pair的类型: pair 是 一种模版类型.每个pair 可以存储两个值.这两种值无限制.也可以将自己写的struct的对象放进去.. pair<string,int ...

  8. Fragment嵌套Fragment时候。子类fragment调用父容器Fragment方法

    业务场景:有的时候我们的页面可能是Activity 嵌套多个Fragment ..其中某个Fragment 又嵌套多个Fragment. 其中某个子Fragment  定义为  NewsFragmen ...

  9. 获取spring容器对象方法和原因

    为什么要获取Spring容器对象:拿到spring容器对象后,你就可以用spring管理的bean了,拿到bean,自然可以使用bean的方法,场景:比如jsp页面.通过注解是无法注入bean的,在开 ...

随机推荐

  1. 20181013xlVba计算优秀率及合格率

    Sub 计算高一优秀合格率() Dim Wb As Workbook Dim Sht As Worksheet Dim oSht As Worksheet Dim dOs As Object 'Out ...

  2. Confluence 6 指派空间权限概念

    如果你是一个空间的管理员,你可以对空间的使用权限进行控制.你可以为独立用户或者Confluence Groups的权限进行权限的指派/收回. Confluence 管理员可以将用户分配到用户组中,这样 ...

  3. 『TensorFlow』分布式训练_其三_多机分布式

    本节中的代码大量使用『TensorFlow』分布式训练_其一_逻辑梳理中介绍的概念,是成熟的多机分布式训练样例 一.基本概念 Cluster.Job.task概念:三者可以简单的看成是层次关系,tas ...

  4. centos7 --kubeadm安装

    One or more machines running one of: Ubuntu 16.04+ Debian 9 CentOS 7 RHEL 7 Fedora 25/26 (best-effor ...

  5. leetcode-algorithms-20 Valid Parentheses

    leetcode-algorithms-20 Valid Parentheses Given a string containing just the characters '(', ')', '{' ...

  6. Spring Boot项目打包部署到外部Tomcat

    1.生成war包 1)修改POM文件,将打包类型改为war:<packaging>war</packaging> <packaging>war</packag ...

  7. C++ string的用法和例子

    使用场合: string是C++标准库的一个重要的部分,主要用于字符串处理.可以使用输入输出流方式直接进行操作,也可以通过文件等手段进行操作.同时C++的算法库对string也有着很好的支持,而且st ...

  8. Oracle11g温习-第十一章:管理undo

    2013年4月27日 星期六 10:40 1.undo tablespace 功能 undo tablespace 功能:用来存放从datafiles 读出的数据块旧的镜像 [             ...

  9. python 小练习2

    给你一个整数列表L,判断L中是否存在相同的数字, 若存在,输出YES,否则输出NO.解1l=[]for i in L:    if L.count(i) != 1:        print('YES ...

  10. IntelliJ IDEA下载及安装,破解

    IntelliJ IDEA下载及安装,破解 百度百科:IDEA 全称IntelliJ IDEA,是java语言开发的集成环境,IntelliJ在业界被公认为最好的java开发工具之一,尤其在智能代码助 ...