题意:从四个集合各选一个数,使和等于0,问有多少种选法。

分析:求出来所有ai + bi,在里面找所有等于ci + di的个数。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {, , -, , -, -, , };
const int dc[] = {-, , , , -, , -, };
const int MOD = 1e9 + ;
const double pi = acos(-1.0);
const double eps = 1e-;
const int MAXN = + ;
const int MAXT = + ;
using namespace std;
int a[MAXN], b[MAXN], c[MAXN], d[MAXN];
int sum[MAXT];
int main(){
int T;
scanf("%d", &T);
while(T--){
int n;
scanf("%d", &n);
for(int i = ; i < n; ++i){
int aa, bb, cc, dd;
scanf("%d%d%d%d", &a[i], &b[i], &c[i], &d[i]);
}
int cnt = ;
for(int i = ; i < n; ++i){
for(int j = ; j < n; ++j){
sum[cnt++] = a[i] + b[j];
}
}
sort(sum, sum + cnt);
int ans = ;
for(int i = ; i < n; ++i){
for(int j = ; j < n; ++j){
int t = -(c[i] + d[j]);
ans += upper_bound(sum, sum + cnt, t) - lower_bound(sum, sum + cnt, t);//sum中可能有许多与t相等的数,都要计算上
}
}
printf("%d\n", ans);
if(T) printf("\n");
}
return ;
}

UVA - 1152 4 Values whose Sum is 0(中途相遇法)的更多相关文章

  1. K - 4 Values whose Sum is 0(中途相遇法)

    K - 4 Values whose Sum is 0 Crawling in process... Crawling failed Time Limit:9000MS     Memory Limi ...

  2. 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< ...

  3. 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 ...

  4. 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 ...

  5. UVA - 1152 --- 4 Values whose Sum is 0(二分)

    问题分析 首先枚举a和b, 把所有a+b记录下来放在一个有序数组,然后枚举c和d, 在有序数组中查一查-c-d共有多少个.注意这里不可以直接用二分算法的那个模板,因为那个模板只能查找是否有某个数,一旦 ...

  6. UVA - 1152 4 Values whose Sum is 0问题分解,二分查找

    题目:点击打开题目链接 思路:暴力循环显然会超时,根据紫书提示,采取问题分解的方法,分成A+B与C+D,然后采取二分查找,复杂度降为O(n2logn) AC代码: #include <bits/ ...

  7. UVA 1152 4 Values Whose Sum is Zero 和为0的4个值 (中途相遇)

    摘要:中途相遇.对比map,快排+二分查找,Hash效率. n是4000的级别,直接O(n^4)肯定超,所以中途相遇法,O(n^2)的时间枚举其中两个的和,O(n^2)的时间枚举其他两个的和的相反数, ...

  8. 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 ...

  9. UVA-1152-4 Values whose Sum is 0---中途相遇法

    题目链接: https://cn.vjudge.net/problem/UVA-1152 题目大意: 给出4个数组,每个数组有n个数,问有多少种方案在每个数组中选一个数,使得四个数相加为0. n &l ...

随机推荐

  1. jsp 页面使用标签遍历

    <tbody> <c:forEach items="${page.list}" var="exhiMain"> <c:set va ...

  2. 「CH6201」走廊泼水节

    「CH6201」走廊泼水节 传送门 考虑 \(\text{Kruskal}\) 的过程以及用到一个最小生成树的性质即可. 在联通两个联通块时,我们肯定会选择最小的一条边来连接这两个联通块,那么这两个联 ...

  3. Python 基础之linux基础相关

    一: python3.6.x在Ubuntu16.04下安装过程 #(1)保证网络正常连接 sudo add-apt-repository ppa:jonathonf/python-3.6  (如果超时 ...

  4. super与this无法用在static方法中

    更新记录 [1]2020.02.08-10:32 1.主要编辑了super 正文 我原本是在学习\(super\)关键字,后来突发奇想,能不能在父类中用\(super\)间接调用本类方法呢? 也就是说 ...

  5. 设计模式课程 设计模式精讲 7-2 建造者模式Coding

    1 代码演练 1.1 建造者模式演练 1.2 静态内部类演练建造者模式(链式调用) 1 代码演练 1.1 建造者模式演练 需求: 根据讲师提供的课程名称,课程ppt,课程视频,课程手记,课程问答 制作 ...

  6. EcShop二次开发学习方法

    EcShop二次开发学习方法 (2012-03-08 11:10:08) 转载▼ 标签: 京东 公用函数库 二次开发 sql语言 数据库设计 杂谈 分类: ecshop 近年来,随着互联网的发展,电子 ...

  7. JAVA培训—线程同步--卖票问题

    线程同步方法: (1).同步代码块,格式: synchronized (同步对象){ //同步代码 } (2).同步方法,格式: 在方法前加synchronized修饰 问题: 多个人同时买票. 1. ...

  8. python第三方库介绍

  9. 自定义autograd

    这个操作博主认为应该是判断其requires_grad是否为True,从而判断是否进行梯度的运算但是至于这个为啥这么写,博主也不是很清楚 device = torch.device('cuda' if ...

  10. routes 学习

    对于routes的学习,感觉还是看官方文档理解的比较快,主要说明connect和resource Setting up routes¶ It is assumed that you are using ...