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

  1. 5
  2. 2
  3. 3
  4. 5
  5. 7
  6. 12
  7. 5
  8. 2
  9. 16
  10. 64
  11. 256
  12. 1024
  13. 0

Sample Output

  1. 12
  2. no solution

Source

 
分成a + b  和 d - c两个集合
x = d - c    保存a + b 的所有值,二分查找x并判断合理性
 
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. struct node { int v,a,b; };
  9. const int MAX = ;
  10. int N,len = ;
  11. int ele[MAX];
  12. node y[MAX * MAX];
  13.  
  14. bool cmp(node a,node b) { return a.v < b.v; }
  15.  
  16. bool solve() {
  17. for(int i = N - ; i >= ; --i) {
  18. for(int j = N - ; j >= ; --j) {
  19. if(i == j) continue;
  20. int d = ele[i] - ele[j];
  21. // printf("ele = %d d = %d\n",ele[i],d);
  22. int l = ,r = len - ;
  23. while(l < r) {
  24. int mid = (l + r + ) >> ;
  25. if(y[mid].v > d) r = mid - ;
  26. else l = mid;
  27. }
  28. if(y[l].v == d) {
  29. for(int k = l; k < len; ++k) {
  30. if(y[k].v == d && y[k].a != ele[j] && y[k].b != ele[j]
  31. && y[k].a != ele[i] && y[k].b != ele[i]) {
  32. printf("%d\n",ele[i]);
  33. return true;
  34. }
  35. }
  36. for(int k = l; k >= ; --k) {
  37. if(y[k].v == d && y[k].a != ele[j] && y[k].b != ele[j]
  38. && y[k].a != ele[i] && y[k].b != ele[i]) {
  39. printf("%d\n",ele[i]);
  40. return true;
  41. }
  42.  
  43. }
  44.  
  45. }
  46.  
  47. }
  48. }
  49.  
  50. return false;
  51.  
  52. }
  53.  
  54. int main()
  55. {
  56. // freopen("sw.in","r",stdin);
  57.  
  58. while(~scanf("%d",&N) && N ) {
  59. for(int i = ; i < N; ++i) scanf("%d",&ele[i]);
  60.  
  61. sort(ele,ele + N);
  62. N = unique(ele,ele + N) - ele;
  63.  
  64. len = ;
  65. for(int i = ; i < N; ++i) {
  66. for(int j = i + ; j < N; ++j) {
  67. y[len].v = ele[i] + ele[j];
  68. y[len].a = ele[i];
  69. y[len++].b = ele[j];
  70. }
  71. }
  72.  
  73. sort(y,y + len,cmp);
  74.  
  75. if(!solve()) printf("no solution\n");
  76. }
  77. return ;
  78. }

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. rails中ActionController::InvalidAuthenticityToken解决办法

    Ror代码 class FooController < ApplicationController protect_from_forgery :except => :index # you ...

  2. WPF中多窗口共享静态属性

    由于我的DoubanFm在重新考虑之后,需要设置一个全局的CurrentSong,这个字段要让所有的VM都知道,而我同时又想把它作为我所有VM的共有属性.而且我想尽量减少代码的复制,提高重用.所以我做 ...

  3. Iframe 自适应高度的方法!

    第一种方法:代码简单,兼容性还可以,大家可以先测试下. function SetWinHeight(obj) { var win=obj; if (document.getElementById) { ...

  4. java中的排序

    排序是数据结构中重要的一个部分,也是在实际开发中最易遇到的问题之一,当然了,你也可以不考虑这些排序的算法,直接把要排序的数据insert到数据库中,用数据库的order by再select一下,也能产 ...

  5. Oracle Database Links解析

    什么是Database Links呢? 首先我们阐述下它的作用:使用户可以通过一个数据库访问到另外一个远程数据库. 那么Database Link是存储着远程数据库的连接信息. 如下图所示: 用户Sc ...

  6. hdu 5265 pog loves szh II

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5265 pog loves szh II Description Pog and Szh are pla ...

  7. Hadoop之Hive UDAF TopN函数实现

    public class GenericUDAFTopNRow extends AbstractGenericUDAFResolver { @Overridepublic GenericUDAFEva ...

  8. PHP调用WEBSERVICE接口常见问题答疑以及总结

    最近的工作项目中,接触到了很多的政府 微信开发项目.对方的外包公司都是使用JAVA作为开发语言,然后通过WEBSERVICE进行接口返回数据到我的项目中.一般情况下,能在浏览器打开并显示数据的接口是直 ...

  9. 关于1>LINK : fatal error LNK1168: 无法打开 ....exe或者....dll进行写入的问题

      我们用VS编译器运行我们的程序时候,可能会出现关于1>LINK : fatal error LNK1168: 无法打开 ...dll 进行写入或者是1>LINK : fatal err ...

  10. 四则运算出题器(C++)-BUG修复

    定制题目数量这个功能测试: (1)输入题目数为负数时: 可正确处理: (2)输入题目数量为0时: 可正确处理: (3)输入题目数量为小数时: 程序运行出错: 错误分析: 因为代码中题目数量的变量Que ...