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<=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手撕快排+二分)的更多相关文章
- 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—[哈希表实现]
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
题意:给出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, 把所有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 Zero(算法效率--中途相遇法+Hash或STL库)
题意:给定4个N元素几个A,B,C,D,要求分别从中选取一个元素a,b,c,d使得a+b+c+d=0.问有多少种选法.(N≤4000,D≤2^28) 解法:首先我们从最直接最暴力的方法开始思考:四重循 ...
- K - 4 Values whose Sum is 0(中途相遇法)
K - 4 Values whose Sum is 0 Crawling in process... Crawling failed Time Limit:9000MS Memory Limi ...
随机推荐
- Excel 斜线表头制作方法
Excel 斜线表头制作方法
- 比较 Spring AOP 与 AspectJ
本文翻译自博客Comparing Spring AOP and AspectJ(转载:https://juejin.im/post/5a695b3cf265da3e47449471) 介绍 如今有多个 ...
- C++版 - Leetcode 69. Sqrt(x) 解题报告【C库函数sqrt(x)模拟-求平方根】
69. Sqrt(x) Total Accepted: 93296 Total Submissions: 368340 Difficulty: Medium 提交网址: https://leetcod ...
- Java代码更改shape和selector文件的颜色值
Android里面经常会使用shape或者selector来定制一些View的背景.那么如果想要动态更改shape或者seletor文件中的颜色值,该怎么处理呢? 一.Java代码更改shape的颜色 ...
- AspNetPager分页控件的使用方法
1. 首先将AspNetPager.dll复制于应用程序下的bin目录,打开解决方案,引入dll文件 (通过NuGet获取) 2. 在工具栏中添加控件,这样可以支持拖拽使用 3.页面拖入分页控件,设置 ...
- 非业务 Oracle SQL 语句备份
1.创建一个将 Oracle 生成的 GUID 格式化为标准的 GUID 的函数 2.在 PLSQL 中测试并输出语句异常的语句块 3.在查询语句中日期的一种特殊表示方法 4.利用 ROWNUM 做分 ...
- Java集合:整体结构
一.Java中集合 Java中集合类是Java编程中使用最频繁.最方便的类.集合类作为容器类可以存储任何类型的数据,当然也可以结合泛型存储指定的类型(不过泛型仅仅在编译期有效,运行时是会被擦除的).集 ...
- Tomcat8源码笔记(七)组件启动Server Service Engine Host启动
一.Tomcat启动的入口 Tomcat初始化简单流程前面博客介绍了一遍,组件除了StandardHost都有博客,欢迎大家指文中错误.Tomcat启动类是Bootstrap,而启动容器启动入口位于 ...
- Python系列:三、流程控制循环语句--技术流ken
Python条件语句 Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 可以通过下图来简单了解条件语句的执行过程: Python程序语言指定任何非0和非 ...
- 前端(五)之display 总结与浮动
前端之浮动布局.清浮动 display 总结 <!DOCTYPE html> <html> <head> <meta charset="UTF-8& ...