Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

Submit
Status

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

5
2
3
5
7
12
5
2
16
64
256
1024
0

Sample Output

12
no solution

Source

Waterloo local 2001.06.02

/*在数列中找四个数使a+b+c=d;转化之后得a+b=d-c,先对数列排序列举每一个d-c,
二分查找a和b*/
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define MAX 100100
#define INF -0x3f3f3f
int a[MAX];
int main()
{
int n;
while(scanf("%d",&n),n)
{
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);
int ans=INF;
for(int i=n-1;i>=0;i--)
{
for(int j=n-1;j>=0;j--)
{
if(i==j)
continue;
int sum=a[i]-a[j];
int l=0,r=j-1;
while(l<r)
{
if(a[l]+a[r]==sum)
{
ans=a[i];
break;
}
if(a[l]+a[r]>sum)
r--;
else
l++;
}
if(ans!=INF) break;
}
if(ans!=INF) break;
}
if(ans!=INF)
printf("%d\n",ans);
else
printf("no solution\n");
}
return 0;
}

poj--2549--Sumsets(二分查找)的更多相关文章

  1. POJ 2549 Sumsets(折半枚举+二分)

    Sumsets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11946   Accepted: 3299 Descript ...

  2. POJ 2549 Sumsets

    Sumsets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10593   Accepted: 2890 Descript ...

  3. poj 2452(RMQ+二分查找)

    题目链接: http://poj.org/problem?id=2452 题意:在区间[1,n]上找到满足 a[i]<a[k]<a[j] (i<=k<=j) 的最大子区间 (j ...

  4. POJ 2549 Sumsets hash值及下标

    题目大意:找到几何中的4个数字使他们能够组成 a+b+c=d , 得到最大的d值 我们很容易想到a+b = d-c 那么将所有a+b的值存入hash表中,然后查找能否在表中找到这样的d-c的值即可 因 ...

  5. poj 3122 (二分查找)

    链接:http://poj.org/problem?id=3122 Pie Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1 ...

  6. POJ 3061 Subsequence 二分查找

    题目大意:给出长度为n的一个序列,给出一个数字S,求长度最短的序列和大于等于S的连续子序列,输出该长度,如果没有答案输出0. 题目思路:看数据范围,这道题就是卡时间的.我们可以用sum[i]记录前i项 ...

  7. poj 3122(二分查找)

    Pie Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13564   Accepted: 4650   Special Ju ...

  8. [poj] 2549 Sumsets || 双向bfs

    原题 在集合里找到a+b+c=d的最大的d. 显然枚举a,b,c不行,所以将式子移项为a+b=d-c,然后双向bfs,meet int the middle. #include<cstdio&g ...

  9. POJ 3273 Monthly Expense(二分查找+边界条件)

    POJ 3273 Monthly Expense 此题与POJ3258有点类似,一开始把判断条件写错了,wa了两次,二分查找可以有以下两种: ){ mid=(lb+ub)/; if(C(mid)< ...

  10. POJ 1064 Cable master(二分查找+精度)(神坑题)

    POJ 1064 Cable master 一开始把 int C(double x) 里面写成了  int C(int x) ,莫名奇妙竟然过了样例,交了以后直接就wa. 后来发现又把二分查找的判断条 ...

随机推荐

  1. UI设计师不可不知的安卓屏幕知识-安卓100分享

    http://www.android100.org/html/201505/24/149342.html UI设计师不可不知的安卓屏幕知识-安卓100分享 不少设计师和工程师都被安卓设备纷繁的屏幕搞得 ...

  2. remote debug

    https://docs.microsoft.com/en-us/visualstudio/debugger/remote-debugging https://docs.microsoft.com/e ...

  3. linux执行shell脚本时提示bad interpreter:No such file or directory的解决办法

    故障现象:在终端直接cd /var正常,在shell脚本中执行则报错.原因是脚本是在windows平台下写的,换行符与Linux不同,造成脚本不能正确执行 出现bad interpreter:No s ...

  4. SQLSERVER 链接服务器

    1. 执行代码 EXEC sp_addlinkedserver @server='XLZFSqlServer', --链接服务器别名 @srvproduct='', @provider='SQLOLE ...

  5. border-spacing和borer-collapse以及empty-cells属性

    1.border-spacing 用于设置表格中,单元格之间的间隙 示例1:border-spacing: 7px: 单元格水平方向7px间隔,垂直方向7px间隔 示例2:border-spacing ...

  6. VC工程里,如何编译汇编语言的文件

    最近老是有朋友问,VC工程里,如何编译汇编语言的文件,接下来就说一下步骤: 1.将汇编语言文件,加入到工程里: 2.在Solution Explorer窗口中右键单击Visual C++项目,选择Bu ...

  7. UNP学习笔记4——I/O复用:select和poll函数

    1 概述 之间的学习中发现,传统的阻塞式系统调用不仅浪费进程运行时间,而且会带来狠毒问题.因此进程需要有一种预先告知内核的能力,使得内核一旦发现进程指定的一个或者多个I/O条件就绪,它就通知进程.这个 ...

  8. 第三章 Python函数

    函数 如果在开发程序时,需要某块代码多次,但是为了提⾼编写的效率以及代码的重⽤,所以把具有独⽴功能的代码块组织为⼀个⼩模块,这就是函数 函数的定义 函数定义的三种方式:无参函数,有参函数,空函数 #无 ...

  9. Java用freemarker导出Word 文档

    1.用Microsoft Office Word打开word原件: 2.把需要动态修改的内容替换成***,如果有图片,尽量选择较小的图片几十K左右,并调整好位置: 3.另存为,选择保存类型Word 2 ...

  10. 路飞学城Python-Day24

    12.粘包现象 客户端接收的信息指定了的字节,TCP协议没有丢失协议,只是只能接收指定的字节数,于是产生出了粘包现象 服务端接收命令只能接收1024字节,服务端执行命令结果以后传输给客户端,客户端再以 ...