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. put_user

    1. put_user Name put_user --    Write a simple value into user space. Synopsis put_user ( x, ptr); A ...

  2. 判断JS数据类型的几种方法

    原文转自http://www.cnblogs.com/onepixel/p/5126046.html! 说到数据类型,我们先说一下JavaScript 中常见的几种数据类型: 基本类型:string, ...

  3. (转)!注意:PreTranslateMessage弹出框出错

    dlg.DoModal()截住了界面消息,所以返回时原来的pMsg的内容已经更改了,消息,窗口句柄都不在是if以前的值了,而且窗口句柄应该是对话框里的子窗口的句柄,所以调用CFrameWnd::Pre ...

  4. Delphi7使用二维码

    参考:http://jingyan.baidu.com/article/e75057f2ad6481ebc81a897b.html 首先下载对应的 dll (已经上传到博客园文件) 然后就是Delph ...

  5. 修改wamp的数据库密码

    方法/步骤     一:修改数据库密码 1.点开MySQL console进入数据库编辑框,然后按回车键,会出现图2的效果. 2.接着输入“use mysql” 下面提示“Database chang ...

  6. js使用my97插件显示当前时间,且select控制计算时间差

    做页面需要两个时间输入框一个显示当前时间,一个显示之前的时间,并且需要一个select下拉框控制两个时间输入框之间的差,效果如下图: 这里使用的是My97DatePicer,简单方便,引入my97插件 ...

  7. UIView Border color

    // // UIView+Borders.h // // Created by Aaron Ng on 12/28/13. // Copyright (c) 2013 Delve. All right ...

  8. UVALive 4794 Sharing Chocolate(状压,枚举子集)

    n的规模可以状压,f[x][y][S]表示x行,y列,S集合的巧克力能否被切割. 预处理出每个状态S对应的面积和sum(S),对于一个合法的状态一定满足x*y=sum(S),实际上只有两个变量是独立的 ...

  9. GIT分布式版本控制器的前后今生

    Git的入门与安装 GIT基础操作 GIT的分支应用 GITLAB应用 gitlab与pycharm应用 GITHUB使用

  10. fork新建进程

    #include <sys/types.h>#include<sys/wait.h>#include<unistd.h>#include<stdio.h> ...