Sumsets
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11612   Accepted: 3189

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

/*
* @Author: Lyucheng
* @Date: 2017-08-02 22:10:24
* @Last Modified by: Lyucheng
* @Last Modified time: 2017-08-04 20:32:30
*/
/*
题意:给你n个数,让你找出最大的d=a+b+c 思路:3sum问题先转化成2sum问题,先处理出任意两个数的和,然后二分查找d-c的值是不是存在,并且组成d-c的值
的两个加数是不是d和c,这个算法有个漏洞,就是如果d-c的值有多个,二分只能找到其中的一个,但是数据很水
所以就水过去了。
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h> #define MAXN 1005 using namespace std; struct Node{
int x,y;
int val;
bool operator < (const Node & other) const {
return val<other.val;
}
}node[MAXN*MAXN];//存放两数之和 int n;
int a[MAXN];
int res;
int tol; inline int findx(int x){
int l=,r=tol-,m;
while(l<=r){
m=(l+r)/;
if(node[m].val==x){
return m;
}else if(node[m].val<x){
l=m+;
}else{
r=m-;
}
}
return -;
}
void init(){
tol=;
res=;
} int main(){
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(scanf("%d",&n)!=EOF&&n){
init();
for(int i=;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);
for(int i=;i<n;i++){//n^2的时间处理一下
for(int j=i+;j<n;j++){
node[tol].val=a[i]+a[j];
node[tol].x=i;
node[tol].y=j;
tol++;
}
} sort(node,node+tol); bool flag=false;
for(int i=n-;i>=;i--){
for(int j=;j<n;j++){
if(j==i) continue;
int cnt=a[i]-a[j];
int pos=findx(cnt);
if(pos==-) continue;
else {
if(min(node[pos].x,node[pos].y)!=min(i,j)&&max(node[pos].x,node[pos].y)!=max(i,j)){
printf("%d\n",a[i]);
flag=true;
break;
}
}
}
if(flag==true){
break;
}
}
if(flag==false){
puts("no solution");
}
}
return ;
}

poj 2459 Sumsets的更多相关文章

  1. POJ 2229 Sumsets

    Sumsets Time Limit: 2000MS   Memory Limit: 200000K Total Submissions: 11892   Accepted: 4782 Descrip ...

  2. poj -2229 Sumsets (dp)

    http://poj.org/problem?id=2229 题意很简单就是给你一个数n,然后选2的整数幂之和去组成这个数.问你不同方案数之和是多少? n很大,所以输出后9位即可. dp[i] 表示组 ...

  3. POJ 2549 Sumsets

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

  4. poj 2220 Sumsets

                                                                                                     Sum ...

  5. poj 2229 Sumsets(dp)

    Sumsets Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 400000/200000K (Java/Other) Total Sub ...

  6. poj 2229 Sumsets 完全背包求方案总数

    Sumsets Description Farmer John commanded his cows to search for different sets of numbers that sum ...

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

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

  8. POJ 2549 Sumsets hash值及下标

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

  9. poj 2229 Sumsets DP

    题意:给定一个整数N (1<= N <= 1000000),求出以 N为和 的式子有多少个,式子中的加数只能有2的幂次方组成 如5 : 1+1+1+1+1.1+1+1+2.1+2+2.1+ ...

随机推荐

  1. 业余草通告CSDN博客用户zhang__ao非法转载文章的公告

    今天早上有粉丝给我反馈,CSDN的一位用户大量非法的转载了我的个人网站:业余草(www.xttblog.com)上的大量文章.现一对该用户转载业余草上网站上的所有文章进行了举报! 从上图中可以看出,该 ...

  2. Spring定时器的使用详解

    写个最简单的demo吧,反正睡前没什么事儿,来祸害一下园子~~虽然我菜,但是我不会承认啊,哈哈哈 明天详细补充点儿吧,很晚了,不睡觉的程序员不是好程序员,我总能给自己找借口~~~ //spring开启 ...

  3. JAVA编码互转(application/x-www-form-urlencoded)

    本质上来说,java.net.UrlEncoder适用于将 String 转换为 application/x-www-form-urlencoded MIME 格式的静态方法 时 ,使用 但!一般情况 ...

  4. js循环生成多个easyui datagrid数据网格时,初始化表格

    $.each( content, function(i, item){ var info_tpl = "";var result_tpl = "";var pr ...

  5. python爬取煎蛋网图片

    ``` py2版本: #-*- coding:utf-8 -*-#from __future__ import unicode_literimport urllib,urllib2,timeimpor ...

  6. IDEA Maven 三层架构 2、运行 springMVC

    运行 SpringMVC 首先要理解 SpringMVC 应用程序的入口是配置文件 web.xml,其路径为"src/main/webapp/WEB-INF/web.xml",通过 ...

  7. 3.ElasticSearch的倒排索引

    一. 正向索引 常规的索引建立方式 文档---> 关键词的映射过程(正向索引) 比如: 我有很多个文章,如果想查询其中几个文章是否含有刘耀这个关键词,那么我就需要打开所以文章,找到里面含义刘耀的 ...

  8. 知识树杂谈Java面试(4)

    一. Java集合 1.  集合分类:  Collection.Map. 2. Collection: 3. Map 4. 注意点 a. List 有序.可重复:Set 无序.不可重复:Map  键值 ...

  9. 前端笔记----jquery入门知识点总结

    一.jquery的加载方法 $(document).ready(function(){js代码}); $(function(){js代码});(一般使用这个); 注意点1:使用jquery必须先导入函 ...

  10. hdu 1018 共同交流~

    Big Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...