[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 ...
随机推荐
- mydetails-yii1
1.yii验证码多余的get a new code ,即使在main.php中配置了中文也是出现获取新图片,影响效果 需要把 <?php $this->widget('CCaptcha') ...
- 【海岛帝国系列赛】No.4 海岛帝国:LYF的太空运输站
50212228海岛帝国:LYF的太空运输站 [试题描述] 最近,“购物券”WHT在“药师傅”帝国资源大会上提出了“SSTS”太空运输站计划.由于恐怖分子前些日子刚猖狂完,炸毁高楼无数,YSF不得不执 ...
- equals和==
在初学Java时,可能会经常碰到下面的代码: 1 String str1 = new String("hello"); 2 String str2 = new String(&qu ...
- MVC架构剖析--ASP.NET MVC图解(二)
- Asp.Net MVC 模型验证详解-实现客户端、服务端双重验证
概要 在asp.net webform开发中经常会对用户提交输入的信息进行校验,一般为了安全起见大家都会在客户端进行Javascript(利于交互).服务端双重校验(安全).书写校验代码是一个繁琐的过 ...
- python字符串列表字典相互转换
字符串转换成字典 json越来越流行,通过python获取到json格式的字符串后,可以通过eval函数转换成dict格式: >>> a='{"name":&qu ...
- How To Set Up Apache Virtual Hosts on CentOS 6
About Virtual Hosts 虚拟主机,用于在一个单一IP地址上,运行多个域.这对那些想在一个VPS上,运行多个网站的人,尤其有用.基于用户访问的不同网站,给访问者显示不同的信息.没有限制能 ...
- Centos7下卸载docker
最近发现某台机器上的Docker服务无法开启,报如下错误: [root@localhost ~]# docker ps -a Cannot connect to the Docker daemon. ...
- HDU 5950:Recursive sequence(矩阵快速幂)
http://acm.hdu.edu.cn/showproblem.php?pid=5950 题意:给出 a,b,n,递推出 f(n) = f(n-1) + f(n-2) * 2 + n ^ 4. f ...
- EF 用CallContext上下文管理
public class ObjectContextFactory { private static CIK_NewsEntities context; public static DbContext ...