Answer the question
In order to leave comments, you need to log in
How to increase script performance?
Artist
(Time: 1 sec. Memory: 16 MB Difficulty: 26%)
A famous artist decided to paint a new masterpiece. After many days of hard work, he wanted to explore his creation. The artist recalled that the picture was painted as follows: first, a white canvas was taken, having the shape of a rectangle with a width w and a height h. Then the artist drew on this canvas n rectangles with sides parallel to the sides of the canvas and vertices located in integer coordinates. Help the artist determine the area of the unpainted part of the canvas.
The first line of the input file INPUT.TXT contains two natural numbers w and h (1 ≤ w, h ≤ 100). The second line contains an integer n (0 ≤ n ≤ 5000) – the number of rectangles. The next n lines contain information about all rectangles. Each line describes one rectangle as four numbers x1, y1, x2, y2 , where (x1, y1) and (x2, y2) are the coordinates of the upper left and lower right corners of the rectangle, respectively.
Output to the output file OUTPUT.TXT a single integer - the area of the unpainted part of the canvas.
w,h = input().split()
w = int(w)
k = 0
h = int(h)
sq = []
count = int(input())
arr = [[0]*h for i in range(w)]
for i in range(count):
square = [int(l) for l in input().split()]
for i in range(square[0],square[2]):
for j in range(square[1],square[3]):
arr[i][j] = 1
for i in arr:
for j in i:
if j==0:
k += 1
print(k)
Answer the question
In order to leave comments, you need to log in
The most obvious option is to reduce the number of passes through the array.
You don't need the second pass at all, because already on the first one you can count the number of "painted" points (if the point is not painted over yet, paint over it and increase the counter, otherwise go to the next one). Having the number of "filled" points and the area of the canvas, the calculation is reduced to one action, and the complexity of the algorithm is halved.
If I'm not confusing anything, then it will turn out something like this
w,h = input().split()
w = int(w)
k = 0
h = int(h)
canvasArea = w * h
count = int(input())
arr = [[0]*h for i in range(w)]
for i in range(count):
square = [int(l) for l in input().split()]
for i in range(square[0],square[2]):
for j in range(square[1],square[3]):
if arr[i][j]==0:
k += 1
arr[i][j] = 1
print(canvasArea - k)
Problem analysis on Reddit https://www.reddit.com/comments/zaa0v .
Adapted to the case specified in the condition:
the rectangle is defined by the values (x1, y1, x2, y2), where (x1, y1) is the upper left corner, (x2, y2) is the lower right corner.
x1,y1───────────┐
│ │
│ │
│ │
│ │
└───────────x2,y2
Прямоугольники (0,3,4,0), (4,7,7,3) и (7,3,10,0) будут выглядеть примерно так:
* * *
* * *
* * *
* * *
* * * * * * *
* * * * * * *
* * * * * * *
Рассмотрим три прямоугольника (0,3,4,0), (2,5,5,1) и (4,7,7,4):
* * *
* * *
* * + * *
* * *
* * + + *
* * + + *
* * * *
Рассмотрим прямоугольники (0,3,4,0), (2,5,5,1) и (3,3,0,6)
* * *
* * *
* * + x + *
* * + x + *
* * * + * *
S1 = 12 + 12 + 9
S2 = 4 + 3 + 4
S3 = 2
def bounding_box(rects):
return (min(r[0] for r in rects),
max(r[1] for r in rects),
max(r[2] for r in rects),
min(r[3] for r in rects))
def area(rect):
a, b, c, d = rect
return (c - a) * (b - d)
def clip(bb, rects):
if not rects:
return []
(x1, y1, x2, y2) = rects[0]
rs = rects[1:]
(a1, b1, a2, b2) = bb
if a1 == a2 or b1 == b2:
return []
if a1 >= x2 or a2 <= x1 or y1 <= b2 or y2 >= b1:
return clip(bb, rs)
result = [(max(a1, x1), min(b1, y1), min(a2, x2), max(b2, y2))]
return result + clip(bb, rs)
def cover(bb, rects):
if not rects:
return 0
rc = rects[0]
rs = rects[1:]
(a1, b1, a2, b2) = bb
(x1, y1, x2, y2) = rc
top = (a1, b1, a2, y1)
bottom = (a1, y2, a2, b2)
left = (a1, y1, x1, y2)
right = (x2, y1, a2, y2)
sum_area = sum(cover(x, clip(x, rs)) for x in [top, bottom, left, right])
return area(rc) + sum_area
def main():
width_canvas = 10
height_canvas = 10
rect1 = (0, 3, 4, 0)
rect2 = (2, 5, 5, 1)
rect3 = (3, 3, 6, 0)
rs = [rect1, rect2, rect3]
painted_area = cover(bounding_box(rs), rs)
canvas_area = width_canvas * height_canvas
unpainted_area = canvas_area - painted_area
print("Canvas area:", canvas_area)
print("Painted area:", painted_area)
print("Unpainted area:", unpainted_area)
if __name__ == '__main__':
main()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question