[51NOD1090] 3个数和为0(水题,二分)
题目链接: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(水题,二分)的更多相关文章
- hdu - 6276,2018CCPC湖南全国邀请赛A题,水题,二分
题意: 求H的最大值, H是指存在H篇论文,这H篇被引用的次数都大于等于H次. 思路:题意得, 最多只有N遍论文,所以H的最大值为N, 常识得知H的最小值为0. 所以H的答案在[0,N]之间,二分 ...
- UVaLive 3971 Assemble (水题二分+贪心)
题意:你有b元钱,有n个配件,每个配件有各类,品质因子,价格,要每种买一个,让最差的品质因子尽量大. 析:很简单的一个二分题,二分品质因子即可,每次计算要花的钱的多少,每次尽量买便宜且大的品质因子. ...
- ytu 1061: 从三个数中找出最大的数(水题,模板函数练习 + 宏定义练习)
1061: 从三个数中找出最大的数 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 154 Solved: 124[Submit][Status][We ...
- SPOJ CNTPRIME 13015 Counting Primes (水题,区间更新,求区间的素数个数)
题目连接:http://www.spoj.com/problems/CNTPRIME/ #include <iostream> #include <stdio.h> #incl ...
- UVa 10970 - Big Chocolate 水题 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- HDOJ 2317. Nasty Hacks 模拟水题
Nasty Hacks Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- [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 ...
- gdutcode 1195: 相信我这是水题 GDUT中有个风云人物pigofzhou,是冰点奇迹队的主代码手,
1195: 相信我这是水题 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 821 Solved: 219 Description GDUT中有个风云人 ...
- BZOJ 1303 CQOI2009 中位数图 水题
1303: [CQOI2009]中位数图 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 2340 Solved: 1464[Submit][Statu ...
随机推荐
- UI优化
进入正题,我们这一篇文章会提到为什么使用HierarchyViewer,怎么使用HierarchyViewer,后者内容会多一下. 为什么使用HierarchyViewer 不合理的布局会使我们的应用 ...
- scala的静态,单列模式
package com.test.scala.test /** * 单例对象,这种对象不能提供构造函数 */ object SingleObject { private var lastnumber= ...
- jquery通过name属性取值的方法
jquery通过name属性取值的方法//$("input[name='imgtitle']").val();//这个只能取到第一个的值//通过each函数取得所有input的值v ...
- java 面试每日一题2
题目:输入一行字符,分别统计出其中英文字母.空格.数字和其它字符的个数. 注:如果想单独输出中文的个数和中文符号的个数,只需把isChinese()中的if语句修改 知识补充: java不像C中拥有s ...
- 【转】Eclipse插件大全介绍及下载地址
转载地址:http://developer.51cto.com/art/200906/127169.htm 尚未一一验证. eclipse插件大全介绍,以及下载地址 Eclipse及其插件下载网址大全 ...
- MVC模式(Model View Controller)下实现数据库的连接,对数据的删,查操作
MVC模式(Model View Controller): Model:DAO模型 View:JSP 在页面上填写java代码实现显示 Controller:Servlet 重定向和请求的转发: 若 ...
- Poj(2679),SPFA,邻接表(主流写法)
题目链接:http://poj.org/problem?id=3268 题意: 有编号为1-N的牛,它们之间存在一些单向的路径.给定一头牛的编号,其他牛要去拜访它并且拜访完之后要返回自己原来的位置,求 ...
- C#之桶中取黑白球问题
<编程之美>284页,问题4.6:桶中取黑白球. 有一个桶,里面有白球.黑球各100个,人们必须按照以下规则把球取出来: 1. 每次从桶中拿两个球: 2. 如果两球同色,再放入一个黑球: ...
- 2016年10月25日 星期二 --出埃及记 Exodus 19:9
2016年10月25日 星期二 --出埃及记 Exodus 19:9 The LORD said to Moses, "I am going to come to you in a dens ...
- 2016年6月30日 星期四 --出埃及记 Exodus 14:27
2016年6月30日 星期四 --出埃及记 Exodus 14:27 Moses stretched out his hand over the sea, and at daybreak the se ...