結果

問題 No.115 遠足のおやつ
ユーザー GrenacheGrenache
提出日時 2015-11-30 23:26:29
言語 Java21
(openjdk 21)
結果
AC  
実行時間 126 ms / 5,000 ms
コード長 1,198 bytes
コンパイル時間 2,889 ms
コンパイル使用メモリ 75,076 KB
実行使用メモリ 56,400 KB
最終ジャッジ日時 2023-08-31 00:36:18
合計ジャッジ時間 8,969 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 104 ms
56,028 KB
testcase_01 AC 102 ms
55,948 KB
testcase_02 AC 126 ms
55,900 KB
testcase_03 AC 104 ms
55,944 KB
testcase_04 AC 107 ms
56,376 KB
testcase_05 AC 105 ms
55,764 KB
testcase_06 AC 96 ms
55,340 KB
testcase_07 AC 102 ms
55,812 KB
testcase_08 AC 106 ms
56,000 KB
testcase_09 AC 102 ms
56,188 KB
testcase_10 AC 101 ms
55,676 KB
testcase_11 AC 106 ms
56,144 KB
testcase_12 AC 108 ms
54,092 KB
testcase_13 AC 102 ms
55,980 KB
testcase_14 AC 106 ms
55,840 KB
testcase_15 AC 105 ms
56,136 KB
testcase_16 AC 111 ms
55,496 KB
testcase_17 AC 107 ms
56,332 KB
testcase_18 AC 102 ms
55,568 KB
testcase_19 AC 104 ms
55,768 KB
testcase_20 AC 111 ms
55,952 KB
testcase_21 AC 111 ms
55,884 KB
testcase_22 AC 111 ms
55,812 KB
testcase_23 AC 105 ms
55,660 KB
testcase_24 AC 109 ms
56,264 KB
testcase_25 AC 107 ms
56,292 KB
testcase_26 AC 105 ms
55,904 KB
testcase_27 AC 108 ms
56,400 KB
testcase_28 AC 112 ms
55,844 KB
testcase_29 AC 106 ms
55,564 KB
testcase_30 AC 105 ms
55,960 KB
testcase_31 AC 105 ms
55,732 KB
testcase_32 AC 108 ms
55,940 KB
testcase_33 AC 102 ms
55,896 KB
testcase_34 AC 108 ms
55,980 KB
testcase_35 AC 111 ms
56,096 KB
testcase_36 AC 108 ms
54,096 KB
testcase_37 AC 107 ms
56,100 KB
testcase_38 AC 103 ms
56,148 KB
testcase_39 AC 107 ms
55,788 KB
testcase_40 AC 101 ms
55,920 KB
testcase_41 AC 106 ms
55,788 KB
testcase_42 AC 109 ms
56,264 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class Main_yukicoder115 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int d = sc.nextInt();
        int k = sc.nextInt();

		List<Integer> ret = new ArrayList<Integer>();
        for (int i = 1; i <= n && k > 0; i++) {
        	if (isOK(i, n, d - i, k - 1)) {
        		ret.add(i);
        		d -= i;
        		k--;
        	}
        }

        if (k == 0) {
        	for (int kk = 0; kk < ret.size(); kk++) {
        		if (kk == 0) {
        			System.out.printf("%d", ret.get(kk));
        		} else {
        			System.out.printf(" %d", ret.get(kk));
        		}
        	}
        	System.out.println("");
        } else {
        	System.out.println("-1");
        }

        sc.close();
    }

    private static boolean isOK(int i, int n, int d, int k) {
    	if (k == 0) {
    		return d == 0;
    	}

    	int min = (i + k - 1) * (i + k) / 2 - (i - 1) * i / 2;
    	int max = n * (n + 1) / 2 - (n - k) *( n - k + 1) / 2;

    	if (d < min || d > max) {
    		return false;
    	} else {
    		return true;
    	}
	}
}
0