Sicily.1022. Poor contestant Prob(字符串对应数字排序,建立大小根堆)

来源:互联网 发布:淘宝联盟 淘客助手 编辑:程序博客网 时间:2024/06/05 08:55
/*1022. Poor contestant Prob(字符串对应数字排序,建立大小根堆)*/#include <iostream>#include <stdlib.h>#include <cstring>#include <stdio.h>#include <algorithm>using namespace std;#define MAXCONTEST 100000struct People{       char name[12];       int number;} MinHeap[MAXCONTEST], MaxHeap[MAXCONTEST];int MinLen, MaxLen;       //大根堆比较函数 bool cmpLess(People a, People b){     return a.number < b.number;}       //小根堆比较函数 bool cmpGreater(People a, People b){     return a.number > b.number;}   void insertHeap(People p, int form){     if(form == 0){   //插入到大根堆中         MaxHeap[MaxLen] = p;    //放到堆后面         MaxLen++;        push_heap(MaxHeap, MaxHeap+MaxLen, cmpLess);  //重新调整堆      }     else {           //插入到小根堆中         MinHeap[MinLen] = p;    //放到堆后面         MinLen++;        push_heap(MinHeap, MinHeap+MinLen, cmpGreater);  //重新调整堆        }}void popHeap(int form){     if(form == 0){        pop_heap(MaxHeap,MaxHeap+MaxLen,cmpLess);            MaxLen--;          }          else {        pop_heap(MinHeap,MinHeap+MinLen,cmpGreater);            MinLen--;         }}int main(){    //freopen("in.txt", "r", stdin);    //freopen("out.txt", "w", stdout);    int testNum;    cin >> testNum;    char command[10];    for(int i=1; i<=testNum; i++){      if(i > 1)        printf("\n");      MinLen = 0;      MaxLen = 0;      while(scanf("%s", &command)){         if(strcmp(command ,"Add" ) == 0) {             People pp;             //cin >> pp.name>> pp.number;             scanf("%s %d", &pp.name, &pp.number);             if(MinLen == MaxLen){   //如果堆长度相等,则插入到小根堆中                 if(MinLen == 0)                  insertHeap(pp, 1);                else {                   if(pp.number > MaxHeap[0].number){   //当插入数值比大根堆顶节点大时,直接插入小根堆                       insertHeap(pp,1);                            }                     else {                                             //比大根堆顶节点小,则先把大根堆的顶插入到小根堆中                       insertHeap(MaxHeap[0],1);                      //然后把大根堆的顶节点弹出                       popHeap(0);                      //再把新节点插入大根堆                       insertHeap(pp,0);                     }                }             }             else{   //如果两个堆长度不同,则说明大根堆比小根堆的数量小,插入大根堆中                 if(pp.number < MinHeap[0].number)   //如果插入值比小根堆顶节点小,则直接插入大根堆                     insertHeap(pp,0);                else {                     //如果比小根堆顶节点大,则先把小根堆的顶插入到大根堆中                    insertHeap(MinHeap[0],0);                   //然后弹出小根堆顶点                    popHeap(1);                   //再把新节点插入小根堆                    insertHeap(pp, 1);                        }               }          }                 if(strcmp(command ,"Query" ) == 0){             if(MinLen == MaxLen)               printf("No one!\n");             else {                 printf("%s\n", MinHeap[0].name);                }          }                    if(strcmp(command ,"End" ) == 0) {             if(MinLen == MaxLen)               printf("Happy BG meeting!!\n");             else {               printf("%s is so poor.\n", MinHeap[0].name);                }             break;                  }             }                }    //system("pause");    return 0;}
/*
//方法二:用优先队列
#include <iostream>#include <stdlib.h>#include <memory.h>#include <string>#include <queue>#include <stdio.h>using namespace std;struct Node{   char name[20];   int score;       };struct maxCmp{   bool operator ()(const Node &a, const Node &b)   {       return a.score < b.score;        }       };struct minCmp{   bool operator ()(const Node &a, const Node &b)   {       return a.score > b.score;        }       };int main(){    int testNum;    cin >> testNum;    char command[10];    char name[20];    int score;    bool ok = false;         while(testNum--)    {        if(ok)          cout << endl;        ok = true;        priority_queue<Node, vector<Node>, maxCmp> maxQueue;        priority_queue<Node, vector<Node>, minCmp> minQueue;                while(scanf("%s", &command))        {           if(strcmp(command, "Add") == 0)           {                     Node newNode;               scanf("%s %d", &newNode.name, &newNode.score);                               if(maxQueue.size() == minQueue.size())               {                  if(minQueue.empty())                    minQueue.push(newNode);                  else       //当两个堆数目同时,插入小根堆                   {                     Node temp = maxQueue.top();                     if(newNode.score > temp.score ) //交换                     {                         minQueue.push(newNode);                     }                      else                     {                          minQueue.push(temp);                          maxQueue.pop();                         maxQueue.push(newNode);                     }                  }               }                else                 {                      Node temp = minQueue.top();                       if(newNode.score < temp.score ) //交换                           maxQueue.push(newNode);                     else                     {                          maxQueue.push(temp);                          minQueue.pop();                          minQueue.push(newNode);                     }                }                }               if(strcmp(command, "Query") == 0)           {                if(maxQueue.size() == minQueue.size())                   printf("No one!\n");                  else                    printf("%s\n", minQueue.top().name);              }               if(strcmp(command ,"End" ) == 0)           {               if(maxQueue.size() == minQueue.size())                  printf("Happy BG meeting!!\n");                else                  printf("%s is so poor.\n", minQueue.top().name);                  break;           }           }                    }    system("pause");    return 0;}
*/