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
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
题解:这个题主要是太陈了,觉得是个大水题,但是第一次见的时候不是太容易想。思想很深刻,分块,明明都是暴力枚举,但即便不加二分查找这个方法也在数量级上碾压四重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(分块)的更多相关文章
- UVA-1152 4 Values whose Sum is 0 (二分)
题目大意:在4个都有n个元素的集合中,每个集合选出一个元素,使得4个数和为0.问有几种方案. 题目分析:二分.任选两组求和,剩下两组求和,枚举第一组中每一个和sum,在第二组和中查找-sum的个数,累 ...
- uva1152 - 4 Values whose Sum is 0(枚举,中途相遇法)
用中途相遇法的思想来解题.分别枚举两边,和直接暴力枚举四个数组比可以降低时间复杂度. 这里用到一个很实用的技巧: 求长度为n的有序数组a中的数k的个数num? num=upper_bound(a,a+ ...
- 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: ...
- K - 4 Values whose Sum is 0(中途相遇法)
K - 4 Values whose Sum is 0 Crawling in process... Crawling failed Time Limit:9000MS Memory Limi ...
- POJ - 2785 4 Values whose Sum is 0 二分
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 25615 Accep ...
随机推荐
- matlab rank
k =rank(A) %a is matrix s = svd(A); tol = max(size(A))*eps(max(s)); r = sum(s > tol);
- es6 语法 (let 和const)
一.let 和const 1.let 只在自己声明的块作用域中有效: function test(){ let a = 'a'; var b = 'b'; for(let i =1;i<3;i+ ...
- sublime text2 中标签高亮效果BracketHighlighter插件
1.打开package Control,选择install Package 2.输入BracketHighlighter,回车 3.这样该插件会自动安装,安装后所有的提示高亮都是白色或没有提示.按 p ...
- RequireJS模块化编程详解
1.模块的写法 模块化编程一般都有这么几个过渡过程,如下描述. 原始方法 function m1(){ //... } function m2(){ //... } 上面的函数m1()和m2(),组成 ...
- Redis 开启远程连接
默认 bind 127.0.0.1 即绑定本机 IP,只能本机访问,你也可以绑定别的 IP 地址,如果注释掉,表示不限制 IP,所有 IP 都能访问 # ~~~ WARNING ~~~ If the ...
- 性能测试 基于Python结合InfluxDB及Grafana图表实时采集Linux多主机或Docker容器性能数据
基于Python结合InfluxDB及Grafana图表实时采集Linux多主机性能数据 by:授客 QQ:1033553122 实现功能 1 测试环境 1 环境搭建 3 使用前提 3 使用方法 ...
- loadrunner 脚本优化-参数化方法
脚本优化-参数化方法 by:授客 QQ:1033553122 方法一 1.确定需要参数化的内容 2.选中需要参数化的内容 3.右键选中的内容->Replace with a Parameter- ...
- MFC 键盘响应
键盘响应 插入函数:在...对话框/menu中进入建立类模式,建立preTranslateMessage(MSG * pMsg) 在CXXXView类中,添加: BOOL CMy9_1View::Pr ...
- Django 2.0 URL新版配置介绍
实例 先看一个例子: from django.urls import path from . import views urlpatterns = [ path('articles/2003/', v ...
- Spark GraphX快速入门
GraphX是Spark用于图形并行计算的新组件.在较高的层次上,GraphX通过引入一个新的Graph抽象来扩展Spark RDD:一个定向的多图,其属性附加到每个定点和边.为了支持图计算,Grap ...