import sysdata = sys.stdin.read().split()n = int(data[0])a = list(map(int, data[1:1 + n]))# length[i] = a[i]로 끝나는 가장 긴 증가 부분 수열의 길이length = [1] * nfor i in range(n): for j in range(i): if a[j] < a[i] and length[j] + 1 > length[i]: length[i] = length[j] + 1print(max(length))
JavaScriptmain.js · 10 行
const [n, ...a] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);// length[i] = a[i]로 끝나는 가장 긴 증가 부분 수열의 길이const length = new Array(n).fill(1);for (let i = 0; i < n; i++) { for (let j = 0; j < i; j++) { if (a[j] < a[i] && length[j] + 1 > length[i]) length[i] = length[j] + 1; }}console.log(Math.max(...length));
TypeScriptmain.ts · 11 行
import { readFileSync } from "node:fs";const [n, ...a]: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);// length[i] = a[i]로 끝나는 가장 긴 증가 부분 수열의 길이const length: number[] = new Array(n).fill(1);for (let i = 0; i < n; i++) { for (let j = 0; j < i; j++) { if (a[j] < a[i] && length[j] + 1 > length[i]) length[i] = length[j] + 1; }}console.log(Math.max(...length));
JavaMain.java · 26 行
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.StringTokenizer;public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine().trim()); StringTokenizer st = new StringTokenizer(br.readLine()); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = Integer.parseInt(st.nextToken()); // length[i] = a[i]로 끝나는 가장 긴 증가 부분 수열의 길이 int[] length = new int[n]; int best = 0; for (int i = 0; i < n; i++) { length[i] = 1; for (int j = 0; j < i; j++) { if (a[j] < a[i] && length[j] + 1 > length[i]) length[i] = length[j] + 1; } best = Math.max(best, length[i]); } System.out.println(best); }}
C#Main.cs · 23 行
using System;using System.Linq;class Program{ static void Main() { int n = int.Parse(Console.ReadLine()); int[] a = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray(); // length[i] = a[i]로 끝나는 가장 긴 증가 부분 수열의 길이 int[] length = new int[n]; for (int i = 0; i < n; i++) { length[i] = 1; for (int j = 0; j < i; j++) { if (a[j] < a[i] && length[j] + 1 > length[i]) length[i] = length[j] + 1; } } Console.WriteLine(length.Max()); }}
C++main.cpp · 19 行
#include <algorithm>#include <iostream>#include <vector>int main() { int n; std::cin >> n; std::vector<int> a(n); for (int& x : a) std::cin >> x; // length[i] = a[i]로 끝나는 가장 긴 증가 부분 수열의 길이 std::vector<int> length(n, 1); for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { if (a[j] < a[i] && length[j] + 1 > length[i]) length[i] = length[j] + 1; } } std::cout << *std::max_element(length.begin(), length.end()) << '\n';}
Cmain.c · 22 行
#include <stdio.h>#define MAXN 2000int main(void) { static int a[MAXN], length[MAXN]; int n; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); /* length[i] = a[i]로 끝나는 가장 긴 증가 부분 수열의 길이 */ int best = 0; for (int i = 0; i < n; i++) { length[i] = 1; for (int j = 0; j < i; j++) { if (a[j] < a[i] && length[j] + 1 > length[i]) length[i] = length[j] + 1; } if (length[i] > best) best = length[i]; } printf("%d\n", best); return 0;}
Craykonmain.cray · 20 行
n = 控制台.读行().转数字();a = 控制台.读数字();// len[i] = a[i]로 끝나는 가장 긴 증가 부분 수열의 길이len = 新列表(n, 1);best = 1;循环 (i = 1; i < n; i++){ 循环 (j = 0; j < i; j++) { 如果 (a[j] < a[i] && len[j] + 1 > len[i]) { len[i] = len[j] + 1; } } 如果 (len[i] > best) { best = len[i]; }}控制台.输出(best);