結果
| 問題 | No.2218 Multiple LIS |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-03-02 21:42:13 |
| 言語 | Go (1.23.4) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,464 bytes |
| 記録 | |
| コンパイル時間 | 11,994 ms |
| コンパイル使用メモリ | 235,060 KB |
| 実行使用メモリ | 26,336 KB |
| 最終ジャッジ日時 | 2024-09-17 14:57:13 |
| 合計ジャッジ時間 | 17,186 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 19 TLE * 1 -- * 19 |
ソースコード
package main
import (
"bufio"
"fmt"
"os"
"sort"
"strconv"
)
var sc = bufio.NewScanner(os.Stdin)
var out = bufio.NewWriter(os.Stdout)
func main() {
//bufサイズ以上の文字列入力が必要な場合は拡張すること
buf := make([]byte, 9*1024*1024)
sc.Buffer(buf, bufio.MaxScanTokenSize)
sc.Split(bufio.ScanWords)
n := nextInt()
a := nextIntSlice(n)
ans := solve(n, a)
PrintInt(ans)
}
func divide(x int) []int {
m := make(map[int]struct{})
for i := 1; i*i <= x; i++ {
if x%i == 0 {
m[i] = struct{}{}
m[x/i] = struct{}{}
}
}
var res []int
for k := range m {
res = append(res, k)
}
sort.Ints(res)
return res
}
func solve(n int, a []int) int {
dp := make(map[int]int)
dp[0] = 0
for i := 1; i <= n; i++ {
next := make(map[int]int)
for k := range dp {
next[k] = dp[k]
}
d := divide(a[i-1])
d = append([]int{0}, d...)
for _, v := range d {
if _, found := dp[v]; found {
next[a[i-1]] = Max(next[a[i-1]], dp[v]+1)
}
}
dp = next
}
var ans int
for _, v := range dp {
ans = Max(ans, v)
}
return ans
}
func nextInt() int {
sc.Scan()
i, _ := strconv.Atoi(sc.Text())
return int(i)
}
func nextIntSlice(n int) []int {
s := make([]int, n)
for i := range s {
s[i] = nextInt()
}
return s
}
func PrintInt(x int) {
defer out.Flush()
fmt.Fprintln(out, x)
}
func Min(x, y int) int {
if x < y {
return x
}
return y
}
func Max(x, y int) int {
if x < y {
return y
}
return x
}