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 

这道题真没什么难的啊,但是昨晚自己竟然没做,一直在想A和C,不过昨晚也就做了半个多小时,甚至还不到?这道题的意思就是找离它最近的0的距离,那就是只用找它两边的0就好了,我写的复杂程度应该是0(k*n),我把那个min函数省了,这样可以有效的节省时间啊,毕竟被这个坑过,就是二分一下,因为我的区间是左闭又开,所以中间值要在和上加1,这个东西调的有点久,其实是变量自己莫名写错,找不到错误。代码很简单,看一下应该都懂。

#include <stdio.h>
#include <algorithm>
using namespace std;
int a[];
int main()
{int n;
scanf("%d",&n);
int f=,c=;
int p;
for(int i=;i<=n;i++){
scanf("%d",&p);
if(!p)
a[f++]=i;
}
int r=a[],l;
for(int i=;i<r;i++){
if(c)printf(" ");c=;
printf("%d",r-i);
}
for(int i=;i<f-;i++){
l=a[i],r=a[i+];
int s=(l+r+)/;
for(int j=l;j<s;j++){
if(c)printf(" ");c=;
printf("%d",j-l);
}
for(int j=s;j<r;j++){
if(c)printf(" ");c=;
printf("%d",r-j);
}}
l=a[f-];
for(int i=l;i<=n;i++){
if(c)printf(" ");c=;
printf("%d",i-l);}
return ;
}

Educational Codeforces Round 20 B. Distances to Zero的更多相关文章

  1. Educational Codeforces Round 20

    Educational Codeforces Round 20  A. Maximal Binary Matrix 直接从上到下从左到右填,注意只剩一个要填的位置的情况 view code //#pr ...

  2. Educational Codeforces Round 20 D. Magazine Ad

    The main city magazine offers its readers an opportunity to publish their ads. The format of the ad ...

  3. Educational Codeforces Round 20 C(math)

    題目鏈接: http://codeforces.com/problemset/problem/803/C 題意: 給出兩個數n, k, 將n拆分成k個數的和,要求這k個數是嚴格遞增的,並且這k個數的g ...

  4. Educational Codeforces Round 20.C

    C. Maximal GCD time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  5. Educational Codeforces Round 20 C 数学/贪心/构造

    C. Maximal GCD time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  6. Educational Codeforces Round 20 C. Maximal GCD

    C. Maximal GCD time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  7. Educational Codeforces Round 20 A. Maximal Binary Matrix

    A. Maximal Binary Matrix time limit per test 1 second memory limit per test 256 megabytes input stan ...

  8. Educational Codeforces Round 20 E - Roma and Poker(dp)

    传送门 题意 Roma在玩一个游戏,一共玩了n局,赢则bourle+1,输则bourle-1,Roma将会在以下情况中退出 1.他赢了k个bourle 2.他输了k个bourle 现在给出一个字符串 ...

  9. Educational Codeforces Round 20 B

    Description You are given the array of integer numbers a0, a1, ..., an - 1. For each element find th ...

随机推荐

  1. 【exFat】利用命令提示符在windows 7 及 windows server 2008 r2 中将卷(分区)格式化为exFAT

    步骤 运行cmd.exe: 查看磁盘信息.输入diskpart并回车: 选择磁盘.输入select disk 0(“0”代表要选择的磁盘号)并回车: 查看所选硬盘的分区.输入list partitio ...

  2. Android(java)学习笔记111:Java异常分类

    Java异常可分为3种: (1)编译时异常:Java.lang.Exception (2)运行期异常:Java.lang.RuntimeException (3)错误:Java.lang.Error

  3. Android(java)学习笔记135:SQLite数据库(表)的创建 以及 SQLite数据库的升级

    一.数据库的创建 1.文件的创建      //引用,如果文件不存在是不会创建的   File  file = new File("haha.txt"):     //输出流写数据 ...

  4. caffe parse_log.sh

    画loss曲线需要用到此shell脚本 #!/bin/bash # Usage parse_log.sh caffe.log # It creates the following two text f ...

  5. Java的引用StrongReference、 SoftReference、 WeakReference 、PhantomReference

    1. Strong Reference StrongReference 是 Java 的默认引用实现,  它会尽可能长时间的存活于 JVM 内, 当没有任何对象指向它时 GC 执行后将会被回收 @Te ...

  6. 《毛毛虫组》【Alpha】Scrum meeting 3

    第二天 日期:2019/6/16 1.1 今日完成任务情况以及遇到的问题. 今日完成任务情况: 货物入库管理模块设计: (1)对数据库表--tb_InStore进行修改并完善: (2)学习SQL Se ...

  7. sql 经典加强巩固练习题

    由于本人需要加强巩固一下数据库知识,就搜罗了一些题目来练习,感觉不错,故分享一下资源难度层度依次上升这50道里面自认为应该没有太多错误,而且尽可能使用了最简单或是最直接的查询,有多种不相上下解法的题目 ...

  8. sphinx关键字套红

    sphinx定义搜索结果,搜索的内容着重显示,可以使用下面代码 <?php /** * Created by PhpStorm. * User: pc00001 * Date: 2015/4/1 ...

  9. 201621123080《Java程序设计》第三周学习总结

    Week03-面向对象入门 1. 本周学习总结 2. 书面作业 1.以面向对象方式改造数据结构作业'有理数'(重点) 1.1 截图你主要代码(需要在程序中出现你的学号和姓名)并粘贴程序的git地址. ...

  10. url,href,src区别

    URL(Uniform Resource Locator) 统一资源定位符是对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示,是互联网上标准资源的地址.互联网上的每个文件都有一个唯一的URL ...