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. jsoup获取网页属性

    package com.open1111.jsoup; import org.apache.http.HttpEntity;import org.apache.http.client.methods. ...

  2. Python中Numpy mat的使用

    前面介绍过用dnarray来模拟,但mat更符合矩阵,这里的mat与Matlab中的很相似.(mat与matrix等同) 基本操作 >>> m= np.mat([1,2,3]) #创 ...

  3. 2717: 递归函数求n的阶乘

    2717: 递归函数求n的阶乘 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 1329  Solved: 942[Submit][Status][Web ...

  4. 2018.5.11 Java利用反射实现对象克隆

    package com.lanqiao.demo; /** * 创建人 * @author qichunlin * */ public class Person { private int id; p ...

  5. 基于idea创建Tomcat远程调试

    编辑完catalina文件后重启tomcat

  6. 标准C++(1)

    一.引用 引用就是某一变量(目标)的一个别名,对引用的操作与对变量直接操作完全一样. 引用的声明方法:类型标识符 &引用名=目标变量名: 例: int& num; 引用类似于起别名 注 ...

  7. golang http 中间件

    golang http 中间件 源码链接 golang的http中间件的实现 首先实现一个http的handler接口 type Handler interface { ServeHTTP(Respo ...

  8. 20181207(sys,shelve,logging)

    一.logging模块 logging专门用来记录日志 日志的级别分为五级,可以用数字表示,从低到高分别为: import  logginglogging.info('info')   #10logg ...

  9. gdb调试时查看内存

    x/<n/f/u> <addr> n.f.u是可选的参数,<addr>表示一个内存地址 1) n 是一个正整数,表示显示内存的长度,也就是说从当前地址向后显示几个地 ...

  10. hihocoder1174 拓扑排序1

    #1174 : 拓扑排序·一 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 由于今天上课的老师讲的特别无聊,小Hi和小Ho偷偷地聊了起来. 小Ho:小Hi,你这学期有选 ...