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
2Add Magicpig 100Add Radium 600Add Kingfkong 300Add Dynamic 700 QueryAdd Axing 400QueryAdd Inkfish 1000Add Carp 800EndAdd Radium 100Add Magicpig 200End
Sample Output
No one!AxingRadium 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 ifelse 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 whileif(xx!=n-1)printf("\n");}//end for xx}