message
stringlengths
2
22.8k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
16
109k
cluster
float64
1
1
__index_level_0__
int64
32
217k
Provide tags and a correct Python 3 solution for this coding contest problem. There are n cities situated along the main road of Berland. Cities are represented by their coordinates — integer numbers a1, a2, ..., an. All coordinates are pairwise distinct. It is possible to get from one city to another only by bus. But all buses and roads are very old, so the Minister of Transport decided to build a new bus route. The Minister doesn't want to spend large amounts of money — he wants to choose two cities in such a way that the distance between them is minimal possible. The distance between two cities is equal to the absolute value of the difference between their coordinates. It is possible that there are multiple pairs of cities with minimal possible distance, so the Minister wants to know the quantity of such pairs. Your task is to write a program that will calculate the minimal possible distance between two pairs of cities and the quantity of pairs which have this distance. Input The first line contains one integer number n (2 ≤ n ≤ 2·105). The second line contains n integer numbers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). All numbers ai are pairwise distinct. Output Print two integer numbers — the minimal distance and the quantity of pairs with this distance. Examples Input 4 6 -3 0 4 Output 2 1 Input 3 -2 0 2 Output 2 2 Note In the first example the distance between the first city and the fourth city is |4 - 6| = 2, and it is the only pair with this distance.
instruction
0
97,521
1
195,042
Tags: implementation, sortings Correct Solution: ``` def main(): input() a = map(int, input().split()) a = sorted(a) d = [a[i + 1] - a[i] for i in range(len(a) - 1)] m = int(2 * 1e9 + 1) c = 0 for x in d: if x < m: m = x c = 1 elif x == m: c += 1 print(m, c) if __name__ == '__main__': # import sys # sys.stdin = open("A.txt") main() ```
output
1
97,521
1
195,043
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n cities situated along the main road of Berland. Cities are represented by their coordinates — integer numbers a1, a2, ..., an. All coordinates are pairwise distinct. It is possible to get from one city to another only by bus. But all buses and roads are very old, so the Minister of Transport decided to build a new bus route. The Minister doesn't want to spend large amounts of money — he wants to choose two cities in such a way that the distance between them is minimal possible. The distance between two cities is equal to the absolute value of the difference between their coordinates. It is possible that there are multiple pairs of cities with minimal possible distance, so the Minister wants to know the quantity of such pairs. Your task is to write a program that will calculate the minimal possible distance between two pairs of cities and the quantity of pairs which have this distance. Input The first line contains one integer number n (2 ≤ n ≤ 2·105). The second line contains n integer numbers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). All numbers ai are pairwise distinct. Output Print two integer numbers — the minimal distance and the quantity of pairs with this distance. Examples Input 4 6 -3 0 4 Output 2 1 Input 3 -2 0 2 Output 2 2 Note In the first example the distance between the first city and the fourth city is |4 - 6| = 2, and it is the only pair with this distance. Submitted Solution: ``` n=int(input()) a=sorted(list(map(int,input().split()))) diff= 20000000000 c=0 for i in range(1,n): if abs(a[i]-a[i-1]) < diff: diff=abs(a[i]-a[i-1]) for i in range(1, n): if abs(a[i]-a[i-1])== diff: c+=1 print(diff, c) ```
instruction
0
97,522
1
195,044
Yes
output
1
97,522
1
195,045
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n cities situated along the main road of Berland. Cities are represented by their coordinates — integer numbers a1, a2, ..., an. All coordinates are pairwise distinct. It is possible to get from one city to another only by bus. But all buses and roads are very old, so the Minister of Transport decided to build a new bus route. The Minister doesn't want to spend large amounts of money — he wants to choose two cities in such a way that the distance between them is minimal possible. The distance between two cities is equal to the absolute value of the difference between their coordinates. It is possible that there are multiple pairs of cities with minimal possible distance, so the Minister wants to know the quantity of such pairs. Your task is to write a program that will calculate the minimal possible distance between two pairs of cities and the quantity of pairs which have this distance. Input The first line contains one integer number n (2 ≤ n ≤ 2·105). The second line contains n integer numbers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). All numbers ai are pairwise distinct. Output Print two integer numbers — the minimal distance and the quantity of pairs with this distance. Examples Input 4 6 -3 0 4 Output 2 1 Input 3 -2 0 2 Output 2 2 Note In the first example the distance between the first city and the fourth city is |4 - 6| = 2, and it is the only pair with this distance. Submitted Solution: ``` n = int(input()) mn = 2 * 10 ** 9 + 1 a = [int(i) for i in input().split()] a.sort() cnt = 0 for i in range(n-1): x = abs(a[i] - a[i+1]) if x < mn: mn = x cnt = 1 elif x == mn: cnt = cnt + 1 print(mn, cnt) ```
instruction
0
97,523
1
195,046
Yes
output
1
97,523
1
195,047
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n cities situated along the main road of Berland. Cities are represented by their coordinates — integer numbers a1, a2, ..., an. All coordinates are pairwise distinct. It is possible to get from one city to another only by bus. But all buses and roads are very old, so the Minister of Transport decided to build a new bus route. The Minister doesn't want to spend large amounts of money — he wants to choose two cities in such a way that the distance between them is minimal possible. The distance between two cities is equal to the absolute value of the difference between their coordinates. It is possible that there are multiple pairs of cities with minimal possible distance, so the Minister wants to know the quantity of such pairs. Your task is to write a program that will calculate the minimal possible distance between two pairs of cities and the quantity of pairs which have this distance. Input The first line contains one integer number n (2 ≤ n ≤ 2·105). The second line contains n integer numbers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). All numbers ai are pairwise distinct. Output Print two integer numbers — the minimal distance and the quantity of pairs with this distance. Examples Input 4 6 -3 0 4 Output 2 1 Input 3 -2 0 2 Output 2 2 Note In the first example the distance between the first city and the fourth city is |4 - 6| = 2, and it is the only pair with this distance. Submitted Solution: ``` n = int(input()) coords = sorted(map(int, input().split())) diffs = [coords[i]-coords[i-1] for i in range(1,n)] minimum = min(diffs) total = diffs.count(minimum) print(minimum, total) ```
instruction
0
97,524
1
195,048
Yes
output
1
97,524
1
195,049
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n cities situated along the main road of Berland. Cities are represented by their coordinates — integer numbers a1, a2, ..., an. All coordinates are pairwise distinct. It is possible to get from one city to another only by bus. But all buses and roads are very old, so the Minister of Transport decided to build a new bus route. The Minister doesn't want to spend large amounts of money — he wants to choose two cities in such a way that the distance between them is minimal possible. The distance between two cities is equal to the absolute value of the difference between their coordinates. It is possible that there are multiple pairs of cities with minimal possible distance, so the Minister wants to know the quantity of such pairs. Your task is to write a program that will calculate the minimal possible distance between two pairs of cities and the quantity of pairs which have this distance. Input The first line contains one integer number n (2 ≤ n ≤ 2·105). The second line contains n integer numbers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). All numbers ai are pairwise distinct. Output Print two integer numbers — the minimal distance and the quantity of pairs with this distance. Examples Input 4 6 -3 0 4 Output 2 1 Input 3 -2 0 2 Output 2 2 Note In the first example the distance between the first city and the fourth city is |4 - 6| = 2, and it is the only pair with this distance. Submitted Solution: ``` n = int(input()) a = sorted(map(int, input().split())) d, c = 10000000000000000000000000, 1 for x in range(n-1): dx = a[x+1]-a[x] if dx < d: d = dx c = 1 elif dx == d: c += 1 print(d,c) ```
instruction
0
97,525
1
195,050
Yes
output
1
97,525
1
195,051
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n cities situated along the main road of Berland. Cities are represented by their coordinates — integer numbers a1, a2, ..., an. All coordinates are pairwise distinct. It is possible to get from one city to another only by bus. But all buses and roads are very old, so the Minister of Transport decided to build a new bus route. The Minister doesn't want to spend large amounts of money — he wants to choose two cities in such a way that the distance between them is minimal possible. The distance between two cities is equal to the absolute value of the difference between their coordinates. It is possible that there are multiple pairs of cities with minimal possible distance, so the Minister wants to know the quantity of such pairs. Your task is to write a program that will calculate the minimal possible distance between two pairs of cities and the quantity of pairs which have this distance. Input The first line contains one integer number n (2 ≤ n ≤ 2·105). The second line contains n integer numbers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). All numbers ai are pairwise distinct. Output Print two integer numbers — the minimal distance and the quantity of pairs with this distance. Examples Input 4 6 -3 0 4 Output 2 1 Input 3 -2 0 2 Output 2 2 Note In the first example the distance between the first city and the fourth city is |4 - 6| = 2, and it is the only pair with this distance. Submitted Solution: ``` n = int(input()) points = [int(x) for x in input().split(" ")] points.sort() min = 99999999999999 count = 0 for i in range(len(points) - 1): distance = abs(points[i+1] - points[i]) if distance < min: min = distance count = 1 elif min == distance: count += 1 print("{0} {1}".format(distance, count)) ```
instruction
0
97,526
1
195,052
No
output
1
97,526
1
195,053
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n cities situated along the main road of Berland. Cities are represented by their coordinates — integer numbers a1, a2, ..., an. All coordinates are pairwise distinct. It is possible to get from one city to another only by bus. But all buses and roads are very old, so the Minister of Transport decided to build a new bus route. The Minister doesn't want to spend large amounts of money — he wants to choose two cities in such a way that the distance between them is minimal possible. The distance between two cities is equal to the absolute value of the difference between their coordinates. It is possible that there are multiple pairs of cities with minimal possible distance, so the Minister wants to know the quantity of such pairs. Your task is to write a program that will calculate the minimal possible distance between two pairs of cities and the quantity of pairs which have this distance. Input The first line contains one integer number n (2 ≤ n ≤ 2·105). The second line contains n integer numbers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). All numbers ai are pairwise distinct. Output Print two integer numbers — the minimal distance and the quantity of pairs with this distance. Examples Input 4 6 -3 0 4 Output 2 1 Input 3 -2 0 2 Output 2 2 Note In the first example the distance between the first city and the fourth city is |4 - 6| = 2, and it is the only pair with this distance. Submitted Solution: ``` def getilist(): return list(map(int, input().split())) n = int(input()) a = sorted(getilist()) lb = 2e9 c = 0 for i in range(n-1): c += 1 t = abs(a[i] - a[i+1]) if t < lb: lb = t c = 1 print(lb, c) ```
instruction
0
97,527
1
195,054
No
output
1
97,527
1
195,055
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n cities situated along the main road of Berland. Cities are represented by their coordinates — integer numbers a1, a2, ..., an. All coordinates are pairwise distinct. It is possible to get from one city to another only by bus. But all buses and roads are very old, so the Minister of Transport decided to build a new bus route. The Minister doesn't want to spend large amounts of money — he wants to choose two cities in such a way that the distance between them is minimal possible. The distance between two cities is equal to the absolute value of the difference between their coordinates. It is possible that there are multiple pairs of cities with minimal possible distance, so the Minister wants to know the quantity of such pairs. Your task is to write a program that will calculate the minimal possible distance between two pairs of cities and the quantity of pairs which have this distance. Input The first line contains one integer number n (2 ≤ n ≤ 2·105). The second line contains n integer numbers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). All numbers ai are pairwise distinct. Output Print two integer numbers — the minimal distance and the quantity of pairs with this distance. Examples Input 4 6 -3 0 4 Output 2 1 Input 3 -2 0 2 Output 2 2 Note In the first example the distance between the first city and the fourth city is |4 - 6| = 2, and it is the only pair with this distance. Submitted Solution: ``` input() cities = input().split() cities = list(map(int, cities)) min_dist = 9999 min_count = 0 for idx, x in enumerate(cities): for y in cities[idx + 1:]: xy = abs(x - y) if xy < min_dist: min_dist = xy min_count = 1 continue if xy == min_dist: min_count += 1 print("{} {}".format(min_dist, min_count)) ```
instruction
0
97,528
1
195,056
No
output
1
97,528
1
195,057
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There are n cities situated along the main road of Berland. Cities are represented by their coordinates — integer numbers a1, a2, ..., an. All coordinates are pairwise distinct. It is possible to get from one city to another only by bus. But all buses and roads are very old, so the Minister of Transport decided to build a new bus route. The Minister doesn't want to spend large amounts of money — he wants to choose two cities in such a way that the distance between them is minimal possible. The distance between two cities is equal to the absolute value of the difference between their coordinates. It is possible that there are multiple pairs of cities with minimal possible distance, so the Minister wants to know the quantity of such pairs. Your task is to write a program that will calculate the minimal possible distance between two pairs of cities and the quantity of pairs which have this distance. Input The first line contains one integer number n (2 ≤ n ≤ 2·105). The second line contains n integer numbers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). All numbers ai are pairwise distinct. Output Print two integer numbers — the minimal distance and the quantity of pairs with this distance. Examples Input 4 6 -3 0 4 Output 2 1 Input 3 -2 0 2 Output 2 2 Note In the first example the distance between the first city and the fourth city is |4 - 6| = 2, and it is the only pair with this distance. Submitted Solution: ``` import sys input=sys.stdin.readline from collections import defaultdict as dc from collections import Counter from bisect import bisect_right, bisect_left import math from operator import itemgetter from heapq import heapify, heappop, heappush from queue import PriorityQueue as pq n=int(input()) l=list(map(int,input().split())) l.sort() x=dc(int) m=sys.maxsize for i in range(n-1): p=abs(l[i]-l[i+1]) x[p]+=1 m=min(m,p) print(p,x[p]) ```
instruction
0
97,529
1
195,058
No
output
1
97,529
1
195,059
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Petr is a detective in Braginsk. Somebody stole a huge amount of money from a bank and Petr is to catch him. Somebody told Petr that some luxurious car moves along the roads without stopping. Petr knows that it is the robbers who drive the car. The roads in Braginsk are one-directional and each of them connects two intersections. Petr wants to select one intersection such that if the robbers continue to drive the roads indefinitely, they will sooner or later come to that intersection. The initial position of the robbers is unknown. Find such an intersection that fits the requirements. Input The first line of the input contains two integers n and m (2 ≤ n ≤ 10^5, 2 ≤ m ≤ 5 ⋅ 10^5) — the number of intersections and the number of directed roads in Braginsk, respectively. Each of the next m lines contains two integers u_i and v_i (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i) — the start and finish of the i-th directed road. It is guaranteed that the robbers can move along the roads indefinitely. Output Print a single integer k — the intersection Petr needs to choose. If there are multiple answers, print any. If there are no such intersections, print -1. Examples Input 5 6 1 2 2 3 3 1 3 4 4 5 5 3 Output 3 Input 3 3 1 2 2 3 3 1 Output 1 Note In the first example the robbers can move, for example, along the following routes: (1-2-3-1), (3-4-5-3), (1-2-3-4-5-3-1). We can show that if Petr chooses the 3-rd intersection, he will eventually meet the robbers independently of their route. Submitted Solution: ``` from collections import defaultdict from operator import itemgetter def find_path(graph, start, end, path=[]): path = path + [start] if start == end: return path if graph.get(start,0) == 0: return None for node in graph[start]: if node not in path: newpath = find_path(graph, node, end, path) if newpath: return newpath return None n , k = input().split() n , k =[int(n) , int(k) ] map = defaultdict(int) map2 = defaultdict(set) for i in range(k) : a , b = input().split() a , b =[int(a) , int(b) ] map[a] += 1 map[b] += 1 map2[a].add(b) map = sorted(map.items(), key=itemgetter(1),reverse = True) case = True for i in range(1,n+1) : if find_path(map2,map[0][0],i) == None : case = False break if case : print(map[0][0]) else : print(-1) ```
instruction
0
97,587
1
195,174
No
output
1
97,587
1
195,175
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Petr is a detective in Braginsk. Somebody stole a huge amount of money from a bank and Petr is to catch him. Somebody told Petr that some luxurious car moves along the roads without stopping. Petr knows that it is the robbers who drive the car. The roads in Braginsk are one-directional and each of them connects two intersections. Petr wants to select one intersection such that if the robbers continue to drive the roads indefinitely, they will sooner or later come to that intersection. The initial position of the robbers is unknown. Find such an intersection that fits the requirements. Input The first line of the input contains two integers n and m (2 ≤ n ≤ 10^5, 2 ≤ m ≤ 5 ⋅ 10^5) — the number of intersections and the number of directed roads in Braginsk, respectively. Each of the next m lines contains two integers u_i and v_i (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i) — the start and finish of the i-th directed road. It is guaranteed that the robbers can move along the roads indefinitely. Output Print a single integer k — the intersection Petr needs to choose. If there are multiple answers, print any. If there are no such intersections, print -1. Examples Input 5 6 1 2 2 3 3 1 3 4 4 5 5 3 Output 3 Input 3 3 1 2 2 3 3 1 Output 1 Note In the first example the robbers can move, for example, along the following routes: (1-2-3-1), (3-4-5-3), (1-2-3-4-5-3-1). We can show that if Petr chooses the 3-rd intersection, he will eventually meet the robbers independently of their route. Submitted Solution: ``` from collections import defaultdict graph=defaultdict(list) a=list(map(int,input().split())) l=[0]*(a[0]+1) for i in range(a[1]): b=list(map(int,input().split())) l[b[1]]=l[b[1]]+1 graph[b[0]].append(b[1]) vis=[False]*(a[0]+1) vis[1]=True nu=0 stack=[1] while(len(stack)!=0): di=stack.pop() vis[di]=True nu+=1 for val in graph[di]: if(vis[val]==False): stack.append(val) if(a[0]>a[1] or nu<a[0]): print("-1") else: c=l[1] t=1 for i in range(2,a[0]+1): if(c<=l[i]): c=l[i] t=i print(t) ```
instruction
0
97,588
1
195,176
No
output
1
97,588
1
195,177
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Petr is a detective in Braginsk. Somebody stole a huge amount of money from a bank and Petr is to catch him. Somebody told Petr that some luxurious car moves along the roads without stopping. Petr knows that it is the robbers who drive the car. The roads in Braginsk are one-directional and each of them connects two intersections. Petr wants to select one intersection such that if the robbers continue to drive the roads indefinitely, they will sooner or later come to that intersection. The initial position of the robbers is unknown. Find such an intersection that fits the requirements. Input The first line of the input contains two integers n and m (2 ≤ n ≤ 10^5, 2 ≤ m ≤ 5 ⋅ 10^5) — the number of intersections and the number of directed roads in Braginsk, respectively. Each of the next m lines contains two integers u_i and v_i (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i) — the start and finish of the i-th directed road. It is guaranteed that the robbers can move along the roads indefinitely. Output Print a single integer k — the intersection Petr needs to choose. If there are multiple answers, print any. If there are no such intersections, print -1. Examples Input 5 6 1 2 2 3 3 1 3 4 4 5 5 3 Output 3 Input 3 3 1 2 2 3 3 1 Output 1 Note In the first example the robbers can move, for example, along the following routes: (1-2-3-1), (3-4-5-3), (1-2-3-4-5-3-1). We can show that if Petr chooses the 3-rd intersection, he will eventually meet the robbers independently of their route. Submitted Solution: ``` from collections import defaultdict from operator import itemgetter def find_path(graph, start, end, path=[]): path = path + [start] if start == end: return path if graph.get(start,0) == 0: return None for node in graph[start]: if node not in path: newpath = find_path(graph, node, end, path) if newpath: return newpath return None n , k = input().split() n , k =[int(n) , int(k) ] map = defaultdict(int) map2 = defaultdict(set) for i in range(k) : a , b = input().split() a , b =[int(a) , int(b) ] map[a] += 1 map[b] += 1 map2[a].add(b) map = sorted(map.items(), key=itemgetter(1),reverse = True) case = True for inter,road in map : s = set() for i in range(1,n+1) : road = find_path(map2,inter,i) if road == None : case = False break else : s.add(len(road)) if n not in s : case = False if not case : break if case : print(map[0][0]) else : print(-1) ```
instruction
0
97,589
1
195,178
No
output
1
97,589
1
195,179
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Petr is a detective in Braginsk. Somebody stole a huge amount of money from a bank and Petr is to catch him. Somebody told Petr that some luxurious car moves along the roads without stopping. Petr knows that it is the robbers who drive the car. The roads in Braginsk are one-directional and each of them connects two intersections. Petr wants to select one intersection such that if the robbers continue to drive the roads indefinitely, they will sooner or later come to that intersection. The initial position of the robbers is unknown. Find such an intersection that fits the requirements. Input The first line of the input contains two integers n and m (2 ≤ n ≤ 10^5, 2 ≤ m ≤ 5 ⋅ 10^5) — the number of intersections and the number of directed roads in Braginsk, respectively. Each of the next m lines contains two integers u_i and v_i (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i) — the start and finish of the i-th directed road. It is guaranteed that the robbers can move along the roads indefinitely. Output Print a single integer k — the intersection Petr needs to choose. If there are multiple answers, print any. If there are no such intersections, print -1. Examples Input 5 6 1 2 2 3 3 1 3 4 4 5 5 3 Output 3 Input 3 3 1 2 2 3 3 1 Output 1 Note In the first example the robbers can move, for example, along the following routes: (1-2-3-1), (3-4-5-3), (1-2-3-4-5-3-1). We can show that if Petr chooses the 3-rd intersection, he will eventually meet the robbers independently of their route. Submitted Solution: ``` import collections; def getIntList(): return list(map(int, input().split())); def getTransIntList(n): first=getIntList(); m=len(first); result=[[0]*n for _ in range(m)]; for i in range(m): result[i][0]=first[i]; for j in range(1, n): curr=getIntList(); for i in range(m): result[i][j]=curr[i]; return result; n, m =getIntList(); Arcs=[]; for _ in range(m): u, v=getIntList(); Arcs.append((u-1, v-1)); prevVerts=[set() for _ in range(n)]; nextCount=[0 for _ in range(n)]; for a in Arcs: u, v=a; prevVerts[v].add(u); nextCount[u]+=1; #Удаляем тупики Verts=set(range(n)); badVerts=set(); deq=collections.deque(); for v in range(n): if nextCount[v]==0: deq.append(v); while len(deq)>0: v=deq.pop(); badVerts.add(v); for u in prevVerts[v]: nextCount[u]-=1; if nextCount[u]==0: deq.append(u); Verts-=badVerts; seen=set(); def solve(): for v in Verts: if v in seen: continue; currSeen=set(); deq=collections.deque([v]); nextRemoved=[0 for _ in range(n)]; while len(deq)>0: v1=deq.pop(); currSeen.add(v1); for u in prevVerts[v1]: if u in currSeen: continue; nextRemoved[u]+=1; if nextCount[u]==nextRemoved[u]: deq.append(u); #print(v, currSeen) if len(currSeen)==len(Verts): return v+1; seen.update(currSeen); return -1; print(solve()); ```
instruction
0
97,590
1
195,180
No
output
1
97,590
1
195,181
Provide a correct Python 3 solution for this coding contest problem. Heiankyo is known as a town with a grid of roads. Hokusai, a cat who lives in Heiankyo, has to go from his home to a secret place on the outskirts of town every day for patrol. However, I get tired of following the same path every day, and there is a risk of being followed, so Hokusai wants to use a different route every day as much as possible. On the other hand, Hokusai has a troublesome smell, so I don't want to go down the road away from my destination. There are Actinidia polygama on the roads here and there in Heiankyo, and Hokusai cannot go through the road where Actinidia polygama is falling. This is because if you go through such a road, you will be sick. Fortunately, there are no Actinidia polygama at the intersection. Hokusai wants to know the number of possible routes from his home to a secret place. Here, Hokusai's home is at (0, 0) and the secret place is at (gx, gy). The roads are laid out in a grid with x = i (i is an integer) and y = j (j is an integer). Notes on Submission Multiple datasets are given in the above format. The first line of input data gives the number of datasets. Create a program that outputs the output for each data set in order in the above format. Input The first line of input is given the coordinates of the secret location (gx, gy). All of these are integers between 1 and 15 and are given separated by a single space. The second line is given the number of sections where Actinidia polygama is falling p (0 ≤ p ≤ 100), and the following lines p are given one section for each line where Actinidia polygama is falling. The p sections are different from each other. One interval is expressed in the form of x1 y1 x2 y2 and is a line segment of length 1 parallel to the x-axis or y-axis, with (x1, y1) and (x2, y2) as endpoints. x1 and x2 are in the range [0, gx] and y1 and y2 are in the range [0, gy]. Output Output the number of possible routes. If there is no possible route, output "Miserable Hokusai!" On one line. Example Input 4 2 2 0 1 1 2 0 0 0 1 0 0 1 0 4 3 4 1 0 0 0 3 3 4 3 4 1 4 0 0 2 0 3 15 15 0 Output 6 Miserable Hokusai! 5 155117520
instruction
0
97,776
1
195,552
"Correct Solution: ``` t=int(input()) #mapH[j][i]=(i,j) for repeet in range(t): actinidia_tate=[] actinidia_yoko=[] gx,gy=[int(i) for i in input().split(" ")] mapH=[[0 for i in range(gx+1)] for j in range(gy+1)] mapH[0][0]=1 p=int(input()) for i in range(p): x1,y1,x2,y2=[int(j) for j in input().split(" ")] if x1==x2: actinidia_tate.append([x1,max(y1,y2)]) else: actinidia_yoko.append([max(x1,x2),y1]) for i in range(1,gx+1): try: actinidia_yoko.remove([i,0]) except: mapH[0][i]+=mapH[0][i-1] for j in range(1,gy+1): try: actinidia_tate.remove([0,j]) except: mapH[j][0]+=mapH[j-1][0] for i in range(1,gx+1): for j in range(1,gy+1): try: actinidia_yoko.remove([i,j]) except: mapH[j][i]+=mapH[j][i-1] try: actinidia_tate.remove([i,j]) except: mapH[j][i]+=mapH[j-1][i] if mapH[-1][-1]==0: print("Miserable Hokusai!") else: print(mapH[-1][-1]) ```
output
1
97,776
1
195,553
Provide a correct Python 3 solution for this coding contest problem. Heiankyo is known as a town with a grid of roads. Hokusai, a cat who lives in Heiankyo, has to go from his home to a secret place on the outskirts of town every day for patrol. However, I get tired of following the same path every day, and there is a risk of being followed, so Hokusai wants to use a different route every day as much as possible. On the other hand, Hokusai has a troublesome smell, so I don't want to go down the road away from my destination. There are Actinidia polygama on the roads here and there in Heiankyo, and Hokusai cannot go through the road where Actinidia polygama is falling. This is because if you go through such a road, you will be sick. Fortunately, there are no Actinidia polygama at the intersection. Hokusai wants to know the number of possible routes from his home to a secret place. Here, Hokusai's home is at (0, 0) and the secret place is at (gx, gy). The roads are laid out in a grid with x = i (i is an integer) and y = j (j is an integer). Notes on Submission Multiple datasets are given in the above format. The first line of input data gives the number of datasets. Create a program that outputs the output for each data set in order in the above format. Input The first line of input is given the coordinates of the secret location (gx, gy). All of these are integers between 1 and 15 and are given separated by a single space. The second line is given the number of sections where Actinidia polygama is falling p (0 ≤ p ≤ 100), and the following lines p are given one section for each line where Actinidia polygama is falling. The p sections are different from each other. One interval is expressed in the form of x1 y1 x2 y2 and is a line segment of length 1 parallel to the x-axis or y-axis, with (x1, y1) and (x2, y2) as endpoints. x1 and x2 are in the range [0, gx] and y1 and y2 are in the range [0, gy]. Output Output the number of possible routes. If there is no possible route, output "Miserable Hokusai!" On one line. Example Input 4 2 2 0 1 1 2 0 0 0 1 0 0 1 0 4 3 4 1 0 0 0 3 3 4 3 4 1 4 0 0 2 0 3 15 15 0 Output 6 Miserable Hokusai! 5 155117520
instruction
0
97,777
1
195,554
"Correct Solution: ``` N=int(input()) for n in range(N): gx,gy=map(int,input().split()) T=[[0 for i in range(gy+1)]for j in range(gx+1)] left=[[0 for i in range(gy+1)]for j in range(gx+1)] upper=[[0 for i in range(gy+1)]for j in range(gx+1)] p=int(input()) for i in range(p): x1,y1,x2,y2=map(int,input().split()) if x1==x2: upper[x1][max(y1,y2)]=1 if y1==y2: left[max(x1,x2)][y1]=1 for i in range(gx+1): for j in range(gy+1): if i==0 and j==0: T[0][0]=1 continue if i==0 and j>=1 and upper[i][j]==0 and left[i][j]==0: T[i][j]=T[i][j-1] continue if i>=1 and j==0 and upper[i][j]==0 and left[i][j]==0: T[i][j]=T[i-1][j] continue if left[i][j]==1 and upper[i][j]==1: T[i][j]=0 elif left[i][j]==0 and upper[i][j]==1: T[i][j]=T[i-1][j] elif left[i][j]==1 and upper[i][j]==0: T[i][j]=T[i][j-1] else: T[i][j]=T[i-1][j]+T[i][j-1] if T[gx][gy]==0: print("Miserable Hokusai!") else: print(T[gx][gy]) ```
output
1
97,777
1
195,555
Provide a correct Python 3 solution for this coding contest problem. Heiankyo is known as a town with a grid of roads. Hokusai, a cat who lives in Heiankyo, has to go from his home to a secret place on the outskirts of town every day for patrol. However, I get tired of following the same path every day, and there is a risk of being followed, so Hokusai wants to use a different route every day as much as possible. On the other hand, Hokusai has a troublesome smell, so I don't want to go down the road away from my destination. There are Actinidia polygama on the roads here and there in Heiankyo, and Hokusai cannot go through the road where Actinidia polygama is falling. This is because if you go through such a road, you will be sick. Fortunately, there are no Actinidia polygama at the intersection. Hokusai wants to know the number of possible routes from his home to a secret place. Here, Hokusai's home is at (0, 0) and the secret place is at (gx, gy). The roads are laid out in a grid with x = i (i is an integer) and y = j (j is an integer). Notes on Submission Multiple datasets are given in the above format. The first line of input data gives the number of datasets. Create a program that outputs the output for each data set in order in the above format. Input The first line of input is given the coordinates of the secret location (gx, gy). All of these are integers between 1 and 15 and are given separated by a single space. The second line is given the number of sections where Actinidia polygama is falling p (0 ≤ p ≤ 100), and the following lines p are given one section for each line where Actinidia polygama is falling. The p sections are different from each other. One interval is expressed in the form of x1 y1 x2 y2 and is a line segment of length 1 parallel to the x-axis or y-axis, with (x1, y1) and (x2, y2) as endpoints. x1 and x2 are in the range [0, gx] and y1 and y2 are in the range [0, gy]. Output Output the number of possible routes. If there is no possible route, output "Miserable Hokusai!" On one line. Example Input 4 2 2 0 1 1 2 0 0 0 1 0 0 1 0 4 3 4 1 0 0 0 3 3 4 3 4 1 4 0 0 2 0 3 15 15 0 Output 6 Miserable Hokusai! 5 155117520
instruction
0
97,778
1
195,556
"Correct Solution: ``` for _ in range(int(input())): X, Y = map(int,input().split()) m = int(input()) Judge = [[[0] * 2 for i in range(Y+1)] for j in range(X+1)] # Judはある点(x, y)において左から通路があるか、また上から通路あるかを判定するための配列 # 形式は # [x座標][y座標][0] = 0:点(x, y)において左からの通路あり # [x座標][y座標][0] = 1:点(x, y)において左からの通路なし # [x座標][y座標][1] = 0:点(x, y)において上からの通路あり # [x座標][y座標][1] = 1:点(x, y)において上からの通路なし for i in range(Y+1): Judge[0][i][0] = 1 for j in range(X+1): Judge[j][0][1] = 1 # 端っこの点は上から、または左からの通路が絶対にない Tot = [[0] * (Y+1) for i in range(X+1)] #Tot[x座標][y座標]が、点(x, y)まで行ける道の数 Tot[0][0] = 1 for i in range(m): A = list(map(int, input().split())) x1 = A[0] y1 = A[1] x2 = A[2] y2 = A[3] if x1 == x2: if y2 > y1: Judge[x2][y2][1] = 1 # (x,y)→(x,y+1)にマタタビがあるということだからJudge[x][y+1][0]=1 else: Judge[x2][y1][1] = 1 if y1 == y2: if x2 > x1: Judge[x2][y2][0] = 1 else: Judge[x1][y2][0] = 1 # print(Judge)→デバックで使いました。 for i in range(X+1): for j in range(Y+1): if Judge[i][j][0] == 0: Tot[i][j] += Tot[i-1][j] if Judge[i][j][1] == 0: Tot[i][j] += Tot[i][j-1] if Tot[X][Y] == 0: print("Miserable Hokusai!") else: print(Tot[X][Y]) ```
output
1
97,778
1
195,557
Provide a correct Python 3 solution for this coding contest problem. Heiankyo is known as a town with a grid of roads. Hokusai, a cat who lives in Heiankyo, has to go from his home to a secret place on the outskirts of town every day for patrol. However, I get tired of following the same path every day, and there is a risk of being followed, so Hokusai wants to use a different route every day as much as possible. On the other hand, Hokusai has a troublesome smell, so I don't want to go down the road away from my destination. There are Actinidia polygama on the roads here and there in Heiankyo, and Hokusai cannot go through the road where Actinidia polygama is falling. This is because if you go through such a road, you will be sick. Fortunately, there are no Actinidia polygama at the intersection. Hokusai wants to know the number of possible routes from his home to a secret place. Here, Hokusai's home is at (0, 0) and the secret place is at (gx, gy). The roads are laid out in a grid with x = i (i is an integer) and y = j (j is an integer). Notes on Submission Multiple datasets are given in the above format. The first line of input data gives the number of datasets. Create a program that outputs the output for each data set in order in the above format. Input The first line of input is given the coordinates of the secret location (gx, gy). All of these are integers between 1 and 15 and are given separated by a single space. The second line is given the number of sections where Actinidia polygama is falling p (0 ≤ p ≤ 100), and the following lines p are given one section for each line where Actinidia polygama is falling. The p sections are different from each other. One interval is expressed in the form of x1 y1 x2 y2 and is a line segment of length 1 parallel to the x-axis or y-axis, with (x1, y1) and (x2, y2) as endpoints. x1 and x2 are in the range [0, gx] and y1 and y2 are in the range [0, gy]. Output Output the number of possible routes. If there is no possible route, output "Miserable Hokusai!" On one line. Example Input 4 2 2 0 1 1 2 0 0 0 1 0 0 1 0 4 3 4 1 0 0 0 3 3 4 3 4 1 4 0 0 2 0 3 15 15 0 Output 6 Miserable Hokusai! 5 155117520
instruction
0
97,779
1
195,558
"Correct Solution: ``` n = int(input()) ans = [] for _ in range(n): gx,gy = map(int,input().split()) m = [[0 for i_ in range(gy+1)] for j_ in range(gx+1)] mat_num = int(input()) mat_h = [[0 for i_ in range(gy+1)] for j_ in range(gx+1)] mat_v = [[0 for i_ in range(gy+1)] for j_ in range(gx+1)] ifm = [[0 for i_ in range(gy+1)] for j_ in range(gx+1)] for __ in range(mat_num): x1,y1,x2,y2 = map(int,input().split()) if x1 == x2: mat_h[x1][max(y1,y2)] = 1 else: mat_v[max(x1,x2)][y1] = 1 queue = [(0,0)] while queue: x,y = queue.pop(0) if ifm[x][y] == 0: if x == 0: if y == 0: m[x][y] = 1 else: if mat_h[x][y] == 0: m[x][y] += m[x][y-1] else: pass else: if y == 0: if mat_v[x][y] == 0: m[x][y] += m[x-1][y] else: pass else: if mat_v[x][y] == 0: m[x][y] += m[x-1][y] if mat_h[x][y] == 0: m[x][y] += m[x][y-1] ifm[x][y] = 1 if x < gx: queue.append((x+1,y)) if y < gy: queue.append((x,y+1)) else: pass ans.append(m[gx][gy]) for x in ans: if x == 0: print('Miserable Hokusai!') else: print(x) ```
output
1
97,779
1
195,559
Provide a correct Python 3 solution for this coding contest problem. Heiankyo is known as a town with a grid of roads. Hokusai, a cat who lives in Heiankyo, has to go from his home to a secret place on the outskirts of town every day for patrol. However, I get tired of following the same path every day, and there is a risk of being followed, so Hokusai wants to use a different route every day as much as possible. On the other hand, Hokusai has a troublesome smell, so I don't want to go down the road away from my destination. There are Actinidia polygama on the roads here and there in Heiankyo, and Hokusai cannot go through the road where Actinidia polygama is falling. This is because if you go through such a road, you will be sick. Fortunately, there are no Actinidia polygama at the intersection. Hokusai wants to know the number of possible routes from his home to a secret place. Here, Hokusai's home is at (0, 0) and the secret place is at (gx, gy). The roads are laid out in a grid with x = i (i is an integer) and y = j (j is an integer). Notes on Submission Multiple datasets are given in the above format. The first line of input data gives the number of datasets. Create a program that outputs the output for each data set in order in the above format. Input The first line of input is given the coordinates of the secret location (gx, gy). All of these are integers between 1 and 15 and are given separated by a single space. The second line is given the number of sections where Actinidia polygama is falling p (0 ≤ p ≤ 100), and the following lines p are given one section for each line where Actinidia polygama is falling. The p sections are different from each other. One interval is expressed in the form of x1 y1 x2 y2 and is a line segment of length 1 parallel to the x-axis or y-axis, with (x1, y1) and (x2, y2) as endpoints. x1 and x2 are in the range [0, gx] and y1 and y2 are in the range [0, gy]. Output Output the number of possible routes. If there is no possible route, output "Miserable Hokusai!" On one line. Example Input 4 2 2 0 1 1 2 0 0 0 1 0 0 1 0 4 3 4 1 0 0 0 3 3 4 3 4 1 4 0 0 2 0 3 15 15 0 Output 6 Miserable Hokusai! 5 155117520
instruction
0
97,780
1
195,560
"Correct Solution: ``` L = int(input().strip()) for _ in range(0,L): gx,gy = map(int,input().strip().split(" ")) heiankyo = [[0 for j in range(0,gx+1)] for i in range(0,gy+1)] heiankyo[0][0] = 1 P = int(input()) matatabi = [] for p in range(P): x1,y1,x2,y2 = map(int,input().strip().split(" ")) l = [[y1,x1],[y2,x2]] l.sort() matatabi.append(l) for i in range(1,gy+1): if not [[i-1,0],[i,0]] in matatabi: heiankyo[i][0] = heiankyo[i-1][0] for j in range(1,gx+1): if not [[0,j-1],[0,j]] in matatabi: heiankyo[0][j] = heiankyo[0][j-1] for i in range(1,gy+1): for j in range(1,gx+1): if not [[i-1,j],[i,j]] in matatabi: heiankyo[i][j] = heiankyo[i][j] + heiankyo[i-1][j] if not [[i,j-1],[i,j]] in matatabi: heiankyo[i][j] = heiankyo[i][j] + heiankyo[i][j-1] if heiankyo[gy][gx] == 0: print("Miserable Hokusai!") else: print(heiankyo[gy][gx]) ```
output
1
97,780
1
195,561
Provide a correct Python 3 solution for this coding contest problem. Heiankyo is known as a town with a grid of roads. Hokusai, a cat who lives in Heiankyo, has to go from his home to a secret place on the outskirts of town every day for patrol. However, I get tired of following the same path every day, and there is a risk of being followed, so Hokusai wants to use a different route every day as much as possible. On the other hand, Hokusai has a troublesome smell, so I don't want to go down the road away from my destination. There are Actinidia polygama on the roads here and there in Heiankyo, and Hokusai cannot go through the road where Actinidia polygama is falling. This is because if you go through such a road, you will be sick. Fortunately, there are no Actinidia polygama at the intersection. Hokusai wants to know the number of possible routes from his home to a secret place. Here, Hokusai's home is at (0, 0) and the secret place is at (gx, gy). The roads are laid out in a grid with x = i (i is an integer) and y = j (j is an integer). Notes on Submission Multiple datasets are given in the above format. The first line of input data gives the number of datasets. Create a program that outputs the output for each data set in order in the above format. Input The first line of input is given the coordinates of the secret location (gx, gy). All of these are integers between 1 and 15 and are given separated by a single space. The second line is given the number of sections where Actinidia polygama is falling p (0 ≤ p ≤ 100), and the following lines p are given one section for each line where Actinidia polygama is falling. The p sections are different from each other. One interval is expressed in the form of x1 y1 x2 y2 and is a line segment of length 1 parallel to the x-axis or y-axis, with (x1, y1) and (x2, y2) as endpoints. x1 and x2 are in the range [0, gx] and y1 and y2 are in the range [0, gy]. Output Output the number of possible routes. If there is no possible route, output "Miserable Hokusai!" On one line. Example Input 4 2 2 0 1 1 2 0 0 0 1 0 0 1 0 4 3 4 1 0 0 0 3 3 4 3 4 1 4 0 0 2 0 3 15 15 0 Output 6 Miserable Hokusai! 5 155117520
instruction
0
97,781
1
195,562
"Correct Solution: ``` x=int(input()) for i in range(x): gx,gy=map(int,input().split()) HK= [[0 for j in range(16)] for i in range(16)] mttb=[[0 for j in range(16)] for i in range(16)] for i in range(int(input())): x1,y1,x2,y2=map(int,input().split()) if x1==x2: mttb[x1][max(y1,y2)]+=1 else: mttb[max(x1,x2)][y1]+=2 for i in range(16): if(mttb[0][i]!=0): break HK[0][i]=1 for i in range(16): if(mttb[i][0]!=0): break HK[i][0]=1 for i in range(1,16): for j in range(1,16): k=mttb[i][j] if k==0: HK[i][j]=HK[i-1][j]+HK[i][j-1] elif k==1: HK[i][j]=HK[i-1][j] elif k==2: HK[i][j]=HK[i][j-1] ans=HK[gx][gy] if ans==0: print("Miserable Hokusai!") else: print(ans) ```
output
1
97,781
1
195,563
Provide a correct Python 3 solution for this coding contest problem. Heiankyo is known as a town with a grid of roads. Hokusai, a cat who lives in Heiankyo, has to go from his home to a secret place on the outskirts of town every day for patrol. However, I get tired of following the same path every day, and there is a risk of being followed, so Hokusai wants to use a different route every day as much as possible. On the other hand, Hokusai has a troublesome smell, so I don't want to go down the road away from my destination. There are Actinidia polygama on the roads here and there in Heiankyo, and Hokusai cannot go through the road where Actinidia polygama is falling. This is because if you go through such a road, you will be sick. Fortunately, there are no Actinidia polygama at the intersection. Hokusai wants to know the number of possible routes from his home to a secret place. Here, Hokusai's home is at (0, 0) and the secret place is at (gx, gy). The roads are laid out in a grid with x = i (i is an integer) and y = j (j is an integer). Notes on Submission Multiple datasets are given in the above format. The first line of input data gives the number of datasets. Create a program that outputs the output for each data set in order in the above format. Input The first line of input is given the coordinates of the secret location (gx, gy). All of these are integers between 1 and 15 and are given separated by a single space. The second line is given the number of sections where Actinidia polygama is falling p (0 ≤ p ≤ 100), and the following lines p are given one section for each line where Actinidia polygama is falling. The p sections are different from each other. One interval is expressed in the form of x1 y1 x2 y2 and is a line segment of length 1 parallel to the x-axis or y-axis, with (x1, y1) and (x2, y2) as endpoints. x1 and x2 are in the range [0, gx] and y1 and y2 are in the range [0, gy]. Output Output the number of possible routes. If there is no possible route, output "Miserable Hokusai!" On one line. Example Input 4 2 2 0 1 1 2 0 0 0 1 0 0 1 0 4 3 4 1 0 0 0 3 3 4 3 4 1 4 0 0 2 0 3 15 15 0 Output 6 Miserable Hokusai! 5 155117520
instruction
0
97,782
1
195,564
"Correct Solution: ``` N=int(input()) for n in range(N): gx,gy=map(int,input().split()) T=[[0 for i in range(gy+1)] for j in range(gx+1)] left=[[0 for i in range(gy+1)] for j in range(gx+1)] upper=[[0 for i in range(gy+1)] for j in range(gx+1)] p=int(input()) for i in range(p): x1,y1,x2,y2=map(int,input().split()) if x1==x2: upper[x1][max(y1,y2)]=1 if y1==y2: left[max(x1,x2)][y1]=1 for i in range(gx+1): for j in range(gy+1): if i==0 and j==0: T[i][j]=1 elif upper[i][j]==1 and left[i][j]==1: T[i][j]=0 elif (upper[i][j]==1 or left[i][j]==1) and (i==0 or j==0): T[i][j]=0 elif i==0: T[i][j]=T[i][j-1] elif j==0: T[i][j]=T[i-1][j] elif upper[i][j]==1: T[i][j]=T[i-1][j] elif left[i][j]==1: T[i][j]=T[i][j-1] else: T[i][j]=T[i-1][j]+T[i][j-1] if T[gx][gy]==0: print("Miserable Hokusai!") else: print(T[gx][gy]) ```
output
1
97,782
1
195,565
Provide a correct Python 3 solution for this coding contest problem. Heiankyo is known as a town with a grid of roads. Hokusai, a cat who lives in Heiankyo, has to go from his home to a secret place on the outskirts of town every day for patrol. However, I get tired of following the same path every day, and there is a risk of being followed, so Hokusai wants to use a different route every day as much as possible. On the other hand, Hokusai has a troublesome smell, so I don't want to go down the road away from my destination. There are Actinidia polygama on the roads here and there in Heiankyo, and Hokusai cannot go through the road where Actinidia polygama is falling. This is because if you go through such a road, you will be sick. Fortunately, there are no Actinidia polygama at the intersection. Hokusai wants to know the number of possible routes from his home to a secret place. Here, Hokusai's home is at (0, 0) and the secret place is at (gx, gy). The roads are laid out in a grid with x = i (i is an integer) and y = j (j is an integer). Notes on Submission Multiple datasets are given in the above format. The first line of input data gives the number of datasets. Create a program that outputs the output for each data set in order in the above format. Input The first line of input is given the coordinates of the secret location (gx, gy). All of these are integers between 1 and 15 and are given separated by a single space. The second line is given the number of sections where Actinidia polygama is falling p (0 ≤ p ≤ 100), and the following lines p are given one section for each line where Actinidia polygama is falling. The p sections are different from each other. One interval is expressed in the form of x1 y1 x2 y2 and is a line segment of length 1 parallel to the x-axis or y-axis, with (x1, y1) and (x2, y2) as endpoints. x1 and x2 are in the range [0, gx] and y1 and y2 are in the range [0, gy]. Output Output the number of possible routes. If there is no possible route, output "Miserable Hokusai!" On one line. Example Input 4 2 2 0 1 1 2 0 0 0 1 0 0 1 0 4 3 4 1 0 0 0 3 3 4 3 4 1 4 0 0 2 0 3 15 15 0 Output 6 Miserable Hokusai! 5 155117520
instruction
0
97,783
1
195,566
"Correct Solution: ``` nn= int(input()) for _ in range(0,nn): gx,gy = map(int,input().split()) p=int(input()) m_vert,m_horiz=[],[] for _ in range(0,p): a,b,c,d = map(int, input().split()) if a==c: if b < d: # b > d nisuru b,d = d,b m_vert.append([a,b]) elif b==d: if a < c: a,c= c,a m_horiz.append([a,b]) ans= [[0]*(gx+100) for _ in range(gy+100)] horiz= [[True]*(gx+100) for _ in range(gy+100)] vert= [[True]*(gx+100) for _ in range(gy+100)] for i in m_horiz: horiz[i[0]][i[1]] = False for i in m_vert: vert[i[0]][i[1]] = False for i in range(0,gx+1): for j in range(0,gy+1): if i==0 and j==0: ans[0][0]=1 if i!=0 and horiz[i][j] : ans[i][j] += ans[i-1][j] if j!=0 and vert[i][j] : ans[i][j] += ans[i][j-1] if ans[gx][gy]==0: print("Miserable Hokusai!") else: print(ans[gx][gy]) #print(ans) ```
output
1
97,783
1
195,567
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Heiankyo is known as a town with a grid of roads. Hokusai, a cat who lives in Heiankyo, has to go from his home to a secret place on the outskirts of town every day for patrol. However, I get tired of following the same path every day, and there is a risk of being followed, so Hokusai wants to use a different route every day as much as possible. On the other hand, Hokusai has a troublesome smell, so I don't want to go down the road away from my destination. There are Actinidia polygama on the roads here and there in Heiankyo, and Hokusai cannot go through the road where Actinidia polygama is falling. This is because if you go through such a road, you will be sick. Fortunately, there are no Actinidia polygama at the intersection. Hokusai wants to know the number of possible routes from his home to a secret place. Here, Hokusai's home is at (0, 0) and the secret place is at (gx, gy). The roads are laid out in a grid with x = i (i is an integer) and y = j (j is an integer). Notes on Submission Multiple datasets are given in the above format. The first line of input data gives the number of datasets. Create a program that outputs the output for each data set in order in the above format. Input The first line of input is given the coordinates of the secret location (gx, gy). All of these are integers between 1 and 15 and are given separated by a single space. The second line is given the number of sections where Actinidia polygama is falling p (0 ≤ p ≤ 100), and the following lines p are given one section for each line where Actinidia polygama is falling. The p sections are different from each other. One interval is expressed in the form of x1 y1 x2 y2 and is a line segment of length 1 parallel to the x-axis or y-axis, with (x1, y1) and (x2, y2) as endpoints. x1 and x2 are in the range [0, gx] and y1 and y2 are in the range [0, gy]. Output Output the number of possible routes. If there is no possible route, output "Miserable Hokusai!" On one line. Example Input 4 2 2 0 1 1 2 0 0 0 1 0 0 1 0 4 3 4 1 0 0 0 3 3 4 3 4 1 4 0 0 2 0 3 15 15 0 Output 6 Miserable Hokusai! 5 155117520 Submitted Solution: ``` n=int(input()) for l in range(n): gx,gy=map(int,input().split()) Count= [[0 for j in range(16)] for i in range(16)] Grid= [[0 for j in range(16)] for i in range(16)] for i in range(int(input())): x1,y1,x2,y2=map(int,input().split()) if x1==x2: Grid[x1][max(y1,y2)]=Grid[x1][max(y1,y2)]+1 else: Grid[max(x1,x2)][y1]=Grid[max(x1,x2)][y1]+2 for i in range (16): if(Grid[0][i]!=0): break Count[0][i]=1 for i in range (16): if(Grid[i][0]!=0): break Count[i][0]=1 for i in range(1,16): for j in range(1,16): k=Grid[i][j] if k==0: Count[i][j]=Count[i-1][j]+Count[i][j-1] elif k==1: Count[i][j]=Count[i-1][j] elif k==2: Count[i][j]=Count[i][j-1] ans=Count[gx][gy] if ans==0: print("Miserable Hokusai!" ) else: print(ans) ```
instruction
0
97,784
1
195,568
Yes
output
1
97,784
1
195,569
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Heiankyo is known as a town with a grid of roads. Hokusai, a cat who lives in Heiankyo, has to go from his home to a secret place on the outskirts of town every day for patrol. However, I get tired of following the same path every day, and there is a risk of being followed, so Hokusai wants to use a different route every day as much as possible. On the other hand, Hokusai has a troublesome smell, so I don't want to go down the road away from my destination. There are Actinidia polygama on the roads here and there in Heiankyo, and Hokusai cannot go through the road where Actinidia polygama is falling. This is because if you go through such a road, you will be sick. Fortunately, there are no Actinidia polygama at the intersection. Hokusai wants to know the number of possible routes from his home to a secret place. Here, Hokusai's home is at (0, 0) and the secret place is at (gx, gy). The roads are laid out in a grid with x = i (i is an integer) and y = j (j is an integer). Notes on Submission Multiple datasets are given in the above format. The first line of input data gives the number of datasets. Create a program that outputs the output for each data set in order in the above format. Input The first line of input is given the coordinates of the secret location (gx, gy). All of these are integers between 1 and 15 and are given separated by a single space. The second line is given the number of sections where Actinidia polygama is falling p (0 ≤ p ≤ 100), and the following lines p are given one section for each line where Actinidia polygama is falling. The p sections are different from each other. One interval is expressed in the form of x1 y1 x2 y2 and is a line segment of length 1 parallel to the x-axis or y-axis, with (x1, y1) and (x2, y2) as endpoints. x1 and x2 are in the range [0, gx] and y1 and y2 are in the range [0, gy]. Output Output the number of possible routes. If there is no possible route, output "Miserable Hokusai!" On one line. Example Input 4 2 2 0 1 1 2 0 0 0 1 0 0 1 0 4 3 4 1 0 0 0 3 3 4 3 4 1 4 0 0 2 0 3 15 15 0 Output 6 Miserable Hokusai! 5 155117520 Submitted Solution: ``` t = int(input()) for _ in range(t): gx, gy = map(int, input().split()) p = int(input()) right_stop = set() down_stop = set() for _ in range(p): x1, y1, x2, y2 = map(int, input().split()) if x1 == x2: down_stop.add((x1 + 1, min(y1, y2) + 1)) if y1 == y2: right_stop.add((min(x1, x2) + 1, y1 + 1)) dp = [[0] * (gx + 2) for _ in range(gy + 2)] dp[1][1] = 1 for y in range(1, gy + 2): for x in range(1, gx + 2): if (x - 1, y) not in right_stop: dp[y][x] += dp[y][x - 1] if (x, y - 1) not in down_stop: dp[y][x] += dp[y - 1][x] if dp[gy + 1][gx + 1] > 0: print(dp[gy + 1][gx + 1]) else: print("Miserable Hokusai!") ```
instruction
0
97,785
1
195,570
Yes
output
1
97,785
1
195,571
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Heiankyo is known as a town with a grid of roads. Hokusai, a cat who lives in Heiankyo, has to go from his home to a secret place on the outskirts of town every day for patrol. However, I get tired of following the same path every day, and there is a risk of being followed, so Hokusai wants to use a different route every day as much as possible. On the other hand, Hokusai has a troublesome smell, so I don't want to go down the road away from my destination. There are Actinidia polygama on the roads here and there in Heiankyo, and Hokusai cannot go through the road where Actinidia polygama is falling. This is because if you go through such a road, you will be sick. Fortunately, there are no Actinidia polygama at the intersection. Hokusai wants to know the number of possible routes from his home to a secret place. Here, Hokusai's home is at (0, 0) and the secret place is at (gx, gy). The roads are laid out in a grid with x = i (i is an integer) and y = j (j is an integer). Notes on Submission Multiple datasets are given in the above format. The first line of input data gives the number of datasets. Create a program that outputs the output for each data set in order in the above format. Input The first line of input is given the coordinates of the secret location (gx, gy). All of these are integers between 1 and 15 and are given separated by a single space. The second line is given the number of sections where Actinidia polygama is falling p (0 ≤ p ≤ 100), and the following lines p are given one section for each line where Actinidia polygama is falling. The p sections are different from each other. One interval is expressed in the form of x1 y1 x2 y2 and is a line segment of length 1 parallel to the x-axis or y-axis, with (x1, y1) and (x2, y2) as endpoints. x1 and x2 are in the range [0, gx] and y1 and y2 are in the range [0, gy]. Output Output the number of possible routes. If there is no possible route, output "Miserable Hokusai!" On one line. Example Input 4 2 2 0 1 1 2 0 0 0 1 0 0 1 0 4 3 4 1 0 0 0 3 3 4 3 4 1 4 0 0 2 0 3 15 15 0 Output 6 Miserable Hokusai! 5 155117520 Submitted Solution: ``` for _ in range(int(input())): G = list(map(int, input().split())) yokomata = [] tatemata = [] for i in range(int(input())): m = (list(map(int, input().split()))) if m[1] == m[3]: yokomata.append([max(m[0], m[2]), m[1]]) else: tatemata.append([m[0], max(m[1], m[3])]) matrix = [[0 for i in range(G[0]+1)] for j in range(G[1]+1)] matrix[0][0] = 1 for i in range(1, G[0]+1): if [i, 0] in yokomata: continue else: matrix[0][i] = matrix[0][i-1] for j in range(1, G[1]+1): if [0, j] in tatemata: continue else: matrix[j][0] = matrix[j-1][0] for j in range(1, G[1] + 1): for i in range(1, G[0] + 1): if [i, j] in yokomata and [i, j] in tatemata: matrix[j][i] = 0 elif [i, j] in yokomata: matrix[j][i] = matrix[j-1][i] elif [i, j] in tatemata: matrix[j][i] = matrix[j][i-1] else: matrix[j][i] = matrix[j-1][i] + matrix[j][i-1] if matrix[G[1]][G[0]] == 0: print("Miserable Hokusai!") else: print(matrix[G[1]][G[0]]) ```
instruction
0
97,786
1
195,572
Yes
output
1
97,786
1
195,573
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Heiankyo is known as a town with a grid of roads. Hokusai, a cat who lives in Heiankyo, has to go from his home to a secret place on the outskirts of town every day for patrol. However, I get tired of following the same path every day, and there is a risk of being followed, so Hokusai wants to use a different route every day as much as possible. On the other hand, Hokusai has a troublesome smell, so I don't want to go down the road away from my destination. There are Actinidia polygama on the roads here and there in Heiankyo, and Hokusai cannot go through the road where Actinidia polygama is falling. This is because if you go through such a road, you will be sick. Fortunately, there are no Actinidia polygama at the intersection. Hokusai wants to know the number of possible routes from his home to a secret place. Here, Hokusai's home is at (0, 0) and the secret place is at (gx, gy). The roads are laid out in a grid with x = i (i is an integer) and y = j (j is an integer). Notes on Submission Multiple datasets are given in the above format. The first line of input data gives the number of datasets. Create a program that outputs the output for each data set in order in the above format. Input The first line of input is given the coordinates of the secret location (gx, gy). All of these are integers between 1 and 15 and are given separated by a single space. The second line is given the number of sections where Actinidia polygama is falling p (0 ≤ p ≤ 100), and the following lines p are given one section for each line where Actinidia polygama is falling. The p sections are different from each other. One interval is expressed in the form of x1 y1 x2 y2 and is a line segment of length 1 parallel to the x-axis or y-axis, with (x1, y1) and (x2, y2) as endpoints. x1 and x2 are in the range [0, gx] and y1 and y2 are in the range [0, gy]. Output Output the number of possible routes. If there is no possible route, output "Miserable Hokusai!" On one line. Example Input 4 2 2 0 1 1 2 0 0 0 1 0 0 1 0 4 3 4 1 0 0 0 3 3 4 3 4 1 4 0 0 2 0 3 15 15 0 Output 6 Miserable Hokusai! 5 155117520 Submitted Solution: ``` def Heian(): g = input().split() x = int(g[0]) y = int(g[1]) #= int(input().strip()) M=int(input()) a = [] for i in range(M): a.append(list(map(int, input().split()))) #Process Vert and Horiz Vert=[[True for i2 in range(x+1)]for i1 in range(y+1)] Horiz=[[True for i2 in range(x+1)]for i1 in range(y+1)] for i in range(x+1): Vert[0][i]=False for i in range(y+1): Horiz[i][0]=False for i in range(M): x1=a[i][0] x2=a[i][2] y1=a[i][1] y2=a[i][3] if x1==x2: Vert[max(y1,y2)][x1]=False if y1==y2: Horiz[y1][max(x1,x2)]=False Txy=[[0 for i2 in range(x+1)]for i1 in range(y+1)] for i in range(x+1): for j in range(y+1): if Vert[j][i]==False and Horiz[j][i]==False: if i==0 and j==0: Txy[i][j]=1 else: Txy[j][i]=0 elif Vert[j][i]==False: if i==0: Txy[j][i]=0 else: Txy[j][i]=Txy[j][i-1] elif Horiz[j][i]==False: if j==0: Txy[j][i]=0 else: Txy[j][i]=Txy[j-1][i] else: Txy[j][i]=Txy[j-1][i]+Txy[j][i-1] if Txy[y][x]==0: print("Miserable Hokusai!") else: print(Txy[y][x]) N = int(input()) for i in range(N): Heian() ```
instruction
0
97,787
1
195,574
Yes
output
1
97,787
1
195,575
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Heiankyo is known as a town with a grid of roads. Hokusai, a cat who lives in Heiankyo, has to go from his home to a secret place on the outskirts of town every day for patrol. However, I get tired of following the same path every day, and there is a risk of being followed, so Hokusai wants to use a different route every day as much as possible. On the other hand, Hokusai has a troublesome smell, so I don't want to go down the road away from my destination. There are Actinidia polygama on the roads here and there in Heiankyo, and Hokusai cannot go through the road where Actinidia polygama is falling. This is because if you go through such a road, you will be sick. Fortunately, there are no Actinidia polygama at the intersection. Hokusai wants to know the number of possible routes from his home to a secret place. Here, Hokusai's home is at (0, 0) and the secret place is at (gx, gy). The roads are laid out in a grid with x = i (i is an integer) and y = j (j is an integer). Notes on Submission Multiple datasets are given in the above format. The first line of input data gives the number of datasets. Create a program that outputs the output for each data set in order in the above format. Input The first line of input is given the coordinates of the secret location (gx, gy). All of these are integers between 1 and 15 and are given separated by a single space. The second line is given the number of sections where Actinidia polygama is falling p (0 ≤ p ≤ 100), and the following lines p are given one section for each line where Actinidia polygama is falling. The p sections are different from each other. One interval is expressed in the form of x1 y1 x2 y2 and is a line segment of length 1 parallel to the x-axis or y-axis, with (x1, y1) and (x2, y2) as endpoints. x1 and x2 are in the range [0, gx] and y1 and y2 are in the range [0, gy]. Output Output the number of possible routes. If there is no possible route, output "Miserable Hokusai!" On one line. Example Input 4 2 2 0 1 1 2 0 0 0 1 0 0 1 0 4 3 4 1 0 0 0 3 3 4 3 4 1 4 0 0 2 0 3 15 15 0 Output 6 Miserable Hokusai! 5 155117520 Submitted Solution: ``` trial = int(input()) for t in range(trial): targ = [int(n) for n in input().split(' ')] root = [[0 for n in range(targ[0] + 1)] for m in range(targ[1] + 1)] matanum = int(input()) for m in range(matanum): matax,matay,secx,secy = (int(n) for n in input().split(' ')) if matax == secx: root[max(matay,secy)][matax] = 'y' else: root[matay][max(secx,matax)] = 'x' root[0][0] = 1 for yaxis in range(targ[1] + 1): for xaxis in range(targ[0] + 1): if xaxis == 0: if root[yaxis][xaxis] == 'y': root[yaxis][xaxis] = 0 else: root[yaxis][xaxis] = 1 elif yaxis == 0: if root[yaxis][xaxis] == 'x': root[yaxis][xaxis] = 0 else: root[yaxis][xaxis] = root[yaxis][xaxis-1] else: if root[yaxis][xaxis] == 'y': root[yaxis][xaxis] = root[yaxis][xaxis - 1] elif root[yaxis][xaxis] == 'x': root[yaxis][xaxis] = root[yaxis - 1][xaxis] else: root[yaxis][xaxis] = root[yaxis - 1][xaxis] + root[yaxis][xaxis - 1] if root[targ[1]][targ[0]] == 0: print("Miserable Hokusai!") else: print(root[targ[1]][targ[0]]) ```
instruction
0
97,788
1
195,576
No
output
1
97,788
1
195,577
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Heiankyo is known as a town with a grid of roads. Hokusai, a cat who lives in Heiankyo, has to go from his home to a secret place on the outskirts of town every day for patrol. However, I get tired of following the same path every day, and there is a risk of being followed, so Hokusai wants to use a different route every day as much as possible. On the other hand, Hokusai has a troublesome smell, so I don't want to go down the road away from my destination. There are Actinidia polygama on the roads here and there in Heiankyo, and Hokusai cannot go through the road where Actinidia polygama is falling. This is because if you go through such a road, you will be sick. Fortunately, there are no Actinidia polygama at the intersection. Hokusai wants to know the number of possible routes from his home to a secret place. Here, Hokusai's home is at (0, 0) and the secret place is at (gx, gy). The roads are laid out in a grid with x = i (i is an integer) and y = j (j is an integer). Notes on Submission Multiple datasets are given in the above format. The first line of input data gives the number of datasets. Create a program that outputs the output for each data set in order in the above format. Input The first line of input is given the coordinates of the secret location (gx, gy). All of these are integers between 1 and 15 and are given separated by a single space. The second line is given the number of sections where Actinidia polygama is falling p (0 ≤ p ≤ 100), and the following lines p are given one section for each line where Actinidia polygama is falling. The p sections are different from each other. One interval is expressed in the form of x1 y1 x2 y2 and is a line segment of length 1 parallel to the x-axis or y-axis, with (x1, y1) and (x2, y2) as endpoints. x1 and x2 are in the range [0, gx] and y1 and y2 are in the range [0, gy]. Output Output the number of possible routes. If there is no possible route, output "Miserable Hokusai!" On one line. Example Input 4 2 2 0 1 1 2 0 0 0 1 0 0 1 0 4 3 4 1 0 0 0 3 3 4 3 4 1 4 0 0 2 0 3 15 15 0 Output 6 Miserable Hokusai! 5 155117520 Submitted Solution: ``` for _ in range(int(input())): x, y = map(int, input().split()) m = set() for _ in range(int(input())):m.add(tuple(map(int, input().split()))) q = {(0,0):1} for _ in range(x + y): nq = {} for i in q: if (i[0], i[1], i[0] + 1, i[1]) not in m and (i[0] + 1, i[1], i[0], i[1]) not in m and i[0] + 1 <= x: if (i[0] + 1, i[1]) in nq:nq[(i[0] + 1, i[1])] += q[i] else:nq[(i[0] + 1, i[1])] = q[i] if (i[0], i[1], i[0], i[1] + 1) not in m and (i[0], i[1] + 1, i[0], i[1]) not in m and i[1] + 1 <= y: if (i[0], i[1] + 1) in nq:nq[(i[0], i[1] + 1)] += q[i] else:nq[(i[0], i[1] + 1)] = q[i] ```
instruction
0
97,789
1
195,578
No
output
1
97,789
1
195,579
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Heiankyo is known as a town with a grid of roads. Hokusai, a cat who lives in Heiankyo, has to go from his home to a secret place on the outskirts of town every day for patrol. However, I get tired of following the same path every day, and there is a risk of being followed, so Hokusai wants to use a different route every day as much as possible. On the other hand, Hokusai has a troublesome smell, so I don't want to go down the road away from my destination. There are Actinidia polygama on the roads here and there in Heiankyo, and Hokusai cannot go through the road where Actinidia polygama is falling. This is because if you go through such a road, you will be sick. Fortunately, there are no Actinidia polygama at the intersection. Hokusai wants to know the number of possible routes from his home to a secret place. Here, Hokusai's home is at (0, 0) and the secret place is at (gx, gy). The roads are laid out in a grid with x = i (i is an integer) and y = j (j is an integer). Notes on Submission Multiple datasets are given in the above format. The first line of input data gives the number of datasets. Create a program that outputs the output for each data set in order in the above format. Input The first line of input is given the coordinates of the secret location (gx, gy). All of these are integers between 1 and 15 and are given separated by a single space. The second line is given the number of sections where Actinidia polygama is falling p (0 ≤ p ≤ 100), and the following lines p are given one section for each line where Actinidia polygama is falling. The p sections are different from each other. One interval is expressed in the form of x1 y1 x2 y2 and is a line segment of length 1 parallel to the x-axis or y-axis, with (x1, y1) and (x2, y2) as endpoints. x1 and x2 are in the range [0, gx] and y1 and y2 are in the range [0, gy]. Output Output the number of possible routes. If there is no possible route, output "Miserable Hokusai!" On one line. Example Input 4 2 2 0 1 1 2 0 0 0 1 0 0 1 0 4 3 4 1 0 0 0 3 3 4 3 4 1 4 0 0 2 0 3 15 15 0 Output 6 Miserable Hokusai! 5 155117520 Submitted Solution: ``` trial = int(input()) for t in range(trial): targ = [int(n) for n in input().split(' ')] root = [[0 for n in range(targ[0] + 1)] for m in range(targ[1] + 1)] matanum = int(input()) for m in range(matanum): matax,matay,secx,secy = (int(n) for n in input().split(' ')) if matax == secx: root[max(matay,secy)][matax] = 'y' else: root[matay][max(secx,matax)] = 'x' for yaxis in range(targ[1] + 1): for xaxis in range(targ[0] + 1): if xaxis == 0: if root[yaxis][xaxis] == 'y': root[yaxis][xaxis] = 0 else: if yaxis == 0: root[0][0] = 1 else: root[yaxis][xaxis] = root[yaxis- 1][xaxis] elif yaxis == 0: if root[yaxis][xaxis] == 'x': root[yaxis][xaxis] = 0 else: root[yaxis][xaxis] = root[yaxis][xaxis-1] else: if root[yaxis][xaxis] == 'y': root[yaxis][xaxis] = root[yaxis][xaxis - 1] elif root[yaxis][xaxis] == 'x': root[yaxis][xaxis] = root[yaxis - 1][xaxis] else: root[yaxis][xaxis] = root[yaxis - 1][xaxis] + root[yaxis][xaxis - 1] if root[targ[1]][targ[0]] == 0: print("Miserable Hokusai!") else: print(root[targ[1]][targ[0]]) ```
instruction
0
97,790
1
195,580
No
output
1
97,790
1
195,581
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Heiankyo is known as a town with a grid of roads. Hokusai, a cat who lives in Heiankyo, has to go from his home to a secret place on the outskirts of town every day for patrol. However, I get tired of following the same path every day, and there is a risk of being followed, so Hokusai wants to use a different route every day as much as possible. On the other hand, Hokusai has a troublesome smell, so I don't want to go down the road away from my destination. There are Actinidia polygama on the roads here and there in Heiankyo, and Hokusai cannot go through the road where Actinidia polygama is falling. This is because if you go through such a road, you will be sick. Fortunately, there are no Actinidia polygama at the intersection. Hokusai wants to know the number of possible routes from his home to a secret place. Here, Hokusai's home is at (0, 0) and the secret place is at (gx, gy). The roads are laid out in a grid with x = i (i is an integer) and y = j (j is an integer). Notes on Submission Multiple datasets are given in the above format. The first line of input data gives the number of datasets. Create a program that outputs the output for each data set in order in the above format. Input The first line of input is given the coordinates of the secret location (gx, gy). All of these are integers between 1 and 15 and are given separated by a single space. The second line is given the number of sections where Actinidia polygama is falling p (0 ≤ p ≤ 100), and the following lines p are given one section for each line where Actinidia polygama is falling. The p sections are different from each other. One interval is expressed in the form of x1 y1 x2 y2 and is a line segment of length 1 parallel to the x-axis or y-axis, with (x1, y1) and (x2, y2) as endpoints. x1 and x2 are in the range [0, gx] and y1 and y2 are in the range [0, gy]. Output Output the number of possible routes. If there is no possible route, output "Miserable Hokusai!" On one line. Example Input 4 2 2 0 1 1 2 0 0 0 1 0 0 1 0 4 3 4 1 0 0 0 3 3 4 3 4 1 4 0 0 2 0 3 15 15 0 Output 6 Miserable Hokusai! 5 155117520 Submitted Solution: ``` for _ in range(int(input())): x, y = map(int, input().split()) m = set() for _ in range(int(input())):m.add(tuple(map(int, input().split()))) q = {(0,0):1} for _ in range(x + y): nq = {} for i in q: if (i[0], i[1], i[0] + 1, i[1]) not in m and i[0] + 1 <= x: if (i[0] + 1, i[1]) in nq:nq[(i[0] + 1, i[1])] += q[i] else:nq[(i[0] + 1, i[1])] = q[i] if (i[0], i[1], i[0], i[1] + 1) not in m and i[1] + 1 <= y: if (i[0], i[1] + 1) in nq:nq[(i[0], i[1] + 1)] += q[i] else:nq[(i[0], i[1] + 1)] = q[i] q = nq if nq == {}:print("Miserable Hokusai!") else:print(q[x, y]) ```
instruction
0
97,791
1
195,582
No
output
1
97,791
1
195,583
Provide tags and a correct Python 3 solution for this coding contest problem. There is a road with length l meters. The start of the road has coordinate 0, the end of the road has coordinate l. There are two cars, the first standing at the start of the road and the second standing at the end of the road. They will start driving simultaneously. The first car will drive from the start to the end and the second car will drive from the end to the start. Initially, they will drive with a speed of 1 meter per second. There are n flags at different coordinates a_1, a_2, …, a_n. Each time when any of two cars drives through a flag, the speed of that car increases by 1 meter per second. Find how long will it take for cars to meet (to reach the same coordinate). Input The first line contains one integer t (1 ≤ t ≤ 10^4): the number of test cases. The first line of each test case contains two integers n, l (1 ≤ n ≤ 10^5, 1 ≤ l ≤ 10^9): the number of flags and the length of the road. The second line contains n integers a_1, a_2, …, a_n in the increasing order (1 ≤ a_1 < a_2 < … < a_n < l). It is guaranteed that the sum of n among all test cases does not exceed 10^5. Output For each test case print a single real number: the time required for cars to meet. Your answer will be considered correct, if its absolute or relative error does not exceed 10^{-6}. More formally, if your answer is a and jury's answer is b, your answer will be considered correct if \frac{|a-b|}{max{(1, b)}} ≤ 10^{-6}. Example Input 5 2 10 1 9 1 10 1 5 7 1 2 3 4 6 2 1000000000 413470354 982876160 9 478 1 10 25 33 239 445 453 468 477 Output 3.000000000000000 3.666666666666667 2.047619047619048 329737645.750000000000000 53.700000000000000 Note In the first test case cars will meet in the coordinate 5. The first car will be in the coordinate 1 in 1 second and after that its speed will increase by 1 and will be equal to 2 meters per second. After 2 more seconds it will be in the coordinate 5. So, it will be in the coordinate 5 in 3 seconds. The second car will be in the coordinate 9 in 1 second and after that its speed will increase by 1 and will be equal to 2 meters per second. After 2 more seconds it will be in the coordinate 5. So, it will be in the coordinate 5 in 3 seconds. In the second test case after 1 second the first car will be in the coordinate 1 and will have the speed equal to 2 meters per second, the second car will be in the coordinate 9 and will have the speed equal to 1 meter per second. So, they will meet after (9-1)/(2+1) = 8/3 seconds. So, the answer is equal to 1 + 8/3 = 11/3.
instruction
0
98,067
1
196,134
Tags: binary search, dp, implementation, math, two pointers Correct Solution: ``` import os,io input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline cases = int(input()) for t in range(cases): n,l = list(map(int,input().split())) a = list(map(int, input().split())) s1,s2 = 1,1 i,j = 0,n-1 cp1,cp2 = 0,l time = 0 while True: t1 = (a[i]-cp1)/s1 t2 = (cp2-a[j])/s2 if t1>t2: time += t2 cp1 += s1*t2 cp2 = a[j] s2 += 1 j -= 1 elif t1==t2: time += t1 cp1 = a[i] cp2 = a[j] i += 1 j -= 1 s1 += 1 s2 += 1 else: time += t1 cp2 -= s2*t1 cp1 = a[i] s1 += 1 i += 1 if i>j: time += (cp2-cp1)/(s1+s2) break print(time) ```
output
1
98,067
1
196,135
Provide tags and a correct Python 3 solution for this coding contest problem. There is a road with length l meters. The start of the road has coordinate 0, the end of the road has coordinate l. There are two cars, the first standing at the start of the road and the second standing at the end of the road. They will start driving simultaneously. The first car will drive from the start to the end and the second car will drive from the end to the start. Initially, they will drive with a speed of 1 meter per second. There are n flags at different coordinates a_1, a_2, …, a_n. Each time when any of two cars drives through a flag, the speed of that car increases by 1 meter per second. Find how long will it take for cars to meet (to reach the same coordinate). Input The first line contains one integer t (1 ≤ t ≤ 10^4): the number of test cases. The first line of each test case contains two integers n, l (1 ≤ n ≤ 10^5, 1 ≤ l ≤ 10^9): the number of flags and the length of the road. The second line contains n integers a_1, a_2, …, a_n in the increasing order (1 ≤ a_1 < a_2 < … < a_n < l). It is guaranteed that the sum of n among all test cases does not exceed 10^5. Output For each test case print a single real number: the time required for cars to meet. Your answer will be considered correct, if its absolute or relative error does not exceed 10^{-6}. More formally, if your answer is a and jury's answer is b, your answer will be considered correct if \frac{|a-b|}{max{(1, b)}} ≤ 10^{-6}. Example Input 5 2 10 1 9 1 10 1 5 7 1 2 3 4 6 2 1000000000 413470354 982876160 9 478 1 10 25 33 239 445 453 468 477 Output 3.000000000000000 3.666666666666667 2.047619047619048 329737645.750000000000000 53.700000000000000 Note In the first test case cars will meet in the coordinate 5. The first car will be in the coordinate 1 in 1 second and after that its speed will increase by 1 and will be equal to 2 meters per second. After 2 more seconds it will be in the coordinate 5. So, it will be in the coordinate 5 in 3 seconds. The second car will be in the coordinate 9 in 1 second and after that its speed will increase by 1 and will be equal to 2 meters per second. After 2 more seconds it will be in the coordinate 5. So, it will be in the coordinate 5 in 3 seconds. In the second test case after 1 second the first car will be in the coordinate 1 and will have the speed equal to 2 meters per second, the second car will be in the coordinate 9 and will have the speed equal to 1 meter per second. So, they will meet after (9-1)/(2+1) = 8/3 seconds. So, the answer is equal to 1 + 8/3 = 11/3.
instruction
0
98,068
1
196,136
Tags: binary search, dp, implementation, math, two pointers Correct Solution: ``` from math import * from bisect import * from collections import * from random import * from decimal import * import sys input=sys.stdin.readline def inp(): return int(input()) def st(): return input().rstrip('\n') def lis(): return list(map(int,input().split())) def ma(): return map(int,input().split()) t=inp() while(t): t-=1 n,l=ma() a=lis() i,j=0,n-1 fc=0 lc=l spfc=1 splc=1 tot=0 while(i<=j): t1=(a[i]-fc)/spfc t2=(lc-a[j])/splc #print(t1,t2,fc,lc) if(t1>t2): tot+=t2 lc=a[j] splc+=1 j-=1 fc+=t2*(spfc) elif(t1<t2): fc=a[i] i+=1 lc-=t1*(splc) tot+=t1 spfc+=1 else: tot+=t1 fc=a[i] lc=a[j] i+=1 j-=1 spfc+=1 splc+=1 #print(lc,fc,spfc) tot+=(lc-fc)/(splc+spfc) print(tot) ```
output
1
98,068
1
196,137
Provide tags and a correct Python 3 solution for this coding contest problem. There is a road with length l meters. The start of the road has coordinate 0, the end of the road has coordinate l. There are two cars, the first standing at the start of the road and the second standing at the end of the road. They will start driving simultaneously. The first car will drive from the start to the end and the second car will drive from the end to the start. Initially, they will drive with a speed of 1 meter per second. There are n flags at different coordinates a_1, a_2, …, a_n. Each time when any of two cars drives through a flag, the speed of that car increases by 1 meter per second. Find how long will it take for cars to meet (to reach the same coordinate). Input The first line contains one integer t (1 ≤ t ≤ 10^4): the number of test cases. The first line of each test case contains two integers n, l (1 ≤ n ≤ 10^5, 1 ≤ l ≤ 10^9): the number of flags and the length of the road. The second line contains n integers a_1, a_2, …, a_n in the increasing order (1 ≤ a_1 < a_2 < … < a_n < l). It is guaranteed that the sum of n among all test cases does not exceed 10^5. Output For each test case print a single real number: the time required for cars to meet. Your answer will be considered correct, if its absolute or relative error does not exceed 10^{-6}. More formally, if your answer is a and jury's answer is b, your answer will be considered correct if \frac{|a-b|}{max{(1, b)}} ≤ 10^{-6}. Example Input 5 2 10 1 9 1 10 1 5 7 1 2 3 4 6 2 1000000000 413470354 982876160 9 478 1 10 25 33 239 445 453 468 477 Output 3.000000000000000 3.666666666666667 2.047619047619048 329737645.750000000000000 53.700000000000000 Note In the first test case cars will meet in the coordinate 5. The first car will be in the coordinate 1 in 1 second and after that its speed will increase by 1 and will be equal to 2 meters per second. After 2 more seconds it will be in the coordinate 5. So, it will be in the coordinate 5 in 3 seconds. The second car will be in the coordinate 9 in 1 second and after that its speed will increase by 1 and will be equal to 2 meters per second. After 2 more seconds it will be in the coordinate 5. So, it will be in the coordinate 5 in 3 seconds. In the second test case after 1 second the first car will be in the coordinate 1 and will have the speed equal to 2 meters per second, the second car will be in the coordinate 9 and will have the speed equal to 1 meter per second. So, they will meet after (9-1)/(2+1) = 8/3 seconds. So, the answer is equal to 1 + 8/3 = 11/3.
instruction
0
98,069
1
196,138
Tags: binary search, dp, implementation, math, two pointers Correct Solution: ``` for t in range(int(input())): n,l=list(map(int,input().split())) flag=list(map(int,input().split())) a={} prvt=0 prvs=1 prvd=0 a[0]=[0,1]#time,speed for i in flag: tmpd=i-prvd tmpt=tmpd/prvs prvs+=1 a[i]=[a[prvd][0]+tmpt,prvs] prvd=i if l not in flag: tmpd=l-prvd tmpt=tmpd/prvs a[l]=[a[prvd][0]+tmpt,prvs] #print(a) #print(flag) b={} prvt=0 prvs=1 prvd=l b[l]=[0,1]#time,speed flag.reverse() for i in flag: tmpd=prvd-i tmpt=tmpd/prvs prvs+=1 b[i]=[b[prvd][0]+tmpt,prvs] prvd=i if 0 not in flag: tmpd=prvd-i tmpt=tmpd/prvs b[0]=[b[prvd][0]+tmpt,prvs] #print(b) #print(flag) flag.reverse() if 0 not in flag:flag.insert(0,0) if l not in flag:flag.append(l) for idx,i in enumerate(flag): if a[i][0]>b[i][0]: if idx-1<0: prev=0 else: prev=flag[idx-1] nxt=i speed=a[prev][1]+b[nxt][1] distance=(a[prev][1]*(b[nxt][0]-a[prev][0])) time=(nxt-prev-distance)/speed #print(speed,nxt,prev,time,a[prev][0]+time,b[nxt][0]+time) ans=b[nxt][0]+time break print(ans) ```
output
1
98,069
1
196,139
Provide tags and a correct Python 3 solution for this coding contest problem. There is a road with length l meters. The start of the road has coordinate 0, the end of the road has coordinate l. There are two cars, the first standing at the start of the road and the second standing at the end of the road. They will start driving simultaneously. The first car will drive from the start to the end and the second car will drive from the end to the start. Initially, they will drive with a speed of 1 meter per second. There are n flags at different coordinates a_1, a_2, …, a_n. Each time when any of two cars drives through a flag, the speed of that car increases by 1 meter per second. Find how long will it take for cars to meet (to reach the same coordinate). Input The first line contains one integer t (1 ≤ t ≤ 10^4): the number of test cases. The first line of each test case contains two integers n, l (1 ≤ n ≤ 10^5, 1 ≤ l ≤ 10^9): the number of flags and the length of the road. The second line contains n integers a_1, a_2, …, a_n in the increasing order (1 ≤ a_1 < a_2 < … < a_n < l). It is guaranteed that the sum of n among all test cases does not exceed 10^5. Output For each test case print a single real number: the time required for cars to meet. Your answer will be considered correct, if its absolute or relative error does not exceed 10^{-6}. More formally, if your answer is a and jury's answer is b, your answer will be considered correct if \frac{|a-b|}{max{(1, b)}} ≤ 10^{-6}. Example Input 5 2 10 1 9 1 10 1 5 7 1 2 3 4 6 2 1000000000 413470354 982876160 9 478 1 10 25 33 239 445 453 468 477 Output 3.000000000000000 3.666666666666667 2.047619047619048 329737645.750000000000000 53.700000000000000 Note In the first test case cars will meet in the coordinate 5. The first car will be in the coordinate 1 in 1 second and after that its speed will increase by 1 and will be equal to 2 meters per second. After 2 more seconds it will be in the coordinate 5. So, it will be in the coordinate 5 in 3 seconds. The second car will be in the coordinate 9 in 1 second and after that its speed will increase by 1 and will be equal to 2 meters per second. After 2 more seconds it will be in the coordinate 5. So, it will be in the coordinate 5 in 3 seconds. In the second test case after 1 second the first car will be in the coordinate 1 and will have the speed equal to 2 meters per second, the second car will be in the coordinate 9 and will have the speed equal to 1 meter per second. So, they will meet after (9-1)/(2+1) = 8/3 seconds. So, the answer is equal to 1 + 8/3 = 11/3.
instruction
0
98,070
1
196,140
Tags: binary search, dp, implementation, math, two pointers Correct Solution: ``` import sys import bisect input = sys.stdin.readline def main(): t = int(input()) for _ in range(t): N, L = [int(x) for x in input().split()] A = [0] + [int(x) for x in input().split()] + [L] LA = [0] * (N + 2) sokudo = 0 for i in range(N + 2): if i == 0: LA[i] = 0 else: LA[i] = LA[i - 1] + (A[i] - A[i - 1]) / sokudo sokudo += 1 RA = [0] * (N + 2) sokudo = 0 for i in range(N + 1, -1, -1): if i == N + 1: RA[i] = 0 else: RA[i] = RA[i + 1] + (A[i + 1] - A[i]) / sokudo sokudo += 1 ans = 0 for i in range(N + 2): if RA[i] == LA[i]: ans = RA[i] break if LA[i] > RA[i]: distance = A[i] - A[i - 1] if LA[i - 1] > RA[i]: distance -= (LA[i - 1] - RA[i]) * (N - i + 2) ans = LA[i - 1] + distance / (i + N - i + 2) else: distance -= (RA[i] - LA[i - 1]) * (i) ans = RA[i] + distance / (i + N - i + 2) break print(ans) if __name__ == '__main__': main() ```
output
1
98,070
1
196,141
Provide tags and a correct Python 3 solution for this coding contest problem. There is a road with length l meters. The start of the road has coordinate 0, the end of the road has coordinate l. There are two cars, the first standing at the start of the road and the second standing at the end of the road. They will start driving simultaneously. The first car will drive from the start to the end and the second car will drive from the end to the start. Initially, they will drive with a speed of 1 meter per second. There are n flags at different coordinates a_1, a_2, …, a_n. Each time when any of two cars drives through a flag, the speed of that car increases by 1 meter per second. Find how long will it take for cars to meet (to reach the same coordinate). Input The first line contains one integer t (1 ≤ t ≤ 10^4): the number of test cases. The first line of each test case contains two integers n, l (1 ≤ n ≤ 10^5, 1 ≤ l ≤ 10^9): the number of flags and the length of the road. The second line contains n integers a_1, a_2, …, a_n in the increasing order (1 ≤ a_1 < a_2 < … < a_n < l). It is guaranteed that the sum of n among all test cases does not exceed 10^5. Output For each test case print a single real number: the time required for cars to meet. Your answer will be considered correct, if its absolute or relative error does not exceed 10^{-6}. More formally, if your answer is a and jury's answer is b, your answer will be considered correct if \frac{|a-b|}{max{(1, b)}} ≤ 10^{-6}. Example Input 5 2 10 1 9 1 10 1 5 7 1 2 3 4 6 2 1000000000 413470354 982876160 9 478 1 10 25 33 239 445 453 468 477 Output 3.000000000000000 3.666666666666667 2.047619047619048 329737645.750000000000000 53.700000000000000 Note In the first test case cars will meet in the coordinate 5. The first car will be in the coordinate 1 in 1 second and after that its speed will increase by 1 and will be equal to 2 meters per second. After 2 more seconds it will be in the coordinate 5. So, it will be in the coordinate 5 in 3 seconds. The second car will be in the coordinate 9 in 1 second and after that its speed will increase by 1 and will be equal to 2 meters per second. After 2 more seconds it will be in the coordinate 5. So, it will be in the coordinate 5 in 3 seconds. In the second test case after 1 second the first car will be in the coordinate 1 and will have the speed equal to 2 meters per second, the second car will be in the coordinate 9 and will have the speed equal to 1 meter per second. So, they will meet after (9-1)/(2+1) = 8/3 seconds. So, the answer is equal to 1 + 8/3 = 11/3.
instruction
0
98,071
1
196,142
Tags: binary search, dp, implementation, math, two pointers Correct Solution: ``` for t in range(int(input())): n,l=map(int,input().split()) a=list(map(int,input().split())) d1, d2 = 0.0, float(l) # start and end points s1, s2 = 1.0, 1.0 # initial speeds of both cars => 1m/s p1, p2 = 0, n-1 # positions time = 0.0 # time taken to meet while p1 <= p2: # check for crossing t = min((a[p1]-d1)/s1, (d2 -a[p2])/s2) # time = dist / speed, find nearest flag time += t d1 += t * s1 # positions at the time of nearest flag d2 -= t * s2 # find whether it is first car or second and increase the respective spped and position if d1 >= a[p1]: p1+=1 s1+=1 if d2 <= a[p2]: p2-=1 s2+=1 # due to both are moving in opp directions with speeds => time = distance / speed1 + speed2 time += (d2-d1)/(s1+s2) print(time) ```
output
1
98,071
1
196,143
Provide tags and a correct Python 3 solution for this coding contest problem. There is a road with length l meters. The start of the road has coordinate 0, the end of the road has coordinate l. There are two cars, the first standing at the start of the road and the second standing at the end of the road. They will start driving simultaneously. The first car will drive from the start to the end and the second car will drive from the end to the start. Initially, they will drive with a speed of 1 meter per second. There are n flags at different coordinates a_1, a_2, …, a_n. Each time when any of two cars drives through a flag, the speed of that car increases by 1 meter per second. Find how long will it take for cars to meet (to reach the same coordinate). Input The first line contains one integer t (1 ≤ t ≤ 10^4): the number of test cases. The first line of each test case contains two integers n, l (1 ≤ n ≤ 10^5, 1 ≤ l ≤ 10^9): the number of flags and the length of the road. The second line contains n integers a_1, a_2, …, a_n in the increasing order (1 ≤ a_1 < a_2 < … < a_n < l). It is guaranteed that the sum of n among all test cases does not exceed 10^5. Output For each test case print a single real number: the time required for cars to meet. Your answer will be considered correct, if its absolute or relative error does not exceed 10^{-6}. More formally, if your answer is a and jury's answer is b, your answer will be considered correct if \frac{|a-b|}{max{(1, b)}} ≤ 10^{-6}. Example Input 5 2 10 1 9 1 10 1 5 7 1 2 3 4 6 2 1000000000 413470354 982876160 9 478 1 10 25 33 239 445 453 468 477 Output 3.000000000000000 3.666666666666667 2.047619047619048 329737645.750000000000000 53.700000000000000 Note In the first test case cars will meet in the coordinate 5. The first car will be in the coordinate 1 in 1 second and after that its speed will increase by 1 and will be equal to 2 meters per second. After 2 more seconds it will be in the coordinate 5. So, it will be in the coordinate 5 in 3 seconds. The second car will be in the coordinate 9 in 1 second and after that its speed will increase by 1 and will be equal to 2 meters per second. After 2 more seconds it will be in the coordinate 5. So, it will be in the coordinate 5 in 3 seconds. In the second test case after 1 second the first car will be in the coordinate 1 and will have the speed equal to 2 meters per second, the second car will be in the coordinate 9 and will have the speed equal to 1 meter per second. So, they will meet after (9-1)/(2+1) = 8/3 seconds. So, the answer is equal to 1 + 8/3 = 11/3.
instruction
0
98,072
1
196,144
Tags: binary search, dp, implementation, math, two pointers Correct Solution: ``` from sys import stdin tt = int(stdin.readline()) for loop in range(tt): n,l = map(int,stdin.readline().split()) a = list(map(int,stdin.readline().split())) a = [0] + a + [l] L = [0] now = 1 for i in range(1,n+2): L.append( L[-1] + (a[i]-a[i-1]) / now ) now += 1 R = [0] now = 1 for i in range(n+1,0,-1): R.append( R[-1] + (a[i]-a[i-1]) / now ) now += 1 R.reverse() #print (L) #print (R) rside = None for i in range(n+2): if L[i] > R[i]: rside = i break lside = rside-1 #print (lside,rside) ll = a[lside] rr = a[rside] while abs(rr-ll) > 10**(-7) and abs(rr-ll) > ll*(10**(-7)): mid = (ll+rr)/2 ltime = L[lside] + (mid-a[lside])/(lside+1) rtime = R[rside] + (a[rside]-mid)/(n+2-rside) #print (ll,rr) if ltime < rtime: ll = mid else: rr = mid ltime = L[lside] + (ll-a[lside])/(lside+1) print (ltime) ```
output
1
98,072
1
196,145
Provide tags and a correct Python 3 solution for this coding contest problem. There is a road with length l meters. The start of the road has coordinate 0, the end of the road has coordinate l. There are two cars, the first standing at the start of the road and the second standing at the end of the road. They will start driving simultaneously. The first car will drive from the start to the end and the second car will drive from the end to the start. Initially, they will drive with a speed of 1 meter per second. There are n flags at different coordinates a_1, a_2, …, a_n. Each time when any of two cars drives through a flag, the speed of that car increases by 1 meter per second. Find how long will it take for cars to meet (to reach the same coordinate). Input The first line contains one integer t (1 ≤ t ≤ 10^4): the number of test cases. The first line of each test case contains two integers n, l (1 ≤ n ≤ 10^5, 1 ≤ l ≤ 10^9): the number of flags and the length of the road. The second line contains n integers a_1, a_2, …, a_n in the increasing order (1 ≤ a_1 < a_2 < … < a_n < l). It is guaranteed that the sum of n among all test cases does not exceed 10^5. Output For each test case print a single real number: the time required for cars to meet. Your answer will be considered correct, if its absolute or relative error does not exceed 10^{-6}. More formally, if your answer is a and jury's answer is b, your answer will be considered correct if \frac{|a-b|}{max{(1, b)}} ≤ 10^{-6}. Example Input 5 2 10 1 9 1 10 1 5 7 1 2 3 4 6 2 1000000000 413470354 982876160 9 478 1 10 25 33 239 445 453 468 477 Output 3.000000000000000 3.666666666666667 2.047619047619048 329737645.750000000000000 53.700000000000000 Note In the first test case cars will meet in the coordinate 5. The first car will be in the coordinate 1 in 1 second and after that its speed will increase by 1 and will be equal to 2 meters per second. After 2 more seconds it will be in the coordinate 5. So, it will be in the coordinate 5 in 3 seconds. The second car will be in the coordinate 9 in 1 second and after that its speed will increase by 1 and will be equal to 2 meters per second. After 2 more seconds it will be in the coordinate 5. So, it will be in the coordinate 5 in 3 seconds. In the second test case after 1 second the first car will be in the coordinate 1 and will have the speed equal to 2 meters per second, the second car will be in the coordinate 9 and will have the speed equal to 1 meter per second. So, they will meet after (9-1)/(2+1) = 8/3 seconds. So, the answer is equal to 1 + 8/3 = 11/3.
instruction
0
98,073
1
196,146
Tags: binary search, dp, implementation, math, two pointers Correct Solution: ``` t = int(input()) def f(mid): s=1 dis=0 for j in range(n): if a[j]-dis<mid*s: mid-=(a[j]-dis)/s dis=a[j] s=s+1 else: break return dis+s*mid def f2(mid): s=1 dis=l for j in range(n): if dis-a[-j-1]<mid*s: mid-=(dis-a[-j-1])/s dis=a[-j-1] s=s+1 else: break return dis-s*mid for j in range(t): n,l=(map(int,input().split())) a=list(map(int,input().split())) low=0 h=l/2 ans=0 while True: mid=(low+h)/2 if abs(f(mid)-f2(mid))<10**(-6): ans=mid break elif f(mid)>f2(mid): h=mid-1 else: low=mid+1 print(ans) ```
output
1
98,073
1
196,147
Provide tags and a correct Python 3 solution for this coding contest problem. There is a road with length l meters. The start of the road has coordinate 0, the end of the road has coordinate l. There are two cars, the first standing at the start of the road and the second standing at the end of the road. They will start driving simultaneously. The first car will drive from the start to the end and the second car will drive from the end to the start. Initially, they will drive with a speed of 1 meter per second. There are n flags at different coordinates a_1, a_2, …, a_n. Each time when any of two cars drives through a flag, the speed of that car increases by 1 meter per second. Find how long will it take for cars to meet (to reach the same coordinate). Input The first line contains one integer t (1 ≤ t ≤ 10^4): the number of test cases. The first line of each test case contains two integers n, l (1 ≤ n ≤ 10^5, 1 ≤ l ≤ 10^9): the number of flags and the length of the road. The second line contains n integers a_1, a_2, …, a_n in the increasing order (1 ≤ a_1 < a_2 < … < a_n < l). It is guaranteed that the sum of n among all test cases does not exceed 10^5. Output For each test case print a single real number: the time required for cars to meet. Your answer will be considered correct, if its absolute or relative error does not exceed 10^{-6}. More formally, if your answer is a and jury's answer is b, your answer will be considered correct if \frac{|a-b|}{max{(1, b)}} ≤ 10^{-6}. Example Input 5 2 10 1 9 1 10 1 5 7 1 2 3 4 6 2 1000000000 413470354 982876160 9 478 1 10 25 33 239 445 453 468 477 Output 3.000000000000000 3.666666666666667 2.047619047619048 329737645.750000000000000 53.700000000000000 Note In the first test case cars will meet in the coordinate 5. The first car will be in the coordinate 1 in 1 second and after that its speed will increase by 1 and will be equal to 2 meters per second. After 2 more seconds it will be in the coordinate 5. So, it will be in the coordinate 5 in 3 seconds. The second car will be in the coordinate 9 in 1 second and after that its speed will increase by 1 and will be equal to 2 meters per second. After 2 more seconds it will be in the coordinate 5. So, it will be in the coordinate 5 in 3 seconds. In the second test case after 1 second the first car will be in the coordinate 1 and will have the speed equal to 2 meters per second, the second car will be in the coordinate 9 and will have the speed equal to 1 meter per second. So, they will meet after (9-1)/(2+1) = 8/3 seconds. So, the answer is equal to 1 + 8/3 = 11/3.
instruction
0
98,074
1
196,148
Tags: binary search, dp, implementation, math, two pointers Correct Solution: ``` t=int(input()) for _ in range(t): n,k=map(int,input().split()) a=list(map(int,input().split())) i=0 j=n-1 first=0 second=k ans=0 speeda=1 speedb=1 preva=0 prevb=k ans=0 while(i<=j): #print(i,j) c=min((a[i]-first)/speeda,(second-a[j])/speedb) if((a[i]-first)/speeda<(second-a[j])/speedb): ans+=c speeda+=1 first=a[i] i+=1 second=second-(c*speedb) elif((a[i]-first)/speeda>(second-a[j])/speedb): ans+=c speedb+=1 second=a[j] j-=1 first=first+(c*speeda) else: ans+=c first=a[i] second=a[j] i+=1 j-=1 speeda+=1 speedb+=1 #print(first,second) d=abs(second-first) ans1=d/(speeda+speedb) print(ans1+ans) ```
output
1
98,074
1
196,149
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a road with length l meters. The start of the road has coordinate 0, the end of the road has coordinate l. There are two cars, the first standing at the start of the road and the second standing at the end of the road. They will start driving simultaneously. The first car will drive from the start to the end and the second car will drive from the end to the start. Initially, they will drive with a speed of 1 meter per second. There are n flags at different coordinates a_1, a_2, …, a_n. Each time when any of two cars drives through a flag, the speed of that car increases by 1 meter per second. Find how long will it take for cars to meet (to reach the same coordinate). Input The first line contains one integer t (1 ≤ t ≤ 10^4): the number of test cases. The first line of each test case contains two integers n, l (1 ≤ n ≤ 10^5, 1 ≤ l ≤ 10^9): the number of flags and the length of the road. The second line contains n integers a_1, a_2, …, a_n in the increasing order (1 ≤ a_1 < a_2 < … < a_n < l). It is guaranteed that the sum of n among all test cases does not exceed 10^5. Output For each test case print a single real number: the time required for cars to meet. Your answer will be considered correct, if its absolute or relative error does not exceed 10^{-6}. More formally, if your answer is a and jury's answer is b, your answer will be considered correct if \frac{|a-b|}{max{(1, b)}} ≤ 10^{-6}. Example Input 5 2 10 1 9 1 10 1 5 7 1 2 3 4 6 2 1000000000 413470354 982876160 9 478 1 10 25 33 239 445 453 468 477 Output 3.000000000000000 3.666666666666667 2.047619047619048 329737645.750000000000000 53.700000000000000 Note In the first test case cars will meet in the coordinate 5. The first car will be in the coordinate 1 in 1 second and after that its speed will increase by 1 and will be equal to 2 meters per second. After 2 more seconds it will be in the coordinate 5. So, it will be in the coordinate 5 in 3 seconds. The second car will be in the coordinate 9 in 1 second and after that its speed will increase by 1 and will be equal to 2 meters per second. After 2 more seconds it will be in the coordinate 5. So, it will be in the coordinate 5 in 3 seconds. In the second test case after 1 second the first car will be in the coordinate 1 and will have the speed equal to 2 meters per second, the second car will be in the coordinate 9 and will have the speed equal to 1 meter per second. So, they will meet after (9-1)/(2+1) = 8/3 seconds. So, the answer is equal to 1 + 8/3 = 11/3. Submitted Solution: ``` def bsearch(mn, mx, func): idx = (mx + mn)//2 while mx-mn>1: if func(idx): idx, mx = (idx + mn)//2, idx continue idx, mn = (idx + mx)//2, idx return idx T, = map(int, input().split()) for _ in range(T): N, L = map(int, input().split()) X = [0] + list(map(int, input().split())) + [L] Y = [0]*(N+2) Z = [0]*(N+2) #時間 for i in range(1, N+2): Y[i] = Y[i-1]+(X[i]-X[i-1])/i for i in range(N, -1, -1): Z[i] = Z[i+1]+(X[i+1]-X[i])/(N-i+1) for i in range(N+1): a, b = Y[i], Y[i+1] c, d = Z[i+1], Z[i] if not (d<a or b<c): va, vb = i+1, N-i+1 ta, tb = a, c break # 0<=X<X[i+1]-X[i] ll = X[i+1]-X[i] mx,mn = ll,0 idx = (mx+mn)/2 for _ in range(1000): tx = ta + idx/va ty = tb + (ll-idx)/vb if tx > ty: idx,mx=(idx+mn)/2,idx else: idx,mn=(idx+mx)/2,idx print(ta+idx/va) ```
instruction
0
98,075
1
196,150
Yes
output
1
98,075
1
196,151
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a road with length l meters. The start of the road has coordinate 0, the end of the road has coordinate l. There are two cars, the first standing at the start of the road and the second standing at the end of the road. They will start driving simultaneously. The first car will drive from the start to the end and the second car will drive from the end to the start. Initially, they will drive with a speed of 1 meter per second. There are n flags at different coordinates a_1, a_2, …, a_n. Each time when any of two cars drives through a flag, the speed of that car increases by 1 meter per second. Find how long will it take for cars to meet (to reach the same coordinate). Input The first line contains one integer t (1 ≤ t ≤ 10^4): the number of test cases. The first line of each test case contains two integers n, l (1 ≤ n ≤ 10^5, 1 ≤ l ≤ 10^9): the number of flags and the length of the road. The second line contains n integers a_1, a_2, …, a_n in the increasing order (1 ≤ a_1 < a_2 < … < a_n < l). It is guaranteed that the sum of n among all test cases does not exceed 10^5. Output For each test case print a single real number: the time required for cars to meet. Your answer will be considered correct, if its absolute or relative error does not exceed 10^{-6}. More formally, if your answer is a and jury's answer is b, your answer will be considered correct if \frac{|a-b|}{max{(1, b)}} ≤ 10^{-6}. Example Input 5 2 10 1 9 1 10 1 5 7 1 2 3 4 6 2 1000000000 413470354 982876160 9 478 1 10 25 33 239 445 453 468 477 Output 3.000000000000000 3.666666666666667 2.047619047619048 329737645.750000000000000 53.700000000000000 Note In the first test case cars will meet in the coordinate 5. The first car will be in the coordinate 1 in 1 second and after that its speed will increase by 1 and will be equal to 2 meters per second. After 2 more seconds it will be in the coordinate 5. So, it will be in the coordinate 5 in 3 seconds. The second car will be in the coordinate 9 in 1 second and after that its speed will increase by 1 and will be equal to 2 meters per second. After 2 more seconds it will be in the coordinate 5. So, it will be in the coordinate 5 in 3 seconds. In the second test case after 1 second the first car will be in the coordinate 1 and will have the speed equal to 2 meters per second, the second car will be in the coordinate 9 and will have the speed equal to 1 meter per second. So, they will meet after (9-1)/(2+1) = 8/3 seconds. So, the answer is equal to 1 + 8/3 = 11/3. Submitted Solution: ``` import sys import random from fractions import Fraction from math import * def input(): return sys.stdin.readline().strip() def iinput(): return int(input()) def finput(): return float(input()) def tinput(): return input().split() def linput(): return list(input()) def rinput(): return map(int, tinput()) def fiinput(): return map(float, tinput()) def rlinput(): return list(map(int, input().split())) def trinput(): return tuple(rinput()) def srlinput(): return sorted(list(map(int, input().split()))) def NOYES(fl): if fl: print("NO") else: print("YES") def YESNO(fl): if fl: print("YES") else: print("NO") def main(): n, l = rinput() #n = iinput() #k = iinput() #m = iinput() #n = int(sys.stdin.readline().strip()) #n, k = rinput() #n, m = rinput() #m, k = rinput() #n, k, m = rinput() #n, m, k = rinput() #k, n, m = rinput() #k, m, n = rinput() #m, k, n = rinput() #m, n, k = rinput() q = rlinput() #q = linput() q = [0] + q + [l] w, e = [0] * (n + 2), [0] * (n + 2) for i in range(1, n + 2): e[n + 1 - i] = e[n + 2 - i] + ((q[-i] - q[-1 - i]) / i) w[i] = w[i - 1] + ((q[i] - q[i - 1]) / i) left, right = 0, n + 2 while right > left + 1: mid = (right + left) // 2 if w[mid] >= e[mid]: right = mid else: left = mid print((q[right] - q[right - 1] - (max(0, w[right - 1] - e[right]) * (n - right + 2) + max(0, e[right] - w[right - 1]) * right)) / (n + 2) + max(w[right - 1], e[right])) for i in range(iinput()): main() ```
instruction
0
98,076
1
196,152
Yes
output
1
98,076
1
196,153
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a road with length l meters. The start of the road has coordinate 0, the end of the road has coordinate l. There are two cars, the first standing at the start of the road and the second standing at the end of the road. They will start driving simultaneously. The first car will drive from the start to the end and the second car will drive from the end to the start. Initially, they will drive with a speed of 1 meter per second. There are n flags at different coordinates a_1, a_2, …, a_n. Each time when any of two cars drives through a flag, the speed of that car increases by 1 meter per second. Find how long will it take for cars to meet (to reach the same coordinate). Input The first line contains one integer t (1 ≤ t ≤ 10^4): the number of test cases. The first line of each test case contains two integers n, l (1 ≤ n ≤ 10^5, 1 ≤ l ≤ 10^9): the number of flags and the length of the road. The second line contains n integers a_1, a_2, …, a_n in the increasing order (1 ≤ a_1 < a_2 < … < a_n < l). It is guaranteed that the sum of n among all test cases does not exceed 10^5. Output For each test case print a single real number: the time required for cars to meet. Your answer will be considered correct, if its absolute or relative error does not exceed 10^{-6}. More formally, if your answer is a and jury's answer is b, your answer will be considered correct if \frac{|a-b|}{max{(1, b)}} ≤ 10^{-6}. Example Input 5 2 10 1 9 1 10 1 5 7 1 2 3 4 6 2 1000000000 413470354 982876160 9 478 1 10 25 33 239 445 453 468 477 Output 3.000000000000000 3.666666666666667 2.047619047619048 329737645.750000000000000 53.700000000000000 Note In the first test case cars will meet in the coordinate 5. The first car will be in the coordinate 1 in 1 second and after that its speed will increase by 1 and will be equal to 2 meters per second. After 2 more seconds it will be in the coordinate 5. So, it will be in the coordinate 5 in 3 seconds. The second car will be in the coordinate 9 in 1 second and after that its speed will increase by 1 and will be equal to 2 meters per second. After 2 more seconds it will be in the coordinate 5. So, it will be in the coordinate 5 in 3 seconds. In the second test case after 1 second the first car will be in the coordinate 1 and will have the speed equal to 2 meters per second, the second car will be in the coordinate 9 and will have the speed equal to 1 meter per second. So, they will meet after (9-1)/(2+1) = 8/3 seconds. So, the answer is equal to 1 + 8/3 = 11/3. Submitted Solution: ``` t = int(input()) for _ in range(t): n, m = [int(x) for x in input().split()] a = [int(x) for x in input().split()] # m = 7 # a = [1,2,3,4,6] # n = len(a) # start positions c1, c2 = 0, m # acc index l, r = 0, n-1 # speed s1, s2 = 1, 1 ans = 0 while l <= r: time2acc1 = (a[l] - c1) / s1 time2acc2 = (c2 - a[r]) / s2 ans += min(time2acc1, time2acc2) if time2acc1 == time2acc2: c1 += time2acc1 * s1 c2 -= time2acc2 * s2 s1 += 1 s2 += 1 l += 1 r -= 1 elif time2acc1 < time2acc2: c1 += time2acc1 * s1 c2 -= time2acc1 * s2 s1 += 1 l += 1 else: c1 += time2acc2 * s1 c2 -= time2acc2 * s2 s2 += 1 r -= 1 ans += (c2 - c1) / (s1 + s2) print(ans) ```
instruction
0
98,077
1
196,154
Yes
output
1
98,077
1
196,155
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a road with length l meters. The start of the road has coordinate 0, the end of the road has coordinate l. There are two cars, the first standing at the start of the road and the second standing at the end of the road. They will start driving simultaneously. The first car will drive from the start to the end and the second car will drive from the end to the start. Initially, they will drive with a speed of 1 meter per second. There are n flags at different coordinates a_1, a_2, …, a_n. Each time when any of two cars drives through a flag, the speed of that car increases by 1 meter per second. Find how long will it take for cars to meet (to reach the same coordinate). Input The first line contains one integer t (1 ≤ t ≤ 10^4): the number of test cases. The first line of each test case contains two integers n, l (1 ≤ n ≤ 10^5, 1 ≤ l ≤ 10^9): the number of flags and the length of the road. The second line contains n integers a_1, a_2, …, a_n in the increasing order (1 ≤ a_1 < a_2 < … < a_n < l). It is guaranteed that the sum of n among all test cases does not exceed 10^5. Output For each test case print a single real number: the time required for cars to meet. Your answer will be considered correct, if its absolute or relative error does not exceed 10^{-6}. More formally, if your answer is a and jury's answer is b, your answer will be considered correct if \frac{|a-b|}{max{(1, b)}} ≤ 10^{-6}. Example Input 5 2 10 1 9 1 10 1 5 7 1 2 3 4 6 2 1000000000 413470354 982876160 9 478 1 10 25 33 239 445 453 468 477 Output 3.000000000000000 3.666666666666667 2.047619047619048 329737645.750000000000000 53.700000000000000 Note In the first test case cars will meet in the coordinate 5. The first car will be in the coordinate 1 in 1 second and after that its speed will increase by 1 and will be equal to 2 meters per second. After 2 more seconds it will be in the coordinate 5. So, it will be in the coordinate 5 in 3 seconds. The second car will be in the coordinate 9 in 1 second and after that its speed will increase by 1 and will be equal to 2 meters per second. After 2 more seconds it will be in the coordinate 5. So, it will be in the coordinate 5 in 3 seconds. In the second test case after 1 second the first car will be in the coordinate 1 and will have the speed equal to 2 meters per second, the second car will be in the coordinate 9 and will have the speed equal to 1 meter per second. So, they will meet after (9-1)/(2+1) = 8/3 seconds. So, the answer is equal to 1 + 8/3 = 11/3. Submitted Solution: ``` for _ in range(int(input())): n, d = map(int, input().split(' ')) a = [int(item) for item in input().split(' ')] ans = 0 s1, s2 = 1,1 x, y = 0, d l, r = 0, n-1 while l<=r: t1 = (a[l] - x) / s1 t2 = (y - a[r]) / s2 if t1 ==t2: s1 += 1 s2 += 1 x = a[l] y = a[r] l += 1 r -= 1 ans += t1 elif t1< t2: s1 += 1 x = a[l] y = y - (t1 * s2) l += 1 ans += t1 else: s2 += 1; y = a[r]; x = x + (t2 * s1); r -= 1 ans += t2; ans += (y -x) / (s1 + s2) print(ans) ```
instruction
0
98,078
1
196,156
Yes
output
1
98,078
1
196,157
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a road with length l meters. The start of the road has coordinate 0, the end of the road has coordinate l. There are two cars, the first standing at the start of the road and the second standing at the end of the road. They will start driving simultaneously. The first car will drive from the start to the end and the second car will drive from the end to the start. Initially, they will drive with a speed of 1 meter per second. There are n flags at different coordinates a_1, a_2, …, a_n. Each time when any of two cars drives through a flag, the speed of that car increases by 1 meter per second. Find how long will it take for cars to meet (to reach the same coordinate). Input The first line contains one integer t (1 ≤ t ≤ 10^4): the number of test cases. The first line of each test case contains two integers n, l (1 ≤ n ≤ 10^5, 1 ≤ l ≤ 10^9): the number of flags and the length of the road. The second line contains n integers a_1, a_2, …, a_n in the increasing order (1 ≤ a_1 < a_2 < … < a_n < l). It is guaranteed that the sum of n among all test cases does not exceed 10^5. Output For each test case print a single real number: the time required for cars to meet. Your answer will be considered correct, if its absolute or relative error does not exceed 10^{-6}. More formally, if your answer is a and jury's answer is b, your answer will be considered correct if \frac{|a-b|}{max{(1, b)}} ≤ 10^{-6}. Example Input 5 2 10 1 9 1 10 1 5 7 1 2 3 4 6 2 1000000000 413470354 982876160 9 478 1 10 25 33 239 445 453 468 477 Output 3.000000000000000 3.666666666666667 2.047619047619048 329737645.750000000000000 53.700000000000000 Note In the first test case cars will meet in the coordinate 5. The first car will be in the coordinate 1 in 1 second and after that its speed will increase by 1 and will be equal to 2 meters per second. After 2 more seconds it will be in the coordinate 5. So, it will be in the coordinate 5 in 3 seconds. The second car will be in the coordinate 9 in 1 second and after that its speed will increase by 1 and will be equal to 2 meters per second. After 2 more seconds it will be in the coordinate 5. So, it will be in the coordinate 5 in 3 seconds. In the second test case after 1 second the first car will be in the coordinate 1 and will have the speed equal to 2 meters per second, the second car will be in the coordinate 9 and will have the speed equal to 1 meter per second. So, they will meet after (9-1)/(2+1) = 8/3 seconds. So, the answer is equal to 1 + 8/3 = 11/3. Submitted Solution: ``` import sys input = sys.stdin.readline for _ in range(int(input())): n,l = map(int,input().split()) n += 2 a = list(map(int,input().split())) a = [0]+a a.append(l) v1 = 1 v2 = 1 d1 = a[0] d2 = a[n-1] i = 0 j = n-1 # print(j,"Ko") ans = 0 # print(a) while i+1 < j: nd1 = abs(a[i+1]-d1) nd2 = abs(a[j-1]-d2) t1 = nd1/v1 t2 = nd2/v2 if t1 < t2: d1 = a[i + 1] i += 1 v1 += 1 ad = v2 * t1 d2 += ad ans += t1 elif t2 < t1: d2 = a[j-1] j -= 1 v2 += 1 ad = v1*t2 d1 += ad ans += t2 else: d1 = a[i+1] d2 = a[j-1] i += 1 j -= 1 v1 += 1 v2 += 1 ans += t1 b = abs(d1-d2)/(v1+v2) ans += b print(ans) ```
instruction
0
98,079
1
196,158
No
output
1
98,079
1
196,159
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a road with length l meters. The start of the road has coordinate 0, the end of the road has coordinate l. There are two cars, the first standing at the start of the road and the second standing at the end of the road. They will start driving simultaneously. The first car will drive from the start to the end and the second car will drive from the end to the start. Initially, they will drive with a speed of 1 meter per second. There are n flags at different coordinates a_1, a_2, …, a_n. Each time when any of two cars drives through a flag, the speed of that car increases by 1 meter per second. Find how long will it take for cars to meet (to reach the same coordinate). Input The first line contains one integer t (1 ≤ t ≤ 10^4): the number of test cases. The first line of each test case contains two integers n, l (1 ≤ n ≤ 10^5, 1 ≤ l ≤ 10^9): the number of flags and the length of the road. The second line contains n integers a_1, a_2, …, a_n in the increasing order (1 ≤ a_1 < a_2 < … < a_n < l). It is guaranteed that the sum of n among all test cases does not exceed 10^5. Output For each test case print a single real number: the time required for cars to meet. Your answer will be considered correct, if its absolute or relative error does not exceed 10^{-6}. More formally, if your answer is a and jury's answer is b, your answer will be considered correct if \frac{|a-b|}{max{(1, b)}} ≤ 10^{-6}. Example Input 5 2 10 1 9 1 10 1 5 7 1 2 3 4 6 2 1000000000 413470354 982876160 9 478 1 10 25 33 239 445 453 468 477 Output 3.000000000000000 3.666666666666667 2.047619047619048 329737645.750000000000000 53.700000000000000 Note In the first test case cars will meet in the coordinate 5. The first car will be in the coordinate 1 in 1 second and after that its speed will increase by 1 and will be equal to 2 meters per second. After 2 more seconds it will be in the coordinate 5. So, it will be in the coordinate 5 in 3 seconds. The second car will be in the coordinate 9 in 1 second and after that its speed will increase by 1 and will be equal to 2 meters per second. After 2 more seconds it will be in the coordinate 5. So, it will be in the coordinate 5 in 3 seconds. In the second test case after 1 second the first car will be in the coordinate 1 and will have the speed equal to 2 meters per second, the second car will be in the coordinate 9 and will have the speed equal to 1 meter per second. So, they will meet after (9-1)/(2+1) = 8/3 seconds. So, the answer is equal to 1 + 8/3 = 11/3. Submitted Solution: ``` from collections import defaultdict t=int(input()) while(t): n,l=map(int,input().split()) a=list(map(int,input().split())) s1,s2=1,1 next1,next2=0,n-1 curr1,curr2=0,l ans=0 while(curr1<curr2): if (next1>next2): ans+=(curr2-curr1)/(s1+s2) break t1=(a[next1]-curr1)/s1 t2=(curr2-a[next2])/s2 # print(t1,t2) if t1<t2: curr1+=s1*t1 curr2-=s2*t1 s1+=1 ans+=t1 next1+=1 elif t2<t1: curr1+=s1*t2 curr2-=s2*t2 s2+=1 ans+=t2 next2-=1 print(curr1,curr2) else: curr1+=s1*t2 curr2-=s2*t2 s2+=1 s1+=1 ans+=t1 next1+=1 next2-=1 print(ans) t-=1 ```
instruction
0
98,080
1
196,160
No
output
1
98,080
1
196,161
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a road with length l meters. The start of the road has coordinate 0, the end of the road has coordinate l. There are two cars, the first standing at the start of the road and the second standing at the end of the road. They will start driving simultaneously. The first car will drive from the start to the end and the second car will drive from the end to the start. Initially, they will drive with a speed of 1 meter per second. There are n flags at different coordinates a_1, a_2, …, a_n. Each time when any of two cars drives through a flag, the speed of that car increases by 1 meter per second. Find how long will it take for cars to meet (to reach the same coordinate). Input The first line contains one integer t (1 ≤ t ≤ 10^4): the number of test cases. The first line of each test case contains two integers n, l (1 ≤ n ≤ 10^5, 1 ≤ l ≤ 10^9): the number of flags and the length of the road. The second line contains n integers a_1, a_2, …, a_n in the increasing order (1 ≤ a_1 < a_2 < … < a_n < l). It is guaranteed that the sum of n among all test cases does not exceed 10^5. Output For each test case print a single real number: the time required for cars to meet. Your answer will be considered correct, if its absolute or relative error does not exceed 10^{-6}. More formally, if your answer is a and jury's answer is b, your answer will be considered correct if \frac{|a-b|}{max{(1, b)}} ≤ 10^{-6}. Example Input 5 2 10 1 9 1 10 1 5 7 1 2 3 4 6 2 1000000000 413470354 982876160 9 478 1 10 25 33 239 445 453 468 477 Output 3.000000000000000 3.666666666666667 2.047619047619048 329737645.750000000000000 53.700000000000000 Note In the first test case cars will meet in the coordinate 5. The first car will be in the coordinate 1 in 1 second and after that its speed will increase by 1 and will be equal to 2 meters per second. After 2 more seconds it will be in the coordinate 5. So, it will be in the coordinate 5 in 3 seconds. The second car will be in the coordinate 9 in 1 second and after that its speed will increase by 1 and will be equal to 2 meters per second. After 2 more seconds it will be in the coordinate 5. So, it will be in the coordinate 5 in 3 seconds. In the second test case after 1 second the first car will be in the coordinate 1 and will have the speed equal to 2 meters per second, the second car will be in the coordinate 9 and will have the speed equal to 1 meter per second. So, they will meet after (9-1)/(2+1) = 8/3 seconds. So, the answer is equal to 1 + 8/3 = 11/3. Submitted Solution: ``` import sys import math import bisect from sys import stdin,stdout from math import gcd,floor,sqrt,log from collections import defaultdict as dd from bisect import bisect_left as bl,bisect_right as br sys.setrecursionlimit(100000000) ii =lambda: int(input()) si =lambda: input() jn =lambda x,l: x.join(map(str,l)) sl =lambda: list(map(str,input().strip())) mi =lambda: map(int,input().split()) mif =lambda: map(float,input().split()) lii =lambda: list(map(int,input().split())) ceil =lambda x: int(x) if(x==int(x)) else int(x)+1 ceildiv=lambda x,d: x//d if(x%d==0) else x//d+1 flush =lambda: stdout.flush() stdstr =lambda: stdin.readline() stdint =lambda: int(stdin.readline()) stdpr =lambda x: stdout.write(str(x)) mod=1000000007 #main code for _ in range(ii()): n,l=mi() arr=lii() a,b=1,1 ai,bi=0,l i=0 j=n-1 ta,tb=0,0 flag=1 if i==j: ai=arr[i] ta+=arr[i] a+=1 bi=l-ai ta+=(bi-ai)/(a+b) tb+=(bi-ai)/(a+b) flag=0 while True: if flag==0: break ta+=(arr[i]-ai)/a ai=arr[i] a+=1 tb+=(bi-arr[j])/b bi=arr[j] b+=1 i+=1 if i==j: ta+=(bi-ai)/(a+b) tb+=(bi-ai)/(a+b) break j-=1 if i>j: break sol=(ta+tb)/2 print(sol) ```
instruction
0
98,081
1
196,162
No
output
1
98,081
1
196,163
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a road with length l meters. The start of the road has coordinate 0, the end of the road has coordinate l. There are two cars, the first standing at the start of the road and the second standing at the end of the road. They will start driving simultaneously. The first car will drive from the start to the end and the second car will drive from the end to the start. Initially, they will drive with a speed of 1 meter per second. There are n flags at different coordinates a_1, a_2, …, a_n. Each time when any of two cars drives through a flag, the speed of that car increases by 1 meter per second. Find how long will it take for cars to meet (to reach the same coordinate). Input The first line contains one integer t (1 ≤ t ≤ 10^4): the number of test cases. The first line of each test case contains two integers n, l (1 ≤ n ≤ 10^5, 1 ≤ l ≤ 10^9): the number of flags and the length of the road. The second line contains n integers a_1, a_2, …, a_n in the increasing order (1 ≤ a_1 < a_2 < … < a_n < l). It is guaranteed that the sum of n among all test cases does not exceed 10^5. Output For each test case print a single real number: the time required for cars to meet. Your answer will be considered correct, if its absolute or relative error does not exceed 10^{-6}. More formally, if your answer is a and jury's answer is b, your answer will be considered correct if \frac{|a-b|}{max{(1, b)}} ≤ 10^{-6}. Example Input 5 2 10 1 9 1 10 1 5 7 1 2 3 4 6 2 1000000000 413470354 982876160 9 478 1 10 25 33 239 445 453 468 477 Output 3.000000000000000 3.666666666666667 2.047619047619048 329737645.750000000000000 53.700000000000000 Note In the first test case cars will meet in the coordinate 5. The first car will be in the coordinate 1 in 1 second and after that its speed will increase by 1 and will be equal to 2 meters per second. After 2 more seconds it will be in the coordinate 5. So, it will be in the coordinate 5 in 3 seconds. The second car will be in the coordinate 9 in 1 second and after that its speed will increase by 1 and will be equal to 2 meters per second. After 2 more seconds it will be in the coordinate 5. So, it will be in the coordinate 5 in 3 seconds. In the second test case after 1 second the first car will be in the coordinate 1 and will have the speed equal to 2 meters per second, the second car will be in the coordinate 9 and will have the speed equal to 1 meter per second. So, they will meet after (9-1)/(2+1) = 8/3 seconds. So, the answer is equal to 1 + 8/3 = 11/3. Submitted Solution: ``` """ Author: Q.E.D Time: 2020-09-30 09:44:27 """ T = int(input()) for _ in range(T): n, l = list(map(int, input().split())) a = list(map(int, input().split())) i, j = 0, n - 1 va, vb = 1, 1 pa, pb = 0, l ans = 0 while True: next_pa = a[i] if i < n else l next_pb = a[j] if j >= 0 else 0 ta = (next_pa - pa) / va tb = (pb - next_pb) / vb mt = min(ta, tb) if pa + mt * va + (l - pb) + mt * vb >= l: dt = (pb - pa) / (va + vb) ans += dt break else: ans += mt if abs(ta - tb) <= 1e-5: va +=1 pa = next_pa i += 1 vb += 1 pb = next_pb j -= 1 elif ta < tb: va += 1 pa = next_pa i += 1 pb -= ta * vb elif ta > tb: vb += 1 pb = next_pb j -= 1 pa += tb * va print(ans) ```
instruction
0
98,082
1
196,164
No
output
1
98,082
1
196,165
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A long time ago in some country in Asia were civil wars. Each of n cities wanted to seize power. That's why sometimes one city gathered an army and sent it to campaign against another city. Road making was difficult, so the country had few roads, exactly n - 1. Also you could reach any city from any other city going on those roads. Even during the war the Oriental people remain spiritually rich and appreciate the beauty of nature. And to keep the memory of this great crusade for the centuries to come, they planted one beautiful tree by the road on which the army spent most time. The Oriental people love nature, that's why if there were several such roads, then one tree was planted by each of them. Recently, when the records of the war were found, it became clear that each city attacked each other one exactly once. There were exactly n(n - 1) attacks in total. Everyone has been wondering what road after those wars became the most beautiful, that is, by which road they planted the largest number of beautiful trees. Input The first line contains an integer n (2 ≤ n ≤ 105), which represents the number of cities. Next n - 1 lines contain three integers each: the numbers of cities ai, bi (1 ≤ ai, bi ≤ n), connected by the i-th road and the number of days di the army spends to go on it (1 ≤ di ≤ 109). The lengths of several roads may coincide. Output Print on the first line two integers — the number of beautiful trees on the most beautiful road and the number of the most beautiful roads. Print on the second line the list of the most beautiful roads in the sorted order by the numbers' increasing. The roads are numbered from 1 to n - 1 in the order in which they are given in the input data. Please, do not use %lld specificator to write 64-bit integers in C++. It is preferred to use the cout stream (also you may use the %I64d specificator). Examples Input 2 2 1 5 Output 2 1 1 Input 6 1 2 1 1 3 5 3 4 2 3 5 3 3 6 4 Output 16 1 2 Submitted Solution: ``` from collections import deque from operator import itemgetter class DisjointSet: def __init__(self, n): self.padre = [x for x in range(0, n)] self. cantidad_vertices = [1]*n def Padre(self, x): if x == self.padre[x]: return x self.padre[x] = self.Padre(self.padre[x]) return self.padre[x] def Union(self, x, y): raiz_x = self.Padre(x) raiz_y = self.Padre(y) if self.cantidad_vertices[raiz_x] > self.cantidad_vertices[raiz_y]: self.padre[raiz_y] = raiz_x self.cantidad_vertices[raiz_x] += self.cantidad_vertices[raiz_y] else: self.padre[raiz_x] = raiz_y self.cantidad_vertices[raiz_y] += self.cantidad_vertices[raiz_x] # Dfs para saber el numero de aristas a la derecha de un vertice def DFSsumando(x, ady, num, i, DS): num[x] = i for y in ady[x]: if num[y] == 0: DFSsumando(y, ady, num, i, DS) DS.Union(x, y) def Solucion(): # organizar las aristas por valor d edges.sort(key=itemgetter(2)) maximo = -1 cant_maximos = 0 aristas_maximos = [-1]*m DS = DisjointSet(n) i = 0 while i < m: min = edges[i][2] k = i+1 # si se repite un valor d if k < m and edges[k][2] == min: adyacentes_temp = [[] for _ in range(0, n)] edges_temp = deque() # ir agregando todas las aristas con peso min while True: x, y, _, posI = edges[k-1] r_a = DS.Padre(x) r_b = DS.Padre(y) edges_temp.append((r_a, r_b, posI)) adyacentes_temp[r_a].append(r_b) adyacentes_temp[r_b].append(r_a) if k >= m or edges[k][2] != min: break k += 1 num_cc = [0]*n contador = 1 # por cada arista tempoar el dfs for x, y, i_inicio in edges_temp: if num_cc[x] == 0: DFSsumando(x, adyacentes_temp, num_cc, contador, DS) # una nueva cc contador += 1 # calculando el valor de cada arista for x, y, i_inicio in edges_temp: li = total = 0 if DS.Padre(x) == x: li = DS.cantidad_vertices[y] total = DS.cantidad_vertices[x] else: li = DS.cantidad_vertices[y] total = DS.cantidad_vertices[x] # cantidad de vertices en esa cc ld = total-li res = li*ld*2 if res < maximo: continue if res > maximo: maximo = res cant_maximos = 0 aristas_maximos[cant_maximos] = i_inicio+1 cant_maximos += 1 # saltar las aristas q ya se analizaron i = k # si no ay varias aristas con ese valor d else: x, y, _, i_inicio = edges[i] px = DS.Padre(x) py = DS.Padre(y) res = DS.cantidad_vertices[px]*DS.cantidad_vertices[py]*2 DS.Union(px, py) i += 1 if res < maximo: continue if res > maximo: maximo = res cant_maximos = 0 aristas_maximos[cant_maximos] = i_inicio+1 cant_maximos += 1 # poner las aristas con mas arboles hacia delante print(str(maximo)+' '+str(cant_maximos)) # imprime cada arista hermosa aristas_maximos[0:cant_maximos].sort() print(*aristas_maximos[0:cant_maximos], sep=" ") n = int(input()) m = n-1 edges = [-1]*m adyacentes = [[] for _ in range(0, n)] for i in range(0, m): x, y, d = map(int, input().split()) edges[i] = (x-1, y-1, d, i) adyacentes[x-1].append(y-1) adyacentes[y-1].append(x-1) Solucion() ```
instruction
0
98,346
1
196,692
No
output
1
98,346
1
196,693
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A long time ago in some country in Asia were civil wars. Each of n cities wanted to seize power. That's why sometimes one city gathered an army and sent it to campaign against another city. Road making was difficult, so the country had few roads, exactly n - 1. Also you could reach any city from any other city going on those roads. Even during the war the Oriental people remain spiritually rich and appreciate the beauty of nature. And to keep the memory of this great crusade for the centuries to come, they planted one beautiful tree by the road on which the army spent most time. The Oriental people love nature, that's why if there were several such roads, then one tree was planted by each of them. Recently, when the records of the war were found, it became clear that each city attacked each other one exactly once. There were exactly n(n - 1) attacks in total. Everyone has been wondering what road after those wars became the most beautiful, that is, by which road they planted the largest number of beautiful trees. Input The first line contains an integer n (2 ≤ n ≤ 105), which represents the number of cities. Next n - 1 lines contain three integers each: the numbers of cities ai, bi (1 ≤ ai, bi ≤ n), connected by the i-th road and the number of days di the army spends to go on it (1 ≤ di ≤ 109). The lengths of several roads may coincide. Output Print on the first line two integers — the number of beautiful trees on the most beautiful road and the number of the most beautiful roads. Print on the second line the list of the most beautiful roads in the sorted order by the numbers' increasing. The roads are numbered from 1 to n - 1 in the order in which they are given in the input data. Please, do not use %lld specificator to write 64-bit integers in C++. It is preferred to use the cout stream (also you may use the %I64d specificator). Examples Input 2 2 1 5 Output 2 1 1 Input 6 1 2 1 1 3 5 3 4 2 3 5 3 3 6 4 Output 16 1 2 Submitted Solution: ``` from collections import deque from operator import itemgetter class DisjointSet: def __init__(self, n): self.padre = [x for x in range(0, n)] self. cantidad_vertices = [1]*n def Padre(self, x): if x == self.padre[x]: return x self.padre[x] = self.Padre(self.padre[x]) return self.padre[x] def Union(self, x, y): raiz_x = self.Padre(x) raiz_y = self.Padre(y) self.padre[raiz_y] = raiz_x self.cantidad_vertices[raiz_x] += self.cantidad_vertices[raiz_y] # if self.cantidad_vertices[raiz_x] > self.cantidad_vertices[raiz_y]: # self.padre[raiz_y] = raiz_x # # self.cantidad_vertices[raiz_x] += self.cantidad_vertices[raiz_y] # else: # self.padre[raiz_x] = raiz_y # self.cantidad_vertices[raiz_y] += self.cantidad_vertices[raiz_x] # # Dfs para saber el numero de aristas a la derecha de un vertice def DFSsumando(x, ady, num, i, DS): num[x] = i for y in ady[x]: if num[y] == 0: DFSsumando(y, ady, num, i, DS) DS.Union(x, y) def Solucion(): # organizar las aristas por valor d edges.sort(key=itemgetter(2)) maximo = -1 cant_maximos = 0 aristas_maximos = [-1]*m DS = DisjointSet(n) i = 0 while i < m: min = edges[i][2] k = i+1 # si se repite un valor d if k < m and edges[k][2] == min: adyacentes_temp = [[] for _ in range(0, n)] edges_temp = deque() # ir agregando todas las aristas con peso min while True: x, y, _, posI = edges[k-1] r_a = DS.Padre(x) r_b = DS.Padre(y) edges_temp.append((r_a, r_b, posI)) adyacentes_temp[r_a].append(r_b) adyacentes_temp[r_b].append(r_a) if k >= m or edges[k][2] != min: break k += 1 num_cc = [0]*n contador = 1 # por cada arista tempoar el dfs for x, y, i_inicio in edges_temp: if num_cc[x] == 0: DFSsumando(x, adyacentes_temp, num_cc, contador, DS) # una nueva cc contador += 1 # calculando el valor de cada arista for x, y, i_inicio in edges_temp: li = total = 0 if DS.Padre(x) == x: li = DS.cantidad_vertices[y] total = DS.cantidad_vertices[x] else: li = DS.cantidad_vertices[x] total = DS.cantidad_vertices[y] # cantidad de vertices en esa cc ld = total-li res = li*ld*2 if res < maximo: continue if res > maximo: maximo = res cant_maximos = 0 aristas_maximos[cant_maximos] = i_inicio+1 cant_maximos += 1 # saltar las aristas q ya se analizaron i = k # si no ay varias aristas con ese valor d else: x, y, _, i_inicio = edges[i] px = DS.Padre(x) py = DS.Padre(y) res = DS.cantidad_vertices[px]*DS.cantidad_vertices[py]*2 DS.Union(px, py) i += 1 if res < maximo: continue if res > maximo: maximo = res cant_maximos = 0 aristas_maximos[cant_maximos] = i_inicio+1 cant_maximos += 1 # poner las aristas con mas arboles hacia delante print(str(maximo)+' '+str(cant_maximos)) # imprime cada arista hermosa aristas_maximos[0:cant_maximos] = sorted(aristas_maximos[0:cant_maximos]) a=aristas_maximos[0:cant_maximos] print(*a, sep=" ") n = int(input()) m = n-1 edges = [-1]*m adyacentes = [[] for _ in range(0, n)] for i in range(0, m): x, y, d = map(int, input().split()) edges[i] = (x-1, y-1, d, i) adyacentes[x-1].append(y-1) adyacentes[y-1].append(x-1) Solucion() ```
instruction
0
98,347
1
196,694
No
output
1
98,347
1
196,695
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A long time ago in some country in Asia were civil wars. Each of n cities wanted to seize power. That's why sometimes one city gathered an army and sent it to campaign against another city. Road making was difficult, so the country had few roads, exactly n - 1. Also you could reach any city from any other city going on those roads. Even during the war the Oriental people remain spiritually rich and appreciate the beauty of nature. And to keep the memory of this great crusade for the centuries to come, they planted one beautiful tree by the road on which the army spent most time. The Oriental people love nature, that's why if there were several such roads, then one tree was planted by each of them. Recently, when the records of the war were found, it became clear that each city attacked each other one exactly once. There were exactly n(n - 1) attacks in total. Everyone has been wondering what road after those wars became the most beautiful, that is, by which road they planted the largest number of beautiful trees. Input The first line contains an integer n (2 ≤ n ≤ 105), which represents the number of cities. Next n - 1 lines contain three integers each: the numbers of cities ai, bi (1 ≤ ai, bi ≤ n), connected by the i-th road and the number of days di the army spends to go on it (1 ≤ di ≤ 109). The lengths of several roads may coincide. Output Print on the first line two integers — the number of beautiful trees on the most beautiful road and the number of the most beautiful roads. Print on the second line the list of the most beautiful roads in the sorted order by the numbers' increasing. The roads are numbered from 1 to n - 1 in the order in which they are given in the input data. Please, do not use %lld specificator to write 64-bit integers in C++. It is preferred to use the cout stream (also you may use the %I64d specificator). Examples Input 2 2 1 5 Output 2 1 1 Input 6 1 2 1 1 3 5 3 4 2 3 5 3 3 6 4 Output 16 1 2 Submitted Solution: ``` from collections import deque from operator import itemgetter class DisjointSet: def __init__(self, n): self.padre = [x for x in range(0, n)] self. cantidad_vertices = [1]*n def Padre(self, x): if x == self.padre[x]: return x self.padre[x] = self.Padre(self.padre[x]) return self.padre[x] def Union(self, x, y): raiz_x = self.Padre(x) raiz_y = self.Padre(y) if self.cantidad_vertices[raiz_x] > self.cantidad_vertices[raiz_y]: self.padre[raiz_y] = raiz_x self.cantidad_vertices[raiz_x] += self.cantidad_vertices[raiz_y] else: self.padre[raiz_x] = raiz_y self.cantidad_vertices[raiz_y] += self.cantidad_vertices[raiz_x] # Dfs para saber el numero de aristas a la derecha de un vertice def DFSsumando(x, ady, num, i, DS): num[x] = i for y in ady[x]: if num[y] == 0: DS.Union(x, y) DFSsumando(y, ady, num, i, DS) def Solucion(): # organizar las aristas por valor d edges.sort(key=itemgetter(2)) maximo = -1 cant_maximos = 0 aristas_maximos = [-1]*m DS = DisjointSet(n) i = 0 while i < m: min = edges[i][2] k = i+1 # si se repite un valor d if k < m and edges[k][2] == min: adyacentes_temp = [[] for _ in range(0, n)] edges_temp = deque() # ir agregando todas las aristas con peso min while True: x, y, _, posI = edges[k-1] r_a = DS.Padre(x) r_b = DS.Padre(y) edges_temp.append((r_a, r_b, posI)) adyacentes_temp[r_a].append(r_b) adyacentes_temp[r_b].append(r_a) if k >= m or edges[k][2] != min: break k += 1 num_cc = [0]*n contador = 1 # por cada arista tempoar el dfs for x, y, i_inicio in edges_temp: if num_cc[x] == 0: DFSsumando(x, adyacentes_temp, num_cc, contador, DS) # una nueva cc contador += 1 # calculando el valor de cada arista for x, y, i_inicio in edges_temp: li = total = 0 if DS.Padre(x) == x: li = DS.cantidad_vertices[y] total = DS.cantidad_vertices[x] else: li = DS.cantidad_vertices[x] total = DS.cantidad_vertices[y] # cantidad de vertices en esa cc ld = total-li res = li*ld*2 if res < maximo: continue if res > maximo: maximo = res cant_maximos = 0 aristas_maximos[cant_maximos] = i_inicio+1 cant_maximos += 1 # saltar las aristas q ya se analizaron i = k # si no ay varias aristas con ese valor d else: x, y, _, i_inicio = edges[i] px = DS.Padre(x) py = DS.Padre(y) res = DS.cantidad_vertices[px]*DS.cantidad_vertices[py]*2 DS.Union(px, py) i += 1 if res < maximo: continue if res > maximo: maximo = res cant_maximos = 0 aristas_maximos[cant_maximos] = i_inicio+1 cant_maximos += 1 # poner las aristas con mas arboles hacia delante print(str(maximo)+' '+str(cant_maximos)) # imprime cada arista hermosa aristas_maximos[0:cant_maximos].sort() print(*aristas_maximos[0:cant_maximos], sep=" ") n = int(input()) m = n-1 edges = [-1]*m adyacentes = [[] for _ in range(0, n)] for i in range(0, m): x, y, d = map(int, input().split()) edges[i] = (x-1, y-1, d, i) adyacentes[x-1].append(y-1) adyacentes[y-1].append(x-1) Solucion() ```
instruction
0
98,348
1
196,696
No
output
1
98,348
1
196,697
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A long time ago in some country in Asia were civil wars. Each of n cities wanted to seize power. That's why sometimes one city gathered an army and sent it to campaign against another city. Road making was difficult, so the country had few roads, exactly n - 1. Also you could reach any city from any other city going on those roads. Even during the war the Oriental people remain spiritually rich and appreciate the beauty of nature. And to keep the memory of this great crusade for the centuries to come, they planted one beautiful tree by the road on which the army spent most time. The Oriental people love nature, that's why if there were several such roads, then one tree was planted by each of them. Recently, when the records of the war were found, it became clear that each city attacked each other one exactly once. There were exactly n(n - 1) attacks in total. Everyone has been wondering what road after those wars became the most beautiful, that is, by which road they planted the largest number of beautiful trees. Input The first line contains an integer n (2 ≤ n ≤ 105), which represents the number of cities. Next n - 1 lines contain three integers each: the numbers of cities ai, bi (1 ≤ ai, bi ≤ n), connected by the i-th road and the number of days di the army spends to go on it (1 ≤ di ≤ 109). The lengths of several roads may coincide. Output Print on the first line two integers — the number of beautiful trees on the most beautiful road and the number of the most beautiful roads. Print on the second line the list of the most beautiful roads in the sorted order by the numbers' increasing. The roads are numbered from 1 to n - 1 in the order in which they are given in the input data. Please, do not use %lld specificator to write 64-bit integers in C++. It is preferred to use the cout stream (also you may use the %I64d specificator). Examples Input 2 2 1 5 Output 2 1 1 Input 6 1 2 1 1 3 5 3 4 2 3 5 3 3 6 4 Output 16 1 2 Submitted Solution: ``` from collections import deque from operator import itemgetter # Para encontrar el representante o padre de una componente conexa def Padre(x, P): while x != P[x]: x = P[x] return x # Dfs para saber el numero de aristas a la derecha de un vertice def DFSsumando(x, suma, color, ady, num, i): color[x] = 1 num[x] = i for adyacente in ady[x]: v = adyacente[0] if color[v] == 1: continue elif color[v] == 0: suma[x] += DFSsumando(v, suma, color, ady, num, i) color[x] = 2 return suma[x] def Solucion(): # organizar las aristas por valor d edges.sort(key=itemgetter(2)) maximo = 1 lista_maximos = deque() # caso 1 en que todas las aristas tienen el mismo valor d inicio = edges[0] final = edges[n-2] if inicio[2] == final[2]: valorI = [1]*n color= num = [0]*n for x, y, d, pos in edges: if color[x] == 0: DFSsumando(x, valorI, color, adyacentes, num, 1) for i in range(0, m): x, y, d, pos = edges[i] vix = valorI[x] viy = valorI[y] li = vix if vix < viy else viy ld = n-li res = li*ld*2 if res > maximo: maximo = res lista_maximos = deque() lista_maximos.append(edges[i][3]) elif res == maximo: lista_maximos.append(edges[i][3]) # existen al menos dos aristas con d diferente else: i = 0 padre = [x for x in range(0, n)] cc = [1]*n while i < m: min = edges[i][2] k = i+1 # si se repite un valor d if k < m and edges[k][2] == min: adyacentes_temp = [[] for _ in range(0, n)] edges_temp = [] bol = True # ir agregando todas las aristas con peso min while bol: x, y, d, posI = edges[k-1] r_a = Padre(x, padre) r_b = Padre(y, padre) edges_temp.append([r_a, r_b, i, posI]) adyacentes_temp[r_a].append([r_b, k-1]) adyacentes_temp[r_b].append([r_a, k-1]) k += 1 bol = k < m and edges[k][2] == min cant = deque() num_cc = [0]*n color = [0]*n copia = cc.copy() # por cada arista tempoar el dfs for x, y, i_edg, i_inicio in edges_temp: contador = 1 if color[x] == 0: r = DFSsumando(x, copia, color, adyacentes_temp, num_cc, contador) # una nueva cc cant.append(r) contador += 1 # calculando el valor de cada arista for x, y, i_edg, i_inicio in edges_temp: vx = copia[x] vy = copia[y] li = vx if vx < vy else vy h = num_cc[li] ld = cant[h-1]-li res = li*ld*2 if res > maximo: maximo = res lista_maximos = deque() lista_maximos.append(i_inicio) elif res == maximo: lista_maximos.append(i_inicio) padre[y] = x cc[x] += cc[y] # saltar las aristas q ya se analizaron i = k # si no ay varias aristas con ese valor d else: x, y, i_edg, i_inicio = edges[i] px = padre[x] py = padre[y] res = cc[px]*cc[py]*2 if res > maximo: maximo = res lista_maximos = deque() lista_maximos.append(i_inicio) elif res == maximo: lista_maximos.append(i_inicio) padre[py] = px cc[px] += cc[py] i += 1 # poner las aristas con mas arboles hacia delante cantidad = len(lista_maximos) print(str(maximo)+' '+str(cantidad)) # imprime cada arista hermosa for x in lista_maximos: print(str(x+1)) n = int(input()) m = n-1 edges = [-1]*m adyacentes = [[] for _ in range(0, n)] for i in range(0, m): x, y, d = map(int, input().split()) edges[i] = [x-1, y-1, d, i] adyacentes[x-1].append([y-1, d, i]) adyacentes[y-1].append([x-1, d, i]) Solucion() ```
instruction
0
98,349
1
196,698
No
output
1
98,349
1
196,699
Provide tags and a correct Python 3 solution for this coding contest problem. Our brave travelers reached an island where pirates had buried treasure. However as the ship was about to moor, the captain found out that some rat ate a piece of the treasure map. The treasure map can be represented as a rectangle n × m in size. Each cell stands for an islands' square (the square's side length equals to a mile). Some cells stand for the sea and they are impenetrable. All other cells are penetrable (i.e. available) and some of them contain local sights. For example, the large tree on the hills or the cave in the rocks. Besides, the map also has a set of k instructions. Each instruction is in the following form: "Walk n miles in the y direction" The possible directions are: north, south, east, and west. If you follow these instructions carefully (you should fulfill all of them, one by one) then you should reach exactly the place where treasures are buried. Unfortunately the captain doesn't know the place where to start fulfilling the instructions — as that very piece of the map was lost. But the captain very well remembers that the place contained some local sight. Besides, the captain knows that the whole way goes through the island's penetrable squares. The captain wants to know which sights are worth checking. He asks you to help him with that. Input The first line contains two integers n and m (3 ≤ n, m ≤ 1000). Then follow n lines containing m integers each — the island map's description. "#" stands for the sea. It is guaranteed that all cells along the rectangle's perimeter are the sea. "." stands for a penetrable square without any sights and the sights are marked with uppercase Latin letters from "A" to "Z". Not all alphabet letters can be used. However, it is guaranteed that at least one of them is present on the map. All local sights are marked by different letters. The next line contains number k (1 ≤ k ≤ 105), after which k lines follow. Each line describes an instruction. Each instruction possesses the form "dir len", where dir stands for the direction and len stands for the length of the way to walk. dir can take values "N", "S", "W" and "E" for North, South, West and East correspondingly. At that, north is to the top, South is to the bottom, west is to the left and east is to the right. len is an integer from 1 to 1000. Output Print all local sights that satisfy to the instructions as a string without any separators in the alphabetical order. If no sight fits, print "no solution" without the quotes. Examples Input 6 10 ########## #K#..##### #.#..##.## #..L.#...# ###D###A.# ########## 4 N 2 S 1 E 1 W 2 Output AD Input 3 4 #### #.A# #### 2 W 1 N 2 Output no solution
instruction
0
98,621
1
197,242
Tags: brute force, implementation Correct Solution: ``` #!/usr/bin/env python3 from sys import stdin n, m = map(int, stdin.readline().rstrip().split()) island = [] pos = {} for i in range(n): island.append(stdin.readline().rstrip()) for j, c in enumerate(island[i]): if c >= 'A' and c <= 'Z': pos[c] = [i, j] l_reach = [[-1 for j in range(m)] for i in range(n)] r_reach = [[-1 for j in range(m)] for i in range(n)] u_reach = [[-1 for j in range(m)] for i in range(n)] d_reach = [[-1 for j in range(m)] for i in range(n)] for i in range(1, n-1): for j in range(1, m-1): if island[i][j] != '#': l_reach[i][j] = 1 + l_reach[i][j-1] u_reach[i][j] = 1 + u_reach[i-1][j] for i in range(n-2, 0, -1): for j in range(m-2, 0, -1): if island[i][j] != '#': r_reach[i][j] = 1 + r_reach[i][j+1] d_reach[i][j] = 1 + d_reach[i+1][j] dir = [None] * 100 dir[ord('N')] = [-1, 0, u_reach] dir[ord('W')] = [0, -1, l_reach] dir[ord('S')] = [1, 0, d_reach] dir[ord('E')] = [0, 1, r_reach] for c in range(int(stdin.readline().rstrip())): x, y, d = dir[ord(stdin.read(1))] c = int(stdin.readline()[1:-1]) to_delete = [] for k, v in pos.items(): if c > d[v[0]][v[1]]: to_delete.append(k) else: v[0] += c * x v[1] += c * y for k in to_delete: del pos[k] ans = ''.join(sorted(pos.keys())) print(ans if ans else 'no solution') ```
output
1
98,621
1
197,243