Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class oaks_md implements Runnable {
- private Scanner in;
- private PrintWriter out;
- public void run() {
- try {
- in = new Scanner(new FileReader("oaks.in"));
- out = new PrintWriter(new File("oaks.out"));
- solve();
- in.close();
- out.close();
- } catch (IOException e) {
- e.printStackTrace();
- System.exit(1);
- }
- }
- int n;
- int[] a;
- boolean[][] wipe;
- int[][] howWipe;
- int[] max;
- int[] how;
- public void solve() {
- n = in.nextInt();
- a = new int[n];
- wipe = new boolean[n][n];
- howWipe = new int[n][n];
- max = new int[n];
- how = new int[n];
- for (int i = 0; i < n; i++) {
- a[i] = in.nextInt();
- }
- for (int i = 0; i < n - 1; i++) {
- wipe[i][i + 1] = true;
- howWipe[i][i + 1] = -1;
- }
- for (int d = 2; d < n; d++) {
- for (int i = 0, j; (j = i + d) < n; i++) {
- for (int k = i + 1; k < j; k++) {
- if (a[k] > Math.max(a[i], a[j]) || a[k] < Math.min(a[i], a[j])) {
- if (wipe[i][k] && wipe[k][j]) {
- wipe[i][j] = true;
- howWipe[i][j] = k;
- break;
- }
- }
- }
- }
- }
- max[0] = 1;
- for (int i = 1; i < n; i++) {
- max[i] = 0;
- for (int j = 0; j < i; j++) {
- if (max[j] > 0 && wipe[j][i] && a[j] <= a[i] && max[j] + 1 > max[i]) {
- max[i] = max[j] + 1;
- how[i] = j;
- }
- }
- }
- if (max[n - 1] == 0) {
- out.println(-1);
- return;
- }
- out.println(n - max[n - 1]);
- cut(n - 1);
- }
- private void cut(int v) {
- if (v == 0)
- return;
- cut(how[v]);
- wipe(how[v], v);
- }
- private void wipe(int u, int v) {
- int w = howWipe[u][v];
- if (w < 0)
- return;
- wipe(u, w);
- wipe(w, v);
- out.println(w + 1);
- }
- public static void main(String[] args) throws IOException {
- Locale.setDefault(Locale.US);
- new Thread(new oaks_md()).start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement