結果

問題 No.451 575
ユーザー ヒッキープログラミングするスレ GitHub ガチヒッキープログラミングするスレ GitHub ガチ
提出日時 2016-12-02 05:44:06
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 3,557 bytes
コンパイル時間 525 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 15,872 KB
最終ジャッジ日時 2024-05-09 18:31:53
合計ジャッジ時間 7,709 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 27 ms
11,008 KB
testcase_02 AC 27 ms
11,136 KB
testcase_03 WA -
testcase_04 AC 28 ms
11,136 KB
testcase_05 AC 47 ms
11,264 KB
testcase_06 AC 88 ms
11,904 KB
testcase_07 AC 342 ms
15,744 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 29 ms
11,136 KB
testcase_15 AC 41 ms
11,008 KB
testcase_16 AC 82 ms
11,648 KB
testcase_17 AC 340 ms
15,744 KB
testcase_18 AC 339 ms
15,872 KB
testcase_19 AC 232 ms
14,080 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 29 ms
11,008 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 30 ms
11,008 KB
testcase_27 AC 31 ms
11,008 KB
testcase_28 AC 32 ms
11,008 KB
testcase_29 AC 60 ms
11,520 KB
testcase_30 AC 353 ms
15,744 KB
testcase_31 AC 28 ms
11,008 KB
testcase_32 AC 145 ms
12,416 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n = int(input())
b = [0] + [int(input()) for _ in range(n)]

# i is odd
# b(i) = a(i) + a(i + 1)
# b(i + 1) = a(i + 1) - a(i + 2)
# -> a(i + 1) = b(i + 1) + a(i + 2)
# b(i) = a(i) + (b(i + 1) + a(i + 2))
# -> a(i + 2) = b(i) - b(i + 1) - a(i)
# i -> i - 2
# a(i) = b(i - 2) - b(i - 1) - a(i - 2)
# apply
# a(3) = b(1) - b(2) - a(1) > 0
# -> a(1) < b(1) - b(2) = G(3)
# a(5) = b(3) - b(4) - a(3)
#      = b(3) - b(4) - (b(1) - b(2) - a(1))
#      = b(3) - b(4) - b(1) + b(2) + a(1) > 0
# -> L(5) = b(1) - b(2) - b(3) + b(4) < a(1)
# a(7) = b(5) - b(6) - a(5)
#      = b(5) - b(6) - (b(3) - b(4) - b(1) + b(2) + a(1))
#      = b(5) - b(6) - b(3) + b(4) + b(1) - b(2) - a(1) > 0
# -> a(1) < b(1) - (2) - b(3) + b(4) + b(5) - b(6) = G(7)
# and ...
# b(i) = a(i) + a(i + 1)
# -> 0 < a(i) = b(i) - a(i + 1) < b(i)
# if i is in [3, 7, 11, ...], 0 < a(i) = G(i) - a(1) < b(i) ->  G_odd(i) = G(i) - b(i) < a(1)
# if i is in [5, 9, 13, ...], 0 < a(i) = a(1) - L(i) < b(i) -> a(1) < b(i) + L(i) = L_odd(i) 
# repeat then ... max(L_odd) < a(1) < min(G_odd)
# if min(G_odd) - max(L_odd) > 1, a(1) exists maybe, otherwise not


# j is even
# b(j) = a(j) - a(j + 1)
# b(j + 1) = a(j + 1) + a(j + 2)
# -> a(j + 1) = b(j + 1) - a(j + 2)
# b(j) = a(j) - (b(j + 1) - a(j + 2))
# -> a(j + 2) = b(j) + b(j + 1) - a(j)
# j -> j - 2
# a(j) = b(j - 2) + b(j - 1) - a(j - 2)
# apply
# a(4) = b(2) + b(3) - a(2) > 0
# -> a(2) < b(2) + b(3) = G(4)
# a(6) = b(4) + b(5) - a(4)
#      = b(4) + b(5) - (b(2) + b(3) - a(2))
#      = b(4) + b(5) - b(2) - b(3) + a(2) > 0
# -> L(6) = b(2) + b(3) - b(4) - b(5) < a(2)
# b(j) = a(j) - a(j + 1)
# -> b(j) < a(j) = b(j) + a(j + 1)
# if j is in [2, 6, 10, ...], b(j) < G(j) - a(2) = a(j) -> a(2) < G(j) - b(j) = G_even(j)
# if j is in [4, 8, 12, ...], b(j) < a(2) - L(j) = a(j) -> L_even(j) = b(j) + L(j) < a(2)
# repeat then ... max(L_even) < a(2) < min(G_odd)
# if min(G_even) - max(L_even) > 1, a(2) exists maybe, otherwise not

# don't forget: b(1) = a(1) + a(2)
# max(L_odd) < a(1) < min(G_odd, b(1))
# max(L_even) < a(2) < min(G_even, b(1))

debug = False

L_odd = 0
G_odd = b[1]
tmp = 0
for i in range(3, n + 3, 2):
    if i > n:
        break
    tmp = b[i - 2] - b[i - 1] - tmp
    if ((i - 3) // 2) % 2 == 0:
        G_odd = min(G_odd, tmp, tmp - b[i])
    else:
        L_odd = max(L_odd, -tmp, b[i] + tmp)

L_even = 0
G_even = b[1]
tmp = 0
for j in range(4, n + 3, 2):
    if j > n:
        break
    tmp = b[j - 2] + b[j - 1] - tmp
    if ((j - 4) // 2) % 2 == 0:
        G_even = min(G_even, tmp, tmp - b[j])
    else:
        L_even = max(L_even, -tmp, b[j] + tmp)

if debug:
    print(L_odd, G_odd, L_even, G_even)
        
a1 = G_odd - 1
a2 = b[1] - a1
if a2 <= L_even:
    a2 = L_even + 1
    a1 = b[1] - a2
if G_even > G_odd:
    a2 = G_even - 1
    a1 = b[1] = a2
    if a1 <= L_odd:
        a1 = L_odd + 1
        a2 = b[1] - a1
        
if a1 in range(L_odd + 1, G_odd) and a2 in range(L_even + 1, G_even):
    a = [0, a1, a2]
    for k in range(3, n + 2):
        if k % 2 == 0:
            a.append(b[k - 2] + b[k - 1] - a[k - 2])
        else:
            a.append(b[k - 2] - b[k - 1] - a[k - 2])
    if debug:
        for x in range(1, n + 1):
            if x % 2 == 0:
                if b[x] != a[x] - a[x + 1]:
                    print(x, b[x], a[x], a[x + 1], a[x] - a[x + 1])
                    break
            else:
                if b[x] != a[x] + a[x + 1]:
                    print(x, b[x], a[x], a[x + 1], a[x] - a[x + 1])
                    break
    print(n + 1, *a[1:], sep='\n')
else:
    print(-1)
0