zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    vue-d2admin-axios异步请求登录,先对比一下Jquery ajax, Axios, Fetch区别
    beego 前后端分离登录验证
    Beego没gin配置静态页面方便
    beego-vue URL重定向(beego和vue前后端分离开发,beego承载vue前端分离页面部署)
    Go程序员面试算法宝典-读后感2-链表
    Go程序员面试算法宝典-读后感1
    bee api new
    golang-结构体的使用
    golang-笔记2
    golang-笔记1
  • 原文地址:https://www.cnblogs.com/yinghualuowu/p/6792605.html
Copyright © 2011-2022 走看看