Dr. Who is organizing a banquet and will be inviting several guests. A guest is happy if…
- User
Dr. Who is organizing a banquet and will be inviting several guests. A guest is happy if he can chat with a fixed number of other guests. We assume that guests cannot talk to themselves. Help Dr. Who make all his guests happy, if possible, by organizing chats between guests.
Input Contains several data sets, and each data set is encoded on a line. A data set consists of n (n≤10000) positive integers a 1 ,a 2 ,...,a n . Each number a i (a i ≤1000,1≤i≤n) is the number of chat partners guest i would like to have.
Output If you can make all the guests happy, you should post "ok". If not all the guests to be happy, you should get the message "fail". After each message should be displayed an empty string.
In the following table, an input data with 4 data sets and the associated output is shown.
Examples Input #1 3 3 1 1 4 4 3 3 2 2 2 3 3 1 1 2 2 2 2 Answer #1 fail
ok
fail
ok Write code in pyhton
- Assistantpython
import sys import heapq def is_graphical(sequence): # Create a max heap using negative numbers heap = [-x for x in sequence if x > 0] heapq.heapify(heap) while heap: # Pop the largest degree d = -heapq.heappop(heap) if d == 0: continue if d > len(heap): return False temp = [] for _ in range(d): if not heap: return False adj = -heapq.heappop(heap) - 1 if adj < 0: return False temp.append(-adj) for item in temp: heapq.heappush(heap, item) return True def main(): for line in sys.stdin: line = line.strip() if not line: print() continue parts = line.split() sequence = list(map(int, parts)) if is_graphical(sequence): print("ok") else: print("fail") print() if __name__ == "__main__": main()