結果

問題 No.1309 テスト
ユーザー QCFiumQCFium
提出日時 2020-06-01 00:39:07
言語 Java21
(openjdk 21)
結果
RE  
実行時間 -
コード長 5,051 bytes
コンパイル時間 2,245 ms
コンパイル使用メモリ 77,936 KB
実行使用メモリ 57,420 KB
最終ジャッジ日時 2024-09-13 03:29:04
合計ジャッジ時間 17,460 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 RE -
testcase_43 RE -
testcase_44 RE -
testcase_45 RE -
testcase_46 RE -
testcase_47 RE -
testcase_48 RE -
testcase_49 RE -
testcase_50 RE -
testcase_51 RE -
testcase_52 RE -
testcase_53 RE -
testcase_54 RE -
testcase_55 RE -
testcase_56 RE -
testcase_57 RE -
testcase_58 RE -
testcase_59 RE -
testcase_60 RE -
testcase_61 RE -
testcase_62 RE -
testcase_63 RE -
testcase_64 RE -
testcase_65 RE -
testcase_66 RE -
testcase_67 RE -
testcase_68 RE -
testcase_69 RE -
testcase_70 RE -
testcase_71 RE -
testcase_72 RE -
testcase_73 RE -
testcase_74 RE -
testcase_75 RE -
testcase_76 RE -
testcase_77 RE -
testcase_78 RE -
testcase_79 RE -
testcase_80 RE -
testcase_81 RE -
testcase_82 RE -
testcase_83 RE -
testcase_84 RE -
testcase_85 RE -
testcase_86 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
	static int n, p, median, mode;
	static final long INF = 1000000000000000000L;
	
	
	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() {
		Scanner stdin = new Scanner(System.in);
		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