Keep It Beautiful — Circular Non-Decreasing Array Construction
Автор: BitChaos
Загружено: 2026-02-21
Просмотров: 4
Описание:
You are given an initially empty array and must process a sequence of queries. For each query, you receive an integer x. You may append x to the array only if the resulting array remains beautiful.
An array is called beautiful if it is possible to remove some (possibly zero) elements from the beginning and append them to the end — preserving order — such that the resulting array becomes non-decreasing.
In other words, the array must be a circular shift of a non-decreasing sequence.
While processing each query:
Append x if the array can still be rotated to become non-decreasing.
Otherwise, ignore it.
After each query, output 1 if appended, 0 otherwise.
Key Insight
A beautiful array can contain at most one “drop” — a position where the next element is smaller than the previous one.
Before the drop, elements must be non-decreasing.
After the drop, elements must remain within valid circular bounds to avoid creating a second drop.
This allows an efficient O(q) solution per test case using a simple simulation and a boolean flag to track whether the drop has occurred.
Повторяем попытку...
Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: