4 Values whose Sum is 0(枚举+二分)
The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how many quadruplet (a, b, c, d ) ∈ A x B x C x D are such that a + b + c + d = 0 . In the following, we assume that all lists have the same size n .
Input
The first line of the input file contains the size of the lists n (this value can be as large as 4000). We then have n lines containing four integer values (with absolute value as large as 2 28 ) that belong respectively to A, B, C and D .
Output
For each input file, your program has to write the number quadruplets whose sum is zero.
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
Sample Explanation: Indeed, the sum of the five following quadruplets is zero: (-45, -27, 42, 30), (26, 30, -10, -46), (-32, 22, 56, -46),(-32, 30, -75, 77), (-32, -54, 56, 30).
思路:和之前更新的校赛的题目差不多,都是枚举+二分查找
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
#define maxn 4040
int a[maxn],b[maxn],c[maxn],d[maxn],sum[maxn*maxn];
int main()
{
int n,i,j;
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<n;++i)
scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);
for(i=0;i<n;++i)
{
for(j=0;j<n;++j)
sum[n*i+j]=c[i]+d[j];
}
sort(sum,sum+n*n);
int ans=0;
for(i=0;i<n;++i)
{
for(j=0;j<n;++j)
{
int k=-(a[i]+b[j]);
ans+=upper_bound(sum,sum+n*n,k)-lower_bound(sum,sum+n*n,k);
}
}
printf("%d\n",ans);
}
return 0;
}
4 Values whose Sum is 0(枚举+二分)的更多相关文章
- 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< ...
- UVA-1152 4 Values whose Sum is 0 (二分)
题目大意:在4个都有n个元素的集合中,每个集合选出一个元素,使得4个数和为0.问有几种方案. 题目分析:二分.任选两组求和,剩下两组求和,枚举第一组中每一个和sum,在第二组和中查找-sum的个数,累 ...
- 4 Values whose Sum is 0 (二分+排序)
题目: The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, com ...
- POJ 2785 4 Values whose Sum is 0 (二分)题解
思路: 如果用朴素的方法算O(n^4)超时,这里用折半二分.把数组分成两块,分别计算前后两个的和,然后枚举第一个再二分查找第二个中是否有满足和为0的数. 注意和有重复 #include<iost ...
- POJ 2785 4 Values whose Sum is 0(折半枚举+二分)
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 25675 Accep ...
- [poj2785]4 Values whose Sum is 0(hash或二分)
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 19322 Accepted: ...
- 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: 25615 Accep ...
- 折半枚举(双向搜索)poj27854 Values whose Sum is 0
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 23757 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 ...
随机推荐
- Linux系统的安装(centos的下载地址:http://mirror.symnds.com/distributions/CentOS-vault/6.3/isos/i386/,选择:CentOS-6.3-i386-bin-DVD1.iso 这个下载并进行安装)
1.首先打开虚拟机: 在上面的那个按钮旁有一个下拉的符号,点开后会看到一个进入固件的按钮,直接点击进去. 便会进入这个界面: 在这个界面其实我们不需要该任何的东西,但是我们需要进入boot界面看一眼, ...
- show table detail
create table #t(name varchar(255), rows bigint, reserved varchar(20),data varchar(20), index_size va ...
- solr笔记--solr3.2以后支持document和json两种对象来更新索引
1.json形式(比如把mongodb数据库的导出结果json) <requestHandler name="/update" class="solr.JsonUp ...
- android下db-journal文件作用
在学习数据库sqlite的过程中,发现在源文件包里除了生成db类型的数据库文件,还生成了db-journal类型的同名文件 查询网上资料后知道该文件是sqlite的一个临时的日志文件,主要用于sqli ...
- ArcGIS Field Type /esriFieldTypeDate(转)
ArcGIS Field Type The following table outlines the equivalent field data types in ArcCatalog, ArcO ...
- webform 内置对象(页面间传值)
QueryString/URL传值 页面名后面加?变量名=值 有点:不占服务器内存. 缺点:保密性差:传递字符串长度有限. Response --相应请求对象 Response.Redirect ...
- Java-马士兵设计模式学习笔记-策略模式-模拟 Comparator接口
续上一篇 <Java 模拟 Comparable接口> 一.Teacher类及Student类的比较大小方式是不固定的,比如老师除了比较职称外,还可比较工龄大小,年龄大小等.则定义Com ...
- Entity Framework Tutorial Basics(21):CRUD Operation in Connected Scenario
CRUD Operation in Connected Scenario: CRUD operation in connected scenario is a fairly easy task bec ...
- AR# 30522:LogiCORE RapidIO - How do system_reset and link_reset work?
Description How do system_reset and link_rest work? Solution lnk_linkreset_n (input): In Xilinx SRIO ...
- matlab处理:批处理图像分块
有一个图像分块的代码,可以直接将一幅图像分为5*5的小块,代码如下: %[FileName,PathName] = uigetfile('*.*','Select the image'); Im=im ...