Educational Codeforces Round 20 B. Distances to Zero
2 seconds
256 megabytes
standard input
standard 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.
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).
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.
9
2 1 0 3 0 0 3 2 4
2 1 0 1 0 0 1 2 3
5
0 1 2 3 4
0 1 2 3 4
7
5 6 0 1 -2 3 4
2 1 0 1 2 3 4
这道题真没什么难的啊,但是昨晚自己竟然没做,一直在想A和C,不过昨晚也就做了半个多小时,甚至还不到?这道题的意思就是找离它最近的0的距离,那就是只用找它两边的0就好了,我写的复杂程度应该是0(k*n),我把那个min函数省了,这样可以有效的节省时间啊,毕竟被这个坑过,就是二分一下,因为我的区间是左闭又开,所以中间值要在和上加1,这个东西调的有点久,其实是变量自己莫名写错,找不到错误。代码很简单,看一下应该都懂。
#include <stdio.h>
#include <algorithm>
using namespace std;
int a[];
int main()
{int n;
scanf("%d",&n);
int f=,c=;
int p;
for(int i=;i<=n;i++){
scanf("%d",&p);
if(!p)
a[f++]=i;
}
int r=a[],l;
for(int i=;i<r;i++){
if(c)printf(" ");c=;
printf("%d",r-i);
}
for(int i=;i<f-;i++){
l=a[i],r=a[i+];
int s=(l+r+)/;
for(int j=l;j<s;j++){
if(c)printf(" ");c=;
printf("%d",j-l);
}
for(int j=s;j<r;j++){
if(c)printf(" ");c=;
printf("%d",r-j);
}}
l=a[f-];
for(int i=l;i<=n;i++){
if(c)printf(" ");c=;
printf("%d",i-l);}
return ;
}
Educational Codeforces Round 20 B. Distances to Zero的更多相关文章
- Educational Codeforces Round 20
Educational Codeforces Round 20 A. Maximal Binary Matrix 直接从上到下从左到右填,注意只剩一个要填的位置的情况 view code //#pr ...
- Educational Codeforces Round 20 D. Magazine Ad
The main city magazine offers its readers an opportunity to publish their ads. The format of the ad ...
- Educational Codeforces Round 20 C(math)
題目鏈接: http://codeforces.com/problemset/problem/803/C 題意: 給出兩個數n, k, 將n拆分成k個數的和,要求這k個數是嚴格遞增的,並且這k個數的g ...
- Educational Codeforces Round 20.C
C. Maximal GCD time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Educational Codeforces Round 20 C 数学/贪心/构造
C. Maximal GCD time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Educational Codeforces Round 20 C. Maximal GCD
C. Maximal GCD time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Educational Codeforces Round 20 A. Maximal Binary Matrix
A. Maximal Binary Matrix time limit per test 1 second memory limit per test 256 megabytes input stan ...
- Educational Codeforces Round 20 E - Roma and Poker(dp)
传送门 题意 Roma在玩一个游戏,一共玩了n局,赢则bourle+1,输则bourle-1,Roma将会在以下情况中退出 1.他赢了k个bourle 2.他输了k个bourle 现在给出一个字符串 ...
- Educational Codeforces Round 20 B
Description You are given the array of integer numbers a0, a1, ..., an - 1. For each element find th ...
随机推荐
- Jenkins上svn更新策略说明
- selenium +python之Page Obiect设计模式
PageObject是selenium自动化测试项目开发实践的最佳设计模式之一,它主要体现对界面交互细节的封装,这样可以使测试案例更关注于业务而非界面细节,从而提高测试案例的可读性. 1.认识Page ...
- Python+selenium之截图图片并保存截取的图片
本文转载:http://blog.csdn.net/u011541946/article/details/70141488 http://www.cnblogs.com/timsheng/archiv ...
- 通过CMD命令行创建和使用Android 模拟器 AVD
进行Android APP测试时,若手持android手机设备稀少的情况下,我们可以通过创建Android模拟器AVD来代替模拟android手机设备,本文就具体介绍如何创建和使用AVD. 1.创建A ...
- linux下杀毒工具clamav
ClamAV 杀毒是Linux平台最受欢迎的杀毒软件,ClamAV属于免费开源产品,支持多种平台,如:Linux/Unix.MAC OS X.Windows.OpenVMS.ClamAV是基于病毒扫描 ...
- javaSe-线程2
package com.java.chap09.sec02; public class Thread3 implements Runnable{ private int baoZi=1; privat ...
- lwz程序人生之启程
本人14年小本科毕业. 第一次接触电脑是小学4年级.当时,是小学公开课,老师让我们去电脑室秀一下.现在,我都记不得当时我第一次看到电脑室好奇呢?还是没感觉呢? 到初中上电脑课的时候,我才真正了解到电脑 ...
- intellij idea自定义快捷键
intellij idea自定义快捷键比较简单,自带. 首先进入idea,File-->Settings-->Live Templates. 点击右上方的“+”,第一次需要创建一个分组. ...
- Bootstrap历练实例:堆叠的进度条
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- Js笔记 14
<script> // <!-- 课 对象 // //对象的创建方法 // 1.var obj = {} plainobject 对象字面量 对象直接量 // 2.构造函数 ...