#P9066. [yLOI2023] 腐草为萤
[yLOI2023] 腐草为萤
Background
At the end of midsummer, the night is still scorching.
Another parting and reunion begins in sorrow.
Is it hiding under a fan, or crushed by rain.
Everywhere is worth it, unwilling to let go.
— Yin Lin, Rotting Grass Turns Into Fireflies
Problem Description
As night falls, on a straight path in the woods, fireflies respond to the call of the night and go out to move around.
Treat the path as a number line. Initially, there are fireflies located at some integer points on the number line, labeled from left to right as . The initial coordinate of firefly is . Each firefly has a different brightness value, and the brightness of firefly is .
At any moment, for any surviving firefly , it flies according to the following rules:
- Among the fireflies adjacent to (there may be one or two) that are still alive, find the one with the maximum brightness, and denote its index as . If , then flies toward ; otherwise, stays in place.
- Here, two fireflies are defined to be “adjacent” if there is no other surviving firefly between them.
- All fireflies fly at a speed of 1 unit length per second.
Fireflies have short lives. When two fireflies meet (i.e., their coordinates become the same), the firefly with the lower brightness will run out of life and disappear from the path. Clearly, in the end only 1 firefly will remain. For each of the other fireflies, find the coordinate where it runs out of life.
Input Format
The first line contains an integer, representing the number of fireflies .
The second line contains integers, where the -th integer represents the initial coordinate of firefly . The testdata guarantees that is strictly increasing.
The third line contains integers, where the -th integer represents the brightness value of firefly . The testdata guarantees that all brightness values are distinct.
Output Format
Output one line with integers separated by single spaces. The -th integer represents the coordinate where firefly runs out of life. If firefly survives to the end, output 0 for the -th number.
4
1 2 3 4
2 3 1 4
2 4 4 0
5
1 2 3 4 5
5 3 2 1 4
0 1 1 5 1
5
2 4 6 10 12
5 3 1 4 2
0 2 2 2 10
7
2 4 6 8 12 14 16
5 3 2 6 1 4 7
8 2 8 16 16 16 0
7
2 4 6 8 12 14 16
7 1 6 3 5 4 2
0 2 2 6 2 12 2
Hint
Explanation of Sample 1
- In the first second, firefly moves to the right, firefly stays still, firefly moves to the right, and firefly stays still.
- At the beginning of the second second, firefly meets firefly . The former has lower brightness and runs out of life, and its coordinate is . Firefly meets firefly . The former has lower brightness and runs out of life, and its coordinate is .
- Next, firefly moves to the right until it meets firefly at coordinate , and then runs out of life.
Constraints
- For of the testdata, .
- For of the testdata, , .
- For of the testdata, .
- Another of the testdata satisfies Special Condition A.
- Another of the testdata satisfies Special Condition B.
- For of the testdata, it is guaranteed that , , . Also, , and is a permutation of length .
Where:
- Special Condition A: the sequence is strictly increasing.
- Special Condition B: the sequence is unimodal with only one maximum. That is, there exists satisfying such that is strictly increasing, and is strictly decreasing.
Notes
- Please note that input and output on large testdata may affect program efficiency. Choose appropriate I/O methods to avoid TLE.
- Please note that the constant factor in time complexity may affect program running efficiency.
Additional Information
There are 7 additional sample files for this problem. See glowworm.zip in the attachments.
Translated by ChatGPT 5