当前位置:网站首页>A consistent friend

A consistent friend

2022-06-09 20:53:00 Eager to learn

On the plateau n A town , The number is 1-n, They connect on the same road , Every town has an altitude h[i] rice . Travel from Xiaoming city 1 Start , Move from one town to another every day , But Xiao Ming moves up to the top every day x rice ,x That is Xiao Ming's physical strength . in other words : If v,u adjacent , If h[u] -h[v] > x, So Xiao Ming can't start from v Move to v, But you can walk from high altitude to low altitude , From u Move to v. Now Xiao Ming takes his friends to , He has m A friend , Every friend has a physical strength value xi, Xiao Ming hopes to find a friend who keeps pace with him , If his friend has the same physical strength as Xiao Ming , Then the town he can reach is exactly the same as Xiao Ming . Now Xiao Ming wants to find the number of friends who meet this situation .

Input :
The first row has three integers ,n,m,x Indicates the number of cities on the plateau , The number of friends and Xiao Ming's physical strength
The second line n It's an integer , Indicates that the number is from 1 To n The altitude of the city hi(1<=hi<=1000000000)
The third line m It's an integer , Indicates Xiao Ming's friend's physical strength value

example :
n = 5
m = 5
x = 6

h = [5, 7, 6, 10, 9]
y = [1, 8, 2, 9, 1]

Output
[2, 4]

def max_len(h, y, x):
    """" When climbing mountains , You can go down the mountain no matter how much physical strength you have , We just have to think about going up the mountain , The highest altitude you can climb left_height,  And the height you can't climb over for the first time right_height  If you can climb over , Then as long as the physical strength is greater than Xiao Ming's physical strength , otherwise , Physical strength left_height On right_height In order to keep pace with each other """
    #  Initialize the maximum height of the climb left_height, The height that cannot be reached for the first time 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:  #  All down the mountain 
        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])
原网站

版权声明
本文为[Eager to learn]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206092044477705.html