UVA 10125(p89)----Sumsets

#include<bits/stdc++.h>
#define debu
using namespace std;
typedef long long LL;
const int maxn=1e3+50;
const LL INF=1e10;
int n;
set<LL> s;
LL a[maxn];
map<LL,map<LL,LL> > num;
int check(LL x,LL y)
{
    LL tmp=x-y;
    /*if(s.count(tmp))
    {
        cout<<x<<" "<<y<<" "<<tmp<<endl;
        cout<<num[tmp][0]<<" "<<num[tmp][1]<<endl;
    }*/
    return (s.count(tmp)&&num[tmp][0]!=x&&num[tmp][1]!=x&&num[tmp][0]!=y&&num[tmp][1]!=y);
}
void solve()
{
    for(int i=0; i<n; i++)
        for(int j=i+1; j<n; j++)
        {
            LL sum=a[i]+a[j];
            num[sum][0]=a[i];
            num[sum][1]=a[j];
            s.insert(sum);
        }
    LL ans=-INF;
    for(int i=0; i<n; i++)
        for(int j=0; j<n; j++)
        {
            if((i!=j)&&check(a[i],a[j]))
                ans=max(ans,a[i]);
        }
    if(ans==-INF) printf("no solution\n");
    else printf("%lld\n",ans);
}
int main()
{
#ifdef debug
    freopen("in.in","r",stdin);
#endif // debug
    while(scanf("%d",&n)==1&&n)
    {
        s.clear();
        num.clear();
        for(int i=0; i<n; i++)
            scanf("%lld",&a[i]);
        solve();
    }
    return 0;
}

题目地址:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1066

题解:中途相遇法。n^2枚举所有a+b的和,n^2枚举两数相减,map[x][0],map[x][1],表示对于和x,使用的数为map[x][0],map[x][1],set记录x是否出现过,若x已存在且两数未使用则更新答案。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值