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

题意:

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

样例:

Input
  1. 9
    2 1 0 3 0 0 3 2 4
Output
  1. 2 1 0 1 0 0 1 2 3
  2.  
  3. 思路:把每一个0的下标都放进一个新数组中,然后枚举每一个数组pos,用二分查找pos在新数组中刚好大于等于pos的那个index
    然后再和前后的index比较下取最小值,然后即得出答案。
  4.  
  5. 作者的博客原链接:https://www.cnblogs.com/qieqiemin/
  1. 我的AC代码:
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <queue>
  7. #include <stack>
  8. #include <map>
  9. #include <set>
  10. #include <vector>
  11. #define rep(i,x,n) for(int i=x;i<n;i++)
  12. #define repd(i,x,n) for(int i=x;i<=n;i++)
  13. #define pii pair<int,int>
  14. #define pll pair<long long ,long long>
  15. #define gbtb std::ios::sync_with_stdio(false)
  16. #define MS0(X) memset((X), 0, sizeof((X)))
  17. #define MSC0(X) memset((X), '\0', sizeof((X)))
  18. #define pb push_back
  19. #define mp make_pair
  20. #define fi first
  21. #define se second
  22. #define gg(x) getInt(&x)
  23. using namespace std;
  24. typedef long long ll;
  25. inline void getInt(int* p);
  26. const int maxn=;
  27. const int inf=0x3f3f3f3f;
  28. /*** TEMPLATE CODE * * STARTS HERE ***/
  29. int n;
  30. int ans[maxn];
  31. int a[maxn];
  32. int cnt=;
  33. int l[maxn];
  34. int main()
  35. {
  36. gg(n);
  37. repd(i,,n)
  38. {
  39. gg(a[i]);
  40. }
  41. repd(i,,n)
  42. {
  43. if(a[i]==)
  44. {
  45. l[cnt++]=i;
  46. }
  47. }
  48. repd(i,,n)
  49. {
  50. if(a[i]!=)
  51. {
  52. int j1=lower_bound(l,l+cnt,i)-l;
  53. // int j2=uppercase()
  54. int v=l[j1];
  55. if(v==)
  56. {
  57. ans[i]=i-l[cnt-];
  58. }else
  59. {
  60. int num=abs(v-i);
  61. if(j1>=cnt-)
  62. {
  63.  
  64. }else
  65. {
  66. num=min(num,abs(l[j1+]-i));
  67.  
  68. }
  69. if(j1>)
  70. {
  71. num=min(num,abs(l[j1-]-i));
  72. }
  73. ans[i]=num;
  74. }
  75. }
  76. }
  77.  
  78. repd(i,,n)
  79. {
  80. printf("%d ",ans[i]);
  81. }
  82. return ;
  83. }
  84.  
  85. inline void getInt(int* p) {
  86. char ch;
  87. do {
  88. ch = getchar();
  89. } while (ch == ' ' || ch == '\n');
  90. if (ch == '-') {
  91. *p = -(getchar() - '');
  92. while ((ch = getchar()) >= '' && ch <= '') {
  93. *p = *p * - ch + '';
  94. }
  95. }
  96. else {
  97. *p = ch - '';
  98. while ((ch = getchar()) >= '' && ch <= '') {
  99. *p = *p * + ch - '';
  100. }
  101. }
  102. }
  1.  

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. SAP CRM 开发学习资料和教程整理【不定时更新】

    本文链接:http://www.cnblogs.com/hhelibeb/p/6276929.html 首先是SAP网站上面的相关内容 SAP Customer Relationship Manage ...

  2. WeakHashMap源码解读

    1. 简介 本文基于JDK8u111的源码分析WeakHashMap的一些主要方法的实现. 2. 数据结构 就数据结构来说WeakHashMap与HashMap原理差不多,都是拉链法来解决哈希冲突. ...

  3. 使用vue-cli脚手架创建的项目结构详解

    项目整体目录结构预览 src目录 src整体结构 开发过程中基本上操作都在该目录下进行操作的,项目所有源码都是在这个目录下 main.js文件,项目核心文件 App.vue文件,项目入口文件 rout ...

  4. 简单规划dp sumsets

    Farmer John commanded his cows to search for different sets of numbers that sum to a given number. T ...

  5. 【BZOJ3622】已经没有什么好害怕的了

    Description 已经使 Modoka 有签订契约, 和自己一起战斗的想法后 , Mami 忽然感到自己不再是孤单一人了呢. 于是, 之前的谨慎的战斗作风也消失了 , 在对 Charlotte ...

  6. linux 的常用命令---------第十一阶段

    软件管理rpm.yum 在 windows 与 linux 之间 实现小文件传输(仅支持在 X shell 中完成文件传输,虚拟机中不可实现): # yum install  lrzsz  -y    ...

  7. 单调队列&单调栈

    单调队列 例题: Poj 2823给定一个数列,从左至右输出每个长度为m的数列段内的最小数和最大数.数列长度:N<=106,m<=N 对于单调队列,我们这样子来定义: 1.维护区间最值 2 ...

  8. 1002-过河卒-洛谷-luogu-动态规划dp

    题目描述 棋盘上AA点有一个过河卒,需要走到目标BB点.卒行走的规则:可以向下.或者向右.同时在棋盘上CC点有一个对方的马,该马所在的点和所有跳跃一步可达的点称为对方马的控制点.因此称之为“马拦过河卒 ...

  9. 吴恩达机器学习CS229课程笔记学习

    监督学习(supervised learning) 假设我们有一个数据集(dataset),给出居住面积和房价的关系如下: 我们以居住面积为横坐标,房价为纵坐标,组成数据点,如(2104, 400), ...

  10. 在模拟器上运行Android项目时报错:DELETE_FAILED_INTERNAL_ERROR Error while Installing APKs

    今天在Android Studio自带的模拟器上运行项目的时候,出现如下所示Error:当点击ok后,发现模拟器不能运行程序. 解决办法: 更改Android Studio中的设置: File---& ...