Skip to content

Commit 11bcd25

Browse files
committed
[1900] CF463D 多排列 LCS 转化成 DAG 最长路
1 parent e8f92a3 commit 11bcd25

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

main/400-499/463D.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
. "fmt"
6+
"io"
7+
)
8+
9+
// github.com/EndlessCheng/codeforces-go
10+
func CF463D(_r io.Reader, out io.Writer) {
11+
in := bufio.NewReader(_r)
12+
max := func(a, b int) int {
13+
if b > a {
14+
return b
15+
}
16+
return a
17+
}
18+
19+
var n, m, ans int
20+
Fscan(in, &m, &n)
21+
a := make([]int, m)
22+
pos := make([][]int, n)
23+
for i := range pos {
24+
pos[i] = make([]int, m+1)
25+
for j := range a {
26+
Fscan(in, &a[j])
27+
pos[i][a[j]] = j
28+
}
29+
}
30+
31+
dp := make([]int, m) // 最长路
32+
for i, v := range a {
33+
o:
34+
for j, w := range a[:i] {
35+
for _, p := range pos {
36+
if p[w] > p[v] { // 把其中一行当作(1-n),其余行映射一下
37+
continue o
38+
}
39+
}
40+
dp[i] = max(dp[i], dp[j])
41+
}
42+
dp[i]++
43+
ans = max(ans, dp[i])
44+
}
45+
Fprint(out, ans)
46+
}
47+
48+
//func main() { CF463D(os.Stdin, os.Stdout) }

main/400-499/463D_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import (
4+
"github.com/EndlessCheng/codeforces-go/main/testutil"
5+
"testing"
6+
)
7+
8+
// https://codeforces.com/problemset/problem/463/D
9+
// https://codeforces.com/problemset/status/463/problem/D
10+
func TestCF463D(t *testing.T) {
11+
// just copy from website
12+
rawText := `
13+
inputCopy
14+
4 3
15+
1 4 2 3
16+
4 1 2 3
17+
1 2 4 3
18+
outputCopy
19+
3`
20+
testutil.AssertEqualCase(t, rawText, 0, CF463D)
21+
}

0 commit comments

Comments
 (0)