Home>

AtCoder's Submit this code for this C issue However, there are two test cases for WA.

Because the official explanation and my assumed logic are a little different, it may be mistaken for the fundamental part.
My thoughts are described in comments in the source code.

Please tell me what is wrong in the code, and in what case is the error.
Thank you.

Applicable source code
n = int (input ())
a = list (map (int, input (). split ()))
a.insert (0,0)
# Compare adjacent squares starting from the left side of the square.
# If there is a cell higher than the adjacent right cell, reduce the height by one.
for i in range (n):
    if a [i]>a [i + 1]:
        a [i]-= 1
# Compare adjacent squares again.
# If there is a cell higher than the adjacent right cell, "No" is displayed.
for i in range (n):
    if a [i]>a [i + 1]:
        print ("No")
        exit ()
print ("Yes")
  • Answer # 1

    1,2,2,1
    In this case, the answer is "Yes" because there are two 2's.
    However, if this logic is applied to this case (hereinafter referred to as the first and second from the left)
    The second is not larger than the third, so it is left as it is, and the third is larger than the fourth, so it falls by one.
    As a result, the data is 1,2,1,1 and the result is No because it is not monotonously non-decreasing.

  • Answer # 2

    That is a lie greed.

    if a [i]>a [i-1]:
        a [i]-= 1


    Is correct.

Trends