text
stringlengths
198
433k
conversation_id
int64
0
109k
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Two players play a game. The game is played on a rectangular board with n Γ— m squares. At the beginning of the game two different squares of the board have two chips. The first player's goal is to shift the chips to the same square. The second player aims to stop the first one with a tube of superglue. We'll describe the rules of the game in more detail. The players move in turns. The first player begins. With every move the first player chooses one of his unglued chips, and shifts it one square to the left, to the right, up or down. It is not allowed to move a chip beyond the board edge. At the beginning of a turn some squares of the board may be covered with a glue. The first player can move the chip to such square, in this case the chip gets tightly glued and cannot move any longer. At each move the second player selects one of the free squares (which do not contain a chip or a glue) and covers it with superglue. The glue dries long and squares covered with it remain sticky up to the end of the game. If, after some move of the first player both chips are in the same square, then the first player wins. If the first player cannot make a move (both of his chips are glued), then the second player wins. Note that the situation where the second player cannot make a move is impossible β€” he can always spread the glue on the square from which the first player has just moved the chip. We will further clarify the case where both chips are glued and are in the same square. In this case the first player wins as the game ends as soon as both chips are in the same square, and the condition of the loss (the inability to move) does not arise. You know the board sizes and the positions of the two chips on it. At the beginning of the game all board squares are glue-free. Find out who wins if the players play optimally. Input The first line contains six integers n, m, x1, y1, x2, y2 β€” the board sizes and the coordinates of the first and second chips, correspondingly (1 ≀ n, m ≀ 100; 2 ≀ n Γ— m; 1 ≀ x1, x2 ≀ n; 1 ≀ y1, y2 ≀ m). The numbers in the line are separated by single spaces. It is guaranteed that the chips are located in different squares. Output If the first player wins, print "First" without the quotes. Otherwise, print "Second" without the quotes. Examples Input 1 6 1 2 1 6 Output First Input 6 5 4 3 2 1 Output First Input 10 10 1 1 10 10 Output Second Submitted Solution: ``` n,m,X,Y,x,y=map(int,input().split()) a=[abs(X-x),abs(Y-y)] a.sort() print("First" if a[0]<5 and a[0]+a[1]<7 else "Second") ``` No
90,400
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Two players play a game. The game is played on a rectangular board with n Γ— m squares. At the beginning of the game two different squares of the board have two chips. The first player's goal is to shift the chips to the same square. The second player aims to stop the first one with a tube of superglue. We'll describe the rules of the game in more detail. The players move in turns. The first player begins. With every move the first player chooses one of his unglued chips, and shifts it one square to the left, to the right, up or down. It is not allowed to move a chip beyond the board edge. At the beginning of a turn some squares of the board may be covered with a glue. The first player can move the chip to such square, in this case the chip gets tightly glued and cannot move any longer. At each move the second player selects one of the free squares (which do not contain a chip or a glue) and covers it with superglue. The glue dries long and squares covered with it remain sticky up to the end of the game. If, after some move of the first player both chips are in the same square, then the first player wins. If the first player cannot make a move (both of his chips are glued), then the second player wins. Note that the situation where the second player cannot make a move is impossible β€” he can always spread the glue on the square from which the first player has just moved the chip. We will further clarify the case where both chips are glued and are in the same square. In this case the first player wins as the game ends as soon as both chips are in the same square, and the condition of the loss (the inability to move) does not arise. You know the board sizes and the positions of the two chips on it. At the beginning of the game all board squares are glue-free. Find out who wins if the players play optimally. Input The first line contains six integers n, m, x1, y1, x2, y2 β€” the board sizes and the coordinates of the first and second chips, correspondingly (1 ≀ n, m ≀ 100; 2 ≀ n Γ— m; 1 ≀ x1, x2 ≀ n; 1 ≀ y1, y2 ≀ m). The numbers in the line are separated by single spaces. It is guaranteed that the chips are located in different squares. Output If the first player wins, print "First" without the quotes. Otherwise, print "Second" without the quotes. Examples Input 1 6 1 2 1 6 Output First Input 6 5 4 3 2 1 Output First Input 10 10 1 1 10 10 Output Second Submitted Solution: ``` n,m,x1,y1,x2,y2 = map(int, input().split()) n = abs(x1 - x2) m = abs(y1 - y2) if n > 2 or m > 2: print("Second") elif n == 2 and m == 2: print("Second") else: print("First") ``` No
90,401
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Pavel plays a famous computer game. A player is responsible for a whole country and he can travel there freely, complete quests and earn experience. This country has n cities connected by m bidirectional roads of different lengths so that it is possible to get from any city to any other one. There are portals in k of these cities. At the beginning of the game all portals are closed. When a player visits a portal city, the portal opens. Strange as it is, one can teleport from an open portal to an open one. The teleportation takes no time and that enables the player to travel quickly between rather remote regions of the country. At the beginning of the game Pavel is in city number 1. He wants to open all portals as quickly as possible. How much time will he need for that? Input The first line contains two space-separated integers n and m (1 ≀ n ≀ 105, 0 ≀ m ≀ 105) that show how many cities and roads are in the game. Each of the next m lines contains the description of a road as three space-separated integers xi, yi, wi (1 ≀ xi, yi ≀ n, xi β‰  yi, 1 ≀ wi ≀ 109) β€” the numbers of the cities connected by the i-th road and the time needed to go from one city to the other one by this road. Any two cities are connected by no more than one road. It is guaranteed that we can get from any city to any other one, moving along the roads of the country. The next line contains integer k (1 ≀ k ≀ n) β€” the number of portals. The next line contains k space-separated integers p1, p2, ..., pk β€” numbers of the cities with installed portals. Each city has no more than one portal. Output Print a single number β€” the minimum time a player needs to open all portals. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 3 3 1 2 1 1 3 1 2 3 1 3 1 2 3 Output 2 Input 4 3 1 2 1 2 3 5 2 4 10 3 2 3 4 Output 16 Input 4 3 1 2 1000000000 2 3 1000000000 3 4 1000000000 4 1 2 3 4 Output 3000000000 Note In the second sample the player has to come to city 2, open a portal there, then go to city 3, open a portal there, teleport back to city 2 and finally finish the journey in city 4. Submitted Solution: ``` print("Hello World!") print("Hello World!") print("Hello World!") ``` No
90,402
Provide tags and a correct Python 3 solution for this coding contest problem. The Little Elephant has got a problem β€” somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≀ n ≀ 105) β€” the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, β€” array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Tags: implementation, sortings Correct Solution: ``` n=int(input()) arr1=list(map(int,input().split())) count=0 j=0 arr2=sorted(arr1) for i in range(n): if(arr1[i]!=arr2[i]): count+=1 if(count<=2): print('YES') else: print('NO') ```
90,403
Provide tags and a correct Python 3 solution for this coding contest problem. The Little Elephant has got a problem β€” somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≀ n ≀ 105) β€” the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, β€” array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Tags: implementation, sortings Correct Solution: ``` n = int(input()) arr = list(map(int, input().split())) ideal = sorted(arr) i = 0 c = 0 f = 1 while i < n: if arr[i] != ideal[i]: c += 1 i += 1 if c > 2: f = 0 break if f == 1: print('YES') else: print('NO') # little elephant ```
90,404
Provide tags and a correct Python 3 solution for this coding contest problem. The Little Elephant has got a problem β€” somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≀ n ≀ 105) β€” the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, β€” array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Tags: implementation, sortings Correct Solution: ``` # Target - Expert on CF # Be Humblefool import sys # inf = float("inf") # sys.setrecursionlimit(10000000) # abc='abcdefghijklmnopqrstuvwxyz' # abd={'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25} # mod, MOD = 1000000007, 998244353 # words = {1:'one',2:'two',3:'three',4:'four',5:'five',6:'six',7:'seven',8:'eight',9:'nine',10:'ten',11:'eleven',12:'twelve',13:'thirteen',14:'fourteen',15:'quarter',16:'sixteen',17:'seventeen',18:'eighteen',19:'nineteen',20:'twenty',21:'twenty one',22:'twenty two',23:'twenty three',24:'twenty four',25:'twenty five',26:'twenty six',27:'twenty seven',28:'twenty eight',29:'twenty nine',30:'half'} # vow=['a','e','i','o','u'] # dx,dy=[-1,1,0,0],[0,0,1,-1] # import random # from collections import deque, Counter, OrderedDict,defaultdict # from heapq import nsmallest, nlargest, heapify,heappop ,heappush, heapreplace # from math import ceil,floor,log,sqrt,factorial,pi,gcd # from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right def get_array(): return list(map(int, sys.stdin.readline().strip().split())) def get_ints(): return map(int, sys.stdin.readline().strip().split()) def input(): return sys.stdin.readline().strip() n = int(input()) Arr = get_array() New = sorted(Arr) count = 0 for i in range(n): if Arr[i]!=New[i]: count+=1 if count<=2: print('YES') else: print('NO') ```
90,405
Provide tags and a correct Python 3 solution for this coding contest problem. The Little Elephant has got a problem β€” somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≀ n ≀ 105) β€” the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, β€” array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Tags: implementation, sortings Correct Solution: ``` n=int(input());lt=list(map(int,input().split())) ltt=sorted(lt) cnt=sum(lt[i]!=ltt[i] for i in range(n)) if(cnt>2):print('NO');exit(0) print('YES') ```
90,406
Provide tags and a correct Python 3 solution for this coding contest problem. The Little Elephant has got a problem β€” somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≀ n ≀ 105) β€” the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, β€” array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Tags: implementation, sortings Correct Solution: ``` n=int(input()) #n,k=map(int,input().split()) a=list(map(int, input().split())) b=sorted(a) f=0 for i in range(n): if a[i]!=b[i]: f+=1 if f<=2: print("YES") else: print("NO") ```
90,407
Provide tags and a correct Python 3 solution for this coding contest problem. The Little Elephant has got a problem β€” somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≀ n ≀ 105) β€” the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, β€” array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Tags: implementation, sortings Correct Solution: ``` n = int(input()) a = [] b = input() i = 0 while i < len(b): to_add = "" while i < len(b) and b[i] != " ": to_add += b[i] i += 1 a.append(int(to_add)) i += 1 two_times = False first = second = -1 prev = -1 for i in range(1, n): if a[i] == prev: second += 1 if a[i] < a[i - 1]: if first == -1: first = i - 1 second = i prev = a[i] else: second = i break if first == second == -1: print("YES") exit() for i in range(n): if a[i] > a[second]: a[i], a[second] = a[second], a[i] break for i in range(1, n): if a[i] < a[i - 1]: print("NO") exit() print("YES") ```
90,408
Provide tags and a correct Python 3 solution for this coding contest problem. The Little Elephant has got a problem β€” somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≀ n ≀ 105) β€” the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, β€” array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Tags: implementation, sortings Correct Solution: ``` n = int(input()) A = [int(a) for a in input().split()] B = A.copy() B.sort() c = 0 for i in range(n): a = A[i] b = B[i] if a == b: continue else: c += 1 if c == 0 or c == 2: print("YES") else: print("NO") ```
90,409
Provide tags and a correct Python 3 solution for this coding contest problem. The Little Elephant has got a problem β€” somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≀ n ≀ 105) β€” the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, β€” array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Tags: implementation, sortings Correct Solution: ``` n = int(input()) c = [int(x) for x in input().split(' ')] d = sorted(c) v = 0 for i in range(n): if c[i] != d[i]: v += 1 #print(v) if v < 3: print('YES') else: print('NO') ```
90,410
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Little Elephant has got a problem β€” somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≀ n ≀ 105) β€” the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, β€” array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Submitted Solution: ``` n, values, wrong = int(input()), [int(i) for i in input().split()], 0 sorted_values = list(sorted(values)) for i in range(n): if values[i] != sorted_values[i]: wrong += 1 if wrong > 2: print("NO") else: print("YES") ``` Yes
90,411
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Little Elephant has got a problem β€” somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≀ n ≀ 105) β€” the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, β€” array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Submitted Solution: ``` from sys import stdin, stdout n = int(stdin.readline()) values = list(map(int, stdin.readline().split())) challengers = [] for i in range(n): challengers.append(values[i]) challengers.sort() cnt = 0 for i in range(1, n + 1): a = challengers.pop() if a != values[-i]: for j in range(n, i, -1): if values[-j] == a: values[-j], values[-i] = values[-i], values[-j] break break if sorted(values) != values: stdout.write('NO') else: stdout.write('YES') ``` Yes
90,412
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Little Elephant has got a problem β€” somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≀ n ≀ 105) β€” the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, β€” array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Submitted Solution: ``` # cook your dish here n=int(input()) arr=[int(x) for x in input().split()] li=arr[:] li.sort() c=0 for i in range(n): if(arr[i]!=li[i]): c+=1 if(c>2): print("NO") break else: print("YES") ``` Yes
90,413
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Little Elephant has got a problem β€” somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≀ n ≀ 105) β€” the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, β€” array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Submitted Solution: ``` tamanho = int(input()) vetor = input() vetor = vetor.split() vetor = [int(vetor[i]) for i in range(0, tamanho)] troca = 0 copia =[vetor[i] for i in range(0, tamanho)] vetor.sort() for i in range(0, tamanho): if (copia[i] != vetor[i]): troca += 1 #print(troca) if (troca <= 2): print("YES") else: print("NO") ``` Yes
90,414
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Little Elephant has got a problem β€” somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≀ n ≀ 105) β€” the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, β€” array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Submitted Solution: ``` # Author : raj1307 - Raj Singh # Date : 02.01.2020 from __future__ import division, print_function import os,sys from io import BytesIO, IOBase if sys.version_info[0] < 3: from __builtin__ import xrange as range from future_builtins import ascii, filter, hex, map, oct, zip def ii(): return int(input()) def si(): return input() def mi(): return map(int,input().strip().split(" ")) def msi(): return map(str,input().strip().split(" ")) def li(): return list(mi()) def dmain(): sys.setrecursionlimit(100000000) threading.stack_size(40960000) thread = threading.Thread(target=main) thread.start() #from collections import deque, Counter, OrderedDict,defaultdict #from heapq import nsmallest, nlargest, heapify,heappop ,heappush, heapreplace #from math import ceil,floor,log,sqrt,factorial #from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right #from decimal import *,threading #from itertools import permutations abc='abcdefghijklmnopqrstuvwxyz' abd={'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25} mod=1000000007 #mod=998244353 inf = float("inf") vow=['a','e','i','o','u'] dx,dy=[-1,1,0,0],[0,0,1,-1] def getKey(item): return item[1] def sort2(l):return sorted(l, key=getKey) def d2(n,m,num):return [[num for x in range(m)] for y in range(n)] def isPowerOfTwo (x): return (x and (not(x & (x - 1))) ) def decimalToBinary(n): return bin(n).replace("0b","") def ntl(n):return [int(i) for i in str(n)] def powerMod(x,y,p): res = 1 x %= p while y > 0: if y&1: res = (res*x)%p y = y>>1 x = (x*x)%p return res def gcd(x, y): while y: x, y = y, x % y return x def isPrime(n) : # Check Prime Number or not if (n <= 1) : return False if (n <= 3) : return True if (n % 2 == 0 or n % 3 == 0) : return False i = 5 while(i * i <= n) : if (n % i == 0 or n % (i + 2) == 0) : return False i = i + 6 return True def read(): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') def main(): #for _ in range(ii()): n=ii() a=li() a.insert(0,-inf) a.insert(n+1,inf) f=0 for i in range(1,n+1): if a[i-1]<a[i] and a[i]>a[i+1]: for j in range(i+2,n+1): if a[j-1]>a[j] and a[j]<a[j+1]: a[i],a[j]=a[j],a[i] f=1 break if f: break if sorted(a)==a: print('YES') else: print('NO') # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") def print(*args, **kwargs): """Prints the values to a stream, or to sys.stdout by default.""" sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout) at_start = True for x in args: if not at_start: file.write(sep) file.write(str(x)) at_start = False file.write(kwargs.pop("end", "\n")) if kwargs.pop("flush", False): file.flush() if sys.version_info[0] < 3: sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout) else: sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion if __name__ == "__main__": #read() main() #dmain() # Comment Read() ``` No
90,415
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Little Elephant has got a problem β€” somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≀ n ≀ 105) β€” the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, β€” array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Submitted Solution: ``` n=int(input()) a=list(map(int,input().split())) sortedA=sorted(a) if sortedA==a: print("YES") else: fst=0 snd=0 for i in range(len(a)): if i+1!=a[i] and fst==0: fst=i+1 elif i+1!=a[i] and fst!=0: snd=i break fst-=1 a[fst],a[snd]=a[snd],a[fst] if sortedA==a: print("YES") else: print("NO") ``` No
90,416
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Little Elephant has got a problem β€” somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≀ n ≀ 105) β€” the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, β€” array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Submitted Solution: ``` n = int(input()) k = list(map(int,input().split())) kk = sorted(k) count = 0 for i in range(n-1): if k[i] >= k[i+1]: for j in reversed(range(i+1, n)): if (k[j] < k[j-1]): count = 1 k[i],k[j] = k[j],k[i] break if count == 1: break if k == kk: print("YES") else: print("NO") ``` No
90,417
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Little Elephant has got a problem β€” somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array. The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements. Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself. Input The first line contains a single integer n (2 ≀ n ≀ 105) β€” the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, β€” array a. Note that the elements of the array are not necessarily distinct numbers. Output In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise. Examples Input 2 1 2 Output YES Input 3 3 2 1 Output YES Input 4 4 3 2 1 Output NO Note In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES". In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES". In the third sample we can't sort the array in more than one swap operation, so the answer is "NO". Submitted Solution: ``` n, t = int(input()), list(map(int, input().split())) s = sum(t[i] < t[i - 1] for i in range(1, n)) print('YES' if s in [0, 2] else 'NO') ``` No
90,418
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarpus loves lucky numbers. Everybody knows that lucky numbers are positive integers, whose decimal representation (without leading zeroes) contain only the lucky digits x and y. For example, if x = 4, and y = 7, then numbers 47, 744, 4 are lucky. Let's call a positive integer a undoubtedly lucky, if there are such digits x and y (0 ≀ x, y ≀ 9), that the decimal representation of number a (without leading zeroes) contains only digits x and y. Polycarpus has integer n. He wants to know how many positive integers that do not exceed n, are undoubtedly lucky. Help him, count this number. Input The first line contains a single integer n (1 ≀ n ≀ 109) β€” Polycarpus's number. Output Print a single integer that says, how many positive integers that do not exceed n are undoubtedly lucky. Examples Input 10 Output 10 Input 123 Output 113 Note In the first test sample all numbers that do not exceed 10 are undoubtedly lucky. In the second sample numbers 102, 103, 104, 105, 106, 107, 108, 109, 120, 123 are not undoubtedly lucky. Tags: bitmasks, brute force, dfs and similar Correct Solution: ``` from itertools import product n = int(input().strip()) if n<102: print(n) else: res = set() combination = (str(i)+str(j) for i in range(10) for j in range(i+1,10)) for it in (product(comb,repeat=l) for comb in combination for l in range(1,len(str(n))+1)): for line in it: tmp = int("".join(line)) if tmp and tmp<=n: res.add(tmp) print(len(res)) ```
90,419
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarpus loves lucky numbers. Everybody knows that lucky numbers are positive integers, whose decimal representation (without leading zeroes) contain only the lucky digits x and y. For example, if x = 4, and y = 7, then numbers 47, 744, 4 are lucky. Let's call a positive integer a undoubtedly lucky, if there are such digits x and y (0 ≀ x, y ≀ 9), that the decimal representation of number a (without leading zeroes) contains only digits x and y. Polycarpus has integer n. He wants to know how many positive integers that do not exceed n, are undoubtedly lucky. Help him, count this number. Input The first line contains a single integer n (1 ≀ n ≀ 109) β€” Polycarpus's number. Output Print a single integer that says, how many positive integers that do not exceed n are undoubtedly lucky. Examples Input 10 Output 10 Input 123 Output 113 Note In the first test sample all numbers that do not exceed 10 are undoubtedly lucky. In the second sample numbers 102, 103, 104, 105, 106, 107, 108, 109, 120, 123 are not undoubtedly lucky. Tags: bitmasks, brute force, dfs and similar Correct Solution: ``` def p(k): if 0 < k <= n: s.add(k) k *= 10 p(k + x) p(k + y) n = int(input()) s = set() for x in range(10): for y in range(10): p(x) print(len(s)) ```
90,420
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarpus loves lucky numbers. Everybody knows that lucky numbers are positive integers, whose decimal representation (without leading zeroes) contain only the lucky digits x and y. For example, if x = 4, and y = 7, then numbers 47, 744, 4 are lucky. Let's call a positive integer a undoubtedly lucky, if there are such digits x and y (0 ≀ x, y ≀ 9), that the decimal representation of number a (without leading zeroes) contains only digits x and y. Polycarpus has integer n. He wants to know how many positive integers that do not exceed n, are undoubtedly lucky. Help him, count this number. Input The first line contains a single integer n (1 ≀ n ≀ 109) β€” Polycarpus's number. Output Print a single integer that says, how many positive integers that do not exceed n are undoubtedly lucky. Examples Input 10 Output 10 Input 123 Output 113 Note In the first test sample all numbers that do not exceed 10 are undoubtedly lucky. In the second sample numbers 102, 103, 104, 105, 106, 107, 108, 109, 120, 123 are not undoubtedly lucky. Tags: bitmasks, brute force, dfs and similar Correct Solution: ``` # https://codeforces.com/contest/244/problem/B def gen(digit, x, length, S): S.append(x) if length == 10: return if len(digit) == 1: for i in range(0, 10): next_x = x * 10 + i if i == digit[0]: gen(digit, next_x, length+1, S) else: gen(digit+[i], next_x, length+1, S) else: next_x1 = x * 10 + digit[0] next_x2 = x * 10 + digit[1] gen(digit, next_x1, length+1, S) gen(digit, next_x2, length+1, S) S = [] for i in range(1, 10): gen([i], i, 1, S) S = sorted(S) x = int(input()) l = -1 u = len(S) while u-l>1: md = (u+l) // 2 if x >= S[md]: l = md else: u = md print(u) ```
90,421
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarpus loves lucky numbers. Everybody knows that lucky numbers are positive integers, whose decimal representation (without leading zeroes) contain only the lucky digits x and y. For example, if x = 4, and y = 7, then numbers 47, 744, 4 are lucky. Let's call a positive integer a undoubtedly lucky, if there are such digits x and y (0 ≀ x, y ≀ 9), that the decimal representation of number a (without leading zeroes) contains only digits x and y. Polycarpus has integer n. He wants to know how many positive integers that do not exceed n, are undoubtedly lucky. Help him, count this number. Input The first line contains a single integer n (1 ≀ n ≀ 109) β€” Polycarpus's number. Output Print a single integer that says, how many positive integers that do not exceed n are undoubtedly lucky. Examples Input 10 Output 10 Input 123 Output 113 Note In the first test sample all numbers that do not exceed 10 are undoubtedly lucky. In the second sample numbers 102, 103, 104, 105, 106, 107, 108, 109, 120, 123 are not undoubtedly lucky. Tags: bitmasks, brute force, dfs and similar Correct Solution: ``` #CF Round 150. Div II Prob. A - Dividing Orange import sys dp = [[[-1 for j in range(3)] for i in range (1 << 10)] for k in range(11)] In = sys.stdin n = In.readline().strip() def go (idx, mask, equal): if dp[idx][mask][equal] != -1: return dp[idx][mask][equal] if bin(mask).count("1") > 2: return 0 if idx == len(n): return 1 res = 0 if idx == 0 or equal == 2: res += go(idx + 1, mask, 2) elif equal == 1 and int(n[idx]) == 0: res += go(idx + 1, mask | 1, 1) else: res += go(idx + 1, mask | 1, 0) for i in range(1, 10): if equal == 1 and i > int(n[idx]): break elif equal == 1 and i == int(n[idx]): res += go(idx + 1, mask | (1 << i), 1) else: res += go(idx + 1, mask | (1 << i), 0) dp[idx][mask][equal] = res return res print(go(0, 0, 1) - 1) ```
90,422
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarpus loves lucky numbers. Everybody knows that lucky numbers are positive integers, whose decimal representation (without leading zeroes) contain only the lucky digits x and y. For example, if x = 4, and y = 7, then numbers 47, 744, 4 are lucky. Let's call a positive integer a undoubtedly lucky, if there are such digits x and y (0 ≀ x, y ≀ 9), that the decimal representation of number a (without leading zeroes) contains only digits x and y. Polycarpus has integer n. He wants to know how many positive integers that do not exceed n, are undoubtedly lucky. Help him, count this number. Input The first line contains a single integer n (1 ≀ n ≀ 109) β€” Polycarpus's number. Output Print a single integer that says, how many positive integers that do not exceed n are undoubtedly lucky. Examples Input 10 Output 10 Input 123 Output 113 Note In the first test sample all numbers that do not exceed 10 are undoubtedly lucky. In the second sample numbers 102, 103, 104, 105, 106, 107, 108, 109, 120, 123 are not undoubtedly lucky. Tags: bitmasks, brute force, dfs and similar Correct Solution: ``` def dfs(k): if 0 < k <= n: s.add(k) k *= 10 dfs(k + x) dfs(k + y) n = int(input()) s = set() for x in range(10): for y in range(10): dfs(x) print(len(s)) ```
90,423
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarpus loves lucky numbers. Everybody knows that lucky numbers are positive integers, whose decimal representation (without leading zeroes) contain only the lucky digits x and y. For example, if x = 4, and y = 7, then numbers 47, 744, 4 are lucky. Let's call a positive integer a undoubtedly lucky, if there are such digits x and y (0 ≀ x, y ≀ 9), that the decimal representation of number a (without leading zeroes) contains only digits x and y. Polycarpus has integer n. He wants to know how many positive integers that do not exceed n, are undoubtedly lucky. Help him, count this number. Input The first line contains a single integer n (1 ≀ n ≀ 109) β€” Polycarpus's number. Output Print a single integer that says, how many positive integers that do not exceed n are undoubtedly lucky. Examples Input 10 Output 10 Input 123 Output 113 Note In the first test sample all numbers that do not exceed 10 are undoubtedly lucky. In the second sample numbers 102, 103, 104, 105, 106, 107, 108, 109, 120, 123 are not undoubtedly lucky. Tags: bitmasks, brute force, dfs and similar Correct Solution: ``` def findNum(num): if 0 < num <= n: s.add(num) num*=10 findNum(num+x) findNum(num+y) n = int(input()) s = set() for x in range(10): for y in range(10): findNum(x) print(len(s)) ```
90,424
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarpus loves lucky numbers. Everybody knows that lucky numbers are positive integers, whose decimal representation (without leading zeroes) contain only the lucky digits x and y. For example, if x = 4, and y = 7, then numbers 47, 744, 4 are lucky. Let's call a positive integer a undoubtedly lucky, if there are such digits x and y (0 ≀ x, y ≀ 9), that the decimal representation of number a (without leading zeroes) contains only digits x and y. Polycarpus has integer n. He wants to know how many positive integers that do not exceed n, are undoubtedly lucky. Help him, count this number. Input The first line contains a single integer n (1 ≀ n ≀ 109) β€” Polycarpus's number. Output Print a single integer that says, how many positive integers that do not exceed n are undoubtedly lucky. Examples Input 10 Output 10 Input 123 Output 113 Note In the first test sample all numbers that do not exceed 10 are undoubtedly lucky. In the second sample numbers 102, 103, 104, 105, 106, 107, 108, 109, 120, 123 are not undoubtedly lucky. Tags: bitmasks, brute force, dfs and similar Correct Solution: ``` n=int(input()) def bruteforce(number): res=0 liste=[] while number>0: integer=number%10 if integer not in liste: liste.append(integer) number=number//10 return len(liste)<=2 answer=[0] def dfs(num): if (num>0 and num<=n): answer[0]+=1 for a in range(10): if bruteforce(num*10+a): dfs(num*10+a) for chiffre in range(1,10): dfs(chiffre) print(answer[0]) ```
90,425
Provide tags and a correct Python 3 solution for this coding contest problem. Polycarpus loves lucky numbers. Everybody knows that lucky numbers are positive integers, whose decimal representation (without leading zeroes) contain only the lucky digits x and y. For example, if x = 4, and y = 7, then numbers 47, 744, 4 are lucky. Let's call a positive integer a undoubtedly lucky, if there are such digits x and y (0 ≀ x, y ≀ 9), that the decimal representation of number a (without leading zeroes) contains only digits x and y. Polycarpus has integer n. He wants to know how many positive integers that do not exceed n, are undoubtedly lucky. Help him, count this number. Input The first line contains a single integer n (1 ≀ n ≀ 109) β€” Polycarpus's number. Output Print a single integer that says, how many positive integers that do not exceed n are undoubtedly lucky. Examples Input 10 Output 10 Input 123 Output 113 Note In the first test sample all numbers that do not exceed 10 are undoubtedly lucky. In the second sample numbers 102, 103, 104, 105, 106, 107, 108, 109, 120, 123 are not undoubtedly lucky. Tags: bitmasks, brute force, dfs and similar Correct Solution: ``` ii=lambda:int(input()) kk=lambda:map(int, input().split()) ll=lambda:list(kk()) q =[] s=set() n=ii() for x in range(10): for y in range(10): q.append(x) while q: q2 = [] for item in q: if item > 0 and item <= n: s.add(item) item*=10 q2.append(item+x) q2.append(item+y) q=q2 print(len(s)) ```
90,426
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarpus loves lucky numbers. Everybody knows that lucky numbers are positive integers, whose decimal representation (without leading zeroes) contain only the lucky digits x and y. For example, if x = 4, and y = 7, then numbers 47, 744, 4 are lucky. Let's call a positive integer a undoubtedly lucky, if there are such digits x and y (0 ≀ x, y ≀ 9), that the decimal representation of number a (without leading zeroes) contains only digits x and y. Polycarpus has integer n. He wants to know how many positive integers that do not exceed n, are undoubtedly lucky. Help him, count this number. Input The first line contains a single integer n (1 ≀ n ≀ 109) β€” Polycarpus's number. Output Print a single integer that says, how many positive integers that do not exceed n are undoubtedly lucky. Examples Input 10 Output 10 Input 123 Output 113 Note In the first test sample all numbers that do not exceed 10 are undoubtedly lucky. In the second sample numbers 102, 103, 104, 105, 106, 107, 108, 109, 120, 123 are not undoubtedly lucky. Submitted Solution: ``` #!/usr/bin/python3.6 # -*- coding: utf-8 -*- # @Time : 2020/12/4 1:44 PM # @Author : Songtao Li s = set() def DFS(x, y, num, max_n): s.add(num) num_x = 10 * num + x num_y = 10 * num + y if num_x and num_x <= max_n: DFS(x, y, num_x, max_n) if num_y and num_y <= max_n: DFS(x, y, num_y, max_n) if __name__ == "__main__": n = int(input()) for i in range(10): for j in range(i, 10): DFS(i, j, 0, n) print(len(s)-1) ``` Yes
90,427
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarpus loves lucky numbers. Everybody knows that lucky numbers are positive integers, whose decimal representation (without leading zeroes) contain only the lucky digits x and y. For example, if x = 4, and y = 7, then numbers 47, 744, 4 are lucky. Let's call a positive integer a undoubtedly lucky, if there are such digits x and y (0 ≀ x, y ≀ 9), that the decimal representation of number a (without leading zeroes) contains only digits x and y. Polycarpus has integer n. He wants to know how many positive integers that do not exceed n, are undoubtedly lucky. Help him, count this number. Input The first line contains a single integer n (1 ≀ n ≀ 109) β€” Polycarpus's number. Output Print a single integer that says, how many positive integers that do not exceed n are undoubtedly lucky. Examples Input 10 Output 10 Input 123 Output 113 Note In the first test sample all numbers that do not exceed 10 are undoubtedly lucky. In the second sample numbers 102, 103, 104, 105, 106, 107, 108, 109, 120, 123 are not undoubtedly lucky. Submitted Solution: ``` def cnt(s, p): ans = 0 if p > s: ans = 0 elif len(p) == len(s): ans = 1 if len(set(p.lstrip('0'))) <= 2 else 0 elif len(set(p.lstrip('0'))) > 2: ans = 0 elif s[:len(p)] > p: if len(set(p.lstrip('0'))) == 2: ans = 2**(len(s)-len(p)) elif len(set(p.lstrip('0'))) == 1: ans = 1 + 9 * (2**(len(s)-len(p)) - 1) else: # ab for all a, b != 0 ans = 10 + 45 * (2**(len(s)-len(p)) - 2) ans += 36 * sum([2**l-2 for l in range(2,len(s)-len(p))]) else: ans = sum(cnt(s, p+c) for c in '0123456789') return ans print(cnt(input().strip(), '')-1) # Made By Mostafa_Khaled ``` Yes
90,428
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarpus loves lucky numbers. Everybody knows that lucky numbers are positive integers, whose decimal representation (without leading zeroes) contain only the lucky digits x and y. For example, if x = 4, and y = 7, then numbers 47, 744, 4 are lucky. Let's call a positive integer a undoubtedly lucky, if there are such digits x and y (0 ≀ x, y ≀ 9), that the decimal representation of number a (without leading zeroes) contains only digits x and y. Polycarpus has integer n. He wants to know how many positive integers that do not exceed n, are undoubtedly lucky. Help him, count this number. Input The first line contains a single integer n (1 ≀ n ≀ 109) β€” Polycarpus's number. Output Print a single integer that says, how many positive integers that do not exceed n are undoubtedly lucky. Examples Input 10 Output 10 Input 123 Output 113 Note In the first test sample all numbers that do not exceed 10 are undoubtedly lucky. In the second sample numbers 102, 103, 104, 105, 106, 107, 108, 109, 120, 123 are not undoubtedly lucky. Submitted Solution: ``` #d=sorted(d,key=lambda x:(len(d[x]),-x)) d=dictionary d={x:set() for x in arr} #n=int(input()) #n,m,k= map(int, input().split()) import heapq #for _ in range(int(input())): #n,k=map(int, input().split()) #input=sys.stdin.buffer.readline #for _ in range(int(input())): def dfs(x): if 0< x <=n : s.add(x) x*=10 dfs(x+i) dfs(x+j) n=int(input()) #arr = list(map(int, input().split())) s=set() for i in range(1,10): for j in range(10): dfs(i) print(len(s)) ``` Yes
90,429
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarpus loves lucky numbers. Everybody knows that lucky numbers are positive integers, whose decimal representation (without leading zeroes) contain only the lucky digits x and y. For example, if x = 4, and y = 7, then numbers 47, 744, 4 are lucky. Let's call a positive integer a undoubtedly lucky, if there are such digits x and y (0 ≀ x, y ≀ 9), that the decimal representation of number a (without leading zeroes) contains only digits x and y. Polycarpus has integer n. He wants to know how many positive integers that do not exceed n, are undoubtedly lucky. Help him, count this number. Input The first line contains a single integer n (1 ≀ n ≀ 109) β€” Polycarpus's number. Output Print a single integer that says, how many positive integers that do not exceed n are undoubtedly lucky. Examples Input 10 Output 10 Input 123 Output 113 Note In the first test sample all numbers that do not exceed 10 are undoubtedly lucky. In the second sample numbers 102, 103, 104, 105, 106, 107, 108, 109, 120, 123 are not undoubtedly lucky. Submitted Solution: ``` n=int(input()) a=[] def foo(x,i,j): if x>n: return if x: a.append(x) if 10*x+i!=x: foo(10*x+i,i,j) foo(10*x+j,i,j) for i in range(10): for j in range(i+1,10): foo(0,i,j) print(len(set(a))) ``` Yes
90,430
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarpus loves lucky numbers. Everybody knows that lucky numbers are positive integers, whose decimal representation (without leading zeroes) contain only the lucky digits x and y. For example, if x = 4, and y = 7, then numbers 47, 744, 4 are lucky. Let's call a positive integer a undoubtedly lucky, if there are such digits x and y (0 ≀ x, y ≀ 9), that the decimal representation of number a (without leading zeroes) contains only digits x and y. Polycarpus has integer n. He wants to know how many positive integers that do not exceed n, are undoubtedly lucky. Help him, count this number. Input The first line contains a single integer n (1 ≀ n ≀ 109) β€” Polycarpus's number. Output Print a single integer that says, how many positive integers that do not exceed n are undoubtedly lucky. Examples Input 10 Output 10 Input 123 Output 113 Note In the first test sample all numbers that do not exceed 10 are undoubtedly lucky. In the second sample numbers 102, 103, 104, 105, 106, 107, 108, 109, 120, 123 are not undoubtedly lucky. Submitted Solution: ``` global ans ans = 0; global n def dfs(num): global ans if num > n : return a = [0]*10 k = num while k != 0 : a[int(k%10)] += 1 k = k / 10 cnt = 0 for i in range(10): if a[i] > 0 : cnt += 1 if cnt > 2 : return if num > 0 : ans += 1 for i in range(10): if i == 0 and num == 0 : continue dfs(num*10 + i) global n n = int(input()) dfs(0) print(ans) ``` No
90,431
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarpus loves lucky numbers. Everybody knows that lucky numbers are positive integers, whose decimal representation (without leading zeroes) contain only the lucky digits x and y. For example, if x = 4, and y = 7, then numbers 47, 744, 4 are lucky. Let's call a positive integer a undoubtedly lucky, if there are such digits x and y (0 ≀ x, y ≀ 9), that the decimal representation of number a (without leading zeroes) contains only digits x and y. Polycarpus has integer n. He wants to know how many positive integers that do not exceed n, are undoubtedly lucky. Help him, count this number. Input The first line contains a single integer n (1 ≀ n ≀ 109) β€” Polycarpus's number. Output Print a single integer that says, how many positive integers that do not exceed n are undoubtedly lucky. Examples Input 10 Output 10 Input 123 Output 113 Note In the first test sample all numbers that do not exceed 10 are undoubtedly lucky. In the second sample numbers 102, 103, 104, 105, 106, 107, 108, 109, 120, 123 are not undoubtedly lucky. Submitted Solution: ``` from random import randint n = int(input()) ses = set() for c1 in range(10): for c2 in range(c1 + 1, 10): C = [c1, c2] for _ in range(4000): kek = C[randint(0, 1)] while kek <= n: ses.add(kek) kek *= 10; kek += C[randint(0, 1)] print(len(ses)-1) ``` No
90,432
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarpus loves lucky numbers. Everybody knows that lucky numbers are positive integers, whose decimal representation (without leading zeroes) contain only the lucky digits x and y. For example, if x = 4, and y = 7, then numbers 47, 744, 4 are lucky. Let's call a positive integer a undoubtedly lucky, if there are such digits x and y (0 ≀ x, y ≀ 9), that the decimal representation of number a (without leading zeroes) contains only digits x and y. Polycarpus has integer n. He wants to know how many positive integers that do not exceed n, are undoubtedly lucky. Help him, count this number. Input The first line contains a single integer n (1 ≀ n ≀ 109) β€” Polycarpus's number. Output Print a single integer that says, how many positive integers that do not exceed n are undoubtedly lucky. Examples Input 10 Output 10 Input 123 Output 113 Note In the first test sample all numbers that do not exceed 10 are undoubtedly lucky. In the second sample numbers 102, 103, 104, 105, 106, 107, 108, 109, 120, 123 are not undoubtedly lucky. Submitted Solution: ``` import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time sys.setrecursionlimit(10**7) inf = 10**9 n = int(input()) if n < 100: print(n) sys.exit() a = [[_,set(map(int,list(str(_))))] for _ in range(100)] c = 99 for k in range(2,11): t = (10 ** (k-1)) for aa in a: if aa[0] >= t: break aa[1] |= set([0]) for i in range(1,10): t = i * (10 ** k) for ai, ae in a[:]: ac = ai + t if ac > n: print(c) sys.exit() if len(ae) < 2: ae = ae | set([i]) if i in ae: c += 1 a.append([ac,ae]) ``` No
90,433
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Polycarpus loves lucky numbers. Everybody knows that lucky numbers are positive integers, whose decimal representation (without leading zeroes) contain only the lucky digits x and y. For example, if x = 4, and y = 7, then numbers 47, 744, 4 are lucky. Let's call a positive integer a undoubtedly lucky, if there are such digits x and y (0 ≀ x, y ≀ 9), that the decimal representation of number a (without leading zeroes) contains only digits x and y. Polycarpus has integer n. He wants to know how many positive integers that do not exceed n, are undoubtedly lucky. Help him, count this number. Input The first line contains a single integer n (1 ≀ n ≀ 109) β€” Polycarpus's number. Output Print a single integer that says, how many positive integers that do not exceed n are undoubtedly lucky. Examples Input 10 Output 10 Input 123 Output 113 Note In the first test sample all numbers that do not exceed 10 are undoubtedly lucky. In the second sample numbers 102, 103, 104, 105, 106, 107, 108, 109, 120, 123 are not undoubtedly lucky. Submitted Solution: ``` from random import randint n = int(input()) ses = set() for c1 in range(10): for c2 in range(c1 + 1, 10): C = [c1, c2] for _ in range(4100): kek = C[randint(0, 1)] while kek <= n: ses.add(kek) kek *= 10; kek += C[randint(0, 1)] print(len(ses)-1) ``` No
90,434
Provide tags and a correct Python 3 solution for this coding contest problem. This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution. Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computers Polycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices. Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node. Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. <image> (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown. Input The first line contains two space-separated integers n and m (4 ≀ n ≀ 105; 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself. Output In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes). Examples Input 4 3 1 2 2 3 3 4 Output bus topology Input 4 4 1 2 2 3 3 4 4 1 Output ring topology Input 4 3 1 2 1 3 1 4 Output star topology Input 4 4 1 2 2 3 3 1 1 4 Output unknown topology Tags: graphs, implementation Correct Solution: ``` from collections import Counter n, m = map(int, input().split()) g = [[] for _ in range(n)] for i in range(m): x, y = map(int, input().split()) g[x - 1].append(y - 1) g[y - 1].append(x - 1) bus = ([2] * (n - 2)) + ([1] * 2) star = [1] * (n - 1) + [n - 1] lns = [len(lst) for lst in g] if lns == [2] * n: print("ring topology") elif Counter(lns) == Counter(bus): print("bus topology") elif Counter(lns) == Counter(star): print("star topology") else: print("unknown topology") ```
90,435
Provide tags and a correct Python 3 solution for this coding contest problem. This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution. Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computers Polycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices. Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node. Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. <image> (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown. Input The first line contains two space-separated integers n and m (4 ≀ n ≀ 105; 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself. Output In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes). Examples Input 4 3 1 2 2 3 3 4 Output bus topology Input 4 4 1 2 2 3 3 4 4 1 Output ring topology Input 4 3 1 2 1 3 1 4 Output star topology Input 4 4 1 2 2 3 3 1 1 4 Output unknown topology Tags: graphs, implementation Correct Solution: ``` from collections import defaultdict I = lambda: map(int, input().split()) degrees = defaultdict(int) n, m = I() for _ in range(m): u, v = I() degrees[u] += 1 degrees[v] += 1 values = set(degrees.values()) answer = 'unknown' if values == {2}: answer = 'ring' elif values == {1, 2}: answer = 'bus' elif values == {1, m}: answer = 'star' print(answer, 'topology') ```
90,436
Provide tags and a correct Python 3 solution for this coding contest problem. This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution. Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computers Polycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices. Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node. Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. <image> (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown. Input The first line contains two space-separated integers n and m (4 ≀ n ≀ 105; 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself. Output In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes). Examples Input 4 3 1 2 2 3 3 4 Output bus topology Input 4 4 1 2 2 3 3 4 4 1 Output ring topology Input 4 3 1 2 1 3 1 4 Output star topology Input 4 4 1 2 2 3 3 1 1 4 Output unknown topology Tags: graphs, implementation Correct Solution: ``` n, m = map(int, input().split()) graph = { i: [] for i in range(1, n+1) } for i in range(m): start, end = map(int, input().split()) graph[start].append(end) graph[end].append(start) length_array = [] for i in graph: length_array.append(len(graph[i])) if min(length_array) == max(length_array) and max(length_array) == 2: print('ring topology') elif length_array.count(1) == 2 and length_array.count(2) == n - 2: print('bus topology') elif max(length_array) == m and m == n - 1: print('star topology') else: print('unknown topology') ```
90,437
Provide tags and a correct Python 3 solution for this coding contest problem. This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution. Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computers Polycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices. Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node. Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. <image> (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown. Input The first line contains two space-separated integers n and m (4 ≀ n ≀ 105; 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself. Output In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes). Examples Input 4 3 1 2 2 3 3 4 Output bus topology Input 4 4 1 2 2 3 3 4 4 1 Output ring topology Input 4 3 1 2 1 3 1 4 Output star topology Input 4 4 1 2 2 3 3 1 1 4 Output unknown topology Tags: graphs, implementation Correct Solution: ``` n, m = [int(i) for i in input().split()] s = [] for i in range(n + 1): s.append([]) for i in range(m): x, y = [int(i) for i in input().split()] s[x].append(y) s[y].append(x) sbool = [False] * (n + 1) v2 = 0 v1 = 0 other = 0 def dfs(new): global sbool, s, v2, v1, other sbool[new] = True if len(s[new]) == 1: v1 += 1 elif len(s[new]) == 2: v2 += 1 else: other += 1 for i in range(1, n + 1): dfs(i) if v1 == 2 and v2 == n - 2: print('bus topology') elif v1 == n - 1 and other == 1: print('star topology') elif v2 == n: print('ring topology') else: print('unknown topology') ```
90,438
Provide tags and a correct Python 3 solution for this coding contest problem. This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution. Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computers Polycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices. Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node. Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. <image> (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown. Input The first line contains two space-separated integers n and m (4 ≀ n ≀ 105; 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself. Output In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes). Examples Input 4 3 1 2 2 3 3 4 Output bus topology Input 4 4 1 2 2 3 3 4 4 1 Output ring topology Input 4 3 1 2 1 3 1 4 Output star topology Input 4 4 1 2 2 3 3 1 1 4 Output unknown topology Tags: graphs, implementation Correct Solution: ``` n, m = [int(i) for i in input().split()] graf = [[] for i in range(n + 1)] for i in range(m): k1, k2 = [int(i) for i in input().split()] graf[k1].append(k2) graf[k2].append(k1) seks = [0, 0, 0] for i in range(1, n + 1): if len(graf[i]) == 1 or len(graf[i]) == 2: seks[len(graf[i])] += 1 else: seks[0] += 1 if seks[0] == 0 and seks[1] == 2: print('bus topology') elif seks[0] == seks[1] == 0: print('ring topology') elif seks[0] == 1 and seks[2] == 0: print('star topology') else: print('unknown topology') ```
90,439
Provide tags and a correct Python 3 solution for this coding contest problem. This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution. Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computers Polycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices. Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node. Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. <image> (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown. Input The first line contains two space-separated integers n and m (4 ≀ n ≀ 105; 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself. Output In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes). Examples Input 4 3 1 2 2 3 3 4 Output bus topology Input 4 4 1 2 2 3 3 4 4 1 Output ring topology Input 4 3 1 2 1 3 1 4 Output star topology Input 4 4 1 2 2 3 3 1 1 4 Output unknown topology Tags: graphs, implementation Correct Solution: ``` n, m = map(int, input().split()) d = dict() maxi = 0 maxv = 0 for _ in range(m): a, b = map(int, input().split()) if a - 1 in d: d[a - 1] += 1 else: d[a - 1] = 1 if b - 1 in d: d[b - 1] += 1 else: d[b - 1] = 1 if d[a - 1] > maxv: maxv = d[a - 1] maxi = a - 1 if d[b - 1] > maxv: maxv = d[b - 1] maxi = b - 1 if m + 1 == n: if maxv == m: print('star topology') elif maxv == 2: print('bus topology') else: print('unknown topology') elif m == n: if maxv == 2: print('ring topology') else: print('unknown topology') else: print('unknown topology') ```
90,440
Provide tags and a correct Python 3 solution for this coding contest problem. This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution. Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computers Polycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices. Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node. Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. <image> (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown. Input The first line contains two space-separated integers n and m (4 ≀ n ≀ 105; 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself. Output In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes). Examples Input 4 3 1 2 2 3 3 4 Output bus topology Input 4 4 1 2 2 3 3 4 4 1 Output ring topology Input 4 3 1 2 1 3 1 4 Output star topology Input 4 4 1 2 2 3 3 1 1 4 Output unknown topology Tags: graphs, implementation Correct Solution: ``` n,m = map(int,input().split()) v = [] def chek_star(v): t = 0 for i in v: if i != 1: t+=1 if t == 1: return True return False for i in range(n): v.append(0) for i in range(m): t1,t2 = map(int,input().split()) v[t1-1]+=1 v[t2-1]+=1 if max(v) == 2 and min(v) == 2: print("ring topology") elif set(v) == set([1,2]): print("bus topology") elif set(v) == set([1,max(v)]) and chek_star(v): print("star topology") else: print("unknown topology") ```
90,441
Provide tags and a correct Python 3 solution for this coding contest problem. This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution. Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computers Polycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices. Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node. Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. <image> (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown. Input The first line contains two space-separated integers n and m (4 ≀ n ≀ 105; 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself. Output In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes). Examples Input 4 3 1 2 2 3 3 4 Output bus topology Input 4 4 1 2 2 3 3 4 4 1 Output ring topology Input 4 3 1 2 1 3 1 4 Output star topology Input 4 4 1 2 2 3 3 1 1 4 Output unknown topology Tags: graphs, implementation Correct Solution: ``` # brute force baby from sys import stdin node,edge = map(int,input().split()) capt = [0]*node for l in stdin.readlines(): a,b = map(int,l.split()) capt[a-1] +=1; capt[b-1] += 1 if capt.count(1) == node - 1: print('star topology') elif capt.count(2) == node: print('ring topology') elif capt.count(1) == 2 and capt.count(2) == node - 2: print('bus topology') else: print('unknown topology') ```
90,442
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution. Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computers Polycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices. Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node. Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. <image> (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown. Input The first line contains two space-separated integers n and m (4 ≀ n ≀ 105; 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself. Output In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes). Examples Input 4 3 1 2 2 3 3 4 Output bus topology Input 4 4 1 2 2 3 3 4 4 1 Output ring topology Input 4 3 1 2 1 3 1 4 Output star topology Input 4 4 1 2 2 3 3 1 1 4 Output unknown topology Submitted Solution: ``` from functools import reduce def add(a,b): return a+b n,m = [int(x) for x in input().split()] neigh = [[] for x in range(n)] for i in range(m): x,y = [int(a) for a in input().split()] neigh[x-1].append(y) neigh[y-1].append(x) oneneigh, twoneigh, nneigh = 0,0,0 for i in neigh: if len(i) == 1: oneneigh += 1 elif len(i) == 2: twoneigh += 1 else: nneigh += 1 if twoneigh == n: print('ring topology') elif oneneigh == 2 and twoneigh == n-2: print('bus topology') elif nneigh == 1 and oneneigh == m: print('star topology') else: print('unknown topology') ``` Yes
90,443
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution. Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computers Polycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices. Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node. Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. <image> (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown. Input The first line contains two space-separated integers n and m (4 ≀ n ≀ 105; 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself. Output In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes). Examples Input 4 3 1 2 2 3 3 4 Output bus topology Input 4 4 1 2 2 3 3 4 4 1 Output ring topology Input 4 3 1 2 1 3 1 4 Output star topology Input 4 4 1 2 2 3 3 1 1 4 Output unknown topology Submitted Solution: ``` n,m = map(int, input().split()) db = [[] for i in range(n)] for i in range(m): x,y = map(int, input().split()) db[x-1].append(y) db[y-1].append(x) one = 0 two = 0 mx = 0 for i in range(n): ln = len(db[i]) if ln == 1: one += 1 elif ln == 2: two += 1 mx = max(mx,ln) if one == 2 and two == n-2: print("bus topology") elif two == n: print("ring topology") elif one == n-1 and mx == n-1: print("star topology") else: print("unknown topology") ``` Yes
90,444
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution. Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computers Polycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices. Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node. Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. <image> (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown. Input The first line contains two space-separated integers n and m (4 ≀ n ≀ 105; 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself. Output In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes). Examples Input 4 3 1 2 2 3 3 4 Output bus topology Input 4 4 1 2 2 3 3 4 4 1 Output ring topology Input 4 3 1 2 1 3 1 4 Output star topology Input 4 4 1 2 2 3 3 1 1 4 Output unknown topology Submitted Solution: ``` n,m = map(int, input().split()) adj = [[] for i in range(n+1)] for i in range(m): a,b = map(int, input().split()) adj[a].append(b) adj[b].append(a) edgeCount = [0] for i in range(1,n+1): edgeCount.append(len(adj[i])) edgeCountCount = [0 for i in range(n+1)] for i in edgeCount: edgeCountCount[i] += 1 if edgeCountCount[1] == 2 and edgeCountCount[2] == n-2: print('bus topology') elif edgeCountCount[2] == m: print('ring topology') elif edgeCountCount[1] == m and edgeCountCount[m] == 1: print('star topology') else: print('unknown topology') ``` Yes
90,445
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution. Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computers Polycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices. Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node. Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. <image> (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown. Input The first line contains two space-separated integers n and m (4 ≀ n ≀ 105; 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself. Output In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes). Examples Input 4 3 1 2 2 3 3 4 Output bus topology Input 4 4 1 2 2 3 3 4 4 1 Output ring topology Input 4 3 1 2 1 3 1 4 Output star topology Input 4 4 1 2 2 3 3 1 1 4 Output unknown topology Submitted Solution: ``` n,m=map(int,input().split()) graph={} for i in range(1,n+1): graph[i]=[] for i in range(m): u,v=map(int,input().split()) graph[u].append(v) graph[v].append(u) c=0 d=0 #print(graph) for i in range(1,n+1): if len(graph[i])==1: c=c+1 elif len(graph[i])>=2: d=d+1 if m==n-1 and c==2 and d==n-2: print("bus topology") elif c==0 and d==n and m==n: print("ring topology") elif c==n-1 and d==1: print("star topology") else: print("unknown topology") ``` Yes
90,446
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution. Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computers Polycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices. Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node. Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. <image> (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown. Input The first line contains two space-separated integers n and m (4 ≀ n ≀ 105; 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself. Output In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes). Examples Input 4 3 1 2 2 3 3 4 Output bus topology Input 4 4 1 2 2 3 3 4 4 1 Output ring topology Input 4 3 1 2 1 3 1 4 Output star topology Input 4 4 1 2 2 3 3 1 1 4 Output unknown topology Submitted Solution: ``` from collections import defaultdict I = lambda: list(map(int, input().split())) degrees = defaultdict(int) for _ in range(I()[1]): u, v = I() degrees[u] += 1 degrees[v] += 1 values = set(degrees.values()) if values == {1, 2}: print('bus topology') elif values == {2}: print('ring topology') elif len(values) == 2 and 1 in values: print('star topology') else: print('unknown topology') ``` No
90,447
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution. Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computers Polycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices. Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node. Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. <image> (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown. Input The first line contains two space-separated integers n and m (4 ≀ n ≀ 105; 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself. Output In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes). Examples Input 4 3 1 2 2 3 3 4 Output bus topology Input 4 4 1 2 2 3 3 4 4 1 Output ring topology Input 4 3 1 2 1 3 1 4 Output star topology Input 4 4 1 2 2 3 3 1 1 4 Output unknown topology Submitted Solution: ``` from collections import defaultdict n, m = map(int, input().split()) h = defaultdict(set) nums = [0] * n for i in range(m): u, v = map(int, input().split()) h[u].add(v) h[v].add(u) full = 0 one = 0 two = 0 for key, value in h.items(): if len(value) == n - 1: full += 1 if len(value) == 1: one += 1 if len(value) == 2: two += 1 if full == 1 and full + one == n: print('star topology') elif two == n: print('ring topology') elif two == n - 2 and one == 2: print('bus topology') else: print('unkown topology') ``` No
90,448
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution. Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computers Polycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices. Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node. Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. <image> (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown. Input The first line contains two space-separated integers n and m (4 ≀ n ≀ 105; 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself. Output In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes). Examples Input 4 3 1 2 2 3 3 4 Output bus topology Input 4 4 1 2 2 3 3 4 4 1 Output ring topology Input 4 3 1 2 1 3 1 4 Output star topology Input 4 4 1 2 2 3 3 1 1 4 Output unknown topology Submitted Solution: ``` n, m = [int(i) for i in input().split()] nodes = [] edges = [] x = 0 for i in range(m): nums = [int(i) for i in input().split()] nodes.append(nums[0]) edges.append(nums[1]) if sum(nodes) == len(nodes): print('star topology') elif sum(nodes) == sum(edges): print('ring topology') elif x == 0: for i in range(m): if nodes[i] + 1 == edges[i]: x += 1 if x == m: print('bus topology') else: print('unknown topology') ``` No
90,449
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution. Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computers Polycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices. Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node. Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. <image> (1) β€” bus, (2) β€” ring, (3) β€” star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown. Input The first line contains two space-separated integers n and m (4 ≀ n ≀ 105; 3 ≀ m ≀ 105) β€” the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 ≀ xi, yi ≀ n) β€” the numbers of nodes that are connected by the i-the edge. It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself. Output In a single line print the network topology name of the given graph. If the answer is the bus, print "bus topology" (without the quotes), if the answer is the ring, print "ring topology" (without the quotes), if the answer is the star, print "star topology" (without the quotes). If no answer fits, print "unknown topology" (without the quotes). Examples Input 4 3 1 2 2 3 3 4 Output bus topology Input 4 4 1 2 2 3 3 4 4 1 Output ring topology Input 4 3 1 2 1 3 1 4 Output star topology Input 4 4 1 2 2 3 3 1 1 4 Output unknown topology Submitted Solution: ``` n, m = map(int, input().split()) s = [set() for _ in range(n+1)] for _ in range(m): a, b = map(int, input().split()) s[a].add(b) s[b].add(a) x = set() for i in range(1, n+1): if len(s[i]) == 1: x.add(i) if len(x) == 0: print("ring topology") elif x == {1, n}: print("bus topology") elif len(x) == m: print("star topology") else: print("unknown topology") ``` No
90,450
Provide tags and a correct Python 3 solution for this coding contest problem. There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights. <image> Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1] Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible. Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic). Input The first line of the input contains integers n and k (1 ≀ n ≀ 1.5Β·105, 1 ≀ k ≀ n) β€” the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn (1 ≀ hi ≀ 100), where hi is the height of the i-th plank of the fence. Output Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them. Examples Input 7 3 1 2 6 1 1 7 1 Output 3 Note In the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8. Tags: brute force, dp Correct Solution: ``` # cf 363 B 1200 n, k = map(int, input().split()) A = [*map(int, input().split())] wsz = 0 wsum = 0 min_ = float("inf") mini = 0 for i in range(len(A)): wsum += A[i] wsz += 1 if wsz > k: wsum -= A[1 + i - wsz] wsz -= 1 if wsum < min_ and wsz == k: min_ = wsum mini = 1 + i - wsz print(1 + mini) ```
90,451
Provide tags and a correct Python 3 solution for this coding contest problem. There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights. <image> Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1] Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible. Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic). Input The first line of the input contains integers n and k (1 ≀ n ≀ 1.5Β·105, 1 ≀ k ≀ n) β€” the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn (1 ≀ hi ≀ 100), where hi is the height of the i-th plank of the fence. Output Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them. Examples Input 7 3 1 2 6 1 1 7 1 Output 3 Note In the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8. Tags: brute force, dp Correct Solution: ``` a,b=map(int,input().split());c=list(map(int,input().split()));s=k=sum(c[:b]);j=0 for i in range(1,a-b+1): n=s-c[i-1]+c[i+b-1] if k>n:j=i;k=n s=n print(j+1) ```
90,452
Provide tags and a correct Python 3 solution for this coding contest problem. There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights. <image> Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1] Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible. Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic). Input The first line of the input contains integers n and k (1 ≀ n ≀ 1.5Β·105, 1 ≀ k ≀ n) β€” the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn (1 ≀ hi ≀ 100), where hi is the height of the i-th plank of the fence. Output Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them. Examples Input 7 3 1 2 6 1 1 7 1 Output 3 Note In the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8. Tags: brute force, dp Correct Solution: ``` n, k = [int(i) for i in input().split()] #int(input()) arr = [int(i) for i in input().split()] acc = 0 for i in range(n): acc += arr[i] arr[i] = acc arr = [0] + arr min_idx = 0 min_br = arr[-1] + 1 for i in range(n-k+1): if arr[k+i] - arr[i] < min_br: min_br = arr[k+i] - arr[i] min_idx = i+1 print(min_idx) ```
90,453
Provide tags and a correct Python 3 solution for this coding contest problem. There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights. <image> Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1] Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible. Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic). Input The first line of the input contains integers n and k (1 ≀ n ≀ 1.5Β·105, 1 ≀ k ≀ n) β€” the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn (1 ≀ hi ≀ 100), where hi is the height of the i-th plank of the fence. Output Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them. Examples Input 7 3 1 2 6 1 1 7 1 Output 3 Note In the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8. Tags: brute force, dp Correct Solution: ``` import os import sys from collections import Counter from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") def main(): t=1 for _ in range(t): n,k=map(int,input().split()) a=list(map(int,input().split())) s=sum(a[:k]) # print(s) mins=s idx=0 for i in range(1,n-k+1): # print('i',i) s=s-a[i-1]+a[i+k-1] # print(s) if(s<mins): mins=s idx=i print(idx+1) if __name__ == "__main__": main() ```
90,454
Provide tags and a correct Python 3 solution for this coding contest problem. There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights. <image> Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1] Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible. Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic). Input The first line of the input contains integers n and k (1 ≀ n ≀ 1.5Β·105, 1 ≀ k ≀ n) β€” the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn (1 ≀ hi ≀ 100), where hi is the height of the i-th plank of the fence. Output Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them. Examples Input 7 3 1 2 6 1 1 7 1 Output 3 Note In the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8. Tags: brute force, dp Correct Solution: ``` n,k=map(int,input().split()) l=list(map(int,input().split())) dp=[0]*(n+1) for i in range(n): dp[i+1]=dp[i]+l[i] mn=999999999 ans=-1 for i in range(k,n+1): if(dp[i]-dp[i-k]<mn): mn=dp[i]-dp[i-k] ans=i-k+1 print(ans) ```
90,455
Provide tags and a correct Python 3 solution for this coding contest problem. There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights. <image> Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1] Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible. Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic). Input The first line of the input contains integers n and k (1 ≀ n ≀ 1.5Β·105, 1 ≀ k ≀ n) β€” the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn (1 ≀ hi ≀ 100), where hi is the height of the i-th plank of the fence. Output Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them. Examples Input 7 3 1 2 6 1 1 7 1 Output 3 Note In the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8. Tags: brute force, dp Correct Solution: ``` n,k = map(int,input().split()) lis = list(map(int,input().split())) ans=[] mi=sum(lis[:k]) su=mi ind=0 for i in range(k,n): su+=lis[i] su-=lis[i-k] if su<mi: ind=i-k+1 mi=su print(ind+1) ```
90,456
Provide tags and a correct Python 3 solution for this coding contest problem. There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights. <image> Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1] Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible. Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic). Input The first line of the input contains integers n and k (1 ≀ n ≀ 1.5Β·105, 1 ≀ k ≀ n) β€” the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn (1 ≀ hi ≀ 100), where hi is the height of the i-th plank of the fence. Output Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them. Examples Input 7 3 1 2 6 1 1 7 1 Output 3 Note In the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8. Tags: brute force, dp Correct Solution: ``` n,k=map(int,input().split()) a=list(map(int,input().split())) c=[0]*len(a) c[0]=a[0] for i in range(1,n): c[i]=c[i-1]+a[i] mini=c[k-1] h=1 #print(mini) for i in range(k,n): t=mini #print('i',i) mini=min(mini,c[i]-c[i-k]) #print(c[i]-c[i-k]) # print(mini) if mini!=t: h=i-k+2 print(h) #print(i) ```
90,457
Provide tags and a correct Python 3 solution for this coding contest problem. There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights. <image> Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1] Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible. Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic). Input The first line of the input contains integers n and k (1 ≀ n ≀ 1.5Β·105, 1 ≀ k ≀ n) β€” the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn (1 ≀ hi ≀ 100), where hi is the height of the i-th plank of the fence. Output Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them. Examples Input 7 3 1 2 6 1 1 7 1 Output 3 Note In the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8. Tags: brute force, dp Correct Solution: ``` n,k=map(int,input().split()) l=list(map(int,input().split())) s=0 d=[0] for i in l: s+=i d.append(s) prev=float('inf') idx=-1 for i in range(k-1,n): s=d[i+1]-d[i-k+1] #print(i,i-k+1,s,d[i+1],d[i-k+1]) if prev>s: prev=s idx=i+1-k+1 print(idx) ```
90,458
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights. <image> Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1] Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible. Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic). Input The first line of the input contains integers n and k (1 ≀ n ≀ 1.5Β·105, 1 ≀ k ≀ n) β€” the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn (1 ≀ hi ≀ 100), where hi is the height of the i-th plank of the fence. Output Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them. Examples Input 7 3 1 2 6 1 1 7 1 Output 3 Note In the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8. Submitted Solution: ``` arr=[] x=input() xx=x.split(' ') n=int(xx[0]) k=int(xx[1]) l = 0 t = 15000001 x=input() xx=x.split(' ') arr.append(0) for i in xx: arr.append(int(i)) for i in range(1,n+1): arr[i] += arr[i - 1] for i in range(k,n+1): if t > arr[i] - arr[i - k]: t = arr[i] - arr[i - k] z = i print(z - k + 1) ``` Yes
90,459
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights. <image> Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1] Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible. Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic). Input The first line of the input contains integers n and k (1 ≀ n ≀ 1.5Β·105, 1 ≀ k ≀ n) β€” the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn (1 ≀ hi ≀ 100), where hi is the height of the i-th plank of the fence. Output Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them. Examples Input 7 3 1 2 6 1 1 7 1 Output 3 Note In the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8. Submitted Solution: ``` #from functools import reduce #mod=int(1e9+7) #import resource #resource.setrlimit(resource.RLIMIT_STACK, [0x100000000, resource.RLIM_INFINITY]) #import threading #threading.stack_size(2**26) """fact=[1] #for i in range(1,100001): # fact.append((fact[-1]*i)%mod) #ifact=[0]*100001 #ifact[100000]=pow(fact[100000],mod-2,mod) #for i in range(100000,0,-1): # ifact[i-1]=(i*ifact[i])%mod""" #from collections import deque, defaultdict, Counter, OrderedDict #from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd #from heapq import heappush, heappop, heapify, nlargest, nsmallest # sys.setrecursionlimit(10**6) from sys import stdin, stdout import bisect #c++ upperbound from bisect import bisect_left as bl #c++ lowerbound bl(array,element) from bisect import bisect_right as br #c++ upperbound import itertools from collections import Counter from math import sqrt import collections import math import heapq import re def modinv(n,p): return pow(n,p-2,p) def cin(): return map(int,sin().split()) def ain(): #takes array as input return list(map(int,sin().split())) def sin(): return input() def inin(): return int(input()) def Divisors(n) : l = [] for i in range(1, int(math.sqrt(n) + 1)) : if (n % i == 0) : if (n // i == i) : l.append(i) else : l.append(i) l.append(n//i) return l def most_frequent(list): return max(set(list), key = list.count) def GCD(x,y): while(y): x, y = y, x % y return x def ncr(n,r,p): #To use this, Uncomment 19-25 t=((fact[n])*((ifact[r]*ifact[n-r])%p))%p return t def Convert(string): li = list(string.split("")) return li def SieveOfEratosthenes(n): global prime prime = [True for i in range(n+1)] p = 2 while (p * p <= n): if (prime[p] == True): for i in range(p * p, n+1, p): prime[i] = False p += 1 f=[] for p in range(2, n): if prime[p]: f.append(p) return f prime=[] q=[] def dfs(n,d,v,c): global q v[n]=1 x=d[n] q.append(n) j=c for i in x: if i not in v: f=dfs(i,d,v,c+1) j=max(j,f) # print(f) return j #Implement heapq #grades = [110, 25, 38, 49, 20, 95, 33, 87, 80, 90] #print(heapq.nlargest(3, grades)) #top 3 largest #print(heapq.nsmallest(4, grades)) #Always make a variable of predefined function for ex- fn=len #n,k=map(int,input().split()) """*******************************************************""" def main(): n,k = map(int,input().split()) f = list(map(int,input().split())) s = sum(f[:k]) best =s j = 1 for i in range(n-k): s-=f[i] s+=f[k+i] if s<best: best = s j = i+2 print(j) """*******************************************************""" ######## Python 2 and 3 footer by Pajenegod and c1729 py2 = round(0.5) if py2: from future_builtins import ascii, filter, hex, map, oct, zip range = xrange import os, sys from io import IOBase, BytesIO BUFSIZE = 8192 class FastIO(BytesIO): newlines = 0 def __init__(self, file): self._file = file self._fd = file.fileno() self.writable = "x" in file.mode or "w" in file.mode self.write = super(FastIO, self).write if self.writable else None def _fill(self): s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0]) return s def read(self): while self._fill(): pass return super(FastIO,self).read() def readline(self): while self.newlines == 0: s = self._fill(); self.newlines = s.count(b"\n") + (not s) self.newlines -= 1 return super(FastIO, self).readline() def flush(self): if self.writable: os.write(self._fd, self.getvalue()) self.truncate(0), self.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable if py2: self.write = self.buffer.write self.read = self.buffer.read self.readline = self.buffer.readline else: self.write = lambda s:self.buffer.write(s.encode('ascii')) self.read = lambda:self.buffer.read().decode('ascii') self.readline = lambda:self.buffer.readline().decode('ascii') sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip('\r\n') if __name__== "__main__": main() #threading.Thread(target=main).start() ``` Yes
90,460
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights. <image> Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1] Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible. Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic). Input The first line of the input contains integers n and k (1 ≀ n ≀ 1.5Β·105, 1 ≀ k ≀ n) β€” the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn (1 ≀ hi ≀ 100), where hi is the height of the i-th plank of the fence. Output Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them. Examples Input 7 3 1 2 6 1 1 7 1 Output 3 Note In the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8. Submitted Solution: ``` n,m=map(int,input().strip().split()) arr=list(map(int,input().strip().split())) s=sum(arr[0:m]) minimum=s index=0 for i in range(m, n): s = s - arr[i - m] + arr[i] if s<minimum: minimum=s index = i - m + 1 print(index + 1) ``` Yes
90,461
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights. <image> Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1] Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible. Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic). Input The first line of the input contains integers n and k (1 ≀ n ≀ 1.5Β·105, 1 ≀ k ≀ n) β€” the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn (1 ≀ hi ≀ 100), where hi is the height of the i-th plank of the fence. Output Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them. Examples Input 7 3 1 2 6 1 1 7 1 Output 3 Note In the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8. Submitted Solution: ``` def func(): n, k = map(int, input().split()) arr = list(map(int, input().split())) min_possible = k curr_sum = sum(arr[:k]) min_sum = float("inf") index = None for i in range(k, n): if curr_sum == min_possible: return i - k + 1 if curr_sum < min_sum: index = i - k + 1 min_sum = curr_sum curr_sum += arr[i] - arr[i-k] if curr_sum < min_sum: return n - k + 1 return index print(func()) ``` Yes
90,462
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights. <image> Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1] Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible. Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic). Input The first line of the input contains integers n and k (1 ≀ n ≀ 1.5Β·105, 1 ≀ k ≀ n) β€” the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn (1 ≀ hi ≀ 100), where hi is the height of the i-th plank of the fence. Output Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them. Examples Input 7 3 1 2 6 1 1 7 1 Output 3 Note In the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8. Submitted Solution: ``` # -*- coding: utf-8 -*- """ Created on Tue Mar 12 18:07:43 2019 @author: avina """ n,l = map(int, input().strip().split()) L = list(map(int, input().strip().split())) s = sum(L[:l]) m = s k = l - 1 for i in range(l,n-l+1): s-= L[i-l] - L[i] if s<m: k = i m = s print(k+2 - l) ``` No
90,463
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights. <image> Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1] Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible. Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic). Input The first line of the input contains integers n and k (1 ≀ n ≀ 1.5Β·105, 1 ≀ k ≀ n) β€” the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn (1 ≀ hi ≀ 100), where hi is the height of the i-th plank of the fence. Output Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them. Examples Input 7 3 1 2 6 1 1 7 1 Output 3 Note In the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8. Submitted Solution: ``` x,y=map(int,input().split()) a=list(map(int,input().split())) for i in range(1,x): a[i]+=a[i-1] s=1000000 p=-1 for i in range(y+1,x): if s>=a[i]-a[i-y-1]: s=a[i]-a[i-y-1] p=i-y p=1 if p==-1 else p print(p) ``` No
90,464
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights. <image> Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1] Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible. Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic). Input The first line of the input contains integers n and k (1 ≀ n ≀ 1.5Β·105, 1 ≀ k ≀ n) β€” the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn (1 ≀ hi ≀ 100), where hi is the height of the i-th plank of the fence. Output Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them. Examples Input 7 3 1 2 6 1 1 7 1 Output 3 Note In the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8. Submitted Solution: ``` n,k=map(int,input().split()) l=list(map(int,input().split())) s=[11**11,sum(l[0:k])] for i in range(1,n-k): s+=[s[-1]-l[i-1]+l[i+k-1]] print(s.index(min(s))) ``` No
90,465
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights. <image> Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1] Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible. Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic). Input The first line of the input contains integers n and k (1 ≀ n ≀ 1.5Β·105, 1 ≀ k ≀ n) β€” the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn (1 ≀ hi ≀ 100), where hi is the height of the i-th plank of the fence. Output Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them. Examples Input 7 3 1 2 6 1 1 7 1 Output 3 Note In the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8. Submitted Solution: ``` n, k = [int(i) for i in input().split(" ")] A = [int(i) for i in input().split(" ")] cons = [A[0]] for i in range(1, len(A)): cons.append(cons[-1] + A[i]) a = [] Min = 999999 ans = 0 i = 0 j = k while j < n: a.append(cons[j] - cons[i]) if cons[j] - cons[i] < Min: Min = cons[j] - cons[i] ans = i j += 1 i += 1 print(i + 2) ``` No
90,466
Provide tags and a correct Python 3 solution for this coding contest problem. George woke up and saw the current time s on the digital clock. Besides, George knows that he has slept for time t. Help George! Write a program that will, given time s and t, determine the time p when George went to bed. Note that George could have gone to bed yesterday relatively to the current time (see the second test sample). Input The first line contains current time s as a string in the format "hh:mm". The second line contains time t in the format "hh:mm" β€” the duration of George's sleep. It is guaranteed that the input contains the correct time in the 24-hour format, that is, 00 ≀ hh ≀ 23, 00 ≀ mm ≀ 59. Output In the single line print time p β€” the time George went to bed in the format similar to the format of the time in the input. Examples Input 05:50 05:44 Output 00:06 Input 00:00 01:00 Output 23:00 Input 00:01 00:00 Output 00:01 Note In the first sample George went to bed at "00:06". Note that you should print the time only in the format "00:06". That's why answers "0:06", "00:6" and others will be considered incorrect. In the second sample, George went to bed yesterday. In the third sample, George didn't do to bed at all. Tags: implementation Correct Solution: ``` from sys import stdin from datetime import datetime, timedelta def main(): s = stdin.readline().strip() t = stdin.readline().strip() s = datetime.strptime(s, '%H:%M') t = datetime.strptime(t, '%H:%M') t = timedelta(hours = t.hour, minutes = t.minute) p = s - t print('{:02}:{:02}'.format(p.hour, p.minute)) if __name__ == '__main__': main() ```
90,467
Provide tags and a correct Python 3 solution for this coding contest problem. George woke up and saw the current time s on the digital clock. Besides, George knows that he has slept for time t. Help George! Write a program that will, given time s and t, determine the time p when George went to bed. Note that George could have gone to bed yesterday relatively to the current time (see the second test sample). Input The first line contains current time s as a string in the format "hh:mm". The second line contains time t in the format "hh:mm" β€” the duration of George's sleep. It is guaranteed that the input contains the correct time in the 24-hour format, that is, 00 ≀ hh ≀ 23, 00 ≀ mm ≀ 59. Output In the single line print time p β€” the time George went to bed in the format similar to the format of the time in the input. Examples Input 05:50 05:44 Output 00:06 Input 00:00 01:00 Output 23:00 Input 00:01 00:00 Output 00:01 Note In the first sample George went to bed at "00:06". Note that you should print the time only in the format "00:06". That's why answers "0:06", "00:6" and others will be considered incorrect. In the second sample, George went to bed yesterday. In the third sample, George didn't do to bed at all. Tags: implementation Correct Solution: ``` # ///==========Libraries, Constants and Functions=============/// #mkraghav import sys inf = float("inf") mod = 1000000007 def get_array(): return list(map(int, sys.stdin.readline().split())) def get_ints(): return map(int, sys.stdin.readline().split()) def input(): return sys.stdin.readline() def int1():return int(input()) import string import math from itertools import combinations # ///==========MAIN=============/// def main(): s1=input() s2=input() x1=int(s1[:2]) x2=int(s1[3:]) y1=int(s2[:2]) y2=int(s2[3:]) x3=x1-y1 y=x2-y2 if y<0: y=60+y x3=x3-1 if x3<0: x3=24+x3 if x3<10: x3=str(0)+str(x3) if y<10: y=str(0)+str(y) print(str(x3)+':'+str(y)) if __name__ == "__main__": main() ```
90,468
Provide tags and a correct Python 3 solution for this coding contest problem. George woke up and saw the current time s on the digital clock. Besides, George knows that he has slept for time t. Help George! Write a program that will, given time s and t, determine the time p when George went to bed. Note that George could have gone to bed yesterday relatively to the current time (see the second test sample). Input The first line contains current time s as a string in the format "hh:mm". The second line contains time t in the format "hh:mm" β€” the duration of George's sleep. It is guaranteed that the input contains the correct time in the 24-hour format, that is, 00 ≀ hh ≀ 23, 00 ≀ mm ≀ 59. Output In the single line print time p β€” the time George went to bed in the format similar to the format of the time in the input. Examples Input 05:50 05:44 Output 00:06 Input 00:00 01:00 Output 23:00 Input 00:01 00:00 Output 00:01 Note In the first sample George went to bed at "00:06". Note that you should print the time only in the format "00:06". That's why answers "0:06", "00:6" and others will be considered incorrect. In the second sample, George went to bed yesterday. In the third sample, George didn't do to bed at all. Tags: implementation Correct Solution: ``` wu = input() t = input() a = wu.split(':') b = t.split(':') resHr = int(a[0]) - int(b[0]) resMin = int(a[1]) - int(b[1]) if int(resMin) < 0: resMin = str(int(resMin) % 60) resHr = str(int(resHr) - 1) else: resMin = str(resMin) if int(resHr) < 0: resHr = str(int(resHr) % 24) else: resHr = str(resHr) if int(resHr) < 10: resHr = '0'+str(abs(int(resHr))) else: resHr = str(resHr) if abs(int(resMin)) < 10: resMin = '0'+str(abs(int(resMin))) else: resMin = str(resMin) print(resHr+':'+resMin) ```
90,469
Provide tags and a correct Python 3 solution for this coding contest problem. George woke up and saw the current time s on the digital clock. Besides, George knows that he has slept for time t. Help George! Write a program that will, given time s and t, determine the time p when George went to bed. Note that George could have gone to bed yesterday relatively to the current time (see the second test sample). Input The first line contains current time s as a string in the format "hh:mm". The second line contains time t in the format "hh:mm" β€” the duration of George's sleep. It is guaranteed that the input contains the correct time in the 24-hour format, that is, 00 ≀ hh ≀ 23, 00 ≀ mm ≀ 59. Output In the single line print time p β€” the time George went to bed in the format similar to the format of the time in the input. Examples Input 05:50 05:44 Output 00:06 Input 00:00 01:00 Output 23:00 Input 00:01 00:00 Output 00:01 Note In the first sample George went to bed at "00:06". Note that you should print the time only in the format "00:06". That's why answers "0:06", "00:6" and others will be considered incorrect. In the second sample, George went to bed yesterday. In the third sample, George didn't do to bed at all. Tags: implementation Correct Solution: ``` a=input() b=input() c=a.split(":") d=b.split(":") currenttime1=int (c[0]) if int(c[0][0])==0 : currenttime1=int(c[0][1]) currenttime2= int (c[1]) if int(c[-1][0])==0 : currenttime2=int(c[1][1]) duration1=int(d[0]) if int(d[0][0])==0 : duration1=int(d[0][1]) duration2=int(d[1]) if int(d[-1][0])==0 : duration=int(d[1][1]) time2 = currenttime2-duration2 if currenttime2<duration2: time2= 60 - (duration2-currenttime2) duration1= duration1+1 time =currenttime1-duration1 if currenttime1 < duration1: time = 24 - (duration1-currenttime1) if time <=9 : time='0'+ str(time) if time2 <=9 : time2='0'+ str(time2) print(str(time)+':'+str(time2)) ```
90,470
Provide tags and a correct Python 3 solution for this coding contest problem. George woke up and saw the current time s on the digital clock. Besides, George knows that he has slept for time t. Help George! Write a program that will, given time s and t, determine the time p when George went to bed. Note that George could have gone to bed yesterday relatively to the current time (see the second test sample). Input The first line contains current time s as a string in the format "hh:mm". The second line contains time t in the format "hh:mm" β€” the duration of George's sleep. It is guaranteed that the input contains the correct time in the 24-hour format, that is, 00 ≀ hh ≀ 23, 00 ≀ mm ≀ 59. Output In the single line print time p β€” the time George went to bed in the format similar to the format of the time in the input. Examples Input 05:50 05:44 Output 00:06 Input 00:00 01:00 Output 23:00 Input 00:01 00:00 Output 00:01 Note In the first sample George went to bed at "00:06". Note that you should print the time only in the format "00:06". That's why answers "0:06", "00:6" and others will be considered incorrect. In the second sample, George went to bed yesterday. In the third sample, George didn't do to bed at all. Tags: implementation Correct Solution: ``` s, t = (tuple(map(int, input().split(':'))) for i in "01") z = ((s[0], 24)[not s[0]] - t[0]) * 60 + s[1] - t[1] print('{:02}:{:02}'.format((z // 60) % 24, z % 60)) ```
90,471
Provide tags and a correct Python 3 solution for this coding contest problem. George woke up and saw the current time s on the digital clock. Besides, George knows that he has slept for time t. Help George! Write a program that will, given time s and t, determine the time p when George went to bed. Note that George could have gone to bed yesterday relatively to the current time (see the second test sample). Input The first line contains current time s as a string in the format "hh:mm". The second line contains time t in the format "hh:mm" β€” the duration of George's sleep. It is guaranteed that the input contains the correct time in the 24-hour format, that is, 00 ≀ hh ≀ 23, 00 ≀ mm ≀ 59. Output In the single line print time p β€” the time George went to bed in the format similar to the format of the time in the input. Examples Input 05:50 05:44 Output 00:06 Input 00:00 01:00 Output 23:00 Input 00:01 00:00 Output 00:01 Note In the first sample George went to bed at "00:06". Note that you should print the time only in the format "00:06". That's why answers "0:06", "00:6" and others will be considered incorrect. In the second sample, George went to bed yesterday. In the third sample, George didn't do to bed at all. Tags: implementation Correct Solution: ``` s=input().split(':') s[0]=int(s[0]) s[1]=int(s[1]) t=input().split(':') t[0]=int(t[0]) t[1]=int(t[1]) def v(n): n=str(n) if len(n)==1: return '0'+n else: return n if s[0]>=t[0] and s[1]>=t[1]: print(v(s[0]-t[0])+':'+v(s[1]-t[1])) elif s[0]>t[0] and s[1]<t[1]: print(v(s[0]-t[0]-1)+':'+v(s[1]+60-t[1])) elif s[0]==t[0] and s[1]<t[1]: print('23'+':'+v(s[1]+60-t[1])) elif s[0]<t[0] and s[1]>=t[1]: print(v(24-(t[0]-s[0]))+':'+v(s[1]-t[1])) elif s[0]<t[0] and s[1]<t[1]: print(v(24-(t[0]-s[0])-1)+':'+v(60+s[1]-t[1])) ```
90,472
Provide tags and a correct Python 3 solution for this coding contest problem. George woke up and saw the current time s on the digital clock. Besides, George knows that he has slept for time t. Help George! Write a program that will, given time s and t, determine the time p when George went to bed. Note that George could have gone to bed yesterday relatively to the current time (see the second test sample). Input The first line contains current time s as a string in the format "hh:mm". The second line contains time t in the format "hh:mm" β€” the duration of George's sleep. It is guaranteed that the input contains the correct time in the 24-hour format, that is, 00 ≀ hh ≀ 23, 00 ≀ mm ≀ 59. Output In the single line print time p β€” the time George went to bed in the format similar to the format of the time in the input. Examples Input 05:50 05:44 Output 00:06 Input 00:00 01:00 Output 23:00 Input 00:01 00:00 Output 00:01 Note In the first sample George went to bed at "00:06". Note that you should print the time only in the format "00:06". That's why answers "0:06", "00:6" and others will be considered incorrect. In the second sample, George went to bed yesterday. In the third sample, George didn't do to bed at all. Tags: implementation Correct Solution: ``` def func(s): a, b = list(map(int, s.split(":"))) return a*60+b ct = func(input()) ts = func(input()) ans = ct - ts if ans < 0: ans += 60*24 print("%02i:%02i" % (ans//60, ans%60)) ```
90,473
Provide tags and a correct Python 3 solution for this coding contest problem. George woke up and saw the current time s on the digital clock. Besides, George knows that he has slept for time t. Help George! Write a program that will, given time s and t, determine the time p when George went to bed. Note that George could have gone to bed yesterday relatively to the current time (see the second test sample). Input The first line contains current time s as a string in the format "hh:mm". The second line contains time t in the format "hh:mm" β€” the duration of George's sleep. It is guaranteed that the input contains the correct time in the 24-hour format, that is, 00 ≀ hh ≀ 23, 00 ≀ mm ≀ 59. Output In the single line print time p β€” the time George went to bed in the format similar to the format of the time in the input. Examples Input 05:50 05:44 Output 00:06 Input 00:00 01:00 Output 23:00 Input 00:01 00:00 Output 00:01 Note In the first sample George went to bed at "00:06". Note that you should print the time only in the format "00:06". That's why answers "0:06", "00:6" and others will be considered incorrect. In the second sample, George went to bed yesterday. In the third sample, George didn't do to bed at all. Tags: implementation Correct Solution: ``` a,b = map(int, input().split(":")) c, d = map(int, input().split(":")) a1, a2 = [0]*2 if b< d: a2 = b - d + 60 if a: a -= 1 else: a = 23 else: a2= b - d if a < c: a1 = a - c + 24 else: a1 = a - c a1 = str(a1) a2 =str(a2) if len(a1) != 2: a1 = "0" + a1 if len(a2) != 2: a2 = "0" + a2 print(a1, a2, sep = ":") ```
90,474
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. George woke up and saw the current time s on the digital clock. Besides, George knows that he has slept for time t. Help George! Write a program that will, given time s and t, determine the time p when George went to bed. Note that George could have gone to bed yesterday relatively to the current time (see the second test sample). Input The first line contains current time s as a string in the format "hh:mm". The second line contains time t in the format "hh:mm" β€” the duration of George's sleep. It is guaranteed that the input contains the correct time in the 24-hour format, that is, 00 ≀ hh ≀ 23, 00 ≀ mm ≀ 59. Output In the single line print time p β€” the time George went to bed in the format similar to the format of the time in the input. Examples Input 05:50 05:44 Output 00:06 Input 00:00 01:00 Output 23:00 Input 00:01 00:00 Output 00:01 Note In the first sample George went to bed at "00:06". Note that you should print the time only in the format "00:06". That's why answers "0:06", "00:6" and others will be considered incorrect. In the second sample, George went to bed yesterday. In the third sample, George didn't do to bed at all. Submitted Solution: ``` s = [int(x) for x in input().split(":")] t = [int(x) for x in input().split(":")] s1 = s[0] * 60 + s[1] + 1440 t1 = t[0] * 60 + t[1] p = (s1 - t1) % 1440 print("0" * (2 - len(str(p // 60))) + str(p // 60) + ":" + "0" * (2 - len(str(p % 60))) + str(p % 60)) ``` Yes
90,475
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. George woke up and saw the current time s on the digital clock. Besides, George knows that he has slept for time t. Help George! Write a program that will, given time s and t, determine the time p when George went to bed. Note that George could have gone to bed yesterday relatively to the current time (see the second test sample). Input The first line contains current time s as a string in the format "hh:mm". The second line contains time t in the format "hh:mm" β€” the duration of George's sleep. It is guaranteed that the input contains the correct time in the 24-hour format, that is, 00 ≀ hh ≀ 23, 00 ≀ mm ≀ 59. Output In the single line print time p β€” the time George went to bed in the format similar to the format of the time in the input. Examples Input 05:50 05:44 Output 00:06 Input 00:00 01:00 Output 23:00 Input 00:01 00:00 Output 00:01 Note In the first sample George went to bed at "00:06". Note that you should print the time only in the format "00:06". That's why answers "0:06", "00:6" and others will be considered incorrect. In the second sample, George went to bed yesterday. In the third sample, George didn't do to bed at all. Submitted Solution: ``` s = list(map(int, input().split(':'))) t = list(map(int, input().split(':'))) p = [0, 0] if s[0] - t[0] < 0: p[0] = 24 - (t[0] - s[0]) else: p[0] = s[0] - t[0] if s[1] - t[1] < 0: p[1] = 60 - (t[1] - s[1]) if p[0] == 0: p[0] = 23 else: p[0] -= 1 else: p[1] = s[1] - t[1] if p[0] < 10: print('0', p[0], sep = '', end = ':') else: print(p[0], end = ':') if p[1] < 10: print('0', p[1], sep = '') else: print(p[1]) ``` Yes
90,476
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. George woke up and saw the current time s on the digital clock. Besides, George knows that he has slept for time t. Help George! Write a program that will, given time s and t, determine the time p when George went to bed. Note that George could have gone to bed yesterday relatively to the current time (see the second test sample). Input The first line contains current time s as a string in the format "hh:mm". The second line contains time t in the format "hh:mm" β€” the duration of George's sleep. It is guaranteed that the input contains the correct time in the 24-hour format, that is, 00 ≀ hh ≀ 23, 00 ≀ mm ≀ 59. Output In the single line print time p β€” the time George went to bed in the format similar to the format of the time in the input. Examples Input 05:50 05:44 Output 00:06 Input 00:00 01:00 Output 23:00 Input 00:01 00:00 Output 00:01 Note In the first sample George went to bed at "00:06". Note that you should print the time only in the format "00:06". That's why answers "0:06", "00:6" and others will be considered incorrect. In the second sample, George went to bed yesterday. In the third sample, George didn't do to bed at all. Submitted Solution: ``` h1,m1=map(int,input().split(':')) h2,m2=map(int,input().split(':')) if h1 == 00: h1=24 if h2 == 00: h2=24 time1=h1*60+m1 time2=h2*60+m2 if time2>time1: time1+=24*60 timex=time1-time2 hx=timex//60 mx=timex%60 if hx<10 : hx='0'+str(hx) if mx<10 : mx='0'+str(mx) print(hx,end='') print(':',end='') print(mx,end='') ``` Yes
90,477
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. George woke up and saw the current time s on the digital clock. Besides, George knows that he has slept for time t. Help George! Write a program that will, given time s and t, determine the time p when George went to bed. Note that George could have gone to bed yesterday relatively to the current time (see the second test sample). Input The first line contains current time s as a string in the format "hh:mm". The second line contains time t in the format "hh:mm" β€” the duration of George's sleep. It is guaranteed that the input contains the correct time in the 24-hour format, that is, 00 ≀ hh ≀ 23, 00 ≀ mm ≀ 59. Output In the single line print time p β€” the time George went to bed in the format similar to the format of the time in the input. Examples Input 05:50 05:44 Output 00:06 Input 00:00 01:00 Output 23:00 Input 00:01 00:00 Output 00:01 Note In the first sample George went to bed at "00:06". Note that you should print the time only in the format "00:06". That's why answers "0:06", "00:6" and others will be considered incorrect. In the second sample, George went to bed yesterday. In the third sample, George didn't do to bed at all. Submitted Solution: ``` bangun = input().split(":") lama_tidur = input().split(":") menit = int(bangun[1]) - int(lama_tidur[1]) jam = int(bangun[0]) - int(lama_tidur[0]) if menit < 0: menit += 60 jam -= 1 if jam < 0: jam += 24 A = str(menit) B = str(jam) if len(A) < 2: A = "0"+A if len(B)<2: B = "0"+B print(B+":"+A) ``` Yes
90,478
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. George woke up and saw the current time s on the digital clock. Besides, George knows that he has slept for time t. Help George! Write a program that will, given time s and t, determine the time p when George went to bed. Note that George could have gone to bed yesterday relatively to the current time (see the second test sample). Input The first line contains current time s as a string in the format "hh:mm". The second line contains time t in the format "hh:mm" β€” the duration of George's sleep. It is guaranteed that the input contains the correct time in the 24-hour format, that is, 00 ≀ hh ≀ 23, 00 ≀ mm ≀ 59. Output In the single line print time p β€” the time George went to bed in the format similar to the format of the time in the input. Examples Input 05:50 05:44 Output 00:06 Input 00:00 01:00 Output 23:00 Input 00:01 00:00 Output 00:01 Note In the first sample George went to bed at "00:06". Note that you should print the time only in the format "00:06". That's why answers "0:06", "00:6" and others will be considered incorrect. In the second sample, George went to bed yesterday. In the third sample, George didn't do to bed at all. Submitted Solution: ``` a=input() b=input() c=a.split(":") d=b.split(":") currenttime1=int (c[0]) if int(c[0][0])==0 : currenttime1=int(c[0][1]) currenttime2= int (c[1]) if int(c[-1][0])==0 : currenttime2=int(c[1][1]) duration1=int(d[0]) if int(c[0][0])==0 : duration1=int(d[0][1]) duration2=int(d[1]) if int(c[-1][0])==0 : duration=int(d[1][1]) time =currenttime1-duration1 if currenttime1<duration1: time = 24 - duration1 time2 = currenttime2-duration2 if currenttime2<duration2: time2= 60 - duration2 if time == 0 : time= 24 - 1 else: time= time -1 if time <=9 : time='0'+ str(time) if time2 <=9 : time2='0'+ str(time2) print(str(time)+':'+str(time2)) ``` No
90,479
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. George woke up and saw the current time s on the digital clock. Besides, George knows that he has slept for time t. Help George! Write a program that will, given time s and t, determine the time p when George went to bed. Note that George could have gone to bed yesterday relatively to the current time (see the second test sample). Input The first line contains current time s as a string in the format "hh:mm". The second line contains time t in the format "hh:mm" β€” the duration of George's sleep. It is guaranteed that the input contains the correct time in the 24-hour format, that is, 00 ≀ hh ≀ 23, 00 ≀ mm ≀ 59. Output In the single line print time p β€” the time George went to bed in the format similar to the format of the time in the input. Examples Input 05:50 05:44 Output 00:06 Input 00:00 01:00 Output 23:00 Input 00:01 00:00 Output 00:01 Note In the first sample George went to bed at "00:06". Note that you should print the time only in the format "00:06". That's why answers "0:06", "00:6" and others will be considered incorrect. In the second sample, George went to bed yesterday. In the third sample, George didn't do to bed at all. Submitted Solution: ``` s=input() t=input() cth=sth=ctm=stm='' for i in range(0,5): if i<2: cth+=s[i] sth+=t[i] elif i>2: ctm += s[i] stm+=t[i] ah=int(cth)-int(sth) am=int(ctm)-int(stm) if ah>0 and am!=0: ah=ah-1 if cth==sth and ctm<stm: ah=23 if ah<0 and am>=0: ah=24+ah if ah<0 and am<0: ah=23+ah if am<0: am=60+am if len(str(ah))==1 and len(str(am))==1: print("0",ah,":","0",am,sep='') elif len(str(ah))==1 and len(str(am))==2: print("0",ah,":",am,sep='') elif len(str(ah))==2 and len(str(am))==1: print(ah,":","0",am,sep='') else: print(ah,":",am,sep='') ``` No
90,480
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. George woke up and saw the current time s on the digital clock. Besides, George knows that he has slept for time t. Help George! Write a program that will, given time s and t, determine the time p when George went to bed. Note that George could have gone to bed yesterday relatively to the current time (see the second test sample). Input The first line contains current time s as a string in the format "hh:mm". The second line contains time t in the format "hh:mm" β€” the duration of George's sleep. It is guaranteed that the input contains the correct time in the 24-hour format, that is, 00 ≀ hh ≀ 23, 00 ≀ mm ≀ 59. Output In the single line print time p β€” the time George went to bed in the format similar to the format of the time in the input. Examples Input 05:50 05:44 Output 00:06 Input 00:00 01:00 Output 23:00 Input 00:01 00:00 Output 00:01 Note In the first sample George went to bed at "00:06". Note that you should print the time only in the format "00:06". That's why answers "0:06", "00:6" and others will be considered incorrect. In the second sample, George went to bed yesterday. In the third sample, George didn't do to bed at all. Submitted Solution: ``` a = input() q1, q2, q3, q4 = int(a[0]), int(a[1]), int(a[3]), int(a[4]) b = input() w1, w2, w3, w4 = int(b[0]), int(b[1]), int(b[3]), int(b[4]) ww = (q3*10 + q4) + (q1*10 + q2)*60 qq = (w3*10 + w4) + (w1*10 + w2)*60 s = "" if q1 == q2 and q3 == q2 and q3 == q4 and w1== q1 and w2 == q1 and w3 == q1 and w4 == q1: print("00:00") elif (ww - qq) > 0: if(ww - qq) // 60 > 9: d = str((ww - qq)//60) s += d else: s += "0" d = str((ww - qq)//60) s += d s += ":" if (ww - qq) % 60 > 9: d = str((ww - qq)%60) s += d else: s += "0" d = str((ww - qq)%60) s += d print(s) else: if(ww - qq + 1440) // 60 > 9: d = str((ww - qq+ 1440)//60) s += d else: s += "0" d = str((ww - qq+ 1440)//60) s += d s += ":" if (ww - qq + 1440) % 60 > 9: d = str((ww - qq + 1440)%60) s += d else: s += "0" d = str((ww - qq + 1440)%60) s += d print(s) ``` No
90,481
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. George woke up and saw the current time s on the digital clock. Besides, George knows that he has slept for time t. Help George! Write a program that will, given time s and t, determine the time p when George went to bed. Note that George could have gone to bed yesterday relatively to the current time (see the second test sample). Input The first line contains current time s as a string in the format "hh:mm". The second line contains time t in the format "hh:mm" β€” the duration of George's sleep. It is guaranteed that the input contains the correct time in the 24-hour format, that is, 00 ≀ hh ≀ 23, 00 ≀ mm ≀ 59. Output In the single line print time p β€” the time George went to bed in the format similar to the format of the time in the input. Examples Input 05:50 05:44 Output 00:06 Input 00:00 01:00 Output 23:00 Input 00:01 00:00 Output 00:01 Note In the first sample George went to bed at "00:06". Note that you should print the time only in the format "00:06". That's why answers "0:06", "00:6" and others will be considered incorrect. In the second sample, George went to bed yesterday. In the third sample, George didn't do to bed at all. Submitted Solution: ``` while True: current = input().split(":") duration = input().split(":") if len(current) == 2 and len(duration) == 2: break jam = 0 menit = 0 jam = int(current[0]) - int(duration[0]) menit = int(current[1]) - int(duration[1]) if int(current[1]) + int(duration[1]) >= 60: jam -= 1 if jam < 0: jam = 24 + jam if menit < 0: menit = 60 + menit if 0 <= jam < 10: jam = "0" + str(jam) if 0 <= menit < 10: menit = "0" + str(menit) print(str(jam) + ":" + str(menit)) ``` No
90,482
Provide tags and a correct Python 3 solution for this coding contest problem. Piegirl was asked to implement two table join operation for distributed database system, minimizing the network traffic. Suppose she wants to join two tables, A and B. Each of them has certain number of rows which are distributed on different number of partitions. Table A is distributed on the first cluster consisting of m partitions. Partition with index i has ai rows from A. Similarly, second cluster containing table B has n partitions, i-th one having bi rows from B. In one network operation she can copy one row from any partition to any other partition. At the end, for each row from A and each row from B there should be a partition that has both rows. Determine the minimal number of network operations to achieve this. Input First line contains two integer numbers, m and n (1 ≀ m, n ≀ 105). Second line contains description of the first cluster with m space separated integers, ai (1 ≀ ai ≀ 109). Similarly, third line describes second cluster with n space separated integers, bi (1 ≀ bi ≀ 109). Output Print one integer β€” minimal number of copy operations. Examples Input 2 2 2 6 3 100 Output 11 Input 2 3 10 10 1 1 1 Output 6 Note In the first example it makes sense to move all the rows to the second partition of the second cluster which is achieved in 2 + 6 + 3 = 11 operations In the second example Piegirl can copy each row from B to the both partitions of the first cluster which needs 2Β·3 = 6 copy operations. Tags: greedy Correct Solution: ``` import sys n,m=map(int,sys.stdin.readline().split()) A=list(map(int,sys.stdin.readline().split())) B=list(map(int,sys.stdin.readline().split())) A.sort(reverse=True) B.sort(reverse=True) a=sum(A) b=sum(B) ans=0 left=0 for i in range(n): left+=A[i] temp=b*(i+1)+a-left if(ans==0): ans=temp ans=min(ans,temp) left=0 for i in range(m): left+=B[i] temp=a*(i+1)+b-left if(ans==0): ans=temp ans=min(ans,temp) print(ans) # Made By Mostafa_Khaled ```
90,483
Provide tags and a correct Python 3 solution for this coding contest problem. Piegirl was asked to implement two table join operation for distributed database system, minimizing the network traffic. Suppose she wants to join two tables, A and B. Each of them has certain number of rows which are distributed on different number of partitions. Table A is distributed on the first cluster consisting of m partitions. Partition with index i has ai rows from A. Similarly, second cluster containing table B has n partitions, i-th one having bi rows from B. In one network operation she can copy one row from any partition to any other partition. At the end, for each row from A and each row from B there should be a partition that has both rows. Determine the minimal number of network operations to achieve this. Input First line contains two integer numbers, m and n (1 ≀ m, n ≀ 105). Second line contains description of the first cluster with m space separated integers, ai (1 ≀ ai ≀ 109). Similarly, third line describes second cluster with n space separated integers, bi (1 ≀ bi ≀ 109). Output Print one integer β€” minimal number of copy operations. Examples Input 2 2 2 6 3 100 Output 11 Input 2 3 10 10 1 1 1 Output 6 Note In the first example it makes sense to move all the rows to the second partition of the second cluster which is achieved in 2 + 6 + 3 = 11 operations In the second example Piegirl can copy each row from B to the both partitions of the first cluster which needs 2Β·3 = 6 copy operations. Tags: greedy Correct Solution: ``` n, m = list(map(int, input().split())) A = list(map(int, input().split())) B = list(map(int, input().split())) A.sort(reverse=True) B.sort(reverse=True) sumA = sum(A) sumB = sum(B) ansA = sumB ansB = sumA for i in range(1, n): ansA += min(A[i],sumB) for i in range(1, m): ansB += min(B[i], sumA) print(min(ansA,ansB)) ```
90,484
Provide tags and a correct Python 3 solution for this coding contest problem. Piegirl was asked to implement two table join operation for distributed database system, minimizing the network traffic. Suppose she wants to join two tables, A and B. Each of them has certain number of rows which are distributed on different number of partitions. Table A is distributed on the first cluster consisting of m partitions. Partition with index i has ai rows from A. Similarly, second cluster containing table B has n partitions, i-th one having bi rows from B. In one network operation she can copy one row from any partition to any other partition. At the end, for each row from A and each row from B there should be a partition that has both rows. Determine the minimal number of network operations to achieve this. Input First line contains two integer numbers, m and n (1 ≀ m, n ≀ 105). Second line contains description of the first cluster with m space separated integers, ai (1 ≀ ai ≀ 109). Similarly, third line describes second cluster with n space separated integers, bi (1 ≀ bi ≀ 109). Output Print one integer β€” minimal number of copy operations. Examples Input 2 2 2 6 3 100 Output 11 Input 2 3 10 10 1 1 1 Output 6 Note In the first example it makes sense to move all the rows to the second partition of the second cluster which is achieved in 2 + 6 + 3 = 11 operations In the second example Piegirl can copy each row from B to the both partitions of the first cluster which needs 2Β·3 = 6 copy operations. Tags: greedy Correct Solution: ``` n,m=map(int,input().split()) a=list(map(int,input().split())) b=list(map(int,input().split())) # asum=0 # bsum=0 a.sort() b.sort() asum=sum(a) bsum=sum(b) ans=9999999999999999999999999999999999999999 # print(a[0]) le=0 for i in range(0,n): tmp=le+(n-i)*(bsum) ans=min(ans,tmp) le=le+a[i] le=0 for i in range(0,m): tmp=le+(m-i)*(asum) ans=min(ans,tmp) le=le+b[i] print(ans) ```
90,485
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Piegirl was asked to implement two table join operation for distributed database system, minimizing the network traffic. Suppose she wants to join two tables, A and B. Each of them has certain number of rows which are distributed on different number of partitions. Table A is distributed on the first cluster consisting of m partitions. Partition with index i has ai rows from A. Similarly, second cluster containing table B has n partitions, i-th one having bi rows from B. In one network operation she can copy one row from any partition to any other partition. At the end, for each row from A and each row from B there should be a partition that has both rows. Determine the minimal number of network operations to achieve this. Input First line contains two integer numbers, m and n (1 ≀ m, n ≀ 105). Second line contains description of the first cluster with m space separated integers, ai (1 ≀ ai ≀ 109). Similarly, third line describes second cluster with n space separated integers, bi (1 ≀ bi ≀ 109). Output Print one integer β€” minimal number of copy operations. Examples Input 2 2 2 6 3 100 Output 11 Input 2 3 10 10 1 1 1 Output 6 Note In the first example it makes sense to move all the rows to the second partition of the second cluster which is achieved in 2 + 6 + 3 = 11 operations In the second example Piegirl can copy each row from B to the both partitions of the first cluster which needs 2Β·3 = 6 copy operations. Submitted Solution: ``` [n,m]=[int(i) for i in input().split()] a=[int(i) for i in input().split()] b=[int(i) for i in input().split()] sum1=sum(a);sum2=sum(b) max1=max(a);max2=max(b) ans=min(sum1+sum2-max2,sum1*m,sum1+sum2-max1,sum2*n) print(ans) ``` No
90,486
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Piegirl was asked to implement two table join operation for distributed database system, minimizing the network traffic. Suppose she wants to join two tables, A and B. Each of them has certain number of rows which are distributed on different number of partitions. Table A is distributed on the first cluster consisting of m partitions. Partition with index i has ai rows from A. Similarly, second cluster containing table B has n partitions, i-th one having bi rows from B. In one network operation she can copy one row from any partition to any other partition. At the end, for each row from A and each row from B there should be a partition that has both rows. Determine the minimal number of network operations to achieve this. Input First line contains two integer numbers, m and n (1 ≀ m, n ≀ 105). Second line contains description of the first cluster with m space separated integers, ai (1 ≀ ai ≀ 109). Similarly, third line describes second cluster with n space separated integers, bi (1 ≀ bi ≀ 109). Output Print one integer β€” minimal number of copy operations. Examples Input 2 2 2 6 3 100 Output 11 Input 2 3 10 10 1 1 1 Output 6 Note In the first example it makes sense to move all the rows to the second partition of the second cluster which is achieved in 2 + 6 + 3 = 11 operations In the second example Piegirl can copy each row from B to the both partitions of the first cluster which needs 2Β·3 = 6 copy operations. Submitted Solution: ``` mn = [int(i) for i in input().split()] a = [int(j) for j in input().split()] b = [int(k) for k in input().split()] c = a+b print(c.count(max(c))*(sum(c)-(c.count(max(c))*max(c)))) ``` No
90,487
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Piegirl was asked to implement two table join operation for distributed database system, minimizing the network traffic. Suppose she wants to join two tables, A and B. Each of them has certain number of rows which are distributed on different number of partitions. Table A is distributed on the first cluster consisting of m partitions. Partition with index i has ai rows from A. Similarly, second cluster containing table B has n partitions, i-th one having bi rows from B. In one network operation she can copy one row from any partition to any other partition. At the end, for each row from A and each row from B there should be a partition that has both rows. Determine the minimal number of network operations to achieve this. Input First line contains two integer numbers, m and n (1 ≀ m, n ≀ 105). Second line contains description of the first cluster with m space separated integers, ai (1 ≀ ai ≀ 109). Similarly, third line describes second cluster with n space separated integers, bi (1 ≀ bi ≀ 109). Output Print one integer β€” minimal number of copy operations. Examples Input 2 2 2 6 3 100 Output 11 Input 2 3 10 10 1 1 1 Output 6 Note In the first example it makes sense to move all the rows to the second partition of the second cluster which is achieved in 2 + 6 + 3 = 11 operations In the second example Piegirl can copy each row from B to the both partitions of the first cluster which needs 2Β·3 = 6 copy operations. Submitted Solution: ``` [n,m]=[int(i) for i in input().split()] a=[int(i) for i in input().split()] b=[int(i) for i in input().split()] sum1=sum(a);sum2=sum(b) max1=max(a);max2=max(b) if sum1<sum2: ans=min(sum1+sum2-max2,sum1*m) else: ans=min(sum2+sum1-max1,sum2*n) print(ans) ``` No
90,488
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Piegirl was asked to implement two table join operation for distributed database system, minimizing the network traffic. Suppose she wants to join two tables, A and B. Each of them has certain number of rows which are distributed on different number of partitions. Table A is distributed on the first cluster consisting of m partitions. Partition with index i has ai rows from A. Similarly, second cluster containing table B has n partitions, i-th one having bi rows from B. In one network operation she can copy one row from any partition to any other partition. At the end, for each row from A and each row from B there should be a partition that has both rows. Determine the minimal number of network operations to achieve this. Input First line contains two integer numbers, m and n (1 ≀ m, n ≀ 105). Second line contains description of the first cluster with m space separated integers, ai (1 ≀ ai ≀ 109). Similarly, third line describes second cluster with n space separated integers, bi (1 ≀ bi ≀ 109). Output Print one integer β€” minimal number of copy operations. Examples Input 2 2 2 6 3 100 Output 11 Input 2 3 10 10 1 1 1 Output 6 Note In the first example it makes sense to move all the rows to the second partition of the second cluster which is achieved in 2 + 6 + 3 = 11 operations In the second example Piegirl can copy each row from B to the both partitions of the first cluster which needs 2Β·3 = 6 copy operations. Submitted Solution: ``` [n,m]=[int(i) for i in input().split()] a=[int(i) for i in input().split()] b=[int(i) for i in input().split()] sum1=sum(a);sum2=sum(b) max1=max(a);max2=max(b) ans=min(sum1+sum2-max2,sum1*m,sum1+sum2-max2,sum2*n) print(ans) ``` No
90,489
Provide tags and a correct Python 3 solution for this coding contest problem. Imagine that you are in a building that has exactly n floors. You can move between the floors in a lift. Let's number the floors from bottom to top with integers from 1 to n. Now you're on the floor number a. You are very bored, so you want to take the lift. Floor number b has a secret lab, the entry is forbidden. However, you already are in the mood and decide to make k consecutive trips in the lift. Let us suppose that at the moment you are on the floor number x (initially, you were on floor a). For another trip between floors you choose some floor with number y (y β‰  x) and the lift travels to this floor. As you cannot visit floor b with the secret lab, you decided that the distance from the current floor x to the chosen y must be strictly less than the distance from the current floor x to floor b with the secret lab. Formally, it means that the following inequation must fulfill: |x - y| < |x - b|. After the lift successfully transports you to floor y, you write down number y in your notepad. Your task is to find the number of distinct number sequences that you could have written in the notebook as the result of k trips in the lift. As the sought number of trips can be rather large, find the remainder after dividing the number by 1000000007 (109 + 7). Input The first line of the input contains four space-separated integers n, a, b, k (2 ≀ n ≀ 5000, 1 ≀ k ≀ 5000, 1 ≀ a, b ≀ n, a β‰  b). Output Print a single integer β€” the remainder after dividing the sought number of sequences by 1000000007 (109 + 7). Examples Input 5 2 4 1 Output 2 Input 5 2 4 2 Output 2 Input 5 3 4 1 Output 0 Note Two sequences p1, p2, ..., pk and q1, q2, ..., qk are distinct, if there is such integer j (1 ≀ j ≀ k), that pj β‰  qj. Notes to the samples: 1. In the first sample after the first trip you are either on floor 1, or on floor 3, because |1 - 2| < |2 - 4| and |3 - 2| < |2 - 4|. 2. In the second sample there are two possible sequences: (1, 2); (1, 3). You cannot choose floor 3 for the first trip because in this case no floor can be the floor for the second trip. 3. In the third sample there are no sought sequences, because you cannot choose the floor for the first trip. Tags: combinatorics, dp Correct Solution: ``` import sys input=sys.stdin.readline def solve(n,st,k): mod=10**9+7 dp=[0]*(n+1) pre=[0]*(n+1) dp[st]=1 for t in range(k): pre[0]=0 for i in range(1,n+1): pre[i]=pre[i-1]+dp[i] pre[i]%=mod for i in range(1,n+1): dp[i]=(pre[n]-pre[i]+pre[i-1]-pre[i>>1])%mod return sum(dp)%mod n,a,b,k=map(int,input().split()) mod=10**9+7 if a>b: print(solve(n-b,a-b,k)) else: print(solve(b-1,b-a,k)) ```
90,490
Provide tags and a correct Python 3 solution for this coding contest problem. Imagine that you are in a building that has exactly n floors. You can move between the floors in a lift. Let's number the floors from bottom to top with integers from 1 to n. Now you're on the floor number a. You are very bored, so you want to take the lift. Floor number b has a secret lab, the entry is forbidden. However, you already are in the mood and decide to make k consecutive trips in the lift. Let us suppose that at the moment you are on the floor number x (initially, you were on floor a). For another trip between floors you choose some floor with number y (y β‰  x) and the lift travels to this floor. As you cannot visit floor b with the secret lab, you decided that the distance from the current floor x to the chosen y must be strictly less than the distance from the current floor x to floor b with the secret lab. Formally, it means that the following inequation must fulfill: |x - y| < |x - b|. After the lift successfully transports you to floor y, you write down number y in your notepad. Your task is to find the number of distinct number sequences that you could have written in the notebook as the result of k trips in the lift. As the sought number of trips can be rather large, find the remainder after dividing the number by 1000000007 (109 + 7). Input The first line of the input contains four space-separated integers n, a, b, k (2 ≀ n ≀ 5000, 1 ≀ k ≀ 5000, 1 ≀ a, b ≀ n, a β‰  b). Output Print a single integer β€” the remainder after dividing the sought number of sequences by 1000000007 (109 + 7). Examples Input 5 2 4 1 Output 2 Input 5 2 4 2 Output 2 Input 5 3 4 1 Output 0 Note Two sequences p1, p2, ..., pk and q1, q2, ..., qk are distinct, if there is such integer j (1 ≀ j ≀ k), that pj β‰  qj. Notes to the samples: 1. In the first sample after the first trip you are either on floor 1, or on floor 3, because |1 - 2| < |2 - 4| and |3 - 2| < |2 - 4|. 2. In the second sample there are two possible sequences: (1, 2); (1, 3). You cannot choose floor 3 for the first trip because in this case no floor can be the floor for the second trip. 3. In the third sample there are no sought sequences, because you cannot choose the floor for the first trip. Tags: combinatorics, dp Correct Solution: ``` def solve(n, st, k): MOD = int(1e9 + 7) dp = [0] * (n + 1) prefix_sum = [0] * (n + 1) dp[st] = 1 for times in range(k): prefix_sum[0] = 0 for i in range(1, n + 1): prefix_sum[i] = prefix_sum[i - 1] + dp[i] if prefix_sum[i] >= MOD: prefix_sum[i] -= MOD for i in range(1, n + 1): dp[i] = prefix_sum[n] - prefix_sum[i] + prefix_sum[i - 1] - prefix_sum[i >> 1] while dp[i] < 0: dp[i] += MOD while dp[i] >= MOD: dp[i] -= MOD return sum(dp) % MOD def main(): n, a, b, k = [int(i) for i in input().split()] if a > b: print(solve(n - b, a - b, k)) else: print(solve(b - 1, b - a, k)) main() ```
90,491
Provide tags and a correct Python 3 solution for this coding contest problem. Imagine that you are in a building that has exactly n floors. You can move between the floors in a lift. Let's number the floors from bottom to top with integers from 1 to n. Now you're on the floor number a. You are very bored, so you want to take the lift. Floor number b has a secret lab, the entry is forbidden. However, you already are in the mood and decide to make k consecutive trips in the lift. Let us suppose that at the moment you are on the floor number x (initially, you were on floor a). For another trip between floors you choose some floor with number y (y β‰  x) and the lift travels to this floor. As you cannot visit floor b with the secret lab, you decided that the distance from the current floor x to the chosen y must be strictly less than the distance from the current floor x to floor b with the secret lab. Formally, it means that the following inequation must fulfill: |x - y| < |x - b|. After the lift successfully transports you to floor y, you write down number y in your notepad. Your task is to find the number of distinct number sequences that you could have written in the notebook as the result of k trips in the lift. As the sought number of trips can be rather large, find the remainder after dividing the number by 1000000007 (109 + 7). Input The first line of the input contains four space-separated integers n, a, b, k (2 ≀ n ≀ 5000, 1 ≀ k ≀ 5000, 1 ≀ a, b ≀ n, a β‰  b). Output Print a single integer β€” the remainder after dividing the sought number of sequences by 1000000007 (109 + 7). Examples Input 5 2 4 1 Output 2 Input 5 2 4 2 Output 2 Input 5 3 4 1 Output 0 Note Two sequences p1, p2, ..., pk and q1, q2, ..., qk are distinct, if there is such integer j (1 ≀ j ≀ k), that pj β‰  qj. Notes to the samples: 1. In the first sample after the first trip you are either on floor 1, or on floor 3, because |1 - 2| < |2 - 4| and |3 - 2| < |2 - 4|. 2. In the second sample there are two possible sequences: (1, 2); (1, 3). You cannot choose floor 3 for the first trip because in this case no floor can be the floor for the second trip. 3. In the third sample there are no sought sequences, because you cannot choose the floor for the first trip. Tags: combinatorics, dp Correct Solution: ``` def solve(n, st, k): MOD = int(1e9 + 7) prev = [0] * (n + 1) current = [0] * (n + 1) prefix_sum = [0] * (n + 1) prev[st] = 1 for times in range(k): prefix_sum[0] = 0 for i in range(1, n + 1): prefix_sum[i] = prefix_sum[i - 1] + prev[i] if prefix_sum[i] >= MOD: prefix_sum[i] -= MOD for i in range(1, n + 1): current[i] = prefix_sum[n] - prefix_sum[i >> 1] - prev[i] while current[i] < 0: current[i] += MOD while current[i] >= MOD: current[i] -= MOD prev, current = current, prev return sum(prev) % MOD def main(): n, a, b, k = [int(i) for i in input().split()] if a > b: print(solve(n - b, a - b, k)) else: print(solve(b - 1, b - a, k)) main() ```
90,492
Provide tags and a correct Python 3 solution for this coding contest problem. Imagine that you are in a building that has exactly n floors. You can move between the floors in a lift. Let's number the floors from bottom to top with integers from 1 to n. Now you're on the floor number a. You are very bored, so you want to take the lift. Floor number b has a secret lab, the entry is forbidden. However, you already are in the mood and decide to make k consecutive trips in the lift. Let us suppose that at the moment you are on the floor number x (initially, you were on floor a). For another trip between floors you choose some floor with number y (y β‰  x) and the lift travels to this floor. As you cannot visit floor b with the secret lab, you decided that the distance from the current floor x to the chosen y must be strictly less than the distance from the current floor x to floor b with the secret lab. Formally, it means that the following inequation must fulfill: |x - y| < |x - b|. After the lift successfully transports you to floor y, you write down number y in your notepad. Your task is to find the number of distinct number sequences that you could have written in the notebook as the result of k trips in the lift. As the sought number of trips can be rather large, find the remainder after dividing the number by 1000000007 (109 + 7). Input The first line of the input contains four space-separated integers n, a, b, k (2 ≀ n ≀ 5000, 1 ≀ k ≀ 5000, 1 ≀ a, b ≀ n, a β‰  b). Output Print a single integer β€” the remainder after dividing the sought number of sequences by 1000000007 (109 + 7). Examples Input 5 2 4 1 Output 2 Input 5 2 4 2 Output 2 Input 5 3 4 1 Output 0 Note Two sequences p1, p2, ..., pk and q1, q2, ..., qk are distinct, if there is such integer j (1 ≀ j ≀ k), that pj β‰  qj. Notes to the samples: 1. In the first sample after the first trip you are either on floor 1, or on floor 3, because |1 - 2| < |2 - 4| and |3 - 2| < |2 - 4|. 2. In the second sample there are two possible sequences: (1, 2); (1, 3). You cannot choose floor 3 for the first trip because in this case no floor can be the floor for the second trip. 3. In the third sample there are no sought sequences, because you cannot choose the floor for the first trip. Tags: combinatorics, dp Correct Solution: ``` #!/usr/bin/env python3 import io import os import sys input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline def printd(*args, **kwargs): #print(*args, **kwargs, file=sys.stderr) #print(*args, **kwargs) pass def get_str(): return input().decode().strip() def rint(): return map(int, input().split()) def oint(): return int(input()) mod = 1000000007 n, a, b, k = rint() if a > b: a, b = n-a+1, n-b+1 a -= 1 b -= 1 printd(n, a, b, k) d = [0]*n d[a] = 1 ps = [0]*b ps[0] = d[0] for j in range(1, b): ps[j] = ps[j-1]+d[j] ps[j] %= mod printd(n, a, b, k) printd(d, ps) for i in range(k): for j in range(b): #b-t > t-j #2*t < b+j #t < (b+j)/2 if (b+j)%2: t = (b+j)//2 else: t = (b+j)//2 - 1 if j == 0: d[j] = ps[t] - ps[j] else: d[j] = ps[t] - ps[j] + ps[j-1] d[j] %= mod #d[j] %=mod ps[0] = d[0] for j in range(1, b): ps[j] = (ps[j-1]+d[j])# %mod ps[j] %= mod printd(d,ps) ans = ps[b-1] print(ans%mod) ```
90,493
Provide tags and a correct Python 3 solution for this coding contest problem. Imagine that you are in a building that has exactly n floors. You can move between the floors in a lift. Let's number the floors from bottom to top with integers from 1 to n. Now you're on the floor number a. You are very bored, so you want to take the lift. Floor number b has a secret lab, the entry is forbidden. However, you already are in the mood and decide to make k consecutive trips in the lift. Let us suppose that at the moment you are on the floor number x (initially, you were on floor a). For another trip between floors you choose some floor with number y (y β‰  x) and the lift travels to this floor. As you cannot visit floor b with the secret lab, you decided that the distance from the current floor x to the chosen y must be strictly less than the distance from the current floor x to floor b with the secret lab. Formally, it means that the following inequation must fulfill: |x - y| < |x - b|. After the lift successfully transports you to floor y, you write down number y in your notepad. Your task is to find the number of distinct number sequences that you could have written in the notebook as the result of k trips in the lift. As the sought number of trips can be rather large, find the remainder after dividing the number by 1000000007 (109 + 7). Input The first line of the input contains four space-separated integers n, a, b, k (2 ≀ n ≀ 5000, 1 ≀ k ≀ 5000, 1 ≀ a, b ≀ n, a β‰  b). Output Print a single integer β€” the remainder after dividing the sought number of sequences by 1000000007 (109 + 7). Examples Input 5 2 4 1 Output 2 Input 5 2 4 2 Output 2 Input 5 3 4 1 Output 0 Note Two sequences p1, p2, ..., pk and q1, q2, ..., qk are distinct, if there is such integer j (1 ≀ j ≀ k), that pj β‰  qj. Notes to the samples: 1. In the first sample after the first trip you are either on floor 1, or on floor 3, because |1 - 2| < |2 - 4| and |3 - 2| < |2 - 4|. 2. In the second sample there are two possible sequences: (1, 2); (1, 3). You cannot choose floor 3 for the first trip because in this case no floor can be the floor for the second trip. 3. In the third sample there are no sought sequences, because you cannot choose the floor for the first trip. Tags: combinatorics, dp Correct Solution: ``` from sys import stdin #parser def parser(): return map(int, stdin.readline().split()) #Guardando el valor de 10^9+7 div=pow(10,9)+7 #Recibiendo los valores de n,a,b,k de la entrada n,a,b,k=parser() #Actualizando los valores de a,b y n if a>b: a=n-a+1 n=n-b b=n+1 else: n=b-1 #Lista para ir guardando las sumas parciales prefix_sum=[0 for x in range(n+1)] #Lista para ir guandando los valores d(i,j) secuences=[0 for x in range(n+1)] secuences[a]=1 for i in range(k): #0 piso ficticio que se usa como comodΓ­n prefix_sum[0]=secuences[0] #Calculando los valores de las sumas parciales for j in range(1,n+1): prefix_sum[j]=prefix_sum[j-1]+secuences[j] prefix_sum[j]%=div #Calculando los nuevos valores de ''secuences'' for j in range(1,n+1): distance=b-j mid_distance=0 #distancia necesaria entre el piso j y un piso de mayor numeracion que el j if distance % 2 == 0: mid_distance=distance//2-1 else: mid_distance=distance//2 #prefix_sum[j-1] es la cantidad de formas de alcanzar el piso j por uno de menor numeracion #prefix_sum[j+mid_distance]-prefix_sum[j] es la cantidad de formas de alcanzar el piso j por un piso de mayor numeracion secuences[j]=prefix_sum[j-1]+prefix_sum[j+mid_distance]-prefix_sum[j] secuences[j]%=div #Imprimiendo el resultado print(sum(secuences)%div) ```
90,494
Provide tags and a correct Python 3 solution for this coding contest problem. Imagine that you are in a building that has exactly n floors. You can move between the floors in a lift. Let's number the floors from bottom to top with integers from 1 to n. Now you're on the floor number a. You are very bored, so you want to take the lift. Floor number b has a secret lab, the entry is forbidden. However, you already are in the mood and decide to make k consecutive trips in the lift. Let us suppose that at the moment you are on the floor number x (initially, you were on floor a). For another trip between floors you choose some floor with number y (y β‰  x) and the lift travels to this floor. As you cannot visit floor b with the secret lab, you decided that the distance from the current floor x to the chosen y must be strictly less than the distance from the current floor x to floor b with the secret lab. Formally, it means that the following inequation must fulfill: |x - y| < |x - b|. After the lift successfully transports you to floor y, you write down number y in your notepad. Your task is to find the number of distinct number sequences that you could have written in the notebook as the result of k trips in the lift. As the sought number of trips can be rather large, find the remainder after dividing the number by 1000000007 (109 + 7). Input The first line of the input contains four space-separated integers n, a, b, k (2 ≀ n ≀ 5000, 1 ≀ k ≀ 5000, 1 ≀ a, b ≀ n, a β‰  b). Output Print a single integer β€” the remainder after dividing the sought number of sequences by 1000000007 (109 + 7). Examples Input 5 2 4 1 Output 2 Input 5 2 4 2 Output 2 Input 5 3 4 1 Output 0 Note Two sequences p1, p2, ..., pk and q1, q2, ..., qk are distinct, if there is such integer j (1 ≀ j ≀ k), that pj β‰  qj. Notes to the samples: 1. In the first sample after the first trip you are either on floor 1, or on floor 3, because |1 - 2| < |2 - 4| and |3 - 2| < |2 - 4|. 2. In the second sample there are two possible sequences: (1, 2); (1, 3). You cannot choose floor 3 for the first trip because in this case no floor can be the floor for the second trip. 3. In the third sample there are no sought sequences, because you cannot choose the floor for the first trip. Tags: combinatorics, dp Correct Solution: ``` def solve(n, st, k): MOD = int(1e9 + 7) dp = [0] * (n + 1) prefix_sum = [0] * (n + 1) dp[st] = 1 for times in range(k): prefix_sum[0] = 0 for i in range(1, n + 1): prefix_sum[i] = prefix_sum[i - 1] + dp[i] if prefix_sum[i] >= MOD: prefix_sum[i] -= MOD for i in range(1, n + 1): dp[i] = prefix_sum[n] - dp[i] - prefix_sum[i >> 1] while dp[i] < 0: dp[i] += MOD while dp[i] >= MOD: dp[i] -= MOD return sum(dp) % MOD def main(): n, a, b, k = [int(i) for i in input().split()] if a > b: print(solve(n - b, a - b, k)) else: print(solve(b - 1, b - a, k)) main() ```
90,495
Provide tags and a correct Python 3 solution for this coding contest problem. Imagine that you are in a building that has exactly n floors. You can move between the floors in a lift. Let's number the floors from bottom to top with integers from 1 to n. Now you're on the floor number a. You are very bored, so you want to take the lift. Floor number b has a secret lab, the entry is forbidden. However, you already are in the mood and decide to make k consecutive trips in the lift. Let us suppose that at the moment you are on the floor number x (initially, you were on floor a). For another trip between floors you choose some floor with number y (y β‰  x) and the lift travels to this floor. As you cannot visit floor b with the secret lab, you decided that the distance from the current floor x to the chosen y must be strictly less than the distance from the current floor x to floor b with the secret lab. Formally, it means that the following inequation must fulfill: |x - y| < |x - b|. After the lift successfully transports you to floor y, you write down number y in your notepad. Your task is to find the number of distinct number sequences that you could have written in the notebook as the result of k trips in the lift. As the sought number of trips can be rather large, find the remainder after dividing the number by 1000000007 (109 + 7). Input The first line of the input contains four space-separated integers n, a, b, k (2 ≀ n ≀ 5000, 1 ≀ k ≀ 5000, 1 ≀ a, b ≀ n, a β‰  b). Output Print a single integer β€” the remainder after dividing the sought number of sequences by 1000000007 (109 + 7). Examples Input 5 2 4 1 Output 2 Input 5 2 4 2 Output 2 Input 5 3 4 1 Output 0 Note Two sequences p1, p2, ..., pk and q1, q2, ..., qk are distinct, if there is such integer j (1 ≀ j ≀ k), that pj β‰  qj. Notes to the samples: 1. In the first sample after the first trip you are either on floor 1, or on floor 3, because |1 - 2| < |2 - 4| and |3 - 2| < |2 - 4|. 2. In the second sample there are two possible sequences: (1, 2); (1, 3). You cannot choose floor 3 for the first trip because in this case no floor can be the floor for the second trip. 3. In the third sample there are no sought sequences, because you cannot choose the floor for the first trip. Tags: combinatorics, dp Correct Solution: ``` #!/usr/bin/env python3 import io import os import sys input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline def printd(*args, **kwargs): #print(*args, **kwargs, file=sys.stderr) #print(*args, **kwargs) pass def get_str(): return input().decode().strip() def rint(): return map(int, input().split()) def oint(): return int(input()) mod = 1000000007 n, a, b, k = rint() if a > b: a, b = n-a+1, n-b+1 a -= 1 b -= 1 printd(n, a, b, k) d = [0]*n d[a] = 1 ps = [0]*b ps[0] = d[0] for j in range(1, b): ps[j] = ps[j-1]+d[j] while ps[j] > mod: ps[j] -= mod printd(n, a, b, k) printd(d, ps) for i in range(k): for j in range(b): #b-t > t-j #2*t < b+j #t < (b+j)/2 if (b+j)%2: t = (b+j)//2 else: t = (b+j)//2 - 1 if j == 0: d[j] = ps[t] - ps[j] else: d[j] = ps[t] - ps[j] + ps[j-1] while d[j] > mod: d[j] -= mod while d[j] <0: d[j] += mod #d[j] %=mod ps[0] = d[0] for j in range(1, b): ps[j] = (ps[j-1]+d[j])# %mod while ps[j] > mod: ps[j] -= mod while ps[j] < 0: ps[j] += mod printd(d,ps) ans = ps[b-1] print(ans%mod) ```
90,496
Provide tags and a correct Python 3 solution for this coding contest problem. Imagine that you are in a building that has exactly n floors. You can move between the floors in a lift. Let's number the floors from bottom to top with integers from 1 to n. Now you're on the floor number a. You are very bored, so you want to take the lift. Floor number b has a secret lab, the entry is forbidden. However, you already are in the mood and decide to make k consecutive trips in the lift. Let us suppose that at the moment you are on the floor number x (initially, you were on floor a). For another trip between floors you choose some floor with number y (y β‰  x) and the lift travels to this floor. As you cannot visit floor b with the secret lab, you decided that the distance from the current floor x to the chosen y must be strictly less than the distance from the current floor x to floor b with the secret lab. Formally, it means that the following inequation must fulfill: |x - y| < |x - b|. After the lift successfully transports you to floor y, you write down number y in your notepad. Your task is to find the number of distinct number sequences that you could have written in the notebook as the result of k trips in the lift. As the sought number of trips can be rather large, find the remainder after dividing the number by 1000000007 (109 + 7). Input The first line of the input contains four space-separated integers n, a, b, k (2 ≀ n ≀ 5000, 1 ≀ k ≀ 5000, 1 ≀ a, b ≀ n, a β‰  b). Output Print a single integer β€” the remainder after dividing the sought number of sequences by 1000000007 (109 + 7). Examples Input 5 2 4 1 Output 2 Input 5 2 4 2 Output 2 Input 5 3 4 1 Output 0 Note Two sequences p1, p2, ..., pk and q1, q2, ..., qk are distinct, if there is such integer j (1 ≀ j ≀ k), that pj β‰  qj. Notes to the samples: 1. In the first sample after the first trip you are either on floor 1, or on floor 3, because |1 - 2| < |2 - 4| and |3 - 2| < |2 - 4|. 2. In the second sample there are two possible sequences: (1, 2); (1, 3). You cannot choose floor 3 for the first trip because in this case no floor can be the floor for the second trip. 3. In the third sample there are no sought sequences, because you cannot choose the floor for the first trip. Tags: combinatorics, dp Correct Solution: ``` #!/usr/bin/env python3 import io import os import sys input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline def printd(*args, **kwargs): #print(*args, **kwargs, file=sys.stderr) #print(*args, **kwargs) pass def get_str(): return input().decode().strip() def rint(): return map(int, input().split()) def oint(): return int(input()) mod = 1000000007 n, a, b, k = rint() if a > b: a, b = n-a+1, n-b+1 a -= 1 b -= 1 printd(n, a, b, k) d = [0]*n d[a] = 1 ps = [0]*b ps[0] = d[0] for j in range(1, b): ps[j] = ps[j-1]+d[j] while ps[j] > mod: ps[j] -= mod ps[j] %= mod printd(n, a, b, k) printd(d, ps) for i in range(k): for j in range(b): #b-t > t-j #2*t < b+j #t < (b+j)/2 if (b+j)%2: t = (b+j)//2 else: t = (b+j)//2 - 1 if j == 0: d[j] = ps[t] - ps[j] else: d[j] = ps[t] - ps[j] + ps[j-1] while d[j] > mod: d[j] -= mod while d[j] <0: d[j] += mod d[j] %= mod #d[j] %=mod ps[0] = d[0] for j in range(1, b): ps[j] = (ps[j-1]+d[j])# %mod while ps[j] > mod: ps[j] -= mod while ps[j] < 0: ps[j] += mod ps[j] %= mod printd(d,ps) ans = ps[b-1] print(ans%mod) ```
90,497
Provide tags and a correct Python 2 solution for this coding contest problem. Imagine that you are in a building that has exactly n floors. You can move between the floors in a lift. Let's number the floors from bottom to top with integers from 1 to n. Now you're on the floor number a. You are very bored, so you want to take the lift. Floor number b has a secret lab, the entry is forbidden. However, you already are in the mood and decide to make k consecutive trips in the lift. Let us suppose that at the moment you are on the floor number x (initially, you were on floor a). For another trip between floors you choose some floor with number y (y β‰  x) and the lift travels to this floor. As you cannot visit floor b with the secret lab, you decided that the distance from the current floor x to the chosen y must be strictly less than the distance from the current floor x to floor b with the secret lab. Formally, it means that the following inequation must fulfill: |x - y| < |x - b|. After the lift successfully transports you to floor y, you write down number y in your notepad. Your task is to find the number of distinct number sequences that you could have written in the notebook as the result of k trips in the lift. As the sought number of trips can be rather large, find the remainder after dividing the number by 1000000007 (109 + 7). Input The first line of the input contains four space-separated integers n, a, b, k (2 ≀ n ≀ 5000, 1 ≀ k ≀ 5000, 1 ≀ a, b ≀ n, a β‰  b). Output Print a single integer β€” the remainder after dividing the sought number of sequences by 1000000007 (109 + 7). Examples Input 5 2 4 1 Output 2 Input 5 2 4 2 Output 2 Input 5 3 4 1 Output 0 Note Two sequences p1, p2, ..., pk and q1, q2, ..., qk are distinct, if there is such integer j (1 ≀ j ≀ k), that pj β‰  qj. Notes to the samples: 1. In the first sample after the first trip you are either on floor 1, or on floor 3, because |1 - 2| < |2 - 4| and |3 - 2| < |2 - 4|. 2. In the second sample there are two possible sequences: (1, 2); (1, 3). You cannot choose floor 3 for the first trip because in this case no floor can be the floor for the second trip. 3. In the third sample there are no sought sequences, because you cannot choose the floor for the first trip. Tags: combinatorics, dp Correct Solution: ``` from sys import stdin, stdout from collections import Counter, defaultdict from itertools import permutations, combinations raw_input = stdin.readline pr = stdout.write def in_num(): return int(raw_input()) def in_arr(): return map(int,raw_input().split()) def pr_num(n): stdout.write(str(n)+'\n') def pr_arr(arr): pr(' '.join(map(str,arr))+'\n') # fast read function for total integer input def inp(): # this function returns whole input of # space/line seperated integers # Use Ctrl+D to flush stdin. return map(int,stdin.read().split()) range = xrange # not for python 3.0+ mod=10**9+7 n,a,b,k=in_arr() dp=[[0 for i in range(n+1)] for j in range(k+1)] dp[0][a-1]=1 dp[0][a]=(-1)%mod b-=1 for i in range(k): temp=0 for j in range(n): temp=(temp+dp[i][j])%mod x=int(abs(b-j)) x-=1 if x<=0: continue dp[i+1][max(0,j-x)]=(dp[i+1][max(0,j-x)]+temp)%mod dp[i+1][min(n,j+x+1)]=(dp[i+1][min(n,j+x+1)]-temp)%mod dp[i+1][j]=(dp[i+1][j]-temp)%mod dp[i+1][j+1]=(dp[i+1][j+1]+temp)%mod ans=0 temp=0 for i in range(n): temp+=dp[k][i] temp%=mod ans+=temp ans%=mod pr_num(ans) ```
90,498
Provide tags and a correct Python 2 solution for this coding contest problem. Imagine that you are in a building that has exactly n floors. You can move between the floors in a lift. Let's number the floors from bottom to top with integers from 1 to n. Now you're on the floor number a. You are very bored, so you want to take the lift. Floor number b has a secret lab, the entry is forbidden. However, you already are in the mood and decide to make k consecutive trips in the lift. Let us suppose that at the moment you are on the floor number x (initially, you were on floor a). For another trip between floors you choose some floor with number y (y β‰  x) and the lift travels to this floor. As you cannot visit floor b with the secret lab, you decided that the distance from the current floor x to the chosen y must be strictly less than the distance from the current floor x to floor b with the secret lab. Formally, it means that the following inequation must fulfill: |x - y| < |x - b|. After the lift successfully transports you to floor y, you write down number y in your notepad. Your task is to find the number of distinct number sequences that you could have written in the notebook as the result of k trips in the lift. As the sought number of trips can be rather large, find the remainder after dividing the number by 1000000007 (109 + 7). Input The first line of the input contains four space-separated integers n, a, b, k (2 ≀ n ≀ 5000, 1 ≀ k ≀ 5000, 1 ≀ a, b ≀ n, a β‰  b). Output Print a single integer β€” the remainder after dividing the sought number of sequences by 1000000007 (109 + 7). Examples Input 5 2 4 1 Output 2 Input 5 2 4 2 Output 2 Input 5 3 4 1 Output 0 Note Two sequences p1, p2, ..., pk and q1, q2, ..., qk are distinct, if there is such integer j (1 ≀ j ≀ k), that pj β‰  qj. Notes to the samples: 1. In the first sample after the first trip you are either on floor 1, or on floor 3, because |1 - 2| < |2 - 4| and |3 - 2| < |2 - 4|. 2. In the second sample there are two possible sequences: (1, 2); (1, 3). You cannot choose floor 3 for the first trip because in this case no floor can be the floor for the second trip. 3. In the third sample there are no sought sequences, because you cannot choose the floor for the first trip. Tags: combinatorics, dp Correct Solution: ``` from sys import stdin, stdout from collections import Counter, defaultdict from itertools import permutations, combinations raw_input = stdin.readline pr = stdout.write def in_num(): return int(raw_input()) def in_arr(): return map(int,raw_input().split()) def pr_num(n): stdout.write(str(n)+'\n') def pr_arr(arr): pr(' '.join(map(str,arr))+'\n') # fast read function for total integer input def inp(): # this function returns whole input of # space/line seperated integers # Use Ctrl+D to flush stdin. return map(int,stdin.read().split()) range = xrange # not for python 3.0+ mod=10**9+7 n,a,b,k=in_arr() dp=[[0 for i in range(n+1)] for j in range(k+1)] dp[0][a-1]=1 dp[0][a]=(-1)%mod b-=1 for i in range(k): temp=0 for j in range(n+1): temp=(temp+dp[i][j])%mod if i and int(abs(b-j))>1: temp-=dp[i-1][j] temp%=mod dp[i][j]=temp if j<n: x=int(abs(b-j)) x-=1 if x<=0: continue dp[i+1][max(0,j-x)]=(dp[i+1][max(0,j-x)]+temp)%mod dp[i+1][min(n,j+x+1)]=(dp[i+1][min(n,j+x+1)]-temp)%mod #dp[i+1][j]=(dp[i+1][j]-1)%mod #dp[i+1][j+1]=(dp[i+1][j+1]+1)%mod if i and int(abs(b-j))>1: temp+=dp[i-1][j] temp%=mod ans=0 temp=0 for i in range(n): temp+=dp[k][i] temp%=mod if int(abs(i-b))>1: temp-=dp[k-1][i] temp%=mod ans+=temp ans%=mod if int(abs(i-b))>1: temp+=dp[k-1][i] temp%=mod pr_num(ans) ```
90,499