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 现在给出一个字符串 ...
随机推荐
- mysql order by的一些技巧
1. 只按日期排序,忽略年份> select date, description from table_name order by month(date),dayofmonth(date);注意 ...
- 利用PHP判断iPhone、iPad、Android、PC设备
首页那张大图确实是一个比较头疼的问题 在PC上显示是没问题的,可是到手机上就会超出页面一大截,如果做自适应,图片会被强制压缩 无奈只能用wp_is_mobile()函数在手机上隐藏了这张图,可是这函数 ...
- 深入浅出 - Android系统移植与平台开发(九)- Android系统system_server及Home启动
3.3 Zygote守护进程与system_server进程 Android的执行环境和Java执行环境有着本质的差别,在Android系统中每一个应用程序都是一独立的进程,当一个进程死掉时,不会影响 ...
- LeetCode(26)题解:Remove Duplicates from Sorted Array
https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove the ...
- iptables apache2
Apache2 iptables 安装指令:sudo apt-get install apache2 2.产生的启动和停止文件是:/etc/init.d/apache2 3.启动:sudo apach ...
- BZOJ2327: [HNOI2011]勾股定理
BZOJ2327: [HNOI2011]勾股定理 Description 题解Here! 这是一道神题... 我一开始把题目看错了,我以为是在$n$根木棒中选两个$i,j$满足$gcd(i,j)==1 ...
- strong and weak 强引用和弱引用的差别
(weak和strong)不同的是 当一个对象不再有strong类型的指针指向它的时候 它会被释放 ,即使还有weak型指针指向它. 一旦最后一个strong型指针离去 .这个对象将被释放,全部剩余 ...
- oracle问题系列 : ORA-02290: 违反检查约束条件
报错如下: ### Error updating database. Cause: java.sql.SQLIntegrityConstraintViolationException: ORA-022 ...
- POJ3278 Catch That Cow —— BFS
题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total S ...
- VC++编译说明
目录 第1章编译步骤 1 第2章编译源文件 2 2.1 编译器 2 2.2 包含头文件 3 2.3 重复包含 6 2.4 预编译头文件 7 2.4.1 创建 ...