D. 1D Cafeteria (B) (STL-set-set<pair<int,int> > ) (2016 PSUT Coding Marathon )
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
4 7
1 2 6 7
in 4
in 1
in 3
in 5
out 1
out 4
in 7
3
1
4
-1
4
#include<iostream> #include<cstring> #include<set> using namespace std; set< pair<int,int> >sp; set< pair<int,int> >::iterator it; int n,m,k,num[100005]; string str; int main() { while(cin>>n>>m) { for(int i=1; i<=n; i++) { cin>>num[i]; sp.insert(make_pair(num[i],i));//插入 } for(int i=1; i<=m; i++) { cin>>str>>k; if(str[0]=='i') { it=sp.lower_bound(make_pair(k,0));//找最小的 if(it==sp.end()) cout<<-1<<endl; else { cout<<it->second<<endl; sp.erase(it);//删除 } } else if(str[0]=='o') { sp.insert(make_pair(num[k],k)); } } } return 0; }
#define oi(x) cout<<x<<endl; #define en(xx) xx.begin(),xx.end()
set<int>st; cin>>n; set<int>::iterator it=st.begin(); st.insert( n ); oi( *it ); oi( *st.lower_bound (x) );//大于(或等于)某值的第一个元素的迭代器 oi( *st.upper_bound (x) );//大于某个值元素的迭代器 oi( *st.find (x) ); oi( st.size () ); oi( st.max_size() ); oi( st.count(x) ); st.clear(); st.erase(iterator) //删除定位器iterator指向的值 st.erase(first,second)//删除定位器first和second之间的值 st.erase(key_value) //删除键值key_value的值 }
#define en(xx) xx.begin(),xx.end() int main() { vector<int>v; vector<int>::iterator it; v.push_back( 0 ); it=lower_bound(en(v),5 ); //返回指针 int pos=lower_bound(v.begin(),v.end(),3)-v.begin(); //返回位置 cout<<*it<<endl; set<int>st; set<int>::iterator it; st.insert( 0 ); it=st.lower_bound( 0 ); } set< pair<int,int> >sp; set对pair排序时先按第一个由小到大排,第一个相同,才按第二个由小到大 upper_bound来说,返回的是被查序列中第一个大于查找值的指针 lower_bound则是返回的是被查序列中第一个大于等于查找值的指针