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. appium python andiroid自动化文档整理笔记

    from appium import webdriver import time,unittest,HTMLTestRunner class Testlogin(unittest.TestCase): ...

  2. Linux 文件查找

    在Linux系统的查找相关的命令: which 查看可执行文件的位置 whereis 查看文件的位置 locate 配合数据库查看文件位置 find 实际搜寻硬盘查询文件名称 whereis wher ...

  3. Ansible(二) - 配置及命令简介

    Ⅰ. Ansible Inventory Hosts文件配置 # mkdir /etc/ansible # touch /etc/ansible/hosts # cat /etc/hosts 127. ...

  4. php中常用的字符串大小写转换函数实例解释

    PHP字符串处理函数中,最为简单的几个函数,相关解释就不上了,直接看例子. PHP字符串处理函数中,最为简单的几个函数,相关解释就不上了,直接看例子. strtolower函数.strtoupper函 ...

  5. 【转】Python装饰器与面向切面编程

    原文请参考: http://www.cnblogs.com/huxi/archive/2011/03/01/1967600.html 今天来讨论一下装饰器.装饰器是一个很著名的设计模式,经常被用于有切 ...

  6. Win CE 6.0 获取手持机GPS定位1----基础知识 (C#)

    一.GPS全球定位系统的组成 (1)GPS卫星(空间部分) 由沿接近环形的地球轨道运行的24颗卫星组成,位于距地表20200千米的高空,均匀分布在6个轨道面上(每个轨道面4颗),轨道倾角55度.此外, ...

  7. 通过nginx的fastcgi_param来设置环境变量

    在nginx配置文件中,可以在nginx总体的配置文件nginx.conf中,也可以在单独的网站配置环境中进行设置,如:www.tomener.com.conf 在配置环境server段locatio ...

  8. OpenCV 学习笔记(模板匹配)

    OpenCV 学习笔记(模板匹配) 模板匹配是在一幅图像中寻找一个特定目标的方法之一.这种方法的原理非常简单,遍历图像中的每一个可能的位置,比较各处与模板是否"相似",当相似度足够 ...

  9. NopCommerce 3. Controller 分析

    1. 继承关系,3个abstract类 System.Web.Mvc.Controller Nop.Web.Framework.Controllers.BaseController Nop.Admin ...

  10. PHP中使用 $_GET 与$_POST 实现简单的前后台数据传输交互功能

    在之前的学习过程中我们接触过前后台数据请求交互的方法有表单提交.AJAX请求以及Angularjs中的$http,今天我们尝试在PHP中使用 $_GET 与$_POST 实现简单的前后台数据传输交互功 ...