Gym - 100989D-Cafeteria (B)

Posted bendandedaima

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Gym - 100989D-Cafeteria (B)相关的知识,希望对你有一定的参考价值。

Gym - 100989D-Cafeteria (B)
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 1to 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
题意:给你N个桌子,Q组操作(情况),操作1:in+k,表示有k个人进来。操作2:out+w,表示第w号桌子的人走了。
要求:前提(优先)要求:k个人进来要给他安排在椅子数量最接近k,也就是安排到的桌子要椅子最少但又满足坐得下的情况,
(其次)要求:在上述(优先要求)下,如果存在在桌子的椅子数量(优先数量)一样的话那么选最靠近窗口的桌子。
举个栗子吧:例如总共有3张桌子,椅子数量分别是6 5 5,现在进行操作in 5,我们可以看到最少满足但又坐的下的情况是第2张桌子和第3张桌子,但是第二张桌子更靠近窗口,所以应该输出(安排在)第2张桌子。
然后这题是要用set,也就是STL里面的容器来写:
lower_bound():返回一个 iterator 它指向在[first,last)标记的有序序列中可以插入value,而不会破坏容器顺序的第一个位置,而这个位置标记了一个不小于value 的值。
 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<set>
 4 using namespace std;
 5 int a[100010];
 6 int main()
 7 {
 8     int N,Q,num;
 9     set<pair<int ,int> >s;//s容器 ,里面存的都是没有"被占用"【第i个桌子的椅子数量,第i个桌子】 
10     set<pair<int ,int> >::iterator it;
11     while(scanf("%d%d",&N,&Q)!=EOF)
12     {
13         for(int i=1;i<=N;i++)
14         {
15             cin>>a[i];
16             s.insert(make_pair(a[i],i)); //插入容器 
17         }
18         while(Q--)
19         {
20             char choose[10];
21             cin>>choose;
22             if(choose[0]==i)
23             {
24                 int number;
25                 cin>>number;
26                 it=s.lower_bound(make_pair(number,0));
27                 if(it==s.end())//表明没有符合要求的 
28                 {
29                     cout<<-1<<endl;
30                 }
31                 else
32                 {
33                     cout<<it->second<<endl;//输出符合要求的 
34                     s.erase(it);// 把这个符合要求的桌子从容器中删除 
35                 }
36             }
37             else
38             {
39                 int number;
40                 scanf("%d",&number);
41                 s.insert(make_pair(a[number],number));//第number个桌子的人走了,所以空出来了,所以要插入到容器中。 
42             }
43         }
44     }
45     return 0;
46 }

 

 



















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

Gym - 100989H (贪心)

Gym - 100989F

Gym - 100989E

Gym - 100989B

Gym - 100989G (二分法)

Gym 100989 D