D - 4
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).
OutputFor 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[200000];
int main()
{
int t , e , a[200000];
while(~scanf("%d%d" , &t , &e))
{
set< pair<int,int> >s;
set< pair<int,int> >::iterator it ;
for(int i = 1 ; 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 = 1 ; i <= e ; i++)
{
char str[10] ;
scanf("%s" , str);
if(strcmp(str , "in") == 0)
{
int peo ;
cin >> peo ;
it = s.lower_bound(make_pair(peo , 1)); // 依次比较first、second找到大于等该pair的第一组个最小值
if(it != s.end())
{
cout << it->second << endl ; // 输出该桌子的序号
s.erase(it);
}
else // 若无符合条件的桌子。即peo人数大于如何空桌子。返回该函数end()地址。
cout << -1 << endl ;
}
else
{
int d ;
cin >> d ;
s.insert(make_pair(a[d] , d));
}
}
}
return 0;
}
//#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[10]; int a[100009]; int main() { int n , m ; scanf("%d%d" , &n , &m); set<pair<int , int> >s; for(int i = 1 ; i <= n ; i++) { int x ; scanf("%d" , &x); a[i] = x ; s.insert(make_pair(x , i)); } for(int i = 0 ; i < m ; i++) { int y ; scanf("%s%d" , str , &y); if(str[0] == 'i') { auto it = s.lower_bound(make_pair(y , 1)); if(it != s.end()) { cout << it->second << endl ; s.erase(it); } else { cout << -1 << endl ; } } else { s.insert(make_pair(a[y] , y)); } } return 0 ; }