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 ...
随机推荐
- 微信WeUI常见页面模板
购物车模板 就是popup弹层(css样式+js),还有slider滑动操作,还有增减的js 代码: <!DOCTYPE html> <html lang="zh-CN&q ...
- [PHP] apache在worker模式配置fastcgi使用php-fpm
1.准备: dpkg -L apache2查看所有安装的apache2的应用 a2query -M查看apache2使用的模式 httpd -l旧版本查看当前apache模式 2.查看apache的进 ...
- 撩课-Python-每天5道面试题-第3天
一. 代码实现: 计算1到100之间, 所有的奇数之和 result = , ): result += i print(result) 二. 代码实现: 接收用户输入数字, 求出从0至这个数字的累加和 ...
- 用js 实现代码获取下拉框的value值
var rtl=document.getElementById("selpartyorg"); //获取下拉框对象 var id=rtl.options[rtl.selectedI ...
- [APIO2018] Circle selection 选圆圈(假题解)
题面 自己去\(LOJ\)上找 Sol 直接排序然后\(KDTree\)查询 然后发现\(TLE\)了 然后把点旋转一下,就过了.. # include <bits/stdc++.h> # ...
- js迪杰斯特拉算法求最短路径
1.后台生成矩阵 名词解释和下图参考:https://blog.csdn.net/csdnxcn/article/details/80057574 double[,] arr = new double ...
- toast, 警告窗
//浮动提示框 1秒后消失 toast(msg, isError, sec) { var div = $('#toast'); div.html(msg); div.css({visibility: ...
- 使用nodeJs安装Vue-cli (win10 使用管理员身份)
TIP:win10下安装,使用管理员身份进行,否则会有权限限制. 1,安装完成node,node有自带的npm,可以直接在cmd中,找到nodeJs安装的路径下,进行命令行全局安装vue-cli.(n ...
- Android学习笔记(1)----播放音乐文件
原文地址:http://www.cnblogs.com/wynet/p/5526905.html 这里介绍两种播放资源文件的方法: 第一种. assets类资源放在工程根目录的assets子目录下,它 ...
- 微服务架构之spring cloud feign
在spring cloud ribbon中我们用RestTemplate实现了服务调用,可以看到我们还是需要配置服务名称,调用的方法 等等,其实spring cloud提供了更优雅的服务调用方式,就是 ...