LeeBlog

导航

hdu 1509

#include<stdio.h>
#include<string.h>
struct E
{
       char name[1000];
       char less[1000];
       int d;
       int num; 
}e[60024];
int c = 0,max = 0,count = 0;
void swap( int x,int y )
{
     struct E t;
     t = e[ x ];
     e[ x ] = e[ y ];
     e[ y ] = t;
 }
int cmp( int x,int y )
{
    if( e[ x ].d == e[ y ].d )
        if( e[x].num < e[y].num )
            return 1;
        else
            return 0;
    else
        return e[x].d < e[y].d ? 1 : 0;
}
void down( int i )
{
     int p = 2 * i;
     if( p <= c )
     {
         if( p + 1 <= c && cmp( p + 1,p ) )
             ++p;
         if( cmp( p,i ) )
         {
             swap( i,p );
             down( p );
             }
     }
 }
void pop(  )
{
     printf( "%s %s\n",e[1].name,e[1].less );
     swap( 1,c-- );
     down( 1 );
 }
void up( int i )
{
     if( i > 1  )
     {
         if( cmp( i, i / 2 ) )
         {
                 swap( i,i/2 );
                 up( i / 2 );
                 }
         
     }
     else
         return ;
 }
void insert(  )
{
     ++c;
     ++count;
     scanf( "%s%s%d",e[c].name,e[c].less,&e[c].d );
     e[c].num = count;
     int num = 0;
     up( c );
 }

char str[100] = { 0 },get[ ] = "GET",put[ ] = "PUT";
int main( )
{
    while(  scanf( "%s",str )!= EOF )
    {
           if( !strcmp( str,get ) )
           {
               if( !c )
                   printf( "EMPTY QUEUE!\n" );
               else
               {
                   pop( );
               } 
           }
           else
           {
               insert( );
           }
           }
    return 0;
}

posted on 2011-03-01 17:23  LeeBlog  阅读(374)  评论(0编辑  收藏  举报