您的位置:首页 > 其它

Educational Codeforces Round 20 B

2017-05-01 19:53 357 查看
Description

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<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=654321;
vector<ll>x,y;
ll n;
ll num[300000];
ll a[300000];
long long sum;
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>num[i];
if(num[i])
{
x.push_back(i);
}
else
{
y.push_back(i);
}
}
for(int i=0;i<x.size();i++)
{
int pos=lower_bound(y.begin(),y.end(),x[i])-y.begin();
//cout<<pos<<endl;
if(pos==0) pos++;
if(pos<=y.size()&&pos>=1)
{
a[x[i]]=min(abs(x[i]-y[pos]),abs(x[i]-y[pos-1]));
}
}
for(int i=1;i<=n;i++)
{
cout<<a[i]<<" ";
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: