题意:给出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. centos 7 有点意思

    Centos 7 防火墙 Centos尼马换了防火墙,名叫firewalld,还有iptables命令,这就叫坑爹.整了半个世纪才知道,他换了防火墙.添加滤镜,停止iptables都打不开80端口,简 ...

  2. oracle——外连接查询

    一.问题描述 有时我们为了保留某个表中的数据,而该表中的数据在另外一个关联表中未必都存在对应,此时就应该试用外连接查询. 比如:两个表,产品表和子产品表 注:子产品的parent_product_id ...

  3. .bash_profile备份

    # ~/.bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User sp ...

  4. WCF 笔记 (2) - 传输泛型 List 对象

    WCF 笔记 (2) - 传输泛型 List 对象 本帖介绍怎么在 WCF 中,在 Server-side 和 Client-side 之间,传递默认无法传输的 List<T>.List& ...

  5. mySql 的基本操作

    mysql -uroot -proot show databases; use ltcl_net;show tables; desc tablename; 查看表结构 create table tes ...

  6. php集成开发环境的安装以及Zend Studio开发工具的安装

    一.集成开发环境: wampserver 下载地址: 官网: http://www.wampserver.com/ 直接下载 http://sourceforge.net/projects/wamps ...

  7. 51Nod 有限背包计数问题 题解报告

    首先这道题理论上是可以做到O(nlogn)的,因为OEIS上有一个明显可以用多项式乘法加速的式子 但是由于模数不是很兹磁,所以导致nlogn很难写 在这里说一下O(n*sqrt(n))的做法 首先我们 ...

  8. HTML5入门2---js获取HTML元素的值

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  9. 80. Remove Duplicates from Sorted Array II

    题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...

  10. java对称加密报错:Input length must be multiple of 8 when decrypting with padded cipher

    HTTP Status 500 - Request processing failed; nested exception is javax.crypto.IllegalBlockSizeExcept ...