4 Values whose Sum is 0 poj-2785(四个长度为4000的数组,从每个数组中取出一个数,使取出的四个数和为0 二分优化)

生物信息学避坑指南:Scissor算法参数alpha和cutoff的黄金设置法则 本文深入解析了生物信息学中Scissor算法的关键参数alpha和cutoff的黄金设置法则,帮助研究者有效整合bulk RNA与单细胞数据,识别单细胞亚群。通过实战验证的调参策略,避开常见陷阱,获得可靠的生物学发现,特别适用于肿瘤异质性研究和免疫细胞分型等场景。 阅读详情

The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how many quadruplet (a, b, c, d ) ∈ A x B x C x D are such that a + b + c + d = 0 . In the following, we assume that all lists have the same size n .
Input
The first line of the input file contains the size of the lists n (this value can be as large as 4000). We then have n lines containing four integer values (with absolute value as large as 2 28 ) that belong respectively to A, B, C and D .
Output
For each input file, your program has to write the number quadruplets whose sum is zero.
Sample Input
6
-45 22 42 -16
-41 -27 56 30
-36 53 -37 77
-36 30 -75 -46
26 -38 -10 62
-32 -54 -6 45
Sample Output
5
Hint
Sample Explanation: Indeed, the sum of the five following quadruplets is zero: (-45, -27, 42, 30), (26, 30, -10, -46), (-32, 22, 56, -46),(-32, 30, -75, 77), (-32, -54, 56, 30).

思路: 看到数组长度,三层循环一定超时,你怎么能优化呢, 好好学学吧,在这里用到了二分优化,两个函数,upper_bound(),lower_bound();

先把 c和d数组的所有可能数的和,算出来,放到一个数组中,再把这个数组排序,在枚举a和b数组的和,再在排好序的数组中用二分算法找;

upper_bound(); 返回第一个大于 k(要查找的数字)的数字的位置;

lower_bound(); 返回第一个等于k的数字位置,或返回第一个大于k数字的位置;


代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define Max 4040
#define ll long long 
ll  a[Max],b[Max],c[Max],d[Max];
int n; 
ll  tt[Max*Max];

int main()
{
	int i,j;
	while(~scanf("%d",&n))
	{
		for(i = 0;i<n;i++)
			scanf("%lld%lld%lld%lld",&a[i],&b[i],&c[i],&d[i]);
		int sum = 0;
		for(i = 0;i<n;i++)
			for(j = 0;j<n;j++)
				tt[sum++] = c[i]+d[j];
		sort(tt,tt+sum);
		ll res = 0;
		for(i = 0;i<n;i++)
		{
			for(j = 0;j<n;j++)
			{
				ll k = a[i]+b[j];
				res += upper_bound(tt,tt+sum,-k) - lower_bound(tt,tt+sum,-k);
			}
		}
		printf("%lld\n",res);
	}
	return 0;
} 

史上最全!研究生科研必备科研工具(含AI科研工具),附使用说明和示例 这些工具在科研中的应用非常广泛,可以帮助研究生更高效地完成研究任务。为了更好地利用这些工具,建议深入学习它们的官方文档和教程,或者参加相关的工作坊和培训。 阅读详情

相关推荐

光猫改桥接后,一根网线搞定IPTV和上网的保姆级教程(附VLAN配置避坑点)

本文详细介绍了光猫改桥接后如何通过单线复用技术实现IPTV和上网功能共存。从VLAN配置原理到具体操作步骤,包括光猫超级管理员权限获取、VLAN绑定设置及路由器IPTV功能配置,提供了全面的保姆级教程。特别针对常见问题如IPTV信号丢失、网速下降等给出了实用解决方案,帮助用户轻松避坑。

weixin_29927965的博客 2017

leetcode_454. 4Sum II 4个数组求和,返回和为0的所有情况的种数 数组两两相加存在map中

题目: Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero. To make problem a bit easier, all A, B, C, D ha

qqzj_bupt的博客 1020

近4000张真实城市场景行人数据集:YOLO训练实战指南

行人检测是计算机视觉在智能驾驶与机器人感知中的基础任务,其核心挑战在于模型对真实道路场景的鲁棒性——尤其在小目标、遮挡、光照变化及中国特有交通参与者(如外卖骑手、接娃家长)下的泛化能力。本文围绕轻量化、高覆盖、强落地性的近4000张实采图像,系统阐释如何构建面向YOLO系列模型的高质量训练数据集:从四维场景采样策略(光照-遮挡-尺度-姿态)出发,解析标注规范对检测精度的关键影响,并说明数据增强需服务于真实干扰源(雨痕、动态模糊、对比度衰减),而非盲目堆叠。该数据集专为已掌握YOLO训练流程的算法工程师、嵌入

weixin_32495691的博客 304

经典算法 | 求解数组中的4个数的和为指定数的所有组合

给你一个数,问你数组中的哪四个数的和为指定的target,这四个数不允许出现重复的数。 这一题并不难,但是非常的复杂,首先设定一个i和j,遍历i和j之间的所有数,这个过程并难,但是剪枝比较复杂,并且为了防止出现重复的数,需要使用非常复杂的过滤函数 首先需要对数组进行排序,实际上很多涉及到数组的题目都首先需要对数组进行排序,能够节省很多的时间, 首先是if (nums[i] + nums[i

u012737193的博客 4525

YOLO26改进策略【多YAML融合】| UNetV2 + BiFormer + WIoU

Wise-IoU是一种基于IoU的损失函数,旨在解决目标检测中边界框回归损失函数在处理低质量训练数据时的问题。

Limiiiing的博客 236

基于Solidworks的三维光路结构示意图绘制实例演示-技术细节

基于Solidworks的三维光路结构示意图绘制实例演示-技术细节

简单光学 1万+

POJ 二分查找 快速找到和为零的四个数

问题描述 定义求和问题如下:给定4组整数A,B,C,D,找到有多少四元组(a,b,c,d)∈A×B×C×D,满足条件a+b+c+d=0。此问题中,假设A,B,C,D具有相同的大小n。 输入数据 输入包含多组测试数据。每组测试数据的第一行包含一个整数n,表示A,B,C,D的元素个数(n&lt;=4000)。接下来n行每行4个整数,分别属于A,B,C,D,每个整数的大小在-228~228之间。 输出要...

紫藤&code的博客 1361

YOLO11检测中的内存占用分析与优化:讲解如何分析并优化推理时的内存使用

YOLOv11模型内存分析与优化摘要:本文系统分析了YOLOv11目标检测模型的内存使用模式,重点探讨了显存(VRAM)、系统内存(RAM)和共享内存的特性差异。通过PyTorch内置工具和自定义分析器,详细剖析了骨干网络、颈部网络和检测头各组件内存占用规律,指出激活值内存与输入尺寸呈平方级增长关系。文章提供了卷积层内存计算公式,并强调合理规划"工作台"(显存)与"仓库"(内存)间的数据流动是优化关键。最后通过流程图直观展示了YOLOv11推理过程的三类内存使用模式,

本博客聚焦 YOLOv11 全流程落地,涵盖架构优化、数据集处理、训练技巧与多场景部署,兼及国产数据库、Java 开发与 AI 模型应用,内容兼顾理论与工程实践,为开发者提供系统干货,助力高效提升技术能力。 350

POJ - 2785

4 Values whose Sum is 0 POJ - 2785 The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values,

qq_51343746的博客 557

反应动力学仿真软件:CHEMKIN_(12).CHEMKIN与其他软件的接口

通过上述介绍,我们详细探讨了如何将 CHEMKIN 与 MATLAB、Python、COMSOL、Aspen Plus 和 Ansys Fluent 等软件工具进行接口配置和数据交互。这些接口不仅能够显著提升 CHEMKIN 在反应动力学仿真中的应用效果,还能充分利用其他软件工具的强大功能,实现更复杂的数据处理和分析。希望这些内容对您在化学反应动力学仿真中的工作有所帮助。

kkchenjj的博客 1150

matlab 点云按高程进行赋色【2027最新版】

首先按照Z轴方向求取所在点云的高程极值(包括高程最大值与最小值),然后将高程归一化到0~255范围内,最后采用matlab中的colormap颜色渲染函数进行赋色并保存渲染结果到点云。

点云侠的博客 3349

四个数列:判断使四个数列中取出的数和为0共有多少种方案。c++

Vjudge:利用二分法解决四个数列的问题取数的问题

qq_44654498的博客 589

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

poj 2785 4 Values whose Sum is 0 (二分+枚举)

题目链接:http://poj.org/problem?id=2785 题目大意:  给出四个长度为n的列表(从上到下)                     从每个列表分别取出一个数据,使得相加的结果为0                     问最多存在多少种情况? 解题思路:  n的范围(1                     可以转化为O(2*n^2):

ンZee的解题报告 1157

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

poj 2785 4 Values whose Sum is 0

4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 14474   Accepted: 4137 Case Time Limit: 5000MS Description The SUM problem can

liu_fanyi3的专栏 638

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

[poj2785]4 Values whose Sum is 0(hash或二分)

4 Values whose Sum is 0Time Limit:15000MSMemory Limit:228000KTotal Submissions:19322Accepted:5778Case Time Limit:5000MSDescriptionThe SUM problem can be formulated as follows: given four lis...

237

POJ2785 4 Values whose Sum is 0(不光是查找值!转化为“二分搜索”(附:二分搜索的基本模板))

题目链接:https://cn.vjudge.net/contest/323985#problem/H Sample Input 6 -45 22 42 -16 -41 -27 56 30 -36 53 -37 77 -36 30 -75 -46 26 -38 -10 62 -32 -54 -6 45 Sample Output 5 翻译:先输入一个数t,4个长度为t的数组(不是t个长度为4的...

zaiyang遇见 271

POJ2785-4 Values whose Sum is 0(二分基础)

题目链接 题意: 给定四个长度均为n的数字序列,从这四个序列中各任选一个数字,设分别为a,b,c,d,求a+b+c+d=0有多少种可能的方案。(各个序列间数字的选择是相互独立的) 题目条件: n<=4000,|Xi|<=2^28。(Xi表示选择的数字) 分析: 因为之前在挑战程序设计竞赛书上看过原题,但是解答当时并未全部理解,这次碰到了就好好分析一下。首先看完题目,想到暴力遍历四层的f...

flying_whale1的博客 177

NC107382 POJ2785 4 Values whose Sum is 0

题意 牛客网 给定n行4列的整数,从每一列各选择一个数加起来等于0的组合有多少种? 思路 这题很容易让人想到枚举,要让四个数字的和等于0,从前三列选择了三个数以后第四列要找的数就是确定的,可以不断地枚举找到答案,但这样时间复杂度为O(n4)O(n^4)O(n4),这可不是我们负担的起的。 所以在枚举的基础上可以加入二分,确定了前三列的数字以后,对第四列进行排序,之后再从第四列种二分查找判断有多少个我们要找的数即可。 但即使是这样,我们对四列数据的枚举仍会带来大量重复的计算,所以我们可以对第一、二列的数据两两

羊蝎子的博客 222

POJ 2785 4 Values whose Sum is 0 [二分]

传送门 13773503 njczy2010 2785 Accepted 25248K 7079MS G++ 1423B 2015-01-11 10:26:48 4 Values whose Sum is 0 Time Limit:15000MS Memory Limit:228000K Total Subm...

weixin_33693070的博客 89

4 Values whose Sum is 0 [POJ2785] [折半搜索]

题意 给你长度为n四个数列,每个数列选一个数使总和为4,有多少种选法(不同选法仅当起码有一个元素的下标不同) 输入 第一行,n 下面n行,每行四个数,代表ai,bi,ci,di 输出 选法数量 样例输入 6 -45 22 42 -16 -41 -27 56 30 -36 53 -37 77 -36 30 -75 -46 26 -38 -10 62 -32 -54 -6 ...

weixin_30861797的博客 133

POJ-2785-4 Values whose Sum is 0

4 Values whose Sum is 0 Description The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how many quadruplet (a, b, c, d ) ∈ A x B x C x D are such th...

weixin_30901729的博客 66

四个数组取数为0 (二分策略)

问题描述 ZJM 有四个数列 A,B,C,D,每个数列都有 n 个数字。ZJM 从每个数列中各取出一个数,他想知道有多少种方案使得 4 个数的和为 0。 当一个数列中有多个相同的数字的时候,把它们当做不同的数对待。 请你帮帮他吧! Input 第一行:n(代表数列中数字的个数) (1≤n≤4000) 接下来的 n 行中,第 i 行有四个数字,分别表示数列 A,B,C,D 中的第 i 个数字(数字不...

beautiful_pain的博客 625

POJ 2785 有多少种4个数相加等于0的方案(二分查找 or hash)

文章目录1.二分查找法思路:AC代码2.hash查找法思路: 题目链接:http://poj.org/problem?id=2785 题目大意:给定不超过4000行4个数,每列挑出来1个,使之和为0,有多少种方案。 1.二分查找法 思路: 对左边两列的所有和求出来,右边两列所有的和的求出来再取负 右侧两列的值排序(进行二分查找) 对左边所有的和在右边的和中进行二分查找,并查找前后都满足要求的,计...

Michael是个半路程序员 2224

二分查找

给出N个整数,你来判断一下是否能够选出4个数,他们的和为0,可以则输出"Yes",否则输出"No"。 Input 第1行,1个数N,N为数组的长度(4 第2 - N + 1行:Aii(-10^9 ii <= 10^9) Output 如果可以选出4个数,使得他们的和为0,则输出"Yes",否则输出"No"。 Sample Input 5 -1 1 -5 2 4

xiaoyao_zhang的博客 325
上一篇: lower_bound() 和 upper_bound() 返回的位置区别
下一篇: Ultra-QuickSort POJ - 2299 (数状数组 线段树 归并排序 求逆序数)
obsorb_knowledge
博客等级 码龄9年 38粉丝 213原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址