Java快速读入模板

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.*;
public class Main{
    public static int heap[];
    public static boolean exist[];
    public static int sum=1;
    public static void main(String args[])throws IOException
    {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        in.nextToken();
        int n = (int) in.nval;
        heap = new int[10000005];
        while(n-->0)
        {
            in.nextToken();
            int x = (int) in.nval;
            if(x==1)
            {
                in.nextToken();
                int xx = (int) in.nval;
                InsertHeap(xx);
            }
            if(x==2)
                out.println(heap[1]);
            if(x==3)
                MovetHeap();
        }
        out.flush();
    }
    public static void InsertHeap(int x)
    {
        heap[sum] = x;
        int p = sum;
        sum++;
        while(p/2>=1)
        {
            if(heap[p]>=heap[p/2])
                return;
            else
            {
                int temp = heap[p];
                heap[p] = heap[p/2];
                heap[p/2] = temp;
            }
            p/=2;
        }
    }
    public static void MovetHeap()
    {
        heap[1] = heap[sum-1];
        sum--;
        int p = 1;
        while(p*2<sum)
        {
            int next = p*2;
            if(p*2<sum&&heap[p*2]>heap[p*2+1]) next++;
            if(next<sum&&heap[next]<heap[p])
            {
                int temp = heap[p];
                heap[p] = heap[next];
                heap[next] = temp;
            }
            p = next;
        }
    }
}
全部评论

相关推荐

牛客410815733号:这是什么电影查看图片
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务