結果

問題 No.360 増加門松列
ユーザー GrenacheGrenache
提出日時 2016-04-29 12:16:48
言語 Java21
(openjdk 21)
結果
AC  
実行時間 151 ms / 2,000 ms
コード長 2,463 bytes
コンパイル時間 3,581 ms
コンパイル使用メモリ 75,176 KB
実行使用メモリ 56,672 KB
最終ジャッジ日時 2023-08-25 18:38:23
合計ジャッジ時間 6,875 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 136 ms
56,544 KB
testcase_01 AC 140 ms
56,024 KB
testcase_02 AC 112 ms
55,720 KB
testcase_03 AC 115 ms
56,124 KB
testcase_04 AC 140 ms
56,480 KB
testcase_05 AC 124 ms
56,472 KB
testcase_06 AC 140 ms
56,172 KB
testcase_07 AC 142 ms
56,652 KB
testcase_08 AC 129 ms
56,672 KB
testcase_09 AC 130 ms
54,300 KB
testcase_10 AC 131 ms
56,044 KB
testcase_11 AC 116 ms
55,684 KB
testcase_12 AC 137 ms
56,212 KB
testcase_13 AC 113 ms
55,752 KB
testcase_14 AC 117 ms
55,948 KB
testcase_15 AC 151 ms
56,380 KB
testcase_16 AC 113 ms
56,284 KB
testcase_17 AC 121 ms
55,964 KB
testcase_18 AC 139 ms
56,092 KB
testcase_19 AC 141 ms
56,644 KB
testcase_20 AC 139 ms
56,160 KB
testcase_21 AC 124 ms
56,380 KB
権限があれば一括ダウンロードができます

ソースコード

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