题意:给出n,四个集合a,b,c,d每个集合分别有n个数,分别从a,b,c,d中选取一个数相加,问使得a+b+c+d=0的选法有多少种

看的紫书,先试着用hash写了一下,

是用hash[]记录下来a[i]+b[j]的值,

如果a[i]+b[j]>0,hash[a[i]+b[j]]=1

如果a[i]+b[j]<0,hash[-(a[i]+b[j])]=-1

再用一个hash0[]去储存c[i]+d[j] 这样只需要满足hash[i]==1||hash0[i]==-1或者hash[i]==-1,hash0[i]==1就可以了

可是这样写过不了样例,因为在选择的过程中,不一定只有一种选择的和为i,最后可能会将本应该符合题意的hash值覆盖掉

然后学习的lrj的代码,记录下所有a[]+b[]的值,在这个序列里面找-c[i]-d[j] 有多少个

 #include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
#define mod=1e9+7;
using namespace std; typedef long long LL;
const int maxn=+;
int s[maxn*maxn];
int a[maxn],b[maxn],c[maxn],d[maxn]; int main(){
int t,n,i,j,cnt;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(i=;i<n;i++) scanf("%d %d %d %d",&a[i],&b[i],&c[i],&d[i]); int len=;
for(i=;i<n;i++){
for(j=;j<n;j++)
s[len++]=a[i]+b[j];
} sort(s,s+len); cnt=;
for(i=;i<n;i++){
for(j=;j<n;j++)
cnt += upper_bound(s,s+len, -c[i]-d[j]) - lower_bound(s,s+len, -c[i]-d[j]);
}
cout<<cnt<<"\n";
if(t) cout<<"\n";
}
}

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—[哈希表实现]

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

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

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

  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. Codeforces Round #321 (Div. 2) D. Kefa and Dishes 状压dp

    题目链接: 题目 D. Kefa and Dishes time limit per test:2 seconds memory limit per test:256 megabytes 问题描述 W ...

  2. oracle字符集问题总结

    在进行web开发和oracle安装的过程中经常有人对字符集搞不清楚,因此对此做一下总结. 1.第一个问题:字符集之间的区别是什么呢?   常见的字符集有:UTF-8和GBK   (1)GBK字符集 G ...

  3. css文件都写在一个里面还是每个页面都引用单独的css样式好?

    因为网站比较小,外加网站页面有很多重复构件,决定采用“构件复用”搭建网页,但是遇到了一个问题.因为虽然有共同的css,但是每个页面或多或少都有独立的样式控制,到底是写在同一个css还是分离看上去清楚一 ...

  4. React vs Angular 2: 冰与火之歌

    黄玄 · 3 个月前 本文译自 Angular 2 versus React: There Will Be Blood ,其实之前有人翻译过,但是翻得水平有一点不忍直视,我们不希望浪费这篇好文章. 本 ...

  5. java基础知识回顾之javaIO类--java序列化和反序列化

    /** *  * 一:理解序列化反序列化及其应用 * 序列化:把堆内存的对象转化成字节流的过程. * 反序列化:把字节流序列恢复重构成对象的过程. * 对象的序列化的用途:1.把对象的字节序列持久化, ...

  6. Download Manager

    从Android 2.3(API level 9)开始Android用系统服务(Service)的方式提供了Download Manager来优化处理长时间的下载操作.Download Manager ...

  7. hdu 4335 What is N?

    此题用到的公式:a^b%c=a^(b%phi(c)+phi(c))%c (b>=phi(c)). 1.当n!<phi(p)时,直接暴力掉: 2.当n!>=phi(p) &&a ...

  8. Android Service 的一些笔记

    绑定服务: 用于间接调用服务里面的方法.如果调用者Activity被销毁了,服务也跟着销毁了,服务也会跟着销毁. 开启服务: 不可以调用服务里面的方法.如果调用者的Activity退出了,服务还会长期 ...

  9. Minimum_Window_Substring两种方法求解

    题目描述: Given a string S and a string T, find the minimum window in S which will contain all the chara ...

  10. [topcoder]BoxesDiv2

    https://community.topcoder.com/stat?c=problem_statement&pm=13192 #include <vector> #includ ...