嘟嘟嘟




很简单的折半搜索。

把式子变一下型,得到\(a + b = d - c\)。

然后枚举\(a, b\),存到\(map\)里,再枚举\(c, d\)就好了。

\(map\)以\(a,b\)两数之和为下标。为了判重,\(map\)的第二个参数是一个\(vector\),\(vector\)里面又存了两个数\(a, b\)。

这样先\(O(n ^ 2)\)跑一边\(a, b\)之和,存到\(map\)里,然后从大到小枚举\(d\)和\(c\),遍历\(map\)中\(d - c\)的\(vector\),如果四个数都没有一样的,就直接返回好了。

用\(map\)的最大好处是代码特别短。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<map>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 1e3 + 5;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
} int n, a[maxn]; struct Node
{
int x, y;
};
map<int, vector<Node> > mp; int solve()
{
for(int i = n; i; --i)
for(int j = 1; j < i; ++j)
{
int sum = a[i] - a[j];
for(int k = 0; k < (int)mp[sum].size(); ++k)
{
int x = mp[sum][k].x, y = mp[sum][k].y;
if(a[i] != x && a[i] != y && a[j] != x && a[j] != y)
return a[i];
}
}
return 536870912;
} int main()
{
while(scanf("%d", &n) && n)
{
mp.clear(); Mem(a, 0);
for(int i = 1; i <= n; ++i) a[i] = read();
sort(a + 1, a + n + 1);
for(int i = 1; i < n; ++i)
for(int j = i + 1; j <= n; ++j)
mp[a[i] + a[j]].push_back((Node){a[i], a[j]});
int ans = solve();
if(ans == 536870912) puts("no solution");
else write(ans), enter;
}
return 0;
}

UVA10125 Sumsets的更多相关文章

  1. C++算法代码——Sumsets[uva10125]

    题目来自:http://218.5.5.242:9018/JudgeOnline/problem.php?id=1278 题目描述 给你一个整数的集合S(里面所有的整数均不相同),请你找出最大的 d, ...

  2. POJ 2229 Sumsets

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

  3. HDU 2709 Sumsets(递推)

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

  4. POJ 2549 Sumsets

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

  5. hdu 2709 Sumsets

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

  6. poj 2220 Sumsets

                                                                                                     Sum ...

  7. BZOJ1677: [Usaco2005 Jan]Sumsets 求和

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

  8. Sumsets(POJ 2229 DP)

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

  9. Sumsets(3sum问题,枚举d,c二分a+b)

    Sumsets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9997   Accepted: 2736 Descripti ...

随机推荐

  1. IoC容器之Unity

    关于IoC.Unity见博友文章点击这里. 话不多说,上程序HelloUnity,程序采用VS2010,Unity2.1. 1.程序框架如下 2.类库HelloUnity.Objects,主要为实体类 ...

  2. SQL Server使用ROW_NUMBER进行快速分页查询

    DECLARE @pageSize INTDECLARE @pageIndex INT --第4页,每页显示10条数据SET @pageSize = 10SET @pageIndex = 4 SELE ...

  3. [C语言] 变量和数据类型和整数和字符

    1.数据类型和变量: 数据是放在内存中的,明确三件事:数据存储在哪里.数据的长度以及数据的处理方式 int n;数据类型指明了数据的长度和处理方式,变量名指明了数据存储在哪里 2.数据长度:是指数据占 ...

  4. 十二、curator recipes之双重屏障DoubleBarrier

    简介 curator实现了单个屏障barrier和双重屏障DoubleBarrier,单个屏障就是在一个进程里面设置了屏障,并等待其它进程去移除这个屏障,否则一直阻塞.双重屏障就是设置了两道屏障,两个 ...

  5. Spring扩展:Spring框架的由来

    一.Spring框架的由来

  6. easy canvas shape with react antdesign 简单的canvas图形in antd & react

    //show: //code: import React from "react" import {findDOMNode} from 'react-dom' import { B ...

  7. 在Ubuntu 14.04.1 LTS 上安装gettext失败

    使用apt-get install -f,因为有额外的依赖.

  8. Sass、Less、Stylus,我选Sass!

    Sass官网 | Sass中文 简介:待添加 Less 简介:待添加 Stylus 简介:待添加

  9. zookeeper安装和配置(单机+伪集群+集群)

    #单机模式 解压到合适目录. 进入zookeeper目录下的conf子目录, 复制zoo_sample.cfg-->zoo.cfg(如果没有data和logs就新建):tickTime=2000 ...

  10. Linux开篇

    1.为什么学习Linux? 2.学习Linux的资料