Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Heap {
- public static int len;
- public static int extract(int[] h) {
- int tmp = h[0];
- h[0] = h[len - 1];
- h[len - 1] = tmp;
- len--;
- int i = 0;
- int j;
- while (true) {
- j = i;
- if ((2 * i + 1) < len && h[2 * i + 1] > h [j]) {
- j = 2 * i + 1;
- }
- if ((2 * i + 2) < len && h[2 * i + 2] > h [j]) {
- j = 2 * i + 2;
- }
- if (j == i) {
- break;
- }
- i = j;
- }
- return h[len];
- }
- public static int[] insert(int[] h, int x) {
- len++;
- h[len - 1] = x;
- int i = len - 1;
- int tmp;
- while (i > 0 && h[i] > h[(i - 1) / 2]) {
- tmp = h[i];
- h[i] = h[(i - 1) / 2];
- h[(i - 1) / 2] = tmp;
- i = (i - 1) / 2;
- }
- return h;
- }
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int n = sc.nextInt();
- int x, res;
- len = 0;
- int[] h = new int[100001];
- for (int i = 0; i < n; i++) {
- if (sc.nextInt() == 0) {
- x = sc.nextInt();
- insert(h,x);
- } else {
- res = extract(h);
- System.out.println(res);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement