SOJ 1022. Poor contestant Prob
2013-08-26 20:34
344 查看
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对三个参数的解释
TType of the elements.
Aliased as member type priority_queue::value_type.
ContainerType 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.
CompareA 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
trueif 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; }
相关文章推荐
- SOJ 3254 Windy和水星-水星交通
- soj 2749 The Fewest Coins
- soj 3137 Simple Computing 容斥原理 hdu 1796 How many integers can you find
- SOJ 4150 合唱队形
- SOJ Binary Numbers dp + 逼近思想
- soj 1283. Comparison
- soj 1664. Su-Su-Sudoku
- soj 1893. Sell Ticket
- soj 2382. Flowers Flourish from France
- Sicily 1033 City Road (SOJ 1033)【dp动态规划】
- Sicily 1305 Who’s Winner? (SOJ 1305) 【Nim问题】
- soj1090.Highways
- soj dsa extra 1002 sudoku
- soj 1698 Hungry Cow_三角函数
- 【异或】SOJ Find Differences
- soj题目分类
- soj 4393模拟
- 【贪心】SOJ 13983
- SOJ.Parity
- soj 2930 积木城堡(恰好装满的01背包 + 枚举)