D - 4

Gym - 100989D

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.

这个题的题解似乎只能是set配pair 。。 我尝试了一个下午的其他方法都以失败告终。。

set特点:有序,无重。。

pair特点:将两个值捆绑起来。

#include <iostream>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <set>
using namespace std;
int vis[];
int main()
{ int t , e , a[];
while(~scanf("%d%d" , &t , &e))
{
set< pair<int,int> >s;
set< pair<int,int> >::iterator it ;
for(int i = ; i <= t ; i++)
{
cin >> a[i] ;
s.insert(make_pair(a[i] , i)); }
/* for(it = s.begin() ; it != s.end() ; it++)
{
cout << it -> first << " " << it -> second <<endl ;
}
cout << s.lower_bound(make_pair(2 , 2))->first << " " << s.lower_bound(make_pair(2 , 5))->second<< endl ;*/ for(int i = ; i <= e ; i++)
{
char str[] ;
scanf("%s" , str);
if(strcmp(str , "in") == )
{
int peo ;
cin >> peo ;
it = s.lower_bound(make_pair(peo , )); // 依次比较first、second找到大于等该pair的第一组个最小值
if(it != s.end())
{ cout << it->second << endl ; // 输出该桌子的序号
s.erase(it);
}
else // 若无符合条件的桌子。即peo人数大于如何空桌子。返回该函数end()地址。
cout << - << endl ;
}
else
{
int d ;
cin >> d ;
s.insert(make_pair(a[d] , d));
}
}
} return ;
}
//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdio.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std;
typedef long long ll ;
char str[];
int a[]; int main()
{
int n , m ;
scanf("%d%d" , &n , &m);
set<pair<int , int> >s;
for(int i = ; i <= n ; i++)
{
int x ;
scanf("%d" , &x);
a[i] = x ;
s.insert(make_pair(x , i));
}
for(int i = ; i < m ; i++)
{
int y ;
scanf("%s%d" , str , &y);
if(str[] == 'i')
{
auto it = s.lower_bound(make_pair(y , ));
if(it != s.end())
{
cout << it->second << endl ;
s.erase(it);
}
else
{
cout << - << endl ;
}
}
else
{
s.insert(make_pair(a[y] , y));
}
} return ;
}

stl(set和pair)的更多相关文章

  1. C++标准模板库(STL)之Pair

    1.Pair的常用用法 pair:两个元素绑在一起作为一个合成元素.可以看成是两个元素的结构体. struct pair { typeName1 first; typeName2 second; }; ...

  2. c++11 stl 学习之 pair

    pair以模板的方式存储两个数据 namespace std {template <typename T1, typename T2>struct pair {// memberT1 fi ...

  3. STL模板整理 pair

    pair pair是一种模板类型,其中包含两个数据值,两个数据的类型可以不同.如果一个函数有两个返回值的话,如果是相同类型,就可以用数组返回,如果是不同类型,就可以自己写个struct ,但为了方便就 ...

  4. 4.1 pair类模板

    在学习关联容器之前,首先先要了解一下STL中的pair类模板,因为关联容器的一些成员函数返回值都是pair对象,而且map 和multimap中的元素都是pair对象. 1)pair类模板定义 pai ...

  5. STL中map与hash_map容器的选择收藏

    这篇文章来自我今天碰到的一个问题,一个朋友问我使用map和hash_map的效率问题,虽然我也了解一些,但是我不敢直接告诉朋友,因为我怕我说错了,通过我查询一些帖子,我这里做一个总结!内容分别来自al ...

  6. pair queue____多源图广搜

    .简介 class pair ,中文译为对组,可以将两个值视为一个单元.对于map和multimap,就是用pairs来管理value/key的成对元素.任何函数需要回传两个值,也需要pair. 该函 ...

  7. HDU 5033 (单调栈维护凸包) Building

    题意: 一个人在x轴上,他的左右两侧都有高楼,给出楼的横坐标Xi和高度Hi还有人的位置pos,求人所能看到的天空的最大角度. 分析: 将建筑物和人的位置从左到右排序,对于每个位置利用栈求一次人左边建筑 ...

  8. Book 最短路算法

    用HDU2544整理一下最近学的最短路算法 1.Dijkstra算法 原理:集合S表示已经找到最短路径的点,d[]表示当前各点到源点的距离 初始时,集合里面只有源点,当每个点u进入集合S时,用d[u] ...

  9. Boost 1.61.0 Library Documentation

    http://www.boost.org/doc/libs/1_61_0/ Boost 1.61.0 Library Documentation Accumulators Framework for ...

  10. C++ Boost库简介(转载)

    boost是一个准标准库,相当于STL的延续和扩充,它的设计理念和STL比较接近,都是利用泛型让复用达到最大化.不过对比STL,boost更加实用.STL集中在算法部分,而boost包含了不少工具类, ...

随机推荐

  1. vue v-model 的使用

    Vue的核心特性之一是双向绑定,vue的响应式原理是实现了数据->视图 v-bind只能实现数据的单向绑定 ,从M自动绑定到V 使用v-model 可以实现表单元素的双向绑定,且只能应用到表单元 ...

  2. HTML文档流和脱离文档流

    文档流:也就是我们通常看到的由左到右.由上而下的元素排列形式,在网页中每个元素都是默认按照这个顺序进行排序和显示的. 脱离文档流:元素脱离文档流之后,将不再在文档流中占据空间,而是处于浮动状态(可以理 ...

  3. Matlab 多个版本的安装包下载、安装和激活教程 + 多套数学建模视频教程

    目录 1. 关键词 1.1. 说明 2. 下载地址 2.1. OneDrive高速云盘 2.1.1. 多版本的安装包 2.1.2. 多套数学建模的视频教程 2.2. 百度云 3. 安装教程 1. 关键 ...

  4. linux NFS 的安装准备

    关闭 iptables 和 selinux [root@allentuns ~]# service iptables stop [root@allentuns ~]# chkconfig iptabl ...

  5. 【改】utf-8 的去掉BOM的方法

    最近在测试中发现,linux系统中导出的文件,有记事本打开另存为或者保存后,再次导入进linux系统,发现失败了,对比文件内容,没发现区别,打开二进制文件对比发现,文件头部多了三个字符:EF BB B ...

  6. phpStorm 配置PHP_CodeSniffer自动检查代码

    环境 ubuntu18.4 phpstorm php7.2 最正确安装方法 sudo apt-get install php-codesniffer 一.composer安装PHP_CodeSniff ...

  7. ORM和JDBC

    为什么要用 ORM? 和 JDBC有何不一样? ORM(Object-Relational Mapping),对象关系映射,是一种思想,就是把Object转变成数据库中的记录,或者把数据库中的记录转变 ...

  8. Django组件---Django请求生命周期和中间件

    Django组件---Django请求生命周期和中间件 Django请求生命周期 说明: client代表浏览器,浏览器的内部为我们封装了socket,Django的WSGI模块也为我们封装了sock ...

  9. 谷歌浏览器 安装 Vue.js devtools 工具

    如果是vue写的项目,谷歌浏览器右上角的vue小图标会亮起!下面介绍如何安装 1.谷歌浏览器插件商店可以点击安装[需要梯子] 2.vue官网有对应的安装包,需要自己手动 npm run build 一 ...

  10. python绘制图的度分布柱状图, draw graph degree histogram with Python

    图的度数分布 import collections import matplotlib.pyplot as plt import networkx as nx G = nx.gnp_random_gr ...