SOJ 1022. Poor contestant Prob

1022. Poor contestant Prob

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!!

Problem Source

ZSUACM Team Member

// Problem#: 1022
// Submission#: 2193706
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include<iostream>
#include<queue>
#include<string.h>  // for strcmp
#include<stdio.h>
using namespace std;
class Person
{
public:
    char name[11];
    int count;
};
class less_than
{
public: 
    bool operator()(const Person& a,const Person& b)
    {
        return a.count<b.count;
    }

};
class greater_than
{
public: 
    bool operator()( const Person &a,const Person & b)
    {
        return a.count > b.count;
    }

};
priority_queue<Person,vector<Person>, less_than> queue1;
priority_queue<Person,vector<Person>, greater_than> queue2;
int main(int argc, char const *argv[])
{
    int casen;
    char cmd[6];
    cin>>casen;
    while(casen--)
    {
        while(!queue1.empty())
            queue1.pop();
        while(!queue2.empty())
            queue2.pop();
        while(1)    //优先队列,不是最大最小堆啊,老师的题解真坑爹
        {
            scanf("%s", cmd);
            if(strcmp(cmd,"Add")==0)
            {
                Person per;
                scanf("%s%d",&per.name,&per.count);
                if(queue1.empty()||per.count<queue1.top().count)
                {
                    queue1.push(per);
                }
                else 
                {
                    queue2.push(per);
                }
                if(queue1.size()>queue2.size()+1)
                {
                    queue2.push(queue1.top());
                    queue1.pop();
                }
                else if(queue2.size()>queue1.size()+1)
                {
                    queue1.push(queue2.top());
                    queue2.pop();
                }

            }
            else if(strcmp(cmd,"Query")==0)
            {
                if((queue1.size()+queue2.size())%2==0)
                      printf("No one!\n");
                else if(queue1.size()>queue2.size())
                      printf("%s\n", queue1.top().name);
                else
                     printf("%s\n", queue2.top().name);
            }
            else 
            {
                  if ((queue1.size()+queue2.size())%2==0)
                {
                    printf("Happy BG meeting!!\n");
                }
                else
                {
                    if (queue1.size() > queue2.size())
                        printf("%s is so poor.\n", queue1.top().name);
                    else
                        printf("%s is so poor.\n", queue2.top().name);
                }
                break;
            }
        }
         if (casen>=1) printf("\n");
    }
    return 0;
}                                 


这道题主要是自己定义了两个优先队列,用到了的priority_queue<person, vector <person >  , greater_than > 中greatror_than 是队列的三个参数模板之一,如下是cplusplus对三个参数的解释

T
Type of the elements.
Aliased as member type priority_queue::value_type.
Container
Type of the internal underlying container object where the elements are stored.
Its value_type shall be T.
Aliased as member type priority_queue::container_type.
Compare
A binary predicate that takes two elements (of type T) as arguments and returns a bool.
The expression comp(a,b), where comp is an object of this type and a and b are elements in the container, shall return true if a is considered to go before b in the strict weak ordering the function defines.
The priority_queue uses this function to maintain the elements sorted in a way that preserves heap properties(i.e., that the element popped is the last according to this strict weak ordering).

This can be a function pointer or a function object, and defaults to less<T>, which returns the same as applying the less-than operator (a<b).可以是一个函数指针或者是对象,缺省的是less<T>,小于号重载是一样的。重载< 号等

自己写的无聊的代码

#include<iostream>
#include<queue>
using namespace std;
class Person
{
public:
	char name[11];
	int countt;
	friend  bool operator < (Person a,Person b)
	{
      return a.countt < b.countt;
	}
	// bool operator > (Person a)
	// {
 //       return this.count > a.count;
	// }
};

priority_queue<Person,vector<Person> > que;
int main(int argc, char const *argv[])
{
	while(1)
	{
	Person temp;
	cin>>temp.name>>temp.countt;
	que.push(temp);
	if(que.size()==3)
		break;
   }

	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值