题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1090

找到所有数的和,然后再原数组里二分找符合条件的第三个数。

 #include <bits/stdc++.h>
using namespace std; const int maxn = ;
const int maxm = maxn * maxn;
typedef struct R {
int x, y, z;
R(){}
R(int x, int y, int z) : x(x), y(y), z(z) {}
bool operator == (R t) {
return x == t.x && y == t.y && z == t.z;
}
}R;
typedef struct S {
int i, j;
int s;
}S; vector<R> ret;
int n, m;
int a[maxn];
S s[maxm]; bool cmp(R a, R b) {
if(a.x == b.x) {
if(a.y == b.y) return a.z < b.z;
return a.y < b.y;
}
return a.x < b.x;
} int main() {
// freopen("in", "r", stdin);
while(~scanf("%d", &n)) {
m = ; ret.clear();
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++) {
s[m].i = i; s[m].j = j; s[m].s = a[i] + a[j];
m++;
}
}
for(int i = ; i < m; i++) {
int x = -s[i].s;
int id = lower_bound(a, a+n, x) - a;
if(a[id] == x && id != s[i].i && id != s[i].j) {
int x = a[s[i].i], y = a[s[i].j], z = a[id];
if(x > y) swap(x, y);
if(x > z) swap(x, z);
if(y > z) swap(y, z);
ret.push_back(R(x,y,z));
}
}
if(ret.size() == ) {
puts("No Solution");
continue;
}
sort(ret.begin(), ret.end(), cmp);
ret.erase(unique(ret.begin(), ret.end()), ret.end());
for(int i = ; i < ret.size(); i++) {
printf("%d %d %d\n", ret[i].x, ret[i].y, ret[i].z);
}
}
return ;
}

[51NOD1090] 3个数和为0(水题,二分)的更多相关文章

  1. hdu - 6276,2018CCPC湖南全国邀请赛A题,水题,二分

    题意: 求H的最大值,  H是指存在H篇论文,这H篇被引用的次数都大于等于H次. 思路:题意得,  最多只有N遍论文,所以H的最大值为N, 常识得知H的最小值为0. 所以H的答案在[0,N]之间,二分 ...

  2. UVaLive 3971 Assemble (水题二分+贪心)

    题意:你有b元钱,有n个配件,每个配件有各类,品质因子,价格,要每种买一个,让最差的品质因子尽量大. 析:很简单的一个二分题,二分品质因子即可,每次计算要花的钱的多少,每次尽量买便宜且大的品质因子. ...

  3. ytu 1061: 从三个数中找出最大的数(水题,模板函数练习 + 宏定义练习)

    1061: 从三个数中找出最大的数 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 154  Solved: 124[Submit][Status][We ...

  4. SPOJ CNTPRIME 13015 Counting Primes (水题,区间更新,求区间的素数个数)

    题目连接:http://www.spoj.com/problems/CNTPRIME/ #include <iostream> #include <stdio.h> #incl ...

  5. UVa 10970 - Big Chocolate 水题 难度: 0

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  6. HDOJ 2317. Nasty Hacks 模拟水题

    Nasty Hacks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  7. [poj2247] Humble Numbers (DP水题)

    DP 水题 Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The se ...

  8. gdutcode 1195: 相信我这是水题 GDUT中有个风云人物pigofzhou,是冰点奇迹队的主代码手,

    1195: 相信我这是水题 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 821  Solved: 219 Description GDUT中有个风云人 ...

  9. BZOJ 1303 CQOI2009 中位数图 水题

    1303: [CQOI2009]中位数图 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 2340  Solved: 1464[Submit][Statu ...

随机推荐

  1. Java中this关键字的使用

    本文介绍了在Java中this关键字的作用于使用方法 当局部变量和成员变量重名时,在方法中使用this时,表示的是该方法所在类中的成员变量.(this指的是当前对象自己) 如:public class ...

  2. selenium滚动条

    element = self.brower.find_element_by_id('xxx')brower.execute_script('arguments[0].scrollIntoView(); ...

  3. LAMP php5.4编译

    编译时出现下列问题时: In file included from /usr/local/src/php-5.4.6/ext/gd/gd.c:103: /usr/local/src/php-5.4.6 ...

  4. Java总结第一次//有些图片未显示,文章包含基础java语言及各种语句

    一.java入门 1.Java入门学习框架: 2.常用的DOS命令: dir(directory) :    列出当前目录下的文件以及文件夹 md(make directory) :   创建目录 r ...

  5. 161122、BOM 操作写法示例

    浏览器相关信息 // 浏览器信息 navigator.userAgent // Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/ ...

  6. iOS录音加播放.

    现在发现的事实有: 如果没有蓝牙设备, 那么可以用下面的方法边录音, 边放音乐: 在录音按钮按下的时候: _avSession = [AVAudioSession sharedInstance];   ...

  7. chrome调试找不到 XXXX.min.map 原因及解决办法

    什么是source map文件. source map文件是js文件压缩后,文件的变量名替换对应.变量所在位置等元信息数据文件,一般这种文件和min.js主文件放在同一个目录下. 比如压缩后原变量是m ...

  8. C# 控制连接超时

    首先连接超时分为三种,TCP Connection to SQL Server -> SqlConnection.Open -> SqlCommand.Execute先说第二种超时,sql ...

  9. C# Driver LINQ Tutorial

    1.介绍 该教程涵盖了1.8版本的C#驱动中的LINQ查询.你可能已经阅读最新的C# Driver Tutorial. 2.快速开始 首先,给程序添加下面的using声明 using MongoDB. ...

  10. Haproxy安装与配置

    一.Haproxy概念 Haproxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.Haproxy特别适用于那些负载特大的web站点,这 ...