4 Values whose Sum is 0 (二分+排序)
题目:
Input
Output
Sample Input
6
-45 22 42 -16
-41 -27 56 30
-36 53 -37 77
-36 30 -75 -46
26 -38 -10 62
-32 -54 -6 45
Sample Output
5
Hint
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=;
int a[N],b[N],c[N],d[N];
int ab[N*N],cd[N*N];
int main()
{
int n,total=,i,j;
while (cin>>n)
{
for (i=;i<n;i++)
cin>>a[i]>>b[i]>>c[i]>>d[i];
int num1=,num2=;
for (i=;i<n;i++)
for (j=;j<n;j++)
{
ab[num1++]=a[i]+b[j];
cd[num2++]=-(c[i]+d[j]);
}
sort (cd,cd+num2);
for (i=;i<num1;i++)
{
int mid,up=num2-,low=;
while (low<=up)
{
mid=low+(up-low)/;
if (ab[i]==cd[mid])
{
total++;
for (j=mid+;j<=up;j++)
{ if (ab[i]==cd[j])
total++;
else
break;
}
for (j=mid-;j>=low;j--)
{
if (ab[i]==cd[j])
total++;
else
break;
} break;
}
else
{
if (ab[i]>cd[mid])
low=mid+;
else
up=mid-;
}
}
}
cout << total << endl;
} return ;
}
4 Values whose Sum is 0 (二分+排序)的更多相关文章
- POJ - 2785 4 Values whose Sum is 0 二分
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 25615 Accep ...
- 4 Values whose Sum is 0(二分)
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 21370 Accep ...
- POJ 2785:4 Values whose Sum is 0 二分
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 18221 Accep ...
- POJ - 2785 - 4 Values whose Sum is 0 - 二分折半查找
2017-08-01 21:29:14 writer:pprp 参考:http://blog.csdn.net/piaocoder/article/details/45584763 算法分析:直接暴力 ...
- UVA - 1152 --- 4 Values whose Sum is 0(二分)
问题分析 首先枚举a和b, 把所有a+b记录下来放在一个有序数组,然后枚举c和d, 在有序数组中查一查-c-d共有多少个.注意这里不可以直接用二分算法的那个模板,因为那个模板只能查找是否有某个数,一旦 ...
- POJ2785 4 Values whose Sum is 0 (二分)
题意:给你四组长度为\(n\)序列,从每个序列中选一个数出来,使得四个数字之和等于\(0\),问由多少种组成情况(仅于元素的所在位置有关). 题解:\(n\)最大可以取4000,直接暴力肯定是不行的, ...
- [poj2785]4 Values whose Sum is 0(hash或二分)
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 19322 Accepted: ...
- UVA 1152 4 Values whose Sum is 0 (枚举+中途相遇法)(+Java版)(Java手撕快排+二分)
4 Values whose Sum is 0 题目链接:https://cn.vjudge.net/problem/UVA-1152 ——每天在线,欢迎留言谈论. 题目大意: 给定4个n(1< ...
- POJ 2785 4 Values whose Sum is 0(折半枚举+二分)
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 25675 Accep ...
- 二分-G - 4 Values whose Sum is 0
G - 4 Values whose Sum is 0 The SUM problem can be formulated as follows: given four lists A, B, C, ...
随机推荐
- 拉格朗日插值Python代码实现
1. 数学原理 对某个多项式函数有已知的k+1个点,假设任意两个不同的都互不相同,那么应用拉格朗日插值公式所得到的拉格朗日插值多项式为: 其中每个lj(x)为拉格朗日基本多项式(或称插值基函数),其表 ...
- 运行SQL文件报错Invalid ON UPDATE clause for 'create_date' column
Invalid ON UPDATE clause for 'create_date' column 原因: 高版本的mysql导数据到低版本出现的问题 日期类型报错 MySQL 5.5 每个表只允 ...
- Redis分布式锁前世今生
1.redis锁前世即基于单Redis节点的分布式锁,诸如setkey value px milliseconds nx 前世者,必将经历种种磨砺,才能稍微符合一些主流.推荐自测非常好用的redis工 ...
- Python笔记_第四篇_高阶编程_GUI编程之Tkinter_1.使用Python进行GUI编程的概述
1. GUI概述: GUI全称为Graphical User Interface,叫做图形用户界面,也是一种交互方式(Interaction).早期计算机使用的命令行界面(command-line i ...
- doc文件转txt
doc文件转txt # -*- coding:utf-8 -*- # 安装pywin32包 http://sourceforge.net/projects/pywin32/files/pywin32/ ...
- mysql 视图 事务 索引
为什么需要有视图 * 对于复杂的查询,往往是有多个数据表进行关联查询而得到,而这种语句往往比较复杂,也可能非常频繁的使用. 比如这样的SQL语句 select goods.name,goods_cat ...
- linux c 调用 so 库
/***********编译时要链接 -l dl 库************/ #include<stdlib.h> #include<stdio.h> #include< ...
- python学习笔记(26)-request模块
python学习笔记 #requests import requests #from class_005.http_resuest import HttpRequest login_url = &qu ...
- HTMLTestRunner 报告框架使用
HTMLTestRunner 报告框架使用 file_path = base_path + '/Report/report.html' with open(file_path, 'wb') as f: ...
- springboot多数据源+jta事务管理配置
1.创建一个maven项目,导入相关配置: <?xml version="1.0" encoding="UTF-8"?> <project x ...