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

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

 
 
 
解析:折半枚举。之前碰到的折半枚举都是将集合折半,这次不同,折半的思想体现在等式上。将等式a + b + c = d转化为a + b = d - c,等式分成左右两部分,预处理出左右两部分,将左半部分排序,然后枚举右半部分,二分查找即可。时间复杂度为O(n2logn2)。
 
 
 
#include <cstdio>
#include <algorithm>
using namespace std; const int MAXN = 1000+5;
int n;
int a[MAXN]; struct S{
int val;
int i, j;
bool operator < (const S& b)const
{
return val < b.val;
}
};
S l[MAXN*MAXN], r[MAXN*MAXN]; bool ok(S& a, S& b)
{
return a.i != b.i && a.j != b.j && a.i != b.j && a.j != b.i;
} void solve()
{
int lcnt = 0;
for(int i = 0; i < n; ++i){
for(int j = i+1; j < n; ++j){
l[lcnt].val = a[i]+a[j];
l[lcnt].i = i;
l[lcnt++].j = j;
}
}
int rcnt = 0;
for(int i = 0; i < n; ++i){
for(int j = i+1; j < n; ++j){
r[rcnt].val = a[i]-a[j];
r[rcnt].i = i;
r[rcnt++].j = j;
r[rcnt].val = a[j]-a[i];
r[rcnt].i = j;
r[rcnt++].j = i;
}
}
sort(l, l+lcnt);
int res = 0xffffffff;
for(int i = 0; i < rcnt; ++i){
int d = lower_bound(l, l+lcnt, r[i])-l;
if(ok(l[d], r[i]) && l[d].val == r[i].val){
res = max(res, r[i].val+a[r[i].j]);
}
}
if(res == 0xffffffff)
printf("no solution\n");
else
printf("%d\n", res);
} int main()
{
while(scanf("%d", &n), n){
for(int i = 0; i < n; ++i)
scanf("%d", &a[i]);
solve();
}
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 hash值及下标

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

  3. [poj] 2549 Sumsets || 双向bfs

    原题 在集合里找到a+b+c=d的最大的d. 显然枚举a,b,c不行,所以将式子移项为a+b=d-c,然后双向bfs,meet int the middle. #include<cstdio&g ...

  4. UVA 10125 - Sumsets(POJ 2549) hash

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  5. Divide and conquer:Sumsets(POJ 2549)

    数集 题目大意:给定一些数的集合,要你求出集合中满足a+b+c=d的最大的d(每个数只能用一次) 这题有两种解法, 第一种就是对分,把a+b的和先求出来,然后再枚举d-c,枚举的时候输入按照降序搜索就 ...

  6. POJ 2549:Subsets(哈希表)

    [题目链接] http://poj.org/problem?id=2549 [题目大意] 给出一个数集,从中选择四个元素,使得a+b+c=d,最小化d [题解] 我们对a+b建立Hash_table, ...

  7. POJ 2229 Sumsets

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

  8. POJ 2549 二分+HASH

    题目链接:http://poj.org/problem?id=2002 题意:给定一个n个数字组成的序列,然后求出4个数使得a+b+c=d,求d的最大值.其中a,b,c,d要求是给定序列的数,并且不能 ...

  9. POJ 2549

    Sumsets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8235   Accepted: 2260 Descripti ...

随机推荐

  1. zabbix3.0 安装方法,一键实现短信、电话、微信、APP 告警

    引言 免费开源监控工具 Zabbix 因其强大的监控功能得到各大互联网公司的广泛认可,具体功能不再详细介绍,在之前发布的 Zabbix 2.4.1 安装及微信短信提醒已经做了详细介绍,本篇主要对 Za ...

  2. 如何用 OneAPM 优化你的 Node.js 应用?

    本文主要讲解如何使用 OneAPM 提供的信息从内存.CPU 使用.响应速度等方面优化 Node.js 应用.适用于定位于刚刚使用 Node.js 开发后台的读者.本文系 OneAPM 用户投稿,本网 ...

  3. C++ char*,const char*,string,int 的相互转换

    C++ char*,const char*,string,int 的相互转换   1. string转const char* string s ="abc";const char* ...

  4. mutable关键字

    mutalbe的中文意思是“可变的,易变的”,跟constant(既C++中的const)是反义词.在C++中,mutable也是为了突破const的限制而设置的.被mutable修饰的变量(muta ...

  5. 转: 在.NET中操作数字证书

    作者:玄魂出处:博客2010-06-23 12:05 http://winsystem.ctocio.com.cn/19/9492019.shtml .NET为我们提供了操作数字证书的两个主要的类,分 ...

  6. POJ1470 Closest Common Ancestors

    LCA问题,用了离线的tarjan算法.输入输出参考了博客http://www.cnblogs.com/rainydays/archive/2011/06/20/2085503.htmltarjan算 ...

  7. 3.Spring-用反射模拟IoC

    1.BeanFactory.java package com.jike.spring.chapter03.reflect; import java.io.InputStream; import jav ...

  8. import java.util.Scanner;

    一.扫描控制台输入     当通过new Scanner(System.in)创建一个Scanner,控制台会一直等待输入,,,,,,,直到敲回车键结束,把所输入的内容传给Scanner,作为扫描对象 ...

  9. 用ISO C++实现自己的信号槽(Qt另类学习)

    qtc++objectsignalclassstring   目录(?)[-] Qt信号与槽 引入元对象系统 建立信号槽链接 信号的激活 槽的调用 全家福 零零散散写在后面 Q_OBJECT Conn ...

  10. C++Builder和VC的比较

    C++Builder和VC的比较 其实很久以前我就想写这篇文章,其原因一方面是因为笔者深深感觉到C++ Builder的确是一个先进与强大的程式开发工具,但更最重要的一点是,我深信C++ Builde ...