当前位置:网站首页>步调一致的朋友
步调一致的朋友
2022-06-09 20:45:00 【馋学习的身子】
高原上有n个城镇,编号为1-n,它们在同一条道路上连接,每个城镇都有一个海拔高度h[i]米。小明的旅行从城市1开始,每天都从一个城镇移动到相邻的城镇,但是小明每天最多向高处移动x米,x也就是小明的体力值。也就是说:如果v,u相邻,如果h[u] -h[v] > x,那么小明无法从v移动到v,但可以从海拔高的地方走到海拔低的地方,即从u移动到v。现在小明带上朋友去,他有m个朋友,每个朋友都有一个体力值xi,小明希望找和他步调一致的朋友,如果他的朋友体力和小明差不多,那么他可以到达的城镇和小明完全一样。现在小明要找到符合这种情况的朋友的编号。
输入:
第一行三个整数,n,m,x表示高原上城市的个数,朋友个数和小明的体力值
第二行n个整数,表示编号从1到n的城市的海拔高度hi(1<=hi<=1000000000)
第三行m个整数,表示小明朋友的体力值
例:
n = 5
m = 5
x = 6
h = [5, 7, 6, 10, 9]
y = [1, 8, 2, 9, 1]
输出
[2, 4]
def max_len(h, y, x):
""""爬山的时候,下山不论体力多少都可以下去,我们只要考虑上山的时候,能够爬过的最高高度left_height, 以及首次不能爬过去的高度right_height 如果都能爬过去,则只要体力大于小明体力即可,否则,体力在left_height于right_height之间才可以步调一致"""
# 初始化爬过的上山的最高高度left_height,首次不能达到的高度right_height
left_height = -1
right_height = 0
for i in range(1, len(h)):
if h[i] - h[i - 1] <= x:
left_height = max(left_height, 0 if h[i] - h[i - 1] < 0 else h[i] - h[i - 1])
else:
right_height = h[i] - h[i - 1]
break
if not right_height: # 都是下山
print([i + 1 for i in range(len(y)) if y[i] > x])
else:
print([i + 1 for i in range(len(y)) if left_height <= y[i] < right_height])
边栏推荐
- Kubernetes native cicd:tekton Hello World
- idea:new没有class
- KubeVirt网络源码分析(3)- 虚拟机热迁移网络
- Lambda Exception
- 编程题:统计字符串中出现次数最多的字母
- Application of commission in C #
- The processor of this virtual machine supports different functions than the processor of the virtual machine that holds the state of the virtual machine
- go安装图文教程
- LeetCode 526. A graceful arrangement***
- Detailed explanation of uboot
猜你喜欢

The processor of this virtual machine supports different functions than the processor of the virtual machine that holds the state of the virtual machine

go安装图文教程

刚学嵌入式,想问问什么是中断,中断的概念是什么

The browser cannot open Baidu, and others can be opened normally

Changshu science and technology applet SQL injection

BI 如何让SaaS产品具有 “安全感”和“敏锐感”(上)

HMI 联机下载失败的问题及解决方案
Who says redis can't save big keys

Mysql异常:The server time zone value‘XXX'解决

HMI 创建工程生成字库的一个潜在bug
随机推荐
LeetCode 526. 优美的排列***
Common regular expressions
【运维有小邓】AD域文件权限管理
C#中String的知识点
SSM driving school management system
Just learning embedded, I want to ask what is interrupt and what is the concept of interrupt
从源码解析flutter_redux 的精准局部刷新
C interface class learning
搭建ngrok服务器,实现内网穿透服务,实现外网到内网的在线访问
Fanwei oa8 foreground SQL injection
Extended application of C # static class
华为云工业智能中枢,为加速工业智能化升级提供新动能
C#中委托与事件之间的一些应用
Kubernetes原生CICD:Tekton hello-world
C#中委托的应用
Redis知识点
Some applications between delegates and events in C #
Huawei's cloud industrial intelligence hub provides new momentum for accelerating the upgrading of industrial intelligence
Mysql异常:The server time zone value‘XXX'解决
CompareTo和compare的区别