UVA-1152-4 Values whose Sum is 0---中途相遇法
题目链接:
https://cn.vjudge.net/problem/UVA-1152
题目大意:
给出4个数组,每个数组有n个数,问有多少种方案在每个数组中选一个数,使得四个数相加为0.
n <= 4000
解题思路:
两重循环求出a + b的所有情况
两重循环求出-c - d的所有情况
枚举a+b的值,在-c-d里面找相同值的数目即可。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
#include<queue>
#include<map>
#include<stack>
#include<vector>
#include<list>
#include<deque>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + ;
const double eps = 1e-;
const int INF = << ;
int T, n, m;
int a[], b[], c[], d[];
int ab[], cd[];
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i = ; i < n; i++)
{
scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);
}
int tot = , ans = ;
for(int i = ; i < n; i++)
{
for(int j = ; j < n; j++)
{
ab[tot] = a[i] + b[j];
cd[tot++] = c[i] + d[j];
}
}
sort(ab, ab + tot);
for(int i = ; i < tot; i++)
ans += upper_bound(ab, ab + tot, -cd[i]) - lower_bound(ab, ab + tot, -cd[i]);
cout<<ans<<endl;
if(T)cout<<endl;
}
return ;
}
UVA-1152-4 Values whose Sum is 0---中途相遇法的更多相关文章
- K - 4 Values whose Sum is 0(中途相遇法)
K - 4 Values whose Sum is 0 Crawling in process... Crawling failed Time Limit:9000MS Memory Limi ...
- 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
题意:给出n,四个集合a,b,c,d每个集合分别有n个数,分别从a,b,c,d中选取一个数相加,问使得a+b+c+d=0的选法有多少种 看的紫书,先试着用hash写了一下, 是用hash[]记录下来a ...
- 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 ...
随机推荐
- echarts实现自动轮播tooltip
最近需要实现echarts图形中hover效果轮播(即tooltip在各个数据点上轮流显示)的功能,以下就是我学习的一个过程,只是提供思路,具体场景需要自己修改.(仅针对echarts 2.2.7及以 ...
- WPF判断两个时间大小避免误差
进行查询操作的时候,经常用到判断开始时间和结束时间大小的条件,由于从控件上获取的时间除了年月日时分秒,还包括毫秒.微秒等,导致直接判断时间大小的时候会产生一些误差,如下: 结果分析:年月日时分秒一致的 ...
- 类库里面添加日志记录 log4net
第一步: 新建一个公共类库common,添加CustomLog4jLogger.cs 并引用log4net.dll /// <summary> /// 日志记录 /// </summ ...
- java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/20 from pid=711, uid=10074 requires android.permission.READ_
java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider ur ...
- 深入辨析jvm内存区域
Java内存区域 Java虚拟机在执行Java程序的过程中会把它所管理的内存划分为若干个不同的数据区域: 程序计数器.虚拟机栈.本地方法栈.Java堆.方法区(运行时常量池).直接内存 程序计数器 当 ...
- VPS虚拟化架构OpenVZ、KVM、Xen、Hyper-V的区别
1.OpenVZ OpenVZ(简称OVZ)采用SWsoft的Virutozzo虚拟化服务器软件产品的内核,是基于Linux平台的操作系统级服务器虚拟化架构.这个架构直接调用宿主机(俗称:母机)中的内 ...
- 线程协作--wait,notify:经典消费者生产者
JDK 中关于wait,notify这两个方法的介绍: 1.wait:线程进入阻塞. synchronized (obj) { while (<condition does not hold&g ...
- jQuery bind()与delegate()的区别
笔试题: bind()与delegate()的区别主要有三点: 1 绑定目标 .bind直接绑在目标元素上 .delegate绑在父元素上 2 监听个数 .bind监听个数多——每个目标元素都需要添 ...
- 前端学习之路之CSS (四)
Infi-chu: http://www.cnblogs.com/Infi-chu/ CSS盒子模型 概念:CSS盒模型本质上是一个盒子,封装周围的HTML元素,它包括:边距,边框,填充,和实际 ...
- iView定制主题报错问题的解决方法
按照iView官网来是这样的: 1. 在main.js当前目录下新建themes文件夹,里面新建一个叫blue.less的文件 2. 在mian.js里面引入blue.less文件 3. blue.l ...