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[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的更多相关文章
- 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< ...
- 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 ...
- UVA - 1152 4 Values whose Sum is 0(中途相遇法)
题意:从四个集合各选一个数,使和等于0,问有多少种选法. 分析:求出来所有ai + bi,在里面找所有等于ci + di的个数. #pragma comment(linker, "/STAC ...
- UVA - 1152 --- 4 Values whose Sum is 0(二分)
问题分析 首先枚举a和b, 把所有a+b记录下来放在一个有序数组,然后枚举c和d, 在有序数组中查一查-c-d共有多少个.注意这里不可以直接用二分算法的那个模板,因为那个模板只能查找是否有某个数,一旦 ...
- UVA - 1152 4 Values whose Sum is 0问题分解,二分查找
题目:点击打开题目链接 思路:暴力循环显然会超时,根据紫书提示,采取问题分解的方法,分成A+B与C+D,然后采取二分查找,复杂度降为O(n2logn) AC代码: #include <bits/ ...
- UVA 1152 4 Values Whose Sum is Zero 和为0的4个值 (中途相遇)
摘要:中途相遇.对比map,快排+二分查找,Hash效率. n是4000的级别,直接O(n^4)肯定超,所以中途相遇法,O(n^2)的时间枚举其中两个的和,O(n^2)的时间枚举其他两个的和的相反数, ...
- 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 ...
- 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: 20334 A ...
随机推荐
- C# - (0x80040154): Retrieving the COM class factory for component with CLSID {877AA945-1CB2-411C-ACD7-C70B1F9E2E32} failed
1. Exeption Error: System.Runtime.InteropServices.COMException (0x80040154): Retrieving the COM clas ...
- 使用SpringMVC+mybatis+事务控制+JSON 配置最简单WEB
最近在总结一些项目的基础知识,根据公司最近的一些意向和技术路线,初步整理了一个简单的配置例子 1.使用springmvc代替strutsMVC 2.使用请求json数据串的方式代替传统 ...
- JQuery,UIbootstrap风格弹出层
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> <met ...
- 【BZOJ】【1272】【BeiJingWC2008】Gate of Babylon
组合数学+容斥原理 Orz zyf-zyf 多重集组合数0.0还带个数限制? ——> <组合数学>第6章 6.2带重复的组合 组合数还要模P 0.0? ——> Lucas ...
- Matlab计算两集合间的海明距离
一.问题描述 B1[1 2 3 4 5 6 7 8 9] B2[12 13 14 21 31 41 51 1 1 81 1 1] 两个十进制矩阵,行数不一样,分别是n1和n2,列数必须一致,为nwo ...
- c++ 虚继承
虚继承(个人感觉用到的地方不多,项目中没有用到这个的) 最典型的例子就是iostream的继承方式 class istream : virtual public ios{...};//此处就是虚继承, ...
- C && C++ 内存分配示意图
<Unix环境系统高级编程>中的C语言内存分布示意图 1.C内存分布 BSS段: 用来存放程序中未初始化的全局变量.BSS是英文Block Started by Symbol的简称.BSS ...
- Spring Boot——2分钟构建spring web mvc REST风格HelloWorld
之前有一篇<5分钟构建spring web mvc REST风格HelloWorld>介绍了普通方式开发spring web mvc web service.接下来看看使用spring b ...
- POJ 3080 Blue Jeans (多个字符串的最长公共序列,暴力比较)
题意:给出m个字符串,找出其中的最长公共子序列,如果相同长度的有多个,输出按字母排序中的第一个. 思路:数据小,因此枚举第一个字符串的所有子字符串s,再一个个比较,是否为其它字符串的字串.判断是否为字 ...
- SDUT1281Cup
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1281 题意 : 一个杯子,告诉你底面半径,顶端 ...