Gym 100989 D

Posted shuizhidao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Gym 100989 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 会超时!!!!
lower_bound()返回一个 iterator 它指向在[first,last)标记的有序序列中可以插入value,而不会破坏容器顺序的第一个位置,而这个位置标记了一个不小于value 的值 [1]  。该函数为C++ STL内的函数。
——百度百科
 1 #include <iostream>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <cstdio>
 5 #include <iomanip>
 6 #include <string>
 7 #include <set>
 8 #include <queue>
 9 #include <stack>
10 #include <map>
11 #include <algorithm>
12 
13 using namespace std;
14 
15 typedef long long LL;
16 const int INF = 0x3f3f3f3f;
17 const int MAXN = 100005;
18 const int MOD = 1e9+7;
19 
20 int main()
21 {
22     int n, Q;
23     cin >> n >> Q;
24     int desk[MAXN];
25     set<pair<int, int> > s;
26     set<pair<int, int> > :: iterator it;
27     for(int i = 1;i <= n;++i)
28     {
29         cin >> desk[i];
30         s.insert(make_pair(desk[i], i));
31     }
32     string str;
33     int t;
34     while(Q--)
35     {
36         cin >> str >> t;
37         if(str[0] == i)
38         {
39             it = s.lower_bound(make_pair(t, 0));
40             if(it == s.end())
41                 cout << -1 << endl;
42             else
43             {
44                 cout << it -> second << endl;
45                 s.erase(it);
46             }
47         }
48         else
49             s.insert(make_pair(desk[t], t));
50     }
51     return 0;
52 }

 

 

















以上是关于Gym 100989 D的主要内容,如果未能解决你的问题,请参考以下文章

D - 4 Gym - 100989D(set配pair爽到爆)

Gym - 100989J -(DFS)

Gym - 100989H (贪心)

Gym - 100989F

Gym - 100989E

Gym - 100989B