! HDU 4,334 sets each one number and 0 whether there is-thinking, card time-(magic of the pointer)

Source: Internet
Author: User

Test instructions: There are 5 sets, the size of the collection is N, and each one sets out a number, asking if you can find five numbers and 0. Data range: t<=50;n<=200

Analysis:

Brute force enumeration is n^5*t, time-out, then you need to use some tricks.

Here is the magic of a pointer: How to find a B in the complexity of O (n), so that A+b==c (a, B, respectively, belongs to a series, A, b). The practice is to first sort a, a, and then a pointer I point to the first, the pointer J points to the tail of B, the number of the pointer to determine whether the sum of ==c, if equal to the end of the search, if less than, then i++, if greater than J-, if there is a pointer has gone to the head has not found A+b==c It indicates that there is no such ab;

This problem re-deformation: There are three series a,b,c, how to find the complexity of O (n^2) a,b,c, making a+b==c. method or the above, is more than a loop to traverse C.

The topic is to use this technique, but also with a little division of the idea of treatment, in fact, is not divided, anyway, I remember this, that is, do not give a question to the problem of the whole solution, the topic said sum, we do not have to one step the sum ah, we can sum this problem into: a1+a2 A3+a4;a5 sum is (A1+A2) + (A3+A4) +a5, and because is to find equals 0, so is: (A1+A2) + (A3+A4) ==-a5, so that the problem is converted into the above model.

In addition, the sort + de-weight can be done with set, and the Set.insert () will be automatically weighed and sorted in ascending order.

Code:

#include <iostream> #include <set> #include <algorithm>using namespace Std;set<long long> s1,s2 ; int T,n;long long A[6][300];long long Sum[3][50000];int main () {cin>>t;while (t--) {cin>>n;s1.clear (); S2.clear (); for (int. i=0;i<5;i++) {for (int j=0;j<n;j++) {cin>>a[i][j];}} for (int i=0;i<n;i++) {for (int j=0;j<n;j++) {S1.insert (a[0][i]+a[1][j]); S2.insert (A[2][i]+a[3][j]);}} for (int i=0;i<n;i++) a[4][i]*= ( -1), int len1=0,len2=0;set<long long>::iterator it;for (It=s1.begin (); it!= S1.end (); it++) sum[1][len1++]= (*it); for (It=s2.begin (); It!=s2.end (); it++) sum[2][len2++]= (*it); int ok1=0;for (int i= 0;i<n;i++) {int ok=1;int j=0,k=len2-1;while (Sum[1][j]+sum[2][k]!=a[4][i]) {if (Sum[1][j]+sum[2][k]<a[4][i]) j+ +;else K--;if (j>=len1| | k<0) {ok=0;break;}} if (OK) {ok1=1;break;}} if (OK1) cout<< "Yes" <<endl;else cout<< "No" <<ENDL;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

! HDU 4,334 sets each one number and 0 whether there is-thinking, card time-(magic of the pointer)

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.