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 eba40aa commit eeebcad
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions main/1900-1999/1926F.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ 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{}
const N = 7
const MASK = 1<<(N-2) - 1
a := [N]int{}
dp := [N][1 << N][1 << (N - 2)]int{}
for Fscan(in, &T); T > 0; T-- {
a := [7]int{}
for i := range a {
Fscan(in, &s)
a[i] = 0
for j, b := range s {
a[i] |= int(b&1^1) << j
}
Expand All @@ -29,15 +32,15 @@ func cf1926F(_r io.Reader, out io.Writer) {
}
}
var f func(int, int, int) int
f = func(i, pre7, pre5 int) (res int) {
if i == 7 {
return
f = func(i, pre7, pre5 int) int {
if i == N {
return 0
}
p := &dp[i][pre7][pre5]
if *p != -1 {
return *p
}
res = 99
res := N * N
o:
for cur, ok := a[i], true; ok; ok = cur != a[i] {
for t, lb := pre5, 0; t > 0; t ^= lb {
Expand All @@ -48,23 +51,21 @@ func cf1926F(_r io.Reader, out io.Writer) {
}
}
cur5 := 0
for t, lb := cur>>1&31, 0; t > 0; t ^= lb {
for t, lb := cur>>1&MASK, 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])))
res = min(res, f(i+1, cur, cur5)+bits.OnesCount(uint(cur^a[i])))
cur = (cur - 1) & a[i]
}
*p = res
return
return res
}
ans := 99
ans := N * N
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)
ans = min(ans, f(1, cur, 0)+bits.OnesCount(uint(cur^a[0])))
}
Fprintln(out, ans)
}
Expand Down

0 comments on commit eeebcad

Please sign in to comment.