set,pair容器使用方法
题目链接: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
4 7
1 2 6 7
in 4
in 1
in 3
in 5
out 1
out 4
in 7
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容器使用方法的更多相关文章
- [笔记]使用Go语言Redigo包在Docker容器内连接Redis容器的方法
Docker容器之间的连接可以带来不少方便,下面记录下如何在自己容器内通过环境变量连接与之连接的Redis容器的方法. 先起一个Redis的Docker容器,命名为 redis,再起一个自己的Dock ...
- .net core 2.0下的容器注册方法
https://www.cnblogs.com/Wddpct/p/5764511.html 自带的容器注册方法真的很好用
- STL中pair容器的用法
1.定义pair容器 1 pair <int, int> p, p1; 2 //定义 [int,int] 型容器 //直接初始化了p的内容 pair<string,int>p( ...
- 关联容器 // append方法
关联容器和顺序容器的差别在于:关联容器通过键(key)存储和读取元素,而顺序容器则通过元素在容器中的位置顺序存储和访问元素. 1.关联容器支持通过键来高效地查找和读取元素.两个基本的关联容器类型是ma ...
- C++中 pair 的使用方法
#include<iostream> #include<string> #include<map> using namespace std; // pair简单讲就 ...
- 两种进入容器的方法 - 每天5分钟玩转 Docker 容器技术(23)
我们经常需要进到容器里去做一些工作,比如查看日志.调试.启动其他进程等.有两种方法进入容器:attach 和 exec. docker attach 通过 docker attach 可以 attac ...
- c++ 中 pair 的 使用方法
原转载地址:点击打开链接 pair的类型: pair 是 一种模版类型.每个pair 可以存储两个值.这两种值无限制.也可以将自己写的struct的对象放进去.. pair<string,int ...
- Fragment嵌套Fragment时候。子类fragment调用父容器Fragment方法
业务场景:有的时候我们的页面可能是Activity 嵌套多个Fragment ..其中某个Fragment 又嵌套多个Fragment. 其中某个子Fragment 定义为 NewsFragmen ...
- 获取spring容器对象方法和原因
为什么要获取Spring容器对象:拿到spring容器对象后,你就可以用spring管理的bean了,拿到bean,自然可以使用bean的方法,场景:比如jsp页面.通过注解是无法注入bean的,在开 ...
随机推荐
- LeetCode--232--用栈实现队列
问题描述: 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从队列首部移除元素. peek() -- 返回队列首部的元素. empty() -- 返回队 ...
- python基础之生成器,生成器函数,列表推导式
内容梗概: 1. 生成器和生成器函数. 2. 列表推导式. 1.生成器函数1.1 生成器函数. 就是把return换成yield def gen(): print("爽歪歪") y ...
- springboot(十九)使用actuator监控应用
微服务的特点决定了功能模块的部署是分布式的,大部分功能模块都是运行在不同的机器上,彼此通过服务调用进行交互,前后台的业务流会经过很多个微服务的处理和传递,出现了异常如何快速定位是哪个环节出现了问题? ...
- leetcode-algorithms-27 Remove Element
leetcode-algorithms-27 Remove Element Given an array nums and a value val, remove all instances of t ...
- hdu-6438-贪心
Buy and Resell Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- Mono jexus
Mono:测试环境 linux系统定义:.NET在Linux上使用的开源工程 C#语言的编译器 在Linux上用C#开发程序 jexus:测试环境linux系统Linux系统下部署Jexus应用服务器 ...
- js操作字符串的常用方法
使用 substring()或者slice() 函数:split() 功能:使用一个指定的分隔符把一个字符串分割存储到数组 例子: str=”jpg|bmp|gif|ico|png”; arr=the ...
- oracle 12c新特性 FETCH FIRST、WITH TIES 关键字详解
几乎都是官方文档上的内容. [ OFFSET offset { ROW | ROWS} ] [ FETCH { FIRST | NEXT }[ { rowcount | percent PER ...
- Hadoop格式化 From hu-hadoop1/192.168.11.11 to hu-hadoop2:8485 failed on connection exception: java.net.
192.168.11.12:8485: Call From hu-hadoop1/192.168.11.11 to hu-hadoop2:8485 failed on connection excep ...
- java旅程(一) 配置环境
(一)安装java JDK 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151. ...