Hdu4334 trouble merge sets can reduce complexity.

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.