这两玩意都不行
import java.util.Scanner;
class Deque {
private int[] elements;
private int top;
private int size;
public void initialize(int size) {
elements = new int[size];
top = -1;
this.size = size;
}
public void push(int element) {
elements[++top] = element;
}
public int pop() {
return elements[top--];
}
public int peek() {
return elements[top];
}
public boolean isEmpty() {
return top == -1;
}
}
public class Main {
public static void calculate(Deque stack, int[] an, int n, int[] res) {
for (int i = n - 1; i >= 0; i--) {
while (!stack.isEmpty() && an[i] >= an[stack.peek()]) {
stack.pop();
}
if (!stack.isEmpty()) {
res[i] = stack.peek() + 1;
} else {
res[i] = 0;
}
stack.push(i);
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] an = new int[n];
int[] res = new int[n];
for (int i = 0; i < n; i++) {
an[i] = scanner.nextInt();
}
Deque stack = new Deque();
stack.initialize(n);
calculate(stack, an, n, res);
for (int i = 0; i < n; i++) {
System.out.print(res[i] + " ");
}
scanner.close();
}
}
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] an = new int[n], res = new int[n];
for (int i = 0; i < n; i++) {
an[i] = sc.nextInt();
}
Deque<Integer> stack = new ArrayDeque<Integer>();
for (int i = n - 1; i >= 0; i--) {
while (!stack.isEmpty() && an[i] >= an[stack.peek()]) {
stack.pop();
}
if (!stack.isEmpty()) {
res[i] = stack.peek() + 1;
} else
res[i] = 0;
stack.push(i);
}
for (int i = 0; i < res.length; i++) {
System.out.print(res[i] + " ");
}
sc.close();
}
}