编程语言:Java
题目:
题解:为了计算方便,将下标为1定义为根结点,要不然调整小根堆的时候要麻烦一点
结果:AC
import java.io.*;
import java.util.Scanner;
public class Main {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static Scanner sc = new Scanner(new BufferedInputStream(System.in));
static int pos = 1;
static int[] a;
public static void main(String[] args) throws IOException {
int n = sc.nextInt();
int m = sc.nextInt();
a = new int[n + 1];
for (int i = 0; i < n; i++) {
insert(sc.nextInt());
}
for (int i = 0; i < m; i++) {
int num = sc.nextInt();
boolean flag = false;
while (num != 0) {
if (!flag) {
out.print(a[num]);
flag = true;
} else
out.print(" " + a[num]);
num/=2;
}
out.println();
}
out.flush();
}
private static void insert(int x) {
a[pos] = x;
int temp = pos++;
//这里必须要有temp!=1的条件,因为当temp==1的时候就意味着该数是该路径中的最小值了
while (x<a[temp/2]&&temp!=1) {
a[temp]=a[temp/2];
a[temp/2]=x;
temp/=2;
}
}
}