hdu 2275 Kiki & Little Kiki 1

Kiki & Little Kiki 1

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 221    Accepted Submission(s): 65


Problem Description
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.
 


 

Output
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
 


 

Source
 


 

Recommend
lcy
 


 

Statistic | Submit | Back
//1282691 2009-04-18 08:18:52 Accepted 2275 234MS 2412K 717 B C++ Wpl 
#include <iostream>
#include 
<string>
#include 
<set>
using namespace std;
multiset
<int>MS;
multiset
<int>::iterator p,q;
int n;
int main()
{
    
string str;
    
int a;
    
while(scanf("%d",&n)!=EOF)
    {
        MS.clear();  
//
        while(n--)
        {
            cin
>>str>>a;
            
if(str=="Push")
                MS.insert(a);
            
else
            {
                
if(a<*(MS.begin()))  //如果a小于最开始的元素肯定是没有这样的元素了
                {
                    printf(
"No Element!\n");                    
                    
continue;
                }
                p
=MS.find(a);  //如果有a这个元素直接输出就好了
                if(p!=MS.end())
                {
                    printf(
"%d\n",*p);
                    MS.erase(p);
                }
                
else
                {
                    MS.insert(a);
                    p
=q=MS.find(a);  //通过这样可以找到不大于a的最大的一个数
                    p--;
                    printf(
"%d\n",*p);
                    MS.erase(p);  
//删除一定要指针
                    MS.erase(q);  //删除指针
                }
            }
        }
        printf(
"\n");
    }
    
return 0;
}
原文地址:https://www.cnblogs.com/forever4444/p/1486012.html