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. Mac OS下PHP开发环境的搭建——基于XAMPP和IntelliJ IDEA

    简单记录一下在MacOS下,搭建PHP的开发环境吧.其实,从本质上来说,Mac对于PHP的支持还是很好的,默认带了PHP和Apache,但是由于前期对系统本身不熟悉,所以还是略微走了一些弯路--也就是 ...

  2. Ruby学习笔记(二)——从管道读取数据

    在对文件名修改后,今天又给自己出了新的难题,想从实验结果中提取数据,并将其作为文件夹的名称.其中,比赛的主办方提供的评估算法是用perl写的,因此读取实验结果最为简单的想法自然是使用管道命令,即 ./ ...

  3. caioj1496: [视频]基于连通性状态压缩的 动态规划问题:Manhattan Wiring

    %%%%orz苏大佬 虽然苏大佬的baff吸不得,苏大佬的梦信不得,但是膜苏大佬是少不得的囧 这题还是比较有收获的 哼居然有我不会做的插头DP 自己yy了下,2表示属于2的插头,3表示3的插头 假如当 ...

  4. electron-vue中使用iview 报错this. is readonly的解决办法

    title: electron-vue中使用iview 报错this. is readonly的解决办法 toc: false date: 2019-02-12 19:33:28 categories ...

  5. CaffeNet用于Flickr Style数据集上的风格识别

    转自 http://blog.csdn.net/liumaolincycle/article/details/48501423 微调是基于已经学习好的模型的,通过修改结构,从已学习好的模型权重中继续训 ...

  6. Selenium键盘鼠标操作总结

    鼠标操作 org.openqa.selenium.interactions.Actions 1.给元素设置焦点. 有时对于a标签等,为了不跳转到别的链接,但是需要设置焦点时就可使用. action.m ...

  7. 【转】C#详解值类型和引用类型区别

    通用类型系统 值类型 引用类型 值类型和引用类型在内存中的部署 1 数组 2 类型嵌套 辨明值类型和引用类型的使用场合 5 值类型和引用类型的区别小结   首先,什么是值类型,什么是引用类型? 在C# ...

  8. DWZ选项卡

    <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> &l ...

  9. CREC 2017

    A:Assignment Algorithm #include <bits/stdc++.h> using namespace std; ][]; ][],n,m,ans=,A,B,l=, ...

  10. Eclipse本地创建新的GIT分支,并推送至远程Git分支

    本地创建新的GIT分支: 1.右击要创建新分支的项目——Team——Switch To——New Branch…: 2.在弹出的对话框中name框中输入要创建的分支名称,(如果是当前显示的要拷贝的分支 ...