Codeforces Round #305 Div2 D: Mike and Feet (スタック)
解法
各人iが最弱になるチームを考え、そのチームには最大で何人まで入れることが出来るかを考える。そのためには、iより小さい人がiより右とiより左のどこにいるかを調べれば良い。
類題:code-festival-2014-qualb.contest.atcoder.jp
donuts-2015.contest.atcoder.jp
コード
import java.io.IOException; import java.util.Stack; public class Main { public void solve() { int N = nextInt(); int[] feet = new int[N]; int minfeet = Integer.MAX_VALUE; for (int i = 0; i < N; i++) { feet[i] = nextInt(); minfeet = Math.min(minfeet, feet[i]); } // left[i]:= feet[i]がチーム内で最小になるためには、iより前は誰まで含んで良いか int[] left = new int[N]; Stack<Integer> stack = new Stack<>(); for (int i = 0; i < N; i++) { while (!stack.isEmpty() && feet[stack.peek()] >= feet[i]) { stack.pop(); } if (stack.isEmpty()) { left[i] = 0; } else { left[i] = stack.peek() + 1; } stack.push(i); } // right[i]:= feet[i]がチーム内で最小になるためには、iより後ろは誰まで含んで良いか int[] right = new int[N]; stack.clear(); for (int i = N - 1; i >= 0; i--) { while (!stack.isEmpty() && feet[stack.peek()] >= feet[i]) { stack.pop(); } if (stack.isEmpty()) { right[i] = N - 1; } else { right[i] = stack.peek() - 1; } stack.push(i); } // iがチーム内で最弱である時、そのチームには何人いるか int[] width = new int[N]; for (int i = 0; i < N; i++) { width[i] = (i - left[i] + 1) + (right[i] - i + 1) - 1; if (feet[i] == minfeet) { width[i] = N; } } // strength[i]:= i+1人でできるチームの最大の強さ int[] strength = new int[N]; for (int i = 0; i < width.length; i++) { strength[width[i] - 1] = Math.max(strength[width[i] - 1], feet[i]); } for (int i = N - 2; i >= 0; i--) { if (strength[i] < strength[i + 1]) { strength[i] = strength[i + 1]; } } for (int i = 0; i < left.length; i++) { System.out.print(strength[i] + " "); } System.out.println(); } private int nextInt() { int c; try { c = System.in.read(); while (c != '-' && (c < '0' || c > '9')) c = System.in.read(); if (c == '-') return -nextInt(); int res = 0; while (c >= '0' && c <= '9') { res = res * 10 + c - '0'; c = System.in.read(); } return res; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return -1; } public static void main(String[] args) { new Main().solve(); } }