【HIHOCODER 1105】题外话·堆

时间:2023-03-09 16:35:00
【HIHOCODER 1105】题外话·堆

描述


小Ho有一个糖果盒子,每过一段时间小Ho都会将新买来的糖果放进去,同时他也会不断的从其中挑选出最大的糖果出来吃掉,但是寻找最大的糖果不是一件非常简单的事情,所以小Ho希望能够用计算机来他帮忙计算这个问题!

提示:吃糖果吃多了会变胖的!

输入


每个测试点(输入文件)有且仅有一组测试数据。

在一组测试数据中:

第1行为1个整数N,表示需要处理的事件数目。

接下来的M行,每行描述一个事件,且事件类型由该行的第一个字符表示,如果为'A',表示小Ho将一粒糖果放进了盒子,且接下来为一个整数W,表示这颗糖果的重量;如果为'T',表示小Ho需要知道当前盒子中最重的糖果的重量是多少,在知道这个值之后,小Ho会将这颗糖果从盒子中取出并吃掉。

对于100%的数据,满足1<=N<=10^5, 1<=w<=10^5。<>

对于100%的数据,满足没有2颗糖果的重量是相同的,最开始的时候小Ho的糖果盒子是空的,且每次小Ho想要取出一颗糖果的时候盒子里一定至少有一颗糖果。

输出


在一组测试数据中:

对于每个类型为'T'的时间,输出1个整数W_MAX,表示在这一时刻,盒子中最重的糖果的重量。

样例输入

5
A 77751
A 1329
A 26239
A 80317
T

样例输出

80317

数组为堆的空间,1为根节点,他的左右孩子为rt<<1 , rt<<1|1

import java.io.*;
import java.util.*; public class Main {
static final int N=(int)1e5+10;
static int h[]=new int[N],size=0;
static void push(int x){
int p=++size,f;
while(h[f=p>>1]<x) {
h[p]=h[f];p=f;
}
h[p]=x;
}
static int pop(){
int ans=h[1];
int p,x,c;
x=h[size--];p=1;
while((c=p<<1)<=size){
if(c<size&&h[c+1]>h[c]) c++;
if(x>h[c]) break;
else h[p]=h[c];
p=c;
}
h[p]=x;return ans;
}
static InputStream sys=System.in;
static InputReader in=new InputReader(sys);
static PrintWriter out=new PrintWriter(System.out);
public static void main(String[] args) {
int n;
char op;
h[0]=0x3f3f3f3f;
n=in.nextInt();
while(n--!=0){
op=in.next().charAt(0);
if(op=='A') push(in.nextInt());
else out.println(pop());
}
out.flush();
} static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer; public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
} public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
} public int nextInt() {
return Integer.parseInt(next());
}
}
}