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 ...
随机推荐
- HTML5模板引擎 Thymeleaf 教程(转)
原文:http://www.open-open.com/lib/view/open1383622135586.html Thymeleaf是一个XML/XHTML/HTML5模板引擎,可用于Web与非 ...
- C++中文件的读取操作,如何读取多行数据,如何一个一个的读取数据
练习8.1:编写函数.接受一个istream&参数,返回值类型也是istream&.此函数必须从给定流中读取数据,直至遇到文件结束标识时停止. #include <iostrea ...
- php 增删改查范例(3)
编辑页面edit.php: <?php$id=$_GET['id'];$db= new mysqli('localhost','root','root','db_0808');$sql=&quo ...
- NSIndexPath等结构体的比较
1.NSIndexPath的比较方式,需要将结构体内部的属性一一对比.比如, if ((indexPath.section == self.selectIndexPath.section) & ...
- unity优化-CPU(网上整理)
CPU方面性能考虑:引擎和代码渲染模块.动画模块.物理模块.ui模块.粒子模块.加载模块.GC模块最重要的是渲染模块.UI模块和加载模块1.渲染模块主要是:场景.物体和特效的渲染a.降低Draw ca ...
- pycharm不能安装第三方库,错误代码Non-zero exit code (1) 的解决办法
pycharm版本 2019.3 大致意思是安装失败,建议的解决方案:尝试从系统终端运行此命令.确保使用正确的'pip'版本,该版本已为位于'C:\ Users \ G \ Desktoplgianf ...
- SparkSQL 疫情Demo练习
在家闲着没事干, 写个简单的疫情数据处理Demo, 顺便回顾下SparkSQL. 模拟数据(以下数据皆为虚构, 如有雷同不胜荣幸) 市民信息(civic_info.csv) id_no,name,se ...
- Keras入门——(5)长短期记忆网络LSTM(二)
参考: https://blog.csdn.net/zwqjoy/article/details/80493341 https://blog.csdn.net/u012735708/article/d ...
- [c#]如何访问 JArray 的元素
JArray 格式文件. public void TestJson() { var jsonString = @"{""trends"": [ { & ...
- 前端Cannot read property 'disabled' of null 问题解决
就是在项目中,控制台一直在报这个错,一直没找到是什么问题, 后来经过一番排查,发现是 因为在页面中使用了el-dropdown,但是在这个标签里面没有设置它的子元素,所以会报错,解决的方法就是在el- ...