Skip to content

Commit

Permalink
CF1926F
Browse files Browse the repository at this point in the history
  • Loading branch information
EndlessCheng committed Feb 20, 2024
1 parent bad4dd2 commit eba40aa
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
73 changes: 73 additions & 0 deletions main/1900-1999/1926F.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package main

import (
"bufio"
. "fmt"
"io"
"math/bits"
)

// https://space.bilibili.com/206214
func cf1926F(_r io.Reader, out io.Writer) {
in := bufio.NewReader(_r)
var T int
var s string
dp := [7][1 << 7][1 << 5]int{}
for Fscan(in, &T); T > 0; T-- {
a := [7]int{}
for i := range a {
Fscan(in, &s)
for j, b := range s {
a[i] |= int(b&1^1) << j
}
}
for i := range dp {
for j := range dp[i] {
for k := range dp[i][j] {
dp[i][j][k] = -1
}
}
}
var f func(int, int, int) int
f = func(i, pre7, pre5 int) (res int) {
if i == 7 {
return
}
p := &dp[i][pre7][pre5]
if *p != -1 {
return *p
}
res = 99
o:
for cur, ok := a[i], true; ok; ok = cur != a[i] {
for t, lb := pre5, 0; t > 0; t ^= lb {
lb = t & -t
if cur&lb > 0 && cur&(lb<<2) > 0 {
cur = (cur - 1) & a[i]
continue o
}
}
cur5 := 0
for t, lb := cur>>1&31, 0; t > 0; t ^= lb {
lb = t & -t
if pre7&lb > 0 && pre7&(lb<<2) > 0 {
cur5 |= lb
}
}
r := f(i+1, cur, cur5)
res = min(res, r+bits.OnesCount(uint(cur^a[i])))
cur = (cur - 1) & a[i]
}
*p = res
return
}
ans := 99
for cur, ok := a[0], true; ok; ok = cur != a[0] {
res := f(1, cur, 0) + bits.OnesCount(uint(cur^a[0]))
ans = min(ans, res)
}
Fprintln(out, ans)
}
}

//func main() { cf1926F(os.Stdin, os.Stdout) }
50 changes: 50 additions & 0 deletions main/1900-1999/1926F_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Generated by copypasta/template/generator_test.go
package main

import (
"github.com/EndlessCheng/codeforces-go/main/testutil"
"testing"
)

// https://codeforces.com/contest/1926/problem/F
// https://codeforces.com/problemset/status/1926/problem/F
func Test_cf1926F(t *testing.T) {
testCases := [][2]string{
{
`4
WWWWWWW
WWWWBBB
WWWWWBW
WWBBBBB
WWWBWWW
WWBBBWW
WWWWWWW
WWWWWWW
WWWWWWW
WBBBBBW
WBBBBBW
WBBBBBW
WWWWWWW
WWWWWWW
WWWWWWW
WWWWWWW
WWWWWWW
WWWWWWW
WWWWWWW
WWWWWWW
WWWWWWW
WBBBBBW
BBBBBBB
BBBBBBB
WWWWWWW
BBBBBBB
BBBBBBB
BBBBBBB`,
`1
2
0
5`,
},
}
testutil.AssertEqualStringCase(t, testCases, 0, cf1926F)
}

0 comments on commit eba40aa

Please sign in to comment.