| 시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
|---|---|---|---|---|---|
| 1 초 | 1024 MB | 40 | 28 | 27 | 75.000% |
Do you know Just Odd Ink Way? It is a national road of length 10ドル^{100}$ in Republic of EGOI from the east end to the west end. It is famous because there are several painting on the road painted by “Just Odd Ink.” In the following, we abbreviate it, and call it JOI Way.
There are several painting of various sizes on JOI Way. Characters are written on some of them.
Rie is a tour guide working on JOI Way. She plans to guide the participants of JOIG Spring Training Camp. In order to cheer the participants, she plans to choose the paintings on which ‘J’, ‘O’, ‘I’, ‘G’ are written, and visit them in this order. There are $N$ candidates of paintings. The $i$-th painting (1ドル ≤ i ≤ N$) is located at the place on JOI Way at a distance of $A_i$ from the west end. In this painting, the character $C_i$ is written.
Rie has $Q$ plans. In the $j$-th plan (1ドル ≤ j ≤ Q$), she will travel as follows.
J’ is written, and moves to its location.O’ is written, and moves to its location.I’ is written, and moves to its location.G’ is written, and moves to its location.During the tour, it is not allowed to go outside JOI Way.
Under the above conditions, Rie wants to minimize the total travel distance for each plan.
Write a program which, given information on the paintings on JOI Way and Rie’s plans, calculates the minimum possible value of the total travel distance for each plan.
Read the following data from the standard input.
$N$
$A_1$ $C_1$
$A_2$ $C_2$
$\vdots$
$A_N$ $C_N$
$Q$
$S_1$ $T_1$
$S_2$ $T_2$
$\vdots$
$S_Q$ $T_Q$
Write $Q$ lines to the standard output. The $j$-th line (1ドル ≤ j ≤ Q$) of the output should contain the minimum possible value of the total travel distance for the $j$-th plan.
J’,‘O’,‘I’,or ‘G’.J’ for at least one $i$ (1ドル ≤ i ≤ N$).O’ for at least one $i$ (1ドル ≤ i ≤ N$).I’ for at least one $i$ (1ドル ≤ i ≤ N$).G’ for at least one $i$ (1ドル ≤ i ≤ N$).| 번호 | 배점 | 제한 |
|---|---|---|
| 1 | 4 | $N ≤ 80,ドル $Q ≤ 10$. |
| 2 | 10 | $N ≤ 500,ドル $Q ≤ 10$. |
| 3 | 6 | $N ≤ 3,000円,ドル $Q ≤ 100$. |
| 4 | 25 | $N ≤ 5,000円,ドル $Q ≤ 1,000円$. |
| 5 | 12 | $C_i$ is equal to ‘ |
| 6 | 8 | $C_i$ is equal to ‘ |
| 7 | 20 | $C_i$ is equal to ‘ |
| 8 | 15 | No additional constraints. |
7 1 J 2 O 3 G 4 I 5 O 8 G 10 J 2 3 2 7 5
7 12
In the first plan, Rie starts a tour from the place on JOI Way at a distance of 3 from the west end, and finishes the tour from the place on JOI Way at a distance of 2 from the west end. For example, if she travels in the following way, the total travel distance becomes 7.
J’.O’.I’.G’.Since 7 is the minimum possible value of the total travel distance, output 7 in the first line.
In the second plan, she starts a tour from the place on JOI Way at a distance of 7 from the west end, and finishes the tour from the place on JOI Way at a distance of 5 from the west end. For example, if she travels in the following way, the total travel distance becomes 12.
J’.O’.I’.G’.Since 12 is the minimum possible value of the total travel distance, output 12 in the second line.
This sample input satisfies the constraints of Subtasks 1, 2, 3, 4, 8.
10 5 J 7 O 10 J 11 G 12 J 13 I 17 J 18 J 19 J 20 J 4 4 9 15 14 6 20 7 20
13 19 20 21
This sample input satisfies the constraints of all the subtasks.
10 1 G 2 J 3 G 4 O 7 G 9 J 10 G 14 I 17 G 19 G 7 11 6 6 3 17 19 1 18 17 17 20 1 20 10
25 27 28 17 26 39 30
This sample input satisfies the constraints of Subtasks 1, 2, 3, 4, 6, 7, 8.
10 3 J 5 G 6 I 7 I 8 J 9 I 10 O 14 G 16 I 19 J 6 4 4 20 3 18 5 15 4 20 11 10 8
12 17 15 15 19 12
This sample input satisfies the constraints of Subtasks 1, 2, 3, 4, 7, 8.
12 179948747891578 I 263779425244614 I 320153642407146 G 383698990675423 J 478483318441339 J 505589213620811 G 507468309040564 O 530441288489671 J 730036011088087 O 896127332008998 I 899298512463927 O 915990785839829 J 10 744829561026263 366031656398270 700496830781726 684771674298690 314138534887378 222241904398827 695615197615084 632164325876673 418419052313523 409258287819812 932490604948180 62799105708059 738126150487131 45378717857226 320047965627255 918203067583346 859632377126681 967370566306944 115848334010451 834089404672067
583302366935305 805077987955000 591304613119987 757352272699625 478217003098189 869691499240121 805495866954969 1085532869547991 928541333618299 1205618838253516
This sample input satisfies the constraints of Subtasks 1, 2, 3, 4, 8.