现在的位置: 首页 > 综合 > 正文

[sicily online]1022. Poor contestant Prob

2013年09月21日 ⁄ 综合 ⁄ 共 3920字 ⁄ 字号 评论关闭

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

As everybody known, “BG meeting” is very very popular in the ACM training team of ZSU. 
After each online contest, they will go out for “smoking”. Who will be the poor ones that have to BG the others? Of course, the half who solve less problems. 
The rule runs well when the number of the contestants is even. But if the number is odd, it is impossible to divide them into two equal parts. It gives a dilemma to the BG meeting committee. After a careful discussion with Mr. Guo, a new rule emerged: if the
number of the contestant is odd, the committee will first sort the contestants according to the number of problems they solved, and then they will pick out the middle one. This poor boy or girl will have no chance to attend the BG meeting. 
Strange rule, isn`t it?
As the number of the contestants is becoming more and more large, the committee need to write a program which will pick out the poor one efficiently.
Note that: Every contestant solves different number of problems. The total number of the contestants will not exceed 10^5.

Input

There are several cases in the input. The first line of the input will be an integer M, the number of the cases.
Each case is consisted of a list of commands. There are 3 types of commands.
1. Add xxx n : add a record to the data base, where xxx is the name of the contestant, which is only consisted of at most 10 letters or digits, n is the number of problems he/she solved. (Each name will appear in Add commands only once).
2.Query :
3.End :End of the case.

Output

1.For the Query command: If the current number of contestants is odd, the program should output the poor contestant’s name currently even if there is only one contestants, otherwise, just out put “No one!” (without quotes).
2.For the End command: 
   If the total number of contestants in the data base is even, you should out put “Happy BG meeting!!”(without quotes),otherwise, you should out put the “xxx is so poor. ”(without quotes) where xxx is the name of the poor one.
3.Each case should be separated by a blank line.

Sample Input

2
Add Magicpig 100
Add Radium 600
Add Kingfkong 300
Add Dynamic 700 
Query
Add Axing 400
Query
Add Inkfish 1000
Add Carp 800
End

Add Radium 100
Add Magicpig 200
End

Sample Output

No one!
Axing
Radium is so poor.

Happy BG meeting!!

题目分析:

这个题目就是求一个数组的中间的数字,主要采取的方法有:

1,排序,然后直接取   插入时间复杂度o(lgn) 查询o(1)

2,,用map来存储,插入时间复杂度O(n) 查询O(n)

3,用stl nth_element(采用局部排序的方法)   插入时间复杂度O(1) 查询的时间复杂度o(n)

4,用两个堆来实现(一个大顶堆,一个小顶堆) 插入O(lgn) 查询时间复杂度O(1)

今天是把所有数据结构都用在这个题目上面了,先用的map,又用链表,又用nth_element ,又用priority_queue。从上面这几个数据结构分析,nth_element和堆时间复杂度较低,我觉得这个题目 查询应该比插入频繁的多,所以我是优先队列来实现(相当于堆),用一个大顶堆和一个小顶堆,小顶堆的最小元素大于大顶堆所有元素,大顶堆的元素和小顶堆元素相等个数或者大一,比较容易理解

最后,还是没过,结果是把cin和cout换成scanf和printf和scanf,直接0.3s就解决了,原来超时的程序 不用cin和cout竟然提升性能这么多,看来还是要用scanf和printf。nth_element这个方法 没用scanf和printf试,不知道还超时,大家可以试试。

#include<iostream>
#include<stdio.h>
#include<cmath>
#include<iomanip>
#include<list>
#include <map>
#include <vector>
#include <string>
#include <algorithm>
#include <sstream>
#include <stack>
#include<queue>
#include<string.h>
using namespace std;

typedef struct strInfo
{
	string name;
	int number;
}info;

struct  cmpSmall
{
	bool operator()(const info &x1,const info &x2)
	{
		return x1.number>x2.number;
	}
};
struct cmpBig
{
	bool operator()(const info &x1,const info &x2)
	{
		return x1.number<x2.number;
	}
};

int main()
{
	int n;
	scanf("%d",&n);
	for(int xx=0;xx<n;xx++)
	{
		priority_queue<info,vector<info>,cmpSmall> small;
		priority_queue<info,vector<info>,cmpBig> big;
		//string command;
		char command[11];
		while(scanf("%s",command))
		{
			if(strcmp(command,"Add")==0)
			{
				char name1[13];
				scanf("%s",name1);
				string name(name1);
				int number;
				scanf("%d",&number);
				const info tmp={name,number};
				if(big.size()==small.size())
				{
					if(big.empty()||tmp.number<small.top().number)
						big.push(tmp);
					else if(tmp.number>small.top().number)
					{
						big.push(small.top());
						small.pop();
						small.push(tmp);
					}//end else if
				}//end if
				else if(big.size()>small.size())
				{
					if(tmp.number>big.top().number)
						small.push(tmp);
					else if(tmp.number<big.top().number)
					{
						small.push(big.top());
						big.pop();
						big.push(tmp);
					}
				}//end else if
			}
			else if(strcmp(command,"Query")==0)
			{
				if(big.size()==small.size())
					printf("No one!\n");
				else 
				{
					printf("%s\n",big.top().name.c_str());
				}
			}
			else if(strcmp(command,"End")==0)
			{
				if(big.size()==small.size())
					printf("Happy BG meeting!!\n");
				else printf("%s is so poor.\n",big.top().name.c_str());
				break;
			}
		}//end while
		if(xx!=n-1)
			printf("\n");
	}//end for xx
}
【上篇】
【下篇】

抱歉!评论已关闭.