結果

問題 No.390 最長の数列
ユーザー ぴろずぴろず
提出日時 2015-04-12 13:36:28
言語 Java21
(openjdk 21)
結果
AC  
実行時間 661 ms / 5,000 ms
コード長 700 bytes
コンパイル時間 2,277 ms
コンパイル使用メモリ 77,528 KB
実行使用メモリ 65,592 KB
最終ジャッジ日時 2024-04-10 08:41:31
合計ジャッジ時間 9,364 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 148 ms
58,228 KB
testcase_01 AC 145 ms
58,256 KB
testcase_02 AC 145 ms
57,972 KB
testcase_03 AC 147 ms
58,348 KB
testcase_04 AC 145 ms
57,952 KB
testcase_05 AC 519 ms
63,940 KB
testcase_06 AC 661 ms
65,592 KB
testcase_07 AC 143 ms
58,284 KB
testcase_08 AC 142 ms
58,088 KB
testcase_09 AC 146 ms
58,340 KB
testcase_10 AC 591 ms
63,916 KB
testcase_11 AC 567 ms
63,832 KB
testcase_12 AC 581 ms
63,888 KB
testcase_13 AC 448 ms
63,156 KB
testcase_14 AC 611 ms
63,928 KB
testcase_15 AC 143 ms
58,472 KB
testcase_16 AC 151 ms
58,304 KB
testcase_17 AC 229 ms
61,392 KB
testcase_18 AC 243 ms
61,928 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package longestsequence;

import java.util.Scanner;

public class Main {
	public static int X_MAX = 1000000;
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] x = new int[n];
		for(int i=0;i<n;i++) {
			x[i] = Integer.parseInt(sc.next());
		}
		System.out.println(solve(n,x));
	}

	public static int solve(int n,int[] x) {
		int[] dp = new int[X_MAX+1];
		for(int i:x) {
			dp[i] = 1;
		}
		int ans = 0;
		for(int i=1;i<=X_MAX;i++) {
			if (dp[i] <= 0) {
				continue;
			}
			for(int j=i*2;j<=X_MAX;j+=i) {
				if (dp[j] >= 1) {
					dp[j] = Math.max(dp[j], dp[i] + 1);
				}
			}
			ans = Math.max(ans, dp[i]);
		}
		return ans;
	}

}
0