poj 2549 双向搜索

Sumsets
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8665 Accepted: 2395

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

5
2 
3 
5 
7 
12
5
2 
16 
64 
256 
1024
0

Sample Output

12
no solution

Source

Waterloo local 2001.06.02

n<=1000, a+b+c=d求解,四个数互不相同。直接枚举abc3个数的话就是10^9了太慢。尝试枚举两个,转化为a+b=d-c。先枚举两个值a和b,存下a+b,O(n^2)。再枚举c和d,然后二分查找是否有相等的O(n^2logn)。ac,110ms。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>

using namespace std;

#define maxn 1001

int arr[maxn];

struct node
{
    int val;
    int a, b;

    node(){}
    node(int v, int l, int r) { val = v; a = l; b = r;}
    bool operator <(const node &sec) const
    {
        return val < sec.val;
    }
};

node sum[maxn*maxn];

int main()
{
    int n;
    while(scanf("%d", &n)!=EOF){
        if(!n) break;

        for(int i = 0; i < n; i++)
            scanf("%d", arr+i);
        sort(arr, arr+n);

        int cnt = 0;
        for(int i = 0; i < n; i++)
            for(int j = i+1; j < n; j++)
            sum[cnt++] = node(arr[i]+arr[j], i, j);

        sort(sum, sum+cnt);
        int flag = 0, ans=-0x3fff3f3f;
        for(int i = n-1; i >= 0 && !flag; i--)
        for(int j = 0; j < n && !flag; j++){
            if(i == j) continue;
            int diff = arr[i]-arr[j];
            int pos = lower_bound(sum, sum+cnt, node(diff,0,0))-sum;
            while(pos < cnt && sum[pos].val==diff){
                    int a = sum[pos].a , b = sum[pos].b;
                    if( i != a && i!= b && j != a && j != b){
                        flag = 1;
                        ans = arr[i];
                        break;
                    }
                    pos++;
            }
        }
        if(flag)
            printf("%d\n", ans);
        else printf("no solution\n");

    }
    return 0;
}


用hash查找,63ms

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>

using namespace std;

#define maxn 1001
#define inf 0x3f3f3f3f
#define M 713117

int arr[maxn];

struct node
{
    int val;
    int a, b;
    int next;

    node(){}
    node(int v, int l, int r, int n) { val = v; a = l; b = r; next = n;}
    bool operator <(const node &sec) const
    {
        return val < sec.val;
    }
};

node h[maxn*maxn];
int first[M], top;

void insert(int val, int a, int b)
{
    int pos = (val+inf)%M;
    h[top] = node(val, a, b, first[pos]);
    first[pos] = top++;
}

int search(int val, int c, int d)
{
    int pos = (val+inf)%M;
    for(int i  = first[pos]; i != -1; i = h[i].next)
    if(h[i].val == val){
        int a= h[i].a, b = h[i].b;
        if(a!=c && a!=d && b!=c && b!=d) return 1;
    }
    return 0;
}

int main()
{
    int n;
    while(scanf("%d", &n)!=EOF){
        if(!n) break;
        top = 0;
        memset(first, -1, sizeof(first));

        for(int i = 0; i < n; i++)
            scanf("%d", arr+i);
        sort(arr, arr+n);

        int cnt = 0;
        for(int i = 0; i < n; i++)
            for(int j = i+1; j < n; j++)
            insert(arr[i]+arr[j],arr[i], arr[j]);

        int flag = 0, ans=-0x3fff3f3f;
        for(int i = n-1; i >= 0 && !flag; i--)
        for(int j = 0; j < n && !flag; j++){
            if(i == j) continue;
            int diff = arr[i]-arr[j];
            flag = search(arr[i]-arr[j], arr[j], arr[i]);
            if(flag) ans = arr[i];
        }

        if(flag)
            printf("%d\n", ans);
        else printf("no solution\n");

    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值