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. The Berland road network consists of n cities and of m bidirectional roads. The cities are numbered from 1 to n, where the main capital city has number n, and the culture capital — number 1. The road network is set up so that it is possible to reach any city from any other one by the roads. Moving on each road in any direction takes the same time. All residents of Berland are very lazy people, and so when they want to get from city v to city u, they always choose one of the shortest paths (no matter which one). The Berland government wants to make this country's road network safer. For that, it is going to put a police station in one city. The police station has a rather strange property: when a citizen of Berland is driving along the road with a police station at one end of it, the citizen drives more carefully, so all such roads are considered safe. The roads, both ends of which differ from the city with the police station, are dangerous. Now the government wonders where to put the police station so that the average number of safe roads for all the shortest paths from the cultural capital to the main capital would take the maximum value. Input The first input line contains two integers n and m (2 ≤ n ≤ 100, <image>) — the number of cities and the number of roads in Berland, correspondingly. Next m lines contain pairs of integers vi, ui (1 ≤ vi, ui ≤ n, vi ≠ ui) — the numbers of cities that are connected by the i-th road. The numbers on a line are separated by a space. It is guaranteed that each pair of cities is connected with no more than one road and that it is possible to get from any city to any other one along Berland roads. Output Print the maximum possible value of the average number of safe roads among all shortest paths from the culture capital to the main one. The answer will be considered valid if its absolute or relative inaccuracy does not exceed 10 - 6. Examples Input 4 4 1 2 2 4 1 3 3 4 Output 1.000000000000 Input 11 14 1 2 1 3 2 4 3 4 4 5 4 6 5 11 6 11 1 8 8 9 9 7 11 7 1 10 10 4 Output 1.714285714286 Note In the first sample you can put a police station in one of the capitals, then each path will have exactly one safe road. If we place the station not in the capital, then the average number of safe roads will also make <image>. In the second sample we can obtain the maximum sought value if we put the station in city 4, then 6 paths will have 2 safe roads each, and one path will have 0 safe roads, so the answer will equal <image>.
instruction
0
1,905
1
3,810
Tags: dp, graphs, shortest paths Correct Solution: ``` from collections import deque n, m = [int(i) for i in input().split()] g = [[] for i in range(n)] for i in range(m): u, v = [int(i) - 1 for i in input().split()] g[u].append(v) g[v].append(u) def bfs(s): d = [-1] * n ct = [0.0] * n q = deque() d[s] = 0 ct[s] = 1.0 q.append(s) while q: v = q.popleft() for u in g[v]: if d[u] == -1: q.append(u) d[u] = d[v] + 1 ct[u] += ct[v] elif d[u] == d[v] + 1: ct[u] += ct[v] return d, ct ds = [] cts = [] for i in range(n): d, ct = bfs(i) ds.append(d) cts.append(ct) x = cts[0][n - 1] r = 1.0 for i in range(1, n - 1): if ds[0][i] + ds[i][n - 1] == ds[0][n - 1]: r = max(r, 2.0 * cts[0][i] * cts[i][n - 1] / x) print('{0:.10f}'.format(r)) ```
output
1
1,905
1
3,811
Provide tags and a correct Python 3 solution for this coding contest problem. The Berland road network consists of n cities and of m bidirectional roads. The cities are numbered from 1 to n, where the main capital city has number n, and the culture capital — number 1. The road network is set up so that it is possible to reach any city from any other one by the roads. Moving on each road in any direction takes the same time. All residents of Berland are very lazy people, and so when they want to get from city v to city u, they always choose one of the shortest paths (no matter which one). The Berland government wants to make this country's road network safer. For that, it is going to put a police station in one city. The police station has a rather strange property: when a citizen of Berland is driving along the road with a police station at one end of it, the citizen drives more carefully, so all such roads are considered safe. The roads, both ends of which differ from the city with the police station, are dangerous. Now the government wonders where to put the police station so that the average number of safe roads for all the shortest paths from the cultural capital to the main capital would take the maximum value. Input The first input line contains two integers n and m (2 ≤ n ≤ 100, <image>) — the number of cities and the number of roads in Berland, correspondingly. Next m lines contain pairs of integers vi, ui (1 ≤ vi, ui ≤ n, vi ≠ ui) — the numbers of cities that are connected by the i-th road. The numbers on a line are separated by a space. It is guaranteed that each pair of cities is connected with no more than one road and that it is possible to get from any city to any other one along Berland roads. Output Print the maximum possible value of the average number of safe roads among all shortest paths from the culture capital to the main one. The answer will be considered valid if its absolute or relative inaccuracy does not exceed 10 - 6. Examples Input 4 4 1 2 2 4 1 3 3 4 Output 1.000000000000 Input 11 14 1 2 1 3 2 4 3 4 4 5 4 6 5 11 6 11 1 8 8 9 9 7 11 7 1 10 10 4 Output 1.714285714286 Note In the first sample you can put a police station in one of the capitals, then each path will have exactly one safe road. If we place the station not in the capital, then the average number of safe roads will also make <image>. In the second sample we can obtain the maximum sought value if we put the station in city 4, then 6 paths will have 2 safe roads each, and one path will have 0 safe roads, so the answer will equal <image>.
instruction
0
1,906
1
3,812
Tags: dp, graphs, shortest paths Correct Solution: ``` from collections import deque n, m = [int(i) for i in input().split()] g = [[] for i in range(n)] for i in range(m): u, v = [int(i) - 1 for i in input().split()] g[u].append(v) g[v].append(u) def bfs(s): d = [-1] * n ct = [0.0] * n q = deque() d[s] = 0 ct[s] = 1.0 q.append(s) while q: v = q.popleft() for u in g[v]: if d[u] == -1: q.append(u) d[u] = d[v] + 1 ct[u] += ct[v] elif d[u] == d[v] + 1: ct[u] += ct[v] return d, ct ds = [] cts = [] for i in range(n): d, ct = bfs(i) ds.append(d) cts.append(ct) x = cts[0][n - 1] r = 1.0 for i in range(1, n - 1): if ds[0][i] + ds[i][n - 1] == ds[0][n - 1]: r = max(r, 2.0 * cts[0][i] * cts[i][n - 1] / x) print('{0:.10f}'.format(r)) # Made By Mostafa_Khaled ```
output
1
1,906
1
3,813
Provide tags and a correct Python 3 solution for this coding contest problem. The Berland road network consists of n cities and of m bidirectional roads. The cities are numbered from 1 to n, where the main capital city has number n, and the culture capital — number 1. The road network is set up so that it is possible to reach any city from any other one by the roads. Moving on each road in any direction takes the same time. All residents of Berland are very lazy people, and so when they want to get from city v to city u, they always choose one of the shortest paths (no matter which one). The Berland government wants to make this country's road network safer. For that, it is going to put a police station in one city. The police station has a rather strange property: when a citizen of Berland is driving along the road with a police station at one end of it, the citizen drives more carefully, so all such roads are considered safe. The roads, both ends of which differ from the city with the police station, are dangerous. Now the government wonders where to put the police station so that the average number of safe roads for all the shortest paths from the cultural capital to the main capital would take the maximum value. Input The first input line contains two integers n and m (2 ≤ n ≤ 100, <image>) — the number of cities and the number of roads in Berland, correspondingly. Next m lines contain pairs of integers vi, ui (1 ≤ vi, ui ≤ n, vi ≠ ui) — the numbers of cities that are connected by the i-th road. The numbers on a line are separated by a space. It is guaranteed that each pair of cities is connected with no more than one road and that it is possible to get from any city to any other one along Berland roads. Output Print the maximum possible value of the average number of safe roads among all shortest paths from the culture capital to the main one. The answer will be considered valid if its absolute or relative inaccuracy does not exceed 10 - 6. Examples Input 4 4 1 2 2 4 1 3 3 4 Output 1.000000000000 Input 11 14 1 2 1 3 2 4 3 4 4 5 4 6 5 11 6 11 1 8 8 9 9 7 11 7 1 10 10 4 Output 1.714285714286 Note In the first sample you can put a police station in one of the capitals, then each path will have exactly one safe road. If we place the station not in the capital, then the average number of safe roads will also make <image>. In the second sample we can obtain the maximum sought value if we put the station in city 4, then 6 paths will have 2 safe roads each, and one path will have 0 safe roads, so the answer will equal <image>.
instruction
0
1,907
1
3,814
Tags: dp, graphs, shortest paths Correct Solution: ``` n, m = map(int, input().split()) adj = [[] for i in range(n)] for _ in range(m): a, b = map(int, input().split()) adj[a-1].append(b-1) adj[b-1].append(a-1) def bfs(s): q, d, res = [s], [-1] * n, [0] * n d[s] = 0 res[s] = 1 i = 0 while i < len(q): v = q[i] for u in adj[v]: if d[u] == -1: d[u] = d[v] + 1 q.append(u) if d[u] == d[v] + 1: res[u] += res[v] i += 1 return q, d, res q0, d0, res0 = bfs(0) q1, d1, res1 = bfs(n-1) min_dist = d0[n-1] num_paths = res0[n-1] res = num_paths for v in range(1, n-1): if d0[v] + d1[v] == min_dist: res = max(res, 2 * res0[v] * res1[v]) print('%.7f' % (res / num_paths)) ```
output
1
1,907
1
3,815
Provide tags and a correct Python 3 solution for this coding contest problem. The Berland road network consists of n cities and of m bidirectional roads. The cities are numbered from 1 to n, where the main capital city has number n, and the culture capital — number 1. The road network is set up so that it is possible to reach any city from any other one by the roads. Moving on each road in any direction takes the same time. All residents of Berland are very lazy people, and so when they want to get from city v to city u, they always choose one of the shortest paths (no matter which one). The Berland government wants to make this country's road network safer. For that, it is going to put a police station in one city. The police station has a rather strange property: when a citizen of Berland is driving along the road with a police station at one end of it, the citizen drives more carefully, so all such roads are considered safe. The roads, both ends of which differ from the city with the police station, are dangerous. Now the government wonders where to put the police station so that the average number of safe roads for all the shortest paths from the cultural capital to the main capital would take the maximum value. Input The first input line contains two integers n and m (2 ≤ n ≤ 100, <image>) — the number of cities and the number of roads in Berland, correspondingly. Next m lines contain pairs of integers vi, ui (1 ≤ vi, ui ≤ n, vi ≠ ui) — the numbers of cities that are connected by the i-th road. The numbers on a line are separated by a space. It is guaranteed that each pair of cities is connected with no more than one road and that it is possible to get from any city to any other one along Berland roads. Output Print the maximum possible value of the average number of safe roads among all shortest paths from the culture capital to the main one. The answer will be considered valid if its absolute or relative inaccuracy does not exceed 10 - 6. Examples Input 4 4 1 2 2 4 1 3 3 4 Output 1.000000000000 Input 11 14 1 2 1 3 2 4 3 4 4 5 4 6 5 11 6 11 1 8 8 9 9 7 11 7 1 10 10 4 Output 1.714285714286 Note In the first sample you can put a police station in one of the capitals, then each path will have exactly one safe road. If we place the station not in the capital, then the average number of safe roads will also make <image>. In the second sample we can obtain the maximum sought value if we put the station in city 4, then 6 paths will have 2 safe roads each, and one path will have 0 safe roads, so the answer will equal <image>.
instruction
0
1,908
1
3,816
Tags: dp, graphs, shortest paths Correct Solution: ``` read_line = lambda: map(int, input().split(' ')) n, m = read_line() g = [[] for i in range(n)] while m: a, b = map(lambda x: x - 1, read_line()) g[a].append(b) g[b].append(a) m -= 1 def bfs(v0): q = [v0] d = [-1] * n d[v0] = 0 ways = [0] * n ways[v0] = 1 i = 0 while i < len(q): v = q[i] for w in g[v]: if d[w] == -1: d[w] = d[v] + 1 q.append(w) if d[w] == d[v] + 1: ways[w] += ways[v] i += 1 return q, d, ways q0, d0, ways0 = bfs(0) q1, d1, ways1 = bfs(n - 1) min_dist = d0[n - 1] num_paths = ways0[n - 1] ans = num_paths for v in range(1, n - 1): if d0[v] + d1[v] == min_dist: ans = max(ans, 2 * ways0[v] * ways1[v]) print('{:.7f}'.format(ans / num_paths)) ```
output
1
1,908
1
3,817
Provide tags and a correct Python 3 solution for this coding contest problem. The Berland road network consists of n cities and of m bidirectional roads. The cities are numbered from 1 to n, where the main capital city has number n, and the culture capital — number 1. The road network is set up so that it is possible to reach any city from any other one by the roads. Moving on each road in any direction takes the same time. All residents of Berland are very lazy people, and so when they want to get from city v to city u, they always choose one of the shortest paths (no matter which one). The Berland government wants to make this country's road network safer. For that, it is going to put a police station in one city. The police station has a rather strange property: when a citizen of Berland is driving along the road with a police station at one end of it, the citizen drives more carefully, so all such roads are considered safe. The roads, both ends of which differ from the city with the police station, are dangerous. Now the government wonders where to put the police station so that the average number of safe roads for all the shortest paths from the cultural capital to the main capital would take the maximum value. Input The first input line contains two integers n and m (2 ≤ n ≤ 100, <image>) — the number of cities and the number of roads in Berland, correspondingly. Next m lines contain pairs of integers vi, ui (1 ≤ vi, ui ≤ n, vi ≠ ui) — the numbers of cities that are connected by the i-th road. The numbers on a line are separated by a space. It is guaranteed that each pair of cities is connected with no more than one road and that it is possible to get from any city to any other one along Berland roads. Output Print the maximum possible value of the average number of safe roads among all shortest paths from the culture capital to the main one. The answer will be considered valid if its absolute or relative inaccuracy does not exceed 10 - 6. Examples Input 4 4 1 2 2 4 1 3 3 4 Output 1.000000000000 Input 11 14 1 2 1 3 2 4 3 4 4 5 4 6 5 11 6 11 1 8 8 9 9 7 11 7 1 10 10 4 Output 1.714285714286 Note In the first sample you can put a police station in one of the capitals, then each path will have exactly one safe road. If we place the station not in the capital, then the average number of safe roads will also make <image>. In the second sample we can obtain the maximum sought value if we put the station in city 4, then 6 paths will have 2 safe roads each, and one path will have 0 safe roads, so the answer will equal <image>.
instruction
0
1,909
1
3,818
Tags: dp, graphs, shortest paths Correct Solution: ``` #build the graph with n cities and m roads #figure out all of the shortest paths from city 1 to city n #best=number of shortest paths (if we have police station at 1 or n) s=list(map(int,input().split())) n=s[0] m=s[1] roads=[] Adj=[] for i in range(0,n+1): roads.append([]) Adj.append([False]*(n+1)) for i in range(0,m): s=list(map(int,input().split())) roads[s[0]].append(s[1]) roads[s[1]].append(s[0]) Adj[s[0]][s[1]]=True Adj[s[1]][s[0]]=True #we gotta count how many shortest paths from 1 to n #first bfs to find the length of the shortest path #in our bfs, all the nodes now have "levels" from 1 Dist=[200]*(n+1) Q=[1] Dist[1]=0 while len(Q)>0: curr=Q.pop(0) for nex in roads[curr]: if Dist[nex]==200: #not Visited Dist[nex]=Dist[curr]+1 Q.append(nex) Levels=[] for l in range(0,100): Levels.append([]) for p in range(1,n+1): if Dist[p]==l: Levels[l].append(p) #given a point p, paths[a][p]=sigma(paths[a][v]) where v is all of the points # on a level lower than p, connected to p fromOne=[0]*(n+1) def fo(b): """how many shortest paths from 1 to b""" if fromOne[b]>0: return fromOne[b] if b==1: return 1 ans=0 for p in Levels[Dist[b]-1]: if Adj[p][b]: ans+=fo(p) fromOne[b]=ans return ans toN=[0]*(n+1) def tN(b): """how many shortest paths from 1 to b""" if toN[b]>0: return toN[b] if b==n: return 1 ans=0 for p in Levels[Dist[b]+1]: if Adj[p][b]: ans+=tN(p) toN[b]=ans return ans #OHHH we count how many shortest paths include policeStation best=fo(n) for policeStation in range(2,n): safe=fo(policeStation)*tN(policeStation)*2 best=max(best,safe) print(round(best/fo(n),10)) ```
output
1
1,909
1
3,819
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Berland road network consists of n cities and of m bidirectional roads. The cities are numbered from 1 to n, where the main capital city has number n, and the culture capital — number 1. The road network is set up so that it is possible to reach any city from any other one by the roads. Moving on each road in any direction takes the same time. All residents of Berland are very lazy people, and so when they want to get from city v to city u, they always choose one of the shortest paths (no matter which one). The Berland government wants to make this country's road network safer. For that, it is going to put a police station in one city. The police station has a rather strange property: when a citizen of Berland is driving along the road with a police station at one end of it, the citizen drives more carefully, so all such roads are considered safe. The roads, both ends of which differ from the city with the police station, are dangerous. Now the government wonders where to put the police station so that the average number of safe roads for all the shortest paths from the cultural capital to the main capital would take the maximum value. Input The first input line contains two integers n and m (2 ≤ n ≤ 100, <image>) — the number of cities and the number of roads in Berland, correspondingly. Next m lines contain pairs of integers vi, ui (1 ≤ vi, ui ≤ n, vi ≠ ui) — the numbers of cities that are connected by the i-th road. The numbers on a line are separated by a space. It is guaranteed that each pair of cities is connected with no more than one road and that it is possible to get from any city to any other one along Berland roads. Output Print the maximum possible value of the average number of safe roads among all shortest paths from the culture capital to the main one. The answer will be considered valid if its absolute or relative inaccuracy does not exceed 10 - 6. Examples Input 4 4 1 2 2 4 1 3 3 4 Output 1.000000000000 Input 11 14 1 2 1 3 2 4 3 4 4 5 4 6 5 11 6 11 1 8 8 9 9 7 11 7 1 10 10 4 Output 1.714285714286 Note In the first sample you can put a police station in one of the capitals, then each path will have exactly one safe road. If we place the station not in the capital, then the average number of safe roads will also make <image>. In the second sample we can obtain the maximum sought value if we put the station in city 4, then 6 paths will have 2 safe roads each, and one path will have 0 safe roads, so the answer will equal <image>. Submitted Solution: ``` from collections import deque def put(): return map(int, input().split()) def bfs(s): q,dis,cnt = deque(), [-1]*n,[0]*n q.append(s) dis[s],cnt[s]=0,1 while q: x = q.popleft() for y in graph[x]: if dis[y]==-1: dis[y]=dis[x]+1 cnt[y]+=cnt[x] q.append(y) elif dis[y]==dis[x]+1: cnt[y]+=cnt[x] return cnt, dis def solve(): for _ in range(m): x,y = put() x,y = x-1,y-1 graph[x].append(y) graph[y].append(x) c,d = bfs(0) ans = 0 for i in range(n): x,y = bfs(i) if y[0]+y[n-1]==d[n-1]: z = x[0]*x[n-1] if i not in [0, n-1]: z*=2 ans = max(ans, z) print(ans,c[n-1]) ans/=c[n-1] print(ans) n,m = put() graph = [[] for i in range(n)] solve() ```
instruction
0
1,910
1
3,820
No
output
1
1,910
1
3,821
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Berland road network consists of n cities and of m bidirectional roads. The cities are numbered from 1 to n, where the main capital city has number n, and the culture capital — number 1. The road network is set up so that it is possible to reach any city from any other one by the roads. Moving on each road in any direction takes the same time. All residents of Berland are very lazy people, and so when they want to get from city v to city u, they always choose one of the shortest paths (no matter which one). The Berland government wants to make this country's road network safer. For that, it is going to put a police station in one city. The police station has a rather strange property: when a citizen of Berland is driving along the road with a police station at one end of it, the citizen drives more carefully, so all such roads are considered safe. The roads, both ends of which differ from the city with the police station, are dangerous. Now the government wonders where to put the police station so that the average number of safe roads for all the shortest paths from the cultural capital to the main capital would take the maximum value. Input The first input line contains two integers n and m (2 ≤ n ≤ 100, <image>) — the number of cities and the number of roads in Berland, correspondingly. Next m lines contain pairs of integers vi, ui (1 ≤ vi, ui ≤ n, vi ≠ ui) — the numbers of cities that are connected by the i-th road. The numbers on a line are separated by a space. It is guaranteed that each pair of cities is connected with no more than one road and that it is possible to get from any city to any other one along Berland roads. Output Print the maximum possible value of the average number of safe roads among all shortest paths from the culture capital to the main one. The answer will be considered valid if its absolute or relative inaccuracy does not exceed 10 - 6. Examples Input 4 4 1 2 2 4 1 3 3 4 Output 1.000000000000 Input 11 14 1 2 1 3 2 4 3 4 4 5 4 6 5 11 6 11 1 8 8 9 9 7 11 7 1 10 10 4 Output 1.714285714286 Note In the first sample you can put a police station in one of the capitals, then each path will have exactly one safe road. If we place the station not in the capital, then the average number of safe roads will also make <image>. In the second sample we can obtain the maximum sought value if we put the station in city 4, then 6 paths will have 2 safe roads each, and one path will have 0 safe roads, so the answer will equal <image>. Submitted Solution: ``` raw = input().split() n = int(raw[0]) m = int(raw[1]) mat = [[False for j in range(n)] for i in range(n)] for i in range(m): raw = input().split() u = int(raw[0]) - 1 v = int(raw[1]) - 1 mat[u][v] = mat[v][u] = True inf = 100000000000 dist = [inf for i in range(n)] dp = [0 for i in range(n)] from collections import * dp[0] = 1 q = deque() q.append(0) dist[0] = 0 while (len(q) > 0): cur = q.popleft() for i in range(n): if mat[cur][i]: if dist[cur] + 1 < dist[i]: dist[i] = dist[cur] + 1 dp[i] = dp[cur] elif dist[cur] + 1 == dist[i]: dp[i] += dp[cur] continue else: continue q.append(i) res = dp.copy() dist = [inf for i in range(n)] dp = [0 for i in range(n)] dp[n - 1] = 1 q = deque() q.append(n - 1) dist[n - 1] = 0 while (len(q) > 0): cur = q.popleft() for i in range(n): if mat[cur][i]: if dist[cur] + 1 < dist[i]: dist[i] = dist[cur] + 1 dp[i] = dp[cur] # print('from ' + str(cur) + ' to ' + str(i)) elif dist[cur] + 1 == dist[i]: # print('equals from ' + str(cur) + ' to ' + str(i)) dp[i] += dp[cur] # print(dp[cur]) # print(dp[i]) continue else: continue q.append(i) # for i in range(n): # print(str(i) + ' dist ' + str(dist[i])) for i in range(n): # print(str(res[i]) + " " + str(dp[i])) res[i] = res[i] * dp[i] maxx = 0 for i in range(1, n - 1): maxx = max(maxx, res[i]) print(max(maxx * 2 / res[0], 1)) ```
instruction
0
1,911
1
3,822
No
output
1
1,911
1
3,823
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Berland road network consists of n cities and of m bidirectional roads. The cities are numbered from 1 to n, where the main capital city has number n, and the culture capital — number 1. The road network is set up so that it is possible to reach any city from any other one by the roads. Moving on each road in any direction takes the same time. All residents of Berland are very lazy people, and so when they want to get from city v to city u, they always choose one of the shortest paths (no matter which one). The Berland government wants to make this country's road network safer. For that, it is going to put a police station in one city. The police station has a rather strange property: when a citizen of Berland is driving along the road with a police station at one end of it, the citizen drives more carefully, so all such roads are considered safe. The roads, both ends of which differ from the city with the police station, are dangerous. Now the government wonders where to put the police station so that the average number of safe roads for all the shortest paths from the cultural capital to the main capital would take the maximum value. Input The first input line contains two integers n and m (2 ≤ n ≤ 100, <image>) — the number of cities and the number of roads in Berland, correspondingly. Next m lines contain pairs of integers vi, ui (1 ≤ vi, ui ≤ n, vi ≠ ui) — the numbers of cities that are connected by the i-th road. The numbers on a line are separated by a space. It is guaranteed that each pair of cities is connected with no more than one road and that it is possible to get from any city to any other one along Berland roads. Output Print the maximum possible value of the average number of safe roads among all shortest paths from the culture capital to the main one. The answer will be considered valid if its absolute or relative inaccuracy does not exceed 10 - 6. Examples Input 4 4 1 2 2 4 1 3 3 4 Output 1.000000000000 Input 11 14 1 2 1 3 2 4 3 4 4 5 4 6 5 11 6 11 1 8 8 9 9 7 11 7 1 10 10 4 Output 1.714285714286 Note In the first sample you can put a police station in one of the capitals, then each path will have exactly one safe road. If we place the station not in the capital, then the average number of safe roads will also make <image>. In the second sample we can obtain the maximum sought value if we put the station in city 4, then 6 paths will have 2 safe roads each, and one path will have 0 safe roads, so the answer will equal <image>. Submitted Solution: ``` from collections import deque n, m = [int(i) for i in input().split()] g = [[] for i in range(n)] for i in range(m): u, v = [int(i) - 1 for i in input().split()] g[u].append(v) g[v].append(u) def bfs(s): d = [-1] * n ct = [0.0] * n q = deque() d[s] = 0 ct[s] = 1 q.append(s) while q: v = q.popleft() for u in g[v]: if d[u] == -1: q.append(u) d[u] = d[v] + 1 ct[u] += ct[v] elif d[u] == d[v] + 1: ct[u] += ct[v] return ct cts = [] for i in range(n): cts.append(bfs(i)) x = cts[0][n - 1] r = 1.0 for i in range(1, n - 1): r = max(r, 2 * cts[0][i] * cts[i][n - 1] / x) print('{0:.10f}'.format(r)) ```
instruction
0
1,912
1
3,824
No
output
1
1,912
1
3,825
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The Berland road network consists of n cities and of m bidirectional roads. The cities are numbered from 1 to n, where the main capital city has number n, and the culture capital — number 1. The road network is set up so that it is possible to reach any city from any other one by the roads. Moving on each road in any direction takes the same time. All residents of Berland are very lazy people, and so when they want to get from city v to city u, they always choose one of the shortest paths (no matter which one). The Berland government wants to make this country's road network safer. For that, it is going to put a police station in one city. The police station has a rather strange property: when a citizen of Berland is driving along the road with a police station at one end of it, the citizen drives more carefully, so all such roads are considered safe. The roads, both ends of which differ from the city with the police station, are dangerous. Now the government wonders where to put the police station so that the average number of safe roads for all the shortest paths from the cultural capital to the main capital would take the maximum value. Input The first input line contains two integers n and m (2 ≤ n ≤ 100, <image>) — the number of cities and the number of roads in Berland, correspondingly. Next m lines contain pairs of integers vi, ui (1 ≤ vi, ui ≤ n, vi ≠ ui) — the numbers of cities that are connected by the i-th road. The numbers on a line are separated by a space. It is guaranteed that each pair of cities is connected with no more than one road and that it is possible to get from any city to any other one along Berland roads. Output Print the maximum possible value of the average number of safe roads among all shortest paths from the culture capital to the main one. The answer will be considered valid if its absolute or relative inaccuracy does not exceed 10 - 6. Examples Input 4 4 1 2 2 4 1 3 3 4 Output 1.000000000000 Input 11 14 1 2 1 3 2 4 3 4 4 5 4 6 5 11 6 11 1 8 8 9 9 7 11 7 1 10 10 4 Output 1.714285714286 Note In the first sample you can put a police station in one of the capitals, then each path will have exactly one safe road. If we place the station not in the capital, then the average number of safe roads will also make <image>. In the second sample we can obtain the maximum sought value if we put the station in city 4, then 6 paths will have 2 safe roads each, and one path will have 0 safe roads, so the answer will equal <image>. Submitted Solution: ``` s=list(map(int,input().split())) n=s[0] m=s[1] roads=[] Adj=[] for i in range(0,n+1): roads.append([]) Adj.append([False]*(n+1)) for i in range(0,m): s=list(map(int,input().split())) roads[s[0]].append(s[1]) roads[s[1]].append(s[0]) Adj[s[0]][s[1]]=True Adj[s[1]][s[0]]=True #we gotta count how many shortest paths from 1 to n #first bfs to find the length of the shortest path #in our bfs, all the nodes now have "levels" from 1 Dist=[200]*(n+1) Q=[1] Dist[1]=0 while len(Q)>0: curr=Q.pop(0) for nex in roads[curr]: if Dist[nex]==200: #not Visited Dist[nex]=Dist[curr]+1 Q.append(nex) Levels=[] for l in range(0,100): Levels.append([]) for p in range(1,n+1): if Dist[p]==l: Levels[l].append(p) #given a point p, paths[a][p]=sigma(paths[a][v]) where v is all of the points # on a level lower than p, connected to p paths=[] for i in range(0,n+1): paths.append([0]*(n+1)) def dp(a,b): """how many shortest paths from a to b""" if paths[a][b]>0: return paths[a][b] if a==b: return 1 if b<a: return dp(b,a) ans=0 for p in Levels[Dist[b]-1]: if Adj[p][b]: ans+=dp(a,p) paths[a][b]=ans return ans for a in range(1,n+1): for b in range(1,n+1): paths[a][b]=dp(a,b) #OHHH we count how many shortest paths include policeStation best=paths[1][n] for policeStation in range(2,n): safe=paths[1][policeStation]*paths[policeStation][n]*2 best=max(best,safe) print(round(best/paths[1][n],10)) ```
instruction
0
1,913
1
3,826
No
output
1
1,913
1
3,827
Provide tags and a correct Python 3 solution for this coding contest problem. The administration of the Tomsk Region firmly believes that it's time to become a megacity (that is, get population of one million). Instead of improving the demographic situation, they decided to achieve its goal by expanding the boundaries of the city. The city of Tomsk can be represented as point on the plane with coordinates (0; 0). The city is surrounded with n other locations, the i-th one has coordinates (xi, yi) with the population of ki people. You can widen the city boundaries to a circle of radius r. In such case all locations inside the circle and on its border are included into the city. Your goal is to write a program that will determine the minimum radius r, to which is necessary to expand the boundaries of Tomsk, so that it becomes a megacity. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 103; 1 ≤ s < 106) — the number of locatons around Tomsk city and the population of the city. Then n lines follow. The i-th line contains three integers — the xi and yi coordinate values of the i-th location and the number ki of people in it (1 ≤ ki < 106). Each coordinate is an integer and doesn't exceed 104 in its absolute value. It is guaranteed that no two locations are at the same point and no location is at point (0; 0). Output In the output, print "-1" (without the quotes), if Tomsk won't be able to become a megacity. Otherwise, in the first line print a single real number — the minimum radius of the circle that the city needs to expand to in order to become a megacity. The answer is considered correct if the absolute or relative error don't exceed 10 - 6. Examples Input 4 999998 1 1 1 2 2 1 3 3 1 2 -2 1 Output 2.8284271 Input 4 999998 1 1 2 2 2 1 3 3 1 2 -2 1 Output 1.4142136 Input 2 1 1 1 999997 2 2 1 Output -1
instruction
0
1,966
1
3,932
Tags: binary search, greedy, implementation, sortings Correct Solution: ``` import math n, s = [int(x) for x in input().split()] cities = [] for i in range(n): x, y, k = [int(x) for x in input().split()] d2 = x ** 2 + y ** 2 cities.append((d2, k)) cities.sort(key=lambda x: x[0]) if s >= int(1E6): print(0) exit() while cities: t = cities.pop(0) s += t[1] if s >= int(1E6): print(math.sqrt(t[0])) exit() print(-1) ```
output
1
1,966
1
3,933
Provide tags and a correct Python 3 solution for this coding contest problem. The administration of the Tomsk Region firmly believes that it's time to become a megacity (that is, get population of one million). Instead of improving the demographic situation, they decided to achieve its goal by expanding the boundaries of the city. The city of Tomsk can be represented as point on the plane with coordinates (0; 0). The city is surrounded with n other locations, the i-th one has coordinates (xi, yi) with the population of ki people. You can widen the city boundaries to a circle of radius r. In such case all locations inside the circle and on its border are included into the city. Your goal is to write a program that will determine the minimum radius r, to which is necessary to expand the boundaries of Tomsk, so that it becomes a megacity. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 103; 1 ≤ s < 106) — the number of locatons around Tomsk city and the population of the city. Then n lines follow. The i-th line contains three integers — the xi and yi coordinate values of the i-th location and the number ki of people in it (1 ≤ ki < 106). Each coordinate is an integer and doesn't exceed 104 in its absolute value. It is guaranteed that no two locations are at the same point and no location is at point (0; 0). Output In the output, print "-1" (without the quotes), if Tomsk won't be able to become a megacity. Otherwise, in the first line print a single real number — the minimum radius of the circle that the city needs to expand to in order to become a megacity. The answer is considered correct if the absolute or relative error don't exceed 10 - 6. Examples Input 4 999998 1 1 1 2 2 1 3 3 1 2 -2 1 Output 2.8284271 Input 4 999998 1 1 2 2 2 1 3 3 1 2 -2 1 Output 1.4142136 Input 2 1 1 1 999997 2 2 1 Output -1
instruction
0
1,967
1
3,934
Tags: binary search, greedy, implementation, sortings Correct Solution: ``` from math import sqrt MEGA_POPULATION = int(1e6) class Location: def __init__(self, x, y, population): self.x = x self.y = y self.population = population self.dist = sqrt(x ** 2 + y ** 2) n, population = map(int, input().split()) locations = [] for _ in range(n): x, y, k = map(int, input().split()) locations.append(Location(x, y, k)) locations.sort(key=lambda l: l.dist) radius = -1 for location in locations: x, y, k, r = location.x, location.y, location.population, location.dist if population + k >= MEGA_POPULATION: radius = r break else: population += k print('{:.7f}'.format(radius)) ```
output
1
1,967
1
3,935
Provide tags and a correct Python 3 solution for this coding contest problem. The administration of the Tomsk Region firmly believes that it's time to become a megacity (that is, get population of one million). Instead of improving the demographic situation, they decided to achieve its goal by expanding the boundaries of the city. The city of Tomsk can be represented as point on the plane with coordinates (0; 0). The city is surrounded with n other locations, the i-th one has coordinates (xi, yi) with the population of ki people. You can widen the city boundaries to a circle of radius r. In such case all locations inside the circle and on its border are included into the city. Your goal is to write a program that will determine the minimum radius r, to which is necessary to expand the boundaries of Tomsk, so that it becomes a megacity. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 103; 1 ≤ s < 106) — the number of locatons around Tomsk city and the population of the city. Then n lines follow. The i-th line contains three integers — the xi and yi coordinate values of the i-th location and the number ki of people in it (1 ≤ ki < 106). Each coordinate is an integer and doesn't exceed 104 in its absolute value. It is guaranteed that no two locations are at the same point and no location is at point (0; 0). Output In the output, print "-1" (without the quotes), if Tomsk won't be able to become a megacity. Otherwise, in the first line print a single real number — the minimum radius of the circle that the city needs to expand to in order to become a megacity. The answer is considered correct if the absolute or relative error don't exceed 10 - 6. Examples Input 4 999998 1 1 1 2 2 1 3 3 1 2 -2 1 Output 2.8284271 Input 4 999998 1 1 2 2 2 1 3 3 1 2 -2 1 Output 1.4142136 Input 2 1 1 1 999997 2 2 1 Output -1
instruction
0
1,968
1
3,936
Tags: binary search, greedy, implementation, sortings Correct Solution: ``` # maa chudaaye duniya n, s = map(int,input().split()) arr = [] for _ in range(n): a, b, c = map(int, input().split()) arr.append([(a, b), c]) arr.sort(key=lambda x : x[0][0]**2 + x[0][1]**2) # print(*arr) pt = [-1, -1] for i in range(n): if s + arr[i][1] >= 10**6: pt = arr[i][0] break s += arr[i][1] if pt == [-1, -1]: print(-1) else: print(pow(pt[0]**2 + pt[1]**2, 0.5)) ```
output
1
1,968
1
3,937
Provide tags and a correct Python 3 solution for this coding contest problem. The administration of the Tomsk Region firmly believes that it's time to become a megacity (that is, get population of one million). Instead of improving the demographic situation, they decided to achieve its goal by expanding the boundaries of the city. The city of Tomsk can be represented as point on the plane with coordinates (0; 0). The city is surrounded with n other locations, the i-th one has coordinates (xi, yi) with the population of ki people. You can widen the city boundaries to a circle of radius r. In such case all locations inside the circle and on its border are included into the city. Your goal is to write a program that will determine the minimum radius r, to which is necessary to expand the boundaries of Tomsk, so that it becomes a megacity. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 103; 1 ≤ s < 106) — the number of locatons around Tomsk city and the population of the city. Then n lines follow. The i-th line contains three integers — the xi and yi coordinate values of the i-th location and the number ki of people in it (1 ≤ ki < 106). Each coordinate is an integer and doesn't exceed 104 in its absolute value. It is guaranteed that no two locations are at the same point and no location is at point (0; 0). Output In the output, print "-1" (without the quotes), if Tomsk won't be able to become a megacity. Otherwise, in the first line print a single real number — the minimum radius of the circle that the city needs to expand to in order to become a megacity. The answer is considered correct if the absolute or relative error don't exceed 10 - 6. Examples Input 4 999998 1 1 1 2 2 1 3 3 1 2 -2 1 Output 2.8284271 Input 4 999998 1 1 2 2 2 1 3 3 1 2 -2 1 Output 1.4142136 Input 2 1 1 1 999997 2 2 1 Output -1
instruction
0
1,969
1
3,938
Tags: binary search, greedy, implementation, sortings Correct Solution: ``` import sys import math import bisect def query_value(m, dist, population, r): n = len(dist) ans = m for i in range(n): if r >= dist[i] - 10 ** -8: ans += population[i] #print('m: %d, dist: %s, population: %s, r; %f, ans: %d' % (m, str(dist), str(population), r, ans)) return ans def solve(m, dist, population): n = len(dist) left = 0 right = 10 ** 18 if query_value(m, dist, population, right) < 10 ** 6: return -1 cnt = 128 while cnt: cnt -= 1 mid = (left + right) / 2 if query_value(m, dist, population, mid) >= 10 ** 6: right = mid else: left = mid return right def main(): n, m = map(int, input().split()) dist = [] population = [] for i in range(n): x, y, a = map(int, input().split()) dist.append(math.sqrt(x ** 2 + y ** 2)) population.append(a) print(solve(m, dist, population)) if __name__ == "__main__": main() ```
output
1
1,969
1
3,939
Provide tags and a correct Python 3 solution for this coding contest problem. The administration of the Tomsk Region firmly believes that it's time to become a megacity (that is, get population of one million). Instead of improving the demographic situation, they decided to achieve its goal by expanding the boundaries of the city. The city of Tomsk can be represented as point on the plane with coordinates (0; 0). The city is surrounded with n other locations, the i-th one has coordinates (xi, yi) with the population of ki people. You can widen the city boundaries to a circle of radius r. In such case all locations inside the circle and on its border are included into the city. Your goal is to write a program that will determine the minimum radius r, to which is necessary to expand the boundaries of Tomsk, so that it becomes a megacity. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 103; 1 ≤ s < 106) — the number of locatons around Tomsk city and the population of the city. Then n lines follow. The i-th line contains three integers — the xi and yi coordinate values of the i-th location and the number ki of people in it (1 ≤ ki < 106). Each coordinate is an integer and doesn't exceed 104 in its absolute value. It is guaranteed that no two locations are at the same point and no location is at point (0; 0). Output In the output, print "-1" (without the quotes), if Tomsk won't be able to become a megacity. Otherwise, in the first line print a single real number — the minimum radius of the circle that the city needs to expand to in order to become a megacity. The answer is considered correct if the absolute or relative error don't exceed 10 - 6. Examples Input 4 999998 1 1 1 2 2 1 3 3 1 2 -2 1 Output 2.8284271 Input 4 999998 1 1 2 2 2 1 3 3 1 2 -2 1 Output 1.4142136 Input 2 1 1 1 999997 2 2 1 Output -1
instruction
0
1,970
1
3,940
Tags: binary search, greedy, implementation, sortings Correct Solution: ``` from math import sqrt def sort_fun(el): return sqrt(el[0] ** 2 + el[1] ** 2) def main(): cities, population = [int(x) for x in input().split(" ")] data = [] for _ in range(cities): data.append([int(x) for x in input().split(" ")]) data.sort(key=sort_fun) for x, y, inc_pop in data: population += inc_pop if population >= 1000000: print(sqrt(x ** 2 + y ** 2)) return print(-1) main() ```
output
1
1,970
1
3,941
Provide tags and a correct Python 3 solution for this coding contest problem. The administration of the Tomsk Region firmly believes that it's time to become a megacity (that is, get population of one million). Instead of improving the demographic situation, they decided to achieve its goal by expanding the boundaries of the city. The city of Tomsk can be represented as point on the plane with coordinates (0; 0). The city is surrounded with n other locations, the i-th one has coordinates (xi, yi) with the population of ki people. You can widen the city boundaries to a circle of radius r. In such case all locations inside the circle and on its border are included into the city. Your goal is to write a program that will determine the minimum radius r, to which is necessary to expand the boundaries of Tomsk, so that it becomes a megacity. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 103; 1 ≤ s < 106) — the number of locatons around Tomsk city and the population of the city. Then n lines follow. The i-th line contains three integers — the xi and yi coordinate values of the i-th location and the number ki of people in it (1 ≤ ki < 106). Each coordinate is an integer and doesn't exceed 104 in its absolute value. It is guaranteed that no two locations are at the same point and no location is at point (0; 0). Output In the output, print "-1" (without the quotes), if Tomsk won't be able to become a megacity. Otherwise, in the first line print a single real number — the minimum radius of the circle that the city needs to expand to in order to become a megacity. The answer is considered correct if the absolute or relative error don't exceed 10 - 6. Examples Input 4 999998 1 1 1 2 2 1 3 3 1 2 -2 1 Output 2.8284271 Input 4 999998 1 1 2 2 2 1 3 3 1 2 -2 1 Output 1.4142136 Input 2 1 1 1 999997 2 2 1 Output -1
instruction
0
1,971
1
3,942
Tags: binary search, greedy, implementation, sortings Correct Solution: ``` from math import * R = lambda:map(int, input().split()) n, s = R() x = sorted((hypot(x, y), k) for x, y, k in (R() for i in range(n))) for t in x: s += t[1] if s >= 1000000: print(t[0]) exit() print(-1) ```
output
1
1,971
1
3,943
Provide tags and a correct Python 3 solution for this coding contest problem. The administration of the Tomsk Region firmly believes that it's time to become a megacity (that is, get population of one million). Instead of improving the demographic situation, they decided to achieve its goal by expanding the boundaries of the city. The city of Tomsk can be represented as point on the plane with coordinates (0; 0). The city is surrounded with n other locations, the i-th one has coordinates (xi, yi) with the population of ki people. You can widen the city boundaries to a circle of radius r. In such case all locations inside the circle and on its border are included into the city. Your goal is to write a program that will determine the minimum radius r, to which is necessary to expand the boundaries of Tomsk, so that it becomes a megacity. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 103; 1 ≤ s < 106) — the number of locatons around Tomsk city and the population of the city. Then n lines follow. The i-th line contains three integers — the xi and yi coordinate values of the i-th location and the number ki of people in it (1 ≤ ki < 106). Each coordinate is an integer and doesn't exceed 104 in its absolute value. It is guaranteed that no two locations are at the same point and no location is at point (0; 0). Output In the output, print "-1" (without the quotes), if Tomsk won't be able to become a megacity. Otherwise, in the first line print a single real number — the minimum radius of the circle that the city needs to expand to in order to become a megacity. The answer is considered correct if the absolute or relative error don't exceed 10 - 6. Examples Input 4 999998 1 1 1 2 2 1 3 3 1 2 -2 1 Output 2.8284271 Input 4 999998 1 1 2 2 2 1 3 3 1 2 -2 1 Output 1.4142136 Input 2 1 1 1 999997 2 2 1 Output -1
instruction
0
1,972
1
3,944
Tags: binary search, greedy, implementation, sortings Correct Solution: ``` n,pop = list(map(int,input().split())) arr = [] d = {} cnt = 0 for i in range(n): x,y,p = list(map(int,input().split())) x,y = abs(x),abs(y) cnt+=p r = (x**2 + y**2)**(0.5) arr.append([r,x,y,p]) arr.sort() if pop+cnt<1000000: print(-1) else: cnt = 0 for i in arr: r,x,y,p = i[0],i[1],i[2],i[3] cnt+=p if cnt+pop>=10**6: u,v = x,y break r = (u**2 + v**2)**(0.5) print(r) ```
output
1
1,972
1
3,945
Provide tags and a correct Python 3 solution for this coding contest problem. The administration of the Tomsk Region firmly believes that it's time to become a megacity (that is, get population of one million). Instead of improving the demographic situation, they decided to achieve its goal by expanding the boundaries of the city. The city of Tomsk can be represented as point on the plane with coordinates (0; 0). The city is surrounded with n other locations, the i-th one has coordinates (xi, yi) with the population of ki people. You can widen the city boundaries to a circle of radius r. In such case all locations inside the circle and on its border are included into the city. Your goal is to write a program that will determine the minimum radius r, to which is necessary to expand the boundaries of Tomsk, so that it becomes a megacity. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 103; 1 ≤ s < 106) — the number of locatons around Tomsk city and the population of the city. Then n lines follow. The i-th line contains three integers — the xi and yi coordinate values of the i-th location and the number ki of people in it (1 ≤ ki < 106). Each coordinate is an integer and doesn't exceed 104 in its absolute value. It is guaranteed that no two locations are at the same point and no location is at point (0; 0). Output In the output, print "-1" (without the quotes), if Tomsk won't be able to become a megacity. Otherwise, in the first line print a single real number — the minimum radius of the circle that the city needs to expand to in order to become a megacity. The answer is considered correct if the absolute or relative error don't exceed 10 - 6. Examples Input 4 999998 1 1 1 2 2 1 3 3 1 2 -2 1 Output 2.8284271 Input 4 999998 1 1 2 2 2 1 3 3 1 2 -2 1 Output 1.4142136 Input 2 1 1 1 999997 2 2 1 Output -1
instruction
0
1,973
1
3,946
Tags: binary search, greedy, implementation, sortings Correct Solution: ``` from math import * a,b=map(int,input().split()) s=sorted([hypot(i,j),k] for (i,j,k) in (map(int,input().split()) for _ in " "*a)) for i in range(1,a): s[i][1]+=s[i-1][1] lo=0;hi=a-1;ans=-1 while lo<=hi: mid=(lo+hi)//2 if ((b+s[mid][1])>=10**6):ans=mid;hi=mid-1 else:lo=mid+1 if (ans!=-1) :print(s[ans][0]) else:print(-1) ```
output
1
1,973
1
3,947
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The administration of the Tomsk Region firmly believes that it's time to become a megacity (that is, get population of one million). Instead of improving the demographic situation, they decided to achieve its goal by expanding the boundaries of the city. The city of Tomsk can be represented as point on the plane with coordinates (0; 0). The city is surrounded with n other locations, the i-th one has coordinates (xi, yi) with the population of ki people. You can widen the city boundaries to a circle of radius r. In such case all locations inside the circle and on its border are included into the city. Your goal is to write a program that will determine the minimum radius r, to which is necessary to expand the boundaries of Tomsk, so that it becomes a megacity. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 103; 1 ≤ s < 106) — the number of locatons around Tomsk city and the population of the city. Then n lines follow. The i-th line contains three integers — the xi and yi coordinate values of the i-th location and the number ki of people in it (1 ≤ ki < 106). Each coordinate is an integer and doesn't exceed 104 in its absolute value. It is guaranteed that no two locations are at the same point and no location is at point (0; 0). Output In the output, print "-1" (without the quotes), if Tomsk won't be able to become a megacity. Otherwise, in the first line print a single real number — the minimum radius of the circle that the city needs to expand to in order to become a megacity. The answer is considered correct if the absolute or relative error don't exceed 10 - 6. Examples Input 4 999998 1 1 1 2 2 1 3 3 1 2 -2 1 Output 2.8284271 Input 4 999998 1 1 2 2 2 1 3 3 1 2 -2 1 Output 1.4142136 Input 2 1 1 1 999997 2 2 1 Output -1 Submitted Solution: ``` from math import sqrt import sys n, m = map(int, input().split()) d = dict() for _ in range(n): x,y,z = map(int, input().split()) r = round(sqrt(x*x + y*y),7) if r in d: d[r] = z + d[r] else: d[r] = z d = sorted(d.items(), key=lambda x: x[0]) N = len(d) dt = 0 for key in d: m += key[1] if m >= 10**6: print(key[0]) sys.exit() print(-1) ## Complexity O(Nlog(N)) ```
instruction
0
1,974
1
3,948
Yes
output
1
1,974
1
3,949
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The administration of the Tomsk Region firmly believes that it's time to become a megacity (that is, get population of one million). Instead of improving the demographic situation, they decided to achieve its goal by expanding the boundaries of the city. The city of Tomsk can be represented as point on the plane with coordinates (0; 0). The city is surrounded with n other locations, the i-th one has coordinates (xi, yi) with the population of ki people. You can widen the city boundaries to a circle of radius r. In such case all locations inside the circle and on its border are included into the city. Your goal is to write a program that will determine the minimum radius r, to which is necessary to expand the boundaries of Tomsk, so that it becomes a megacity. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 103; 1 ≤ s < 106) — the number of locatons around Tomsk city and the population of the city. Then n lines follow. The i-th line contains three integers — the xi and yi coordinate values of the i-th location and the number ki of people in it (1 ≤ ki < 106). Each coordinate is an integer and doesn't exceed 104 in its absolute value. It is guaranteed that no two locations are at the same point and no location is at point (0; 0). Output In the output, print "-1" (without the quotes), if Tomsk won't be able to become a megacity. Otherwise, in the first line print a single real number — the minimum radius of the circle that the city needs to expand to in order to become a megacity. The answer is considered correct if the absolute or relative error don't exceed 10 - 6. Examples Input 4 999998 1 1 1 2 2 1 3 3 1 2 -2 1 Output 2.8284271 Input 4 999998 1 1 2 2 2 1 3 3 1 2 -2 1 Output 1.4142136 Input 2 1 1 1 999997 2 2 1 Output -1 Submitted Solution: ``` n, s = map(int, input().split()) vals = [tuple(map(int, input().split())) for i in range(n)] vals.sort(key = lambda e: e[0]*e[0] + e[1]*e[1]) b = True for v in vals: s += v[2] if s >= 1000000: print((v[0]*v[0] + v[1]*v[1])**0.5) b = False break if b: print(-1) ```
instruction
0
1,975
1
3,950
Yes
output
1
1,975
1
3,951
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The administration of the Tomsk Region firmly believes that it's time to become a megacity (that is, get population of one million). Instead of improving the demographic situation, they decided to achieve its goal by expanding the boundaries of the city. The city of Tomsk can be represented as point on the plane with coordinates (0; 0). The city is surrounded with n other locations, the i-th one has coordinates (xi, yi) with the population of ki people. You can widen the city boundaries to a circle of radius r. In such case all locations inside the circle and on its border are included into the city. Your goal is to write a program that will determine the minimum radius r, to which is necessary to expand the boundaries of Tomsk, so that it becomes a megacity. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 103; 1 ≤ s < 106) — the number of locatons around Tomsk city and the population of the city. Then n lines follow. The i-th line contains three integers — the xi and yi coordinate values of the i-th location and the number ki of people in it (1 ≤ ki < 106). Each coordinate is an integer and doesn't exceed 104 in its absolute value. It is guaranteed that no two locations are at the same point and no location is at point (0; 0). Output In the output, print "-1" (without the quotes), if Tomsk won't be able to become a megacity. Otherwise, in the first line print a single real number — the minimum radius of the circle that the city needs to expand to in order to become a megacity. The answer is considered correct if the absolute or relative error don't exceed 10 - 6. Examples Input 4 999998 1 1 1 2 2 1 3 3 1 2 -2 1 Output 2.8284271 Input 4 999998 1 1 2 2 2 1 3 3 1 2 -2 1 Output 1.4142136 Input 2 1 1 1 999997 2 2 1 Output -1 Submitted Solution: ``` def distanciaAlCero(x, y): return (x**2 + y**2)**0.5 POBLACION_MINIMA = 1_000_000 # Lista de tuplas # Tupla: (distancia, k) ubicaciones = [] # Asumiendo que todos los inputs son correctos n, s = map(int, input().split()) for i in range(n): x, y, k = map(int, input().split()) ubicaciones.append((distanciaAlCero(x, y), k)) ubicaciones.sort() for distancia, k in ubicaciones: s += k if s >= POBLACION_MINIMA: print("%.7f" % distancia) exit(0) print(-1) ```
instruction
0
1,976
1
3,952
Yes
output
1
1,976
1
3,953
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The administration of the Tomsk Region firmly believes that it's time to become a megacity (that is, get population of one million). Instead of improving the demographic situation, they decided to achieve its goal by expanding the boundaries of the city. The city of Tomsk can be represented as point on the plane with coordinates (0; 0). The city is surrounded with n other locations, the i-th one has coordinates (xi, yi) with the population of ki people. You can widen the city boundaries to a circle of radius r. In such case all locations inside the circle and on its border are included into the city. Your goal is to write a program that will determine the minimum radius r, to which is necessary to expand the boundaries of Tomsk, so that it becomes a megacity. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 103; 1 ≤ s < 106) — the number of locatons around Tomsk city and the population of the city. Then n lines follow. The i-th line contains three integers — the xi and yi coordinate values of the i-th location and the number ki of people in it (1 ≤ ki < 106). Each coordinate is an integer and doesn't exceed 104 in its absolute value. It is guaranteed that no two locations are at the same point and no location is at point (0; 0). Output In the output, print "-1" (without the quotes), if Tomsk won't be able to become a megacity. Otherwise, in the first line print a single real number — the minimum radius of the circle that the city needs to expand to in order to become a megacity. The answer is considered correct if the absolute or relative error don't exceed 10 - 6. Examples Input 4 999998 1 1 1 2 2 1 3 3 1 2 -2 1 Output 2.8284271 Input 4 999998 1 1 2 2 2 1 3 3 1 2 -2 1 Output 1.4142136 Input 2 1 1 1 999997 2 2 1 Output -1 Submitted Solution: ``` import math as m n, s = map(int, input().split()) MEGA_CITY = 10**6 cities =[] for _ in range(n): params = tuple(map(int, input().split())) cities.append(params) cities.sort(key=lambda dist: m.sqrt(dist[0]**2+dist[1]**2)) res = 0.0 current_population = s for city in cities: current_population += city[2] if current_population >= MEGA_CITY: res = m.sqrt(city[0] ** 2 + city[1] ** 2) break if current_population < MEGA_CITY: print("-1") else: print("{0:.7f}".format(res)) ```
instruction
0
1,977
1
3,954
Yes
output
1
1,977
1
3,955
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The administration of the Tomsk Region firmly believes that it's time to become a megacity (that is, get population of one million). Instead of improving the demographic situation, they decided to achieve its goal by expanding the boundaries of the city. The city of Tomsk can be represented as point on the plane with coordinates (0; 0). The city is surrounded with n other locations, the i-th one has coordinates (xi, yi) with the population of ki people. You can widen the city boundaries to a circle of radius r. In such case all locations inside the circle and on its border are included into the city. Your goal is to write a program that will determine the minimum radius r, to which is necessary to expand the boundaries of Tomsk, so that it becomes a megacity. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 103; 1 ≤ s < 106) — the number of locatons around Tomsk city and the population of the city. Then n lines follow. The i-th line contains three integers — the xi and yi coordinate values of the i-th location and the number ki of people in it (1 ≤ ki < 106). Each coordinate is an integer and doesn't exceed 104 in its absolute value. It is guaranteed that no two locations are at the same point and no location is at point (0; 0). Output In the output, print "-1" (without the quotes), if Tomsk won't be able to become a megacity. Otherwise, in the first line print a single real number — the minimum radius of the circle that the city needs to expand to in order to become a megacity. The answer is considered correct if the absolute or relative error don't exceed 10 - 6. Examples Input 4 999998 1 1 1 2 2 1 3 3 1 2 -2 1 Output 2.8284271 Input 4 999998 1 1 2 2 2 1 3 3 1 2 -2 1 Output 1.4142136 Input 2 1 1 1 999997 2 2 1 Output -1 Submitted Solution: ``` import math n, s = input ().split () n, s = int (n), int (s) x1 = y1 = 0 p = False r1 = -1 for i in range (n): x, y, k = input ().split () x, y, k = int (x), int (y), int (k) s += k if p == False: if x > x1: x1 = x if y > y1: y1 = y if s >= 1000000: r = math.sqrt (x1 * x1 + y1 * y1) r1 = r p = True print(round(r1,7)) ```
instruction
0
1,978
1
3,956
No
output
1
1,978
1
3,957
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The administration of the Tomsk Region firmly believes that it's time to become a megacity (that is, get population of one million). Instead of improving the demographic situation, they decided to achieve its goal by expanding the boundaries of the city. The city of Tomsk can be represented as point on the plane with coordinates (0; 0). The city is surrounded with n other locations, the i-th one has coordinates (xi, yi) with the population of ki people. You can widen the city boundaries to a circle of radius r. In such case all locations inside the circle and on its border are included into the city. Your goal is to write a program that will determine the minimum radius r, to which is necessary to expand the boundaries of Tomsk, so that it becomes a megacity. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 103; 1 ≤ s < 106) — the number of locatons around Tomsk city and the population of the city. Then n lines follow. The i-th line contains three integers — the xi and yi coordinate values of the i-th location and the number ki of people in it (1 ≤ ki < 106). Each coordinate is an integer and doesn't exceed 104 in its absolute value. It is guaranteed that no two locations are at the same point and no location is at point (0; 0). Output In the output, print "-1" (without the quotes), if Tomsk won't be able to become a megacity. Otherwise, in the first line print a single real number — the minimum radius of the circle that the city needs to expand to in order to become a megacity. The answer is considered correct if the absolute or relative error don't exceed 10 - 6. Examples Input 4 999998 1 1 1 2 2 1 3 3 1 2 -2 1 Output 2.8284271 Input 4 999998 1 1 2 2 2 1 3 3 1 2 -2 1 Output 1.4142136 Input 2 1 1 1 999997 2 2 1 Output -1 Submitted Solution: ``` # @Author: Justin Hershberger # @Date: 28-03-2017 # @Filename: 424B.py # @Last modified by: Justin Hershberger # @Last modified time: 28-03-2017 import math if __name__ == '__main__': n, pop = map(int, input().split()) loc = [] for arg in range(n): loc.append(input().split()) # print(n,pop) # print(loc) radius = -1 for r in range(len(loc)): if pop + int(loc[r][2]) >= 1000000: radius = math.sqrt(int(loc[r][0])**2 + int(loc[r][1])**2) break else: pop += int(loc[r][2]) if radius == -1: print(radius) else: print("{0:.7f}".format(radius)) ```
instruction
0
1,979
1
3,958
No
output
1
1,979
1
3,959
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The administration of the Tomsk Region firmly believes that it's time to become a megacity (that is, get population of one million). Instead of improving the demographic situation, they decided to achieve its goal by expanding the boundaries of the city. The city of Tomsk can be represented as point on the plane with coordinates (0; 0). The city is surrounded with n other locations, the i-th one has coordinates (xi, yi) with the population of ki people. You can widen the city boundaries to a circle of radius r. In such case all locations inside the circle and on its border are included into the city. Your goal is to write a program that will determine the minimum radius r, to which is necessary to expand the boundaries of Tomsk, so that it becomes a megacity. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 103; 1 ≤ s < 106) — the number of locatons around Tomsk city and the population of the city. Then n lines follow. The i-th line contains three integers — the xi and yi coordinate values of the i-th location and the number ki of people in it (1 ≤ ki < 106). Each coordinate is an integer and doesn't exceed 104 in its absolute value. It is guaranteed that no two locations are at the same point and no location is at point (0; 0). Output In the output, print "-1" (without the quotes), if Tomsk won't be able to become a megacity. Otherwise, in the first line print a single real number — the minimum radius of the circle that the city needs to expand to in order to become a megacity. The answer is considered correct if the absolute or relative error don't exceed 10 - 6. Examples Input 4 999998 1 1 1 2 2 1 3 3 1 2 -2 1 Output 2.8284271 Input 4 999998 1 1 2 2 2 1 3 3 1 2 -2 1 Output 1.4142136 Input 2 1 1 1 999997 2 2 1 Output -1 Submitted Solution: ``` from sys import maxsize, stdout, stdin,stderr mod = int(1e9 + 7) import sys def I(): return int(stdin.readline()) def lint(): return [int(x) for x in stdin.readline().split()] def S(): return input().strip() def grid(r, c): return [lint() for i in range(r)] from collections import defaultdict, Counter import math import heapq from heapq import heappop , heappush import bisect from itertools import groupby def gcd(a,b): while b: a %= b tmp = a a = b b = tmp return a def lcm(a,b): return a / gcd(a, b) * b def check_prime(n): for i in range(2, int(n ** (1 / 2)) + 1): if not n % i: return False return True def Bs(a, x): i=0 j=0 left = 1 right = k flag=False while left<right: mi = (left+right)//2 #print(smi,a[mi],x) if a[mi]<=x: left = mi+1 i+=1 else: right = mi j+=1 #print(left,right,"----") #print(i-1,j) if left>0 and a[left-1]==x: return i-1, j else: return -1, -1 def nCr(n, r): return (fact(n) // (fact(r) * fact(n - r))) # Returns factorial of n def fact(n): res = 1 for i in range(2, n+1): res = res * i return res def primefactors(n): num=0 while n % 2 == 0: num+=1 n = n / 2 for i in range(3,int(math.sqrt(n))+1,2): while n % i== 0: num+=1 n = n // i if n > 2: num+=1 return num ''' def iter_ds(src): store=[src] while len(store): tmp=store.pop() if not vis[tmp]: vis[tmp]=True for j in ar[tmp]: store.append(j) ''' def ask(a): print('? {}'.format(a),flush=True) n=lint() return n def bstemp(n,k): l=1 r=n+1 while l<r: mid=(l+r)//2 if (mid*(mid+1)*5)//2+k<=4*60: l=mid+1 else: r=mid return l def dfs(i,p): a,tmp=0,0 for j in d[i]: if j!=p: a+=1 tmp+=dfs(j,i) if a==0: return 0 return tmp/a + 1 def primeFactors(n): l=[] while n % 2 == 0: l.append(2) n = n // 2 if n > 2: l.append(n) return l n,s=lint() s=10**6-s a=[] r=0 for _ in range(n): x,y,k=lint() r+=k a.append([math.sqrt(x**2+y**2),r]) a.sort() if r<s: print(-1) else: l,r=0,n-1 while l<r: mid=l+(r-l)//2 if a[mid][1]>=s: r=mid else: l=mid+1 print(a[r][0]) ```
instruction
0
1,980
1
3,960
No
output
1
1,980
1
3,961
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The administration of the Tomsk Region firmly believes that it's time to become a megacity (that is, get population of one million). Instead of improving the demographic situation, they decided to achieve its goal by expanding the boundaries of the city. The city of Tomsk can be represented as point on the plane with coordinates (0; 0). The city is surrounded with n other locations, the i-th one has coordinates (xi, yi) with the population of ki people. You can widen the city boundaries to a circle of radius r. In such case all locations inside the circle and on its border are included into the city. Your goal is to write a program that will determine the minimum radius r, to which is necessary to expand the boundaries of Tomsk, so that it becomes a megacity. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 103; 1 ≤ s < 106) — the number of locatons around Tomsk city and the population of the city. Then n lines follow. The i-th line contains three integers — the xi and yi coordinate values of the i-th location and the number ki of people in it (1 ≤ ki < 106). Each coordinate is an integer and doesn't exceed 104 in its absolute value. It is guaranteed that no two locations are at the same point and no location is at point (0; 0). Output In the output, print "-1" (without the quotes), if Tomsk won't be able to become a megacity. Otherwise, in the first line print a single real number — the minimum radius of the circle that the city needs to expand to in order to become a megacity. The answer is considered correct if the absolute or relative error don't exceed 10 - 6. Examples Input 4 999998 1 1 1 2 2 1 3 3 1 2 -2 1 Output 2.8284271 Input 4 999998 1 1 2 2 2 1 3 3 1 2 -2 1 Output 1.4142136 Input 2 1 1 1 999997 2 2 1 Output -1 Submitted Solution: ``` from math import sqrt n, s = input().split() n, s = int(n), int(s) coord = [[abs(int(j)) for j in input().split()] for i in range(n)] coord.sort() print(coord) i = 0 b = True while b and i < n: rad = sqrt(coord[i][0]*coord[i][0] + coord[i][1]*coord[i][1]) s += coord[i][2] i += 1 if s >= 1000000: b = False break if b: print(-1) else: print(rad) ```
instruction
0
1,981
1
3,962
No
output
1
1,981
1
3,963
Provide tags and a correct Python 3 solution for this coding contest problem. Meanwhile, the kingdom of K is getting ready for the marriage of the King's daughter. However, in order not to lose face in front of the relatives, the King should first finish reforms in his kingdom. As the King can not wait for his daughter's marriage, reforms must be finished as soon as possible. The kingdom currently consists of n cities. Cities are connected by n - 1 bidirectional road, such that one can get from any city to any other city. As the King had to save a lot, there is only one path between any two cities. What is the point of the reform? The key ministries of the state should be relocated to distinct cities (we call such cities important). However, due to the fact that there is a high risk of an attack by barbarians it must be done carefully. The King has made several plans, each of which is described by a set of important cities, and now wonders what is the best plan. Barbarians can capture some of the cities that are not important (the important ones will have enough protection for sure), after that the captured city becomes impassable. In particular, an interesting feature of the plan is the minimum number of cities that the barbarians need to capture in order to make all the important cities isolated, that is, from all important cities it would be impossible to reach any other important city. Help the King to calculate this characteristic for each of his plan. Input The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cities in the kingdom. Each of the next n - 1 lines contains two distinct integers ui, vi (1 ≤ ui, vi ≤ n) — the indices of the cities connected by the i-th road. It is guaranteed that you can get from any city to any other one moving only along the existing roads. The next line contains a single integer q (1 ≤ q ≤ 100 000) — the number of King's plans. Each of the next q lines looks as follows: first goes number ki — the number of important cities in the King's plan, (1 ≤ ki ≤ n), then follow exactly ki space-separated pairwise distinct numbers from 1 to n — the numbers of important cities in this plan. The sum of all ki's does't exceed 100 000. Output For each plan print a single integer — the minimum number of cities that the barbarians need to capture, or print - 1 if all the barbarians' attempts to isolate important cities will not be effective. Examples Input 4 1 3 2 3 4 3 4 2 1 2 3 2 3 4 3 1 2 4 4 1 2 3 4 Output 1 -1 1 -1 Input 7 1 2 2 3 3 4 1 5 5 6 5 7 1 4 2 4 6 7 Output 2 Note In the first sample, in the first and the third King's plan barbarians can capture the city 3, and that will be enough. In the second and the fourth plans all their attempts will not be effective. In the second sample the cities to capture are 3 and 5.
instruction
0
2,070
1
4,140
Tags: dfs and similar, divide and conquer, dp, graphs, sortings, trees Correct Solution: ``` import sys from collections import deque def solve(): sys.setrecursionlimit(10**6) readline = sys.stdin.readline writelines = sys.stdout.writelines N = int(readline()) G = [[] for i in range(N)] for i in range(N-1): u, v = map(int, readline().split()) G[u-1].append(v-1) G[v-1].append(u-1) # Euler tour technique S = [] FS = [0]*N; LS = [0]*N depth = [0]*N stk = [-1, 0] it = [0]*N while len(stk) > 1: v = stk[-1] i = it[v] if i == 0: FS[v] = len(S) depth[v] = len(stk) if i < len(G[v]) and G[v][i] == stk[-2]: it[v] += 1 i += 1 if i == len(G[v]): LS[v] = len(S) stk.pop() else: stk.append(G[v][i]) it[v] += 1 S.append(v) L = len(S) lg = [0]*(L+1) # Sparse Table for i in range(2, L+1): lg[i] = lg[i >> 1] + 1 st = [[L]*(L - (1 << i) + 1) for i in range(lg[L]+1)] st[0][:] = S b = 1 for i in range(lg[L]): st0 = st[i] st1 = st[i+1] for j in range(L - (b<<1) + 1): st1[j] = (st0[j] if depth[st0[j]] <= depth[st0[j+b]] else st0[j+b]) b <<= 1 INF = 10**18 ans = [] Q = int(readline()) G0 = [[]]*N P = [0]*N deg = [0]*N KS = [0]*N A = [0]*N B = [0]*N for t in range(Q): k, *vs = map(int, readline().split()) for i in range(k): vs[i] -= 1 KS[vs[i]] = 1 vs.sort(key=FS.__getitem__) for i in range(k-1): x = FS[vs[i]]; y = FS[vs[i+1]] l = lg[y - x + 1] w = st[l][x] if depth[st[l][x]] <= depth[st[l][y - (1 << l) + 1]] else st[l][y - (1 << l) + 1] vs.append(w) vs.sort(key=FS.__getitem__) stk = [] prv = -1 for v in vs: if v == prv: continue while stk and LS[stk[-1]] < FS[v]: stk.pop() if stk: G0[stk[-1]].append(v) G0[v] = [] it[v] = 0 stk.append(v) prv = v que = deque() prv = -1 P[vs[0]] = -1 for v in vs: if v == prv: continue for w in G0[v]: P[w] = v deg[v] = len(G0[v]) if deg[v] == 0: que.append(v) prv = v while que: v = que.popleft() if KS[v]: a = 0 for w in G0[v]: ra = A[w]; rb = B[w] if depth[v]+1 < depth[w]: a += min(ra, rb+1) else: a += ra A[v] = INF B[v] = a else: a = 0; b = c = INF for w in G0[v]: ra = A[w]; rb = B[w] a, b, c = a + ra, min(a + rb, b + ra), min(b + rb, c + min(ra, rb)) A[v] = min(a, b+1, c+1) B[v] = b p = P[v] if p != -1: deg[p] -= 1 if deg[p] == 0: que.append(p) v = min(A[vs[0]], B[vs[0]]) if v >= INF: ans.append("-1\n") else: ans.append("%d\n" % v) for v in vs: KS[v] = 0 writelines(ans) solve() ```
output
1
2,070
1
4,141
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Meanwhile, the kingdom of K is getting ready for the marriage of the King's daughter. However, in order not to lose face in front of the relatives, the King should first finish reforms in his kingdom. As the King can not wait for his daughter's marriage, reforms must be finished as soon as possible. The kingdom currently consists of n cities. Cities are connected by n - 1 bidirectional road, such that one can get from any city to any other city. As the King had to save a lot, there is only one path between any two cities. What is the point of the reform? The key ministries of the state should be relocated to distinct cities (we call such cities important). However, due to the fact that there is a high risk of an attack by barbarians it must be done carefully. The King has made several plans, each of which is described by a set of important cities, and now wonders what is the best plan. Barbarians can capture some of the cities that are not important (the important ones will have enough protection for sure), after that the captured city becomes impassable. In particular, an interesting feature of the plan is the minimum number of cities that the barbarians need to capture in order to make all the important cities isolated, that is, from all important cities it would be impossible to reach any other important city. Help the King to calculate this characteristic for each of his plan. Input The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cities in the kingdom. Each of the next n - 1 lines contains two distinct integers ui, vi (1 ≤ ui, vi ≤ n) — the indices of the cities connected by the i-th road. It is guaranteed that you can get from any city to any other one moving only along the existing roads. The next line contains a single integer q (1 ≤ q ≤ 100 000) — the number of King's plans. Each of the next q lines looks as follows: first goes number ki — the number of important cities in the King's plan, (1 ≤ ki ≤ n), then follow exactly ki space-separated pairwise distinct numbers from 1 to n — the numbers of important cities in this plan. The sum of all ki's does't exceed 100 000. Output For each plan print a single integer — the minimum number of cities that the barbarians need to capture, or print - 1 if all the barbarians' attempts to isolate important cities will not be effective. Examples Input 4 1 3 2 3 4 3 4 2 1 2 3 2 3 4 3 1 2 4 4 1 2 3 4 Output 1 -1 1 -1 Input 7 1 2 2 3 3 4 1 5 5 6 5 7 1 4 2 4 6 7 Output 2 Note In the first sample, in the first and the third King's plan barbarians can capture the city 3, and that will be enough. In the second and the fourth plans all their attempts will not be effective. In the second sample the cities to capture are 3 and 5. Submitted Solution: ``` import sys sys.setrecursionlimit(10**6) readline = sys.stdin.readline writelines = sys.stdout.writelines N = int(readline()) G = [[] for i in range(N)] for i in range(N-1): u, v = map(int, readline().split()) G[u-1].append(v-1) G[v-1].append(u-1) # Euler tour technique S = [] FS = [0]*N; LS = [0]*N depth = [0]*N def dfs(v, p, d): depth[v] = d FS[v] = len(S) S.append(v) for w in G[v]: if w == p: continue dfs(w, v, d+1) S.append(v) LS[v] = len(S) S.append(v) dfs(0, -1, 0) L = len(S) lg = [0]*(L+1) # Sparse Table for i in range(2, L+1): lg[i] = lg[i >> 1] + 1 st = [[L]*L for i in range(lg[L]+1)] st[0][:] = S b = 1 for i in range(lg[L]): st0 = st[i] st1 = st[i+1] for j in range(L - (b<<1) + 1): st1[j] = (st0[j] if depth[st0[j]] <= depth[st0[j+b]] else st0[j+b]) b <<= 1 INF = 10**18 ans = [] Q = int(readline()) G0 = [None for i in range(N)] KS = [0]*N for t in range(Q): k, *vs = map(int, readline().split()) for i in range(k): vs[i] -= 1 KS[vs[i]] = 1 vs.sort(key=FS.__getitem__) for i in range(len(vs)-1): x = FS[vs[i]]; y = FS[vs[i+1]] l = lg[y - x + 1] w = st[l][x] if depth[st[l][x]] <= depth[st[l][y - (1 << l) + 1]] else st[l][y - (1 << l) + 1] vs.append(w) vs.sort(key=FS.__getitem__) prv = -1 for v in vs: if v == prv: continue G0[v] = [] prv = v stk = [] prv = -1 for v in vs: if v == prv: continue while stk and LS[stk[-1]] < FS[v]: stk.pop() if stk: G0[stk[-1]].append(v) stk.append(v) prv = v def dfs(v): if KS[v]: a = 0 for w in G0[v]: ra, rb = dfs(w) if depth[v]+1 < depth[w]: a += min(ra, rb+1) else: a += ra return INF, a a = 0; b = c = INF for w in G0[v]: ra, rb = dfs(w) a, b, c = a + ra, min(a + rb, b + ra), min(b + rb, c + min(ra, rb)) if KS[v]: return INF, a return min(a, c+1), b a, b = dfs(vs[0]) v = min(a, b) if v >= INF: ans.append("-1\n") else: ans.append("%d\n" % v) for v in vs: KS[v] = 0 writelines(ans) ```
instruction
0
2,071
1
4,142
No
output
1
2,071
1
4,143
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Meanwhile, the kingdom of K is getting ready for the marriage of the King's daughter. However, in order not to lose face in front of the relatives, the King should first finish reforms in his kingdom. As the King can not wait for his daughter's marriage, reforms must be finished as soon as possible. The kingdom currently consists of n cities. Cities are connected by n - 1 bidirectional road, such that one can get from any city to any other city. As the King had to save a lot, there is only one path between any two cities. What is the point of the reform? The key ministries of the state should be relocated to distinct cities (we call such cities important). However, due to the fact that there is a high risk of an attack by barbarians it must be done carefully. The King has made several plans, each of which is described by a set of important cities, and now wonders what is the best plan. Barbarians can capture some of the cities that are not important (the important ones will have enough protection for sure), after that the captured city becomes impassable. In particular, an interesting feature of the plan is the minimum number of cities that the barbarians need to capture in order to make all the important cities isolated, that is, from all important cities it would be impossible to reach any other important city. Help the King to calculate this characteristic for each of his plan. Input The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cities in the kingdom. Each of the next n - 1 lines contains two distinct integers ui, vi (1 ≤ ui, vi ≤ n) — the indices of the cities connected by the i-th road. It is guaranteed that you can get from any city to any other one moving only along the existing roads. The next line contains a single integer q (1 ≤ q ≤ 100 000) — the number of King's plans. Each of the next q lines looks as follows: first goes number ki — the number of important cities in the King's plan, (1 ≤ ki ≤ n), then follow exactly ki space-separated pairwise distinct numbers from 1 to n — the numbers of important cities in this plan. The sum of all ki's does't exceed 100 000. Output For each plan print a single integer — the minimum number of cities that the barbarians need to capture, or print - 1 if all the barbarians' attempts to isolate important cities will not be effective. Examples Input 4 1 3 2 3 4 3 4 2 1 2 3 2 3 4 3 1 2 4 4 1 2 3 4 Output 1 -1 1 -1 Input 7 1 2 2 3 3 4 1 5 5 6 5 7 1 4 2 4 6 7 Output 2 Note In the first sample, in the first and the third King's plan barbarians can capture the city 3, and that will be enough. In the second and the fourth plans all their attempts will not be effective. In the second sample the cities to capture are 3 and 5. Submitted Solution: ``` import sys sys.setrecursionlimit(10**6) readline = sys.stdin.readline writelines = sys.stdout.writelines N = int(readline()) G = [[] for i in range(N)] for i in range(N-1): u, v = map(int, readline().split()) G[u-1].append(v-1) G[v-1].append(u-1) # Euler tour technique S = [] FS = [0]*N; LS = [0]*N depth = [0]*N def dfs(v, p, d): depth[v] = d FS[v] = len(S) S.append(v) for w in G[v]: if w == p: continue dfs(w, v, d+1) S.append(v) LS[v] = len(S) S.append(v) dfs(0, -1, 0) L = len(S) lg = [0]*(L+1) # Sparse Table for i in range(2, L+1): lg[i] = lg[i >> 1] + 1 st = [[L]*L for i in range(lg[L]+1)] st[0][:] = S b = 1 for i in range(lg[L]): st0 = st[i] st1 = st[i+1] for j in range(L - (b<<1) + 1): st1[j] = (st0[j] if depth[st0[j]] <= depth[st0[j+b]] else st0[j+b]) b <<= 1 INF = 10**18 ans = [] Q = int(readline()) G0 = [None for i in range(N)] KS = [0]*N for t in range(Q): k, *vs = map(int, readline().split()) for i in range(k): vs[i] -= 1 KS[vs[i]] = 1 vs.sort(key=FS.__getitem__) for i in range(len(vs)-1): x = FS[vs[i]]; y = FS[vs[i+1]] l = lg[y - x + 1] w = st[l][x] if depth[st[l][x]] <= depth[st[l][y - (1 << l) + 1]] else st[l][y - (1 << l) + 1] vs.append(w) vs.sort(key=FS.__getitem__) prv = -1 for v in vs: if v == prv: continue G0[v] = [] prv = v stk = [] prv = -1 for v in vs: if v == prv: continue while stk and LS[stk[-1]] < FS[v]: stk.pop() if stk: G0[stk[-1]].append(v) stk.append(v) prv = v def dfs(v): a = 0; b = c = INF for w in G0[v]: ra, rb = dfs(w) rb += (depth[w] - depth[v]) - 1 a, b, c = a + ra, min(a + rb, b + ra), min(b + rb, c + min(ra, rb)) if KS[v]: return min(b, c), min(a, b, c) b += 1; c += 1 return min(a, c), min(b, c) a, b = dfs(vs[0]) if a == 0: ans.append("-1\n") elif a >= INF: ans.append("0\n") else: ans.append("%d\n" % a) for v in vs: KS[v] = 0 writelines(ans) ```
instruction
0
2,072
1
4,144
No
output
1
2,072
1
4,145
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. A new trade empire is rising in Berland. Bulmart, an emerging trade giant, decided to dominate the market of ... shovels! And now almost every city in Berland has a Bulmart store, and some cities even have several of them! The only problem is, at the moment sales are ... let's say a little below estimates. Some people even say that shovels retail market is too small for such a big company to make a profit. But the company management believes in the future of that market and seeks new ways to increase income. There are n cities in Berland connected with m bi-directional roads. All roads have equal lengths. It can happen that it is impossible to reach a city from another city using only roads. There is no road which connects a city to itself. Any pair of cities can be connected by at most one road. There are w Bulmart stores in Berland. Each of them is described by three numbers: * ci — the number of city where the i-th store is located (a city can have no stores at all or have several of them), * ki — the number of shovels in the i-th store, * pi — the price of a single shovel in the i-th store (in burles). The latest idea of the Bulmart management is to create a program which will help customers get shovels as fast as possible for affordable budget. Formally, the program has to find the minimum amount of time needed to deliver rj shovels to the customer in the city gj for the total cost of no more than aj burles. The delivery time between any two adjacent cities is equal to 1. If shovels are delivered from several cities, the delivery time is equal to the arrival time of the last package. The delivery itself is free of charge. The program needs to find answers to q such queries. Each query has to be processed independently from others, i.e. a query does not change number of shovels in stores for the next queries. Input The first line contains two integers n, m (1 ≤ n ≤ 5000, 0 ≤ m ≤ min(5000, n·(n - 1) / 2)). Each of the next m lines contains two integers xe and ye, meaning that the e-th road connects cities xe and ye (1 ≤ xe, ye ≤ n). The next line contains a single integer w (1 ≤ w ≤ 5000) — the total number of Bulmart stores in Berland. Each of the next w lines contains three integers describing the i-th store: ci, ki, pi (1 ≤ ci ≤ n, 1 ≤ ki, pi ≤ 2·105). The next line contains a single integer q (1 ≤ q ≤ 1000) — the number of queries. Each of the next q lines contains three integers describing the j-th query: gj, rj and aj (1 ≤ gj ≤ n, 1 ≤ rj, aj ≤ 109) Output Output q lines. On the j-th line, print an answer for the j-th query — the minimum amount of time needed to deliver rj shovels to the customer in city gj spending no more than aj burles. Print -1 if there is no solution for the j-th query. Example Input 6 4 4 2 5 4 1 2 3 2 2 4 1 2 3 2 3 6 1 2 6 2 3 7 3 1 2 4 3 8 5 2 5 6 1 10 Output 2 -1 2 2 3 -1 Submitted Solution: ``` def add_edge(u,v): global edge_to,edge_next,cnt,head edge_to[cnt] = v; edge_next[cnt] = head[u]; head[u] = cnt; cnt += 1; def _check(time,need,cost): global W,dist,w tot = 0 for i in range(w): if (dist[W[i][2]] > time): continue; if (need-W[i][1] >= 0): if (tot+W[i][1]*W[i][0] > cost): return -1; tot += (W[i][1]*W[i][0]) need -= W[i][1] else : if (tot+W[i][0]*need > cost): return -1; return tot+W[i][0]*need; if (need > 0): return -1; return 1; def bfs(city,need,cost): global n,edge_to,edge_next,head,dist dist[city] = 0 check = [True for i in range(n+1)]; bfs_ = []; bfs_.append(city); while (len(bfs_) > 0): fr = bfs_[0]; check[fr] = False i = head[fr] while (i > 0): to = edge_to[i]; i = edge_next[i]; if (check[to] == False): continue; check[to] = False bfs_.append(to) if (dist[to] > dist[fr]+1): dist[to] = dist[fr]+1; del bfs_[0] l = 0; r = n; while (l <= r): mid = int((l+r)/2) if (_check(mid,need,cost) >= 0): r = mid-1; else : l = mid+1; if (_check(mid,need,cost) >= 0): return mid; elif (mid == n) : return -1; else : return mid+1 n,m = list(map(int,input().split())) head = [0 for i in range(n+1)] edge_to = [0 for i in range(int((n*n)/2))] edge_next = [0 for i in range(int((n*n)/2))] cnt = 1 for i in range(m): x,y = list(map(int,input().split())); add_edge(x,y); add_edge(y,x); w = int(input()) W = [[0]*3 for i in range(m+1)] for i in range(w): x,y,z = list(map(int,input().split())); W[i][2],W[i][1],W[i][0] = x,y,z; q = int(input()) while (q > 0): q -= 1; g,r,a = list(map(int,input().split())) dist =[10**10 for i in range(n+1)]; print(bfs(g,r,a)) ```
instruction
0
2,123
1
4,246
No
output
1
2,123
1
4,247
Provide tags and a correct Python 3 solution for this coding contest problem. The capital of Berland looks like a rectangle of size n × m of the square blocks of same size. Fire! It is known that k + 1 blocks got caught on fire (k + 1 ≤ n·m). Those blocks are centers of ignition. Moreover positions of k of these centers are known and one of these stays unknown. All k + 1 positions are distinct. The fire goes the following way: during the zero minute of fire only these k + 1 centers of ignition are burning. Every next minute the fire goes to all neighbouring blocks to the one which is burning. You can consider blocks to burn for so long that this time exceeds the time taken in the problem. The neighbouring blocks are those that touch the current block by a side or by a corner. Berland Fire Deparment wants to estimate the minimal time it takes the fire to lighten up the whole city. Remember that the positions of k blocks (centers of ignition) are known and (k + 1)-th can be positioned in any other block. Help Berland Fire Department to estimate the minimal time it takes the fire to lighten up the whole city. Input The first line contains three integers n, m and k (1 ≤ n, m ≤ 109, 1 ≤ k ≤ 500). Each of the next k lines contain two integers xi and yi (1 ≤ xi ≤ n, 1 ≤ yi ≤ m) — coordinates of the i-th center of ignition. It is guaranteed that the locations of all centers of ignition are distinct. Output Print the minimal time it takes the fire to lighten up the whole city (in minutes). Examples Input 7 7 3 1 2 2 1 5 5 Output 3 Input 10 5 1 3 3 Output 2 Note In the first example the last block can have coordinates (4, 4). In the second example the last block can have coordinates (8, 3).
instruction
0
2,165
1
4,330
Tags: binary search, data structures Correct Solution: ``` import sys from collections import Counter from operator import itemgetter from heapq import heappop, heappush n, m, k = map(int, input().split()) points = [list(map(int, line.split())) for line in sys.stdin] pts_sorted_x = sorted(points) pts_sorted_y = sorted(points, key=itemgetter(1, 0)) inf = 10**9+1 OK = (inf, inf) def solve2(imos, t): acc, cur = 0, 0 for k in sorted(imos.keys()): if t < k: break if acc <= 0 and cur+1 < k or acc + imos[k] <= 0: acc = 0 break acc += imos[k] return acc <= 0 def add_imos(imos, x, y): imos[x] += y if imos[x] == 0: del imos[x] def solve(t, px=-1, py=-1): set_x = {1, n} set_y = {1, m} for x, y in points: set_x.update((max(1, x-t), max(1, x-t-1), min(n, x+t), min(n, x+t+1))) set_y.update((max(1, y-t), max(1, y-t-1), min(m, y+t), min(m, y+t+1))) ans_x = ans_y = inf pi, imos, hq = 0, Counter(), [] if px != -1: imos[py] += 1 imos[py+t*2+1] -= 1 for cx in sorted(set_x): while hq and hq[0][0] < cx: add_imos(imos, hq[0][1], -1) add_imos(imos, hq[0][2], +1) heappop(hq) while pi < k and pts_sorted_x[pi][0]-t <= cx <= pts_sorted_x[pi][0]+t: x, y = pts_sorted_x[pi] add_imos(imos, max(1, y-t), 1) add_imos(imos, y+t+1, -1) heappush(hq, (x+t, max(1, y-t), y+t+1)) pi += 1 if solve2(imos, m): ans_x = cx break pi = 0 imos.clear() hq.clear() if px != -1: imos[px] += 1 imos[px+t*2+1] -= 1 for cy in sorted(set_y): while hq and hq[0][0] < cy: add_imos(imos, hq[0][1], -1) add_imos(imos, hq[0][2], +1) heappop(hq) while pi < k and pts_sorted_y[pi][1]-t <= cy <= pts_sorted_y[pi][1]+t: x, y = pts_sorted_y[pi] add_imos(imos, max(1, x-t), 1) add_imos(imos, x+t+1, -1) heappush(hq, (y+t, max(1, x-t), x+t+1)) pi += 1 if solve2(imos, n): ans_y = cy break return ans_x, ans_y ok, ng = 10**9+1, -1 while abs(ok - ng) > 1: mid = (ok + ng) >> 1 p = solve(mid) if p == OK: ok = mid continue if solve(mid, p[0], p[1]) == OK: ok = mid else: ng = mid print(ok) ```
output
1
2,165
1
4,331
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. The capital of Berland looks like a rectangle of size n × m of the square blocks of same size. Fire! It is known that k + 1 blocks got caught on fire (k + 1 ≤ n·m). Those blocks are centers of ignition. Moreover positions of k of these centers are known and one of these stays unknown. All k + 1 positions are distinct. The fire goes the following way: during the zero minute of fire only these k + 1 centers of ignition are burning. Every next minute the fire goes to all neighbouring blocks to the one which is burning. You can consider blocks to burn for so long that this time exceeds the time taken in the problem. The neighbouring blocks are those that touch the current block by a side or by a corner. Berland Fire Deparment wants to estimate the minimal time it takes the fire to lighten up the whole city. Remember that the positions of k blocks (centers of ignition) are known and (k + 1)-th can be positioned in any other block. Help Berland Fire Department to estimate the minimal time it takes the fire to lighten up the whole city. Input The first line contains three integers n, m and k (1 ≤ n, m ≤ 109, 1 ≤ k ≤ 500). Each of the next k lines contain two integers xi and yi (1 ≤ xi ≤ n, 1 ≤ yi ≤ m) — coordinates of the i-th center of ignition. It is guaranteed that the locations of all centers of ignition are distinct. Output Print the minimal time it takes the fire to lighten up the whole city (in minutes). Examples Input 7 7 3 1 2 2 1 5 5 Output 3 Input 10 5 1 3 3 Output 2 Note In the first example the last block can have coordinates (4, 4). In the second example the last block can have coordinates (8, 3). Submitted Solution: ``` import sys from collections import Counter n, m, k = map(int, input().split()) pts_x, pts_y = map( list, zip(*[list(map(int, line.split())) for line in sys.stdin])) inf = 10**9+1 OK = (inf, inf) def solve2(set_p, a1, a2, n, m, t): res = inf for cpos in set_p: imos = Counter() for pos1, pos2 in zip(a1, a2): if pos1-t <= cpos <= pos1+t: imos[max(1, pos2-t)] += 1 if pos2+t+1 <= m: imos[pos2+t+1] -= 1 cur, acc = 0, 0 for k in sorted(imos.keys()): if acc <= 0 and cur+1 < k or acc + imos[k] <= 0: acc = 0 break acc += imos[k] if acc <= 0: res = min(res, cpos) return res def solve(t): set_x = set() set_y = set() for x, y in zip(pts_x, pts_y): set_x.update((max(1, x-t), max(1, x-t-1), min(n, x+t), min(n, x+t+1))) set_y.update((max(1, y-t), max(1, y-t-1), min(m, y+t), min(m, y+t+1))) ans_x = solve2(set_x, pts_x, pts_y, n, m, t) ans_y = solve2(set_y, pts_y, pts_x, m, n, t) return ans_x, ans_y ok, ng = 10**9+1, -1 while abs(ok - ng) > 1: mid = (ok + ng) >> 1 p = solve(mid) if p == OK: ok = mid continue pts_x.append(min(n, p[0]+mid)) pts_y.append(min(m, p[1]+mid)) if solve(mid) == OK: ok = mid else: ng = mid pts_x.pop() pts_y.pop() print(ok) ```
instruction
0
2,166
1
4,332
No
output
1
2,166
1
4,333
Provide a correct Python 3 solution for this coding contest problem. At the end of last year, Santa Claus forgot to give Christmas presents to the children in JOI village. Therefore, I decided to deliver the chocolate cake to the children as an apology. The day to deliver is approaching tomorrow, so it's time to come up with a move plan. JOI village is divided into a grid shape by W roads extending straight in the north-south direction and H roads extending straight in the east-west direction. The W roads in the north-south direction are west. The numbers 1, 2, ..., W are numbered in order from the south, and the H roads in the east-west direction are numbered 1, 2, ..., H in order from the south. The intersection of the xth north-south road and the yth east-west road from the south is represented by (x, y). There are N houses in JOI village, and they are located at one of the intersections. Santa Claus can only move along the road. The time it takes to move between adjacent intersections is 1. Every house in JOI village has children, so Santa Claus has to deliver one chocolate cake to every house in JOI village. It's a little to fly with a reindeer with an important chocolate cake. Being dangerous, Santa Claus and Reindeer landed at one of the intersections in JOI Village, where Santa Claus decided to walk to deliver the chocolate cake. Santa Claus would not carry more than one chocolate cake at the same time. In other words, Santa Claus returns to the intersection where he landed every time he delivered a chocolate cake to a house. Santa Claus decided to choose a travel plan that would minimize the time it took to deliver the chocolate cake to all homes after landing in JOI Village. Note that the time it takes to return to the intersection after delivering the chocolate cake to the last house is not included in the time required. Also, I don't think about anything other than the time it takes to move. input Read the following input from standard input. * On the first line, the integers W and H, which represent the number of roads in each direction, are written with blanks as delimiters. * The second line contains the integer N, which represents the number of houses. * The following N lines contain information on the location of the house. On the second line of i + (1 ≤ i ≤ N), the integers Xi and Yi are written separated by blanks, indicating that the i-th house is located at the intersection (Xi, Yi). These N intersections are all different. output Output the following data to standard output. * The first line must contain one integer that represents the minimum required time. * On the second line, when the intersection to be landed to minimize the required time is (x, y), the two integers x, y must be written in this order, separated by blanks. If there are multiple suitable intersections, the westernmost one (that is, the value of x is small), and if it is still not one, the one that is the southernmost (that is, the value of y is small). ) Choose an intersection. Example Input 5 4 3 1 1 3 4 5 3 Output 10 3 3
instruction
0
2,388
1
4,776
"Correct Solution: ``` from bisect import bisect_left as bl INF = 10 ** 20 def main(): w, h = map(int, input().split()) n = int(input()) xlst = [] ylst = [] appx = xlst.append appy = ylst.append for i in range(n): x, y = map(int,input().split()) appx(x) appy(y) sorted_xlst = sorted(xlst) sorted_ylst = sorted(ylst) accx = accy = 0 cum_sum_xlst = [] cum_sum_ylst = [] appx = cum_sum_xlst.append appy = cum_sum_ylst.append for i in range(n): accx += sorted_xlst[i] accy += sorted_ylst[i] appx(accx) appy(accy) if n % 2: clx = crx = sorted_xlst[n // 2] cly = cry = sorted_ylst[n // 2] else: clx = sorted_xlst[n // 2 - 1] crx = sorted_xlst[n // 2] cly = sorted_ylst[n // 2 - 1] cry = sorted_ylst[n // 2] ans = ansx = ansy = INF for i in range(n): xi = xlst[i] yi = ylst[i] if xi <= clx: cx = crx else: cx = clx if yi <= cly: cy = cry else: cy = cly px = bl(sorted_xlst, cx) py = bl(sorted_ylst, cy) dx = xi - cx if dx < 0: dx = -dx if px: csx = cum_sum_xlst[px - 1] xlen = (accx - csx * 2 - cx * (n - px * 2)) * 2 - dx else: xlen = (accx - cx * n) * 2 - dx dy = yi - cy if dy < 0: dy = -dy if py: csy = cum_sum_ylst[py - 1] ylen = (accy - csy * 2 - cy * (n - py * 2)) * 2 - dy else: ylen = (accy - cy * n) * 2 - dy tlen = xlen + ylen if ans > tlen: ans = tlen ansx = cx ansy = cy elif ans == tlen: if ansx > cx: ansx = cx ansy = cy elif ansx == cx: if ansy > cy: ansy = cy print(ans) print(ansx, ansy) main() ```
output
1
2,388
1
4,777
Provide a correct Python 3 solution for this coding contest problem. At the end of last year, Santa Claus forgot to give Christmas presents to the children in JOI village. Therefore, I decided to deliver the chocolate cake to the children as an apology. The day to deliver is approaching tomorrow, so it's time to come up with a move plan. JOI village is divided into a grid shape by W roads extending straight in the north-south direction and H roads extending straight in the east-west direction. The W roads in the north-south direction are west. The numbers 1, 2, ..., W are numbered in order from the south, and the H roads in the east-west direction are numbered 1, 2, ..., H in order from the south. The intersection of the xth north-south road and the yth east-west road from the south is represented by (x, y). There are N houses in JOI village, and they are located at one of the intersections. Santa Claus can only move along the road. The time it takes to move between adjacent intersections is 1. Every house in JOI village has children, so Santa Claus has to deliver one chocolate cake to every house in JOI village. It's a little to fly with a reindeer with an important chocolate cake. Being dangerous, Santa Claus and Reindeer landed at one of the intersections in JOI Village, where Santa Claus decided to walk to deliver the chocolate cake. Santa Claus would not carry more than one chocolate cake at the same time. In other words, Santa Claus returns to the intersection where he landed every time he delivered a chocolate cake to a house. Santa Claus decided to choose a travel plan that would minimize the time it took to deliver the chocolate cake to all homes after landing in JOI Village. Note that the time it takes to return to the intersection after delivering the chocolate cake to the last house is not included in the time required. Also, I don't think about anything other than the time it takes to move. input Read the following input from standard input. * On the first line, the integers W and H, which represent the number of roads in each direction, are written with blanks as delimiters. * The second line contains the integer N, which represents the number of houses. * The following N lines contain information on the location of the house. On the second line of i + (1 ≤ i ≤ N), the integers Xi and Yi are written separated by blanks, indicating that the i-th house is located at the intersection (Xi, Yi). These N intersections are all different. output Output the following data to standard output. * The first line must contain one integer that represents the minimum required time. * On the second line, when the intersection to be landed to minimize the required time is (x, y), the two integers x, y must be written in this order, separated by blanks. If there are multiple suitable intersections, the westernmost one (that is, the value of x is small), and if it is still not one, the one that is the southernmost (that is, the value of y is small). ) Choose an intersection. Example Input 5 4 3 1 1 3 4 5 3 Output 10 3 3
instruction
0
2,389
1
4,778
"Correct Solution: ``` from bisect import bisect_left as bl INF = 10 ** 20 def main(): w, h = map(int, input().split()) n = int(input()) xlst = [] ylst = [] appx = xlst.append appy = ylst.append for i in range(n): x, y = map(int,input().split()) appx(x) appy(y) sorted_xlst = sorted(xlst) sorted_ylst = sorted(ylst) clx = sorted_xlst[n // 2 if n % 2 else n // 2 - 1] crx = sorted_xlst[n // 2] cly = sorted_ylst[n // 2 if n % 2 else n // 2 - 1] cry = sorted_ylst[n // 2] plx = bl(sorted_xlst, clx) prx = bl(sorted_xlst, crx) ply = bl(sorted_ylst, cly) pry = bl(sorted_ylst, cry) sumx = sum(xlst) sumy = sum(ylst) xllen = (sumx - sum(sorted_xlst[:plx]) * 2 - clx * (n - plx * 2)) * 2 xrlen = (sumx - sum(sorted_xlst[:prx]) * 2 - crx * (n - prx * 2)) * 2 yllen = (sumy - sum(sorted_ylst[:ply]) * 2 - cly * (n - ply * 2)) * 2 yrlen = (sumy - sum(sorted_ylst[:pry]) * 2 - cry * (n - pry * 2)) * 2 ans = ansx = ansy = INF max_sumd = 0 for i in range(n): xi = xlst[i] yi = ylst[i] if xi <= clx: cx = crx xlen = xrlen else: cx = clx xlen = xllen if yi <= cly: cy = cry ylen = yrlen else: cy = cly ylen = yllen dx = xi - cx if dx < 0: dx = -dx dy = yi - cy if dy < 0: dy = -dy if max_sumd > dx + dy: continue else: max_sumd = dx + dy tlen = xlen + ylen - max_sumd if ans > tlen: ans = tlen ansx = cx ansy = cy elif ans == tlen: if ansx > cx: ansx = cx ansy = cy elif ansx == cx: if ansy > cy: ansy = cy print(ans) print(ansx, ansy) main() ```
output
1
2,389
1
4,779
Provide a correct Python 3 solution for this coding contest problem. At the end of last year, Santa Claus forgot to give Christmas presents to the children in JOI village. Therefore, I decided to deliver the chocolate cake to the children as an apology. The day to deliver is approaching tomorrow, so it's time to come up with a move plan. JOI village is divided into a grid shape by W roads extending straight in the north-south direction and H roads extending straight in the east-west direction. The W roads in the north-south direction are west. The numbers 1, 2, ..., W are numbered in order from the south, and the H roads in the east-west direction are numbered 1, 2, ..., H in order from the south. The intersection of the xth north-south road and the yth east-west road from the south is represented by (x, y). There are N houses in JOI village, and they are located at one of the intersections. Santa Claus can only move along the road. The time it takes to move between adjacent intersections is 1. Every house in JOI village has children, so Santa Claus has to deliver one chocolate cake to every house in JOI village. It's a little to fly with a reindeer with an important chocolate cake. Being dangerous, Santa Claus and Reindeer landed at one of the intersections in JOI Village, where Santa Claus decided to walk to deliver the chocolate cake. Santa Claus would not carry more than one chocolate cake at the same time. In other words, Santa Claus returns to the intersection where he landed every time he delivered a chocolate cake to a house. Santa Claus decided to choose a travel plan that would minimize the time it took to deliver the chocolate cake to all homes after landing in JOI Village. Note that the time it takes to return to the intersection after delivering the chocolate cake to the last house is not included in the time required. Also, I don't think about anything other than the time it takes to move. input Read the following input from standard input. * On the first line, the integers W and H, which represent the number of roads in each direction, are written with blanks as delimiters. * The second line contains the integer N, which represents the number of houses. * The following N lines contain information on the location of the house. On the second line of i + (1 ≤ i ≤ N), the integers Xi and Yi are written separated by blanks, indicating that the i-th house is located at the intersection (Xi, Yi). These N intersections are all different. output Output the following data to standard output. * The first line must contain one integer that represents the minimum required time. * On the second line, when the intersection to be landed to minimize the required time is (x, y), the two integers x, y must be written in this order, separated by blanks. If there are multiple suitable intersections, the westernmost one (that is, the value of x is small), and if it is still not one, the one that is the southernmost (that is, the value of y is small). ) Choose an intersection. Example Input 5 4 3 1 1 3 4 5 3 Output 10 3 3
instruction
0
2,390
1
4,780
"Correct Solution: ``` from bisect import bisect_left as bl INF = 10 ** 20 def main(): w, h = map(int, input().split()) n = int(input()) xlst = [] ylst = [] appx = xlst.append appy = ylst.append for i in range(n): x, y = map(int,input().split()) appx(x) appy(y) sorted_xlst = sorted(xlst) sorted_ylst = sorted(ylst) accx = accy = 0 cum_sum_xlst = [] cum_sum_ylst = [] appx = cum_sum_xlst.append appy = cum_sum_ylst.append for i in range(n): accx += sorted_xlst[i] accy += sorted_ylst[i] appx(accx) appy(accy) if n % 2: clx = crx = sorted_xlst[n // 2] cly = cry = sorted_ylst[n // 2] else: clx = sorted_xlst[n // 2 - 1] crx = sorted_xlst[n // 2] cly = sorted_ylst[n // 2 - 1] cry = sorted_ylst[n // 2] plx = bl(sorted_xlst, clx) prx = bl(sorted_xlst, crx) ply = bl(sorted_ylst, cly) pry = bl(sorted_ylst, cry) xllen = (accx - cum_sum_xlst[plx - 1] * 2 - clx * (n - plx * 2)) * 2 if plx != 0 else (accx - clx * n) * 2 xrlen = (accx - cum_sum_xlst[prx - 1] * 2 - crx * (n - prx * 2)) * 2 if prx != 0 else (accx - crx * n) * 2 yllen = (accy - cum_sum_ylst[ply - 1] * 2 - cly * (n - ply * 2)) * 2 if ply != 0 else (accy - cly * n) * 2 yrlen = (accy - cum_sum_ylst[pry - 1] * 2 - cry * (n - pry * 2)) * 2 if pry != 0 else (accy - cry * n) * 2 ans = ansx = ansy = INF max_sumd = 0 for i in range(n): xi = xlst[i] yi = ylst[i] if xi <= clx: cx = crx xlen = xrlen else: cx = clx xlen = xllen if yi <= cly: cy = cry ylen = yrlen else: cy = cly ylen = yllen dx = xi - cx if dx < 0: dx = -dx dy = yi - cy if dy < 0: dy = -dy if max_sumd > dx + dy: continue else: max_sumd = dx + dy tlen = xlen + ylen - max_sumd if ans > tlen: ans = tlen ansx = cx ansy = cy elif ans == tlen: if ansx > cx: ansx = cx ansy = cy elif ansx == cx: if ansy > cy: ansy = cy print(ans) print(ansx, ansy) main() ```
output
1
2,390
1
4,781
Provide a correct Python 3 solution for this coding contest problem. At the end of last year, Santa Claus forgot to give Christmas presents to the children in JOI village. Therefore, I decided to deliver the chocolate cake to the children as an apology. The day to deliver is approaching tomorrow, so it's time to come up with a move plan. JOI village is divided into a grid shape by W roads extending straight in the north-south direction and H roads extending straight in the east-west direction. The W roads in the north-south direction are west. The numbers 1, 2, ..., W are numbered in order from the south, and the H roads in the east-west direction are numbered 1, 2, ..., H in order from the south. The intersection of the xth north-south road and the yth east-west road from the south is represented by (x, y). There are N houses in JOI village, and they are located at one of the intersections. Santa Claus can only move along the road. The time it takes to move between adjacent intersections is 1. Every house in JOI village has children, so Santa Claus has to deliver one chocolate cake to every house in JOI village. It's a little to fly with a reindeer with an important chocolate cake. Being dangerous, Santa Claus and Reindeer landed at one of the intersections in JOI Village, where Santa Claus decided to walk to deliver the chocolate cake. Santa Claus would not carry more than one chocolate cake at the same time. In other words, Santa Claus returns to the intersection where he landed every time he delivered a chocolate cake to a house. Santa Claus decided to choose a travel plan that would minimize the time it took to deliver the chocolate cake to all homes after landing in JOI Village. Note that the time it takes to return to the intersection after delivering the chocolate cake to the last house is not included in the time required. Also, I don't think about anything other than the time it takes to move. input Read the following input from standard input. * On the first line, the integers W and H, which represent the number of roads in each direction, are written with blanks as delimiters. * The second line contains the integer N, which represents the number of houses. * The following N lines contain information on the location of the house. On the second line of i + (1 ≤ i ≤ N), the integers Xi and Yi are written separated by blanks, indicating that the i-th house is located at the intersection (Xi, Yi). These N intersections are all different. output Output the following data to standard output. * The first line must contain one integer that represents the minimum required time. * On the second line, when the intersection to be landed to minimize the required time is (x, y), the two integers x, y must be written in this order, separated by blanks. If there are multiple suitable intersections, the westernmost one (that is, the value of x is small), and if it is still not one, the one that is the southernmost (that is, the value of y is small). ) Choose an intersection. Example Input 5 4 3 1 1 3 4 5 3 Output 10 3 3
instruction
0
2,391
1
4,782
"Correct Solution: ``` from bisect import bisect_left as bl INF = 10 ** 20 def main(): w, h = map(int, input().split()) n = int(input()) xlst = [] ylst = [] appx = xlst.append appy = ylst.append for i in range(n): x, y = map(int,input().split()) appx(x) appy(y) sorted_xlst = sorted(xlst) sorted_ylst = sorted(ylst) #x, yの中央値の候補 clx = sorted_xlst[n // 2 if n % 2 else n // 2 - 1] crx = sorted_xlst[n // 2] cly = sorted_ylst[n // 2 if n % 2 else n // 2 - 1] cry = sorted_ylst[n // 2] #x, yの中央値の候補のindex ilx = bl(sorted_xlst, clx) irx = bl(sorted_xlst, crx) ily = bl(sorted_ylst, cly) iry = bl(sorted_ylst, cry) sumx = sum(xlst) sumy = sum(ylst) #x, yそれぞれについての中央値と各点までの往復距離の総和 xllen = (sumx - sum(sorted_xlst[:ilx]) * 2 - clx * (n - ilx * 2)) * 2 xrlen = (sumx - sum(sorted_xlst[:irx]) * 2 - crx * (n - irx * 2)) * 2 yllen = (sumy - sum(sorted_ylst[:ily]) * 2 - cly * (n - ily * 2)) * 2 yrlen = (sumy - sum(sorted_ylst[:iry]) * 2 - cry * (n - iry * 2)) * 2 ans = ansx = ansy = INF max_sumd = 0 #各点について復路を省いた場合の距離の和を求める for i in range(n): xi = xlst[i] yi = ylst[i] cx, xlen = (crx, xrlen) if xi <= clx else (clx, xllen) cy, ylen = (cry, yrlen) if yi <= cly else (cly, yllen) dx = xi - cx if xi >= cx else cx - xi dy = yi - cy if yi >= cy else cy - yi if max_sumd > dx + dy: continue else: max_sumd = dx + dy tlen = xlen + ylen - max_sumd if ans > tlen: ans = tlen ansx = cx ansy = cy elif ans == tlen: if ansx > cx: ansx = cx ansy = cy elif ansx == cx: if ansy > cy: ansy = cy print(ans) print(ansx, ansy) main() ```
output
1
2,391
1
4,783
Provide a correct Python 3 solution for this coding contest problem. At the end of last year, Santa Claus forgot to give Christmas presents to the children in JOI village. Therefore, I decided to deliver the chocolate cake to the children as an apology. The day to deliver is approaching tomorrow, so it's time to come up with a move plan. JOI village is divided into a grid shape by W roads extending straight in the north-south direction and H roads extending straight in the east-west direction. The W roads in the north-south direction are west. The numbers 1, 2, ..., W are numbered in order from the south, and the H roads in the east-west direction are numbered 1, 2, ..., H in order from the south. The intersection of the xth north-south road and the yth east-west road from the south is represented by (x, y). There are N houses in JOI village, and they are located at one of the intersections. Santa Claus can only move along the road. The time it takes to move between adjacent intersections is 1. Every house in JOI village has children, so Santa Claus has to deliver one chocolate cake to every house in JOI village. It's a little to fly with a reindeer with an important chocolate cake. Being dangerous, Santa Claus and Reindeer landed at one of the intersections in JOI Village, where Santa Claus decided to walk to deliver the chocolate cake. Santa Claus would not carry more than one chocolate cake at the same time. In other words, Santa Claus returns to the intersection where he landed every time he delivered a chocolate cake to a house. Santa Claus decided to choose a travel plan that would minimize the time it took to deliver the chocolate cake to all homes after landing in JOI Village. Note that the time it takes to return to the intersection after delivering the chocolate cake to the last house is not included in the time required. Also, I don't think about anything other than the time it takes to move. input Read the following input from standard input. * On the first line, the integers W and H, which represent the number of roads in each direction, are written with blanks as delimiters. * The second line contains the integer N, which represents the number of houses. * The following N lines contain information on the location of the house. On the second line of i + (1 ≤ i ≤ N), the integers Xi and Yi are written separated by blanks, indicating that the i-th house is located at the intersection (Xi, Yi). These N intersections are all different. output Output the following data to standard output. * The first line must contain one integer that represents the minimum required time. * On the second line, when the intersection to be landed to minimize the required time is (x, y), the two integers x, y must be written in this order, separated by blanks. If there are multiple suitable intersections, the westernmost one (that is, the value of x is small), and if it is still not one, the one that is the southernmost (that is, the value of y is small). ) Choose an intersection. Example Input 5 4 3 1 1 3 4 5 3 Output 10 3 3
instruction
0
2,392
1
4,784
"Correct Solution: ``` from bisect import bisect_left as bl INF = 10 ** 20 def main(): w, h = map(int, input().split()) n = int(input()) xlst = [] ylst = [] appx = xlst.append appy = ylst.append for i in range(n): x, y = map(int,input().split()) appx(x) appy(y) sorted_xlst = sorted(xlst) sorted_ylst = sorted(ylst) accx = accy = 0 cum_sum_xlst = [] cum_sum_ylst = [] appx = cum_sum_xlst.append appy = cum_sum_ylst.append for i in range(n): accx += sorted_xlst[i] accy += sorted_ylst[i] appx(accx) appy(accy) if n % 2: clx = crx = sorted_xlst[n // 2] cly = cry = sorted_ylst[n // 2] else: clx = sorted_xlst[n // 2 - 1] crx = sorted_xlst[n // 2] cly = sorted_ylst[n // 2 - 1] cry = sorted_ylst[n // 2] plx = bl(sorted_xlst, clx) prx = bl(sorted_xlst, crx) ply = bl(sorted_ylst, cly) pry = bl(sorted_ylst, cry) ans = ansx = ansy = INF for i in range(n): xi = xlst[i] yi = ylst[i] if xi <= clx: cx = crx px = prx else: cx = clx px = plx if yi <= cly: cy = cry py = pry else: cy = cly py = ply dx = xi - cx if dx < 0: dx = -dx if px: csx = cum_sum_xlst[px - 1] xlen = (accx - csx * 2 - cx * (n - px * 2)) * 2 - dx else: xlen = (accx - cx * n) * 2 - dx dy = yi - cy if dy < 0: dy = -dy if py: csy = cum_sum_ylst[py - 1] ylen = (accy - csy * 2 - cy * (n - py * 2)) * 2 - dy else: ylen = (accy - cy * n) * 2 - dy tlen = xlen + ylen if ans > tlen: ans = tlen ansx = cx ansy = cy elif ans == tlen: if ansx > cx: ansx = cx ansy = cy elif ansx == cx: if ansy > cy: ansy = cy print(ans) print(ansx, ansy) main() ```
output
1
2,392
1
4,785
Provide a correct Python 3 solution for this coding contest problem. At the end of last year, Santa Claus forgot to give Christmas presents to the children in JOI village. Therefore, I decided to deliver the chocolate cake to the children as an apology. The day to deliver is approaching tomorrow, so it's time to come up with a move plan. JOI village is divided into a grid shape by W roads extending straight in the north-south direction and H roads extending straight in the east-west direction. The W roads in the north-south direction are west. The numbers 1, 2, ..., W are numbered in order from the south, and the H roads in the east-west direction are numbered 1, 2, ..., H in order from the south. The intersection of the xth north-south road and the yth east-west road from the south is represented by (x, y). There are N houses in JOI village, and they are located at one of the intersections. Santa Claus can only move along the road. The time it takes to move between adjacent intersections is 1. Every house in JOI village has children, so Santa Claus has to deliver one chocolate cake to every house in JOI village. It's a little to fly with a reindeer with an important chocolate cake. Being dangerous, Santa Claus and Reindeer landed at one of the intersections in JOI Village, where Santa Claus decided to walk to deliver the chocolate cake. Santa Claus would not carry more than one chocolate cake at the same time. In other words, Santa Claus returns to the intersection where he landed every time he delivered a chocolate cake to a house. Santa Claus decided to choose a travel plan that would minimize the time it took to deliver the chocolate cake to all homes after landing in JOI Village. Note that the time it takes to return to the intersection after delivering the chocolate cake to the last house is not included in the time required. Also, I don't think about anything other than the time it takes to move. input Read the following input from standard input. * On the first line, the integers W and H, which represent the number of roads in each direction, are written with blanks as delimiters. * The second line contains the integer N, which represents the number of houses. * The following N lines contain information on the location of the house. On the second line of i + (1 ≤ i ≤ N), the integers Xi and Yi are written separated by blanks, indicating that the i-th house is located at the intersection (Xi, Yi). These N intersections are all different. output Output the following data to standard output. * The first line must contain one integer that represents the minimum required time. * On the second line, when the intersection to be landed to minimize the required time is (x, y), the two integers x, y must be written in this order, separated by blanks. If there are multiple suitable intersections, the westernmost one (that is, the value of x is small), and if it is still not one, the one that is the southernmost (that is, the value of y is small). ) Choose an intersection. Example Input 5 4 3 1 1 3 4 5 3 Output 10 3 3
instruction
0
2,393
1
4,786
"Correct Solution: ``` from bisect import bisect_left as bl INF = 10 ** 20 def main(): w, h = map(int, input().split()) n = int(input()) xlst = [] ylst = [] for i in range(n): x, y = map(int,input().split()) xlst.append(x) ylst.append(y) sorted_xlst = sorted(xlst) sorted_ylst = sorted(ylst) accx = accy = 0 cum_sum_xlst = [] cum_sum_ylst = [] for i in range(n): accx += sorted_xlst[i] accy += sorted_ylst[i] cum_sum_xlst.append(accx) cum_sum_ylst.append(accy) if n % 2: clx = crx = sorted_xlst[n // 2] cly = cry = sorted_ylst[n // 2] else: clx = sorted_xlst[n // 2 - 1] crx = sorted_xlst[n // 2] cly = sorted_ylst[n // 2 - 1] cry = sorted_ylst[n // 2] ans = ansx = ansy = INF for i in range(n): xi = xlst[i] yi = ylst[i] if xi <= clx: cx = crx else: cx = clx if yi <= cly: cy = cry else: cy = cly px = bl(sorted_xlst, cx) py = bl(sorted_ylst, cy) if px: csx = cum_sum_xlst[px - 1] xlen = (cx * px - csx) * 2 + (accx - csx - cx * (n - px)) * 2 - abs(xi - cx) else: xlen = (accx - cx * n) * 2 - abs(xi - cx) if py: csy = cum_sum_ylst[py - 1] ylen = (cy * py - csy) * 2 + (accy - csy - cy * (n - py)) * 2 - abs(yi - cy) else: ylen = (accy - cy * n) * 2 - abs(yi - cy) tlen = xlen + ylen if ans > tlen: ans = tlen ansx = cx ansy = cy elif ans == tlen: if ansx > cx: ansx = cx ansy = cy elif ansx == cx: if ansy > cy: ansy = cy print(ans) print(ansx, ansy) main() ```
output
1
2,393
1
4,787
Provide a correct Python 3 solution for this coding contest problem. At the end of last year, Santa Claus forgot to give Christmas presents to the children in JOI village. Therefore, I decided to deliver the chocolate cake to the children as an apology. The day to deliver is approaching tomorrow, so it's time to come up with a move plan. JOI village is divided into a grid shape by W roads extending straight in the north-south direction and H roads extending straight in the east-west direction. The W roads in the north-south direction are west. The numbers 1, 2, ..., W are numbered in order from the south, and the H roads in the east-west direction are numbered 1, 2, ..., H in order from the south. The intersection of the xth north-south road and the yth east-west road from the south is represented by (x, y). There are N houses in JOI village, and they are located at one of the intersections. Santa Claus can only move along the road. The time it takes to move between adjacent intersections is 1. Every house in JOI village has children, so Santa Claus has to deliver one chocolate cake to every house in JOI village. It's a little to fly with a reindeer with an important chocolate cake. Being dangerous, Santa Claus and Reindeer landed at one of the intersections in JOI Village, where Santa Claus decided to walk to deliver the chocolate cake. Santa Claus would not carry more than one chocolate cake at the same time. In other words, Santa Claus returns to the intersection where he landed every time he delivered a chocolate cake to a house. Santa Claus decided to choose a travel plan that would minimize the time it took to deliver the chocolate cake to all homes after landing in JOI Village. Note that the time it takes to return to the intersection after delivering the chocolate cake to the last house is not included in the time required. Also, I don't think about anything other than the time it takes to move. input Read the following input from standard input. * On the first line, the integers W and H, which represent the number of roads in each direction, are written with blanks as delimiters. * The second line contains the integer N, which represents the number of houses. * The following N lines contain information on the location of the house. On the second line of i + (1 ≤ i ≤ N), the integers Xi and Yi are written separated by blanks, indicating that the i-th house is located at the intersection (Xi, Yi). These N intersections are all different. output Output the following data to standard output. * The first line must contain one integer that represents the minimum required time. * On the second line, when the intersection to be landed to minimize the required time is (x, y), the two integers x, y must be written in this order, separated by blanks. If there are multiple suitable intersections, the westernmost one (that is, the value of x is small), and if it is still not one, the one that is the southernmost (that is, the value of y is small). ) Choose an intersection. Example Input 5 4 3 1 1 3 4 5 3 Output 10 3 3
instruction
0
2,394
1
4,788
"Correct Solution: ``` from bisect import bisect_left as bl from bisect import bisect_right as br INF = 10 ** 20 w, h = map(int, input().split()) n = int(input()) xlst = [] ylst = [] for i in range(n): x, y = map(int,input().split()) xlst.append(x) ylst.append(y) sorted_xlst = sorted(xlst) sorted_xlst_d = sorted(xlst * 2) sorted_ylst = sorted(ylst) sorted_ylst_d = sorted(ylst * 2) accx = accy = 0 cum_sum_xlst = [] cum_sum_ylst = [] for i in range(n): accx += sorted_xlst[i] accy += sorted_ylst[i] cum_sum_xlst.append(accx) cum_sum_ylst.append(accy) clx = sorted_xlst_d[n - 1] crx = sorted_xlst_d[n] cly = sorted_ylst_d[n - 1] cry = sorted_ylst_d[n] num = n * 2 - 1 ans = INF ansx = 10 ** 10 ansy = 10 ** 10 for i in range(n): xi = xlst[i] yi = ylst[i] if xi <= clx: cx = crx else: cx = clx if yi <= cly: cy = cry else: cy = cly px = bl(sorted_xlst, cx) py = bl(sorted_ylst, cy) if px: xlen = (cx * px - cum_sum_xlst[px - 1]) * 2 + (accx - cum_sum_xlst[px - 1] - cx * (n - px)) * 2 - abs(xi - cx) else: xlen = (accx - cx * n) * 2 - abs(xi - cx) if py: ylen = (cy * py - cum_sum_ylst[py - 1]) * 2 + (accy - cum_sum_ylst[py - 1] - cy * (n - py)) * 2 - abs(yi - cy) else: ylen = (accy - cy * n) * 2 - abs(yi - cy) tlen = xlen + ylen if ans > tlen: ans = tlen ansx = cx ansy = cy elif ans == tlen: if ansx > cx: ansx = cx ansy = cy elif ansx == cx: if ansy > cy: ansy = cy print(ans) print(ansx, ansy) ```
output
1
2,394
1
4,789
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. At the end of last year, Santa Claus forgot to give Christmas presents to the children in JOI village. Therefore, I decided to deliver the chocolate cake to the children as an apology. The day to deliver is approaching tomorrow, so it's time to come up with a move plan. JOI village is divided into a grid shape by W roads extending straight in the north-south direction and H roads extending straight in the east-west direction. The W roads in the north-south direction are west. The numbers 1, 2, ..., W are numbered in order from the south, and the H roads in the east-west direction are numbered 1, 2, ..., H in order from the south. The intersection of the xth north-south road and the yth east-west road from the south is represented by (x, y). There are N houses in JOI village, and they are located at one of the intersections. Santa Claus can only move along the road. The time it takes to move between adjacent intersections is 1. Every house in JOI village has children, so Santa Claus has to deliver one chocolate cake to every house in JOI village. It's a little to fly with a reindeer with an important chocolate cake. Being dangerous, Santa Claus and Reindeer landed at one of the intersections in JOI Village, where Santa Claus decided to walk to deliver the chocolate cake. Santa Claus would not carry more than one chocolate cake at the same time. In other words, Santa Claus returns to the intersection where he landed every time he delivered a chocolate cake to a house. Santa Claus decided to choose a travel plan that would minimize the time it took to deliver the chocolate cake to all homes after landing in JOI Village. Note that the time it takes to return to the intersection after delivering the chocolate cake to the last house is not included in the time required. Also, I don't think about anything other than the time it takes to move. input Read the following input from standard input. * On the first line, the integers W and H, which represent the number of roads in each direction, are written with blanks as delimiters. * The second line contains the integer N, which represents the number of houses. * The following N lines contain information on the location of the house. On the second line of i + (1 ≤ i ≤ N), the integers Xi and Yi are written separated by blanks, indicating that the i-th house is located at the intersection (Xi, Yi). These N intersections are all different. output Output the following data to standard output. * The first line must contain one integer that represents the minimum required time. * On the second line, when the intersection to be landed to minimize the required time is (x, y), the two integers x, y must be written in this order, separated by blanks. If there are multiple suitable intersections, the westernmost one (that is, the value of x is small), and if it is still not one, the one that is the southernmost (that is, the value of y is small). ) Choose an intersection. Example Input 5 4 3 1 1 3 4 5 3 Output 10 3 3 Submitted Solution: ``` import numpy def cal_time(W, H, x, y): return 2*(abs(W-x) + abs(H-y)) geo = input().split() W = int(geo[0]) H = int(geo[1]) if W<1 or W>1000000000: print("wrong W") exit() if H<1 or H>1000000000: print("wrong H") exit() N = int(input()) if N<1 or N>100000: print("wrong N") exit() total_time = 0 ''' Input houses and calculate time''' houses = [] x_list = [] y_list = [] for i in range(N): co = input("x y: ").split() x = int(co[0]) if x<1 or x>W: print("wrong x") exit() x_list.append(x) y = int(co[1]) if y<1 or y>H: print("wrong y") exit() y_list.append(y) if (x,y) in houses: print("Coordinate exists") exit() houses.append((x,y)) #total_time += cal_time(W, H, x, y) avg_x = numpy.mean(x_list) avg_y = numpy.mean(y_list) # round target_x = int(round(avg_x)) target_y = int(round(avg_y)) #print(target_x) #print(target_y) all_time = [] for co in houses: #print(co) time = cal_time(target_x, target_y, co[0], co[1]) #print(time) all_time.append(time) total_time += time total_time -= int(max(all_time)/2) print(total_time) print(target_x, target_y) ```
instruction
0
2,395
1
4,790
No
output
1
2,395
1
4,791
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. At the end of last year, Santa Claus forgot to give Christmas presents to the children in JOI village. Therefore, I decided to deliver the chocolate cake to the children as an apology. The day to deliver is approaching tomorrow, so it's time to come up with a move plan. JOI village is divided into a grid shape by W roads extending straight in the north-south direction and H roads extending straight in the east-west direction. The W roads in the north-south direction are west. The numbers 1, 2, ..., W are numbered in order from the south, and the H roads in the east-west direction are numbered 1, 2, ..., H in order from the south. The intersection of the xth north-south road and the yth east-west road from the south is represented by (x, y). There are N houses in JOI village, and they are located at one of the intersections. Santa Claus can only move along the road. The time it takes to move between adjacent intersections is 1. Every house in JOI village has children, so Santa Claus has to deliver one chocolate cake to every house in JOI village. It's a little to fly with a reindeer with an important chocolate cake. Being dangerous, Santa Claus and Reindeer landed at one of the intersections in JOI Village, where Santa Claus decided to walk to deliver the chocolate cake. Santa Claus would not carry more than one chocolate cake at the same time. In other words, Santa Claus returns to the intersection where he landed every time he delivered a chocolate cake to a house. Santa Claus decided to choose a travel plan that would minimize the time it took to deliver the chocolate cake to all homes after landing in JOI Village. Note that the time it takes to return to the intersection after delivering the chocolate cake to the last house is not included in the time required. Also, I don't think about anything other than the time it takes to move. input Read the following input from standard input. * On the first line, the integers W and H, which represent the number of roads in each direction, are written with blanks as delimiters. * The second line contains the integer N, which represents the number of houses. * The following N lines contain information on the location of the house. On the second line of i + (1 ≤ i ≤ N), the integers Xi and Yi are written separated by blanks, indicating that the i-th house is located at the intersection (Xi, Yi). These N intersections are all different. output Output the following data to standard output. * The first line must contain one integer that represents the minimum required time. * On the second line, when the intersection to be landed to minimize the required time is (x, y), the two integers x, y must be written in this order, separated by blanks. If there are multiple suitable intersections, the westernmost one (that is, the value of x is small), and if it is still not one, the one that is the southernmost (that is, the value of y is small). ) Choose an intersection. Example Input 5 4 3 1 1 3 4 5 3 Output 10 3 3 Submitted Solution: ``` import numpy def cal_time(W, H, x, y): return 2*(abs(W-x) + abs(H-y)) geo = input("Input W H: ").split() W = int(geo[0]) H = int(geo[1]) if W<1 or W>1000000000: print("wrong W") exit() if H<1 or H>1000000000: print("wrong H") exit() N = int(input("N: ")) if N<1 or N>100000: print("wrong N") exit() total_time = 0 ''' Input houses and calculate time''' houses = [] x_list = [] y_list = [] for i in range(N): co = input("x y: ").split() x = int(co[0]) if x<1 or x>W: print("wrong x") exit() x_list.append(x) y = int(co[1]) if y<1 or y>H: print("wrong y") exit() y_list.append(y) if (x,y) in houses: print("Coordinate exists") exit() houses.append((x,y)) #total_time += cal_time(W, H, x, y) avg_x = numpy.mean(x_list) avg_y = numpy.mean(y_list) # round target_x = int(round(avg_x)) target_y = int(round(avg_y)) #print(target_x) #print(target_y) all_time = [] for co in houses: #print(co) time = cal_time(target_x, target_y, co[0], co[1]) #print(time) all_time.append(time) total_time += time total_time -= int(max(all_time)/2) print(total_time) print(target_x, target_y) ```
instruction
0
2,396
1
4,792
No
output
1
2,396
1
4,793
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. At the end of last year, Santa Claus forgot to give Christmas presents to the children in JOI village. Therefore, I decided to deliver the chocolate cake to the children as an apology. The day to deliver is approaching tomorrow, so it's time to come up with a move plan. JOI village is divided into a grid shape by W roads extending straight in the north-south direction and H roads extending straight in the east-west direction. The W roads in the north-south direction are west. The numbers 1, 2, ..., W are numbered in order from the south, and the H roads in the east-west direction are numbered 1, 2, ..., H in order from the south. The intersection of the xth north-south road and the yth east-west road from the south is represented by (x, y). There are N houses in JOI village, and they are located at one of the intersections. Santa Claus can only move along the road. The time it takes to move between adjacent intersections is 1. Every house in JOI village has children, so Santa Claus has to deliver one chocolate cake to every house in JOI village. It's a little to fly with a reindeer with an important chocolate cake. Being dangerous, Santa Claus and Reindeer landed at one of the intersections in JOI Village, where Santa Claus decided to walk to deliver the chocolate cake. Santa Claus would not carry more than one chocolate cake at the same time. In other words, Santa Claus returns to the intersection where he landed every time he delivered a chocolate cake to a house. Santa Claus decided to choose a travel plan that would minimize the time it took to deliver the chocolate cake to all homes after landing in JOI Village. Note that the time it takes to return to the intersection after delivering the chocolate cake to the last house is not included in the time required. Also, I don't think about anything other than the time it takes to move. input Read the following input from standard input. * On the first line, the integers W and H, which represent the number of roads in each direction, are written with blanks as delimiters. * The second line contains the integer N, which represents the number of houses. * The following N lines contain information on the location of the house. On the second line of i + (1 ≤ i ≤ N), the integers Xi and Yi are written separated by blanks, indicating that the i-th house is located at the intersection (Xi, Yi). These N intersections are all different. output Output the following data to standard output. * The first line must contain one integer that represents the minimum required time. * On the second line, when the intersection to be landed to minimize the required time is (x, y), the two integers x, y must be written in this order, separated by blanks. If there are multiple suitable intersections, the westernmost one (that is, the value of x is small), and if it is still not one, the one that is the southernmost (that is, the value of y is small). ) Choose an intersection. Example Input 5 4 3 1 1 3 4 5 3 Output 10 3 3 Submitted Solution: ``` from bisect import bisect_left as bl INF = 10 ** 20 w, h = map(int, input().split()) n = int(input()) xlst = [] ylst = [] for i in range(n): x, y = map(int,input().split()) xlst.append(x) ylst.append(y) sorted_xlst = sorted(xlst) sorted_ylst = sorted(ylst) accx = accy = 0 cum_sum_xlst = [] cum_sum_ylst = [] for i in range(n): accx += sorted_xlst[i] accy += sorted_ylst[i] cum_sum_xlst.append(accx) cum_sum_ylst.append(accy) #print(cum_sum_xlst) #print(cum_sum_ylst) sx = accx sy = accy num = n * 2 - 1 ans = INF ansx = 10 ** 10 ansy = 10 ** 10 def make_lenx(avx, xi): px = bl(sorted_xlst, avx) if px: lenx = (avx * px - cum_sum_xlst[px - 1]) * 2 + (sx - cum_sum_xlst[px - 1] - avx * (n - px)) * 2 - abs(xi - avx) else: lenx = (sx - avx * n) * 2 - abs(xi - avx) # print("px:",px,"avx:",avx,"lenx:",lenx) return lenx def make_leny(avy, yi): py = bl(sorted_ylst, avy) if py: leny = (avy * py - cum_sum_ylst[py - 1]) * 2 + (sy - cum_sum_ylst[py - 1] - avy * (n - py)) * 2 - abs(yi - avy) else: leny = (sy - avy * n) * 2 - abs(yi - avy) # print("py:",py,"avy:",avy,"leny:",leny) return leny for i in range(n): xi = xlst[i] yi = ylst[i] sumx = sx * 2 - xi avx1 = sumx // num - 1 avx2 = avx1 + 1 avx3 = avx2 + 1 sumy = sy * 2 - yi avy1 = sumy // num - 1 avy2 = avy1 + 1 avy3 = avy2 + 1 lenx1, lenx2, lenx3 = make_lenx(avx1, xi), make_lenx(avx2, xi), make_lenx(avx3, xi) leny1, leny2, leny3 = make_leny(avy1, yi), make_leny(avy2, yi), make_leny(avy3, yi) lenx = min(lenx1, lenx2, lenx3) leny = min(leny1, leny2, leny3) if lenx == lenx1: avx = avx1 elif lenx == lenx2: avx = avx2 else: avx = avx3 if leny == leny1: avy = avy1 elif leny == leny2: avy = avy2 else: avy = avy3 # print(lenx, leny, avx, avy) lent = lenx + leny if lent <= ans: if lent == ans: if avx < ansx: ansx = avx ansy = avy elif avx == ansx: if avy < ansy: ansy = avy else: ansx = avx ansy = avy ans = lent print(ans) print(ansx, ansy) ```
instruction
0
2,397
1
4,794
No
output
1
2,397
1
4,795
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. At the end of last year, Santa Claus forgot to give Christmas presents to the children in JOI village. Therefore, I decided to deliver the chocolate cake to the children as an apology. The day to deliver is approaching tomorrow, so it's time to come up with a move plan. JOI village is divided into a grid shape by W roads extending straight in the north-south direction and H roads extending straight in the east-west direction. The W roads in the north-south direction are west. The numbers 1, 2, ..., W are numbered in order from the south, and the H roads in the east-west direction are numbered 1, 2, ..., H in order from the south. The intersection of the xth north-south road and the yth east-west road from the south is represented by (x, y). There are N houses in JOI village, and they are located at one of the intersections. Santa Claus can only move along the road. The time it takes to move between adjacent intersections is 1. Every house in JOI village has children, so Santa Claus has to deliver one chocolate cake to every house in JOI village. It's a little to fly with a reindeer with an important chocolate cake. Being dangerous, Santa Claus and Reindeer landed at one of the intersections in JOI Village, where Santa Claus decided to walk to deliver the chocolate cake. Santa Claus would not carry more than one chocolate cake at the same time. In other words, Santa Claus returns to the intersection where he landed every time he delivered a chocolate cake to a house. Santa Claus decided to choose a travel plan that would minimize the time it took to deliver the chocolate cake to all homes after landing in JOI Village. Note that the time it takes to return to the intersection after delivering the chocolate cake to the last house is not included in the time required. Also, I don't think about anything other than the time it takes to move. input Read the following input from standard input. * On the first line, the integers W and H, which represent the number of roads in each direction, are written with blanks as delimiters. * The second line contains the integer N, which represents the number of houses. * The following N lines contain information on the location of the house. On the second line of i + (1 ≤ i ≤ N), the integers Xi and Yi are written separated by blanks, indicating that the i-th house is located at the intersection (Xi, Yi). These N intersections are all different. output Output the following data to standard output. * The first line must contain one integer that represents the minimum required time. * On the second line, when the intersection to be landed to minimize the required time is (x, y), the two integers x, y must be written in this order, separated by blanks. If there are multiple suitable intersections, the westernmost one (that is, the value of x is small), and if it is still not one, the one that is the southernmost (that is, the value of y is small). ) Choose an intersection. Example Input 5 4 3 1 1 3 4 5 3 Output 10 3 3 Submitted Solution: ``` import numpy def cal_time(W, H, x, y): return 2*(abs(W-x) + abs(H-y)) geo = input().split() W = int(geo[0]) H = int(geo[1]) if W<1 or W>1000000000: print("wrong W") exit() if H<1 or H>1000000000: print("wrong H") exit() N = int(input()) if N<1 or N>100000: print("wrong N") exit() total_time = 0 ''' Input houses and calculate time''' houses = [] x_list = [] y_list = [] for i in range(N): co = input().split() x = int(co[0]) if x<1 or x>W: print("wrong x") exit() x_list.append(x) y = int(co[1]) if y<1 or y>H: print("wrong y") exit() y_list.append(y) if (x,y) in houses: print("Coordinate exists") exit() houses.append((x,y)) #total_time += cal_time(W, H, x, y) avg_x = numpy.mean(x_list) avg_y = numpy.mean(y_list) # round target_x = int(round(avg_x)) target_y = int(round(avg_y)) #print(target_x) #print(target_y) all_time = [] for co in houses: #print(co) time = cal_time(target_x, target_y, co[0], co[1]) #print(time) all_time.append(time) total_time += time total_time -= int(max(all_time)/2) print(total_time) print(target_x, target_y) ```
instruction
0
2,398
1
4,796
No
output
1
2,398
1
4,797
Provide tags and a correct Python 3 solution for this coding contest problem. Xenia lives in a city that has n houses built along the main ringroad. The ringroad houses are numbered 1 through n in the clockwise order. The ringroad traffic is one way and also is clockwise. Xenia has recently moved into the ringroad house number 1. As a result, she's got m things to do. In order to complete the i-th task, she needs to be in the house number ai and complete all tasks with numbers less than i. Initially, Xenia is in the house number 1, find the minimum time she needs to complete all her tasks if moving from a house to a neighboring one along the ringroad takes one unit of time. Input The first line contains two integers n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ 105). The second line contains m integers a1, a2, ..., am (1 ≤ ai ≤ n). Note that Xenia can have multiple consecutive tasks in one house. Output Print a single integer — the time Xenia needs to complete all tasks. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 4 3 3 2 3 Output 6 Input 4 3 2 3 3 Output 2 Note In the first test example the sequence of Xenia's moves along the ringroad looks as follows: 1 → 2 → 3 → 4 → 1 → 2 → 3. This is optimal sequence. So, she needs 6 time units.
instruction
0
2,775
1
5,550
Tags: implementation Correct Solution: ``` n,m=map(int,input().split()) arr=[int(i) for i in input().split()] p=1 ans=0 for i in range(m): if arr[i]<p: ans+=n-(p-arr[i]) p=arr[i] elif arr[i]>p: ans+=arr[i]-p p=arr[i] print(ans) ```
output
1
2,775
1
5,551
Provide tags and a correct Python 3 solution for this coding contest problem. Xenia lives in a city that has n houses built along the main ringroad. The ringroad houses are numbered 1 through n in the clockwise order. The ringroad traffic is one way and also is clockwise. Xenia has recently moved into the ringroad house number 1. As a result, she's got m things to do. In order to complete the i-th task, she needs to be in the house number ai and complete all tasks with numbers less than i. Initially, Xenia is in the house number 1, find the minimum time she needs to complete all her tasks if moving from a house to a neighboring one along the ringroad takes one unit of time. Input The first line contains two integers n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ 105). The second line contains m integers a1, a2, ..., am (1 ≤ ai ≤ n). Note that Xenia can have multiple consecutive tasks in one house. Output Print a single integer — the time Xenia needs to complete all tasks. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 4 3 3 2 3 Output 6 Input 4 3 2 3 3 Output 2 Note In the first test example the sequence of Xenia's moves along the ringroad looks as follows: 1 → 2 → 3 → 4 → 1 → 2 → 3. This is optimal sequence. So, she needs 6 time units.
instruction
0
2,776
1
5,552
Tags: implementation Correct Solution: ``` while(1): try: n,m=map(int,input().split()) a=list(map(int,input().split())) tt=a[0]-1 for i in range(1,len(a)): if a[i]<a[i-1]: tt+=n-a[i-1]+a[i] else: tt+=a[i]-a[i-1] print(tt) except EOFError: break ```
output
1
2,776
1
5,553
Provide tags and a correct Python 3 solution for this coding contest problem. Xenia lives in a city that has n houses built along the main ringroad. The ringroad houses are numbered 1 through n in the clockwise order. The ringroad traffic is one way and also is clockwise. Xenia has recently moved into the ringroad house number 1. As a result, she's got m things to do. In order to complete the i-th task, she needs to be in the house number ai and complete all tasks with numbers less than i. Initially, Xenia is in the house number 1, find the minimum time she needs to complete all her tasks if moving from a house to a neighboring one along the ringroad takes one unit of time. Input The first line contains two integers n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ 105). The second line contains m integers a1, a2, ..., am (1 ≤ ai ≤ n). Note that Xenia can have multiple consecutive tasks in one house. Output Print a single integer — the time Xenia needs to complete all tasks. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 4 3 3 2 3 Output 6 Input 4 3 2 3 3 Output 2 Note In the first test example the sequence of Xenia's moves along the ringroad looks as follows: 1 → 2 → 3 → 4 → 1 → 2 → 3. This is optimal sequence. So, she needs 6 time units.
instruction
0
2,777
1
5,554
Tags: implementation Correct Solution: ``` m,n = list(map(int, input().split())) na = list(map(int, input().split())) a = [1] a.extend(na) ans = 0 for i in range(len(a)-1): if a[i+1] - a[i] >=0: ans += a[i+1] - a[i] else: ans += a[i+1]+m - a[i] print(ans) ```
output
1
2,777
1
5,555
Provide tags and a correct Python 3 solution for this coding contest problem. Xenia lives in a city that has n houses built along the main ringroad. The ringroad houses are numbered 1 through n in the clockwise order. The ringroad traffic is one way and also is clockwise. Xenia has recently moved into the ringroad house number 1. As a result, she's got m things to do. In order to complete the i-th task, she needs to be in the house number ai and complete all tasks with numbers less than i. Initially, Xenia is in the house number 1, find the minimum time she needs to complete all her tasks if moving from a house to a neighboring one along the ringroad takes one unit of time. Input The first line contains two integers n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ 105). The second line contains m integers a1, a2, ..., am (1 ≤ ai ≤ n). Note that Xenia can have multiple consecutive tasks in one house. Output Print a single integer — the time Xenia needs to complete all tasks. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 4 3 3 2 3 Output 6 Input 4 3 2 3 3 Output 2 Note In the first test example the sequence of Xenia's moves along the ringroad looks as follows: 1 → 2 → 3 → 4 → 1 → 2 → 3. This is optimal sequence. So, she needs 6 time units.
instruction
0
2,778
1
5,556
Tags: implementation Correct Solution: ``` n,m=map(int,input().split()) a=list(map(int,input().split())) k=0 for i in range(1,len(a)): if (a[i]<a[i-1]): k+=n c=k-1+a[-1]%n if (a[-1]%n==0): print(c+n) else: print(c) ```
output
1
2,778
1
5,557
Provide tags and a correct Python 3 solution for this coding contest problem. Xenia lives in a city that has n houses built along the main ringroad. The ringroad houses are numbered 1 through n in the clockwise order. The ringroad traffic is one way and also is clockwise. Xenia has recently moved into the ringroad house number 1. As a result, she's got m things to do. In order to complete the i-th task, she needs to be in the house number ai and complete all tasks with numbers less than i. Initially, Xenia is in the house number 1, find the minimum time she needs to complete all her tasks if moving from a house to a neighboring one along the ringroad takes one unit of time. Input The first line contains two integers n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ 105). The second line contains m integers a1, a2, ..., am (1 ≤ ai ≤ n). Note that Xenia can have multiple consecutive tasks in one house. Output Print a single integer — the time Xenia needs to complete all tasks. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 4 3 3 2 3 Output 6 Input 4 3 2 3 3 Output 2 Note In the first test example the sequence of Xenia's moves along the ringroad looks as follows: 1 → 2 → 3 → 4 → 1 → 2 → 3. This is optimal sequence. So, she needs 6 time units.
instruction
0
2,779
1
5,558
Tags: implementation Correct Solution: ``` R = lambda: list(map(int, input().split())) n, m = R() a = R() d = lambda i, j: (n - i + j) % n print(a[0] - 1 + sum(d(a[i], a[i + 1]) for i in range(m - 1))) ```
output
1
2,779
1
5,559
Provide tags and a correct Python 3 solution for this coding contest problem. Xenia lives in a city that has n houses built along the main ringroad. The ringroad houses are numbered 1 through n in the clockwise order. The ringroad traffic is one way and also is clockwise. Xenia has recently moved into the ringroad house number 1. As a result, she's got m things to do. In order to complete the i-th task, she needs to be in the house number ai and complete all tasks with numbers less than i. Initially, Xenia is in the house number 1, find the minimum time she needs to complete all her tasks if moving from a house to a neighboring one along the ringroad takes one unit of time. Input The first line contains two integers n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ 105). The second line contains m integers a1, a2, ..., am (1 ≤ ai ≤ n). Note that Xenia can have multiple consecutive tasks in one house. Output Print a single integer — the time Xenia needs to complete all tasks. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 4 3 3 2 3 Output 6 Input 4 3 2 3 3 Output 2 Note In the first test example the sequence of Xenia's moves along the ringroad looks as follows: 1 → 2 → 3 → 4 → 1 → 2 → 3. This is optimal sequence. So, she needs 6 time units.
instruction
0
2,780
1
5,560
Tags: implementation Correct Solution: ``` n,m=list(map(int,input().split())) a=list(map(int,input().split())) last=1 out=0 for i in a: if i>=last: out+=i-last else: out+=i+n-last last=i print(out) ```
output
1
2,780
1
5,561
Provide tags and a correct Python 3 solution for this coding contest problem. Xenia lives in a city that has n houses built along the main ringroad. The ringroad houses are numbered 1 through n in the clockwise order. The ringroad traffic is one way and also is clockwise. Xenia has recently moved into the ringroad house number 1. As a result, she's got m things to do. In order to complete the i-th task, she needs to be in the house number ai and complete all tasks with numbers less than i. Initially, Xenia is in the house number 1, find the minimum time she needs to complete all her tasks if moving from a house to a neighboring one along the ringroad takes one unit of time. Input The first line contains two integers n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ 105). The second line contains m integers a1, a2, ..., am (1 ≤ ai ≤ n). Note that Xenia can have multiple consecutive tasks in one house. Output Print a single integer — the time Xenia needs to complete all tasks. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 4 3 3 2 3 Output 6 Input 4 3 2 3 3 Output 2 Note In the first test example the sequence of Xenia's moves along the ringroad looks as follows: 1 → 2 → 3 → 4 → 1 → 2 → 3. This is optimal sequence. So, she needs 6 time units.
instruction
0
2,781
1
5,562
Tags: implementation Correct Solution: ``` a,b=map(int,input().split()) c=list(map(int,input().split())) k=int(0) x=int(1) while x<b: if c[x]-c[x-1]>=0: k=k+c[x]-c[x-1] else: k=k+a-abs(c[x]-c[x-1]) x=x+1 print(k+c[0]-1) ```
output
1
2,781
1
5,563
Provide tags and a correct Python 3 solution for this coding contest problem. Xenia lives in a city that has n houses built along the main ringroad. The ringroad houses are numbered 1 through n in the clockwise order. The ringroad traffic is one way and also is clockwise. Xenia has recently moved into the ringroad house number 1. As a result, she's got m things to do. In order to complete the i-th task, she needs to be in the house number ai and complete all tasks with numbers less than i. Initially, Xenia is in the house number 1, find the minimum time she needs to complete all her tasks if moving from a house to a neighboring one along the ringroad takes one unit of time. Input The first line contains two integers n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ 105). The second line contains m integers a1, a2, ..., am (1 ≤ ai ≤ n). Note that Xenia can have multiple consecutive tasks in one house. Output Print a single integer — the time Xenia needs to complete all tasks. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 4 3 3 2 3 Output 6 Input 4 3 2 3 3 Output 2 Note In the first test example the sequence of Xenia's moves along the ringroad looks as follows: 1 → 2 → 3 → 4 → 1 → 2 → 3. This is optimal sequence. So, she needs 6 time units.
instruction
0
2,782
1
5,564
Tags: implementation Correct Solution: ``` n, m = map(int, input().split()) ans, loc = 0, 1 homes = list(map(int, input().split())) for dest in homes: if(dest >= loc): ans += dest - loc else: ans += n - loc + dest loc = dest print(ans) ```
output
1
2,782
1
5,565