POJ 2785 4 Values whose Sum is 0 (二分)题解
思路:
如果用朴素的方法算O(n^4)超时,这里用折半二分。把数组分成两块,分别计算前后两个的和,然后枚举第一个再二分查找第二个中是否有满足和为0的数。
注意和有重复
#include<iostream>
#include<algorithm>
#include<cstring>
#define ll long long
using namespace std;
const int N = 4000+5;
int a[N],b[N],c[N],d[N];
int mp1[N*N],mp2[N*N],cn1,cn2;
int main(){
int n;
scanf("%d",&n);
for(int i = 1;i <= n;i++) scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);
cn1 = cn2 = 0;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++){
mp1[cn1++] = a[i] + b[j];
mp2[cn2++] = c[i] + d[j];
}
}
sort(mp2,mp2+cn2);
ll ans = 0;
for(int i = 0;i < cn1;i++){
int x = lower_bound(mp2,mp2+cn2,-mp1[i]) - mp2;
if(mp1[i] + mp2[x] == 0) ans += upper_bound(mp2,mp2+cn2,-mp1[i]) - mp2 - x;
}
printf("%lld\n",ans);
return 0;
}
POJ 2785 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 ...
- POJ - 2785 - 4 Values whose Sum is 0 - 二分折半查找
2017-08-01 21:29:14 writer:pprp 参考:http://blog.csdn.net/piaocoder/article/details/45584763 算法分析:直接暴力 ...
- POJ 2785 4 Values whose Sum is 0(想法题)
传送门 4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 20334 A ...
- POJ 2785 4 Values whose Sum is 0
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 13069 Accep ...
- POJ 2785 4 Values whose Sum is 0(折半枚举+二分)
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 25675 Accep ...
- POJ 2785 4 Values whose Sum is 0(暴力枚举的优化策略)
题目链接: https://cn.vjudge.net/problem/POJ-2785 The SUM problem can be formulated as follows: given fou ...
- POJ 2785 4 Values whose Sum is 0(哈希表)
[题目链接] http://poj.org/problem?id=2785 [题目大意] 给出四个数组,从每个数组中选出一个数,使得四个数相加为0,求方案数 [题解] 将a+b存入哈希表,反查-c-d ...
- POJ 2785 4 Values whose Sum is 0 Hash!
http://poj.org/problem?id=2785 题目大意: 给你四个数组a,b,c,d求满足a+b+c+d=0的个数 其中a,b,c,d可能高达2^28 思路: 嗯,没错,和上次的 HD ...
- poj 2785 4 Values whose Sum is 0(折半枚举(双向搜索))
Description The SUM problem can be formulated . In the following, we assume that all lists have the ...
- [POJ] 2785 4 Values whose Sum is 0(双向搜索)
题目地址:http://poj.org/problem?id=2785 #include<cstdio> #include<iostream> #include<stri ...
随机推荐
- Count the Colors---zoj1610线段树
题目链接 题意: 求每种颜色有几段线段: 模拟数组: #include<stdio.h> #include<iostream> #include<algorithm> ...
- elasticsearch更改mapping,不停服务重建索引(转)
原文:http://donlianli.iteye.com/blog/1924721?utm_source=tuicool&utm_medium=referral Elasticsearch的 ...
- mysql 内置功能 事务 介绍
事务用于将某些操作的多个SQL作为原子性操作,一旦有某一个出现错误,即可回滚到原来的状态,从而保证数据库数据完整性 创建数据库db12 create database db12 charset=utf ...
- c++四种强制类型转化
c++ 数据类型转换: static_cast dynamic_cast reinterpret_cast const_cast C++ 类型转换(C风格的强制转换): (1)将浮点型数据赋值给整型变 ...
- 【C语言】指向一维数组元素的指针
本文目录 一.用指针指向一维数组的元素 二.用指针遍历数组元素 三.指针与数组的总结 四.数组.指针与函数参数 前面我们已经学习了指针,如果指针存储了某个变量的地址,我们就可以说指针指向这个变量.数组 ...
- C/S模型之UDP协议
说明:利用UDP协议,创建一个服务器和一个客户端.两者间进行通信.由客户端进行输入内容,而服务器将接受的内容进行再一次返回,并显示在服务端. // UDP_Seversock.cpp : 定义控制台应 ...
- ngxin开启rewrite伪静态
1.编辑nginx配置文件 vi /usr/local/nginx/conf/nginx.conf #include enable-php.conf; include enable-php-pathi ...
- 公司里面用的iTextSharp(教程)---简介
一.需求: 公司这次要做一个生成PDF的功能,需要设计,刚刚进入公司,组长把任务分配给了我,为了完成这个任务,苦学了许久的iTextSharp.现在记录下实现过程中了了解的一些东东,一起分享哈~~ 二 ...
- linux常用命令:chmod 命令
chmod命令用于改变linux系统文件或目录的访问权限.用它控制文件或目录的访问权限.该命令有两种用法.一种是包含字母和操作符表达式的文字设定法:另一种是包含数字的数字设定法. Linux系统中的每 ...
- git仓库按时间、成员等维度分析统计
git 按时间打印所有成员代码提交: git log --since ==2018-01-01 --until=2018-12-31 --format='%aN' | sort -u | while ...