Sumsets
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11886   Accepted: 3273

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

大致题意:在集合S中找到4个互不相同的数,使得a + b = d - c,输出最大的d.
分析:meet in the middle的裸题.方程模型,将a+b存进hash表中,然后枚举d,c找一下就好了.为了使得4个数不相同,还必须记录一下a+b中的a,b.询问的时候判断a,b,c,d是否互不相同.
          我用map竟然T掉了......hash表跑进100ms内......期间还犯了一个脑残错误:当找到了答案后我只退出了内层循环,没有退出外层循环,结果WA了半天.以后写for还是尽量带括号吧,删减的话到最后在弄.
#include <cstdio>
#include <map>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int mod = ; int n,a[],head[],nextt[],X[],Y[],w[],tot = ;
bool flag = false; void add(int x,int y)
{
int temp = (((x + y) % mod) + mod) % mod;
X[tot] = y;
Y[tot] = x;
nextt[tot] = head[temp];
head[temp] = tot++;
} bool query(int x,int y)
{
int temp = (((x - y) % mod) + mod) % mod;
for (int i = head[temp]; i + ; i = nextt[i])
if (X[i] != y && Y[i] != x && (X[i] + Y[i]) == (x - y) && X[i] != x && Y[i] != y)
return true;
return false;
} int main()
{
while (scanf("%d",&n) != EOF && n)
{
flag = false;
tot = ;
memset(head,-,sizeof(head));
memset(X,,sizeof(X));
memset(Y,,sizeof(Y));
for (int i = ; i <= n; i++)
scanf("%d",&a[i]);
sort(a + ,a + + n);
for (int i = ; i <= n; i++)
for (int j = i + ; j <= n; j++)
add(a[i],a[j]);
for (int i = n; i >= ; i--)
{
for (int j = i - ; j >= ; j--)
if (query(a[i],a[j]))
{
flag = ;
printf("%d\n",a[i]);
break;
}
if (flag)
break;
}
if (!flag)
puts("no solution");
} return ;
}

poj2549 Sumsets的更多相关文章

  1. poj 折半搜索

    poj2549 Sumsets 题目链接: http://poj.org/problem?id=2549 题意:给你一个含有n(n<=1000)个数的数列,问这个数列中是否存在四个不同的数a,b ...

  2. POJ2549:Sumsets——题解

    http://poj.org/problem?id=2549 题目大意:从集合中找到四个不相同的数,满足a+b+c=d,输出最大的d. —————————————————————————— 该式子变为 ...

  3. POJ 2229 Sumsets

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

  4. HDU 2709 Sumsets(递推)

    Sumsets http://acm.hdu.edu.cn/showproblem.php?pid=2709 Problem Description Farmer John commanded his ...

  5. POJ 2549 Sumsets

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

  6. hdu 2709 Sumsets

    Sumsets Time Limit: 6000/2000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) Total S ...

  7. poj 2220 Sumsets

                                                                                                     Sum ...

  8. BZOJ1677: [Usaco2005 Jan]Sumsets 求和

    1677: [Usaco2005 Jan]Sumsets 求和 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 570  Solved: 310[Submi ...

  9. Sumsets(POJ 2229 DP)

    Sumsets Time Limit: 2000MS   Memory Limit: 200000K Total Submissions: 15293   Accepted: 6073 Descrip ...

随机推荐

  1. 【转】: 塞尔达组在GDC2017演讲的文字翻译:技术的智慧

    大家好,我是堂田卓宏,在<荒野之息>的制作中我担任技术总监的职位.我在2003年加入任天堂,并且作为程序员参与了许多不同游戏的制作.在本次的制作的过程中,我们的程序员团队也需要打破许多游戏 ...

  2. openstack-r版(rocky)搭建基于centos7.4 的openstack swift对象存储服务 二

    openstack-r版(rocky)搭建基于centos7.4 的openstack swift对象存储服务 一 openstack-r版(rocky)搭建基于centos7.4 的openstac ...

  3. RNN: Feed Forward, Back Propagation Through Time and Truncated Backpropagation Through Time

    原创作品,转载请注明出处哦~ 了解RNN的前向.后向传播算法的推导原理是非常重要的,这样, 1. 才会选择正确的激活函数: 2. 才会选择合适的前向传播的timesteps数和后向传播的timeste ...

  4. org.apache.poi版本问题

    问题描述: 今天跑一段历史代码,发现不能启动,抛出java.lang.NoSuchFieldError: RETURN_NULL_AND_BLANK 问题 解决办法: 把org.apache.poi的 ...

  5. 重构:越来越长的 switch ... case 和 if ... else if ... else

    在代码中,时常有就一类型码(Type Code)而展开的如 switch ... case 或 if ... else if ... else 的条件表达式.随着项目业务逻辑的增加及代码经年累月的修改 ...

  6. 《Linux内核与分析》第六周

    20135130王川东 1.操作系统的三大管理功能包括:进程管理,内存管理,文件系统. 2. Linux内核通过唯一的进程标识PID来区别每个进程.为了管理进程,内核必须对每个进程进行清晰的描述,进程 ...

  7. U盘安装OSX

    1.插入U盘,磁盘工具,格式化U盘为Mac OS X拓展 (日志式): 2.去网站搜索recovery disk assistant,此文件大约1.1M,直接打开使用它制作启动盘,进度条完毕就完成了. ...

  8. P4 Runtime和p4 info

    p4runtime P4 Runtime是一套基于Protobuf以及gRPC框架上的协议,通过P4runtime,SDN控制器可以控制能够支援p4的设备. p4runtime当前由p4 API wo ...

  9. lintcode-418-整数转罗马数字

    418-整数转罗马数字 给定一个整数,将其转换成罗马数字. 返回的结果要求在1-3999的范围内. 说明 什么是 罗马数字? https://en.wikipedia.org/wiki/Roman_n ...

  10. prototype.js中Function.prototype.bind方法浅解

    prototype.js中的Function.prototype.bind方法: Function.prototype.bind = function() { var __method = this; ...