Sumsets
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10005 | Accepted: 2737 |
Description
![](https://i-blog.csdnimg.cn/blog_migrate/d903c9a08afdf3b6dfac35b18da4660b.jpeg)
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
分析:
先用俩层for循环找出最大的和第二最大的,然后用二分求剩下两个就oK了。
代码:
#include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<iostream> #include<stack> #include<vector> #include<cstdlib> #define SI(x) scanf("%d",&x) #define PI(x) cout<<x<<endl #define FOR(i,x,n) for(i=x;i<n;i++) #define mem(x,y) memset(x,y,sizeof(x)) #define O_O(x) while(x--) #define SS(x) scanf("%s",x) #define SC(x) cin>>x #define SL(x) cin>>x #define PL(x) cout<<x<<endl #define PS(x) cout<<x<<endl #define PC(x) cout<<x<<endl #define R return #define inf -600000000 #define max(a,b) a>b?a:b #define min(a,b) a<b?a:b using namespace std; int pp[1010]; int main() { int n; while(SI(n),n) { for(int i=0;i<n;i++) SI(pp[i]); sort(pp,pp+n); n--; int ans=inf; for(int i=n;i>=0;i--) { for(int j=n;j>=0;j--) { if(i==j) continue; int sum=pp[i]-pp[j]; for(int l=0,r=j-1;l<r;) { if(pp[l]+pp[r]==sum) { ans=pp[i]; break; } else if(pp[l]+pp[r]>sum) r--; else l++; } } if(ans!=inf) break; } if(ans==inf) printf("no solution\n"); else printf("%d\n",ans); } R 0; }