結果

問題 No.1765 While Shining
ユーザー tentententen
提出日時 2023-02-22 14:52:09
言語 Java21
(openjdk 21)
結果
AC  
実行時間 199 ms / 2,000 ms
コード長 2,155 bytes
コンパイル時間 2,408 ms
コンパイル使用メモリ 90,964 KB
実行使用メモリ 56,676 KB
最終ジャッジ日時 2023-09-29 22:51:10
合計ジャッジ時間 7,738 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
47,360 KB
testcase_01 AC 40 ms
49,320 KB
testcase_02 AC 44 ms
49,428 KB
testcase_03 AC 42 ms
49,724 KB
testcase_04 AC 43 ms
49,484 KB
testcase_05 AC 44 ms
49,404 KB
testcase_06 AC 43 ms
49,588 KB
testcase_07 AC 42 ms
49,440 KB
testcase_08 AC 48 ms
49,496 KB
testcase_09 AC 174 ms
56,024 KB
testcase_10 AC 188 ms
56,344 KB
testcase_11 AC 179 ms
55,688 KB
testcase_12 AC 187 ms
56,544 KB
testcase_13 AC 181 ms
54,364 KB
testcase_14 AC 192 ms
56,676 KB
testcase_15 AC 199 ms
56,552 KB
testcase_16 AC 191 ms
56,388 KB
testcase_17 AC 197 ms
56,264 KB
testcase_18 AC 198 ms
56,224 KB
testcase_19 AC 192 ms
56,036 KB
testcase_20 AC 177 ms
56,052 KB
testcase_21 AC 192 ms
56,472 KB
testcase_22 AC 192 ms
56,244 KB
testcase_23 AC 193 ms
56,440 KB
testcase_24 AC 195 ms
56,412 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    static int ans = 0;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        if (n == 1) {
            System.out.println(0);
            return;
        }
        boolean[] isShining = new boolean[n];
        for (int i = 0; i < n; i++) {
            isShining[i] = (sc.nextInt() == 1);
        }
        long ans = 0;
        int[] dp = new int[n];
        if (isShining[n - 2]) {
            dp[n - 2] = 1;
            ans = 1;
        }
        for (int i = n - 3; i >= 0; i--) {
            if (isShining[i]) {
                if (isShining[i + 1]) {
                    dp[i] = 1;
                } else {
                    dp[i] = 2 + dp[i + 2];
                }
            }
            ans += dp[i];
        }
        System.out.println(ans);
    }
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0