poj 2549 Sumsets

来源:互联网 发布:淘宝的steam游戏 编辑:程序博客网 时间:2024/05/18 00:27
Sumsets
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 10551 Accepted: 2873

Description

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 <= 1000 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

52 3 5 7 1252 16 64 256 10240

Sample Output

12no solution

Source

Waterloo local 2001.06.02

提示

题意:

给你一个数的集合,从这些数中找出a+b+c=d,且输出最大d的值。

思路:

类似a1*x1*x1*x1+a2*x2*x2*x2+。。。。。。

我们可以得出a+b=d-c,之后a+b的值用哈希表存下来,之后用d-c去找用没有相同的值,注意它们都不是同一个数。也可以用a+b去进行二分查找,下面给出两种做法。

示例程序

哈希表的做法:

Source CodeProblem: 2549Code Length: 1478BMemory: 31732KTime: 532MSLanguage: GCCResult: Accepted#include <stdio.h>#include <string.h>struct{    int bug,sum,x,y;//bug表示该位置是否有数据}hash[100000][20];int a[1000],max;void insert(int x,int y){    int i=0,sum,key;    sum=a[x]+a[y];    key=abs(sum)%100000;//sum可能为负数,要用绝对值    while(hash[key][i].bug==1)    {        i++;    }    hash[key][i].bug=1;    hash[key][i].sum=sum;    hash[key][i].x=x;    hash[key][i].y=y;}void find(int x,int y){    int i=0,sum,key;    sum=a[x]-a[y];    key=abs(sum)%100000;    for(i=0;hash[key][i].bug==1;i++)    {        if(x!=hash[key][i].x&&y!=hash[key][i].y&&x!=hash[key][i].y&&y!=hash[key][i].x&&sum==hash[key][i].sum&&a[x]>max)//判断它们有没有同一个数的且比最大值大        {            max=a[x];            return;        }    }}int main(){    int i,i1,n;    scanf("%d",&n);    while(n!=0)    {        max=-1000000007;        memset(hash,0,sizeof(hash));        for(i=0;n>i;i++)        {            scanf("%d",&a[i]);        }        for(i=0;n>i;i++)        {            for(i1=0;n>i1;i1++)            {                if(i!=i1)                {                    insert(i,i1);//打表                }            }        }        for(i=0;n>i;i++)        {            for(i1=0;n>i1;i1++)            {                if(i!=i1)                {                    find(i,i1);//哈希查找                }            }        }        if(max!=-1000000007)        {            printf("%d\n",max);        }        else        {            printf("no solution\n");        }        scanf("%d",&n);    }    return 0;}
二分查找,要比哈希不知好到哪去(可能哈希算法写的不是很好,这个和平常所见到的二分不太一样):

Source CodeProblem: 2549Code Length: 1213BMemory: 392KTime: 0MSLanguage: G++Result: Accepted#include <cstdio>#include <algorithm>using namespace std;int a[1000];int f(int sum,int r)//和平常不一样的二分(也不知道是不是二分了){    int l=0;    while(l<r)    {        if(a[l]+a[r]==sum)        {            return 1;        }        else if(a[l]+a[r]>sum)        {            r--;        }        else        {            l++;        }    }    return 0;}int main(){    int i,i1,i2,n,smax,t,sum;    scanf("%d",&n);    while(n!=0)    {        smax=-1000000007;        for(i=0;n>i;i++)        {            scanf("%d",&a[i]);        }        sort(a,a+n);//排序        for(i=n-1;i>=0;i--)//从大到小比较好,找到就可以跳出了        {            for(i1=n-1;i1>=0;i1--)            {                if(i!=i1)                {                    sum=a[i]-a[i1];//枚举右边                    if(f(sum,i1-1)==1)                    {                        smax=a[i];                        break;                    }                }            }            if(smax!=-1000000007)            {                break;            }        }        if(smax==-1000000007)        {            printf("no solution\n");        }        else        {            printf("%d\n",smax);        }        scanf("%d",&n);    }    return 0;}

0 0
原创粉丝点击