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. Python之邮件发送

    Python的smtplib提供了一种很方便的途径用来发送电子邮件,它有SMTP协议进行简单的封装,可以使用SMTP对象的sendmail方法发送邮件,通过help()查看SMTP所提供的方法如下: ...

  2. document.all.item作用

    1.document.all.myCheckBox和 document.all.item通过控件的名字定位控件,item()中是控件的名字例如:<input type="checkbo ...

  3. Java-Web总结03

    *1 dom4j解析器   1)CRUD的含义:CreateReadUpdateDelete增删查改   2)XML解析器有二类,分别是DOM和SAX. a)DOM一次性将整个XML文件读到内存,形成 ...

  4. windows server 2008 R2 的 FTP 防火墙的正确配置方法

    存在问题 FTP搭建完成后,仅本机可以访问,其他机器无法访问. 解决方案 这时,将C:\Windows\System32\svchost.exe添加到例外即可正常访问,如下图所示.将20及21端口添加 ...

  5. HDU 4738 Caocao's Bridges taijan (求割边,神坑)

    神坑题.这题的坑点有1.判断连通,2.有重边,3.至少要有一个人背*** 因为有重边,tarjan的时候不能用子结点和父节点来判断是不是树边的二次访问,所以我的采用用前向星存边编号的奇偶性关系,用^1 ...

  6. Spark源码分析之-Storage模块

    原文链接:http://jerryshao.me/architecture/2013/10/08/spark-storage-module-analysis/ Background 前段时间琐事颇多, ...

  7. sum特殊用法

    在python中,list可以存储False和True a = [False] python的sum除了可以加数字,还可以计算列表中False,True的个数,默认是计算False个数 >> ...

  8. CPP-网络/通信:SSL功能和原理

    面对诸多的网上交易的风险,很多网上银行.网上商城在建立系统时都注意到了安全问题.而我们也经常可以看到很多网上银行.电子商务网站在强调本系统的安全性的时候,都说采用了基于数字证书的SSL加密传输.SSL ...

  9. SC || Git 相关知识

    发现大佬的博客:https://www.cnblogs.com/onetwo/p/4157610.html 注:图片来自小姐姐的笔记~ ┉┉┉∞ ∞┉┉┉┉∞ ∞┉┉┉∞ ∞┉┉┉┉┉∞ ∞┉┉┉┉∞ ...

  10. 01_2_Servlet简介

    01_2_Servlet简介 1. Servlet简介 Servlet是服务器小应用程序 用来完成B/S架构下,客户端请求的响应处理 平台独立,性能优良,能以线程方式运行 Servlet API为Se ...