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.
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
题意:求距离0最近的距离
解法:找距离最近的0,可能是左边或者右边的0,取最近的
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=;
vector<ll>x,y;
ll n;
ll num[];
ll a[];
long long sum;
int main()
{
cin>>n;
for(int i=;i<=n;i++)
{
cin>>num[i];
if(num[i])
{
x.push_back(i);
}
else
{
y.push_back(i);
}
}
for(int i=;i<x.size();i++)
{
int pos=lower_bound(y.begin(),y.end(),x[i])-y.begin();
//cout<<pos<<endl;
if(pos==) pos++;
if(pos<=y.size()&&pos>=)
{
a[x[i]]=min(abs(x[i]-y[pos]),abs(x[i]-y[pos-]));
}
}
for(int i=;i<=n;i++)
{
cout<<a[i]<<" ";
}
return ;
}
Educational Codeforces Round 20 B的更多相关文章
- 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 B. Distances to Zero
B. Distances to Zero time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- 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 现在给出一个字符串 ...
随机推荐
- openwrt: patch-dtb
dts的概念是linux kernel中的,跟openwrt的关系不大.只是恰好在学习openwrt的时候碰到了这个东西,所以记录在openwrt名下. patch-dtb openwrt对arch/ ...
- C++类使用static小例子(新手学习C++)
//为什么类的成员中不能包括动态分配的数据,若包含静态数据怎么使用?#include <iostream>using namespace std;class point{ priva ...
- VC++ 学习笔记(四):停止还是暂停这个系列
我已经很久没有更新这个话题了,原因是多方面的,比如比较忙,比如我参与的项目不使用C++.最近因为需要在C#的客户端中调用第三方的C++API,又想起了这个话题.在跟公司里的C++方面专家聊过之后,我有 ...
- WPF绑定各种数据源之object数据源
一.WPF绑定各种数据源索引 WPF 绑定各种数据源之Datatable WPF绑定各种数据源之object数据源 WPF绑定各种数据源之xml数据源 WPF绑定各种数据源之元素控件属性 Bindin ...
- npm ERR! code EINTEGRITY npm ERR! sha1- 报错解决办法
npm ERR! code EINTEGRITY npm ERR! sha1- 报错日志 npm ERR! code EINTEGRITY npm ERR! sha1-OGchPo3Xm/Ho8jAM ...
- 【直播预告】7月25日3D游戏引擎免费公开课答疑第三期,有奖问答!
喜讯喜讯! 为了酬谢广大学员.CSDN学院特推出iOS和3D游戏引擎开发免费技术答疑公开课.让您度过一个充实的暑假~ 參与本次公开课,进行有奖问答.即有机会获奖. 答疑公开课时间:7月25日 晚7:3 ...
- SKStoreReviewController之程序内评价
在iOS 10.3出现之前,App实现评价方式一般有两种: (一)deep link调用.在app 链接地址后面拼上action=write-review这种方式可以实现程序内评价: (二)App跳转 ...
- jquery.validate ajax验证
$("#form1").validate({ rules: { ctl00$ContentPlaceHolder1$txtShopName: { remote: { type: & ...
- VC FTP服务器程序分析(一)
想在QT上移植一个FTP服务器程序,先学习windows下的FTP服务器例子,然后随便动手写点东西. 在pudn上搜索 "FTP服务器端和客户端实现 VC“这几个关键字,就可以搜到下面要分析 ...
- mybatis中GenericTokenParser的用法
代码示例 import org.apache.ibatis.parsing.GenericTokenParser;import org.apache.ibatis.parsing.TokenHandl ...