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. 小心指针被delete两次

    C++类中,有时候使用到传值调用(对象实体做参数),遇到这种情况,可要小心了!特别是当你所传值的对象生命周期较长,而非临时对象(生命周期段)的时候.来看看下面的情况: #include <ios ...

  2. linux kernel 0.11 setup

    setup作用 ①读取参数放在0x90000处. ②将原本在0x10000处的system模块移至0x00000处 ③加载中断描述符表,全局描述符表,进入32位保护模式. 概念 关于实模式和保护模式区 ...

  3. ios中三种多线程的技术对比

    1.NSThread 使用较少 在NSThread调用的方法中,同样要使用autoreleasepool进行内存管理,否则容易出现内存泄露. 使用流程:创建线程-->启动线程 2.NSOpera ...

  4. AMD 和 CMD as lazy as possible

    http://blog.chinaunix.net/uid-26672038-id-4112229.html AMD 与 CMD 区别到底在哪里?       看了以上 AMD,requireJS 与 ...

  5. iOS开发之构建Widget

    原文出处: 陈凯 在 jianshu 的博客(@chenkaiHome)   欢迎分享原创到伯乐头条 伴随这iOS 8 系统多达4000项API更新而来同样还有Today Extension.而对iO ...

  6. Call C# in powershell

    How to call C# code in powershell Powershell Command Add-Type usage of Add-Type we use Add-Type -Typ ...

  7. bootstrap bootstrapTable 隐藏列

    主要代码: <script type="text/javascript"> $(function () { LoadingDataListOrderRealItems( ...

  8. 24.task的运用

    任务就是一段封装在“task-endtask”之间的程序.任务是通过调用来执行的,而且只有在调用时才执行,如果定义了任务,但是在整个过程中都没有调用它,那么这个任务是不会执行的.调用某个任务时可能需要 ...

  9. JavaScript美术馆进化史

    内容选自<<JavaScript DOM 编程艺术>>第4-6章,跟着作者一起见证美术馆的进化吧. 先放效果图,然后一步步做出每个效果.每个效果都有它实用的地方,且知道过程可以 ...

  10. 心情符号love

    写点什么呢,先谢谢心情吧,算是第一个脚印了,想先把之前的一些笔记和心得迁移进来吧,以后每个月都要充实自己的知识.向大婶们看齐.走你们走过的脚印,看你们前行的身影.沿着你们留下的路,继续为后者拓宽道路. ...