Alibaba Written Test 4.20

1. Both heroes and monsters have ability values. Heroes can only play monsters with ability values ​​less than their own ability values. Each time a monster is played, you can get a gold coin. You can exchange a gold coin for 1 ability value at any time, but the number of gold coins cannot Negative (initially 0). Heroes can fight monsters in any order, and monsters can be played without asking for the maximum number of gold coins.

>> Enter
1 3
2 2 1

Note: The first number in the first row represents the initial ability value of the hero, the second number represents the total number of monsters; the second row represents the ability value of each monster

>> Output
2

Explanation: The hero first hit the monster with the ability value of 1 and replaced the obtained 1 gold coin with 1 ability value. Now there are 2 ability values. After the remaining monsters are defeated, a total of 2 gold coins are obtained.

import sys 
 
def solve (a, n, blist): 
    blist = sorted (blist) 
    curr_a = a 
    #Capability value curr_g = 0 
    #Current gold coin value max_g = 0 #Maximum available gold coin 
 
    for tb in blist: 
        if curr_a> = tb: 
            curr_g + = 1 
            max_g = max (max_g, cur_g) 
        elif cur_g <(tb-curr_a): #The current gold coins are all used to buy abilities but cannot beat monsters, then exit the loop 
             break 
        else: 
            curr_g-= (tb- curr_a) #Spending gold coins to purchase ability 
            curr_a = tb #Update ability value 
            curr_g + = 1 #After updating ability value continue to blame 
            max_g = max (max_g, cur_g) 
    return max_g 
  
lines = (t.strip (). split () for t in sys.stdin.readlines () if t.strip ()]
a, n = int (lines [0] [0] ), int (lines [0] [1]) 
blist = [int (t) for t in lines [1]]
print (solve(a, n, blist))

2. There are n cities, and each city has a rank value. There are n-1 edges between the n cities, and the time weight of each edge is 1, forming a tree structure. Question: From any city to another city of the same level, there can be no duplicate edges in the path. What is the minimum time? (If there is no path that meets the requirements, -1 is returned)

>> Enter
3
1 2 1
1 2
2 3

Note: the number in the first row represents the number of cities n, the second row represents the rank value of each city, and the next n-1 rows represent the connection of each edge

>> Output
2

Explanation: From city 1 to city 3, the level is 1, and the elapsed time is 2

import sys 
def solve (n, alist, edges): #number of cities; city rank; edges of the city 
    adic = {} # {city i: the rank of the city j} 
    reverse_adic = {} # {rank j: the rank corresponds The number of cities m} 
    for i, ta in enumerate (alist): 
        adic [i + 1] = ta 
        reverse_adic.setdefault (ta, 0) 
        reverse_adic [ta] + = 1 

    adj_dic = {} # {city i: and city i connected city list} 
    for f, t in edges: 
        adj_dic.setdefault (f, []) 
        adj_dic.setdefault (t, []) 
         # 无 向 边
        adj_dic [t] .append (f) 
        adj_dic [f] .append (t) 
    
    ans_min = None 
    for tp in adj_dic: #loop through each node of the city tp  
        pa = adic [tp] tp level (initial node level!)
        if reverse_adic [pa] <= 1: #if the number of cities corresponding to the level of the city tp is less than 2, then exit this loop 
            continue

        used_edge = set () #The problem requires that there must be no duplicate edges in the path 
        queue = [(tp, 0)] # (initial node tp, current time spent) 
        flag = False 
        mindist = None 

        for r, tdist in queue:   
            for nxt in adj_dic [r]: #adj_dic [r ] is a list of all nodes connected to the nodes of r, r -> NXT 
                # If no paths through 
                IF (r, NXT) not in used_edge and (NXT, r) not in used_edge: 
                    # If The level corresponding to the connected node is equal to the level of the initial node tp, then the loop from the tp node ends 
                    if adic [nxt] == ​​pa: 
                        flag = True 
                        mindist = tdist + 1 
                        break 
                     there is no end, continue to find
                    queue.append ((nxt, tdist + 1)) # Adding elements in the queue will continue processing in for r, tdist in queue! 
                    #Used 
                     edges need to be recorded, and next time you ca n’t continue to use used_edge.add ((r, nxt)) #If 
            you have found it, you do n’t need to continue to traverse the elements in the queue 
            if flag: # Situation: A node has two Connected nodes, just use two down edges, one of them is found, you do n’t need to     
                break 
        #Continue to find another one if ans_min == None or mindist <ans_min: 
            ans_min = mindist update the minimum time 
    return -1 if ans_min == None else ans_min 
        

lines = [t.strip (). Split () for t in sys.stdin.readlines () if t.strip ()] 
n = int (lines [0] [0]) 
alist = [int (t) for t in lines [1]] 
edges = [(int (f), int (t)) for f, t in lines [2:]] 

print (solve (n, alist, edges ) )

 

Guess you like

Origin www.cnblogs.com/USTC-ZCC/p/12742936.html