Distances to Zero(lower_bound和upper_bound应用)

Python数学建模实战:从数据清洗到路径优化求解完整指南 数学建模是连接现实问题与数学理论的桥梁,其核心在于通过抽象与简化,构建可量化分析的数学模型。在工程实践中,Python凭借其丰富的数据科学生态,成为实现建模与求解的主流工具。数据清洗与特征工程是确保模型有效性的基石,涉及缺失值处理、异常值检测与特征构造,为后续分析提供高质量“食材”。车辆路径问题(VRP)作为经典的组合优化问题,在物流配送、资源调度等场景具有极高应用价值。本文以Python为技术栈,系统演示了使用Pandas进行数据预处理、结合OR-Tools求解器构建并求解带容量约束的VRP模型,并完成结 阅读详情
B. Distances to Zero
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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.

Input

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).

Output

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.

Examples
Input
9
2 1 0 3 0 0 3 2 4
Output
2 1 0 1 0 0 1 2 3 
Input
5
0 1 2 3 4
Output
0 1 2 3 4 
Input
7
5 6 0 1 -2 3 4
Output
2 1 0 1 2 3 4 
题意:题意比较好理解,就是找每个非零数,离这个数组中的零的最短距离,数组中可能有多个零,找最近的算出距离,最后并大印
出来
思路:一开始看到时间限制2000ms,就想直接暴力,遇到非零数左右遍历,但是超时了,但是听他们说暴力做也过了,哎不管了,自己
能想到的暴力都是最简单最单纯的暴力,人家的暴力可能还动了些手脚,后来学长讲了更搞笑(高效)的方法,储存下零的位置,再储存
下非零数的位置,以非零数位置为查找值用lower_bound和upper_bound找到,这个非零位置左右两边零的位置,相减比较位置
下面是代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
int n;
int a[200100];
int dis[200100];
vector<int>zero,number;
int main(){
    int i;
    zero.push_back(-INF);//先把一个最小的数放在数组首位置
    scanf("%d",&n);
    for(i = 0; i < n; i++){
        scanf("%d",&a[i]);
        if(a[i]==0){
            dis[i] = 0;//如果是零距离是零,直接存
            zero.push_back(i);//把零的位置依次存到数组中
        }
        else number.push_back(i);//把非零的数的位置存到另一个数组中
    }
    zero.push_back(INF);//把一个最大数放在数组的末尾位置
    vector<int>::iterator it;
    for(i = 0,it = number.begin(); it != number.end(); it++,i++){//用迭代器遍历非零的数组
        int pos1,pos2;
        pos1 = *(lower_bound(zero.begin(),zero.end(),number[i])-1);//lower_bound找到第一个大于等于的坐标,然后减1,得到这个数左边零的位置
        pos2 = *upper_bound(zero.begin(),zero.end(),number[i]);//upper_bound找到第一个大于的坐标,找到的是右边零的位置
        int dis1,dis2;                                         //注意两个函数返回的是一个迭代器,所以取星得到的值才是零在原来数组的位置
        dis1 = number[i]-pos1;
        dis2 = pos2-number[i];//分别求这两个数和左右两个零的位置
        dis[number[i]] = min(dis1,dis2);//取最小
    }
    for(i = 0; i < n; i++){//输出
        if(i==0)printf("%d",dis[i]);
        else printf(" %d",dis[i]);
    }
    puts("");
    return 0;
}


【信息科学与工程学】【通信工程】第一百五十五篇 网络拓扑学09 聚焦云数据中心(多region多AZ+多边缘DC)与城域网5G/6G网络一体化的拓扑学编号类型领域问题问题的数学分析(拓扑学+其他)参数列表及参数的数值范围关联知识A166图论/组合优化云原生数据湖如何用Apache Hudi的Timeline拓扑优化增量查询?​1. 拓扑学分析(详细): - 拓扑结构:Apache Hudi使用Timeline管理表的历史变更,Timeline是一个时间轴上的DAG。每个Instant(提交、 compaction、 cleaning等)是一个节点,节点之间按时间顺 阅读详情

相关推荐

iPhone用户必看:不装App也能搞定GitHub 2FA验证的3种方法(附恢复码管理技巧)

本文为iPhone用户提供了无需安装第三方App即可完成GitHub双重身份验证(2FA)的三种原生方案:利用相机扫码与iCloud钥匙串自动填充、使用iCloud钥匙串内置的TOTP验证码生成器,以及借助Safari浏览器扩展。文章详细解析了每种方法的操作步骤、适用场景,并重点强调了恢复码的安全管理与多层备份策略,帮助开发者在提升账户安全的同时,享受苹果生态的无缝便捷体验。

weixin_29324401的博客 653

Distances to Zero

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

gtuif的博客 340

Tessent MBIST SDC

主要是两个PROC,tessent_constrain_${design_name}_mentor_ijtag_non_modal和tessent_constrain_${design_name}_mentor_memory_bist_modal。其中,Memory/MBIST controller/MEM Interface可以从func_clk_1来(用func_clk_1和func_clk_2的高频时钟),如图中红色线。其中,SIB和BAP用ijtag_tck的clock,如图绿色线。

po_poo的博客 1987

[简单思维题]Distances to Zero

题目描述 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 elem...

dingfei496705的博客 197

认知网络分析实战:用Python复现课堂对话研究(含LSA代码)

本文通过Python实战,详细演示了如何运用认知网络分析(ENA)和滞后序列分析(LSA)两种方法,对课堂对话数据进行深度挖掘。文章提供了从数据清洗、行为编码到网络构建、序列统计及可视化的完整代码流程,帮助教育研究者和数据分析师量化分析对话中的认知结构与动态互动模式,从而获得超越文本表面的研究洞见。

weixin_28559803的博客 155

倍福Beckhoff运动控制功能块实战解析:从MC_ReadActualPosition到MC_GearInVelo

本文深入解析倍福(Beckhoff)TwinCAT运动控制功能块,从基础的MC_ReadActualPosition、MC_Power等轴状态读取与使能操作,到MC_MoveAbsolute、MC_MoveRelative等单轴定位指令,并重点剖析了用于动态速度同步的核心功能块MC_GearInVelo在飞锯等场景的应用。文章结合实战经验,详细介绍了齿轮耦合、电子凸轮(MC_CamIn)等高级同步功能的参数配置、调试技巧及常见问题,旨在帮助工程师快速掌握倍福运动控制工具箱,实现复杂的同步工艺。

transformer2023的博客 981

Educational Codeforces Round 20 Distances to Zero

B. Distances to Zero time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given the array of integer num

爱梦者的心之旅 643

HBase批量导入工具:BulkLoad使用详解

HBase作为分布式NoSQL数据库,在大数据领域有着广泛应用。传统的数据写入方式(如Put操作)在面对海量数据导入时存在性能瓶颈。BulkLoad提供了一种高效的大批量数据导入方案,可以显著提高数据加载速度并降低集群负载。BulkLoad的核心原理和架构与传统写入方式的性能对比详细的使用方法和实现步骤性能优化技巧和最佳实践实际应用案例分析本文首先介绍BulkLoad的基本概念和原理,然后深入讲解其实现机制和关键技术。接着通过实际案例演示BulkLoad的使用方法,并分析性能优化策略。

大数据洞察的博客 1236

【Codeforces 803 B. Distances to Zero】

B. Distances to Zero time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output You are given the array of integer numbers a0, a1, …, an - 1. For eac

楚江枫 801

嵌入式内存管理之“LittleFS文件系统”开发(附LittleFS项目源码)

本文介绍了如何在嵌入式系统中实现 LittleFS 文件系统,并结合 Flash 存储进行数据管理。通过详细的代码示例,展示了如何配置、挂载 LittleFS 文件系统,并进行文件操作,如读取、写入和更新数据。特别地,博客通过实际应用演示了如何实现启动计数功能,以及如何处理文件系统的格式化和挂载过程。该示例帮助读者掌握在资源受限的嵌入式环境中实现高效文件存储管理的关键技术。

Bit_Dong的博客 3182

【Network Management】AUTOSAR局部网络管理从0到1配置实践

AUTOSAR局部网络管理从0到1配置实践

qq_36056498的博客 2913

量化技术与智能降维:从模型压缩到量化交易实战

量化技术作为数据处理和模型优化的核心方法,通过将连续数据转换为离散数值实现高效压缩。其基本原理是用较少比特表示原始数据,在保持信息完整性的同时显著减少存储和计算开销。这种技术在深度学习领域尤为重要,如将FP32模型量化为INT8或INT4,能够大幅提升推理速度并降低资源消耗。量化技术的工程价值体现在模型部署优化、边缘计算加速等场景,特别是在金融量化交易和AI模型压缩中发挥关键作用。智能降维则通过算法自动识别数据关键特征,结合机器学习处理非线性关系,为高维数据分析提供有效解决方案。本文通过GPTQ等先进量化算

weixin_34396103的博客 457

SGP4/SDP4详解

**SGP4** (Simplified General Perturbations Version 4) 和 **SDP4** (Simplified Deep-space Perturbations Version 4) 是一组**解析轨道预报模型**,用于预测地球轨道卫星的位置和速度。它们是北美防空司令部(NORAD)维护的**TLE**数据格式的标准配套算法。 简单来说:**TLE是卫星轨道的"身份证",而SGP4/SDP4是解读这张身份证的"说明书"**。

ScilogyHunter的博客 589

Learning Latent Dynamics for Planning from Pixels 从像素中学习用于规划的潜在动态

Abstract摘要规划在已知环境动力学的控制任务中取得了巨大成功。为了在未知环境中利用规划,智能体需要从与世界的交互中学习动力学。然而,学习足够准确以用于规划的动力学模型一直是一项长期挑战,尤其是在基于图像的领域中。我们提出了 Deep Planning Network(PlaNet),这是一种纯基于模型的智能体,能够从图像中学习环境动力学,并通过潜在空间中的快速在线规划选择动作。为实现高性能,动力学模型必须能够准确预测未来多个时间步的奖励。

u013250861的博客 264

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

B - Distances to Zero

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 giv

海绵的博客 361

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

803B Distances to Zero

#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using namespace placeholders; int main(){

zju2016的博客 361

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

Codeforces 803B Distances to Zero 题解

Codeforces 803B题解

DarkoDu的博客 706

【CodeForces 803B】Distances to Zero(模拟)

B. Distances to Zero time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given the array of int

FSMM_blog 607

lower_bound(一个不怎么好的栗子)

B. Distances to Zero time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard outputYou are given the array of integer numbers a0, a1, …, an - 1. F

qq_39557517的博客 328

leetcode 1005 K次取反后最大化的数组和

leetcode 1005 K次取反后最大化的数组和

大草的博客 347

lower_bound()函数 的使用

leetcode刷题

weixin_62466612的博客 5965

Codeforces911题解

A. Nearest Minimums You are given an array of n integer numbers a0, a1, ..., an - 1. Find the distance between two closest (nearest) minimums in it. It is guaranteed that in the array a minimum occ

OneDay_pyf的博客 3641

Codeforces Round 722C:Destroying Array(并查集,好题)

题目链接 C. Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You are given an array consistin

fouzhe的博客 3656
上一篇: 列出连通集(25 分)(深搜广搜)
下一篇: lower_bound和upper_bound算法
Guuuuuu老师儿
博客等级 码龄9年 259粉丝 718原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值