From: Skullheadx Date: Fri, 3 Feb 2023 20:35:08 +0000 (-0500) Subject: 2021 S3 X-Git-Url: http://git.skullheadx.com/nixos/README?a=commitdiff_plain;h=ea6496bb63983adf6290d5e02fe8ada266026a02;p=CCC.git 2021 S3 --- diff --git a/Main/Python/2021/S3.py b/Main/Python/2021/S3.py new file mode 100644 index 0000000..245a6e0 --- /dev/null +++ b/Main/Python/2021/S3.py @@ -0,0 +1,39 @@ +class Person: + + def __init__(self, pos, walk_time, hear_distance): + self.pos = pos + self.walk_time = walk_time + self.hear_distance = hear_distance + + def get_walk_time(self, c): + dist = abs(self.pos - c) + if dist <= self.hear_distance: + return 0 + return (dist - self.hear_distance) * self.walk_time + + +def get_cost(people, c): + total_time = 0 + for p in people: + total_time += p.get_walk_time(c) + return total_time + + +def bisection(people, left, right): + if left == right: + return left + mid = (left + right) // 2 + if get_cost(people, mid) < get_cost(people, mid + 1): + return bisection(people, left, mid) + return bisection(people, mid + 1, right) + + +N = int(input()) +people = [] + +for i in range(N): + P, W, D = tuple(map(int, input().split())) + people.append(Person(P, W, D)) + +people.sort(key=lambda x: x.pos) +print(get_cost(people, bisection(people, 0, people[-1].pos)))