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. ACM学习之路___HDU 5723(kruskal + dfs)

    Abandoned country Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s) ...

  2. angularui 分页

    分页组件的使用 <!DOCTYPE html> <html lang="en" ng-app="myApp"> <head> ...

  3. ThinkPHP中:RBAC权限控制的实习步骤

    使用版本ThinkPHP3.1.3 第一步,建表及数据 第二步,建关联模型 第三步,控制器使用关联模型.配置文件 第四步,模板显示数据 第一步,建表及数据 在数据库中,建立一个companysvn数据 ...

  4. #define WIN32_LEAN_AND_MEAN

    不加载MFC所需的模块.用英语解释:Say no to MFC如果你的工程不使用MFC,就加上这句,这样一来在编译链接时,包括最后生成的一些供调试用的模块时,速度更快,容量更小.不过对于较大工程,MF ...

  5. JSP入门3 Servlet

    需要继承类HttpServlet 服务器在获得请求的时候会先根据jsp页面生成一个java文件,然后使用jdk的编译器将此文件编译,最后运行得到的class文件处理用户的请求返回响应.如果再有请求访问 ...

  6. WINDOWS XP中用命令行管理用户 net user命令

    net user <username> [password or *] [/add] [options] [/domain] net user <username] /delete ...

  7. snmp之GenericAddress

    GenericAddress 注册地址类型,而不是默认的,第一次调用解析(java.lang.String的)方法之前,设置系统属性ADDRESS_TYPES_PROPERTIES. 这个类涉及到了工 ...

  8. 分享基于分布式Http长连接框架--设计模型

    追求简单的设计. 也许你的设计功能很强大,但能够在满足你需求的前提下尽量简单明了设计. 当你的设计过于复杂的时候想想是不是有其它路可以走,你站在别人的角度想下,如果别人看了你的设计会不会心领神会,还是 ...

  9. Python自学笔记-面向对象相关(Mr seven)

    ---恢复内容开始--- http://www.cnblogs.com/wupeiqi/articles/5433893.html 类的成员可以分为三大类:字段.方法和属性. 一.字段 字段包括:普通 ...

  10. Mvc 流程调用分析

    链接地址 https://www.processon.com/view/link/59e71fbbe4b09000f03ce78e 总结: 1. 在Global.ascx 中我们使用RouteColl ...