結果

問題 No.1804 Intersection of LIS
ユーザー ks2mks2m
提出日時 2022-01-07 23:19:58
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,985 bytes
コンパイル時間 2,342 ms
コンパイル使用メモリ 79,456 KB
実行使用メモリ 91,652 KB
最終ジャッジ日時 2024-04-26 19:04:42
合計ジャッジ時間 15,244 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 49 ms
36,864 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 48 ms
36,720 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 48 ms
36,836 KB
testcase_34 WA -
testcase_35 AC 639 ms
91,452 KB
testcase_36 AC 630 ms
91,652 KB
testcase_37 AC 398 ms
79,052 KB
testcase_38 AC 397 ms
78,612 KB
testcase_39 AC 50 ms
36,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		String[] sa = br.readLine().split(" ");
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = Integer.parseInt(sa[i]);
		}
		br.close();

		int[] dp = new int[n + 1];
		List<List<Integer>> list = new ArrayList<>(n + 1);
		list.add(new ArrayList<>());
		for (int i = 1; i <= n; i++) {
			dp[i] = Integer.MAX_VALUE;
			list.add(new ArrayList<>());
		}

		for (int i = 0; i < n; i++) {
			int idx = Arrays.binarySearch(dp, a[i]);
			if (idx < 0) idx = ~idx;
			dp[idx] = a[i];
			list.get(idx).add(i);
		}

		int lis = Arrays.binarySearch(dp, Integer.MAX_VALUE - 1);
		if (lis < 0) {
			lis = ~lis;
			lis--;
		}

		List<Integer> list2 = list.get(lis);
		int l = 0;
		int r = list2.size() - 1;
		List<Integer> ans = new ArrayList<>();
		int cnt = 0;
		if (r == 0) {
			ans.add(a[list2.get(0)]);
			cnt++;
		}

		for (int i = lis - 1; i > 0; i--) {
			List<Integer> cur = list.get(i + 1);
			List<Integer> next = list.get(i);
			int max = a[cur.get(l)];
			int min = a[cur.get(r)];
			int nr = -1;
			int nl = -1;
			for (int j = next.size() - 1; j >= 0; j--) {
				if (nr == -1) {
					if (a[next.get(j)] <= max) {
						nr = j;
						nl = nr;
					}
				} else {
					if (a[next.get(j)] >= min) {
						nl = j;
					} else {
						break;
					}
				}
			}
			if (nl == nr) {
				ans.add(a[next.get(nl)]);
				cnt++;
			}
			l = nl;
			r = nr;
		}
		StringBuilder sb = new StringBuilder();
		if (cnt > 0) {
			Collections.reverse(ans);
			for (int i : ans) {
				sb.append(i).append(' ');
			}
			sb.deleteCharAt(sb.length() - 1);
		}
		System.out.println(cnt);
		System.out.println(sb.toString());
	}
}
0