Industrial Hemp
Limits: 2 sec., 256 MiB
Grandpa Panas grows \(n\) hemp bushes on his windowsill. Industrial hemp. Strictly for making rope. That is what he explains every time to the local police officer, who for some reason walks past his house very often.
Each bush grows in its own pot. Initially, the height of the \(i\)-th bush is \(a_i\) centimeters, and all these heights are distinct.
Every morning, Grandpa puts all \(n\) pots in one row on a long windowsill, in any order he likes. The sun shines into the window from the right. A bush gets sunlight if all the bushes to its right are strictly shorter than it. During the day, every bush that got sunlight grows by exactly 1 centimeter, and the others do not grow.
In the evening, Grandpa hides all the pots in the storeroom just in case, and the next morning puts them on the windowsill again, possibly in a different order.
Grandpa does not label the pots, so he does not remember which bush is which. He only cares about the set of heights. His granddaughter Marichka found on the internet that the strongest ropes need bushes of heights exactly \(b_1, b_2, \ldots, b_n\) centimeters (in any order). Help Grandpa find out whether, after several days (possibly zero), he can get exactly this set of heights if he arranges the pots properly every morning.
Formally: can the multiset of heights \(\{a_1, a_2, \ldots, a_n\}\) become equal to the multiset \(\{b_1, b_2, \ldots, b_n\}\) after several days?
Input
The first line contains one integer \(n\) – the number of bushes.
The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) – the initial heights.
The third line contains \(n\) integers \(b_1, b_2, \ldots, b_n\) – the desired heights.
Output
In the only line, print Yes if Grandpa can get the
desired set of heights, or No otherwise.
Constraints
\(1 \le n \le 10^5\),
\(1 \le a_i, b_i \le 10^9\),
all \(a_i\) are pairwise distinct (some \(b_i\) may be equal).
The scoring consists of the following blocks:
1 point for each example from the statement,
10 points: \(n \le 2\),
15 points: \(n \le 5\), \(a_i, b_i \le 8\),
15 points: \(\max(a_1, \ldots, a_n) = \max(b_1, \ldots, b_n)\),
5 points: \(\max(a_1, \ldots, a_n) > \max(b_1, \ldots, b_n)\),
20 points: \(a_1 < a_2 < \ldots < a_n\) and \(b_1 \le b_2 \le \ldots \le b_n\),
31 points: no additional constraints.
You get the points for a block only if your program passes all tests of the block.
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 4 5 2 8 3 3 6 9 4 | Yes |
| Input (stdin) | Output (stdout) |
|---|---|
| 2 4 1 4 2 | No |
| Input (stdin) | Output (stdout) |
|---|---|
| 3 1 2 10 3 12 3 | Yes |
| Input (stdin) | Output (stdout) |
|---|---|
| 3 5 1 7 3 5 8 | No |
Notes
In the first example, one day is enough: if the pots are arranged from left to right as 8, 5, 3, 2, then every bush has only shorter bushes to its right, so all of them get sunlight. The heights become 9, 6, 4, 3, which is exactly the required set.
In the second example, the tallest bush always gets sunlight wherever it stands, so it grows every day. For the height 4 to stay in the set, not a single day may pass, but then the bush of height 1 never becomes a bush of height 2.
In the third example, Grandpa can do the following:
day 1: the arrangement from left to right is 10, 2, 1, all bushes get sunlight, in the evening the heights are 11, 3, 2;
day 2: the arrangement from left to right is 3, 11, 2, the bushes of heights 11 and 2 get sunlight, in the evening the heights are 3, 12, 3.
On the second day, the bush of height 3 did not get sunlight, because a taller bush stands to its right.
In the fourth example, the tallest bush has to grow from 7 to 8, so exactly one day has passed. In one day, every bush grows by at most 1, so the bush of height 1 will have height 1 or 2, and there are no such heights in the desired set.
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
Технічні коноплі - Розбір
Кількість днів. Оскільки всі \(a_i\) різні, найвищий кущик один, і
праворуч від нього можуть стояти лише нижчі кущики. Тому він отримує
сонце щодня, де б його не поставили. Інші кущики за день виростають
щонайбільше на 1, тож він і далі залишається єдиним найвищим. Отже, за
кожен день найбільша висота зростає рівно на 1, і кількість днів
визначена однозначно: \[D = \max(b_1, \ldots,
b_n) - \max(a_1, \ldots, a_n).\] Якщо \(D < 0\), відповідь No.
Необхідна умова. Відсортуємо обидва масиви за зростанням. Якщо кожне число набору збільшити на 0 або на 1, то й \(i\)-те найменше число набору збільшиться на 0 або на 1. Тому за \(D\) днів \(i\)-та найменша висота збільшиться щонайменше на 0 і щонайбільше на \(D\), тобто для кожного \(i\) має виконуватися \[a_i \le b_i \le a_i + D.\]
Ця умова й достатня. Нехай кущик з початковою висотою \(a_i\) росте в перші \(b_i - a_i\) днів. У день номер \(t\) ростуть кущики, для яких \(b_i - a_i \ge t\), і на початку цього дня їхні висоти дорівнюють \(a_i + t - 1\). Ці висоти попарно різні, бо всі \(a_i\) різні, а серед цих кущиків є найвищий, бо для нього \(b_n - a_n = D \ge t\). Поставимо ці кущики в правому кінці підвіконня за спаданням висоти, а решту — ліворуч від них. Тоді кожен кущик правої групи має праворуч лише нижчі кущики й отримує сонце, а кожен кущик лівої групи має праворуч найвищий кущик і сонця не отримує. Через \(D\) днів кущик з висотою \(a_i\) матиме висоту \(a_i + (b_i - a_i) = b_i\), тобто отримаємо саме потрібний набір.
Отже, відповідь Yes тоді й лише тоді, коли \(D \ge 0\) і \(0
\le b_i - a_i \le D\) для всіх \(i\) (масиви \(a\) і \(b\) відсортовані).
Складність розв’язку — \(O(n \log n)\) через сортування.
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|