結果

問題 No.1765 While Shining
ユーザー tentententen
提出日時 2022-09-28 10:17:01
言語 Java21
(openjdk 21)
結果
AC  
実行時間 197 ms / 2,000 ms
コード長 1,620 bytes
コンパイル時間 1,991 ms
コンパイル使用メモリ 74,060 KB
実行使用メモリ 56,852 KB
最終ジャッジ日時 2023-08-24 04:18:44
合計ジャッジ時間 6,682 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
49,360 KB
testcase_01 AC 40 ms
49,484 KB
testcase_02 AC 41 ms
49,344 KB
testcase_03 AC 40 ms
49,332 KB
testcase_04 AC 42 ms
49,384 KB
testcase_05 AC 44 ms
49,500 KB
testcase_06 AC 41 ms
49,400 KB
testcase_07 AC 41 ms
49,268 KB
testcase_08 AC 48 ms
49,536 KB
testcase_09 AC 170 ms
56,432 KB
testcase_10 AC 171 ms
55,932 KB
testcase_11 AC 165 ms
55,636 KB
testcase_12 AC 174 ms
56,356 KB
testcase_13 AC 183 ms
56,156 KB
testcase_14 AC 194 ms
55,956 KB
testcase_15 AC 197 ms
54,264 KB
testcase_16 AC 196 ms
56,852 KB
testcase_17 AC 195 ms
56,208 KB
testcase_18 AC 191 ms
56,224 KB
testcase_19 AC 192 ms
56,236 KB
testcase_20 AC 178 ms
56,084 KB
testcase_21 AC 180 ms
56,180 KB
testcase_22 AC 169 ms
55,908 KB
testcase_23 AC 192 ms
56,132 KB
testcase_24 AC 196 ms
56,212 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    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[] isLights = new boolean[n];
        for (int i = 0; i < n; i++) {
            isLights[i] = (sc.nextInt() == 1);
        }
        int[] dp = new int[n];
        long ans = 0;
        if (isLights[n - 2]) {
            dp[n - 2] = 1;
            ans = 1;
        }
        for (int i = n - 3; i >= 0; i--) {
            if (isLights[i]) {
                if (!isLights[i + 1]) {
                    dp[i] = dp[i + 2] + 2;
                } else {
                    dp[i] = 1;
                }
            }
            ans += dp[i];
        }
        System.out.println(ans);
    }
}
    
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 String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0