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

/*1022. Poor contestant Prob(字符串对应数字排序,建立大小根堆)
*/
#include <iostream>
#include <stdlib.h>
#include <cstring>
#include <stdio.h>
#include <algorithm>

using namespace std;
#define MAXCONTEST 100000
struct 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;
}
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值