File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import sys
2+
3+ def main ():
4+ input = sys .stdin .readline
5+
6+ N , M , K = map (int , input ().split ())
7+
8+ board = [input ().strip () for _ in range (N )]
9+
10+ w = [[0 ] * M for _ in range (N )]
11+ b = [[0 ] * M for _ in range (N )]
12+
13+ for i in range (N ):
14+ for j in range (M ):
15+ expectedColorIfWhite = 'W' if (i + j ) % 2 == 0 else 'B'
16+ expectedColorIfBlack = 'B' if (i + j ) % 2 == 0 else 'W'
17+
18+ if board [i ][j ] != expectedColorIfWhite :
19+ w [i ][j ] = 1
20+ if board [i ][j ] != expectedColorIfBlack :
21+ b [i ][j ] = 1
22+
23+ def prefixSum (array ):
24+ ps = [[0 ] * (M + 1 ) for _ in range (N + 1 )]
25+
26+ for i in range (N ):
27+ for j in range (M ):
28+ ps [i + 1 ][j + 1 ] = array [i ][j ] + ps [i ][j + 1 ] + ps [i + 1 ][j ] - ps [i ][j ]
29+
30+ return ps
31+
32+ prefixSumWhite = prefixSum (w )
33+ print (prefixSumWhite )
34+ prefixSumBlack = prefixSum (b )
35+ print (prefixSumBlack )
36+
37+ minRepaintCount = float ('inf' )
38+ for i in range (N - K + 1 ):
39+ for j in range (M - K + 1 ):
40+ x1 , y1 = i , j
41+ x2 , y2 = i + K , j + K
42+ whiteCount = prefixSumWhite [x2 ][y2 ] - prefixSumWhite [x1 ][y2 ] - prefixSumWhite [x2 ][y1 ] + prefixSumWhite [x1 ][y1 ]
43+ blackCount = prefixSumBlack [x2 ][y2 ] - prefixSumBlack [x1 ][y2 ] - prefixSumBlack [x2 ][y1 ] + prefixSumBlack [x1 ][y1 ]
44+ minRepaintCount = min (minRepaintCount , whiteCount , blackCount )
45+
46+ print (minRepaintCount )
47+
48+ if __name__ == '__main__' :
49+ main ()
Original file line number Diff line number Diff line change 1+ import sys
2+
3+ def main ():
4+ input = sys .stdin .readline
5+
6+ N = int (input ())
7+ heights = list (map (int , input ().split ()))
8+
9+ result = 0
10+
11+
12+ if __name__ == '__main__' :
13+ main ()
You can’t perform that action at this time.
0 commit comments