hdu 4006 The kth great number

Java  写了一个优先队列   不知道咋的  速度没有快多少;

import java.util.Random;
import java.util.Scanner;
import java.util.PriorityQueue;
import java.util.Queue;
import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.PrintStream;
import java.io.FileOutputStream;

class Tree{
    public Tree ltTree = null; 
    public Tree rtTree = null;
    public int value;
    public Tree( int value ){
        this.value = value; 
    }
}

class Priority{
    public Priority( ){ total = 0; }
    public Tree treeNode[] = new Tree[2200000];
    
    public int total;
    public void clear(){  total = 0;
        for( int i = 0; i < 2200000; i++ )
        treeNode[i] = null; 
    }
    public int size(){ return total; }
    public int top(){  return treeNode[1].value;  }
    
    public Tree creat( int value,int postion ) {
        return  treeNode[postion] = new Tree( value );
    }
    public void swap( Tree a,Tree b ){
        int c = a.value; a.value = b.value; b.value = c;
    }
    public void heapIncreaseKey( int i ){
        if( i == 1 )return;
        if( treeNode[i].value < treeNode[i>>1].value ){
            swap( treeNode[i],treeNode[i>>1] );
            heapIncreaseKey(i>>1);
        }
    }
    public void heapExtractMax( int i ){
        Tree Max = treeNode[i]; boolean fell = false;
        if( treeNode[i<<1] != null && treeNode[i<<1].value < Max.value ){
            Max = treeNode[i<<1]; fell = true;
        }
        if( treeNode[i<<1|1] != null && treeNode[i<<1|1].value < Max.value ){
            swap( treeNode[i<<1|1],treeNode[i] );
            heapExtractMax( i<<1|1 );
            return;
        }
        if( fell ){
            swap( treeNode[i<<1],treeNode[i] );
            heapExtractMax( i<<1 );
        }
    }
    public void push( int value ){
       if( total == 0 ){
            creat(value,++total);
       }else{
           Tree nowTree = creat(value,++total);
           if( (total&1) == 1 ) 
                treeNode[total>>1].rtTree = nowTree;
           else treeNode[total>>1].ltTree = nowTree;
           heapIncreaseKey( total );
       }
    }
    public void pop( ){
          if( total == 0 ){ return; }
          if( total == 1 ){ --total; return; }
        swap( treeNode[1],treeNode[total] ); 
        treeNode[total] = null;
        if( (total&1) == 1 ) 
              treeNode[total>>1].rtTree = null; 
        else  treeNode[total>>1].ltTree = null; 
        --total; heapExtractMax( 1 ); 
    }
}
public class PriorityQueueClass 
{
    public static void main( String str[] )throws Exception{
       /*
       Random rad = new Random();
       File file = new File("D:"+File.separator+"in.txt");  
       if( !file.exists() )file.createNewFile();
       System.setOut(new PrintStream(new FileOutputStream( file )) );
      
       int num = rad.nextInt(100)+1000; System.out.println(num + " "+rad.nextInt(10000)+2);
       while( num != 0 ){
           int temp = rad.nextInt(2);
           if( temp == 0 ){
               System.out.println("I"+" "+rad.nextInt(1000000));
           }else if( temp == 1 ){
               System.out.println("Q");
           }
           num--;
       } 
       */
       /*
       File file1 = new File("D:"+File.separator+"in.txt");
       File file2 = new File("D:"+File.separator+"out.txt");
       if( !file2.exists() )file2.createNewFile();
       System.setOut(new PrintStream(new FileOutputStream(file2)) );
       Scanner cin = new Scanner(file1);
       
       int N = cin.nextInt(),M;
       while( N != 0 )
       {
           int fell = cin.nextInt();
           if( fell == 0 ){
               int temp = cin.nextInt();
               que.push( temp );
               que2.add( (Integer)temp );
           }else if( fell == 1 ){
               if( que.size() != 0 ){
                   int temp1 = que.top();
                   int temp2 = que2.peek();
                   System.out.print(temp1+" ");
                   System.out.println(temp2);
               }
           }else{
               if( que.size() != 0 ){
                   que.pop();
                   que2.poll();
               }
           }
           N--;
       }
       */
       //File file2 = new File("D:"+File.separator+"out2.txt");
       //System.setOut(new PrintStream(new FileOutputStream(file2)) );
        
       Scanner cin = new Scanner(System.in); int N,M;
       while( cin.hasNext() ){
           String cha = new String();  
           Priority que = new Priority();
           N = cin.nextInt(); M = cin.nextInt();
           for( int i = 0; i < N; i++ ){
               cha = cin.next();
               if( cha.charAt(0) == 'I' ){
                  int temp = cin.nextInt();
                      que.push(temp);
                      if( que.size() > M )que.pop();
                }
                else System.out.println(que.top());
           }
       }
    }
}

/*
10 14 9 2 9 15 18 12 15 17 

10 14 9 2 9 15 18 12 15 17 
2 9 10 12 9 15 18 14 15 17 

*/
View Code

 

你可能感兴趣的:(hdu 4006 The kth great number)