← Home ← Back to /g/

Thread 106243790

45 posts 14 images /g/
Anonymous No.106243790 >>106244185 >>106244294 >>106246241 >>106247473 >>106248770 >>106249759 >>106249875 >>106250641 >>106251072 >>106251282 >>106255865 >>106256729
/cpg/ - Competitive Programming general
char broiled;
double cheeseburger;
string cheese;


Discuss: contests, resources, problems
Ask for: solutions, hints

Resources:
https://cses.fi/book/book.pdf
https://cp-algorithms.com/
https://usaco.guide/plat/

Sites:
https://cses.fi/problemset/
https://codeforces.com/
https://atcoder.jp/
Anonymous No.106244185 >>106244210 >>106250104 >>106253026
>>106243790 (OP)
use case for finding the area of 3 fictional rectangles in Python?
Anonymous No.106244210 >>106255534 >>106255856 >>106256147
>>106244185
shows that you can and you won't end up like this lady
Anonymous No.106244294 >>106244369
>>106243790 (OP)
Doing algorithms and data structures manually today is the same as still doing arithmetic manually when calculators were invented
Just admit it's a solved problem.
Anonymous No.106244369 >>106244410 >>106246128
>>106244294
You're gonna leak a buncha roasties IDs in a public S3 bucket with this attitude, go ahead. Other anons will make bank cleaning up vibe coded shit for the next decade or two.
Anonymous No.106244410
>>106244369
That's a very high level architecting issue though, thankfully AI can't do it properly. But writing a short area(rec1, rec2, rec3) function is something it's already far above you and me.
Anonymous No.106244461
Imaging wasting your time programming fictional shit from school lessons instead of making something practical that you and other people would actually use.
Programming is solved. Bash scripts is all you need nowadays because everything has already been created.
Anonymous No.106246128
>>106244369
kek have fun "cleaning up vibe coded shit," janny. You sheep will do anything for spare change
Anonymous No.106246241
>>106243790 (OP)
>cp general
Anonymous No.106247473
>>106243790 (OP)
i personally think cp is retarded and a waste of time, if you wanna get good at DSA just practise leetcode and get referrals from your friends working at fagman
Anonymous No.106248770
>>106243790 (OP)
bump. also what an unfortunate acronym.
Anonymous No.106249759 >>106250468
>>106243790 (OP)
I'm retarded, especially at python, but I'm gonna give it a try
Anonymous No.106249875
>>106243790 (OP)
make a function that takes in 2 intervals and returns their intersection. then you can find the union area of two rectangles by adding their separate areas and subtracting the intersection area, the intersection area will be the length of the intersection on x axis and y axis (if no intersection then length = 0). Similarly for 3 rectangles you can do A1 + A2 + A3 - A12 - A23 - A31 + A123, where A1 is area of rec1, A12 is area of inter. between 1 and 2, and A123 is intersection between all. A123 is just going to be the intersection on x axis of the 3 rects time the intersection on y axis (easy to define, inter(a, b, c) = inter(inter(a, b), c))

someone write this in python and check if im right thq
Anonymous No.106250104 >>106250782
>>106244185
Collision detection.
Anonymous No.106250468 >>106250540 >>106256682
>>106249759
def calculateArea(rec):
return abs(rec[0] - rec[2]) * abs(rec[1] - rec[3])

def calculateOverlap(recA, recB):
# check if B is entirely outside A
if (recB[0] >= recA[2] or # B is to the right
recB[2] <= recA[0] or # B is to the left
recB[1] <= recA[3] or # B is below
recB[3] >= recA[1]): # B is above
return 0

hOverlap = 0
vOverlap = 0

if(recB[0] < recA[0]): # poking out to the left
hOverlap = recB[2] - recA[0]
print('one', recB[2], recA[0])
elif(recB[2] > recA[2]): # poking out to the right
hOverlap = recA[2] - recB[0]
else: # entirely inside on this axis
hOverlap = recB[2] - recB[0]

if(recB[1] > recA[1]): # poking out to the top
vOverlap = recB[3] - recA[1]
elif(recB[3] < recA[3]): # poking out to the bottom
vOverlap = recA[3] - recB[1]
else: # entirely inside on this axis
vOverlap = recB[3] - recB[1]

return abs(hOverlap) * abs(vOverlap)

def area(rec1, rec2, rec3):
overlappingArea = 0
rec1rec2Overlap = calculateOverlap(rec1, rec2)
rec2rec3Overlap = calculateOverlap(rec2, rec3)
rec3rec1Overlap = calculateOverlap(rec3, rec1)
overlappingArea += rec1rec2Overlap + rec2rec3Overlap + rec3rec1Overlap

# check if all three are overlapping in one spot and remove double count
if(rec1rec2Overlap > 0 and
rec2rec3Overlap > 0 and
rec3rec1Overlap > 0):
# the rightmost left edge
leftEdge = max(rec1[0], rec2[0], rec3[0])
# the leftmost right edge
rightEdge = min(rec1[2], rec2[2], rec3[2])
# the downmost top edge
topEdge = min(rec1[1], rec2[1], rec3[1])
# the upmost bottom edge
bottomEdge = max(rec1[3], rec2[3], rec3[3])
overlappingArea -= abs(leftEdge - rightEdge) * abs(bottomEdge - topEdge)

combinedArea = calculateArea(rec1) + calculateArea(rec2) + calculateArea(rec3)
return combinedArea - overlappingArea

if __name__ == "__main__":
rec1 = (-1,1,1,-1)
rec2 = (0,3,2,0)
rec3 = (0,2,3,-2)
print(area(rec1, rec2, rec3))

Feels a bit crap somehow but seems to work
Anonymous No.106250540 >>106250554 >>106250569
>>106250468
Does it pass the tests at https://cses.fi/dsa24k/task/2821 ?
Anonymous No.106250554 >>106254224
>>106250540
I guess that requires some Suomi login but https://cses.fi/problemset/task/1741/ looks equivalent
Anonymous No.106250569 >>106250577
>>106250540
I can't see any option to test on either. Probably not, though.
Anonymous No.106250577
>>106250569
I think you have to create an account :(
Anonymous No.106250641 >>106250654 >>106250659 >>106252506
>>106243790 (OP)
a = overlap(1,2)
b = overlap(2,3)
c = overlap(a,b)
return area(c)
Anonymous No.106250654
>>106250641
ok, now implement overlap
Anonymous No.106250659 >>106250741
>>106250641
that might work for the picture shown but not the general case
Anonymous No.106250741 >>106250782 >>106251102
>>106250659
depends what overlap does. if it just returns all the cells as a list, then it does work (but is too slow). I think they are looking for [spoiler]a standard sweepline[/spoiler] here.
Anonymous No.106250782
>>106250741
i would like to know more about the prompt though? why do you need the area? if it is collision detection ( >>106250104) testing for area > 0 then that is super dumb
Anonymous No.106251072 >>106252506
>>106243790 (OP)
inclusion-exclusion problem.
A + B + C - AB - AC - BC + ABC
area of rectangle (x1,y1,x2,y2) is |x1-x2||y1-y2|
overlap of (x1,y1,x2,y2) and (a1,b1,a2,b2) is
(0,0,0,0) if x2 <= a1 or a2 <= x1 or y1 <= b2 or b1 <= y2
otherwise it is (max(a1,x1), min(b1,y1), min(a2,x2), max(b2,y2))

(and you save say the rectangle AB in order to compute ABC = (AB)C)
Anonymous No.106251102
>>106250741
>standard sweepline
That sounds way better than my shitty collision method but I'm too much of a brainlet to work out a good way to know how the rectangles overlap on a line and where there's gaps. Maybe tomorrow
Anonymous No.106251259
I wonder if you can convert the full polygon to a set of lines and then see what side of the line the point falls on. then, see if it is on the other side of a line with a lower x or y
Anonymous No.106251282
>>106243790 (OP)
isnt this literally just calculating the z buffer
Anonymous No.106252506 >>106254224
>>106250641
general case is
>>106251072
Anonymous No.106253026
>>106244185
NMR for YOLO detection
Anonymous No.106254224
>>106252506
general case (k>3) is >>106250554
Anonymous No.106254498 >>106254513 >>106255793
I can't do it anymore.
I think I might have early-onset Alzheimer's.
Anonymous No.106254513
>>106254498
Vaxx and microplastics will do that
Anonymous No.106255534 >>106255810
>>106244210
if anything these problems are what AI does best
Anonymous No.106255793
>>106254498
get more sleep
Anonymous No.106255810 >>106256216
>>106255534
true, the problem is beyond the average coder. and i dont mean that in a bad way, who the fuck here is obsessed about quads?? if the quads cross through each other, then a quadtree is the best approach. also a quadtree can handle more than three. yes a sweeping line algorithm can do it, but it requires a lot of fuckery to handle a cross. the 'right' answer depends how much you want to min/max on the generalization or if you just want to strictly answer the 3 quads provided.
Anonymous No.106255856 >>106256118
>>106244210
would
Anonymous No.106255865
>>106243790 (OP)
Maybe the real competition was the job rejection letters we received along the way...
Anonymous No.106256118
>>106255856
same
Anonymous No.106256147
>>106244210
>I strong womyn need to work at some jew company because jews on tv propagandised me my entire existence
>mah opportooooonity
Anonymous No.106256216 >>106256318
>>106255810
first day in DSA? quadtree is overkill
Anonymous No.106256318
>>106256216
>the 'right' answer depends how much you want to min/max on the generalization or if you just want to strictly answer the 3 quads provided.
Anonymous No.106256682 >>106257039
>>106250468
Why do you define a 1 line function and then only call it 3 times in 1 place when you could have just used a for loop.
Your checks can be loops too.
Anonymous No.106256729
>>106243790 (OP)
GPT oneshot from my naive half description from the problem
def a(r):
return abs(r[0] - r[2]) * abs(r[1] - r[3])

def o(r1, r2):
h = max(0, min(r1[2], r2[2]) - max(r1[0], r2[0]))
v = max(0, min(r1[1], r2[1]) - max(r1[3], r2[3]))
return h * v

def t(*R):
ov = 0
n = len(R)
for i in range(n):
for j in range(i + 1, n):
ov += o(R[i], R[j])

l = max(r[0] for r in R)
r = min(r[2] for r in R)
t_ = min(r[1] for r in R)
b = max(r[3] for r in R)

if l < r and b < t_:
ov -= (r - l) * (t_ - b)

return sum(a(r) for r in R) - ov

if __name__ == "__main__":
r1 = (-1, 1, 1, -1)
r2 = (0, 3, 2, 0)
r3 = (0, 2, 3, -2)
print(t(r1, r2, r3))
Anonymous No.106257039
>>106256682
That solution only works with 3. So, I don't like the implication that it might work with more and it's only three times so the saving is minimal anyway.
I only defined the function for readbility.