Problem UVA1152-4 Values whose Sum is 0

Accept: 794  Submit: 10087
Time Limit: 9000 mSec

Problem 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) ∈ A×B×C×D 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 2^28) that belong respectively to A,B,C and D.

 Output

For each test case, your program has to write the number quadruplets whose sum is zero. The outputs of two consecutive cases will be separated by a blank line.

 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

题解:这个题主要是太陈了,觉得是个大水题,但是第一次见的时候不是太容易想。思想很深刻,分块,明明都是暴力枚举,但即便不加二分查找这个方法也在数量级上碾压四重for循环,感觉上有一点不可思议,想想莫队算法是不是也利用了这个思想(分块真的可以出奇迹)。

 #include <bits/stdc++.h>

 using namespace std;

 const int maxn =  + ;

 int a[maxn], b[maxn], c[maxn], d[maxn];
int sum[maxn*maxn];
int n; int main()
{
//freopen("input.txt", "r", stdin);
int iCase;
scanf("%d", &iCase);
while (iCase--) {
scanf("%d", &n);
for (int i = ; i < n; i++) {
scanf("%d%d%d%d", &a[i], &b[i], &c[i], &d[i]);
} int cnt = ;
for (int i = ; i < n; i++) {
for (int j = ; j < n; j++) {
sum[cnt++] = a[i] + b[j];
}
}
sort(sum, sum + cnt);
long long ans = ;
for (int i = ; i < n; i++) {
for (int j = ; j < n; j++) {
ans += upper_bound(sum, sum + cnt, -c[i] - d[j]) - lower_bound(sum, sum + cnt, -c[i] - d[j]);
}
} printf("%lld\n", ans);
if (iCase) printf("\n");
}
return ;
}

UVA1152-4 Values whose Sum is 0(分块)的更多相关文章

  1. UVA-1152 4 Values whose Sum is 0 (二分)

    题目大意:在4个都有n个元素的集合中,每个集合选出一个元素,使得4个数和为0.问有几种方案. 题目分析:二分.任选两组求和,剩下两组求和,枚举第一组中每一个和sum,在第二组和中查找-sum的个数,累 ...

  2. uva1152 - 4 Values whose Sum is 0(枚举,中途相遇法)

    用中途相遇法的思想来解题.分别枚举两边,和直接暴力枚举四个数组比可以降低时间复杂度. 这里用到一个很实用的技巧: 求长度为n的有序数组a中的数k的个数num? num=upper_bound(a,a+ ...

  3. 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< ...

  4. POJ 2785 4 Values whose Sum is 0(想法题)

    传送门 4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 20334   A ...

  5. POJ 2785 4 Values whose Sum is 0

    4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 13069   Accep ...

  6. 哈希-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: ...

  7. [poj2785]4 Values whose Sum is 0(hash或二分)

    4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 19322 Accepted: ...

  8. K - 4 Values whose Sum is 0(中途相遇法)

    K - 4 Values whose Sum is 0 Crawling in process... Crawling failed Time Limit:9000MS     Memory Limi ...

  9. POJ - 2785 4 Values whose Sum is 0 二分

    4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 25615   Accep ...

随机推荐

  1. nginx配置虚拟机

    在/usr/local/nginx/conf目录下nginx.conf文件是nginx的配置文件. 一.通过端口号区分虚拟机 在nginx.conf文件中添加一个Service节点,修改端口号: se ...

  2. netty-socketio 概述

    netty-socketio 概述 netty-socketio是一个开源的Socket.io服务器端的一个java的实现,它基于Netty框架,可用于服务端推送消息给客户端. 说到服务端推送技术,一 ...

  3. MyBatis学习笔记(二) Executor

    一.概述 当我们打开一个SqlSession的时候,我们就完成了操作数据库的第一步,那MyBatis是如何执行Sql的呢?其实MyBatis的增删改查都是通过Executor执行的,Executor和 ...

  4. python中的eval函数

    eval() 函数十分强大 -- 将字符串 当成 有效的表达式 来求值 并 返回计算结果 In [1]: eval("1 + 3") Out[1]: 4 In [2]: eval( ...

  5. LINUX sed grep awk之间比较整理

    正则表达式基础 在最简单的情况下,一个正则表达式看上去就是一个普通的查找串.例如,正则表达式"testing"中没有包含任何元字符,,它可以匹配"testing" ...

  6. CDRAF之Service mesh

    最近翻看一些网上的文章,偶然发现我们的CDRAF其实就是Service mesh的C++版本.不管从架构的理念上,或者功能的支持上面,基本完全符合.发几个简单的文章链接,等有时间的时候,再来详细描述. ...

  7. HTTP与HTTPs的区别?

    简而言之:   HTTPS = HTTP + SSL HTTP 的 URL 以 http:// 开头,而 HTTPS 的 URL 以 https:// 开头 HTTP 是不安全的,而 HTTPS 是安 ...

  8. mapper代理方式开发

    使用mapper代理方式开发: 需要编写mapper接口,UserMapper.java需要编写映射文件,UserMapper.xml需要遵循一些开发规范,mybatis便可以自动生成mapper接口 ...

  9. osx安装sass

    1.安装ruby (1)OS X 缺少的套件管理工具 homebrew 的安装 http://brew.sh/index_zh-tw.html (2)通过homebrew安装ruby // 到文件夹 ...

  10. K8S helm

    Helm 致力于成为 Kubernetes 的包管理工具,可以方便地发现.共享和构建 Kubernetes 应用,它包含几个基本概念: Chart:一个 Helm 包,其中包含了运行一个应用所需要的镜 ...