HDU 4006 The kth great number

 

Problem Description
Xiao Ming and Xiao Bao are playing a simple Numbers game. In a round Xiao Ming can choose to write down a number, or ask Xiao Bao what the kth great number is. Because the number written by Xiao Ming is too much, Xiao Bao is feeling giddy. Now, try to help Xiao Bao.
 

 

Input
There are several test cases. For each test case, the first line of input contains two positive integer n, k. Then n lines follow. If Xiao Ming choose to write down a number, there will be an " I" followed by a number that Xiao Ming will write down. If Xiao Ming choose to ask Xiao Bao, there will be a "Q", then you need to output the kth great number.
 

 

Output
The output consists of one integer representing the largest number of islands that all lie on one line.
 

 

Sample Input
8 3 I 1 I 2 I 3 Q I 5 Q I 4 Q
 

 

Sample Output
1 2 3
Hint
Xiao Ming won't ask Xiao Bao the kth great number when the number of the written number is smaller than k. (1=<k<=n<=1000000).
 

 

Source
 

 

题意:求第K大的数
昨天学术节比赛,这是最后一题,给我们大一做的题目都比这难点,唉!打击了、要奋斗了!
这题昨天用堆来做,建立K个元素的堆、再更新,维护堆,今早用STL优先队列做,STL是个好东西
 

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define N 1000003
using namespace std;
int hp[N];
void ct(int n)//堆的建立
{
    while(n>1)
        if(hp[n]<hp[n/2])
        {
            swap(hp[n],hp[n/2]);
            n=n/2;
        }
        else
            break;        
}
void wh(int k)//堆的维护
{
     int i=1,min;
     while((i*2)<=k)
     {
       if(i*2+1<=k)
       {
           if(hp[i<<1]>hp[i<<1|1])
             min=i<<1|1;
         else
             min=i<<1;
       }
       else
           min=i<<1;

         if(hp[i]>hp[min])
         {
             swap(hp[i],hp[min]);
             i=min;
         }
         else
             break;
     }

}
int main()
{
    int i,n,k,j,l,up;
    char c;
   while(scanf("%d%d",&n,&k)!=EOF)
   {   
         getchar();
         scanf("%c%d",&c,&up);
         getchar();
         hp[1]=up;l=2;
         for(i=1;i<k;i++)
         {
             scanf("%c%d",&c,&up); getchar();
             hp[l]=up;
             ct(l);l++;
         }
             for(;i<n;i++)
         {
            scanf("%c",&c);
            if(c=='Q')
            {
               printf("%d\n",hp[1]);
            }
            else
            {
              scanf("%d",&up);
              if(up>hp[1])
              {
                  hp[1]=up;
                  wh(k);
              }
            }
            getchar();
         }
  
   }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int main()
{  
    int i,n,k,ad;
    char c;
    while(scanf("%d%d",&n,&k)!=EOF)
    {   priority_queue< int,vector<int>,greater<int> > pq;
         getchar();
        for(i=0;i<k;i++)
         {
             scanf("%c%d",&c,&ad);
             pq.push(ad);getchar();
         }
        for(;i<n;i++)
        {
            scanf("%c",&c);
            if(c=='Q')
              printf("%d\n",pq.top());
            else
            {
                scanf("%d",&ad);
                if(ad>pq.top())
                   {
                       pq.pop();
                       pq.push(ad);
                   }
            }
            getchar();
        }
    }
}

你可能感兴趣的:(number)