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 = . 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 ).
We then have n lines containing four integer values (with absolute value as large as ) 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 - -
- -
- -
- - -
- -
- - - 45

Sample Output

Sample Explanation: Indeed, the sum of the five following quadruplets is zero: (-, -, , ),
(, , -, -), (-, , , -), (-, , -, ), (-, -, , ).

解题思路:

  枚举并存储A+B的和,然后枚举C+D,搜索-C-D的个数,问题的关键是如何存储A+B的和。本题数据量不小,极限数据n=4000时,A+B的和有16,000,000个,数组显然开不下。那么不妨建立哈希表来存储。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <set>
#include <vector>
#include <ctime>
#define H 1000000
#define maxn 4000
#define time__ cout<<" time: "<<double(clock())/CLOCKS_PER_SEC<<endl;
using namespace std;
vector<int> Hash2[H]; int A[maxn+];
int B[maxn+];
int C[maxn+];
int D[maxn+];
int n;
inline void Hash_clear(){
for(int i=;i<H;i++)
Hash2[i].clear();
}
inline int h(int x){
return abs(x%H);
}
inline int count_(int x){ int h_=h(x);
int cnt=;
for(int i=;i<Hash2[h_].size();i++)
if(Hash2[h_][i]==x) cnt++;
return cnt; }
int main(int argc, const char * argv[]) { int T;
scanf("%d",&T);
while (T--) {
Hash_clear(); int cnt=;
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%d%d%d%d",&A[i],&B[i],&C[i],&D[i]);
for(int i=;i<n;i++)
for(int j=;j<n;j++){
int x=A[i]+B[j]; Hash2[h(x)].push_back(x); }
for(int i=;i<n;i++)
for(int j=;j<n;j++){
int x=C[i]+D[j];
cnt+=count_(-x);
}
cout<<cnt<<endl;
if(T)
cout<<endl;
}
//time__;
return ;
}

UVa 1152 -4 Values whose Sum is 0—[哈希表实现]的更多相关文章

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

  2. UVA - 1152 4 Values whose Sum is 0(中途相遇法)

    题意:从四个集合各选一个数,使和等于0,问有多少种选法. 分析:求出来所有ai + bi,在里面找所有等于ci + di的个数. #pragma comment(linker, "/STAC ...

  3. UVa 1152 4 Values whose Sum is 0

    题意:给出n,四个集合a,b,c,d每个集合分别有n个数,分别从a,b,c,d中选取一个数相加,问使得a+b+c+d=0的选法有多少种 看的紫书,先试着用hash写了一下, 是用hash[]记录下来a ...

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

    问题分析 首先枚举a和b, 把所有a+b记录下来放在一个有序数组,然后枚举c和d, 在有序数组中查一查-c-d共有多少个.注意这里不可以直接用二分算法的那个模板,因为那个模板只能查找是否有某个数,一旦 ...

  5. UVA - 1152 4 Values whose Sum is 0问题分解,二分查找

    题目:点击打开题目链接 思路:暴力循环显然会超时,根据紫书提示,采取问题分解的方法,分成A+B与C+D,然后采取二分查找,复杂度降为O(n2logn) AC代码: #include <bits/ ...

  6. UVA 1152 4 Values Whose Sum is Zero 和为0的4个值 (中途相遇)

    摘要:中途相遇.对比map,快排+二分查找,Hash效率. n是4000的级别,直接O(n^4)肯定超,所以中途相遇法,O(n^2)的时间枚举其中两个的和,O(n^2)的时间枚举其他两个的和的相反数, ...

  7. uva 1152 4 values whose sum is zero ——yhx

    The SUM problem can be formulated as follows: given four lists A;B;C;D of integer values, computehow ...

  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: 20334   A ...

随机推荐

  1. list reverse

    You can make use of the reversed function for this as: >>> array=[0,10,20,40] >>> ...

  2. re模块相关

    一.正则表达式中的转义: "\" 表示转义符 [()+*?/$.] 在字符组中一些特殊的字符会现出原形 所有的\w \d \s (\n,\t) \W \D \S 都表示它原本的意义 ...

  3. k8s 超详细总结,面试必问

    一个目标:容器操作:两地三中心:四层服务发现:五种Pod共享资源:六个CNI常用插件:七层负载均衡:八种隔离维度:九个网络模型原则:十类IP地址:百级产品线:千级物理机:万级容器:相如无亿,K8s有亿 ...

  4. python 字符串匹配的应用

  5. LeetCode225 Implement Stack using Queues

    Implement the following operations of a stack using queues. (Easy) push(x) -- Push element x onto st ...

  6. Java练习 SDUT-3337_计算长方体、四棱锥的表面积和体积

    计算长方体.四棱锥的表面积和体积 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 计算如下立体图形的表面积和体积. 从图 ...

  7. 冒泡排序 Day07

    package com.sxt.arraytest2; /* * 冒泡排序 * 思想:两两交换 一路大的向右 */ import java.util.Arrays; public class Bubb ...

  8. 洞见数据库前沿 阿里云数据库最强阵容 DTCC 2019 八大亮点抢先看

    摘要: 作为DTCC的老朋友和全球领先的云计算厂商,阿里云数据库团队受邀参加本次技术盛会,不仅将派出重量级嘉宾阵容,还会为广大数据库业内人士和行业用户奉上8场精彩议题.下面小编就为大家提前梳理了8大亮 ...

  9. oracle函数 last_day(d1)

    [功能]:返回日期d1所在月份最后一天的日期. [参数]:d1,日期型 [返回]:日期 [示例]select sysdate,last_day(sysdate)  hz from dual; 返回:2 ...

  10. 选用适合的ORACLE优化器

    ORACLE的优化器共有3种: a.  RULE (基于规则)   b. COST (基于成本)  c. CHOOSE (选择性) 设置缺省的优化器,可以通过对init.ora文件中OPTIMIZER ...