結果

問題 No.1804 Intersection of LIS
ユーザー ks2mks2m
提出日時 2022-01-07 23:30:42
言語 Java21
(openjdk 21)
結果
AC  
実行時間 692 ms / 2,000 ms
コード長 2,011 bytes
コンパイル時間 2,267 ms
コンパイル使用メモリ 80,420 KB
実行使用メモリ 91,744 KB
最終ジャッジ日時 2024-04-26 19:05:52
合計ジャッジ時間 15,242 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
36,704 KB
testcase_01 AC 49 ms
37,128 KB
testcase_02 AC 50 ms
36,856 KB
testcase_03 AC 549 ms
88,244 KB
testcase_04 AC 537 ms
88,084 KB
testcase_05 AC 518 ms
88,324 KB
testcase_06 AC 530 ms
88,116 KB
testcase_07 AC 520 ms
88,224 KB
testcase_08 AC 519 ms
88,372 KB
testcase_09 AC 528 ms
88,000 KB
testcase_10 AC 515 ms
88,292 KB
testcase_11 AC 509 ms
87,952 KB
testcase_12 AC 523 ms
88,280 KB
testcase_13 AC 511 ms
88,268 KB
testcase_14 AC 553 ms
88,092 KB
testcase_15 AC 526 ms
88,344 KB
testcase_16 AC 530 ms
88,076 KB
testcase_17 AC 531 ms
88,212 KB
testcase_18 AC 82 ms
38,424 KB
testcase_19 AC 85 ms
38,636 KB
testcase_20 AC 86 ms
38,400 KB
testcase_21 AC 76 ms
38,388 KB
testcase_22 AC 74 ms
38,264 KB
testcase_23 AC 81 ms
38,352 KB
testcase_24 AC 80 ms
38,280 KB
testcase_25 AC 82 ms
38,388 KB
testcase_26 AC 73 ms
38,328 KB
testcase_27 AC 74 ms
38,576 KB
testcase_28 AC 50 ms
36,992 KB
testcase_29 AC 48 ms
36,632 KB
testcase_30 AC 50 ms
36,884 KB
testcase_31 AC 49 ms
36,520 KB
testcase_32 AC 49 ms
36,852 KB
testcase_33 AC 50 ms
36,704 KB
testcase_34 AC 50 ms
36,700 KB
testcase_35 AC 692 ms
91,744 KB
testcase_36 AC 659 ms
91,444 KB
testcase_37 AC 397 ms
78,876 KB
testcase_38 AC 396 ms
78,912 KB
testcase_39 AC 48 ms
36,512 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 (next.get(j) < cur.get(r) && a[next.get(j)] < min) {
						nr = j;
						nl = nr;
					}
				} else {
					if (a[next.get(j)] < max) {
						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