赞
踩
import java.util.*; import java.util.Arrays; public class Main{ public static void main(String [] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int [] arr = new int[n]; for(int i=0;i<n;i++){ arr[i] = sc.nextInt(); } int [] [] res = getRes(n,arr); for(int i=0;i<n;i++){ System.out.println(res[i][0]+" "+res[i][1]); } } public static int[][] getRes(int n, int[] arr){ int [][] res = new int[n][2]; Stack<List<Integer>> stack = new Stack<>(); for(int i=0;i<n;i++){ while(!stack.isEmpty() && arr[i]<arr[stack.peek().get(0)]){ List<Integer> temp = stack.pop(); int left = stack.isEmpty()? -1 : stack.peek().get(stack.peek().size()-1); for(Integer ind: temp){ res[ind][0] = left; res[ind][1] = i; } } if(!stack.isEmpty()&& arr[i]==arr[stack.peek().get(0)]){ stack.peek().add(Integer.valueOf(i)); }else{ ArrayList<Integer> list = new ArrayList<>(); list.add(i); stack.push(list); } } while(!stack.isEmpty()){ List<Integer> help = stack.pop(); int left = stack.isEmpty()?-1:stack.peek().get(stack.peek().size()-1); for(Integer temp:help){ res[temp][0] = left; res[temp][1] = -1; } } return res; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。