Poj 2549 --- the legendary bucket used to prevent hash conflicts

Source: Internet
Author: User

Http://poj.org/problem? Id = 2549


Wikipedia has 3sum algorithm: https://en.wikipedia.org/wiki/3SUM

 sort(S); for i=0 to n-3 do    a = S[i];    k = i+1;    l = n-1;    while (k<l) do       b = S[k];       c = S[l];       if (a+b+c == 0) then          output a, b, c;          // Continue search for all triplet combinations summing to zero.           k = k + 1           l = l - 1       else if (a+b+c > 0) then          l = l - 1;       else          k = k + 1;       end       end end

Hash is used here:

1. sort and obtain the sum of any two numbers. Map the sum with (I, j) through the hash array, handle hash conflicts using the adjacent table in graph theory-the bucket hash discussed by the instructor when learning the Data Structure

2. enumerate the difference between two numbers (p, q). Find (I, j) through hash to ensure that all values of P, Q, I, and J are not equal.

Over 40 ms

# Include <cstdio> # include <cstring> # include <algorithm> # include <string> # include <iostream> # include <cmath> # include <map> # include <set> # include <queue> using namespace STD; # define ls (RT) rt * 2 # define RS (RT) rt * 2 + 1 # define ll long # define ull unsigned long # define rep (I, S, e) for (INT I = s; I <E; I ++) # define repe (I, S, e) for (INT I = s; I <= E; I ++) # define Cl (a, B) memset (a, B, sizeof (A) # define in (s) freopen (s), "R", stdin) # define out (s) freopen (S, "W", stdin) const int size = 536870911; const int mod = 9973; const int maxn = 1000 + 100; ll s [maxn]; struct node {int I; Int J; int next;} rec [1100005]; int hash [1100005]; // header node inline ll ABS (ll x) {return x> 0? X :(-x);} int main () {// In ("poj2549.txt"); int N, CNT; while (~ Scanf ("% d", & N) {Cl (hash, 0xff); For (INT I = 0; I <n; I ++) scanf ("% LLD", & S [I]); sort (S, S + n); CNT = 0; For (INT I = 0; I <n-1; I ++) for (Int J = I + 1; j <n; j ++) {rec [CNT]. I = I; REC [CNT]. j = J; ll sum = ABS (s [I] + s [J]) % MOD; REC [CNT]. next = hash [Sum]; hash [Sum] = CNT; CNT ++;} int OK = 0; ll ans = 0; For (INT I = n-1; I> 0; I --) {If (OK) break; For (Int J = 0; j <I; j ++) {If (OK) break; ll sum = ABS (s [I]-s [J]) % MOD; For (INT v = hash [Sum]; V! =-1; V = rec [v]. next) {int H = rec [v]. i; int K = rec [v]. j; If (s [H] + s [k] = s [I]-s [J] & rec [v]. i! = I & rec [v]. I! = J & rec [v]. J! = J & rec [v]. J! = I) {OK = 1; ans = s [I]; break ;}}} if (OK) printf ("% LLD \ n", ANS ); else printf ("no solution \ 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.