結果

問題 No.1361 [Zelkova 4th Tune *] QUADRUPLE-SEQUENCEの詩
ユーザー 👑 KazunKazun
提出日時 2020-10-21 14:04:44
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 3,593 bytes
コンパイル時間 4,103 ms
コンパイル使用メモリ 91,340 KB
実行使用メモリ 36,968 KB
最終ジャッジ日時 2024-12-27 18:29:44
合計ジャッジ時間 16,365 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 71 WA * 3
権限があれば一括ダウンロードができます

ソースコード

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;
		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;

		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