Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as Java by registered user SKYDOS ( 17 years ago )
import java.util.*;
import java.io.*;
import java.math.*;
public class Main {
public static class rec {
int flag, num;
rec (int f, int n) {
flag=f;
num=n;
}
}
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;
rec[] data=new rec[100001];
int[] hlp=new int[100001];
int num, tmp, heapSize, from, left, right, pass, root;
boolean change;
for (int i=1; i<=n; i++) {
in.nextToken();
num=(int)in.nval;
if (num==0) {
in.nextToken();
tmp=(int)in.nval;
data[i] = new rec (num, tmp);
} else {
data[i] = new rec (num, 0);
}
}
heapSize=0;
for (int i=1; i<=n; i++) {
if (data[i].flag==1) {
out.println (hlp[1]);
hlp[1]=hlp[heapSize];
hlp[heapSize]=0;
heapSize--;
change=true;
for (int pos=1; change==true; ) {
left=2*pos;
right=left+1;
change=false;
if ((hlp[left]>=hlp[right]) && (hlp[left]>hlp[pos])) {
pass=hlp[left];
hlp[left]=hlp[pos];
hlp[pos]=pass;
pos=left;
change=true;
continue;
}
if ((hlp[right]>=hlp[left]) && (hlp[right]>hlp[pos])) {
pass=hlp[right];
hlp[right]=hlp[pos];
hlp[pos]=pass;
pos=right;
change=true;
}
}
} else {
heapSize++;
hlp[heapSize]=data[i].num;
change=true;
for (int pos=heapSize; change==true; ) {
root=pos/2;
change=false;
if (root>0 && hlp[root]<hlp[pos]) {
pass=hlp[root];
hlp[root]=hlp[pos];
hlp[pos]=pass;
pos=root;
change=true;
}
}
}
}
out.close();
}
}
Revise this Paste