Datasets:

Modalities:
Image
Text
Formats:
parquet
Size:
< 1K
Tags:
code
Libraries:
Datasets
pandas
License:
hackercup / 2017 /finals /moles.html
wjomlex's picture
2017 Problems
7acee6b verified
<p>
It's dinner time!
A group of <strong>N</strong> Foxen are standing silently in a field, which can be represented as an infinite number line,
patiently waiting for their meals to make an appearance.
The <em>i</em>th Fox is standing at position <strong>P<sub>i</sub></strong>,
with no two Foxen standing at the same position.
There's also one hole in the ground at each each integral position on the number line.
Each of these holes is the entrance to a mole's den, and the Foxen know that some of these delicious critters are
bound to show up sooner or later!
</p>
<p>
A little-known fact about Foxen is that, in addition to having an acute array of regular senses,
they possess a SONAR-like ability to emit imperceptible sound waves and use them to discern objects at great distances.
The <em>i</em>th Fox has tuned their wavelength to a distance of <strong>R<sub>i</sub></strong>,
allowing them to only detect moles which emerge from holes at a distance of exactly <strong>R<sub>i</sub></strong> away from them
(that is, at either position <strong>P<sub>i</sub></strong> - <strong>R<sub>i</sub></strong> or
<strong>P<sub>i</sub></strong> + <strong>R<sub>i</sub></strong>).
</p>
<p>
All of a sudden, some number of moles have just popped up from various holes all at once!
No mole popped up at any Fox's position, no two moles popped up from the same hole, and
every mole was detected by at least one Fox.
Furthermore, each Fox <em>i</em> has determined that there's <i>exactly</i> 1 mole
at a distance of <strong>R<sub>i</sub></strong> away from it (as opposed to there being either 0 or 2 such moles).
</p>
<p>
Following this initial event, there's been quite some commotion.
Some moles may have retreated back underground, and some new moles may have emerged, all in any order.
At every point in time, the set of moles on the surface is subject to all of the same restrictions as before, with one difference:
Each Fox <em>i</em> continues to be sure that <i>at least</i> 1 mole is still present
at a distance of <strong>R<sub>i</sub></strong> away from it, but can no longer determine whether or not
there are perhaps now 2 such moles instead.
</p>
<p>
After some time of this, the Foxen have decided that they're ready to pounce and "invite" some of the moles
currently on the surface over for dinner.
Unfortunately, they've started to become rather overwhelmed with trying to keep track of which moles
may be on the surface, or even roughly how many of them there might be.
Assuming that the Foxen's initial observations were correct, and that some unknown amount of time has since gone by
with moles surfacing or departing, please help the Foxen determine the number of different quantities of moles which
could possibly have ended up on the surface.
</p>
<p>
If it's impossible for their set of initial observations to have been accurate in the first place, output -1 instead.
</p>
<h3>Input</h3>
<p>
Input begins with an integer <strong>T</strong>, the number of different fields.
For each field, there is first a line containing the integer <strong>N</strong>.
Then <strong>N</strong> lines follow, the <em>i</em>th of which contains the space-separated integers
<strong>P<sub>i</sub></strong> and <strong>R<sub>i</sub></strong>.
</p>
<h3>Output</h3>
<p>
For the <em>i</em>th field, print a line containing "Case #<strong>i</strong>: "
followed by a single integer, the number of different quantities of moles which could possibly end up on the surface at any point,
or -1 if the Foxen's initial observations must have been inaccurate.
</p>
<h3>Constraints</h3>
<p>
1 &le; <strong>T</strong> &le; 30<br />
1 &le; <strong>N</strong> &le; 5,000 <br />
0 &le; <strong>P<sub>i</sub></strong> &le; 1,000,000,000 <br />
1 &le; <strong>R<sub>i</sub></strong> &le; 1,000,000,000 <br />
</p>
<h3>Explanation of Sample</h3>
<p>
In the first case, it's possible for there to eventually be 1 mole (at either position -1 or 1), or 2 moles (at both positions -1 and 1). There can't be 0 moles due to the restriction that the Fox must detect at least 1 of them, and there can't be more than 2 moles as they'd have to be at positions which the Fox is unable to detect.
</p>
<p>
In the third case, it's impossible for a set of moles to have initially popped up such that each Fox would have detected <em>exactly</em> one of them.
</p>