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. Java垃圾回收(GC)机制详解

    一.为什么需要垃圾回收 如果不进行垃圾回收,内存迟早都会被消耗空,因为我们在不断的分配内存空间而不进行回收.除非内存无限大,我们可以任性的分配而不回收,但是事实并非如此.所以,垃圾回收是必须的. 二. ...

  2. 模板发送java邮件

    Creating email content using a templating library The code in the previous examples explicitly creat ...

  3. 【HDFS API编程】第一个应用程序的开发-创建文件夹

    /** * 使用Java API操作HDFS文件系统 * 关键点: * 1)创建 Configuration * 2)获取 FileSystem * 3)...剩下的就是 HDFS API的操作了*/ ...

  4. 使用awk和sed获取文件奇偶数行的方法总结

    测试文件test.file [root@localhost ~]# cat test.file 111111111111111 222222222222222 333333333333333 4444 ...

  5. zepto 事件分析4(事件队列)

    前面分析了zepto的事件绑定,接下来分析事件解绑,先看一下zepto中解绑的off方法: $.fn.off = function(event, selector, callback){ var $t ...

  6. LeetCode-两个结构分别遍历,然后合并

    今天做了leetcode67题,两个2进制数相加,回想了一下其实有很多这种类型的题,比如leetcode2两数相加. 在做这种题时我自己的思路就是先循环遍历一个短的,然后跳出循环,判断是哪个结束,再接 ...

  7. FMDB源码解析

    上一篇博客讲述SQLite的使用,本篇将讲述FMDB源码,后面也会讲解SQLite在使用与FMDB的区别.本篇读下来大约20-30分钟,建议大家先收藏一下. FMDB是以OC方式封装SQLite中C语 ...

  8. ASP.NET MVC 学习笔记-7.自定义配置信息

    ASP.NET程序中的web.config文件中,在appSettings这个配置节中能够保存一些配置,比如, <appSettings> <add key="LogInf ...

  9. c# 判断3个数是否连续最优式子

    Math.Abs((own - two) * (two - there) * + ) ==

  10. .net Core使用Orcle官方驱动连接数据库

    最近在研究.net Core,因为公司的项目用到的都是Oracle数据库,所以简单试一下.net Core怎样连接Oracle. Oracle官方现在已经提供.net Core的官方驱动(预览版),也 ...