codeforces-2549【枚举】【特殊的二分】

来源:互联网 发布:opencv实现算法 编辑:程序博客网 时间:2024/05/17 21:38

题目链接:点击打开链接

Sumsets
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 11104 Accepted: 3036

Description

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.

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

52 3 5 7 1252 16 64 256 10240

Sample Output

12no solution

题意:在集合 S 中有 n 个数,找到最大的 d,且 d 满足于集合内 a+b+c=d。

思路:注意一下是集合,所以 a,b,c,d 均不能相同

#include<cstdio>#include<algorithm>#include<cstring>using namespace std;const int MAXN=1e6+10;int n;int a[1010];bool judge(int l,int r,int x) // 特殊的二分 {while(l<r){if(x<a[l]+a[r])r--;else if(x>a[l]+a[r])l++;elsereturn 1;}return 0;}int main(){while(scanf("%d",&n),n){for(int i=0;i<n;i++)scanf("%d",a+i);sort(a,a+n);bool flag=0;for(int i=n-1;i>=0;i--){for(int j=n-1;j>=0;j--){if(i==j)continue;if(judge(0,j-1,a[i]-a[j])){printf("%d\n",a[i]);flag=1;break;}}if(flag)break;}if(!flag)puts("no solution");}return 0; } 


0 0
原创粉丝点击