Two Pointers Easy (En)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

MCO 2023

Malaysia, April 16, 2023

Problem A. Two Pointers (easy version)


Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 256 megabytes

Alice and Bob are visiting cities on a very long road that stretches from points −109 to 109 . Alice starts
at point A while Bob starts at point B.
There are n cities to visit, where the i-th city is at point ti . Each city must be visited by Alice or Bob at
least once, but they can be visited in any order.
What is the minimum total distance Alice and Bob travel?

Input
Each test consists of multiple test cases. The first line contains a single integer T (1 ≤ T ≤ 100), the
number of test cases. Each test case is formatted as follows:
The first line contains three space-separated integers n, A, and B (1 ≤ n ≤ 2 · 105 , −109 ≤ A, B ≤ 109 )
– the number of cities, Alice’s position, and Bob’s position, respectively.
The second line contains n space-separated integers t1 , t2 , . . . , tn (−109 ≤ ti ≤ 109 ) – the positions of the
cities.
It is guaranteed that the sum of n over all test cases is at most 2 · 105 .

Output
For each test case, print the answer on a separate line.
Output the minimum total distance that Alice and Bob must travel to visit all cities.

Scoring
Subtask 1 (16 points): n ≤ 20, −106 ≤ A, B, ti ≤ 106
Subtask 2 (36 points): n ≤ 5000, −106 ≤ A, B, ti ≤ 106
Subtask 3 (21 points): n ≤ 5000
Subtask 4 (27 points): No additional constraints

Example
standard input standard output
4 24
7 -6 10 2000000000
-15 -1 12 8 11 -6 0 3
2 -1000000000 -1000000000 413
1000000000 -1000000000
1 4 6
1
4 727 137
39 852 201 696

Note
In the first test case: There are 7 cities. Alice starts at coordinate −6 and Bob starts at point 10.

Page 1 of 8
MCO 2023
Malaysia, April 16, 2023

(Use this link if the image is not clear: https://ibb.co/RSgbbJW)


x
One possible optimal way to visit all cities is as follows (i −
→ j means to go from i to j, driving x distance):

0 9
• Alice visits the cities (given in order): A →
− city 6 →
− city 1.
1 1 4 8 1
• Bob visits the cities (given in order): B →
− city 5 →
− city 3 →
− city 4 →
− city 7 →
− city 2.

Alice drives for a total of 0 + 9 = 9 distance and Bob drives for a total of 1 + 1 + 4 + 8 + 1 = 15 distance.
The total distance driven by both Alice and Bob is 9 + 15 = 24. It can be proven that there is no way to
drive less than 24 distance, thus the answer is 24.
In the second test case, Alice and Bob are both already at city 2. Bob can visit the city 2 then city 1,
driving 2, 000, 000, 000 total distance. Note that Alice can choose to do nothing.
In the third test case, Alice can visit the only city, driving from point 4 to point 1 for 3 distance. Bob
does nothing.

Page 2 of 8
MCO 2023
Malaysia, April 16, 2023

Problem B. Love Letter


Input file: standard input
Output file: standard output
Time limit: 2.5 seconds
Memory limit: 256 megabytes

There are n dragons numbered from 1 to n. Dragon i has age ai . The values of ai are distinct. Dragon
1 is Evirir the dragon.
Evirir has a letter it wants to send to dragon t. To avoid awkwardness caused by age difference, Evirir
can send the letter to another dragon (not necessarily dragon t). The dragon who received the letter can
then send the letter to another dragon, and so on. The goal is to eventually send the letter to dragon t.
For all i (1 ≤ i ≤ n), when dragon i has the letter, it can send the letter to dragon j in |ai − aj | time1 .
A dragon can “send” the letter to itself in 0 time. However, there are k pairs of dragons who are close
friends. If dragons i and j are close friends, then it takes 0 time instead for dragon i to send a letter to
dragon j (and vice versa).
For each dragon t (1 ≤ t ≤ n), answer the question (independently): What is the minimum total time
needed for dragon 1 to send a letter to dragon t?
1 Note:|x| denotes the absolute value of x. For example, |9| = 9, |−6| = 6, and |0| = 0. See
https://en.wikipedia.org/wiki/Absolute_value for more information.

Input
The first line contains two space-separated integers n and k (1 ≤ n ≤ 2 · 105 , 0 ≤ k ≤ 2 · 105 ) – the number
of dragons and the number of pairs of close friends.
The second line contains n distinct space-separated integers a1 , a2 , . . . , an (1 ≤ ai ≤ 109 ) – the dragons’
ages.
Then, k lines follow. Each of the k lines contains two space-separated integers u and v (1 ≤ u, v ≤ n,
u 6= v), which means that dragons u and v are close friends. It is guaranteed that the same pair of close
friends will not appear twice (if (u, v) appears, then (u, v) and (v, u) will not appear afterwards).

Output
Output n space-separated integers d1 , d2 , . . . , dn , where di is the minimum total time needed for dragon
1 to send the letter to dragon i.

Scoring
Subtask 1 (18 points): n, k ≤ 2000, ai = i
Subtask 2 (14 points): n, k ≤ 2000
Subtask 3 (9 points): k = 0
Subtask 4 (29 points): ai = i for all i (1 ≤ i ≤ n)
Subtask 5 (16 points): The sequence a is non-decreasing, i.e. ai ≤ ai+1 for 1 ≤ i ≤ n − 1
Subtask 6 (14 points): No additional constraints

Page 3 of 8
MCO 2023
Malaysia, April 16, 2023

Examples
standard input standard output
8 4 0 7 0 7 14 2 0 3
50 30 23 10 3 67 69 47
3 7
3 1
2 4
7 1
3 0 0 1 1
2 3 1

Note
Explanation for the first sample:
When t = 1, it takes 0 time because dragon 1 already has the letter.
When t = 3, since dragon 1 and 3 are close friends, dragon 1 can send the letter directly to dragon 3 in 0
time.
When t = 2, dragon 1 can send the letter to dragon 3 first in 0 time (they are close friends), then dragon
3 sends the letter to dragon 2 in |23 − 30| = 7 time. The total time taken is 0 + 7 = 7.
When t = 8, dragon 1 can just send the letter directly to dragon 8, taking |50 − 47| = 3 time.
x
Here is one optimal way each for the remaining t’s (i −
→ j means dragon i sends the letter to dragon j
using x time):

0 7 0
• Dragon 4: 1 →
− 3→
− 2→
− 4
0 7 0 7
• Dragon 5: 1 →
− 3→
− 2→
− 4→
− 5
0 0 2
• Dragon 6: 1 →
− 3→
− 7→
− 6
0
• Dragon 7: 1 →
− 7

Page 4 of 8
MCO 2023
Malaysia, April 16, 2023

Problem C. Two Pointers (hard version)


Input file: standard input
Output file: standard output
Time limit: 2 seconds
Memory limit: 256 megabytes

Alice and Bob are driving on a very long road that stretches from points −109 to 109 . Alice starts at point
A while Bob starts at point B. There are n events to visit, where event i is at position ti . Either Alice
or Bob must visit each event, but they must be visited in order (they must visit event 1, then event 2,
then event 3, . . . then event n).
Find the minimum total distance Alice and Bob can drive to visit all events.

Input
The first line contains a single integer n (1 ≤ n ≤ 3 · 105 ) — the number of events.
The second line contains two integers A and B (−109 ≤ A, B ≤ 109 ) — Alice and Bob’s starting points.
The third line contains n integers t1 , t2 , . . . , tn (−109 ≤ ti ≤ 109 ) — the locations of events either Alice or
Bob must get to.

Output
Output an integer — the minimum total distance Alice and Bob drive.

Scoring
Subtask 1 (5 points) |ti |, |A| ≤ 1000, B = 109
Subtask 2 (8 points) n ≤ 20
Subtask 3 (19 points) n ≤ 3000
Subtask 4 (12 points) n ≤ 105 , |ti |, |A|, |B| ≤ 100
Subtask 5 (43 points) |ti |, |A|, |B| ≤ 2 · 105
Subtask 6 (13 points) No additional constraints

Examples
standard input standard output
5 7
2 3
5 1 4 4 7
6 526
540 152
450 600 532 496 325 336
8 1699
35 315
-406 -543 114 205 -840 161 540 -731

Note
In the first example:

• Bob moves from position 3 to position 5 to attend event 1, driving 2 units.

• Alice moves from position 2 to position 1 to attend event 2, driving 1 unit.

Page 5 of 8
MCO 2023
Malaysia, April 16, 2023

• Bob moves from position 5 to position 4 for event 3, driving 1 unit.

• Bob stays at position 4, attending event 4, driving 0 units.

• Bob moves from position 4 to position 7 for event 5, driving 3 units.

The total distance travelled is 2 + 1 + 1 + 0 + 3 = 7.


In the second example, Alice visits all events.

Page 6 of 8
MCO 2023
Malaysia, April 16, 2023

Problem D. Segment Union


Input file: standard input
Output file: standard output
Time limit: 5 seconds
Memory limit: 512 megabytes

There are N positive integers x1 , x2 , . . . , xN and another N positive integers a1 , a2 , . . . , aN .


Let P = (p1 , p2 , . . . , pN ) be a permutation of {1, 2, . . . , N }. Initially, the entire number line is white. For
each 1 ≤ i ≤ N , the segment [xi − api , xi + api ] is colored black. f (P ) is then defined as the total length of
black segments on the number line. For example, if [1, 3], [2, 4], [6, 7] is colored black, then the total length
of black segments is 4 − 1 + 7 − 6 = 4.
Find the sum of f (P ) over all permutations P of {1, 2, . . . , N }, modulo 109 + 7.

Input
The first line of input contains a single integer N (1 ≤ N ≤ 1500).
The second line of input contains N space-separated integers x1 , x2 , . . . , xN (−109 ≤ xi ≤ 109 ).
The third line of input contains N space-separated integers a1 , a2 , . . . , aN (1 ≤ ai ≤ 109 ).

Output
Output a single integer, the sum of f (P ) over all permutations P of {1, 2, . . . , N }, modulo 109 + 7.

Scoring
Subtask 1 (7 points): All ai are equal, i.e. ai = a1 for all i (1 ≤ i ≤ n)
Subtask 2 (8 points): N ≤ 9, −2000 ≤ xi , ai ≤ 2000
Subtask 3 (31 points): N ≤ 300, −105 ≤ xi , ai ≤ 105
Subtask 4 (17 points): N ≤ 300
Subtask 5 (25 points): −105 ≤ xi , ai ≤ 105
Subtask 6 (12 points): No additional constraints

Page 7 of 8
MCO 2023
Malaysia, April 16, 2023

Examples
standard input standard output
3 78
2 6 15
1 2 4
1 14
1
7
4 240
7 2 7 2
3 2 1 2
7 181440
1 1 2 9 17 26 30
4 4 4 4 4 4 4
11 862900292
257869734 -413759255 671386528 312442221 -479133479 837936940 -775252592 -785229024 -306462979 68
987323333 202379759 242380132 464003610 240120482 288801746 7692451 552912477 795257073 629515685
9 616426169
0 0 -2000 396 727 999 999 1300 2000
26 268 268 396 561 604 883 998 999

Note
Sample 1: There are 3! = 6 permutations of length 3. Let p be the permutation.

• p = (1, 2, 3): the segments are [1, 3], [4, 8], [11, 19], total length = 14.

• p = (1, 3, 2): the segments are [1, 3], [2, 10], [13, 17], total length = 13.

• p = (2, 1, 3): the segments are [0, 4], [5, 7], [11, 19], total length = 14.

• p = (2, 3, 1): the segments are [0, 4], [2, 10], [14, 16], total length = 12.

• p = (3, 1, 2): the segments are [−2, 6], [5, 7], [13, 17], total length = 13.

• p = (3, 2, 1): the segments are [−2, 6], [4, 8], [14, 16], total length = 12.

The answer is 14 + 13 + 14 + 12 + 13 + 12 = 78.


Sample 2: There is only one permutation, and the only segment is [−6, 8]. The answer is 8 − (−6) = 14.
Sample 3: Note that there may be duplicate values. Different permutations may create the same a sequence,
and you should still count them multiple times (as though as they are different).
Sample 4: This fits the constraints of Subtask 1.
Sample 5: Remember to output the answer modulo 109 + 7.

Page 8 of 8

You might also like