UVA - 1152 --- 4 Values whose Sum is 0(二分)
问题分析
首先枚举a和b, 把所有a+b记录下来放在一个有序数组,然后枚举c和d, 在有序数组中查一查-c-d共有多少个。注意这里不可以直接用二分算法的那个模板,因为那个模板只能查找是否有某个数,一旦找到便退出。利用lower_bound,upper_bound比较方便,这两个函数就是用二分实现的,二者之差就是相等的那部分。
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL T, n;
const int maxn = 4000 + 10;
LL a[maxn], b[maxn], c[maxn], d[maxn];
LL s1[maxn*maxn];
int cnt, ans;
int main()
{
std::ios::sync_with_stdio(false);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
cin >> T;
for(LL t = 1; t <= T; t++)
{
cnt = 0, ans = 0;
if(t != 1)
cout << endl;
cin >> n;
for(LL i = 0; i < n; i++)
cin >> a[i] >> b[i] >> c[i] >> d[i];
memset(s1, 0, sizeof(s1));
for(LL i = 0; i < n; i++)
for(LL j = 0; j < n; j++)
{
s1[cnt] = a[i] + b[j];
cnt++;
}
sort(s1, s1 + cnt);
for(LL i = 0; i < n; i++)
{
for(LL j = 0; j < n; j++)
{
ans += upper_bound(s1, s1 + cnt, -(c[i] + d[j])) - lower_bound(s1, s1 + cnt, -(c[i] + d[j]));
}
}
cout << ans << endl;
}
}
UVA - 1152 --- 4 Values whose Sum is 0(二分)的更多相关文章
- 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—[哈希表实现]
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
题意:给出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问题分解,二分查找
题目:点击打开题目链接 思路:暴力循环显然会超时,根据紫书提示,采取问题分解的方法,分成A+B与C+D,然后采取二分查找,复杂度降为O(n2logn) AC代码: #include <bits/ ...
- POJ - 2785 4 Values whose Sum is 0 二分
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 25615 Accep ...
- 4 Values whose Sum is 0(二分)
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 21370 Accep ...
- POJ 2785:4 Values whose Sum is 0 二分
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 18221 Accep ...
- UVA 1152 4 Values Whose Sum is Zero 和为0的4个值 (中途相遇)
摘要:中途相遇.对比map,快排+二分查找,Hash效率. n是4000的级别,直接O(n^4)肯定超,所以中途相遇法,O(n^2)的时间枚举其中两个的和,O(n^2)的时间枚举其他两个的和的相反数, ...
随机推荐
- HTML连载22-序选择器(下)
一.子元素选择器 1. (1)选中标签之中只有一个子元素的子元素,并且那个标签必须使我们格式中前面指定的标签才行 (2)格式: 标签:only-chirld{属性:值:} (3)举例: p:only- ...
- ASP.NET Core Web Api之JWT(一)
前言 最近沉寂了一段,主要是上半年相当于休息和调整了一段时间,接下来我将开始陆续学习一些新的技术,比如Docker.Jenkins等,都会以生活实例从零开始讲解起,到时一并和大家分享和交流.接下来几节 ...
- jekyll搭建个人博客2
目录 个性化 jekyll目录结构 修改个人信息 修改头像 修改背景颜色 关于头像的效果 图片问题 域名 个性化 jekyll目录结构 个性化就是要对文件内容作出修改,使得博客外观发生变化,在修改文件 ...
- ElementUI 源码简析——源码结构篇
ElementUI 作为当前运用的最广的 Vue PC 端组件库,很多 Vue 组件库的架构都是参照 ElementUI 做的.作为一个有梦想的前端(咸鱼),当然需要好好学习一番这套比较成熟的架构. ...
- linux weblogic12.1.3.0卸载过程
主要是一开始以为跟之前版本一样有uninstall.sh,但却找不到. 最后google 才发现改了个名字,deinstall.sh 可能后面的版本都是这个脚本了吧. 先进入脚本目录,命令:cd /h ...
- Ubuntu中目录右下角的锁标志
在ubuntu中如果是用root权限创建的目录,在普通权限下展示时则会出现一个锁的标志,表明该目录是一个受保护的目录(只读,不能写入和删除),普通权限无法对其进行操作.如用命令sudo mkdir d ...
- 2019年7月20日 - LeetCode0003
https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/submissions/ 我的解法: c ...
- 2019年7月19日 - LeetCode0001
https://leetcode-cn.com/problems/two-sum/ 我的方法: class Solution { public int[] twoSum(int[] nums, int ...
- 2017day2
系统模块: # Author: sonny# -*- coding:utf-8 -*-import sys; #print(sys.path);print(sys.argv);print(sys.ar ...
- pycharm编辑器配置(持续更新完善)
谨记:pycharm仅是一款编辑器,不要太依赖 pycharm的提示,不然后期换了编辑器就不行了 python解释器安装.多版本共存等 去python官网下载安装,配置环境变量.多版本共存等问题请参见 ...