4.23美团黑白格子
关键是找两种奇偶位置的第一第二类以及判断
本地测了几都是对的,不过交上去的时候时间太赶没看到结果,,感觉应该是对的
import heapq [row,column]=[int(x) for x in input().split()] board=[] for i in range(row): board.append([int(x) for x in input().split()]) aSum={} bSum={} aNum=[] bNum=[] for i in range(row): rowFlag=i%2 for j in range(column): if j%2==rowFlag: aSum[board[i][j]]=aSum.get(board[i][j],0)+1 else: bSum[board[i][j]]=bSum.get(board[i][j],0)+1 aNum=[aSum[x] for x in aSum] aNum=set(aNum) aNum=heapq.nlargest(2,aNum) aFirst=[x for x in aSum if aSum[x] in aNum] bNum=[bSum[x] for x in bSum] bNum=set(bNum) bNum=heapq.nlargest(2,bNum) bFirst=[x for x in bSum if bSum[x] in bNum] result=row*column-aFirst[0] for aCurrent in aFirst: for bCurrent in bFirst: if aCurrent==bCurrent: result=min(result,row*column-aSum[aCurrent]) else: result=min(result,row*column-aSum[aCurrent]-bSum[bCurrent]) print(result)