4 Values whose Sum is 0

题目链接:https://cn.vjudge.net/problem/UVA-1152

    ——每天在线,欢迎留言谈论。

题目大意:

  给定4个n(1<=n<=4000)元素的集合 A、B、C、D ,从4个集合中分别选取一个元素a, b,c,d。求满足 a+b+c+d=0的对数。

思路:

  直接分别枚举 a,b,c,d ,坑定炸了。我们先枚举 a+b并储存,在B、C中枚举找出(-c-d)后进行比较即可。

亮点:

  由于a+b,中会有值相等的不同组合,如果使用map来存,很遗憾超时(虽然时间限制是9000ms)。

  在一个有序数组求某元素数出现的个数:n = upper_bound(a,a+n,k)-lower_bound(a,a+n,k) ;

C++ AC代码:

 #include <iostream>
#include <cmath>
#include <iostream>
#include <string>
#include <string.h>
#include <cstdio>
#include <algorithm>
#include <map>
using namespace std;
const int INT_INF = 0x3f3f3f3f;
const double EPS = 1e-;
typedef long long ll;
const int MAXN = 4e3+;
int numbers[][MAXN],sumAB[MAXN*MAXN];
int main()
{
int t;
cin>>t;
while(t--)
{
memset(numbers,,sizeof(numbers));
memset(sumAB,INT_INF,sizeof(sumAB));
int n,p1=,p2=;
cin>>n;
for(int i=;i<n;i++)
for(int j=;j<;j++)
cin>>numbers[j][i];
for(int i=;i<n;i++)
for(int j=;j<n;j++)
sumAB[p1++]=numbers[][i]+numbers[][j];
sort(sumAB,sumAB+p1);
int answer=;
for(int i=;i<n;i++)
for(int j=;j<n;j++)
{
answer+=upper_bound(sumAB,sumAB+p1,-numbers[][i]-numbers[][j])-lower_bound(sumAB,sumAB+p1,-numbers[][i]-numbers[][j]);
}
cout<<answer<<endl;
if(t)
cout<<endl;
}
return ;
}

Java AC代码:

 import java.util.Scanner;
public class Main {
static Scanner scn = new Scanner(System.in); static final int MAXN = 4100;
static int[][] numbers = new int[4][MAXN];
static int[] sumAB = new int[MAXN*MAXN];
public static void main(String[] args) {
int t;
t = scn.nextInt();
while ((t--) > 0) {
int n;
n = scn.nextInt();
for (int i = 0; i < n; i++) {
for (int j = 0; j < 4; j++) {
numbers[j][i] = scn.nextInt();
}
}
int p1 = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
sumAB[p1++] = numbers[0][i] + numbers[1][j];
}
}
Tool.quickSort(sumAB,0,p1-1);
//输出看看
// for (int i = 0; i < p1; i++) {
// System.out.print(sumAB[i] + " ");
// } //成功
//开始枚举-c-d
int answer = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
answer += Tool.uppper(sumAB, 0, p1, -numbers[2][i] - numbers[3][j]) - Tool.lower(sumAB, 0, p1, -numbers[2][i] - numbers[3][j]);
}
}
System.out.println(answer);
if(t > 0)
System.out.println();
}
System.exit(0);
}
}
class Tool {
public static int lower(int[] array, int low, int high, int number) {
//[low,high)
int i = low, j = high, m;
while (i < j) {
m = i + (j - i) / 2;
if (array[m] >= number)
j = m;
else
i = m+1;
}
return i;
}
public static int uppper(int[] array, int low, int high, int number) {
int i = low, j = high, m;
while (i < j) {
m = i + (j - i) / 2;
if (array[m] <= number)
i = m+1;
else
j = m;
}
return i;
}
public static void quickSort(int[] array, int low, int high) {
if (low < high) {
int m = partition(array, low, high);
quickSort(array, low, m-1);
quickSort(array, m+1, high);
}
}
private static int partition(int[] array, int low, int high) {
int mNumber = array[low];
int i = low, j = high;
while (i < j) {
while (i < j && array[j] >= mNumber) {--j;}
array[i] = array[j];
while (i < j && array[i] <= mNumber) {++i;}
array[j] = array[i];
}
array[i] = mNumber;
return i;
}
}

2017-07-30 11:06:43 -> 2017-07-30 13:45:32

UVA 1152 4 Values whose Sum is 0 (枚举+中途相遇法)(+Java版)(Java手撕快排+二分)的更多相关文章

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

    题意:从四个集合各选一个数,使和等于0,问有多少种选法. 分析:求出来所有ai + bi,在里面找所有等于ci + di的个数. #pragma comment(linker, "/STAC ...

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

  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(二分)

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

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

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

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

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

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

  8. 【uva 1152】4 Values Whose Sum is Zero(算法效率--中途相遇法+Hash或STL库)

    题意:给定4个N元素几个A,B,C,D,要求分别从中选取一个元素a,b,c,d使得a+b+c+d=0.问有多少种选法.(N≤4000,D≤2^28) 解法:首先我们从最直接最暴力的方法开始思考:四重循 ...

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

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

随机推荐

  1. 详解网络编程必会的poll和epoll函数

    前言 之前已经介绍过select函数,请参考这篇博客:https://www.cnblogs.com/liudw-0215/p/9661583.html,原理都是类似的,有时间先阅读下那篇博客,以便于 ...

  2. 【EF6学习笔记】(四)弹性连接及命令拦截调试

    本章原文地址:Connection Resiliency and Command Interception 原文有些地方讲的比较细,个人根据实际理解做些缩减,或者加入一些个人理解: 第1部分 弹性连接 ...

  3. java正则表达式的忽略大小写

    (?i)abc 表示abc都忽略大小写  a(?i)bc 表示bc忽略大小写  a((?i)b)c 表示只有b忽略大小写

  4. SpringCloud学习5-如何创建一个服务提供者provider

    前几篇主要集中在注册中心eureka的使用上,接下来可以创建服务提供者provider来注册到eureka. demo源码见: https://github.com/Ryan-Miao/spring- ...

  5. linux 命令 — xargs

    xargs xargs能接收stdin并将其转化为特定命令的命令行参数,构建单行命令的重要工具 command | xargs 指定分隔符 echo "splitXsplitXsplitXs ...

  6. MyBatis源码解析(六)——DataSource数据源模块之池型数据源

    原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/6675674.html 1 回顾 上一文中解读了MyBatis中非池型数据源的源码,非池型也 ...

  7. SpringCloud(1) 架构演进和基础知识简介

    一.传统架构演进到分布式架构 简介:讲解单机应用和分布式应用架构演进基础知识 (画图) 高可用 LVS+keepalive 1.单体应用:开发速度慢.启动时间长.依赖庞大.等等 2.微服务:易开发.理 ...

  8. MySQL中间件之ProxySQL(15):ProxySQL代理MySQL组复制

    返回ProxySQL系列文章:http://www.cnblogs.com/f-ck-need-u/p/7586194.html 1.ProxySQL+组复制前言 在以前的ProxySQL版本中,要支 ...

  9. [JavaScript] 前端模块编程实现

    前端模块化 前端早期写代码都是全局变量满天飞,这种情况会造成全局命名空间污染,变量冲突等问题 var a = 1; var b = 2; function c(){} function d(){} 后 ...

  10. zepto中的属性设置

    上次看zepto的init方法时,有一段属性设置的代码,先来看看其表现: if (isPlainObject(properties)) { nodes = $(dom) $.each(properti ...