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 }
分类:
codeforces
, 模拟
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~