UVa 10125:sumsets

Source: Internet
Author: User
Tags bool hash integer printf range sort

Topic Link:

Uva:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&itemid=8&category=24&page=show_ problem&problem=1066

poj:http://poj.org/problem?id=2549

Type: hash, two-point lookup

Original title:

Given S, a set of integers, find the largest d such that A + B + c = d where a, B, C, and D are distinct of S.

Input

Several s, each consisting of a line containing an integer 1 <= n <= 1000 indicating the number of elements in S, fo Llowed by the elements of S. Each element of S is a distinct integer between-536870912 and +536870911 inclusive. The last line of input contains 0.

Output

For each S, a single line containing d, or a single line containing "no solution".

Sample Input

5
2 
3 
5 
7
5
2 
256 
1024
0

Output for Sample Input


No solution

The main effect of the topic;

Give a set of integers between 536870912 and 536870911 to find A + B + c = d, the largest one D output. Where A,b,c,d belong to set S, and they are all different.

Analysis and Summary:

The simplest approach is three-tier for loop, Complexity O (n^3), and N Max is 1000, which is bound to timeout. So we need to convert a + b + c = d to D-c = A+b.

Where a+b can be obtained in advance, you can enumerate D and C with two-tier for loops, and the complexity becomes O (n^2).

The key to this question is to determine whether the a,b,c,d is different number, so in the calculation of the a+b and, but also the A and B in the set S in the subscript record down, you can use a structural question to guess. Consider the set a+b as SUM, and then enumerate t=d-c to determine if T is in sum, and if so, whether the d,c coordinates are in conflict with the subscript of the element in sum equal to T.

The first way to find it is to sort the sum first and then find it directly. Running time: 0.112s (UVa), 250MS (POJ)

* * UVA 10125-sumsets * time:0.112s (UVA), 250MS (POJ) * author:d_double * */#include &LT;IOSTR  
eam> #include <cstring> #include <cstdio> #include <algorithm> const int MAXN = 1003;  
     
using namespace Std;  
     
int S[MAXN], n, ans;  
    struct node{int sum;  
    int A, B;  
    friend BOOL operator < (const node &a, const node &b) {return a.sum < b.sum;  
}  
};  
Node SUM[MAXN*MAXN];  
     
int rear;  
    BOOL Solve () {Node tmp;  
    Ans =-2147483646;  
            for (int i=n-1; i>=0;-i) {for (int j=0; j<n; ++j) if (i!=j) {int t = s[i]-s[j]; Tmp.sum = t; Tmp.a=i;   
            Tmp.b=j;  
            node* p = lower_bound (sum, sum+rear, TMP); if (p->sum==t && S[i]>ans) {while (p->sum = = t) {if (p->a!=i) ;& p->a!=j && p->b!=i && p->b!=j) {ANS = S[i];  
                    Because S[i] is a large to small enumeration, so once found it must be the maximum return true;  
                } ++p;  
}}} return false;  
        int main () {while (scanf ("%d", &n), N) {for (int i=0; i<n; ++i) scanf ("%d", &s[i]);  
        Sort (S, s+n);  
        Rear = 0;  
                for (int i=0; i<n; ++i) {for (int j=0; j<n; ++j) if (i!=j) {sum[rear].sum = S[i]+s[j];  
            Sum[rear].a=i, Sum[rear++].b=j;  
        } sort (sum, sum+rear);  
        if (solve ()) printf ("%d\n", ans);  
    else printf ("no solution\n");  
return 0; }

The second method is to use hashing to find.

Use a hash table to note that, because the data range is -536870912~536870911, there are negative numbers, so to let each value plus 536870912 to a non-negative number, then the data range becomes 0~536870912+536870911, and then hash transcoding, It's obvious that two numbers can add up to 32-bit int, so long long

Running time: 0.080s (UVA), 219MS (POJ)

* * UVA 10125-sumsets * Hash version * time:0.080 s (UVA), 219 MS (POJ) * author:d_double * * * */#include <iostre  
am> #include <cstring> #include <cstdio> #include <algorithm> const int MAXN = 1003;  
Const long Long ADD = 536870912;  
     
using namespace Std;  
     
int n, S[MAXN], ans;  struct node{long long sum;  
To use long long int A, b;  
     
};  
Node SUM[MAXN*MAXN];  
     
int rear;  
const int hashsize = MAXN*MAXN;  
     
int head[hashsize], NEXT[MAXN*MAXN];   
    inline void init_lookup_table () {rear=1;   
memset (head, 0, sizeof);   
inline int hash (long long key) {return (int) (Key & 0x7fffffff)% hashsize);  
    } inline bool Try_to_insert (int s) {int h = hash (sum[s].sum);  
    int u = head[h];  
    while (u) {u = next[u];  
    } Next[s] = Head[h];  
    HEAD[H] = s;  
return true; } inline bool Search (Node &s) {int h = hash (s.sum);  
    int u = head[h]; while (U) {if (sum[u].sum==s.sum && sum[u].a!=s.a && sum[u].a!=s.b && sum[u].b!=s.a && Amp  
        sum[u].b!=s.b) {return true;  
    U = next[u];  
return false;  
    BOOL Solve () {Node tmp;  
    Ans =-2147483646; for (int i=n-1; i>=0;-i) {for (int j=0; j<n; ++j) if (i!=j) {long Long T = s[i]-s[j] + ADD + A  
            DD;  Tmp.sum = t; Tmp.a=i;  
            Tmp.b=j;  
            if (search (tmp)) {ans = s[i]; return true;  
}} return false;  
     
        int main () {while (scanf ("%d", &n), N) {for (int i=0; i<n; ++i) scanf ("%d", &s[i]);  
        Sort (S, s+n);  
        Init_lookup_table (); for (int i=0; i<n; ++i) {for (int j=0; j<n; ++j) if (i!=j) {sum[rear].sum = S[i]+add+s[j  
                ]+add; Sum[rear].a=i; Sum[rear].b=j;  
                Try_to_insert (rear);  
            ++rear;  
        } if (solve ()) printf ("%d\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.