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 ...
随机推荐
- 4.6 explain 之 rows
一.说明 根据表统计信息及索引选用情况,大致估算出找到所需的记录所需读取的行数. 二.示例 关注我的公众号,精彩内容不能错过
- Spring Boot Docker 实战
Spring Boot Docker 开发环境 开发工具: Intellij IDEA 2018.2.6 springboot: 2.0.6.RELEASE jdk: 1.8.0_192 maven: ...
- 02-Java中的对象和类
面向对象: 程序由对象构成,每个对象包含对用户公开的特定功能部分(public)和隐藏实现部分(private). 类: 构造对象的模板 对象: 对象的行为 --- 可以对对象施加的操作(方法) 对象 ...
- Eureka开启登录认证
Eureka服务端配置 一.Eureka的pom.xml 引入spring-boot-starter-security坐标 <dependency> <groupId>org. ...
- 价值1.35亿美元的BUG
价值1.35亿美元的BUG 译者按: 一横值千金啊! 原文: Mariner 1’s $135 million software bug 译者: Fundebug 为了保证可读性,本文采用意译而非直译 ...
- 2018-07-30 对DLL库中的接口进行中文命名
补注: 此文是在探究在Windows上编写DLL时不能使用中文命名 · Issue #74 · program-in-chinese/overview问题时编写的演示用代码, 代码基于官方文档. 正如 ...
- Linux 进程调度的主要策略
1.Linux 下进程分为5种类别,分别是停止类.截止类.实时类.公平类.空闲类, 每种类别都有一个运行队列,每次调度时,就是先按照类别优先级排序,再按照每个类别内的最高优先级任务调度运行. 文件:c ...
- Git 结合Git使用Bitbucket进行代码版本管理流程规范与实践
结合Git使用Bitbucket进行代码版本管理流程规范与实践 By:授客 QQ:1033553122 目录 目录 1 一. 测试环境 2 二. 新建项目 2 三. 新建公有版本库 3 四. ...
- 章节四、1-if条件语句
package introduction5; public class ConditionalStatement { public static void main(String[] args) { ...
- 洗礼灵魂,修炼python(75)--全栈项目实战篇(3)—— 账户注册登录管理系统
要求: 1.系统可以创建用户和登录用户,根据用户的输入不同,做出不同的反应(创建还是登录) 2.创建用户不能创建已存在的用户名 3.登录用户的操作最多只能有三次,超过三次冻结账户,每使用一次提示用户还 ...