首页 > 代码库 > Educational Codeforces Round 20 B

Educational Codeforces Round 20 B

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,取最近的
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 const int maxn=654321;
 5 vector<ll>x,y;
 6 ll n;
 7 ll num[300000];
 8 ll a[300000];
 9 long long sum;
10 int main()
11 {
12     cin>>n;
13     for(int i=1;i<=n;i++)
14     {
15         cin>>num[i];
16         if(num[i])
17         {
18             x.push_back(i);
19         }
20         else
21         {
22             y.push_back(i);
23         }
24     }
25     for(int i=0;i<x.size();i++)
26     {
27         int pos=lower_bound(y.begin(),y.end(),x[i])-y.begin();
28         //cout<<pos<<endl;
29         if(pos==0) pos++;
30         if(pos<=y.size()&&pos>=1)
31         {
32             a[x[i]]=min(abs(x[i]-y[pos]),abs(x[i]-y[pos-1]));
33         }
34     }
35     for(int i=1;i<=n;i++)
36     {
37         cout<<a[i]<<" ";
38     }
39     return 0;
40 }

 

Educational Codeforces Round 20 B