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
output
input
output
input
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 题意:求距离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; }
相关文章推荐
- Educational Codeforces Round 20 F. Coprime Subsequences(莫比乌斯反演)
- Educational Codeforces Round 20
- Educational Codeforces Round 20 C(math)
- Educational Codeforces Round 20总结
- Educational Codeforces Round 20 C 数学/贪心/构造
- codefoces Educational Codeforces Round 20
- Educational Codeforces Round 20 G. Periodic RMQ Problem(线段树动态开点)
- Educational Codeforces Round 20 A
- Educational Codeforces Round 20-C. Maximal GCD
- cf Educational Codeforces Round 20 E. Roma and Poker
- Educational Codeforces Round 20 C
- Educational Codeforces Round 20 G. Periodic RMQ Problem(线段树+主席树)
- Educational Codeforces Round 20 C 数学
- Educational Codeforces Round 20 A - Maximal Binary Matrix
- Educational Codeforces Round 20 B - Distances to Zero
- Educational Codeforces Round 20 F. Coprime Subsequences(容斥)
- Educational Codeforces Round 20 Distances to Zero
- 【解题报告】Educational Codeforces Round 20
- [Updating] Educational Codeforces Round 20 解题报告
- CF Educational Codeforces Round 20 D. Magazine Ad