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 ...
随机推荐
- [转载+原创]Emgu CV on C# (二) —— Emgu CV on 灰度化
本文主要对彩色图片灰度化的方法及其实现过程进行总结,最终给出Emgu CV实现的代码. 一.灰度化原理及数学实现(转载自——<图像灰度化方法总结及其VC实现> 该篇文章使用opencv实现 ...
- windows批处理(cmd/bat)编程详解
reference: http://blog.csdn.net/bingjie1217/article/details/12947327 http://www.cnblogs.com/doit8791 ...
- Extjs4 使用store的post方法
Extjs4 使用store的post方法 引用官网的一句话 Now when we call store.load(), the AjaxProxy springs into action, mak ...
- c++ 时间与字符串转换
.时间转字符串函数 size_t strftime( char *strDest, size_t maxsize, const char *format, const struct tm *timep ...
- SQL Server 之 事务隔离级别
SET TRANSACTION ISOLATION LEVEL xxx -- 每次设置只针对当前事务块 xxx 取值: READ UNCOMMITTED READ COMMITTED REPEATA ...
- VB程序破解之API断点[bp __vbaVarTstEq]
软件名称:风云足彩1.7软件大小:2.1M下载地址:http://free.ys168.com/?zhinengxuanhao软件保护:注册码编写软件:Microsoft Visual Basic 5 ...
- httpsClient抓取证书
在执行webservice的过程中,出现如下异常: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorExcep ...
- Unity3d集成移动MM SDK 2.2的技术要点(坑爹的MM SDK)
原地址:http://dong2008hong.blog.163.com/blog/static/4696882720140423517951/ U3D集成移动MM的SDK绝对是以坑爹为主的东西. 浪 ...
- PHP 打印函数之 print print_r
print 说明 int print ( string $arg ) 输出 arg print 实际上不是一个函数(它是一个语言结构),因此你可以不必使用圆括号来括起它的参数列表 参数 arg:输入数 ...
- C#中sealed关键字
C#中sealed关键字 1. sealed关键字 当对一个类应用 sealed 修饰符时,此修饰符会阻止其他类从该类继承.类似于Java中final关键字. 在下面的示例中,类 B ...