File size: 5,662 Bytes
e329daf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
<p>
Fred works the night shift in a refrigerator storage warehouse. It's not very exciting work, but Fred has ways to pass the time when nobody's around.
For example, lifting fridges turns out to be an amazing bodybuilding method!
</p>
<p>
The warehouse consists of <strong>N</strong> sections in a row, numbered from 1 to <strong>N</strong>.
In each section <em>i</em>, there are initially <strong>F<sub>i</sub></strong> fridges, all arranged in a single stack.
The sections are intended to be separate from one another, and only accessible from the outside.
To that end, each pair of adjacent sections are separated by a wall, for a total of <strong>N</strong>-1 walls.
However, these walls don't stretch all the way to the ceiling, and aren't necessarily all of the same height.
The wall between sections <em>i</em> and <em>i</em>+1 has a height of <strong>H<sub>i</sub></strong> fridge-heights (Fred has come to measure everything relative to fridge dimensions).
Fred's favourite pastime involves climbing over these walls to get between the warehouse's sections!
</p>
<p>
Fred will begin by entering the warehouse in some section, carrying in some number of new fridges from the outside (yes, he's become strong enough to carry multiple fridges in his arms at once). When he's currently in a certain section <em>s</em> and is carrying <em>f</em> fridges, he may perform any of the following actions:
<p>
<ul style="list-style-type:disc; padding-inline-start: 30px;">
<li>Pick up a fridge from section <em>s</em>'s stack of fridges, if it's non-empty. This decreases the number of fridges in that stack by 1, and increases <em>f</em> by 1.</li>
<li>Add a fridge that he's carrying onto section <em>s</em>'s stack of fridges, if he's carrying at least one fridge.
This decreases <em>f</em> by 1, and increases the number of fridges in that stack by 1. </li>
<li>Climb onto section <em>s</em>'s stack of fridges and jump over a wall into an adjacent section, if the number of fridges in that stack is at least as large as the height of that wall
(in fridge-heights). This decreases or increases <em>s</em> by 1. </li>
</ul>
<p>
Fred's goal is to visit all <strong>N</strong> sections at least once each.
He just needs to decide which section he should initially enter and how many additional fridges he should bring from the outside.
He has <strong>M</strong> such possible starting situations in mind, the <em>i</em>th of which involves him beginning in section
<strong>X<sub>i</sub></strong> while carrying <strong>Y<sub>i</sub></strong> fridges.
For each hypothetical starting situation, please help Fred determine whether or not he will be able to visit all <strong>N</strong> sections!
</p>
<h3>Input</h3>
<p>
Input begins with an integer <strong>T</strong>, the number of warehouses Fred works at.
<br />For each warehouse, there is first a line containing the space-separated integers <strong>N</strong> and <strong>M</strong>.
<br />Then follows a line with the <strong>N</strong> space-separated integers <strong>F<sub>1</sub></strong> through <strong>F<sub>N</sub></strong>.
<br />Then follows a line with the <strong>N</strong> - 1 space-separated integers <strong>H<sub>1</sub></strong> through <strong>H<sub>N-1</sub></strong>.
<br />Then, <strong>M</strong> lines follow, the <em>i</em>th of which contains the space-separated integers <strong>X<sub>i</sub></strong> and <strong>Y<sub>i</sub></strong>.
</p>
<h3>Output</h3>
<p>
For the <em>i</em>th warehouse, print a line containing "Case #<em>i</em>: " followed by
a string of <strong>M</strong> characters, the <em>i</em>th of which is "Y" if Fred can visit all <strong>N</strong> sections from the <em>i</em>th starting situation, or "N" otherwise.
</p>
<h3>Constraints</h3>
<p>
1 ≤ <strong>T</strong> ≤ 90 <br />
2 ≤ <strong>N</strong> ≤ 8,000 <br />
1 ≤ <strong>M</strong> ≤ 8,000 <br />
0 ≤ <strong>F<sub>i</sub></strong> ≤ 100,000 <br />
1 ≤ <strong>H<sub>i</sub></strong> ≤ 100,000 <br />
1 ≤ <strong>X<sub>i</sub></strong> ≤ <strong>N</strong> <br />
0 ≤ <strong>Y<sub>i</sub></strong> ≤ 1,000,000,000 <br />
</p>
<p>
The sum of <strong>N</strong> across all <strong>T</strong> test cases is no greater than 80,000. <br />
The sum of <strong>M</strong> across all <strong>T</strong> test cases is no greater than 80,000.
</p>
<h3>Explanation of Sample</h3>
<p>
In the first case, the warehouse is arranged as follows:
</p>
<img src={{PHOTO_ID:668467847037494}} width="100px" />
<p>
If Fred begins in section 1 holding 0 fridges, he can't climb over the wall to visit section 2, whereas if he's holding 1 fridge, he can place it in section 1 and then climb over.
On the other hand, if he begins in section 2, he can climb over the wall to visit section 1 using the existing fridge, regardless of whether he's holding any himself.
</p>
<p>
In the second case, consider the first starting situation, in which Fred begins in section 3 holding 4 fridges:
</p>
<img src={{PHOTO_ID:3188801037832284}} width="200px" />
<p>
He could begin by placing 3 of his fridges in section 3, and using them to climb over the wall into section 4 while still holding 1 fridge:
</p>
<img src={{PHOTO_ID:268184757597716}} width="200px" />
<p>
He could then place his remaining fridge in section 4, climb back to section 3, pick up a fridge there, and climb over to section 2 while holding that 1 fridge:
</p>
<img src={{PHOTO_ID:281442036550128}} width="200px" />
<p>
Finally, he could place his final fridge in section 2 and climb over to section 1:
</p>
<img src={{PHOTO_ID:657561505101033}} width="200px" />
|