D. 1D Cafeteria (B)
time limit per test
1.0 s
memory limit per test
256 MB
input
standard input
output
standard output
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
Copy
4 7 1 2 6 7 in 4 in 1 in 3 in 5 out 1 out 4 in 7
output
Copy
3 1 4 -1 4
题意:输入n,m,表示 n 张桌子,m个操作,后面接第 i 张桌子能坐下的数量。然后输入操作,in 表示进来,后面接人数
输入in的时候要输出这波人能坐下的桌子编号中最小的,当没有桌子能坐下这么多人数的时候输出-1,
out 表示出去多少人,人数和之前 in 的人数会有相同,那么之前坐的桌子就没人坐,后面的人可以接着做
思路:可以用STL中的set来做,set可以自动排序,当人来的时候我们可以删除桌子信息,当走掉人之后我们可以重新输入数据到set中,并且不需要自己再次排序
坑点:无
AC代码:
#include <iostream> #include <cstring> #include <set> #include <cstdio> #include <algorithm> using namespace std; const int maxn=100100; set<pair<int, int> >s; set<pair<int, int> >::iterator it; int tab[maxn]; int main() { char t[3]; int n,m,i,j,q; cin>>n>>q; for (i=1;i<=n;i++) { cin>>tab[i]; s.insert(make_pair(tab[i],i)); //输入桌子能坐下的数量和编号 } while (q--) { cin>>t>>m; if (t[0]=='i') { it=s.lower_bound(make_pair(m,0)); //找到能坐下m个人的桌子的最小编号 if (it==s.end()) //如果找不到,输出-1 cout<<"-1"<<endl; else { cout<<it->second<<endl; //输出桌子编号 s.erase(it); //删除桌子的信息(有人坐了) } } else s.insert(make_pair(tab[m],m)); //如果人走了,桌子没人坐,信息重新输入 } return 0; }