set<pair<int,int> > 的运用
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 做这题之前先来普及下 set<pair<int,int> > 的用法 注意:set<pair<int,int> >; // 最后的> > 是 >空格> 否则会报错 其次
set<pair<int,int> >在头文件<set>中,并且要加上using namespace std; <vector> 头文件 // iterator 迭代器迭代程序
set<pair<int,int> > // 是一种类,注意:定义的时候右边的两个> >要空一格。
在头文件<set>中
set<pair<int,int> > s // 声明 s s是类名,
set<pair<int,int> >::iterator it; // iterator 迭代器迭代程序
lower_bound(key_value) 返回第一个大于等于key_value的定位器
upper_bound(key_value),返回最后一个大于等于key_value的定位器
假若上面查找的没有符合条件的,那么他们的值等于 s.end()
s.erase(it); 释放掉it的值
s.insert(make_pair(a[i],i)) 插入
it=s.lower_bound(make_pair(t,0));//寻找答案
ac代码:
#include<iostream>
#include<set>
#include<vector>
using namespace std;
const int maxn=1e5+10;
int n,q,a[maxn];
set<pair<int,int> > s; //最后两个> > 是 >空格> 否则会报错
set<pair<int,int> >::iterator it; // iterator 迭代器迭代程序
int main() //set默认的比较规则先按照first比较,如果first相同,再按照second 比较。
{
cin>>n>>q;
int i,x;
char str[10];
for (i=1;i<=n;i++){
cin>>a[i];
s.insert(make_pair(a[i],i)); //插入椅子数及其桌号
}
while (q--){
cin>>str>>x;
if (str[0]=='i'){
it=s.lower_bound(make_pair(x,0)); //lower_bound(key_value) 返回第一个大于等于key_value的定位器
if (it==s.end()){ //假若上面查找的没有符合条件的,那么他们的值等于 s.end()
cout<<"-1\n";
continue;
}
else{
cout<<it->second<<endl;
s.erase(it);//释放掉了桌号,相当于标记该桌号不能再用
}
}
else{
s.insert(make_pair(a[x],x));
}
}
return 0;
}
set<pair<int,int> > 的运用的更多相关文章
- set<pair<int,int> >的用法
例题链接:https://vjudge.net/contest/236677#problem/D 题意:首先输入两个数字n,m.n表示有n张桌子,编号从1到n,m表示有m个操作, 然后接下来一行有n个 ...
- C语言复杂声明-void (*signal(int sig, void (*handler)(int)))(int);
问题提出 请分析此声明:void (*signal(int sig, void (*handler)(int)))(int); 求解过程 在对上面的例子作分析之前,我们需要了解C语言的声明优先级,&l ...
- Undefined symbols for architecture i386: "MyGetOpenALAudioData(__CFURL const*, int*, int*, int*)"
今天把apple developer上的例子程序oalTouch中的MyOpenALSupport.h和MyOpenALSupport.c添加到自己的工程中,并在另一个文件xxx.cpp里调用,结果出 ...
- 自定义函数中的参数返回值 “-> (Int -> Int)”的问题
func makeIncrementer() -> (Int -> Int) { func addOne(number: Int) -> Int { + number } retur ...
- void (*f(int, void (*)(int)))(int) 函数解析 转
今天与几个同学看到了一个函数指针定义: void (*f(int, void (*)(int)))(int) 以前在C trap pit fails里面见过,但是文章里面介绍的很详细,但是往往使初学者 ...
- 如何理解这段代码:void (*signal (int sinno,void(*func)(int)))(int)
void (*signal (int sinno,void(*func)(int)))(int) 先来看void(*func)(int) 这里的意思是声明一个函数指针func,它的参数类型为int ...
- 关于typedef int(*lpAddFun)(int, int)
lpAddFun是typedef定义的一个名称 可以用来定义变量 比如 lpAddFun p; 那 p就是 int(*p)(int, int); 首先(*p)说明p是一个指针,(*p)();说明p指向 ...
- void (*f(int, void (*)(int)))(int) 函数解析
函数指针 今天与几个同学看到了一个函数指针定义: void (*f(int, void (*)(int)))(int) 以前在C trap pit fails里面见过,但是文章里面介绍的很详细,但是往 ...
- 一道试题引发的血案 int *ptr2=(int *)((int)a+1);
某日,看到一道比较恶心的C语言的试题,考了很多比较绕的知识点,嘴脸如下: int main(void) { int a[4] = {1, 2, 3, 4}; int *ptr1=(int *)(&am ...
随机推荐
- 团队的初体验与Scrum的初识
一. 队名及宣言 队名: the better for you 宣言: Change our lives with code 二. 队员及分工 a.承担软件工程的角色 姓名 学号 角色 蒋 婷 B20 ...
- CDS view注解解析 - @Environment.systemField
下面的CDS view使用到了@Environment.systemField这个注解,定义了两个参数#SYSTEM_LANGUAGE和#USER. 这个view从CRM物料主数据的产品抬头表COMM ...
- linux下安装jdk和配置环境变量
参考博文:http://www.cnblogs.com/samcn/archive/2011/03/16/1986248.html 系统环境:linux centos 6.4_x64 软件版本:jdk ...
- 围绕react衍生出来的思考
优势一.声明式开发 首先react是声明式的开发方式,这个与之对应的是命令式开发方式,之前在用jquery写代码的时候,都是直接来操作dom,直接操作dom的这种编程方式,我们把他叫做命令式的编程,也 ...
- on-session问题
.D:\0kecheng\bos\bosv2.0_chapter03.无条件查询. 方法1.@JSON(serialize=false)是注解排除不需要加载的实体类上,找到它的get方法,解决no-s ...
- 【洛谷P1726】上白泽慧音
上白泽慧音 题目链接 强联通分量模板题,Tarjan求强联通分量,记录大小即可 #include<iostream> #include<cstring> #include< ...
- java类的初始化程序块以及被实例化时候的执行顺序
初始化块:在类实例化过程中初始化执行顺序是先执行静态初始化块,然后执行普通初始化块,最后执行构造函数,而且静态初始化只在第一次被实例化时执行且只执行一次.public class HelloWorld ...
- 用c#语言编写分解质因数
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- Oracle四舍五入,向上取整,向下取整
用oracle sql对数字进行操作: 取上取整.向下取整.保留N位小数.四舍五入.数字格式化 取整(向下取整): select floor(5.534) from dual; select trun ...
- Hibernate学习第一天
Hibernate框架第一天 今天任务 1. 使用Hibernate框架完成对客户的增删改查的操作 教学导航 1. 能够说出Hibernate的执行流程 2. 能够独立使用Hibernate框架完成增 ...