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
-
-
- #include<stdio.h>
- #include<queue>
- #include<math.h>
- #include<string.h>
- #include<iostream>
- #include<stdlib.h>
- #include<algorithm>
- using namespace std;
- int flag[1000010];//标记是否存有数据
- int data[1000010];//保存和 差
- int h1[1000010];//加数1
- int h2[1000010];//加数2
- int in[1010];//输入数据
- int a,b,c,d,n;
- int hashh(int key){
- int ad=(key%1000000+1001234)%1000000;//取余函数
- while(flag[ad]==1){
- ad+=ad%11+1;
- if(ad>1000000)
- ad=ad%1000000;
- }
- return ad;
- }
- int find(int key){
- //返回key所在位置
- int ad=(key%1000000+1001234)%1000000;//取余函数
- while(flag[ad]==1&&data[ad]!=key){
- ad+=ad%11+1;
- if(ad>1000000)
- ad=ad%1000000;
- }
- return flag[ad]==0?-1:ad;
- }
- int main(){
- cin>>n;
- while(n!=0){
- memset(data,-1,sizeof(data));
- memset(flag,0,sizeof(flag));
- int ans=-536870912;
- for(int i=0;i<n;i++){
- cin>>in[i];
- }
- for(int i=0;i<n;i++){
- for(int j=0;j<n;j++){
- //遍历a+b
- if(in[i]!=in[j]){
- int ad=hashh(in[i]+in[j]);
- flag[ad]=1;
- h1[ad]=in[i];
- h2[ad]=in[j];
- data[ad]=in[i]+in[j];
- }
- }
- }
- for(int i=0;i<n;i++){
- for(int j=0;j<n;j++){
- if(in[i]!=in[j]){
- int t=in[i]-in[j];//d-c
- int ad=find(t);
- if(ad!=-1&&in[i]!=h1[ad]&&in[i]!=h2[ad]&&in[j]!=h1[ad]&&in[j]!=h2[ad]){
- ans=max(ans,in[i]);
- }
- }
- }
- }
- if(ans!=-536870912)
- cout<<ans<<endl;
- else
- cout<<"no solution"<<endl;
- cin>>n;
- }
- }