uva10125 (中途相遇法)

a + b = d - c;

枚举a  + b 加入哈希表,再枚举 d - c 在哈希表中查找,并且四个数字不能重复

/***********************************************
 * Author: fisty
 * Created Time: 2015/3/17 22:36:27
 * File Name   : uva10125.cpp
 *********************************************** */
#include <iostream>
#include <cstring>
#include <deque>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;
#define Debug(x) cout << #x << " " << x <<endl
#define Memset(x, a) memset(x, a, sizeof(x))
const int INF = 0x3f3f3f3f;
typedef long long LL;
typedef pair<int, int> P;
#define FOR(i, a, b) for(int i = a;i < b; i++)
#define MAX_N 1100
int n;   
int a[MAX_N];
int cnt = 0;
struct Point{
    int sum;
    int x;
    int y;
}p;
const int maxsize = MAX_N * MAX_N;
int data_cnt;
Point data[maxsize];
int head[maxsize];
int next[maxsize];

int hash(int x){
    int t = (x << 1) + (x >> 1);
    return (t & 0x7f7f7f7f) % maxsize;
}
void insert(Point x){
    data[data_cnt] = x;
    int key = hash(x.sum);
    next[data_cnt] = head[key];
    head[key] = data_cnt;
    data_cnt++;
}
int find(int x, int y, int sum){
    int s = head[hash(sum)];
    for(int i = s;i != -1; i = next[i]){
        if(sum == data[i].sum){
            int flag = 1;
            if(x == data[i].x || y == data[i].y)
                flag = 0;
            if(flag) return 1;
        }
    }
    return 0;
}
int main() {
    //freopen("in.cpp", "r", stdin);
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(cin >> n){
        if(!n) break;
        Memset(head, -1);
        Memset(next, -1);
        cnt = 0;
        FOR(i, 0, n){
            cin >> a[i];
        }
        for(int i = 0;i < n; i++){
            for(int j = i+1;j < n; j++){
                p.sum = a[i] + a[j];   //将A+B 插入
                p.x = i;
                p.y = j;
                insert(p);
            }
        }
        int ok = 0;
        int _max = -INF;
        for(int i = 0;i < n; i++){
            for(int j = i+1;j < n; j++){
                int d = a[i] - a[j];
                if(find(i, j, d) && a[i] > _max){
                    ok = 1;
                    _max = a[i];
                }
                d = a[j] - a[i];
                if(find(j, i, d) && a[j] > _max){
                    ok = 1;
                    _max = a[j];
                }
            }
        }
        if(ok)
            cout << _max <<endl;
        else
            cout << "no solution" << endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值