CCC '21 S3 - Lunch Concert

View as PDF

Submit solution


Points: 10 (partial)
Time limit: 3.0s
Memory limit: 1G

Author:
Problem type
Canadian Computing Competition: 2021 Stage 1, Senior #3

It's lunchtime at your school! Your N friends are all standing on a long field, as they usually do. The field can be represented as a number line, with the i^\text{th} friend initially at position P_i metres along it. The i^\text{th} friend is able to walk in either direction along the field at a rate of one metre per W_i seconds, and their hearing is good enough to be able to hear music up to and including D_i metres away from their position. Multiple students may occupy the same positions on the field, both initially and after walking.

You're going to hold a little concert at some position c metres along the field (where c is any integer of your choice), and text all of your friends about it. Once you do, each of them will walk along the field for the minimum amount of time such that they end up being able to hear your concert (in other words, such that each friend i ends up within D_i units of c).

You'd like to choose c such that you minimize the sum of all N of your friends' walking times. What is this minimum sum (in seconds)? Please note that the result might not fit within a 32-bit integer.

Input Specification

The first line of input contains N.
The next N lines contain three space-separated integers, P_i, W_i, and D_i (1 \le i \le N).

The following table shows how the available 15 marks are distributed.

Subtask N P_i W_i D_i
4 marks 1 \le N \le 2\,000 0 \le P_i \le 2\,000 1 \le W_i \le 1\,000 0 \le D_i \le 2\,000
9 marks 1 \le N \le 200\,000 0 \le P_i \le 1\,000\,000 1 \le W_i \le 1\,000 0 \le D_i \le 1\,000\,000
2 marks 1 \le N \le 200\,000 0 \le P_i \le 1\,000\,000\,000 1 \le W_i \le 1\,000 0 \le D_i \le 1\,000\,000\,000

Output Specification

Output one integer which is the minimum possible sum of walking times (in seconds) for all N of your friends to be able to hear your concert.

Sample Input 1

1
0 1000 0

Output for Sample Input 1

0

Explanation of Output for Sample Input 1

If you choose c = 0, your single friend won't need to walk at all to hear it.

Sample Input 2

2
10 4 3
20 4 2

Output for Sample Input 2

20

Explanation of Output for Sample Input 2

One possible optimal choice of c is 14, which would require your first friend to walk to position 11 (taking 4 \times 1 = 4 seconds) and your second friend to walk to position 16 (taking 4 \times 4 = 16 seconds), for a total of 20 seconds.

Sample Input 3

3
6 8 3
1 4 1
14 5 2

Output for Sample Input 3

43

Comments


  • -4
    hostsjim  commented on Jan. 19, 2024, 2:41 p.m. edit 7

    I found a way to AC without binary search and with time time complexity of o(n) just consider a function like this: f(x) = 1/2 * W * |x-(P-D)| + 1/2 * W * |x-(P+D)| -W * D this means on point can be considered as two points with half of the weight and D = 0. Then finally minus WD when you calculate the time but if you directly calculate WD's sum will cause numerical overflow so it will be better to minus 1/2WD each time

    https://dmoj.ca/src/6088148


    • 2
      d  commented on Jan. 20, 2024, 3:08 a.m.

      Unfortunately, your code is \mathcal O(N \log N) because of a = sorted(a, key=lambda x: x[0])


    • 6
      tyoueenn  commented on Jan. 20, 2024, 12:34 a.m.

      I would recommend you to post it in the editorial instead of here


  • -17
    kopichiki  commented on Aug. 13, 2022, 7:02 p.m. edit 2

    This comment is hidden due to too much negative feedback. Show it anyway.


    • 9
      omaewamoushindeiru  commented on Aug. 16, 2022, 6:43 a.m. edited

      because it is printing the wrong answer or taking too long to print the wrong answer.