fork download
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Stack;
  5. import java.util.StringTokenizer;
  6.  
  7. public class Main {
  8. public static class pair {
  9. int idx, value;
  10.  
  11. public pair(int idx, int value) {
  12. this.idx = idx;
  13. this.value = value;
  14. }
  15. }
  16.  
  17. public static BufferedReader br;
  18. public static int N;
  19. public static StringTokenizer st;
  20. public static Stack<pair> stack;
  21.  
  22. public static void main(String[] args) throws IOException {
  23. // TODO Auto-generated method stub
  24. N = Integer.parseInt(br.readLine());
  25.  
  26. stack = new Stack<pair>();
  27.  
  28. st = new StringTokenizer(br.readLine());
  29. for (int i = 1; i <= N; i++) {
  30. int val = Integer.parseInt(st.nextToken());
  31. while (!stack.isEmpty()) {
  32.  
  33. if (!stack.empty() && stack.peek().value > val) {
  34. System.out.printf("%d ", stack.peek().idx);
  35. break;
  36. }
  37. else
  38. stack.pop();
  39. }
  40. if (stack.empty()) {
  41. System.out.printf("%d ", 0);
  42. }
  43. stack.push(new pair(i, val));
  44. }
  45. }
  46. }
Success #stdin #stdout 0.09s 48460KB
stdin
5
5 5 5 5 5
stdout
0 0 0 0 0