Distances to Zero

2024-01-02 13:08
文章标签 zero distances

本文主要是介绍Distances to Zero,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

B. Distances to Zero
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given the array of integer numbers a0, a1, ..., an - 1. For each element find the distance to the nearest zero (to the element which equals to zero). There is at least one zero element in the given array.

Input

The first line contains integer n (1 ≤ n ≤ 2·105) — length of the array a. The second line contains integer elements of the array separated by single spaces ( - 109 ≤ ai ≤ 109).

Output

Print the sequence d0, d1, ..., dn - 1, where di is the difference of indices between i and nearest j such that aj = 0. It is possible that i = j.

Examples
Input
9
2 1 0 3 0 0 3 2 4
Output
2 1 0 1 0 0 1 2 3 
Input
5
0 1 2 3 4
Output
0 1 2 3 4 
Input
7
5 6 0 1 -2 3 4
Output
2 1 0 1 2 3 4 


给出一个数列,求每个数距离数列中0的最近距离;

思路:记录每个0的位置,然后从前往后遍历一遍求距离0的距离,如果某个数在两个零之间,则取较小值;

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdlib.h>using namespace std;int num[200005];
int zero[200005];
int main(){int n;cin >> n;int k=0;for(int i=1; i<=n; i++){cin >> num[i];if(num[i]==0) zero[++k]=i;}int dis=0;int cnt=k;k=1;zero[0]=0;for(int i=1; i<=n; i++){dis=abs(i-zero[k]);if(dis==0){k++;}if(k>1 && zero[k]!=0 && k<=cnt) dis=min(dis, abs(i-zero[k-1]));   //如果位于两个0之间,取较小值;if(k>cnt) dis=abs(i-zero[cnt]);//cout << "k:" << k << endl;cout << dis;if(i==n) cout << endl;else cout << ' ';}
return 0;
}






这篇关于Distances to Zero的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/562530

相关文章

uva 10061 How many zero's and how many digits ?(不同进制阶乘末尾几个0)+poj 1401

题意是求在base进制下的 n!的结果有几位数,末尾有几个0。 想起刚开始的时候做的一道10进制下的n阶乘末尾有几个零,以及之前有做过的一道n阶乘的位数。 当时都是在10进制下的。 10进制下的做法是: 1. n阶位数:直接 lg(n!)就是得数的位数。 2. n阶末尾0的个数:由于2 * 5 将会在得数中以0的形式存在,所以计算2或者计算5,由于因子中出现5必然出现2,所以直接一

SAM2POINT:以zero-shot且快速的方式将任何 3D 视频分割为视频

摘要 我们介绍 SAM2POINT,这是一种采用 Segment Anything Model 2 (SAM 2) 进行零样本和快速 3D 分割的初步探索。 SAM2POINT 将任何 3D 数据解释为一系列多向视频,并利用 SAM 2 进行 3D 空间分割,无需进一步训练或 2D-3D 投影。 我们的框架支持各种提示类型,包括 3D 点、框和掩模,并且可以泛化到不同的场景,例如 3D 对象、室

《Zero-Shot Object Counting》CVPR2023

摘要 论文提出了一种新的计数设置,称为零样本对象计数(Zero-Shot Object Counting, ZSC),旨在测试时对任意类别的对象实例进行计数,而只需在测试时提供类别名称。现有的类无关计数方法需要人类标注的示例作为输入,这在许多实际应用中是不切实际的。ZSC方法不依赖于人类标注者,可以自动操作。研究者们提出了一种方法,可以从类别名称开始,准确识别出最佳的图像块(patches),用

class _ContiguousArrayStorage deallocated with non-zero retain count

Xcode报错 : Object 0x11c614000 of class _ContiguousArrayStorage deallocated with non-zero retain count 2. This object's deinit, or something called from it, may have created a strong reference to self w

零样本学习(zero-shot learning)——综述

-------本文内容来自对论文A Survey of Zero-Shot Learning: Settings, Methods, and Applications 的理解和整理,这里省去了众多的数学符号,以比较通俗的语言对零样本学习做一个简单的入门介绍,用词上可能缺乏一定的严谨性。一些图和公式直接来自于论文,并且省略了论文中讲的比较细的东西,如果感兴趣建议还是去通读论文 注1:为了方便,文中

【go-zero】win启动rpc服务报错 panic: context deadline exceeded

win启动rpc服务报错 panic: context deadline exceeded 问题来源 在使用go-zero生成的rpc项目后 启动不起来 原因 这个问题原因是wndows没有启动etcd 官方文档是删除了etcd配置 而我自己的测试yaml配置有etcd,所以需要启动etcd 下载安装好etcd后,在etcd的安装目录下,打开cmd,.\etcd 启动 然后

优雅谈大模型:白话ZeRO 下

机器学习模型的复杂性和规模不断增长,分布式训练变得比以往任何时候都更加重要。训练具有数千亿参数的大型语言模型( LLMs )将是机器学习基础设施面临的挑战。与传统的分布式计算框架不同的地方在于GPU的分布式训练需要将数据传递给GPU芯片等物理硬件层。GPU设备之间会进行频繁、大规模的数据交换以进行高效训练,今天将揭开分布式训练的神秘面纱。 上图为基本的机器学习训练框架,数据准备占据1/3

Android studio编译报non-zero exit value 2;错误

解决方法在工程的build.gradle中添加 multiDexEnabled true,如下所示: android {....defaultConfig {.....multiDexEnabled true} 补充:今天发现针对The number of method references in a .dex file cannot exceed 64K.这个错误,上述的方法同样有效 参

Error:Execution failed for task ':app:transformClassesWithDexForMfinished with non-zero exit value 2

一般报这个错误都是包冲突错误但是我的没有 在这个错误提示上面还有一个错误意思大概就是我的dex文件大于64k  然后我在网上搜有人给出了这个回答  在builde.gradle文件中 android{ defaultConfig {         ......         // dex突破65535的限制         multiDexEnabled true//添加这句

optimizer.zero_grad()

optimizer.zero_grad()意思是把梯度置零,也就是把loss关于weight的导数变成0. 在学习pytorch的时候注意到,对于每个batch大都执行了这样的操作: # zero the parameter gradientsoptimizer.zero_grad()# forward + backward + optimizeoutputs = net(in