牛客周赛 Round 26 解题报告 | 珂学家 | 0-1 BFS + 状态机DP


前言


整体评价

T3是一道0-1 BFS题, 这样时间复杂度可以控制在O(n*m), 也可以用优先队列。

T4这类题型,在牛客Round周赛系列出现好多次了,要么状态机DP,要么容斥,如果n很大,就用矩阵幂优化。


欢迎关注

珂朵莉 牛客周赛专栏

珂朵莉 牛客小白月赛专栏


A. 小红的整数操作

思路:同余分组

对k进行取模分组,同余的任意两个数,一定可以构造成一样

python 复制代码
from collections import Counter

n, k = list(map(int, input().split()))
arr = list(map(int, input().split()))

cnt = Counter()
for v in arr:
    cnt[v % k] += 1

res = max(cnt.values())

print (res)

B. 小红的01串

思路:奇偶分析,找规律

可以枚举最终趋同的数是0,还是1

同时相邻2数翻转,其0,1本身个数的奇偶是不改变的

这样可以发现,如果0的个数,1的个数都是奇数,必然是死局,无法构造。

python 复制代码
t = int(input())

while t > 0:
    t -= 1
    s = input()
    n0, n1 = 0, 0
    for c in s:
        if c == '0':
            n0 += 1
        else:
            n1 += 1
    if n0 % 2 == 1 and n1 % 2 == 1:
        print ("No")
    else:
        print ("Yes")

C. 小红闯沼泽地

思路:0-1 BFS,当然用优先队列寻路,也是的可以

这边采用0-1 BFS,应该时间复杂度最低

python 复制代码
from collections import deque
# from typings import inf

h, w = list(map(int, input().split()))

grids = []
for i in range(h):
    row = list(map(int, input().split()))
    grids.append(row)

deq = deque()
deq.append((0, 0))

dp = [[0x3f3f3f3f] * w for _ in range(h)]

# 0-1 bfs
# 或者优先队列

dp[0][0] = 0

dirs = [(1, 0), (0, -1), (0, 1)]

while len(deq) > 0:
    y, x = deq.popleft()
    for dy, dx in dirs:
        ty, tx = y + dy, x + dx
        if 0 <= ty < h and 0 <= tx < w:
            cost = 2 if grids[y][x] != grids[ty][tx] else 1
            if dp[ty][tx] > dp[y][x] + cost:
                dp[ty][tx] = dp[y][x] + cost
                if cost == 1:
                    deq.appendleft((ty, tx))
                else:
                    deq.append((ty, tx))

print (dp[h - 1][w - 1])

D. 小红的漂亮串(二)

思路:状态机DP

引入7个状态

  • any (0个red): 0
  • r: 1
  • re: 2
  • red: 3
  • r(带1个red): 4
  • re(带1个red): 5
  • red(带2个red): 6

因为n为10^6, 所以线性处理下就行了

python 复制代码
mod = 10 ** 9 + 7

n = int(input())

# other: 0
# r: 1
# re: 2
# red: 3
# red, r: 4
# red, re: 5
# red, red: 6

# 状态机DP
dp = [25, 1, 0, 0, 0, 0, 0]

for i in range(1, n):
    dp2 = [0] * 7
    
    dp2[0] = (dp[0] * 25 + dp[1] * 24 + dp[2] * 24) % mod
    dp2[1] = (dp[0] + dp[1] + dp[2]) % mod
    dp2[2] = dp[1]
    
    dp2[3] = (dp[2] + dp[3] * 25 + dp[4] * 24 + dp[5] * 24) % mod
    dp2[4] = (dp[3] + dp[4] + dp[5]) % mod
    dp2[5] = dp[4]
    dp2[6] = (dp[5] + dp[6] * 26) % mod
    
    dp = dp2

print (dp[6])

写在最后

相关推荐
神王宝宝 王者小学5 小时前
HBase: 看上去很美
大数据·数据库·hbase
wabs6666 小时前
关于图论【卡码网117.软件构建的思考】
数据结构·算法·软件构建·图论·卡码网
io无心6 小时前
Shardingsphere5分库分表
数据库·mysql
mifengxing6 小时前
LeetCode 41.缺失的第一个正数|Hard题O(n)+O(1)最优解法深度解析
java·算法·leetcode·排序算法
wWYy.6 小时前
Mysql:主键索引 唯一索引 普通索引 前缀索引
数据库·mysql
Tongzhi20267 小时前
从部署到运维:通芝科技无感考勤一体机的全流程效率解析
运维·数据结构·科技·算法·贪心算法
宝杰X77 小时前
Android Room3 多平台数据库
android·数据库
Wang's Blog7 小时前
AI Agent白手起家30: 动态示例选择器之根据长度选择 Few Shot 示例
算法
markinmarkin8 小时前
Spring 中Bean 的作用域有哪些?
java·后端·spring
oyguyteggytrrwwwrt8 小时前
自制交叉线路识别算法
算法