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. JAVA学习笔记--初始化与清理

    编写程序时,常会由于变量没有初始化而产生各种错误:用完一个元素,如果不将其占用的内存资源释放,则会导致资源耗尽,这也很严重,为此,C++引入了构造器的概念,这是一个在创建对象时被自动调用的特殊方法,以 ...

  2. Python最简编码规范

    前言 本文是阅读<Python Coding Rule>之后总结的最为精华及简单的编码规范,根据每个人不同喜好有些地方会有不同的选择,我只是做了对自己来说最简单易行的选择,仅供大家参考. ...

  3. [redis] linux下主从篇(2)

    一.前言1.为何要主从架构避免单机故障,主服务器挂掉后,还可以手动切换从服务为主服务继续工作,保持缓存数据完整. 2.主从同步的原理步骤从服务器连接主服务器,发送SYNC命令:主服务器接收到SYNC命 ...

  4. Leftmost Digit(数学)

    Description Given a positive integer N, you should output the leftmost digit of N^N.   Input The inp ...

  5. Win10系统自带输入法的人机交互设计

    过了寒假回校以后,我的电脑重装了系统,为了提升系统运行的速度,自己装了一个内存条同时对硬盘进行了重新的分区,对电脑内的文件也进行了重新的整理,电脑的运行速度提高了很多.老多同学都说win10系统好用, ...

  6. SGU 181 X-Sequence(一题比较水的求模找规律)

    E - X-Sequence Time Limit:500MS     Memory Limit:4096KB     64bit IO Format:%I64d & %I64u Submit ...

  7. 使用ssh公钥登陆

    记录一下使用的具体命令,具体参考: Centos设置禁止密码登录而只使用密钥登录SSH方法  优先参考这个. ssh使用公钥授权不通过的问题解决 Xshell配置ssh免密码登录-密钥公钥(Publi ...

  8. codepen & js demos

    codepen & js demos Mutation Observer & customize resize event listener & demo https://co ...

  9. MAC 下用 brew 搭建 PHP 开发环境

    Mac下用brew搭建PHP(LNMP/LAMP)开发环境 Mac下搭建lamp开发环境很容易,有xampp和mamp现成的集成环境.但是集成环境对于经常需要自定义一些配置的开发者来说会非常麻烦,而且 ...

  10. adb使用过程常见的几种错误总结

    问题1:Failure [INSTALL_FAILED_ALREADY_EXISTS] 问题原因:该程序已存在. 解决方法:增加-r参数,即可成功覆盖安装 问题2:Failure [INSTALL_F ...