ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ๋ฌธ์ œ

 

11722๋ฒˆ: ๊ฐ€์žฅ ๊ธด ๊ฐ์†Œํ•˜๋Š” ๋ถ€๋ถ„ ์ˆ˜์—ด

์ˆ˜์—ด A๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ฐ€์žฅ ๊ธด ๊ฐ์†Œํ•˜๋Š” ๋ถ€๋ถ„ ์ˆ˜์—ด์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ˆ˜์—ด A = {10, 30, 10, 20, 20, 10} ์ธ ๊ฒฝ์šฐ์— ๊ฐ€์žฅ ๊ธด ๊ฐ์†Œํ•˜๋Š” ๋ถ€๋ถ„ ์ˆ˜์—ด์€ A = {10, 30, 10, 20, 20, 10} 

www.acmicpc.net


โœ๐Ÿป ํ’€์ด

๐ŸŽจ Go

// https://www.acmicpc.net/problem/11722
package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n int
	fmt.Fscanln(reader, &n)
	sequence := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d ", &sequence[i])
	}
	fmt.Fprintln(writer, longestDecreasingSequenceCount(sequence))
}

func longestDecreasingSequenceCount(sequence []int) (longestCount int) {
	dp := make([]int, len(sequence))          // ๊ฐ ์š”์†Œ๋กœ๋ถ€ํ„ฐ ์ตœ์žฅ ๊ฐ์†Œ ๋ถ€๋ถ„ ์ˆ˜์—ด์˜ ๊ธธ์ด๋ฅผ ์ €์žฅํ•œ๋‹ค
	for i := len(sequence) - 1; i >= 0; i-- { // ๋น„๊ต๋Œ€์ƒ์„ ์ˆ˜์—ด์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ถ€ํ„ฐ ์ •ํ•˜์—ฌ ๊ณ„์‚ฐํ•œ๋‹ค
		dp[i] = 1
		length := 0
		for j := i + 1; j < len(sequence); j++ {
			if sequence[i] > sequence[j] { // i, j ์š”์†Œ๊ฐ€ ๊ฐ์†Œํ•˜๋Š” ๋ถ€๋ถ„ ์ˆ˜์—ด์— ํ•ด๋‹นํ•  ๊ฒฝ์šฐ
				if length < dp[j] {
					length = dp[j] // j ์š”์†Œ์˜ ์ตœ์žฅ ๊ฐ์†Œ ๋ถ€๋ถ„ ์ˆ˜์—ด์˜ ๊ธธ์ด๊ฐ€ ๋” ํฐ ๊ฒฝ์šฐ length๊ฐ’์„ ๊ฐฑ์‹ ํ•ด์ค€๋‹ค
				}
			}
		}
		dp[i] += length // i ์š”์†Œ์˜ ์ตœ์žฅ ๊ฐ์†Œ ๋ถ€๋ถ„ ์ˆ˜์—ด์˜ ๊ธธ์ด์— length๋ฅผ ๋”ํ•œ๋‹ค
		if longestCount < dp[i] {
			longestCount = dp[i]
		}
	}

	return longestCount
}

๐ŸŽจ Python3

# https://www.acmicpc.net/problem/11722
import sys

def longest_descreasing_sequence_count(sequence):
    dp = [0 for _ in range(len(sequence))]
    longest_count = 0
    for i in range(len(sequence)-1, -1, -1):
        dp[i] = 1
        length = 0
        for j in range(i+1, len(sequence)):
            if sequence[i] > sequence[j]:
                length = max(length, dp[j])
        dp[i] += length
        longest_count = max(longest_count, dp[i])
    return longest_count

if __name__ == "__main__":
    n = int(sys.stdin.readline())
    sequence = list(map(int, sys.stdin.readline().split()))
    print(longest_descreasing_sequence_count(sequence))
728x90
๋Œ“๊ธ€