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. AI智能外呼机器人网络拓扑结构笔记

    最近开发了一套AI智能外呼机器人系统,系统主要有3部分组成:web管理平台:呼叫机器人:SIP软交换.具体网络拓扑结构如下图: 三部分主要功能如下: 1.web管理平台:话术管理.任务管理.线路管理. ...

  2. Windows和Linux系统如何退出python命令行

    python命令行是新手学习python过程中必须要学的一个工具,下面我们来看一下怎么退出python命令行. 第一种方式: 使用python提供的exit()函数,linux平台和windows平台 ...

  3. Druid Monitor小记

    继上篇DruidDataSource源码分析之后 , 公司又要求做一个Druid的数据库监控 , 以及spring监控 , 研究一小时 , 总结出了一点经验 , 特此贴出来分享一下 一 . 利用Dru ...

  4. 所见即所得:七大无需编程的DIY开发工具

    现如今,各种DIY开发工具不断的出现,使得企业和个人在短短几分钟内就能完成应用的创建和发布,大大节省了在时间和资金上的投入.此外,DIY工具的出现,也帮助广大不具备专业知识和技术的“移动开发粉”创建自 ...

  5. javascript event对象操作

    js代码: $(".leads_detail").click(function(e){ e = e || event; var t = e.target || e.srcEleme ...

  6. Windows 10 系统下Python环境的搭建与配置

    1. 在https://www.python.org/downloads/release/python-352/网站上下载python版本 2. 配置环境 右击计算机属性,然后选择高级系统设置 3.  ...

  7. BETA阶段冲刺

    1.介绍小组新加入的成员,Ta担任的角色 新成员 担任工作 江鹭涛 前端设计 2.讨论是否需要更换团队的PM 不需要,上阶段配合不错,这阶段继续努力 3.下一阶段需要改进完善的功能 服务器并发处理,界 ...

  8. ansible介绍和安装

    ansible是由 Python 编写的强大的配置管理解决方案,ansible 的特点就在于它的简洁与高效率 ansible与其他的配置管理工具不同点在于:不需要你在想要配置的每个节点上安装自己的组件 ...

  9. CDN问题

    名称解释:正反向解析 主辅服务器 domain zone 记录:SOA.NS.A.CNAME.PRT.MX DNS配置文件中各字段作用,如TTL DNS端口号? TCP53和UDP53使用场合 Lin ...

  10. Android Studio -导入项目 gradle处理

    如果导入 android studio 项目, 那么一定要注意 需要合适的gradle版本,具体方法为: 首先导入步骤: 打开android studio ==> File ==> New ...