【哈希】1551:Sumsets

本文探讨了如何解决一个特定的算法问题:寻找整数集合中是否存在四个不同的元素,使得它们之和等于其中一个元素。通过使用哈希表来优化搜索过程,文章详细介绍了算法的设计与实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1551:Sumsets

总时间限制: 
1000ms
内存限制: 
65536kB
描述
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.
输入
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.
输出
For each S, a single line containing d, or a single line containing "no solution".
样例输入
5
2 
3 
5 
7 
12
5
2 
16 
64 
256 
1024
0
样例输出
12
no solution
来源
Waterloo local 2001.06.0

[cpp]  view plain  copy
  1. #include<stdio.h>  
  2. #include<queue>  
  3. #include<math.h>  
  4. #include<string.h>  
  5. #include<iostream>  
  6. #include<stdlib.h>  
  7. #include<algorithm>  
  8. using namespace std;  
  9. int flag[1000010];//标记是否存有数据   
  10. int data[1000010];//保存和 差  
  11. int h1[1000010];//加数1  
  12. int h2[1000010];//加数2  
  13. int in[1010];//输入数据  
  14. int a,b,c,d,n;  
  15. int hashh(int key){  
  16.     int ad=(key%1000000+1001234)%1000000;//取余函数  
  17.     while(flag[ad]==1){  
  18.         ad+=ad%11+1;  
  19.         if(ad>1000000)  
  20.             ad=ad%1000000;  
  21.     }  
  22.     return ad;  
  23. }  
  24. int find(int key){  
  25.     //返回key所在位置  
  26.     int ad=(key%1000000+1001234)%1000000;//取余函数  
  27.     while(flag[ad]==1&&data[ad]!=key){  
  28.         ad+=ad%11+1;  
  29.         if(ad>1000000)  
  30.             ad=ad%1000000;  
  31.     }  
  32.     return flag[ad]==0?-1:ad;  
  33. }  
  34. int main(){  
  35.     cin>>n;  
  36.     while(n!=0){  
  37.         memset(data,-1,sizeof(data));  
  38.         memset(flag,0,sizeof(flag));  
  39.         int ans=-536870912;  
  40.         for(int i=0;i<n;i++){  
  41.             cin>>in[i];  
  42.         }  
  43.         for(int i=0;i<n;i++){  
  44.             for(int j=0;j<n;j++){  
  45.                 //遍历a+b  
  46.                 if(in[i]!=in[j]){  
  47.                 int ad=hashh(in[i]+in[j]);  
  48.                 flag[ad]=1;  
  49.                 h1[ad]=in[i];  
  50.                 h2[ad]=in[j];  
  51.                 data[ad]=in[i]+in[j];  
  52.                 }  
  53.             }  
  54.         }     
  55.         for(int i=0;i<n;i++){  
  56.             for(int j=0;j<n;j++){  
  57.                 if(in[i]!=in[j]){  
  58.                     int t=in[i]-in[j];//d-c  
  59.                     int ad=find(t);  
  60.                     if(ad!=-1&&in[i]!=h1[ad]&&in[i]!=h2[ad]&&in[j]!=h1[ad]&&in[j]!=h2[ad]){  
  61.                         ans=max(ans,in[i]);  
  62.                     }  
  63.                 }  
  64.             }  
  65.         }  
  66.         if(ans!=-536870912)  
  67.             cout<<ans<<endl;  
  68.         else  
  69.             cout<<"no solution"<<endl;  
  70.         cin>>n;  
  71.     }  
  72. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值