結果

問題 No.1309 テスト
ユーザー QCFiumQCFium
提出日時 2020-06-01 00:40:46
言語 Java21
(openjdk 21)
結果
AC  
実行時間 903 ms / 4,000 ms
コード長 5,057 bytes
コンパイル時間 2,400 ms
コンパイル使用メモリ 78,108 KB
実行使用メモリ 58,236 KB
最終ジャッジ日時 2024-09-13 03:31:11
合計ジャッジ時間 22,882 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 428 ms
57,588 KB
testcase_01 AC 139 ms
54,140 KB
testcase_02 AC 137 ms
54,020 KB
testcase_03 AC 136 ms
53,968 KB
testcase_04 AC 136 ms
53,932 KB
testcase_05 AC 138 ms
53,844 KB
testcase_06 AC 139 ms
54,120 KB
testcase_07 AC 138 ms
53,844 KB
testcase_08 AC 136 ms
53,740 KB
testcase_09 AC 138 ms
54,224 KB
testcase_10 AC 136 ms
54,228 KB
testcase_11 AC 136 ms
54,032 KB
testcase_12 AC 136 ms
53,872 KB
testcase_13 AC 137 ms
54,144 KB
testcase_14 AC 137 ms
54,248 KB
testcase_15 AC 136 ms
54,060 KB
testcase_16 AC 137 ms
54,304 KB
testcase_17 AC 139 ms
53,864 KB
testcase_18 AC 139 ms
54,328 KB
testcase_19 AC 138 ms
54,152 KB
testcase_20 AC 138 ms
55,984 KB
testcase_21 AC 137 ms
53,848 KB
testcase_22 AC 136 ms
54,052 KB
testcase_23 AC 138 ms
53,972 KB
testcase_24 AC 135 ms
54,016 KB
testcase_25 AC 122 ms
53,116 KB
testcase_26 AC 138 ms
54,244 KB
testcase_27 AC 135 ms
54,192 KB
testcase_28 AC 139 ms
53,972 KB
testcase_29 AC 135 ms
54,040 KB
testcase_30 AC 135 ms
54,132 KB
testcase_31 AC 136 ms
54,264 KB
testcase_32 AC 135 ms
54,088 KB
testcase_33 AC 137 ms
54,120 KB
testcase_34 AC 137 ms
54,284 KB
testcase_35 AC 136 ms
53,996 KB
testcase_36 AC 137 ms
54,084 KB
testcase_37 AC 138 ms
54,124 KB
testcase_38 AC 135 ms
54,024 KB
testcase_39 AC 138 ms
54,004 KB
testcase_40 AC 139 ms
54,084 KB
testcase_41 AC 141 ms
54,108 KB
testcase_42 AC 137 ms
54,360 KB
testcase_43 AC 138 ms
54,128 KB
testcase_44 AC 126 ms
54,240 KB
testcase_45 AC 148 ms
53,980 KB
testcase_46 AC 154 ms
54,212 KB
testcase_47 AC 156 ms
53,944 KB
testcase_48 AC 156 ms
53,980 KB
testcase_49 AC 157 ms
54,092 KB
testcase_50 AC 155 ms
54,124 KB
testcase_51 AC 153 ms
54,308 KB
testcase_52 AC 150 ms
54,232 KB
testcase_53 AC 153 ms
54,204 KB
testcase_54 AC 149 ms
54,088 KB
testcase_55 AC 139 ms
53,344 KB
testcase_56 AC 152 ms
54,252 KB
testcase_57 AC 150 ms
54,272 KB
testcase_58 AC 154 ms
54,492 KB
testcase_59 AC 157 ms
54,208 KB
testcase_60 AC 151 ms
54,228 KB
testcase_61 AC 153 ms
53,968 KB
testcase_62 AC 137 ms
53,096 KB
testcase_63 AC 153 ms
54,076 KB
testcase_64 AC 157 ms
53,948 KB
testcase_65 AC 311 ms
57,712 KB
testcase_66 AC 446 ms
57,268 KB
testcase_67 AC 399 ms
57,456 KB
testcase_68 AC 367 ms
57,596 KB
testcase_69 AC 422 ms
57,528 KB
testcase_70 AC 429 ms
57,636 KB
testcase_71 AC 821 ms
57,552 KB
testcase_72 AC 903 ms
57,216 KB
testcase_73 AC 864 ms
57,604 KB
testcase_74 AC 337 ms
58,236 KB
testcase_75 AC 291 ms
57,688 KB
testcase_76 AC 316 ms
57,608 KB
testcase_77 AC 184 ms
56,484 KB
testcase_78 AC 138 ms
53,856 KB
testcase_79 AC 140 ms
54,016 KB
testcase_80 AC 139 ms
53,888 KB
testcase_81 AC 137 ms
53,852 KB
testcase_82 AC 139 ms
54,344 KB
testcase_83 AC 151 ms
54,028 KB
testcase_84 AC 643 ms
57,512 KB
testcase_85 AC 427 ms
57,676 KB
testcase_86 AC 374 ms
57,784 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