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. Ubuntu 16.04 安装 Wireshark分析tcpdump的pcap包——sudo apt install wireshark-qt

    tcpdump 的抓包保存到文件的命令参数是-w xxx.cap   抓eth1的包  tcpdump -i eth1 -w /tmp/xxx.cap    抓 192.168.1.123的包  tc ...

  2. 循环神经网络(RNN, Recurrent Neural Networks)介绍

    原文地址: http://blog.csdn.net/heyongluoyao8/article/details/48636251# 循环神经网络(RNN, Recurrent Neural Netw ...

  3. Java基础之对象序列化

    1. 什么是Java对象序列化 Java平台允许我们在内存中创建可复用的Java对象,但一般情况下,只有当JVM处于运行时,这些对象才可能存在,即,这些对象的生命周期不会比JVM的生命周期更长.但在现 ...

  4. nodejs免费空间

    https://www.nitrous.io/join/N_aIGoSnOMI node免费空间,可以把自己node 代码部署到云端. 也可以在线编辑,当然也能在外网地址访问到,是自己学习nodejs ...

  5. Redis学习笔记(四) 基本命令:String操作

    原文链接:http://doc.redisfans.com/string/index.html append key value 将指定的值追加到key末尾,若key不存在,则创建并赋值,返回追加后的 ...

  6. ROS-turtlesim

    前言:turtlesim是ros自带的一个功能包,应该是用于基础教学的功能包,帮助新手入门的一个实例,包括:节点,主题,服务以及参数的应用.通过学习使用turtlesim功能包可以了解ros的一些基础 ...

  7. tomcat映射路径(应用程序基本目录)的配置方法

    tomcat映射路径的配置方法 一.默认配置 位置:/conf 文件夹里的server.xml文件 <Host appBase="webapps"> appBase:可 ...

  8. Apache-TomCat安装配置

    Apache-TomCat安装配置 本文是免安装版的Tomcat!(安装JavaJDK的步骤就不多述了!) (1)官网下载地址:https://tomcat.apache.org/download-8 ...

  9. Python爬虫:爬取某网站关键词对应商品ID,且存入DB2数据库

    公司研发不给力,我就自己写了一个,专门爬关键词对应的商品ID. 其中还学会了用Python操作DB2数据库.Python发送邮件.写日志文件.处理浏览器访问限制. #!/usr/bin/python# ...

  10. GRpc-Go使用笔记

      linux下配置GRpc-golang 1.git中下载protobuf包 2.解压(/usr/local/protobuf) unzip protobuf-cpp-3.0.0-alpha-3.z ...