题目链接:https://vjudge.net/problem/CodeForces-803B#author=0

题意:

给你一个数组,其中至少包括一个0,求每一个元素距离最近一个0的距离是多少。

样例:

Input
9
2 1 0 3 0 0 3 2 4
Output
2 1 0 1 0 0 1 2 3 

思路:把每一个0的下标都放进一个新数组中,然后枚举每一个数组pos,用二分查找pos在新数组中刚好大于等于pos的那个index,
然后再和前后的index比较下取最小值,然后即得出答案。 作者的博客原链接:https://www.cnblogs.com/qieqiemin/
我的AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb std::ios::sync_with_stdio(false)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define gg(x) getInt(&x)
using namespace std;
typedef long long ll;
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n;
int ans[maxn];
int a[maxn];
int cnt=;
int l[maxn];
int main()
{
gg(n);
repd(i,,n)
{
gg(a[i]);
}
repd(i,,n)
{
if(a[i]==)
{
l[cnt++]=i;
}
}
repd(i,,n)
{
if(a[i]!=)
{
int j1=lower_bound(l,l+cnt,i)-l;
// int j2=uppercase()
int v=l[j1];
if(v==)
{
ans[i]=i-l[cnt-];
}else
{
int num=abs(v-i);
if(j1>=cnt-)
{ }else
{
num=min(num,abs(l[j1+]-i)); }
if(j1>)
{
num=min(num,abs(l[j1-]-i));
}
ans[i]=num;
}
}
} repd(i,,n)
{
printf("%d ",ans[i]);
}
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}

Distances to Zero CodeForces - 803B (二分)的更多相关文章

  1. Ac日记——Distances to Zero codeforces 803b

    803B - Distances to Zero 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <i ...

  2. codeforces 803B Distances to Zero

    Distances to Zero 题目链接:http://codeforces.com/problemset/problem/803/B 题目大意: 给一串数字,求每个数字到离他最近数字0的距离.. ...

  3. CodeForces - 363D --二分和贪心

    题目:CodeForces - 363D 题意:给定n个学生,其中每个学生都有各自的私己钱,并且自己的私己钱只能用在自己买自行车,不能给别人. 给定m个自行车,每个自行车都有一个价格. 给定公有财产a ...

  4. Codeforces Round #425 (Div. 2) Problem C Strange Radiation (Codeforces 832C) - 二分答案 - 数论

    n people are standing on a coordinate axis in points with positive integer coordinates strictly less ...

  5. Codeforces 732D [二分 ][贪心]

    /* 不要低头,不要放弃,不要气馁,不要慌张 题意: n天进行m科考试,每科考试需要a的复习时间,n天每天最多可以考一科.并且指定哪天考哪科. 注意考试那天不能复习. 问最少需要多少天可全部通过考试. ...

  6. codeforces 359D 二分答案+RMQ

    上学期刷过裸的RMQ模板题,不过那时候一直不理解>_< 其实RMQ很简单: 设f[i][j]表示从i开始的,长度为2^j的一段元素中的最小值or最大值 那么f[i][j]=min/max{ ...

  7. CodeForces - 551C 二分+贪心

    题意:有n个箱子形成的堆,现在有m个学生,每个学生每一秒可以有两种操作: 1: 向右移动一格 2: 移除当前位置的一个箱子 求移除所有箱子需要的最短时间.注意:所有学生可以同时行动. 思路:二分时间, ...

  8. Robin Hood CodeForces - 672D (二分)

    大意: 给定数组$a$, 每次操作使最大元素减小1最小元素增大1, 求k次操作后最大值与最小值的差. 二分出k次操作后最大值的最小值以及最小值的最大值, 若和能平分答案即为$max(0,R-L)$, ...

  9. Queries about less or equal elements CodeForces - 600B(二分)

    You are given two arrays of integers a and b. For each element of the second arraybj you should find ...

随机推荐

  1. ping 127.0.0.1和ping本地ip分别测试什么?

    ping 127.0.0.1 是你本地的回环地址! 实际上只要是127.0.0.1到127.255.255.255都是回环地址!都是可以PING检查的! 它能ping通,说明你的TCP/IP协议栈没问 ...

  2. C#从入门到精通视频教程(2009年最新)- 视频列表

    http://www.jz97.net/index.php/playlist/view/182/page/2 C#从入门到精通视频教程(2009年最新)- 视频列表

  3. go标准库的学习-io/ioutil

    参考https://studygolang.com/pkgdoc 导入方式: import "io/ioutil" 包ioutil实现了一些I/O实用程序函数. 1.var 变量 ...

  4. rac添加新节点的步骤与方法2

    上一篇文章,把节点删除了.这次新增加一个节点 .新增加的节点是host03.如下: #Public IP192.168.16.45 racdb1192.168.16.46 racdb2192.168. ...

  5. MongoDB数据库中更新与删除数据

    MongoDB数据库中更新与删除数据 在MongoDB数据库中,可以使用Collection对象的update方法更新集合中的数据文档.使用方法如下所示: collection.update(sele ...

  6. Android学习之基础知识一

    一.Android的系统架构: 1.Linux内核层:提供Android硬件的各种驱动(显示驱动,音频驱动,蓝牙驱动,WiFi驱动等等) 2.系统运行库层:提供各种特性支持(数据库支持,绘图支持,浏览 ...

  7. linux下文件共享的几种常用方式

    1. python方式,做一个简单的服务器.默认是开启8000端口. > python -m SimpleHTTPServer 执行命令后,在浏览器上输入该机器IP+8000端口即可 2. sc ...

  8. Luogu3877 TJOI2010 打扫房间 二分图、网络流

    传送门 真是菜死了模板题都不会-- 首先\(30 \times 30\)并不能插头DP,但是范围仍然很小所以考虑网络流. 注意每个点都要包含在一个回路中,那么每一个点的度数都必须为\(2\),也就是说 ...

  9. C# 定时器和队列结合,卖包子啦,Timer、 AutoResetEvent、 ManualResetEvent

    再你们得到源码之前,我先做个广告:张家港杰德机械/张家港三兴华轩机械是我一朋友的公司,希望需要做净水,灌装机,拔盖机,封口机,传送带等的朋友光顾. 张家港杰德机械有限公司:http://www.jie ...

  10. postgresql总结

    这篇博客主要对PostgreSQL进行总结,内容偏基础. 这里先附上一个PostgreSQL的中文资源:PostgreSQL 8.1 中文文档.英文不好的同学可以看看这个. 安装PostgreSQL ...