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

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  和 d - c两个集合
x = d - c    保存a + b 的所有值,二分查找x并判断合理性
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; struct node { int v,a,b; };
const int MAX = ;
int N,len = ;
int ele[MAX];
node y[MAX * MAX]; bool cmp(node a,node b) { return a.v < b.v; } bool solve() {
for(int i = N - ; i >= ; --i) {
for(int j = N - ; j >= ; --j) {
if(i == j) continue;
int d = ele[i] - ele[j];
// printf("ele = %d d = %d\n",ele[i],d);
int l = ,r = len - ;
while(l < r) {
int mid = (l + r + ) >> ;
if(y[mid].v > d) r = mid - ;
else l = mid;
}
if(y[l].v == d) {
for(int k = l; k < len; ++k) {
if(y[k].v == d && y[k].a != ele[j] && y[k].b != ele[j]
&& y[k].a != ele[i] && y[k].b != ele[i]) {
printf("%d\n",ele[i]);
return true;
}
}
for(int k = l; k >= ; --k) {
if(y[k].v == d && y[k].a != ele[j] && y[k].b != ele[j]
&& y[k].a != ele[i] && y[k].b != ele[i]) {
printf("%d\n",ele[i]);
return true;
} } } }
} return false; } int main()
{
// freopen("sw.in","r",stdin); while(~scanf("%d",&N) && N ) {
for(int i = ; i < N; ++i) scanf("%d",&ele[i]); sort(ele,ele + N);
N = unique(ele,ele + N) - ele; len = ;
for(int i = ; i < N; ++i) {
for(int j = i + ; j < N; ++j) {
y[len].v = ele[i] + ele[j];
y[len].a = ele[i];
y[len++].b = ele[j];
}
} sort(y,y + len,cmp); if(!solve()) printf("no solution\n");
}
return ;
}

POJ 2549的更多相关文章

  1. POJ 2549:Subsets(哈希表)

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

  2. UVA 10125 - Sumsets(POJ 2549) hash

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

  3. POJ 2549 二分+HASH

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

  4. Divide and conquer:Sumsets(POJ 2549)

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

  5. POJ 2549 Sumsets hash值及下标

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

  6. POJ 2549 Sumsets

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

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

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

  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. ProgrammingContestChallengeBook

    POJ 1852 Ants POJ 2386 Lake Counting POJ 1979 Red and Black AOJ 0118 Property Distribution AOJ 0333 ...

随机推荐

  1. public、private、protected 与 默认 等访问修饰符

    1)public(公共的):被public修饰的属性和方法可以被所有类访问. 2)private(私有的):被private修饰的属性和方法只能在改类的内部使用. 3)protected(受保护的): ...

  2. (1)c语言学习总结之从关键字到循环结构

    一.关键字和标示符 1.关键字:c规定的有特殊含义的单词(也就是系统起的名字),全部是小写,有32个; 由关键字引出数据类型和流程类型 1.分类: (1)数据类型:整型用int标示,字符型用char表 ...

  3. Should .close() be put in finally block or not?

    The following are 3 different ways to close a output writer. The first one puts close() method in tr ...

  4. struts2传递参数值的3中方式

    在使用struts2的时候,当要传递的参数不多的时候,我们会选择使用属性来传参,而当要传递的参数很多的时候,或者多个action会有共用的参数时,我们会使用另外两种传参方式. 注意:使用Model D ...

  5. golang的{}初始化

    之前说到Golang中某些类型可以赋值nil, 某些类型不能赋值nil. 不能赋值nil都是initialized value不为nil的类型, 例如: bool int, intN uint, ui ...

  6. 000 VS2013 c++ 框架

    #include <Windows.h> //全局函数声明 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, ...

  7. perl连接mysql(转载)

    文章来源:http://blog.sina.com.cn/s/blog_9d0445d50101czsr.html 首先需要用ppm安装DBI和DBD-mysql ,如果没有的话点击EDIT-pref ...

  8. POC - ASP.NET & IIS 部分

    终于得到了我VM的管理员权限啦啦.接下来不需要把IIS架在我自己的电脑上了,将架在我的VM上. 1. 先添加ISAP和CGI的组件. 2. 将defaultAppPool的MODE设为CLASSIC, ...

  9. java implement

    接口不能被实例化,但是可以声明一个接口类型的变量. eg. A implements B,则可以有B variableName = new A(),这和extends的用法是类似的 接口可被认为是纯抽 ...

  10. Android L Camera2 API 使用实例程序汇总

    在网上发现几个使用Camera API2开发的实例程序,总结一下方便后续参考: 1.Camera2 Basic : https://github.com/googlesamples/android-C ...