I think this question is a good method, but I have never met it before, and I have never thought of it. This is a simple and straightforward method to reduce complexity.
First, the two sets are merged into one set, and the two (S1, S2) are merged, that is, the number of N ^ 2 in each set, with a set of N numbers left.
In this case, there is still timeout
And then optimization ..
Sum = one of the three sets,
Enumeration is performed directly in a small set,
Sort two large sets of S1 S2 in ascending order,
One starts from small to large, and one starts from large to small,
When sum> 0, the big --
When sum is <0, the small ++
Finally, I was pitted by int64 for a morning.
# Include <iostream> # include <cmath> # include <cstdio> # include <vector> # include <cstring> # include <algorithm> using namespace STD; __int64 s [6] [205], S1 [40005], S2 [40005]; int main () {int N, T; _ int64 I, j, flag, k, L, sum; scanf ("% d", & T); While (t --) {scanf ("% d", & N); for (I = 0; I <5; I ++) {for (j = 0; j <n; j ++) scanf ("% i64d ", & S [I] [J]) ;}for (I = 0, K = 0; I <n; I ++) {for (j = 0; j <N; j ++, K ++) {S1 [k] = s [1] [I] + s [0] [J]; s2 [k] = s [2] [I] + s [3] [J];} Sort (S1, S1 + k); sort (S2, S2 + k); L = K-1; for (I = 0, flag = 0; I <n; I ++) {J = L; k = 0; while (j> = 0 & K <= L) {sum = (S1 [J] + S2 [k] + s [4] [I]); If (! Sum) {flag = 1; break;} else if (sum> 0) j --; else K ++;} If (FLAG) break;} If (FLAG) printf ("Yes \ n"); else printf ("NO \ n");} return 0 ;}