Skip to content

Commit

Permalink
Merge pull request #4892 from freakin23/fix
Browse files Browse the repository at this point in the history
py sol for Modern Art
  • Loading branch information
SansPapyrus683 authored Nov 4, 2024
2 parents af1666e + 8f09864 commit e07ab55
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions solutions/silver/usaco-744.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,73 @@ public class Art {
```
</JavaSection>
<PySection>
```py
class Bound:
def __init__(self, min_r: int, max_r: int, min_c: int, max_c: int):
self.min_r = min_r
self.max_r = max_r
self.min_c = min_c
self.max_c = max_c


with open("art.in") as read:
width = int(read.readline().strip())
canvas = [list(map(int, read.readline().split())) for _ in range(width)]

visible = {}
for r in range(width):
for c in range(width):
color = canvas[r][c]
if color not in visible:
visible[color] = Bound(r, r, c, c)

bounds = visible[color]
bounds.min_r = min(r, bounds.min_r)
bounds.max_r = max(r, bounds.max_r)
bounds.min_c = min(c, bounds.min_c)
bounds.max_c = max(c, bounds.max_c)


visible.pop(0, None) # 0 isn't a color

# all the non-visible colors could've been painted first
poss_first = width * width - len(visible)

# handle the edge case where there's only 1 color
if len(visible) > 1:
# rect_num[r][c] is *going* to contain the # of rectangles
# that must contain the point (r, c)
rect_num = [[0] * (width + 1) for _ in range(width + 1)]
valid_colors = set(visible.keys())

for color, bounds in visible.items():
# initialize it as a difference array
rect_num[bounds.min_r][bounds.min_c] += 1
rect_num[bounds.min_r][bounds.max_c + 1] -= 1
rect_num[bounds.max_r + 1][bounds.min_c] -= 1
rect_num[bounds.max_r + 1][bounds.max_c + 1] += 1

# Apply the difference array to get the actual count
for r in range(width):
for c in range(width):
if r > 0:
rect_num[r][c] += rect_num[r - 1][c]
if c > 0:
rect_num[r][c] += rect_num[r][c - 1]
if r > 0 and c > 0:
rect_num[r][c] -= rect_num[r - 1][c - 1]

# if 2 rectangles overlap, the one at the top
# (aka the shown one) can't have been painted first
if rect_num[r][c] > 1:
valid_colors.discard(canvas[r][c])

poss_first += len(valid_colors)

print(poss_first, file=open("art.out", "w"))
```
</PySection>
</LanguageSection>

0 comments on commit e07ab55

Please sign in to comment.