UVA1152 4Values whose Sum is 0
Description
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 ) $ \in$AxBxCxD are such that a + b + c + d = 0 . In the following, we assume that all lists have the same size n .
Input
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.
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 228 ) that belong respectively to A, B, C and D .
Output
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.
For each input file, your program has to write the number quadruplets whose sum is zero.
Sample Input
1
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
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).
题解:不超时最好。。先枚举a,b,然后检查-(c+d)的值,还是二分优化。
AC代码:
#include <algorithm>
#include <iostream>
using namespace std;
const int Max = + ;
int a[Max],b[Max],c[Max],d[Max];
int ab[];
int total;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
for(int i=; i<n; i++)
{
cin>>a[i]>>b[i]>>c[i]>>d[i];
}
int k=;
for(int i=;i<n; i++)
{
for(int j=;j<n; j++)
{
ab[k]=a[i]+b[j];
k++;
}
}
sort(ab,ab+k);
total=;
int s,l,r,mid;
for(int i=; i<n; i++)
{
for(int j=; j<n; j++)
{
int x=-c[i]-d[j];
l=,r=k-;
while(l<=r)
{
mid=(l+r)/;
if(ab[mid]>x)
r=mid-;
else if(ab[mid]<x)
l=mid+;
else
{
for(s=mid;s>=;s--)
{
if(ab[s]==x)
total++;
else
break;
}
for(s=mid+; s<k; s++)
{
if(ab[s]==x)
total++;
else
break;
}
break;
}
}
}
}
cout<<total<<endl;
if(t>)
cout<<endl;
}
return ;
}
UVA1152 4Values 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< ...
- 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 ...
- 哈希-4 Values whose Sum is 0 分类: POJ 哈希 2015-08-07 09:51 3人阅读 评论(0) 收藏
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 17875 Accepted: ...
- [poj2785]4 Values whose Sum is 0(hash或二分)
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 19322 Accepted: ...
- POJ-2785 4 Values whose Sum is 0(折半枚举 sort + 二分)
题目链接:http://poj.org/problem?id=2785 题意是给你4个数列.要从每个数列中各取一个数,使得四个数的sum为0,求出这样的组合的情况个数. 其中一个数列有多个相同的数字时 ...
- K - 4 Values whose Sum is 0(中途相遇法)
K - 4 Values whose Sum is 0 Crawling in process... Crawling failed Time Limit:9000MS Memory Limi ...
- lintcode 中等题:Submatrix sum is 0 和为零的子矩阵
和为零的子矩阵 给定一个整数矩阵,请找出一个子矩阵,使得其数字之和等于0.输出答案时,请返回左上数字和右下数字的坐标. 样例 给定矩阵 [ [1 ,5 ,7], [3 ,7 ,-8], [4 ,-8 ...
- UVA1152-4 Values whose Sum is 0(分块)
Problem UVA1152-4 Values whose Sum is 0 Accept: 794 Submit: 10087Time Limit: 9000 mSec Problem Desc ...
随机推荐
- Java系统变量设置方式
近期碰到一个编码的问题,发现整个平台都是用的GB2312,因此导致webservice调用时有些字不能正常接受. 反编译中间件的源码如下: public static final String nod ...
- LA 3263 欧拉定理
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- 使用IndexReader.repen提高搜索速度
1,使用indexreader创建indexsearcher. 2,indexsearcher在使用完了以后不要关闭. 3.使用indexreader.isCurrent()判断索引是否被indexw ...
- pat 1060. Are They Equal (25)
题目意思直接,要求将两个数转为科学计数法表示,然后比较是否相同 不过有精度要求 /* test 6 3 0.00 00.00 test 3 3 0.1 0.001 0.001=0.1*10^-2 p ...
- phoneGap 中修改生成APP的名字
最近忙着研究移动开发的事情,去学习了一下移动开发的东西,例如eclipse和phoneGap进行配合使用,感觉还是不错的,先针对eclipse和phoneGa的平台搭建这里先不在详细说啦,主要还是我们 ...
- Android技术精髓-Bitmap详解
Bitmap (android.graphics.Bitmap) Bitmap是Android系统中的图像处理的最重要类之一.用它可以获取图像文件信息,进行图像剪切.旋转.缩放等操作,并可以指定格式保 ...
- MongoDB C++ 2.4.5 driver 编译安装问题
安装参考前文,http://blog.csdn.net/sheismylife/article/details/8794589 方法一致.只不过这次在GCC4.8.1上编译. scons instal ...
- oracle 添加自增索引
1.添加一个Sequence,此处为ID_SEQUENCE. 2.添加对应表,并设置主键 3.设置触发器 create or replace trigger sys.id_add before ins ...
- Servlet学习--练习示例总结
醉了醉了..本来想测试下Servlet生命周期的,然后调了好久的错误,还是没成功,不知道为什么不能这样做 贴上代码: import java.io.IOException; import java.i ...
- ASP.NET 实现上一篇文章 下一篇文章
select top 1 * from job_hrnews where newsid>162 --下一篇 select top 1 * from job_hrnews where newsi ...