結果

問題 No.78 クジ付きアイスバー
ユーザー YamaKasaYamaKasa
提出日時 2018-10-17 00:49:15
言語 Java21
(openjdk 21)
結果
AC  
実行時間 138 ms / 5,000 ms
コード長 1,738 bytes
コンパイル時間 2,171 ms
コンパイル使用メモリ 77,556 KB
実行使用メモリ 41,980 KB
最終ジャッジ日時 2024-04-20 20:42:12
合計ジャッジ時間 7,744 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
41,588 KB
testcase_01 AC 126 ms
40,316 KB
testcase_02 AC 125 ms
41,332 KB
testcase_03 AC 126 ms
41,972 KB
testcase_04 AC 123 ms
41,608 KB
testcase_05 AC 116 ms
41,820 KB
testcase_06 AC 115 ms
41,980 KB
testcase_07 AC 123 ms
41,848 KB
testcase_08 AC 114 ms
41,516 KB
testcase_09 AC 126 ms
41,632 KB
testcase_10 AC 129 ms
41,804 KB
testcase_11 AC 124 ms
40,544 KB
testcase_12 AC 111 ms
41,804 KB
testcase_13 AC 124 ms
41,640 KB
testcase_14 AC 124 ms
41,616 KB
testcase_15 AC 122 ms
41,740 KB
testcase_16 AC 124 ms
41,804 KB
testcase_17 AC 129 ms
41,728 KB
testcase_18 AC 125 ms
41,320 KB
testcase_19 AC 138 ms
41,460 KB
testcase_20 AC 119 ms
40,304 KB
testcase_21 AC 115 ms
41,132 KB
testcase_22 AC 114 ms
40,064 KB
testcase_23 AC 116 ms
41,576 KB
testcase_24 AC 116 ms
41,564 KB
testcase_25 AC 126 ms
41,640 KB
testcase_26 AC 124 ms
41,948 KB
testcase_27 AC 103 ms
41,304 KB
testcase_28 AC 105 ms
41,192 KB
testcase_29 AC 112 ms
41,308 KB
testcase_30 AC 115 ms
41,304 KB
testcase_31 AC 113 ms
41,356 KB
testcase_32 AC 117 ms
41,176 KB
testcase_33 AC 120 ms
41,312 KB
testcase_34 AC 108 ms
41,396 KB
testcase_35 AC 117 ms
41,460 KB
testcase_36 AC 114 ms
41,312 KB
testcase_37 AC 112 ms
41,616 KB
testcase_38 AC 121 ms
41,524 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int N = scan.nextInt();
		int K = scan.nextInt();
		String S = scan.next();
		scan.close();
		if(K <= N) {
			int cnt = 0;
			int a = 0;
			for(int i = 0; i < N; i++) {
				K--;
				if(a == 0) {
					cnt++;
				}else {
					a--;
				}
				if(S.charAt(i) == '1') {
					a++;
				}else if(S.charAt(i) == '2'){
					a += 2;
				}
				if(K == 0) {
					break;
				}
			}
			System.out.println(cnt);
			System.exit(0);
		}
		int a = 0;
		int cnt = 0;
		for(int i = 0; i < N; i++) {
			if(S.charAt(i) == '2') {
				a += 2;
			}else if(S.charAt(i) == '1') {
				a ++;
			}
		}

		if(a >= N) {
			cnt = 0;
			a = 0;
			for(int i = 0; i < N; i++) {
				if(a == 0) {
					cnt++;
				}else {
					a--;
				}
				if(S.charAt(i) == '1') {
					a++;
					K--;
				}else if(S.charAt(i) == '2') {
					K -= 2;
					a += 2;
				}
			}
			System.out.println(cnt);
			System.exit(0);
		}
		cnt = 0;
		int t = N - a;
		int c = 0;
		int d = 0;
		for(int i = 0; i < N; i++) {
			if(d == 0) {
				c++;
			}else {
				d--;
			}
			if(S.charAt(i) == '1') {
				d++;
			}else if(S.charAt(i) == '2') {
				d += 2;
			}
		}
		if(K % N == 0) {
			int ans = t * (K / N - 1) + c;
			System.out.println(ans);
		}else {
			int b = K / N;
			//System.out.println(c);
			int ans = c;
			if(b >= 2) {
				int e = (b - 1) * (N - a);
				ans += e;
			}
			int res = K - b * N;
			//System.out.println(res + " " + d);
			for(int i = 0; i < res; i++) {
				if(d == 0) {
					ans++;
				}else {
					d--;
				}
				if(S.charAt(i) == '1') {
					d++;
				}else if(S.charAt(i) == '2') {
					d += 2;
				}
			}
			System.out.println(ans);

		}

	}
}
0