#P1081. [NOIP 2012 提高组] 开车旅行

[NOIP 2012 提高组] 开车旅行

Description

Little A\text{A} and Little B\text{B} decide to travel during the holidays. They number the cities from 11 to nn, 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 ii be hih_i. The distance di,jd_{i,j} between city ii and city jj is exactly the absolute difference of their altitudes, i.e., di,j=hihjd_{i,j}=|h_i-h_j|.

During the trip, Little A\text{A} and Little B\text{B} take turns driving. Little A\text{A} drives on the first day, and they alternate each subsequent day. They plan to choose some city ss as the starting point, always drive eastward, and end the trip after driving at most xx kilometers in total.

Little A\text{A} and Little B\text{B} have different driving styles. Little B\text{B} always chooses the nearest city in the forward (east) direction as the destination, while Little A\text{A} 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 xx kilometers, the trip ends.

Before setting off, Little A\text{A} wants to know two things:

  1. For a given x=x0x=x_0, from which city should they start so that the ratio of the total distance driven by Little A\text{A} to that driven by Little B\text{B} is minimized (if Little B\text{B}'s total distance is 00, 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.

  2. For any given x=xix=x_i and starting city sis_i, the total distance driven by Little A\text{A} and the total distance driven by Little B\text{B}.

Input Format

  • The first line contains an integer nn, the number of cities.
  • The second line contains nn integers separated by spaces, representing the altitudes of cities 11 through nn, i.e., h1,h2,,hnh_1,h_2,\dots,h_n, and all hih_i are distinct.
  • The third line contains an integer x0x_0.
  • The fourth line contains an integer mm, the number of given pairs (si,xi)(s_i,x_i).
  • Each of the next mm lines contains 22 integers sis_i and xix_i, indicating that the trip starts from city sis_i and the total distance is at most xix_i kilometers.

Output Format

Output m+1m+1 lines.

  • The first line contains an integer s0s_0, indicating that for the given x0x_0, starting from city s0s_0 minimizes the ratio of the total distance driven by Little A\text{A} to that driven by Little B\text{B}.
  • Each of the next mm lines contains 22 integers separated by a space, indicating, for the given sis_i and xix_i, the total distance driven by Little A\text{A} and by Little B\text{B}, 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 11, the reachable cities ahead are 2,3,42,3,4, with distances from city 11 being 1,1,21,1,2, respectively. However, since the altitude of city 33 is lower than that of city 22, we consider city 33 closer to city 11, and city 22 the second closest, so Little A\text{A} goes to city 22. After reaching city 22, the reachable cities ahead are 3,43,4, with distances 2,12,1, respectively, so city 44 is closer to city 22; therefore Little B\text{B} goes to city 44. After reaching city 44, there are no reachable cities ahead, so the trip ends.

If starting from city 22, the reachable cities ahead are 3,43,4, with distances 2,12,1, respectively. Since city 33 is the second closest to city 22, Little A\text{A} goes to city 33. After reaching city 33, the only remaining forward city is 44, so city 44 is the closest to city 33. However, going to city 44 would make the total distance 2+3=5>32+3=5>3, so Little B\text{B} ends the trip at city 33.

If starting from city 33, the only reachable city ahead is 44. Since there is no second closest city to city 33, the trip ends before it starts.

If starting from city 44, there are no reachable cities ahead, so the trip ends before it starts.

[Explanation for Sample 2]

When x=7x=7, if starting from city 11, the route is 123891 \to 2 \to 3 \to 8 \to 9. Little A\text{A} travels 1+2=31+2=3, and Little B\text{B} travels 1+1=21+1=2. (At city 11, the cities closest to Little A\text{A} are 22 and 66, but city 22 has a higher altitude and is regarded as the second closest to city 11, so Little A\text{A} ultimately chooses city 22. After reaching 99, Little A\text{A} can only go to city 1010, and there is no second choice, so no decision can be made and the trip ends.)

If starting from city 22, the route is 2672 \to 6 \to 7. Little A\text{A} and Little B\text{B} travel 22 and 44, respectively.

If starting from city 33, the route is 3893 \to 8 \to 9. Little A\text{A} and Little B\text{B} travel 22 and 11, respectively.

If starting from city 44, the route is 4674 \to 6 \to 7. Little A\text{A} and Little B\text{B} travel 22 and 44, respectively.

If starting from city 55, the route is 5785 \to 7 \to 8. Little A\text{A} and Little B\text{B} travel 55 and 11, respectively.

If starting from city 66, the route is 6896 \to 8 \to 9. Little A\text{A} and Little B\text{B} travel 55 and 11, respectively.

If starting from city 77, the route is 79107 \to 9 \to 10. Little A\text{A} and Little B\text{B} travel 22 and 11, respectively.

If starting from city 88, the route is 8108 \to 10. Little A\text{A} and Little B\text{B} travel 22 and 00, respectively.

If starting from city 99, the route is 99. Little A\text{A} and Little B\text{B} travel 00 and 00, respectively (the trip ends immediately).

If starting from city 1010, the route is 1010. Little A\text{A} and Little B\text{B} travel 00 and 00, respectively.

Starting from city 22 or city 44 yields the same minimum ratio of Little A\text{A}'s total distance to Little B\text{B}'s total distance, but city 22 has a higher altitude, so the first line of the output is 22.

Constraints

  • For 30%30\% of the testdata: 1n20,1m201 \le n \le 20, 1 \le m \le 20.
  • For 40%40\% of the testdata: 1n100,1m1001 \le n \le 100, 1 \le m \le 100.
  • For 50%50\% of the testdata: 1n100,1m10001 \le n \le 100, 1 \le m \le 1000.
  • For 70%70\% of the testdata: 1n1000,1m1041 \le n \le 1000, 1 \le m \le 10^4.
  • For 100%100\% of the testdata: 1n,m1051 \le n,m \le 10^5, 109hi109-10^9 \le h_i≤10^9, 1sin1 \le s_i \le n, 0xi1090 \le x_i \le 10^9.

The testdata guarantees that hih_i are pairwise distinct.

Translated by ChatGPT 5