【CodeForces 803B】Distances to Zero(模拟)
来源:互联网 发布:m1213nf mfp mac扫描 编辑:程序博客网 时间:2024/05/17 23:15
B. Distances to Zero
time limit per test
2 secondsmemory limit per test
256 megabytesinput
standard inputoutput
standard outputYou 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 thati = j.
Examples
input
92 1 0 3 0 0 3 2 4
output
2 1 0 1 0 0 1 2 3
input
50 1 2 3 4
output
0 1 2 3 4
input
75 6 0 1 -2 3 4
output
2 1 0 1 2 3 4
题目大意:n个元素,求每个数到离它最近的0的距离
思路:模拟瞎暴力,记录相邻两个0的位置,依次做比较
#include <bits/stdc++.h>#define manx 200005#define INF 0x3f3f3f3fusing namespace std;int main(){ int a[manx],ans[manx],n; while(~scanf("%d",&n)){ memset(a,0,sizeof(a)); memset(ans,0,sizeof(ans)); int k[2],t=0; //k记录相邻两个0的位置 k[0]=k[1]=INF; for (int i=0; i<n; i++) scanf("%d",&a[i]); for (int i=0; i<n+1; i++){ if (!a[i]){ ans[i]=0; if (i!=n) k[t%2]=i; int f; if (k[(t+1)%2]==INF) f=0; else f=k[(t+1)%2]; for (int j=f; j<i; j++) ans[j]=min(abs(k[0]-j),abs(k[1]-j)); t++; } } for (int i=0; i<n; i++) printf("%d ",ans[i]); printf("\n"); } return 0;}
0 0
- Codeforces 803 B. Distances to Zero (模拟
- Codeforces 803 B. Distances to Zero 模拟
- 【CodeForces 803B】Distances to Zero(模拟)
- Codeforces 803B Distances to Zero 题解
- CodeForces 803B Distances to Zero
- 【Codeforces 803 B. Distances to Zero】
- 803B Distances to Zero
- CF#803 B. Distances to Zero(水题)
- Educational Codeforces Round 20 Distances to Zero
- Codeforces803B Distances to Zero
- Distances to Zero
- Distances to Zero
- Distances to Zero
- Distances to Zero 【二分枚举】
- Distances to Zero(lower_bound和upper_bound应用)
- CODEFORCES 274B Zero Tree
- CodeForces 274B Zero Tree
- CodeForces 754B(模拟)
- 易聚之反馈与修正
- java的三种循环
- 易聚之场景化营销
- Java学习4 运算符、表达式、流程控制
- java 多线程基础之二:线程的中断(interrupt)
- 【CodeForces 803B】Distances to Zero(模拟)
- 2017年智能语音应用专题分析
- 【qscoj】哗啦啦村的刁难(4)(公式)
- 易聚之积木理论
- java.lang.Void 与 void的比较及使用
- 我的2017劳动节
- Spring框架学习实战Demo1笔记
- int& test(int ** p) 引用返回和指针取地址 获得局部变量的问题
- Spring+SpringMVC+Mybatis配置(Maven项目)