結果

問題 No.360 増加門松列
ユーザー Grenache
提出日時 2016-04-29 12:16:48
言語 Java
(openjdk 23)
結果
AC  
実行時間 154 ms / 2,000 ms
コード長 2,463 bytes
コンパイル時間 3,397 ms
コンパイル使用メモリ 79,232 KB
実行使用メモリ 41,868 KB
最終ジャッジ日時 2024-12-24 04:33:58
合計ジャッジ時間 7,465 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;


public class Main_yukicoder360 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int[] d = new int[7];
        for (int i = 0; i < 7; i++) {
        	d[i] = sc.nextInt();
        }

        int[] tmp = new int[3];

		Permutation np = new Permutation(7);
		for (List<Integer> perm : np) {
//			System.out.println(perm);
			if (perm == null) {
				break;
			}

			boolean flag = true;
			for (int i = 0; i < 5; i++) {
				tmp[0] = d[perm.get(i + 0)];
				tmp[1] = d[perm.get(i + 1)];
				tmp[2] = d[perm.get(i + 2)];

				if (tmp[2] <= tmp[0] || !isKado(tmp)) {
					flag = false;
					break;
				}
			}

			if (flag) {
				System.out.println("YES");

				sc.close();
				return;
			}
		}

		System.out.println("NO");

        sc.close();
    }

	private static boolean isKado(int[] a) {
		if (a[0] == a[1] || a[1] == a[2] || a[2] == a[0]) {
			return false;
		}

		if (a[1] > a[0] && a[1] > a[2]) {
			return true;
		}
		if (a[1] < a[0] && a[1] < a[2]) {
			return true;
		}

		return false;
	}

	private static class Permutation implements Iterable<List<Integer>>{
		int n;

		boolean[] used;
		List<Integer> perm;
		Deque<Integer> ist;
		PermutationIterator it;

		Permutation(int n) {
			this.n = n;
			used = new boolean[n];
			perm = new ArrayList<Integer>();
			ist = new ArrayDeque<Integer>();
			ist.add(-1);
			it = new PermutationIterator(this);
		}

		List<Integer> next() {
			out:
			while (!ist.isEmpty()) {
				int k = ist.pop();

				if (k != -1) {
					// ループ途中での戻り
					used[k] = false;
					perm.remove(perm.size() - 1);
				} else {
					// 最初から
					int pos = perm.size();

					if (pos == n) {
						// perm に対する操作 =========================
						return perm;
						// perm に対する操作 =========================
					}
				}

				for (int i = k + 1; i < n; i++) {
					if (!used[i]) {
						ist.push(i);

						used[i] = true;
						perm.add(i);
						ist.push(-1);
						continue out;
					}
				}
			}

		return null;
		}

		@Override
		public Iterator<List<Integer>> iterator() {
			return it;
		}

		private static class PermutationIterator implements Iterator<List<Integer>> {

			Permutation p;
			PermutationIterator(Permutation p) {
				this.p = p;
			}

			@Override
			public boolean hasNext() {
				return !p.ist.isEmpty();
			}

			@Override
			public List<Integer> next() {
				return p.next();
			}

		}
	}
}
0