]> Skullheadx's Git Forge - CCC.git/commitdiff
2021 S3
authorSkullheadx <admonty1@gmail.com>
Fri, 3 Feb 2023 20:35:08 +0000 (15:35 -0500)
committerSkullheadx <admonty1@gmail.com>
Fri, 3 Feb 2023 20:35:08 +0000 (15:35 -0500)
Main/Python/2021/S3.py [new file with mode: 0644]

diff --git a/Main/Python/2021/S3.py b/Main/Python/2021/S3.py
new file mode 100644 (file)
index 0000000..245a6e0
--- /dev/null
@@ -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)))