how to solve problem 1993 C ,explain it also.
how to solve problem 1993 C ,explain it also.
| # | User | Rating |
|---|---|---|
| 1 | Benq | 3857 |
| 2 | jiangly | 3810 |
| 3 | maroonrk | 3534 |
| 4 | tourist | 3528 |
| 5 | Kevin114514 | 3510 |
| 6 | turmax | 3411 |
| 7 | Um_nik | 3387 |
| 8 | Radewoosh | 3367 |
| 9 | heuristica | 3322 |
| 10 | strapple | 3317 |
| # | User | Contrib. |
|---|---|---|
| 1 | Qingyu | 158 |
| 2 | maspy | 150 |
| 3 | Um_nik | 146 |
| 4 | Errichto | 139 |
| 5 | adamant | 136 |
| 6 | maroonrk | 134 |
| 7 | DNR | 133 |
| 8 | Dominater069 | 131 |
| 9 | Proof_by_QED | 130 |
| 9 | AmShZ | 130 |
| Name |
|---|



I have same doubt pls help
ask naivedyam mishra (our senior)
Approach explanation:
Each light follows this pattern after installation at time
a[i]:[a[i], a[i] + k - 1][a[i] + k, a[i] + 2k - 1]2kminutes.So the ON intervals for room
iare:First, sort the installation times. Let the latest installation time be:
The latest-installed light is initially ON only in:
Therefore, the earliest answer, if it exists, must be inside this interval. We store its right endpoint:
For every other room, we move the beginning of its ON interval forward by multiples of
2k:Adding
2kdoes not change the light's state pattern. This finds the latest ON-interval starting time of that room which is not greater thanmx.After adjusting all rooms, sort again. Let:
This is the latest starting point among the selected ON intervals. It is the earliest possible time when all those intervals could overlap.
For every room, its selected ON interval ends just before:
Therefore, if:
then that room has already turned OFF at
candidate, so there is no common ON time and the answer is-1.Otherwise,
candidatebelongs to every selected ON interval, so all lights are ON at that moment, and it is the earliest possible answer.Time complexity:
O(n log n)because of sorting. Space complexity:O(n)for storing the array.See code for more clarification
Maybe read the editorial