UVA 10125 Sumsets

Source: Internet
Author: User
Tags bool hash


Original question:
Given S, a set of integers, find the largest d such that A + B + c = d
Where A, B, C, and D are distinct elements of S.
Input
Several S, each consisting of a line containing an integer 1≤n≤
indicating the number of elements in S, followed by the elements
of S, one per line. 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
12
5
2
16
64
256
1024
0
Sample Output
12
No solution
English:
Give you a bunch of non-repeating numbers, and now let you figure out such a maximum number D, satisfying the condition A+b+c=d, where a,b,c,d are different. No more than 1000 of the number



Using Set


#include <bits/stdc++.h>
using namespace std;
//fstream in,out;
struct node
{
    int num;
    int res;
    node(int n,int r){ num = n, res = r;}
    bool operator<(const node& n) const
    {
        return res<n.res;
    }
};
vector<node> sum;
vector<int> vi;
int main()
{
    ios::sync_with_stdio(false);
    int n;
//    in.open("in.txt");
//    out.open("out.txt");
    while(cin>>n,n)
    {
        sum.clear();
        vi.clear();
        for(int i=0;i<n;i++)
        {
            int res;
            cin>>res;
            vi.push_back(res);
        }
        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                int s=vi[i]+vi[j];
                sum.push_back(node(vi[i],s));
            }
        }
        set<node> sn(sum.begin(),sum.end());
        sort(vi.begin(),vi.end());
        int flag=1;
        for(int i=vi.size()-1;i>=0;i--)
        {
            for(int j=0;j<vi.size();j++)
            {
                if(i==j)
                continue;
                int sub=vi[i]-vi[j];
                auto sit=sn.find(node(0,sub));
                if(sit==sn.end())
                continue;
                int a=sit->num;
                int b=sit->res-a;
                int c=vi[j];
                int d=vi[i];
                if(a!=c&&a!=d&&b!=c&&b!=d)
                {
                    cout<<d<<endl;
                    flag=0;
                    break;
                }
            }
            if(!flag)
            break;
        }
        if(flag)
            cout<<"no solution"<<endl;
    }
//    in.close();
//    out.close();
    return 0;
}


Use hash


#include <bits / stdc ++. h>
using namespace std;
// fstream in, out;
typedef unordered_set <int> usi;

struct node
{
    int num;
    int res;
    node (int n, int r) {num = n, res = r;}
    bool operator == (const node & n) const /// overloaded == operator
    {
        return res == n.res;
    }
};
struct myhash
{
    size_t operator () (const node & n) const
    {
        usi :: hasher fn = usi (). hash_function ();
        return fn (n.res);
    }
};
vector <node> sum;
vector <int> vi;
int main ()
{
    ios :: sync_with_stdio (false);
    int n;
// in.open ("in.txt");
// out.open ("out.txt");
    while (cin >> n, n)
    {
        sum.clear ();
        vi.clear ();
        for (int i = 0; i <n; i ++)
        {
            int res;
            cin >> res;
            vi.push_back (res);
        }
        for (int i = 0; i <n; i ++)
        {
            for (int j = i + 1; j <n; j ++)
            {
                int s = vi [i] + vi [j];
                sum.push_back (node (vi [i], s));
            }
        }
        unordered_set <node, myhash> sn (sum.begin (), sum.end ());
        sort (vi.begin (), vi.end ());
        int flag = 1;
        for (int i = vi.size ()-1; i> = 0; i--)
        {
            for (int j = 0; j <vi.size (); j ++)
            {
                if (i == j)
                continue;
                int sub = vi [i] -vi [j];
                auto sit = sn.find (node (0, sub));
                if (sit == sn.end ())
                continue;
                int a = sit-> num;
                int b = sit-> res-a;
                int c = vi [j];
                int d = vi [i];
                if (a! = c && a! = d && b! = c && b! = d)
                {
                    cout << d << endl;
                    flag = 0;
                    break;
                }
            }
            if (! flag)
            break;
        }
        if (flag)
            cout << "no solution" << endl;
    }
// in.close ();
// out.close ();
    return 0;
} 


Answer:



POJ above has a similar topic, give you four arrays, let you in this four array to select the number, yes these four numbers plus and 0.
Similar to the above problem, A+b+c=d can be converted to a+b=d-c, enumerate a+b values, have a hash table or a lookup tree, and then enumerate the values of d-c, and find them in a hash table or lookup tree. Be careful to determine if the ABCD four numbers are duplicated.


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.