結果

問題 No.1309 テスト
ユーザー QCFiumQCFium
提出日時 2020-06-01 00:54:41
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,934 bytes
コンパイル時間 302 ms
コンパイル使用メモリ 82,384 KB
実行使用メモリ 208,840 KB
最終ジャッジ日時 2024-09-13 03:29:59
合計ジャッジ時間 39,949 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,272 ms
208,840 KB
testcase_01 AC 47 ms
62,052 KB
testcase_02 AC 38 ms
53,716 KB
testcase_03 AC 37 ms
53,272 KB
testcase_04 AC 37 ms
53,188 KB
testcase_05 AC 39 ms
54,796 KB
testcase_06 AC 38 ms
53,352 KB
testcase_07 AC 38 ms
53,320 KB
testcase_08 AC 39 ms
54,584 KB
testcase_09 AC 39 ms
53,544 KB
testcase_10 AC 39 ms
53,960 KB
testcase_11 AC 38 ms
54,328 KB
testcase_12 AC 39 ms
53,908 KB
testcase_13 AC 40 ms
55,052 KB
testcase_14 AC 40 ms
55,108 KB
testcase_15 AC 39 ms
54,264 KB
testcase_16 AC 40 ms
54,100 KB
testcase_17 AC 40 ms
54,216 KB
testcase_18 WA -
testcase_19 AC 41 ms
53,796 KB
testcase_20 AC 40 ms
54,220 KB
testcase_21 AC 40 ms
53,668 KB
testcase_22 AC 40 ms
54,188 KB
testcase_23 AC 40 ms
54,704 KB
testcase_24 AC 41 ms
54,368 KB
testcase_25 AC 43 ms
60,396 KB
testcase_26 AC 43 ms
60,304 KB
testcase_27 AC 39 ms
53,980 KB
testcase_28 AC 40 ms
54,084 KB
testcase_29 AC 39 ms
53,856 KB
testcase_30 AC 39 ms
53,552 KB
testcase_31 AC 40 ms
54,392 KB
testcase_32 AC 40 ms
53,656 KB
testcase_33 AC 45 ms
60,220 KB
testcase_34 AC 39 ms
53,408 KB
testcase_35 AC 39 ms
53,852 KB
testcase_36 AC 40 ms
54,448 KB
testcase_37 WA -
testcase_38 AC 38 ms
54,756 KB
testcase_39 AC 40 ms
55,364 KB
testcase_40 AC 43 ms
60,524 KB
testcase_41 AC 41 ms
54,444 KB
testcase_42 AC 40 ms
53,488 KB
testcase_43 AC 41 ms
55,136 KB
testcase_44 AC 39 ms
53,936 KB
testcase_45 AC 138 ms
77,564 KB
testcase_46 AC 366 ms
80,060 KB
testcase_47 AC 370 ms
81,912 KB
testcase_48 AC 363 ms
81,052 KB
testcase_49 AC 357 ms
80,604 KB
testcase_50 AC 218 ms
78,516 KB
testcase_51 AC 279 ms
78,672 KB
testcase_52 AC 253 ms
79,600 KB
testcase_53 AC 321 ms
79,900 KB
testcase_54 AC 356 ms
81,104 KB
testcase_55 AC 382 ms
79,908 KB
testcase_56 AC 281 ms
79,532 KB
testcase_57 AC 250 ms
79,340 KB
testcase_58 AC 346 ms
80,060 KB
testcase_59 AC 399 ms
81,140 KB
testcase_60 AC 270 ms
78,440 KB
testcase_61 AC 268 ms
79,096 KB
testcase_62 AC 288 ms
79,964 KB
testcase_63 AC 343 ms
80,136 KB
testcase_64 WA -
testcase_65 AC 2,158 ms
102,236 KB
testcase_66 AC 3,757 ms
120,028 KB
testcase_67 AC 3,981 ms
129,312 KB
testcase_68 AC 3,444 ms
119,384 KB
testcase_69 AC 1,693 ms
88,064 KB
testcase_70 AC 3,730 ms
117,620 KB
testcase_71 AC 3,523 ms
96,020 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)
		else : 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) > 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