結果

問題 No.2495 Three Sets
ユーザー startcppstartcpp
提出日時 2023-10-07 17:59:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 606 ms / 3,000 ms
コード長 2,849 bytes
コンパイル時間 715 ms
コンパイル使用メモリ 71,884 KB
実行使用メモリ 6,104 KB
最終ジャッジ日時 2023-10-07 17:59:13
合計ジャッジ時間 10,506 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 250 ms
4,376 KB
testcase_01 AC 248 ms
4,380 KB
testcase_02 AC 471 ms
4,380 KB
testcase_03 AC 276 ms
4,376 KB
testcase_04 AC 250 ms
4,380 KB
testcase_05 AC 254 ms
4,376 KB
testcase_06 AC 444 ms
4,380 KB
testcase_07 AC 515 ms
4,376 KB
testcase_08 AC 482 ms
4,380 KB
testcase_09 AC 264 ms
4,376 KB
testcase_10 AC 258 ms
4,380 KB
testcase_11 AC 491 ms
4,380 KB
testcase_12 AC 520 ms
4,376 KB
testcase_13 AC 520 ms
4,624 KB
testcase_14 AC 570 ms
5,340 KB
testcase_15 AC 517 ms
4,696 KB
testcase_16 AC 581 ms
5,984 KB
testcase_17 AC 606 ms
6,048 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 86 ms
6,028 KB
testcase_20 AC 562 ms
6,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//A, B, Cを降順ソートして累積和を取ったものをa, b, c (0-idx) とすると、j * a_i + k * b_j + i * c_k のmax化
//i, jを固定すると、f(k) = k * b_j + i * c_kのmax化.
//f(k + 1) - f(k) = b_j + i * C_k ≧ 0を解くと、i * C_k ≧ -b_j ⇔ (i≠0のとき) C_k ≧ Ceil(-b_j / i)
//Cは降順だったので、C_k < Ceil(-b_j / i)なるk(≧0, 無ければnC)を求めて、c_kを取ればOK。
//不等式より各数列について同じ数は「全く取らない or 全て取る」が最適と分かる。値ごとに累積和を取っておけばO(M^2)
//残るi = 0のケースは、nC * max(b_j)とすればOK. (M: 最大値 - 最小値 ≦ 6000)
#include <iostream>
#include <algorithm>
#define int long long
#define rep(i, n) for(i = 0; i < n; i++)
using namespace std;

int Ceil(int si, int bo) {
	if (bo < 0) { bo *= -1; si *= -1; }
	if (si >= 0) { return (si + bo - 1) / bo; }
	return si / bo;
}

const int M = 3000;
int nA, nB, nC;
int a[100000];
int b[100000];
int c[100000];
int rcntA[2 * M + 2], rsumA[2 * M + 2];	//[i]: -M + i未満の整数の個数と総和 (0 ≦ i ≦ 2M + 1)
int rcntB[2 * M + 2], rsumB[2 * M + 2];
int rcntC[2 * M + 2], rsumC[2 * M + 2];

void make_rui(int a[], int n, int rCnt[], int rSum[]) {
	int i, len = 1, s = a[0];
	for (i = 1; i < n; i++) {
		if (a[i - 1] != a[i]) {
			rCnt[a[i - 1] + M + 1] += len;
			rSum[a[i - 1] + M + 1] += s;
			len = 0; s = 0;
		}
		len++; s += a[i];
	}
	rCnt[a[n - 1] + M + 1] += len;
	rSum[a[n - 1] + M + 1] += s;
	rep(i, 2 * M + 1) rCnt[i + 1] += rCnt[i];
	rep(i, 2 * M + 1) rSum[i + 1] += rSum[i];
}

signed main() {
	int i, j;

	cin >> nA >> nB >> nC;
	rep(i, nA) cin >> a[i];
	rep(i, nB) cin >> b[i];
	rep(i, nC) cin >> c[i];
	sort(a, a + nA);
	sort(b, b + nB);
	sort(c, c + nC);
	make_rui(a, nA, rcntA, rsumA);
	make_rui(b, nB, rcntB, rsumB);
	make_rui(c, nC, rcntC, rsumC);

	int ans = 0;
	for (i = -M; i <= M + 1; i++) { //Aからはi以上の整数を取る
		int cntA = rcntA[2 * M + 1] - rcntA[i + M];
		int sumA = rsumA[2 * M + 1] - rsumA[i + M];
		if (cntA == 0) continue;
		for (j = -M; j <= M + 1; j++) { //Bからはj以上の整数を取る
			int cntB = rcntB[2 * M + 1] - rcntB[j + M];
			int sumB = rsumB[2 * M + 1] - rsumB[j + M];
			int high = Ceil(-sumB, cntA); 		//Cからはhigh以上の整数を取る
			high = max(-M, min(high, M + 1));	//配列外参照対策
			int cntC = rcntC[2 * M + 1] - rcntC[high + M];
			int sumC = rsumC[2 * M + 1] - rsumC[high + M];
			int score = sumA * cntB + sumB * cntC + sumC * cntA;
			ans = max(ans, score);
		}
	}

	//Aから何も取らないケース
	int maxB = 0;
	for (i = -M; i <= M + 1; i++) { //Bからi以上の整数を取るケース
		int sumB = rsumB[2 * M + 1] - rsumB[i + M];
		maxB = max(maxB, sumB);
	}
	ans = max(ans, maxB * nC);
	cout << ans << endl;
	return 0;
}
0