結果
問題 | No.1309 テスト |
ユーザー |
![]() |
提出日時 | 2020-06-01 00:40:46 |
言語 | Java (openjdk 23) |
結果 |
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 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 85 |
ソースコード
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 sidelower = 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 == 1if (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();}}