HDU2275--Kiki & Little Kiki 1

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
 
/*
弄两个优先队列
一个从大到小,一个从小到大
我们只需要每次看推入的这个和上一个输出的谁大?如果比上个输出的小,推入1队列。否则推入2队列。
每次要删除元素的时候,就看下第二个队列(从小到大的),比要删的小的或等的全都删了推入1队列。
然后在1队列把首元素拉出来就是了。
*/
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
#define inf 0x3f3f3f3f
struct Item1
{
	int key;
	bool operator < (const Item1 & a) const
	{
		return key < a.key;//大的优先级大
	}
};
struct Item2
{
	int key;
	bool operator < (const Item2 & a) const
	{
		return key > a.key;//小的优先级大
	}
};
int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		priority_queue <Item1> q1;//大的先出列
		priority_queue <Item2> q2;//小的先出列
		char str[5];int num;
		int lastpop=-inf;
		for(int i=1;i<=n;i++)
		{
			scanf("%s%d",str,&num);
			if(str[1]=='u')//要推,首先看这个比上一个退的大还是小
			//如果比上个退的大,推入2.小,推入1
			{
				if(num>lastpop)
				{
					Item2 item;
					item.key=num;
					q2.push(item);
				}
				else
				{
					Item1 item;
					item.key=num;
					q1.push(item);
				}
			}
			else//就是要删除元素了
			{
				while(!q2.empty())
				{
					if(q2.top().key<=num)
					{
						Item1 item;
						item.key=q2.top().key;
						q1.push(item);
						q2.pop();
					}
					else break;
				}
				if(!q1.empty())
				{
					printf("%d\n",q1.top().key);
					q1.pop();
				}
				else printf("No Element!\n");
			}
		}
		printf("\n");
	}
	return 0;
}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值