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. 菜鸟学习Spring——60s利用JoinPoint获取参数的值和方法名称

    一.概述 AOP的实现方法在上两篇博客中已经用了两种方法来实现现在的问题来了虽然我们利用AOP,那么客户端如何信息传递?利用JoinPoint接口来实现客户端给具体实现类的传递参数. 二.代码演示. ...

  2. RMAN 报:ORA-19504 ORA-27038

    在itpub中看到下面的问题: oracle 10g备份脚本如下run{allocate channel d1 device type disk MAXPIECESIZE=100M;crosschec ...

  3. hdu 2425 Hiking Trip

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2425 Hiking Trip Description Hiking in the mountains ...

  4. android开发系列之MVP设计模式

    最近在开发一个android的项目中,发现了一个很实用的设计模式(MVP).大家可能一看到这个名字就有点蒙,MVP到底是什么鬼呢?它的好用到底体现在哪呢?别着急,下面就让我们一一分享出来. 说到MVP ...

  5. Android之Activity的四种启动模式

    当应用运行起来后就会开启一条线程,线程中会运行一个任务栈,当Activity实例创建后就会放入任务栈中.Activity启动模式的设置在AndroidManifest.xml文件中,通过配置Activ ...

  6. Windows Phone性能优化建议

    使用background thread解码图片 在Windows Phone中支持的图片格式有jpg和png,微软建议使用jpg格式的图片,因为jpg格式的图片在解码速度上要比png快.那么我们怎么来 ...

  7. Mysql找不到mysql.sock怎么办?

    1. #ps -aux|grep mysql 找mysql的进程. #kill mysql进程号 确定全部kill光 2.直接跳第3步,无效再使用第2步 /usr/local/mysql/bin/my ...

  8. Sliverlight Slide 的左右滑动

    private void btnPrev_Click(object sender, RoutedEventArgs e) { scrollRule = (scrollRule-) >= ?(sc ...

  9. WPF——数据绑定(二)绑定方法—绑定本地对象

    注意:本人初学WPF,文中表达或技术性问题请勿见怪,欢迎指正,谢谢 标记拓展语法:绑定到本地对象 什么是绑定到本地对象,我个人理解就是实现UI层上两个或多个控件的相互关联,一个控件的状态改变,导致另一 ...

  10. QT对话框设计

    软件和系统:QTcreator5.7,win8.1 1. 首先新建项目,选择application中的Qt widgets application. 2. 创建类Dialog,选择QDialog作为基 ...