結果

問題 No.831 都市めぐり
ユーザー ks2mks2m
提出日時 2019-05-24 22:08:31
言語 Java21
(openjdk 21)
結果
AC  
実行時間 445 ms / 2,000 ms
コード長 866 bytes
コンパイル時間 4,721 ms
コンパイル使用メモリ 77,912 KB
実行使用メモリ 127,004 KB
最終ジャッジ日時 2023-10-17 12:40:04
合計ジャッジ時間 9,916 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 135 ms
57,452 KB
testcase_01 AC 134 ms
57,452 KB
testcase_02 AC 133 ms
57,508 KB
testcase_03 AC 133 ms
57,456 KB
testcase_04 AC 133 ms
57,608 KB
testcase_05 AC 133 ms
57,544 KB
testcase_06 AC 138 ms
57,572 KB
testcase_07 AC 141 ms
57,496 KB
testcase_08 AC 142 ms
57,596 KB
testcase_09 AC 139 ms
57,460 KB
testcase_10 AC 140 ms
57,232 KB
testcase_11 AC 139 ms
55,696 KB
testcase_12 AC 177 ms
62,020 KB
testcase_13 AC 174 ms
61,980 KB
testcase_14 AC 181 ms
62,084 KB
testcase_15 AC 309 ms
87,008 KB
testcase_16 AC 230 ms
70,176 KB
testcase_17 AC 315 ms
90,092 KB
testcase_18 AC 341 ms
107,056 KB
testcase_19 AC 444 ms
125,260 KB
testcase_20 AC 445 ms
127,004 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayDeque;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		sc.close();

		if (n == 1) {
			System.out.println(0);
			return;
		}

		ArrayDeque<Long> que = new ArrayDeque<Long>();
		que.add(1L);
		long left = 2;
		long right = n;
		while (left <= right) {
			que.addFirst(right);
			right--;
			if (left <= right) {
				que.addLast(right);
				right--;
			}
			if (left <= right) {
				que.addFirst(left);
				left++;
			}
			if (left <= right) {
				que.addLast(left);
				left++;
			}
		}

		long ans = 0;
		long first = que.pollFirst();
		long i = 0;
		long j = first;
		while (!que.isEmpty()) {
			i = j;
			j = que.pollFirst();
			ans += i * j + j - i;
		}
		ans += j * first + first - j;
		System.out.println(ans);
	}
}
0