結果

問題 No.1309 テスト
ユーザー QCFiumQCFium
提出日時 2020-06-01 00:40:46
言語 Java21
(openjdk 21)
結果
AC  
実行時間 898 ms / 4,000 ms
コード長 5,057 bytes
コンパイル時間 2,349 ms
コンパイル使用メモリ 74,844 KB
実行使用メモリ 59,908 KB
最終ジャッジ日時 2023-10-11 04:07:03
合計ジャッジ時間 22,440 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 419 ms
59,444 KB
testcase_01 AC 130 ms
55,564 KB
testcase_02 AC 129 ms
56,228 KB
testcase_03 AC 124 ms
55,860 KB
testcase_04 AC 126 ms
55,860 KB
testcase_05 AC 123 ms
56,744 KB
testcase_06 AC 123 ms
55,876 KB
testcase_07 AC 124 ms
55,824 KB
testcase_08 AC 126 ms
56,120 KB
testcase_09 AC 124 ms
55,560 KB
testcase_10 AC 122 ms
56,152 KB
testcase_11 AC 122 ms
55,840 KB
testcase_12 AC 124 ms
55,892 KB
testcase_13 AC 125 ms
55,864 KB
testcase_14 AC 127 ms
56,112 KB
testcase_15 AC 127 ms
56,104 KB
testcase_16 AC 127 ms
56,160 KB
testcase_17 AC 128 ms
56,168 KB
testcase_18 AC 130 ms
55,916 KB
testcase_19 AC 128 ms
55,908 KB
testcase_20 AC 126 ms
55,796 KB
testcase_21 AC 126 ms
56,060 KB
testcase_22 AC 124 ms
56,244 KB
testcase_23 AC 124 ms
55,812 KB
testcase_24 AC 125 ms
54,584 KB
testcase_25 AC 125 ms
56,348 KB
testcase_26 AC 125 ms
55,748 KB
testcase_27 AC 122 ms
55,836 KB
testcase_28 AC 126 ms
56,156 KB
testcase_29 AC 124 ms
56,156 KB
testcase_30 AC 124 ms
55,844 KB
testcase_31 AC 123 ms
56,000 KB
testcase_32 AC 124 ms
56,324 KB
testcase_33 AC 125 ms
55,856 KB
testcase_34 AC 126 ms
56,116 KB
testcase_35 AC 130 ms
56,144 KB
testcase_36 AC 126 ms
55,804 KB
testcase_37 AC 128 ms
55,720 KB
testcase_38 AC 127 ms
56,320 KB
testcase_39 AC 128 ms
56,080 KB
testcase_40 AC 127 ms
55,808 KB
testcase_41 AC 127 ms
55,968 KB
testcase_42 AC 127 ms
56,004 KB
testcase_43 AC 126 ms
55,780 KB
testcase_44 AC 126 ms
55,800 KB
testcase_45 AC 141 ms
55,904 KB
testcase_46 AC 143 ms
55,880 KB
testcase_47 AC 144 ms
56,184 KB
testcase_48 AC 144 ms
56,260 KB
testcase_49 AC 144 ms
56,008 KB
testcase_50 AC 143 ms
55,768 KB
testcase_51 AC 143 ms
56,468 KB
testcase_52 AC 139 ms
55,980 KB
testcase_53 AC 143 ms
55,504 KB
testcase_54 AC 141 ms
56,024 KB
testcase_55 AC 146 ms
55,980 KB
testcase_56 AC 148 ms
56,004 KB
testcase_57 AC 149 ms
55,604 KB
testcase_58 AC 141 ms
56,160 KB
testcase_59 AC 144 ms
55,704 KB
testcase_60 AC 140 ms
56,516 KB
testcase_61 AC 144 ms
56,164 KB
testcase_62 AC 141 ms
55,712 KB
testcase_63 AC 140 ms
55,852 KB
testcase_64 AC 146 ms
56,276 KB
testcase_65 AC 295 ms
59,512 KB
testcase_66 AC 453 ms
59,240 KB
testcase_67 AC 385 ms
59,908 KB
testcase_68 AC 364 ms
59,568 KB
testcase_69 AC 411 ms
59,124 KB
testcase_70 AC 436 ms
58,808 KB
testcase_71 AC 828 ms
59,556 KB
testcase_72 AC 898 ms
59,484 KB
testcase_73 AC 869 ms
59,696 KB
testcase_74 AC 306 ms
59,340 KB
testcase_75 AC 271 ms
59,452 KB
testcase_76 AC 333 ms
59,636 KB
testcase_77 AC 174 ms
59,104 KB
testcase_78 AC 131 ms
55,948 KB
testcase_79 AC 130 ms
56,104 KB
testcase_80 AC 129 ms
56,400 KB
testcase_81 AC 131 ms
55,828 KB
testcase_82 AC 129 ms
56,184 KB
testcase_83 AC 146 ms
55,808 KB
testcase_84 AC 634 ms
59,224 KB
testcase_85 AC 428 ms
59,116 KB
testcase_86 AC 367 ms
59,552 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
	static int n, p, median, mode;
	static final long INF = 1000000000000000000L;
	
	static Scanner stdin = new Scanner(System.in);
	
	static long calc(int mode_num, long mode_sum, int mode_freq, int min, int max, int all_num) {
		int other = all_num - mode_num * mode_freq;
		if (mode_num == 0) {
			if (mode_sum == 0) {
				int other_block = other / (mode_freq - 1);
				int other_leftover = other % (mode_freq - 1);
				if (other_block + (other_leftover != 0 ? 1 : 0) > max - min + 1) return -INF;
				return (long) other_block * (max + max - other_block + 1) / 2 * (mode_freq - 1) +
					(long) other_leftover * (max - other_block);
			}
			return -INF; // invalid
		}
		long adding = mode_sum - (long) mode_num * (min + min + mode_num - 1) / 2;
		int other_block = other / (mode_freq - 1);
		int other_leftover = other % (mode_freq - 1);
		if (other_block != 0 && other_leftover == 0) {
			other_block--;
			other_leftover = mode_freq - 1;
		}
		if (other_block + (other_leftover != 0 ? 1 : 0) + mode_num > max - min + 1) return -INF;
		int default_clearance = max - min + 1 - (other_block + (other_leftover != 0 ? 1 : 0) + mode_num);
		int slide = (int) (adding / mode_num - default_clearance);
		long sum = mode_sum * mode_freq + (long) other_block * (max + max - other_block + 1) / 2 * (mode_freq - 1)
			+ (long) other_leftover * (max - other_block);
		if (slide > 0) {
			sum -= (other_leftover + (long) (mode_freq - 1) * (slide - 1)) * mode_num;
			sum -= adding % mode_num * (mode_freq - 1);
		} else if (slide == 0) sum -= adding % mode_num * other_leftover;
		return sum;
	}
	static long get_candidate(int mode_num, int mode_freq, int max, int all_num) {
		int other = all_num - mode_num * mode_freq;
		if (mode_num == 0) return 0;
		
		int other_used = other / (mode_freq - 1);
		if (other % (mode_freq - 1) != 0) other_used++;
		
		return (long) (max - other_used + max - other_used - mode_num + 1) * mode_num / 2;
	}
	static long solve_sub(int median, int median_l, int median_r, int freq, long mode_all_sum, int left, int right) {
		long lower = 0, upper = mode_all_sum; // lower and upper bound(both inclusive) of sum of modes on the left side
		lower = Math.max(lower, (left - 1) * left / 2);
		upper = Math.min(upper, (long) (median - 1 + median - left) * left / 2);
		lower = Math.max(lower, mode_all_sum - (long) (p + p - right + 1) * right / 2);
		upper = Math.min(upper, mode_all_sum - (long) (median + 1 + median + right) * right / 2);
		
		long r0 = get_candidate(left, freq, median - 1, median_l);
		long r1 = mode_all_sum - get_candidate(right, freq, p, n - median_r);
		long[] candidates = {lower, upper, r0, r0 + left, r1, r1 - right};
		long res = -1;
		for (long left_sum : candidates) if (left_sum >= lower && left_sum <= upper) {
			long cur = (long) (median_r - median_l) * median;
			cur += calc(left, left_sum, freq, 0, median - 1, median_l);
			cur += calc(right, mode_all_sum - left_sum, freq, median + 1, p, n - median_r);
			res = Math.max(res, cur);
		}
		return res;
	}
	
	static void solve() {
		n = stdin.nextInt();
		p = stdin.nextInt();
		median = stdin.nextInt();
		mode = stdin.nextInt();
		
		
		int half = n / 2;
		int upper = p - median;
		long res = -1;
		// freq == 1
		if (median >= half && median + half <= p) {
			long min_sum = median + (half - 1) * half / 2 + (long) (median + 1 + median + half) * half / 2;
			long max_sum = median + (long) (median - 1 + median - half) * half / 2 + (long) (p + p - half + 1) * half / 2;
			if ((long) mode * n >= min_sum && (long) mode * n <= max_sum) res = (long) mode * n;
		}
		for (int freq = 2; freq <= n; freq++) {
			for (int left = 0; left * freq <= half && left <= median; left++) {
				for (int right = 0; right * freq <= half && right <= upper; right++) {
					int median_l_min = left * freq;
					int median_l_max = (int) Math.min(half, left * freq + (long) (median - left) * (freq - 1));
					int median_r_max = n - right * freq;
					int median_r_min = (int) Math.max(half + 1, n - right * freq - (long) (upper - right) * (freq - 1));
					if (left != 0 || right != 0) { // don't use median as (one of) mode(s)
						long mode_all_sum = (long) mode * (left + right);
						int median_r = median_r_min;
						int median_l = Math.max(median_l_min, median_r - (freq - 1));
						if (median_l <= half) {
							res = Math.max(res, solve_sub(median, median_l, median_r, freq, mode_all_sum, left, right));
						}
					}
					{ // use median as (one of) mode(s)
						median_l_min = Math.max(median_l_min, median_r_min - freq);
						median_l_max = Math.min(median_l_max, median_r_max - freq);
						if (median_l_max >= median_l_min) {
							int median_l = median_l_min;
							long mode_all_sum = (long) mode * (left + right + 1) - median;
							res = Math.max(res, solve_sub(median, median_l, median_l + freq, freq, mode_all_sum, left, right));
						}
					}
				}
			}
		}
		System.out.println(res);
	}
	
	public static void main(String[] args) {
		for (int i = 0; i < 10; i++) solve();
	}
}
0