結果

問題 No.1361 [Zelkova 4th Tune *] QUADRUPLE-SEQUENCEの詩
ユーザー 👑 KazunKazun
提出日時 2020-10-21 14:13:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 358 ms / 2,000 ms
コード長 3,605 bytes
コンパイル時間 1,015 ms
コンパイル使用メモリ 90,584 KB
実行使用メモリ 37,072 KB
最終ジャッジ日時 2023-08-27 16:07:19
合計ジャッジ時間 17,633 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 14 ms
4,924 KB
testcase_19 AC 6 ms
4,376 KB
testcase_20 AC 16 ms
4,404 KB
testcase_21 AC 9 ms
4,464 KB
testcase_22 AC 12 ms
4,376 KB
testcase_23 AC 16 ms
4,864 KB
testcase_24 AC 17 ms
5,104 KB
testcase_25 AC 16 ms
4,992 KB
testcase_26 AC 17 ms
5,036 KB
testcase_27 AC 10 ms
4,380 KB
testcase_28 AC 137 ms
17,932 KB
testcase_29 AC 115 ms
14,160 KB
testcase_30 AC 89 ms
11,544 KB
testcase_31 AC 102 ms
16,688 KB
testcase_32 AC 148 ms
27,680 KB
testcase_33 AC 84 ms
12,092 KB
testcase_34 AC 151 ms
23,136 KB
testcase_35 AC 162 ms
17,784 KB
testcase_36 AC 258 ms
30,512 KB
testcase_37 AC 67 ms
13,076 KB
testcase_38 AC 119 ms
13,844 KB
testcase_39 AC 69 ms
10,012 KB
testcase_40 AC 99 ms
13,836 KB
testcase_41 AC 158 ms
19,860 KB
testcase_42 AC 205 ms
22,516 KB
testcase_43 AC 150 ms
17,384 KB
testcase_44 AC 153 ms
18,640 KB
testcase_45 AC 128 ms
15,228 KB
testcase_46 AC 122 ms
24,536 KB
testcase_47 AC 87 ms
13,224 KB
testcase_48 AC 229 ms
36,668 KB
testcase_49 AC 188 ms
36,688 KB
testcase_50 AC 245 ms
36,728 KB
testcase_51 AC 224 ms
37,072 KB
testcase_52 AC 175 ms
36,956 KB
testcase_53 AC 329 ms
36,744 KB
testcase_54 AC 318 ms
36,760 KB
testcase_55 AC 358 ms
36,820 KB
testcase_56 AC 313 ms
36,692 KB
testcase_57 AC 204 ms
36,732 KB
testcase_58 AC 92 ms
20,072 KB
testcase_59 AC 91 ms
20,260 KB
testcase_60 AC 82 ms
20,324 KB
testcase_61 AC 84 ms
20,060 KB
testcase_62 AC 82 ms
20,116 KB
testcase_63 AC 18 ms
14,148 KB
testcase_64 AC 18 ms
14,148 KB
testcase_65 AC 18 ms
14,256 KB
testcase_66 AC 18 ms
14,192 KB
testcase_67 AC 18 ms
14,192 KB
testcase_68 AC 1 ms
4,380 KB
testcase_69 AC 1 ms
4,376 KB
testcase_70 AC 2 ms
4,380 KB
testcase_71 AC 2 ms
4,380 KB
testcase_72 AC 2 ms
4,376 KB
testcase_73 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
#include<set>

using namespace std;
using ll = long long;

void input(vector<ll>& X) {
	for (int i = 0; i < X.size(); i++) {
		cin >> X.at(i);
	}
}

ll abs_max(vector<ll> X) {
	ll t = 0;
	for (int i = 0; i < X.size(); i++) {
		if (t < abs(X.at(i))) {
			t = abs(X.at(i));
		}
	}
	return t;
}

pair<ll, ll> h(ll x, vector<ll> G, vector<ll> H) {
	if (x == 0) {
		if (find(G.begin(), G.end(), 0) != G.end()) {
			return make_pair(0, H[0]);
		}
		else {
			return make_pair(G[0], 0);
		}
	}

	set<ll> F(H.begin(), H.end());
	for (auto g : G) {
		if (g == 0) continue;

		if (x % g == 0 && F.count(x / g)) {
			return make_pair(g, x / g);
		}
	}
	return make_pair(-1, -1);
}

int main() {
	ll K, L, M, N, S, T;
	cin >> K >> L >> M >> N >> S;

	vector<ll> A(K), B(L), C(M), D(N);
	vector<ll> U(K * L), V(M * N);
	vector<ll> U_pos(0), U_neg(0);
	vector<ll> V_pos(0), V_neg(0);
	ll U_positive, U_zero, U_negative;
	ll V_positive, V_zero, V_negative;
	ll E_positive, E_zero, E_negative;

	ll A_abs_max, B_abs_max, C_abs_max, D_abs_max;
	ll E_abs_max;

	input(A); input(B); input(C); input(D);
	A_abs_max = abs_max(A);
	B_abs_max = abs_max(B);
	C_abs_max = abs_max(C);
	D_abs_max = abs_max(D);
	E_abs_max = A_abs_max * B_abs_max * C_abs_max * D_abs_max;

	int t;
	t = 0;
	for (int w = 0; w < K; w++) {
		for (int x = 0; x < L; x++) {
			U.at(t++) = A.at(w) * B.at(x);
		}
	}

	t = 0;
	for (int y = 0; y < M; y++) {
		for (int z = 0; z < N; z++) {
			V.at(t++) = C.at(y) * D.at(z);
		}
	}
	sort(V.begin(), V.end());

	for (auto u : U) {
		if (u > 0) {
			U_pos.push_back(u);
		}
		else if (u < 0) {
			U_neg.push_back(u);
		}
	}

	for (auto v : V) {
		if (v > 0) {
			V_pos.push_back(v);
		}
		else if (v < 0) {
			V_neg.push_back(v);
		}
	}

	sort(U_pos.begin(), U_pos.end());
	sort(U_neg.begin(), U_neg.end());

	sort(V_pos.begin(), V_pos.end());
	sort(V_neg.begin(), V_neg.end());

	U_positive = U_pos.size();
	U_negative = U_neg.size();
	U_zero = K * L - (U_positive + U_negative);

	V_positive = V_pos.size();
	V_negative = V_neg.size();
	V_zero = M * N - (V_positive + V_negative);

	E_positive = U_positive * V_positive + U_negative * V_negative;
	E_negative = U_positive * V_negative + U_negative * V_positive; 
	E_zero = K * L * M * N - (E_positive + E_negative);

	ll Left, Right, Z, I;
	if (S <= E_negative) {
		Left = -(E_abs_max + 1);
		Right = 1;

		while (Right - Left > 1) {
			T = Left + (Right - Left) / 2;
			Z = I = 0;

			for (auto u : U_pos) {
				while (I < V_negative && u * V_neg.at(I) <= T) I++;
				Z += I;
			}

			I = 0;
			for (auto v : V_pos) {
				while (I < U_negative && v * U_neg.at(I) <= T) I++;
				Z += I;
			}

			if (Z >= S) {
				Right = T;
			}
			else {
				Left = T;
			}

		}

		T = Right;
	}
	else if (E_negative + 1 <= S && S <= E_negative + E_zero) {
		T = 0;
	}
	else {
		Left = 0;
		Right = (E_abs_max + 1) + 1;

		while (Right - Left > 1) {
			T = Left + (Right - Left) / 2;
			Z = 0;

			I = V_positive;
			for (auto u : U_pos) {
				while (I>0 && u * V_pos.at(I-1) > T) I--;
				Z += I;
			}

			I = 0;
			for (auto v : V_neg) {
				while (I < U_negative && v * U_neg.at(U_negative-(I+1)) <= T) I++;
				Z += I;
			}

			if (Z >= S - (E_negative + E_zero)) {
				Right = T;
			}
			else {
				Left = T;
			}
		}

		T = Right;
	}

	ll alpha, beta, a, b, c, d;
	pair<ll, ll> P;

	P = h(T, U, V);
	alpha = P.first; beta = P.second;

	P = h(alpha, A, B);
	a = P.first; b = P.second;

	P = h(beta, C, D);
	c = P.first; d = P.second;

	cout << T << endl;
	cout << a << " " << b << " " << c << " " << d << endl;
}
0