#P1081. [NOIP 2012 提高组] 开车旅行
[NOIP 2012 提高组] 开车旅行
Description
Little and Little decide to travel during the holidays. They number the cities from to , where a city with a smaller index lies to the west of a city with a larger index. The altitudes of all cities are distinct. Let the altitude of city be . The distance between city and city is exactly the absolute difference of their altitudes, i.e., .
During the trip, Little and Little take turns driving. Little drives on the first day, and they alternate each subsequent day. They plan to choose some city as the starting point, always drive eastward, and end the trip after driving at most kilometers in total.
Little and Little have different driving styles. Little always chooses the nearest city in the forward (east) direction as the destination, while Little always chooses the second nearest city in the forward direction (note: in this problem, if the distances from the current city to two cities are the same, the city with the lower altitude is considered closer). If either person cannot choose a destination according to their rule, or if reaching the chosen destination would cause the total distance to exceed kilometers, the trip ends.
Before setting off, Little wants to know two things:
-
For a given , from which city should they start so that the ratio of the total distance driven by Little to that driven by Little is minimized (if Little 's total distance is , the ratio is considered infinite, and all infinities are regarded equal). If multiple starting cities yield the same minimum ratio, output the city with the highest altitude.
-
For any given and starting city , the total distance driven by Little and the total distance driven by Little .
Input Format
- The first line contains an integer , the number of cities.
- The second line contains integers separated by spaces, representing the altitudes of cities through , i.e., , and all are distinct.
- The third line contains an integer .
- The fourth line contains an integer , the number of given pairs .
- Each of the next lines contains integers and , indicating that the trip starts from city and the total distance is at most kilometers.
Output Format
Output lines.
- The first line contains an integer , indicating that for the given , starting from city minimizes the ratio of the total distance driven by Little to that driven by Little .
- Each of the next lines contains integers separated by a space, indicating, for the given and , the total distance driven by Little and by Little , respectively.
4
2 3 1 4
3
4
1 3
2 3
3 3
4 3
1
1 1
2 0
0 0
0 0
10
4 5 6 1 2 3 7 8 9 10
7
10
1 7
2 7
3 7
4 7
5 7
6 7
7 7
8 7
9 7
10 7
2
3 2
2 4
2 1
2 4
5 1
5 1
2 1
2 0
0 0
0 0
Hint
[Explanation for Sample 1]

The altitudes of the cities and the distances between pairs of cities are as shown above.
If starting from city , the reachable cities ahead are , with distances from city being , respectively. However, since the altitude of city is lower than that of city , we consider city closer to city , and city the second closest, so Little goes to city . After reaching city , the reachable cities ahead are , with distances , respectively, so city is closer to city ; therefore Little goes to city . After reaching city , there are no reachable cities ahead, so the trip ends.
If starting from city , the reachable cities ahead are , with distances , respectively. Since city is the second closest to city , Little goes to city . After reaching city , the only remaining forward city is , so city is the closest to city . However, going to city would make the total distance , so Little ends the trip at city .
If starting from city , the only reachable city ahead is . Since there is no second closest city to city , the trip ends before it starts.
If starting from city , there are no reachable cities ahead, so the trip ends before it starts.
[Explanation for Sample 2]
When , if starting from city , the route is . Little travels , and Little travels . (At city , the cities closest to Little are and , but city has a higher altitude and is regarded as the second closest to city , so Little ultimately chooses city . After reaching , Little can only go to city , and there is no second choice, so no decision can be made and the trip ends.)
If starting from city , the route is . Little and Little travel and , respectively.
If starting from city , the route is . Little and Little travel and , respectively.
If starting from city , the route is . Little and Little travel and , respectively.
If starting from city , the route is . Little and Little travel and , respectively.
If starting from city , the route is . Little and Little travel and , respectively.
If starting from city , the route is . Little and Little travel and , respectively.
If starting from city , the route is . Little and Little travel and , respectively.
If starting from city , the route is . Little and Little travel and , respectively (the trip ends immediately).
If starting from city , the route is . Little and Little travel and , respectively.
Starting from city or city yields the same minimum ratio of Little 's total distance to Little 's total distance, but city has a higher altitude, so the first line of the output is .
Constraints
- For of the testdata: .
- For of the testdata: .
- For of the testdata: .
- For of the testdata: .
- For of the testdata: , , , .
The testdata guarantees that are pairwise distinct.
Translated by ChatGPT 5
京公网安备 11011102002149号