結果

問題 No.1148 土偶Ⅲ
ユーザー tenten
提出日時 2020-08-17 18:44:14
言語 Java
(openjdk 23)
結果
AC  
実行時間 672 ms / 2,000 ms
コード長 974 bytes
コンパイル時間 2,456 ms
コンパイル使用メモリ 75,356 KB
実行使用メモリ 48,480 KB
最終ジャッジ日時 2024-10-11 11:13:10
合計ジャッジ時間 15,314 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int w = sc.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }
        HashSet<Integer> set = new HashSet<>();
        int left = 0;
        int right = -1;
        int sum = 0;
        int max = 0;
        while (right < n - 1) {
            while (right < n - 1 && !set.contains(arr[right + 1]) && sum + arr[right + 1] <= w) {
                right++;
                set.add(arr[right]);
                sum += arr[right];
            }
            max = Math.max(max, right - left + 1);
            set.remove(arr[left]);
            sum -= arr[left];
            left++;
            if (left > right) {
                right = left - 1;
                sum = 0;
            }
        }
        System.out.println(max);
    }
}
0