結果

問題 No.1309 テスト
ユーザー QCFiumQCFium
提出日時 2020-06-01 01:22:00
言語 PyPy3
(7.3.13)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,941 bytes
コンパイル時間 284 ms
コンパイル使用メモリ 87,280 KB
実行使用メモリ 143,444 KB
最終ジャッジ日時 2023-10-11 04:06:39
合計ジャッジ時間 44,541 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,330 ms
85,708 KB
testcase_01 AC 83 ms
75,436 KB
testcase_02 AC 72 ms
71,396 KB
testcase_03 AC 73 ms
71,400 KB
testcase_04 AC 73 ms
71,460 KB
testcase_05 AC 74 ms
71,436 KB
testcase_06 AC 71 ms
71,400 KB
testcase_07 AC 72 ms
71,544 KB
testcase_08 AC 72 ms
71,344 KB
testcase_09 AC 73 ms
71,464 KB
testcase_10 AC 73 ms
71,344 KB
testcase_11 AC 71 ms
71,456 KB
testcase_12 AC 72 ms
71,644 KB
testcase_13 AC 72 ms
71,576 KB
testcase_14 AC 73 ms
71,480 KB
testcase_15 AC 73 ms
71,364 KB
testcase_16 AC 74 ms
71,196 KB
testcase_17 AC 72 ms
71,512 KB
testcase_18 AC 75 ms
71,400 KB
testcase_19 AC 74 ms
71,276 KB
testcase_20 AC 77 ms
71,344 KB
testcase_21 AC 79 ms
71,472 KB
testcase_22 AC 77 ms
71,460 KB
testcase_23 AC 77 ms
71,456 KB
testcase_24 AC 78 ms
71,648 KB
testcase_25 AC 78 ms
75,304 KB
testcase_26 AC 77 ms
75,536 KB
testcase_27 AC 70 ms
71,440 KB
testcase_28 AC 74 ms
71,460 KB
testcase_29 AC 72 ms
71,280 KB
testcase_30 AC 74 ms
71,576 KB
testcase_31 AC 72 ms
71,512 KB
testcase_32 AC 74 ms
71,400 KB
testcase_33 AC 77 ms
75,444 KB
testcase_34 AC 71 ms
71,248 KB
testcase_35 AC 75 ms
71,352 KB
testcase_36 AC 74 ms
71,576 KB
testcase_37 AC 75 ms
71,576 KB
testcase_38 AC 73 ms
71,556 KB
testcase_39 AC 75 ms
71,504 KB
testcase_40 AC 77 ms
75,360 KB
testcase_41 AC 76 ms
71,512 KB
testcase_42 AC 73 ms
71,432 KB
testcase_43 AC 75 ms
71,516 KB
testcase_44 AC 72 ms
71,644 KB
testcase_45 AC 170 ms
78,936 KB
testcase_46 AC 411 ms
81,856 KB
testcase_47 AC 412 ms
81,688 KB
testcase_48 AC 403 ms
82,264 KB
testcase_49 AC 400 ms
81,332 KB
testcase_50 AC 256 ms
80,048 KB
testcase_51 AC 325 ms
80,972 KB
testcase_52 AC 291 ms
81,108 KB
testcase_53 AC 362 ms
81,928 KB
testcase_54 AC 410 ms
82,584 KB
testcase_55 AC 412 ms
83,020 KB
testcase_56 AC 327 ms
80,744 KB
testcase_57 AC 292 ms
80,636 KB
testcase_58 AC 387 ms
81,420 KB
testcase_59 AC 467 ms
83,608 KB
testcase_60 AC 311 ms
80,212 KB
testcase_61 AC 313 ms
81,792 KB
testcase_62 AC 324 ms
80,992 KB
testcase_63 AC 380 ms
81,588 KB
testcase_64 AC 447 ms
83,360 KB
testcase_65 AC 2,352 ms
114,508 KB
testcase_66 AC 3,995 ms
131,856 KB
testcase_67 TLE -
testcase_68 AC 3,646 ms
131,592 KB
testcase_69 AC 1,763 ms
91,680 KB
testcase_70 AC 3,830 ms
129,828 KB
testcase_71 AC 3,584 ms
100,636 KB
testcase_72 TLE -
testcase_73 -- -
testcase_74 -- -
testcase_75 -- -
testcase_76 -- -
testcase_77 -- -
testcase_78 -- -
testcase_79 -- -
testcase_80 -- -
testcase_81 -- -
testcase_82 -- -
testcase_83 -- -
testcase_84 -- -
testcase_85 -- -
testcase_86 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

INF = 1000000000000000000

def calc(mode_num, mode_sum, mode_freq, lower, upper, all_num) :
	other = all_num - mode_num * mode_freq
	if not mode_num :
		if not mode_sum :
			other_block = other // (mode_freq - 1)
			other_leftover = other % (mode_freq - 1)
			if other_block + (1 if other_leftover else 0) > upper - lower + 1 : return -INF
			return other_block * (upper + upper - other_block + 1) // 2 * (mode_freq - 1) + \
				other_leftover * (upper - other_block)
		return -INF
		
	adding = mode_sum - mode_num * (lower + lower + mode_num - 1) // 2
	other_block = other // (mode_freq - 1)
	other_leftover = other % (mode_freq - 1)
	if other_block and not other_leftover :
		other_block -= 1
		other_leftover = mode_freq - 1
	if other_block + (1 if other_leftover else 0) + mode_num > upper - lower + 1 : return -INF
	default_clearance = upper - lower + 1 - (other_block + (1 if other_leftover else 0) + mode_num)
	slide = adding // mode_num - default_clearance
	sum = mode_sum * mode_freq + other_block * (upper + upper - other_block + 1) // 2 * (mode_freq - 1) + \
		other_leftover * (upper - other_block)
	if slide > 0 :
		sum -= (other_leftover + (mode_freq - 1) * (slide - 1)) * mode_num
		sum -= adding % mode_num * (mode_freq - 1)
	elif not slide : sum -= adding % mode_num * other_leftover
	return sum

def get_candidate(mode_num, mode_freq, upper, all_num) :
	other = all_num - mode_num * mode_freq
	if not mode_num : return 0
	other_used = other // (mode_freq - 1)
	if other % (mode_freq - 1) : other_used += 1
	return (upper - other_used + upper - other_used - mode_num + 1) * mode_num // 2

def solve_sub(median, median_l, median_r, freq, mode_all_sum, left, right) :
	lower = 0
	upper = mode_all_sum
	lower = max(lower, (left - 1) * left // 2)
	upper = min(upper, (median - 1 + median - left) * left // 2)
	lower = max(lower, mode_all_sum - (p + p - right + 1) * right // 2)
	upper = min(upper, mode_all_sum - (median + 1 + median + right) * right // 2)
	candidates = [lower, upper]
	candidates.append(get_candidate(left, freq, median - 1, median_l))
	candidates.append(candidates[-1] + left)
	candidates.append(mode_all_sum - get_candidate(right, freq, p, n - median_r))
	candidates.append(candidates[-1] - right)
	res = -1
	for left_sum in candidates :
		if left_sum >= lower and left_sum <= upper :
			cur = (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 = max(res, cur)
	return res


for testcnt in range(10) :
	n, p, median, mode = map(int, input().split())
	half = n // 2
	upper = p - median
	res = -1
	if median >= half and median + half <= p :
		min_sum = median + (half - 1) * half // 2 + (median + 1 + median + half) * half // 2
		max_sum = median + (median - 1 + median - half) * half // 2 + (p + p - half + 1) * half // 2;
		if mode * n >= min_sum and mode * n <= max_sum : res = mode * n

	for freq in range(2, n + 1) :
		for left in range(0, min(median, half // freq) + 1) :
			for right in range(0, min(upper, half // freq) + 1) :
				median_l_min = left * freq
				median_l_max = min(half, left * freq + (median - left) * (freq - 1))
				median_r_max = n - right * freq
				median_r_min = max(half + 1, n - right * freq - (upper - right) * (freq - 1))
				if left or right :
					mode_all_sum = mode * (left + right)
					median_r = median_r_min
					median_l = max(median_l_min, median_r - (freq - 1))
					if median_l <= half :
						res = max(res, solve_sub(median, median_l, median_r, freq, mode_all_sum, left, right))
				median_l_min = max(median_l_min, median_r_min - freq)
				median_l_max = min(median_l_max, median_r_max - freq)
				if median_l_max >= median_l_min :
					median_l = median_l_min
					mode_all_sum = mode * (left + right + 1) - median
					res = max(res, solve_sub(median, median_l, median_l + freq, freq, mode_all_sum, left, right))

	print(res)

0