Kiki & Little Kiki 1 / HDU - 2275
作者:互联网
Kiki is considered as a smart girl in HDU, many boys fall in love with her! Now, kiki will finish her education, and leave school, what a pity! One day, zjt meets a girl, who is like kiki very much, in the campus, and calls her little kiki. Now, little kiki want to design a container, which has two kinds of operation, push operation, and pop operation.
Push M:
Push the integer M into the container.
Pop M:
Find the maximal integer, which is not bigger than M, in the container. And pop it from the container. Specially, for all pop operations, M[i] is no bigger than M[i+1].
Although she is as smart as kiki, she still can't solve this problem! zjt is so busy, he let you to help little kiki to solve the problem. Can you solve the problem?
Input
The input contains one or more data sets. At first line of each input data set is an integer N (1<= N <= 100000) indicate the number of operations. Then N lines follows, each line contains a word (“Push” or “Pop”) and an integer M. The word “Push” means a push operation, while “Pop” means a pop operation. You may assume all the numbers in the input file will be in the range of 32-bit integer.
Outout
For each pop operation, you should print the integer satisfy the condition. If there is no integer to pop, please print “No Element!”. Please print a blank line after each data set.
Sample Input
9
Push 10
Push 20
Pop 2
Pop 10
Push 5
Push 2
Pop 10
Pop 11
Pop 19
3
Push 2
Push 5
Pop 2
Sample Output
No Element!
10
5
2
No Element!
2
题意
多组测试数据,每组数据输入一个N
然后是N个操作
操作分两种
1: push x 表示插入x (多重集合)
2: pop x 表示删除<=x最大的一个整数并删除,不存在的话输出No Element!
题解
multiset 有序序列
upper_bound(key_value),返回最后一个大于等于x的定位器
代码
#include<bits/stdc++.h>
using namespace std;
int n, x;
string s;
int main(){
while(scanf("%d", &n) == 1){
multiset<int> st;
while(n--){
cin >> s; scanf("%d", &x);
if(s == "Push"){
st.insert(x);
}else{
multiset<int> :: iterator st_ = st.upper_bound(x);
if(st_ != st.begin()){
st_--;
printf("%d\n", *st_);
st.erase(st_);
}else{
printf("No Element!\n");
}
}
}
printf("\n");
}
return 0;
}
标签:2275,Little,Pop,st,Kiki,pop,integer,Push,kiki 来源: https://www.cnblogs.com/Little-Turtle--QJY/p/12496388.html