結果

問題 No.451 575
ユーザー 37zigen37zigen
提出日時 2016-12-02 02:58:23
言語 Java19
(openjdk 21)
結果
AC  
実行時間 1,230 ms / 2,000 ms
コード長 2,133 bytes
コンパイル時間 5,277 ms
コンパイル使用メモリ 76,328 KB
実行使用メモリ 70,000 KB
最終ジャッジ日時 2023-08-22 13:24:40
合計ジャッジ時間 21,930 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
55,956 KB
testcase_01 AC 123 ms
55,792 KB
testcase_02 AC 125 ms
55,652 KB
testcase_03 AC 126 ms
55,832 KB
testcase_04 AC 124 ms
56,144 KB
testcase_05 AC 228 ms
59,024 KB
testcase_06 AC 337 ms
60,800 KB
testcase_07 AC 644 ms
65,380 KB
testcase_08 AC 125 ms
55,868 KB
testcase_09 AC 214 ms
56,592 KB
testcase_10 AC 596 ms
61,008 KB
testcase_11 AC 1,078 ms
67,680 KB
testcase_12 AC 1,178 ms
70,000 KB
testcase_13 AC 1,230 ms
69,752 KB
testcase_14 AC 144 ms
55,964 KB
testcase_15 AC 224 ms
58,904 KB
testcase_16 AC 343 ms
60,368 KB
testcase_17 AC 653 ms
65,680 KB
testcase_18 AC 700 ms
64,988 KB
testcase_19 AC 582 ms
61,288 KB
testcase_20 AC 300 ms
60,324 KB
testcase_21 AC 236 ms
56,460 KB
testcase_22 AC 125 ms
55,796 KB
testcase_23 AC 1,206 ms
69,880 KB
testcase_24 AC 888 ms
65,128 KB
testcase_25 AC 128 ms
55,696 KB
testcase_26 AC 126 ms
55,472 KB
testcase_27 AC 181 ms
56,328 KB
testcase_28 AC 194 ms
54,684 KB
testcase_29 AC 285 ms
60,388 KB
testcase_30 AC 653 ms
64,760 KB
testcase_31 AC 126 ms
55,816 KB
testcase_32 AC 420 ms
61,012 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;

import java.util.*;

public class Q451 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		long[] b = new long[n + 1];
		long[] a = new long[n + 2];
		long Max = 1_000_000_000_000_000_000L;
		for (int i = 1; i <= n; ++i) {
			b[i] = sc.nextLong();
			if ((i + 1) % 2 == 0)
				a[i + 1] += b[i];
			else
				a[i + 1] -= b[i];
		}
		a[1] = 1;
		for (int i = 2; i <= n + 1; ++i) {
			if (i % 2 == 0) {
				a[i] -= a[i - 1];
			} else {
				a[i] += a[i - 1];
			}
			if (a[i] > 2 * Max || a[i] < -Max) {
				System.out.println(-1);
				return;
			}
		}

		long oddMax = a[1];
		long oddMin = a[1];
		long evenMax = a[2];
		long evenMin = a[2];
		for (int i = 4; i <= n + 1; i += 3) {
			oddMax = Math.max(oddMax, a[i]);
			oddMin = Math.min(oddMin, a[i]);
			if (i + 1 <= n + 1) {
				oddMax = Math.max(oddMax, a[i + 1]);
				oddMin = Math.min(oddMin, a[i + 1]);
			}
			++i;
		}
		for (int i = 2; i <= n + 1; i += 3) {
			evenMax = Math.max(evenMax, a[i]);
			evenMin = Math.min(evenMin, a[i]);
			if (i + 1 <= n + 1) {
				evenMax = Math.max(evenMax, a[i + 1]);
				evenMin = Math.min(evenMin, a[i + 1]);
			}
			++i;
		}

		ArrayDeque<Long> que = new ArrayDeque<>();
		// oddにどれだけ加算するか
		que.add(Max - oddMax);
		que.add(-Max + evenMax);
		que.add(1 - oddMin);
		que.add(-1 + evenMin);
		while (!que.isEmpty()) {
			long add = que.poll();
			if (oddMax + add <= Max && oddMax + add > 0 && evenMax - add <= Max && evenMax - add > 0 && oddMin + add > 0
					&& oddMin + add <= Max && evenMin - add > 0 && evenMin - add <= Max) {
				System.out.println(n + 1);
				a[1] += add;
				for (int i = 4; i <= n + 1; i += 3) {
					a[i] += add;
					if (i + 1 <= n + 1)
						a[i + 1] += add;
					++i;
				}
				for (int i = 2; i <= n + 1; i += 3) {
					a[i] -= add;
					if (i + 1 <= n + 1)
						a[i + 1] -= add;
					++i;
				}
				for (int i = 1; i <= n + 1; ++i)
					System.out.println(a[i]);
				return;
			}
		}
		System.out.println(-1);

	}

	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}
}
0