UVA - 1152 4 Values whose Sum is 0(中途相遇法)
题意:从四个集合各选一个数,使和等于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(中途相遇法)的更多相关文章
- 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(二分)
问题分析 首先枚举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 ...
- UVA-1152-4 Values whose Sum is 0---中途相遇法
题目链接: https://cn.vjudge.net/problem/UVA-1152 题目大意: 给出4个数组,每个数组有n个数,问有多少种方案在每个数组中选一个数,使得四个数相加为0. n &l ...
随机推荐
- gitlab两种连接方式:ssh和http配置介绍 --转自 散尽浮华
gitlab环境部署好后,创建project工程,在本地或远程下载gitlab代码,有两种方式:ssh和http 1)ssh方式:这是一种相对安全的方式 这要求将本地的公钥上传到gitlab中,如下图 ...
- 转:Entity Framework 5.0 Code First全面学习
Code First 约定 借助 CodeFirst,可通过使用 C# 或Visual Basic .NET 类来描述模型.模型的基本形状可通过约定来检测.约定是规则集,用于在使用 Code Firs ...
- 使用C语言实现文件的操作
#include <stdio.h> int main(int argc, char* argv[]) { // 创建文件类型 FILE* file; char buf[1024] = { ...
- Servlet(1):request和response对象
1.Servlet的生命周期 /** * Servlet的生命周期: * 1.从第一次调用到服务器关闭. * 2.如果Servlet在web.xml中配置了load-on-startup,生命周期为从 ...
- Python中from scipy.misc import imread报错的原因?
from scipy.misc import imread 报错 查询后其原因是from scipy.misc import imread,imwrite 等方法已经被弃用,Python已经将imre ...
- android 基础学习(6)-----sqlite3查看表结构
原文:http://blog.csdn.net/richnaly/article/details/7831933 sqlite3查看表结构 在android下通过adb shell命令可以进入sqli ...
- 【Python数组及其基础操作】【numpy ndarray】
一.创建数组 在python中创建数组最简单的办法就是使用array函数.它接受一切序列型的对象,然后产生一个含有传入数据的numpy数组.其中,嵌套序列(比如由一组等长列表组成的列表)会被转换为一个 ...
- java的基本类型和对应的封装类
封装,是java这门语言的重要核心思想之一,封装也是对面向对象这一思想很好的体现. 在很多情况下,我们需要对数据进行一些转换,如:将一字符串"123"转换成int类型的123,或者 ...
- Day2-I-Knight's Problem POJ - 3985
You must have heard of the Knight's Tour problem. In that problem, a knight is placed on an empty ch ...
- Pyspider的基本使用 -- 入门
简介 一个国人编写的强大的网络爬虫系统并带有强大的WebUI 采用Python语言编写,分布式架构,支持多种数据库后端,强大的WebUI支持脚本编辑器,任务监视器,项目管理器以及结果查看器 官方文档: ...