Достоверно известно, что перед первым днем замеров снега не было вовсе, а далее на протяжении всего периода замеров за день выпадало положительное целое число миллиметров снега. На некоторых листы журнала замеров оказались неразличимые кляксы.
В первой строке входных данных содержится число n (1 ≤ n ≤ 10^5) — количество дней, на протяжении которых проводились замеры.
Во второй строке содержатся n целых чисел ai, разделенных пробелом. ai равно -1, если соответствующий лист нечитаемый, а иначе это число, записанное на 2-м листе, в этом случае ai не превосходит 10^9.
Если в журнале была допущена ошибка, выведите «NO». В противном случае, в первой строке выведите «YES», а во второй строке выведите n натуральных чисел от 1 до 10^9, i-е из которых равняется количеству выпавшего снега в i-й день.
Замечание!!!
Ошибок в примерах нет. Внимательно перечитайте задание и пойми в чем особенность этого условия и самого задания.
Assistant
To solve this problem, we need to reconstruct the daily snowfall amounts based on the given (possibly incomplete) cumulative snowfall measurements and verify if a valid sequence of daily snowfalls exists that satisfies the conditions.
Understanding the Problem:
Initial Conditions:
Before the first day, the cumulative snowfall was 0 millimeters.
Each day, a positive integer number of millimeters of snow fell.
Given Data:
For each day ( i ), we have ( a_i ):
If ( a_i = -1 ), the cumulative snowfall measurement for day ( i ) is unreadable.
If ( a_i \neq -1 ), ( a_i ) is the cumulative snowfall after day ( i ).
Objective:
Determine if it's possible to assign positive integers to the daily snowfall amounts ( s_i ) such that:
The cumulative sums at days with known measurements match the given ( a_i ).
Each daily snowfall ( s_i ) is at least 1.
If possible, output "YES" and the sequence ( s_1, s_2, \dots, s_n ).
If not, output "NO".
Approach:
Identify Known Cumulative Sums:
Create a list of days where ( a_i \neq -1 ) (known cumulative sums).
Verify Feasibility Between Known Points:
For each pair of consecutive known cumulative sums at days ( l ) and ( r ) (where ( l < r )):
Compute the required total snowfall ( \text{required_sum} = a_r - a_l ).
Compute the minimum possible total snowfall between these days ( \text{min_sum} = (r - l) \times 1 ).
Check for Feasibility:
If ( \text{required_sum} < \text{min_sum} ), it's impossible to assign positive integers (since each day must have at least 1 millimeter of snowfall).
If ( \text{required_sum} \geq \text{min_sum} ), proceed to the next step.
Assign Daily Snowfall Amounts:
Between Known Points:
Distribute the ( \text{extra} = \text{required_sum} - \text{min_sum} ) among the days between ( l ) and ( r ).
A simple approach is to assign 1 millimeter to each day and add the extra to the last day in the interval.
Before the First Known Point:
For days before the first known cumulative sum at day ( i ):
The cumulative snowfall ( a_i ) must be at least ( i ) (since each day must have at least 1 millimeter of snowfall).
If ( a_i < i ), output "NO".
Assign 1 millimeter to each day, adjust the last day to match the cumulative sum.
After the Last Known Point:
For days after the last known cumulative sum, assign 1 millimeter to each day (since there are no further constraints).
Check Individual Days:
Ensure that each assigned daily snowfall ( s_i ) is at least 1.
If any ( s_i < 1 ), output "NO".
Output the Result:
If all checks pass, output "YES" and the sequence ( s_1, s_2, \dots, s_n ).
If any check fails, output "NO".
Examples:
Sample Input 1:
code
5
1 3 -1 10 -1
Processing:
Known cumulative sums at days: 1 (value 1), 2 (value 3), 4 (value 10).
Check feasibility between days:
Between day 1 and day 2:
Required sum: ( 3 - 1 = 2 )
Minimum sum: ( 2 - 1 \times 1 = 1 )
Feasible since ( 2 \geq 1 ).
Assign daily snowfall:
Day 1: ( s_1 = 1 ) (from cumulative sum).
Day 2: ( s_2 = 2 ).
Between day 2 and day 4:
Required sum: ( 10 - 3 = 7 )
Minimum sum: ( 4 - 2 \times 1 = 2 )
Feasible since ( 7 \geq 2 ).
Assign daily snowfall:
Distribute extra ( 7 - 2 = 5 ) millimeters.
Days 3 and 4: Assign 1 millimeter each, add extra 5 to day 4.
( s_3 = 1 ), ( s_4 = 1 + 5 = 6 ).
Days without constraints (day 5):
Assign ( s_5 = 1 ).
Output:
code
YES
1 2 1 6 1
Sample Input 2:
code
3
10 -1 4
Processing:
Known cumulative sums at days: 1 (value 10), 3 (value 4).
Check feasibility between days:
Between day 1 and day 3:
Required sum: ( 4 - 10 = -6 )
Minimum sum: ( 3 - 1 \times 1 = 2 )
Feasibility check fails since ( -6 < 2 ).
Conclusion: Impossible to assign positive integers satisfying the conditions.
Output:
code
NO
Final Notes:
Carefully check for feasibility at each interval between known cumulative sums.
Ensure all assigned daily snowfalls are positive integers.
Answering the Problem:
Following the given approach, we can solve the problem for any valid input.
Here's how you can implement it:
python
n = int(input())
a = list(map(int, input().split()))
s = [1]*n # Initialize daily snowfalls to 1
positions = [i for i, x in enumerate(a) if x != -1]
prev_pos = -1
prev_cum = 0
possible = True
if positions:
# Check the days before the first known cumulative sum
first_pos = positions[0]
if a[first_pos] < first_pos + 1:
possible = False
else:
total = a[first_pos] - 0
min_total = first_pos + 1
if total < min_total:
possible = False
else:
# Assign snowfalls
extra = total - min_total
for i in range(first_pos):
s[i] = 1
s[first_pos] = 1 + extra
prev_pos = first_pos
prev_cum = a[first_pos]
# Process intervals between known positions
for idx in range(1, len(positions)):
current_pos = positions[idx]
total = a[current_pos] - prev_cum
min_total = current_pos - prev_pos
if total < min_total:
possible = False
break
else:
extra = total - min_total
for i in range(prev_pos + 1, current_pos):
s[i] = 1
s[current_pos] = 1 + extra
prev_pos = current_pos
prev_cum = a[current_pos]
# Assign 1 to days after the last known position
for i in range(positions[-1] + 1, n):
s[i] = 1
else:
# No known cumulative sums, assign 1 to all days
s = [1]*n
if possible and all(si >= 1 for si in s):
print("YES")
print(' '.join(map(str, s)))
else:
print("NO")
This code reads the inputs, checks for feasibility, assigns daily snowfall amounts, and prints the required outputs. It follows the approach described above, adjusting snowfall amounts based on known cumulative sums and ensuring all constraints are satisfied.