message
stringlengths
2
433k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
113
108k
cluster
float64
12
12
__index_level_0__
int64
226
217k
Provide a correct Python 3 solution for this coding contest problem. You are given a permutation p = (p_1, \ldots, p_N) of \\{ 1, \ldots, N \\}. You can perform the following two kinds of operations repeatedly in any order: * Pay a cost A. Choose integers l and r (1 \leq l < r \leq N), and shift (p_l, \ldots, p_r) to the left by one. That is, replace p_l, p_{l + 1}, \ldots, p_{r - 1}, p_r with p_{l + 1}, p_{l + 2}, \ldots, p_r, p_l, respectively. * Pay a cost B. Choose integers l and r (1 \leq l < r \leq N), and shift (p_l, \ldots, p_r) to the right by one. That is, replace p_l, p_{l + 1}, \ldots, p_{r - 1}, p_r with p_r, p_l, \ldots, p_{r - 2}, p_{r - 1}, respectively. Find the minimum total cost required to sort p in ascending order. Constraints * All values in input are integers. * 1 \leq N \leq 5000 * 1 \leq A, B \leq 10^9 * (p_1 \ldots, p_N) is a permutation of \\{ 1, \ldots, N \\}. Input Input is given from Standard Input in the following format: N A B p_1 \cdots p_N Output Print the minimum total cost required to sort p in ascending order. Examples Input 3 20 30 3 1 2 Output 20 Input 4 20 30 4 2 3 1 Output 50 Input 1 10 10 1 Output 0 Input 4 1000000000 1000000000 4 3 2 1 Output 3000000000 Input 9 40 50 5 3 4 7 6 1 2 9 8 Output 220
instruction
0
8,235
12
16,470
"Correct Solution: ``` import sys input=sys.stdin.readline N,A,B=map(int,input().split()) P=list(map(int,input().split())) data=[0]*(N+1) flag=[0]*(N+1) for i in range(N): p=P[i] flag[p]=1 cnt=0 for j in range(p+1,N+1): if flag[j]==0: data[j]=data[j-1] else: cnt+=1 data[j]=min(cnt*A+data[p-1],data[j]+B) print(data[-1]) ```
output
1
8,235
12
16,471
Provide a correct Python 3 solution for this coding contest problem. You are given a permutation p = (p_1, \ldots, p_N) of \\{ 1, \ldots, N \\}. You can perform the following two kinds of operations repeatedly in any order: * Pay a cost A. Choose integers l and r (1 \leq l < r \leq N), and shift (p_l, \ldots, p_r) to the left by one. That is, replace p_l, p_{l + 1}, \ldots, p_{r - 1}, p_r with p_{l + 1}, p_{l + 2}, \ldots, p_r, p_l, respectively. * Pay a cost B. Choose integers l and r (1 \leq l < r \leq N), and shift (p_l, \ldots, p_r) to the right by one. That is, replace p_l, p_{l + 1}, \ldots, p_{r - 1}, p_r with p_r, p_l, \ldots, p_{r - 2}, p_{r - 1}, respectively. Find the minimum total cost required to sort p in ascending order. Constraints * All values in input are integers. * 1 \leq N \leq 5000 * 1 \leq A, B \leq 10^9 * (p_1 \ldots, p_N) is a permutation of \\{ 1, \ldots, N \\}. Input Input is given from Standard Input in the following format: N A B p_1 \cdots p_N Output Print the minimum total cost required to sort p in ascending order. Examples Input 3 20 30 3 1 2 Output 20 Input 4 20 30 4 2 3 1 Output 50 Input 1 10 10 1 Output 0 Input 4 1000000000 1000000000 4 3 2 1 Output 3000000000 Input 9 40 50 5 3 4 7 6 1 2 9 8 Output 220
instruction
0
8,236
12
16,472
"Correct Solution: ``` n, a, b = map(int, input().split()) p = list(map(int, input().split())) INF = 10**13 dp = [[INF for _ in range(n+1)] for _ in range(n+1)] dp[0][0] = 0 for i in range(n): for j in range(n+1): if j < p[i]: dp[i+1][p[i]] = min(dp[i+1][p[i]], dp[i][j]) dp[i+1][j] = min(dp[i+1][j], dp[i][j] + a) else: dp[i+1][j] = min(dp[i+1][j], dp[i][j] + b) print(min(dp[n])) ```
output
1
8,236
12
16,473
Provide a correct Python 3 solution for this coding contest problem. You are given a permutation p = (p_1, \ldots, p_N) of \\{ 1, \ldots, N \\}. You can perform the following two kinds of operations repeatedly in any order: * Pay a cost A. Choose integers l and r (1 \leq l < r \leq N), and shift (p_l, \ldots, p_r) to the left by one. That is, replace p_l, p_{l + 1}, \ldots, p_{r - 1}, p_r with p_{l + 1}, p_{l + 2}, \ldots, p_r, p_l, respectively. * Pay a cost B. Choose integers l and r (1 \leq l < r \leq N), and shift (p_l, \ldots, p_r) to the right by one. That is, replace p_l, p_{l + 1}, \ldots, p_{r - 1}, p_r with p_r, p_l, \ldots, p_{r - 2}, p_{r - 1}, respectively. Find the minimum total cost required to sort p in ascending order. Constraints * All values in input are integers. * 1 \leq N \leq 5000 * 1 \leq A, B \leq 10^9 * (p_1 \ldots, p_N) is a permutation of \\{ 1, \ldots, N \\}. Input Input is given from Standard Input in the following format: N A B p_1 \cdots p_N Output Print the minimum total cost required to sort p in ascending order. Examples Input 3 20 30 3 1 2 Output 20 Input 4 20 30 4 2 3 1 Output 50 Input 1 10 10 1 Output 0 Input 4 1000000000 1000000000 4 3 2 1 Output 3000000000 Input 9 40 50 5 3 4 7 6 1 2 9 8 Output 220
instruction
0
8,237
12
16,474
"Correct Solution: ``` from bisect import bisect n, a, b = map(int, input().split()) ppp = map(int, input().split()) qqq = [0] * n for i, p in enumerate(ppp, start=1): qqq[p - 1] = i dp = [(0, 0)] for i in qqq: s = bisect(dp, (i,)) ndp = [(j, cost + b) for j, cost in dp[:s]] stay_cost = dp[s - 1][1] ndp.append((i, stay_cost)) remain = iter(dp[s:]) for j, cost in remain: if stay_cost > cost + a: ndp.append((j, cost + a)) break ndp.extend((j, cost + a) for j, cost in remain) dp = ndp print(dp[-1][1]) ```
output
1
8,237
12
16,475
Provide a correct Python 3 solution for this coding contest problem. You are given a permutation p = (p_1, \ldots, p_N) of \\{ 1, \ldots, N \\}. You can perform the following two kinds of operations repeatedly in any order: * Pay a cost A. Choose integers l and r (1 \leq l < r \leq N), and shift (p_l, \ldots, p_r) to the left by one. That is, replace p_l, p_{l + 1}, \ldots, p_{r - 1}, p_r with p_{l + 1}, p_{l + 2}, \ldots, p_r, p_l, respectively. * Pay a cost B. Choose integers l and r (1 \leq l < r \leq N), and shift (p_l, \ldots, p_r) to the right by one. That is, replace p_l, p_{l + 1}, \ldots, p_{r - 1}, p_r with p_r, p_l, \ldots, p_{r - 2}, p_{r - 1}, respectively. Find the minimum total cost required to sort p in ascending order. Constraints * All values in input are integers. * 1 \leq N \leq 5000 * 1 \leq A, B \leq 10^9 * (p_1 \ldots, p_N) is a permutation of \\{ 1, \ldots, N \\}. Input Input is given from Standard Input in the following format: N A B p_1 \cdots p_N Output Print the minimum total cost required to sort p in ascending order. Examples Input 3 20 30 3 1 2 Output 20 Input 4 20 30 4 2 3 1 Output 50 Input 1 10 10 1 Output 0 Input 4 1000000000 1000000000 4 3 2 1 Output 3000000000 Input 9 40 50 5 3 4 7 6 1 2 9 8 Output 220
instruction
0
8,238
12
16,476
"Correct Solution: ``` def main(): import sys input = sys.stdin.readline N, R, L = map(int, input().split()) P = list(map(int, input().split())) val2idx = [0] * (N+1) for i, p in enumerate(P): val2idx[p] = i + 1 bigger = [[0] * (N+1) for _ in range(N+1)] for p in range(1, N+1): for j in range(N): if P[j] > p: bigger[p][j+1] = bigger[p][j] + 1 else: bigger[p][j + 1] = bigger[p][j] #print(bigger) inf = 10**14 dp = [[inf] * (N+1) for _ in range(N+1)] dp[0][0] = 0 for p in range(1, N+1): i = val2idx[p] for j in range(N+1): if i > j: dp[p][j] = min(dp[p][j], dp[p-1][j] + L) dp[p][i] = min(dp[p][i], dp[p-1][j] + R * (bigger[p][i] - bigger[p][j])) else: dp[p][j] = min(dp[p][j], dp[p - 1][j]) print(min(dp[-1])) #[print(i, dp[i]) for i in range(N+1)] if __name__ == '__main__': main() ```
output
1
8,238
12
16,477
Provide a correct Python 3 solution for this coding contest problem. You are given a permutation p = (p_1, \ldots, p_N) of \\{ 1, \ldots, N \\}. You can perform the following two kinds of operations repeatedly in any order: * Pay a cost A. Choose integers l and r (1 \leq l < r \leq N), and shift (p_l, \ldots, p_r) to the left by one. That is, replace p_l, p_{l + 1}, \ldots, p_{r - 1}, p_r with p_{l + 1}, p_{l + 2}, \ldots, p_r, p_l, respectively. * Pay a cost B. Choose integers l and r (1 \leq l < r \leq N), and shift (p_l, \ldots, p_r) to the right by one. That is, replace p_l, p_{l + 1}, \ldots, p_{r - 1}, p_r with p_r, p_l, \ldots, p_{r - 2}, p_{r - 1}, respectively. Find the minimum total cost required to sort p in ascending order. Constraints * All values in input are integers. * 1 \leq N \leq 5000 * 1 \leq A, B \leq 10^9 * (p_1 \ldots, p_N) is a permutation of \\{ 1, \ldots, N \\}. Input Input is given from Standard Input in the following format: N A B p_1 \cdots p_N Output Print the minimum total cost required to sort p in ascending order. Examples Input 3 20 30 3 1 2 Output 20 Input 4 20 30 4 2 3 1 Output 50 Input 1 10 10 1 Output 0 Input 4 1000000000 1000000000 4 3 2 1 Output 3000000000 Input 9 40 50 5 3 4 7 6 1 2 9 8 Output 220
instruction
0
8,239
12
16,478
"Correct Solution: ``` n, a, b = map(int, input().split()) dp = [0] + [2 ** 60] * n p = list(map(int, input().split())) for i in range(n): dp[i + 1] = dp[0] dp[0] += a for j in range(i): if p[j] < p[i]: dp[i + 1] = min(dp[i + 1], dp[j + 1]) dp[j + 1] += [b, a][p[j] < p[i]] print(min(dp)) ```
output
1
8,239
12
16,479
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a permutation p = (p_1, \ldots, p_N) of \\{ 1, \ldots, N \\}. You can perform the following two kinds of operations repeatedly in any order: * Pay a cost A. Choose integers l and r (1 \leq l < r \leq N), and shift (p_l, \ldots, p_r) to the left by one. That is, replace p_l, p_{l + 1}, \ldots, p_{r - 1}, p_r with p_{l + 1}, p_{l + 2}, \ldots, p_r, p_l, respectively. * Pay a cost B. Choose integers l and r (1 \leq l < r \leq N), and shift (p_l, \ldots, p_r) to the right by one. That is, replace p_l, p_{l + 1}, \ldots, p_{r - 1}, p_r with p_r, p_l, \ldots, p_{r - 2}, p_{r - 1}, respectively. Find the minimum total cost required to sort p in ascending order. Constraints * All values in input are integers. * 1 \leq N \leq 5000 * 1 \leq A, B \leq 10^9 * (p_1 \ldots, p_N) is a permutation of \\{ 1, \ldots, N \\}. Input Input is given from Standard Input in the following format: N A B p_1 \cdots p_N Output Print the minimum total cost required to sort p in ascending order. Examples Input 3 20 30 3 1 2 Output 20 Input 4 20 30 4 2 3 1 Output 50 Input 1 10 10 1 Output 0 Input 4 1000000000 1000000000 4 3 2 1 Output 3000000000 Input 9 40 50 5 3 4 7 6 1 2 9 8 Output 220 Submitted Solution: ``` N,A,B=map(int,input().split()) p=list(map(int,input().split())) p=[p[i]-1 for i in range(N)] dp=[[0 for i in range(N+1)] for j in range(N)] for j in range(N+1): if p[0]<=j: dp[0][j]=0 else: dp[0][j]=A for i in range(1,N): for j in range(N+1): if p[i]<=j: dp[i][j]=min(dp[i-1][p[i]],dp[i-1][j]+B) else: dp[i][j]=dp[i-1][j]+A print(dp[-1][N]) ```
instruction
0
8,240
12
16,480
Yes
output
1
8,240
12
16,481
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a permutation p = (p_1, \ldots, p_N) of \\{ 1, \ldots, N \\}. You can perform the following two kinds of operations repeatedly in any order: * Pay a cost A. Choose integers l and r (1 \leq l < r \leq N), and shift (p_l, \ldots, p_r) to the left by one. That is, replace p_l, p_{l + 1}, \ldots, p_{r - 1}, p_r with p_{l + 1}, p_{l + 2}, \ldots, p_r, p_l, respectively. * Pay a cost B. Choose integers l and r (1 \leq l < r \leq N), and shift (p_l, \ldots, p_r) to the right by one. That is, replace p_l, p_{l + 1}, \ldots, p_{r - 1}, p_r with p_r, p_l, \ldots, p_{r - 2}, p_{r - 1}, respectively. Find the minimum total cost required to sort p in ascending order. Constraints * All values in input are integers. * 1 \leq N \leq 5000 * 1 \leq A, B \leq 10^9 * (p_1 \ldots, p_N) is a permutation of \\{ 1, \ldots, N \\}. Input Input is given from Standard Input in the following format: N A B p_1 \cdots p_N Output Print the minimum total cost required to sort p in ascending order. Examples Input 3 20 30 3 1 2 Output 20 Input 4 20 30 4 2 3 1 Output 50 Input 1 10 10 1 Output 0 Input 4 1000000000 1000000000 4 3 2 1 Output 3000000000 Input 9 40 50 5 3 4 7 6 1 2 9 8 Output 220 Submitted Solution: ``` N,A,B=map(int,input().split()) P=[int(i) for i in input().split()] s=1e19 d=[s]*(N+1) d[0]=0 for i in range(N): e=[s]*(N+1) for j in range(N+1): if j < P[i]: e[P[i]]=min(e[P[i]],d[j]) e[j]=min(e[j],d[j]+A) else: e[j]=min(e[j],d[j]+B) d=e print(min(d)) ```
instruction
0
8,241
12
16,482
Yes
output
1
8,241
12
16,483
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a permutation p = (p_1, \ldots, p_N) of \\{ 1, \ldots, N \\}. You can perform the following two kinds of operations repeatedly in any order: * Pay a cost A. Choose integers l and r (1 \leq l < r \leq N), and shift (p_l, \ldots, p_r) to the left by one. That is, replace p_l, p_{l + 1}, \ldots, p_{r - 1}, p_r with p_{l + 1}, p_{l + 2}, \ldots, p_r, p_l, respectively. * Pay a cost B. Choose integers l and r (1 \leq l < r \leq N), and shift (p_l, \ldots, p_r) to the right by one. That is, replace p_l, p_{l + 1}, \ldots, p_{r - 1}, p_r with p_r, p_l, \ldots, p_{r - 2}, p_{r - 1}, respectively. Find the minimum total cost required to sort p in ascending order. Constraints * All values in input are integers. * 1 \leq N \leq 5000 * 1 \leq A, B \leq 10^9 * (p_1 \ldots, p_N) is a permutation of \\{ 1, \ldots, N \\}. Input Input is given from Standard Input in the following format: N A B p_1 \cdots p_N Output Print the minimum total cost required to sort p in ascending order. Examples Input 3 20 30 3 1 2 Output 20 Input 4 20 30 4 2 3 1 Output 50 Input 1 10 10 1 Output 0 Input 4 1000000000 1000000000 4 3 2 1 Output 3000000000 Input 9 40 50 5 3 4 7 6 1 2 9 8 Output 220 Submitted Solution: ``` """ Writer: SPD_9X2 https://atcoder.jp/contests/agc032/tasks/agc032_d 圧倒的挿入ソート https://atcoder.jp/contests/abc006/tasks/abc006_4 これの、左右のコストが違うVer 最長部分増加列を計算すれば、A=Bの時の答えは分かる その時の答えは, A * (N - 最長部分増加列の長さ) 残りのカードが昇順になるように抜き出し、適切な場所に戻す。 最長部分列として採用する or しないで dp? dp[i][部分増加列の最後の数字] = i番目まで見た時のコストの最小値 かな? 増加列の最後に足せば、コストはかからない 足さないとき、最後の数字より大きければ、A払う 小さければ、B払う O(N**2)で解けた? """ N,A,B = map(int,input().split()) p = list(map(int,input().split())) dp = [float("inf")] * (N+1) dp[0] = 0 for i in range(N): ndp = [float("inf")] * (N+1) for j in range(N+1): if p[i] >= j: #p[i]よりjがおおきいので、Aコスト & 更新ができる ndp[j] = min(ndp[j] , dp[j] + A) ndp[p[i]] = min(ndp[p[i]] , dp[j]) else: #Bコストでの更新 ndp[j] = min(ndp[j] , dp[j] + B) dp = ndp print (min(dp)) ```
instruction
0
8,242
12
16,484
Yes
output
1
8,242
12
16,485
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a permutation p = (p_1, \ldots, p_N) of \\{ 1, \ldots, N \\}. You can perform the following two kinds of operations repeatedly in any order: * Pay a cost A. Choose integers l and r (1 \leq l < r \leq N), and shift (p_l, \ldots, p_r) to the left by one. That is, replace p_l, p_{l + 1}, \ldots, p_{r - 1}, p_r with p_{l + 1}, p_{l + 2}, \ldots, p_r, p_l, respectively. * Pay a cost B. Choose integers l and r (1 \leq l < r \leq N), and shift (p_l, \ldots, p_r) to the right by one. That is, replace p_l, p_{l + 1}, \ldots, p_{r - 1}, p_r with p_r, p_l, \ldots, p_{r - 2}, p_{r - 1}, respectively. Find the minimum total cost required to sort p in ascending order. Constraints * All values in input are integers. * 1 \leq N \leq 5000 * 1 \leq A, B \leq 10^9 * (p_1 \ldots, p_N) is a permutation of \\{ 1, \ldots, N \\}. Input Input is given from Standard Input in the following format: N A B p_1 \cdots p_N Output Print the minimum total cost required to sort p in ascending order. Examples Input 3 20 30 3 1 2 Output 20 Input 4 20 30 4 2 3 1 Output 50 Input 1 10 10 1 Output 0 Input 4 1000000000 1000000000 4 3 2 1 Output 3000000000 Input 9 40 50 5 3 4 7 6 1 2 9 8 Output 220 Submitted Solution: ``` from bisect import bisect from operator import itemgetter n, a, b = map(int, input().split()) ppp = map(int, input().split()) qqq = [i for i, p in sorted(enumerate(ppp, start=1), key=itemgetter(1))] INF = 10 ** 15 dp = [(0, 0)] for i in qqq: ndp = [] s = bisect(dp, (i,)) tmp_min = INF for j, cost in dp[:s]: cost += b ndp.append((j, cost)) tmp_min = cost stay_cost = dp[s - 1][1] if tmp_min > stay_cost: ndp.append((i, stay_cost)) tmp_min = stay_cost for j, cost in dp[s:]: cost += a if tmp_min > cost: ndp.append((j, cost)) tmp_min = cost dp = ndp print(dp[-1][1]) ```
instruction
0
8,243
12
16,486
Yes
output
1
8,243
12
16,487
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a permutation p = (p_1, \ldots, p_N) of \\{ 1, \ldots, N \\}. You can perform the following two kinds of operations repeatedly in any order: * Pay a cost A. Choose integers l and r (1 \leq l < r \leq N), and shift (p_l, \ldots, p_r) to the left by one. That is, replace p_l, p_{l + 1}, \ldots, p_{r - 1}, p_r with p_{l + 1}, p_{l + 2}, \ldots, p_r, p_l, respectively. * Pay a cost B. Choose integers l and r (1 \leq l < r \leq N), and shift (p_l, \ldots, p_r) to the right by one. That is, replace p_l, p_{l + 1}, \ldots, p_{r - 1}, p_r with p_r, p_l, \ldots, p_{r - 2}, p_{r - 1}, respectively. Find the minimum total cost required to sort p in ascending order. Constraints * All values in input are integers. * 1 \leq N \leq 5000 * 1 \leq A, B \leq 10^9 * (p_1 \ldots, p_N) is a permutation of \\{ 1, \ldots, N \\}. Input Input is given from Standard Input in the following format: N A B p_1 \cdots p_N Output Print the minimum total cost required to sort p in ascending order. Examples Input 3 20 30 3 1 2 Output 20 Input 4 20 30 4 2 3 1 Output 50 Input 1 10 10 1 Output 0 Input 4 1000000000 1000000000 4 3 2 1 Output 3000000000 Input 9 40 50 5 3 4 7 6 1 2 9 8 Output 220 Submitted Solution: ``` from collections import deque class Dinic: def __init__(self, n): self.n = n self.links = [[] for _ in range(n)] self.depth = None self.progress = None def add_link(self, _from, to, cap): self.links[_from].append([cap, to, len(self.links[to])]) self.links[to].append([0, _from, len(self.links[_from]) - 1]) def bfs(self, s): depth = [-1] * self.n depth[s] = 0 q = deque([s]) while q: v = q.popleft() for cap, to, rev in self.links[v]: if cap > 0 and depth[to] < 0: depth[to] = depth[v] + 1 q.append(to) self.depth = depth def dfs(self, v, t, flow): if v == t: return flow links_v = self.links[v] for i in range(self.progress[v], len(links_v)): self.progress[v] = i cap, to, rev = link = links_v[i] if cap == 0 or self.depth[v] >= self.depth[to]: continue d = self.dfs(to, t, min(flow, cap)) if d == 0: continue link[0] -= d self.links[to][rev][0] += d return d return 0 def max_flow(self, s, t): flow = 0 while True: self.bfs(s) if self.depth[t] < 0: return flow self.progress = [0] * self.n current_flow = self.dfs(s, t, float('inf')) while current_flow > 0: flow += current_flow current_flow = self.dfs(s, t, float('inf')) n, a, b = map(int, input().split()) aaa = list(map(int, input().split())) mf = Dinic(2 * n + 2) t = 2 * n + 1 for i in range(1, n + 1): mf.add_link(0, i, b) mf.add_link(n + i, t, a) INF = 10 ** 18 completed = [False] * (n + 1) for i in range(n): r = aaa[i] for l in reversed(aaa[:i]): if l > r: mf.add_link(r, l + n, INF) if l + 1 == r: mf.add_link(r, l, INF) break print('Time for Construct Graph') # Test exit() print(mf.max_flow(0, t)) ```
instruction
0
8,244
12
16,488
No
output
1
8,244
12
16,489
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a permutation p = (p_1, \ldots, p_N) of \\{ 1, \ldots, N \\}. You can perform the following two kinds of operations repeatedly in any order: * Pay a cost A. Choose integers l and r (1 \leq l < r \leq N), and shift (p_l, \ldots, p_r) to the left by one. That is, replace p_l, p_{l + 1}, \ldots, p_{r - 1}, p_r with p_{l + 1}, p_{l + 2}, \ldots, p_r, p_l, respectively. * Pay a cost B. Choose integers l and r (1 \leq l < r \leq N), and shift (p_l, \ldots, p_r) to the right by one. That is, replace p_l, p_{l + 1}, \ldots, p_{r - 1}, p_r with p_r, p_l, \ldots, p_{r - 2}, p_{r - 1}, respectively. Find the minimum total cost required to sort p in ascending order. Constraints * All values in input are integers. * 1 \leq N \leq 5000 * 1 \leq A, B \leq 10^9 * (p_1 \ldots, p_N) is a permutation of \\{ 1, \ldots, N \\}. Input Input is given from Standard Input in the following format: N A B p_1 \cdots p_N Output Print the minimum total cost required to sort p in ascending order. Examples Input 3 20 30 3 1 2 Output 20 Input 4 20 30 4 2 3 1 Output 50 Input 1 10 10 1 Output 0 Input 4 1000000000 1000000000 4 3 2 1 Output 3000000000 Input 9 40 50 5 3 4 7 6 1 2 9 8 Output 220 Submitted Solution: ``` import sys sys.setrecursionlimit(10000) n, a, b = map(int, input().split()) p = list(map(int, input().split())) p.append(n+1) p.insert(0,0) #''' #print (p) INF = (a+b)*n min_ans =INF # 仮のmin値 # l-shift で 右端を整列する def ptr_l_shift(p, cost, l, r): for i in range(l, r+1): if p[i] == r : if r == i: return (0 + solve(p, cost, l, r-1)) # l_shift(l, r) p = p[0:i] +p[i+1:r+1] +p[i:i+1] +p[r+1:] ret = solve(p, cost, l, r-1) p = p[0:i] +p[r:r+1] +p[i:r] +p[r+1:] return (a + ret) # print("error l_shift", p,l,r) # r-shift で 左端を整列する def ptr_r_shift(p, cost, l, r) : for i in range(l, r+1): if p[i] == l : if i == l : return (0 + solve(p, cost, l+1, r)) # r_shift(i, j) p = p[0:l] +p[i:i+1] +p[l:i] +p[i+1:] ret = solve(p, cost, l+1, r) p = p[0:l] +p[l+1:i+1] +p[l:l+1] +p[i+1:] return (b + ret) # print("error r_shift", p,l,r) def solve(p, cost, ptr_l, ptr_r): global min_ans if cost >= min_ans : return (INF) if ptr_l == ptr_r : return (0) if a < b : # コストの小さい方を優先する ll = ptr_l_shift(p, cost, ptr_l, ptr_r) rr = ptr_r_shift(p, cost, ptr_l, ptr_r) else: rr = ptr_r_shift(p, cost, ptr_l, ptr_r) ll = ptr_l_shift(p, cost, ptr_l, ptr_r) # print ("return ll, rr =", ll, rr) if ll > rr : if rr < min_ans : min_ans = rr return (rr) else : if ll < min_ans : min_ans = ll return (ll) cost = 0 print (solve(p, cost, 1, n)) ```
instruction
0
8,245
12
16,490
No
output
1
8,245
12
16,491
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a permutation p = (p_1, \ldots, p_N) of \\{ 1, \ldots, N \\}. You can perform the following two kinds of operations repeatedly in any order: * Pay a cost A. Choose integers l and r (1 \leq l < r \leq N), and shift (p_l, \ldots, p_r) to the left by one. That is, replace p_l, p_{l + 1}, \ldots, p_{r - 1}, p_r with p_{l + 1}, p_{l + 2}, \ldots, p_r, p_l, respectively. * Pay a cost B. Choose integers l and r (1 \leq l < r \leq N), and shift (p_l, \ldots, p_r) to the right by one. That is, replace p_l, p_{l + 1}, \ldots, p_{r - 1}, p_r with p_r, p_l, \ldots, p_{r - 2}, p_{r - 1}, respectively. Find the minimum total cost required to sort p in ascending order. Constraints * All values in input are integers. * 1 \leq N \leq 5000 * 1 \leq A, B \leq 10^9 * (p_1 \ldots, p_N) is a permutation of \\{ 1, \ldots, N \\}. Input Input is given from Standard Input in the following format: N A B p_1 \cdots p_N Output Print the minimum total cost required to sort p in ascending order. Examples Input 3 20 30 3 1 2 Output 20 Input 4 20 30 4 2 3 1 Output 50 Input 1 10 10 1 Output 0 Input 4 1000000000 1000000000 4 3 2 1 Output 3000000000 Input 9 40 50 5 3 4 7 6 1 2 9 8 Output 220 Submitted Solution: ``` n, a, b = map(int, input().split()) p = list(map(int, input().split())) q = [0] * n for i, x in enumerate(p): q[x - 1] = i dp = [[0] * (n + 1) for _ in range(n + 1)] mi = [[float('inf')] * (n + 1) for _ in range(n + 1)] ma = [[0] * (n + 1) for _ in range(n + 1)] for i in range(n + 1): for j in range(i + 1, n + 1): mi[i][j] = min(mi[i][j - 1], q[j - 1]) ma[i][j] = max(ma[i][j - 1], q[j - 1]) for i in range(1, n + 1): for j in range(i + 1): k = i - j s, t = 1, 1 if j > 0 and j == p[mi[j - 1][n - k]]: s = 0 if k > 0 and n - k + 1 == p[ma[j][n - k + 1]]: t = 0 if j == 0: dp[j][k] = dp[j][k - 1] + a * t elif k == 0: dp[j][k] = dp[j - 1][k] + b * s else: dp[j][k] = min(dp[j - 1][k] + b * s, dp[j][k - 1] + a * t) ans = float('inf') for i in range(n + 1): ans = min(ans, dp[i][n - i]) print(ans) ```
instruction
0
8,246
12
16,492
No
output
1
8,246
12
16,493
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a permutation p = (p_1, \ldots, p_N) of \\{ 1, \ldots, N \\}. You can perform the following two kinds of operations repeatedly in any order: * Pay a cost A. Choose integers l and r (1 \leq l < r \leq N), and shift (p_l, \ldots, p_r) to the left by one. That is, replace p_l, p_{l + 1}, \ldots, p_{r - 1}, p_r with p_{l + 1}, p_{l + 2}, \ldots, p_r, p_l, respectively. * Pay a cost B. Choose integers l and r (1 \leq l < r \leq N), and shift (p_l, \ldots, p_r) to the right by one. That is, replace p_l, p_{l + 1}, \ldots, p_{r - 1}, p_r with p_r, p_l, \ldots, p_{r - 2}, p_{r - 1}, respectively. Find the minimum total cost required to sort p in ascending order. Constraints * All values in input are integers. * 1 \leq N \leq 5000 * 1 \leq A, B \leq 10^9 * (p_1 \ldots, p_N) is a permutation of \\{ 1, \ldots, N \\}. Input Input is given from Standard Input in the following format: N A B p_1 \cdots p_N Output Print the minimum total cost required to sort p in ascending order. Examples Input 3 20 30 3 1 2 Output 20 Input 4 20 30 4 2 3 1 Output 50 Input 1 10 10 1 Output 0 Input 4 1000000000 1000000000 4 3 2 1 Output 3000000000 Input 9 40 50 5 3 4 7 6 1 2 9 8 Output 220 Submitted Solution: ``` # -*- coding: utf-8 -*- """ Created on Sat Feb 16 20:52:46 2019 @author: Owner """ import collections import scipy.misc import sys import numpy as np import math from operator import itemgetter import itertools import copy import bisect #素因数を並べる def prime_decomposition(n): i = 2 table = [] while i * i <= n: while n % i == 0: n /= i table.append(int(i)) i += 1 if n > 1: table.append(int(n)) return table # 桁数を吐く def digit(i): if i > 0: return digit(i//10) + [i%10] else: return [] def getNearestValueIndex(list, num): """ 概要: リストからある値に最も近い値のインデックスを取得する関数 @param list: データ配列 @param num: 対象値 @return 対象値に最も近い値 """ # リスト要素と対象値の差分を計算し最小値のインデックスを取得 idx = np.abs(np.asarray(list) - num).argmin() return idx def find_index(l, x, default=False): if x in l: return l.index(x) else: return default """ N, X = map(int, input().split()) x = [0]*N x[:] = map(int, input().split()) P = [0]*N Y = [0]*N for n in range(N): P[n], Y[n] = map(int, input().split()) all(nstr.count(c) for c in '753') # 複数配列を並び替え ABT = zip(A, B, totAB) result = 0 # itemgetterには何番目の配列をキーにしたいか渡します sorted(ABT,key=itemgetter(2)) A, B, totAB = zip(*ABT) A.sort(reverse=True) # 2進数のbit判定 (x >> i) & 1 # dp最小化問題 dp = [np.inf]*N for n in range(N): if n == 0: dp[n] = 0 else: for k in range(1,K+1): if n-k >= 0: dp[n] = min(dp[n], dp[n-k] + abs(h[n]-h[n-k])) else: break """ N, A, B = map(int, input().split()) p = [0]*N p[:] = map(int, input().split()) p_end = find_index(p, 1) cost = 0 for i in range(N-1): n = i+2 i_idx = find_index(p,n) tmp_p = p[i_idx] if p_end == i_idx-1: p_end += 1 elif p_end > i_idx: cost += A p[i_idx:p_end] = p[i_idx+1:p_end+1] p[p_end] = tmp_p else: cost += B p[p_end+2:i_idx+1] = p[p_end+1:i_idx] p[p_end+1] = tmp_p p_end += 1 print(n, p, p_end, i_idx) print(cost) ```
instruction
0
8,247
12
16,494
No
output
1
8,247
12
16,495
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n arrays a_1, a_2, ..., a_n; each array consists of exactly m integers. We denote the y-th element of the x-th array as a_{x, y}. You have to choose two arrays a_i and a_j (1 ≤ i, j ≤ n, it is possible that i = j). After that, you will obtain a new array b consisting of m integers, such that for every k ∈ [1, m] b_k = max(a_{i, k}, a_{j, k}). Your goal is to choose i and j so that the value of min _{k = 1}^{m} b_k is maximum possible. Input The first line contains two integers n and m (1 ≤ n ≤ 3 ⋅ 10^5, 1 ≤ m ≤ 8) — the number of arrays and the number of elements in each array, respectively. Then n lines follow, the x-th line contains the array a_x represented by m integers a_{x, 1}, a_{x, 2}, ..., a_{x, m} (0 ≤ a_{x, y} ≤ 10^9). Output Print two integers i and j (1 ≤ i, j ≤ n, it is possible that i = j) — the indices of the two arrays you have to choose so that the value of min _{k = 1}^{m} b_k is maximum possible. If there are multiple answers, print any of them. Example Input 6 5 5 0 3 1 2 1 8 9 1 3 1 2 3 4 5 9 1 0 3 7 2 3 0 6 3 6 4 1 7 0 Output 1 5
instruction
0
8,478
12
16,956
Tags: binary search, bitmasks, dp Correct Solution: ``` from collections import defaultdict from sys import stdin input = stdin.readline def check(mid, m): d = defaultdict(int) for idx, i in enumerate(a): string = '' for j in i: if j >= mid: string+='1' else: string+='0' d[int(string, 2)] = idx for i in d.keys(): for j in d.keys(): if i|j == 2**m - 1: return [d[i], d[j]] return [] def binarySearch(lo, hi, m): ans = [] while lo < hi: mid = lo + (hi-lo+1)//2 x = check(mid, m) if x: lo = mid ans = [x[0]+1, x[1]+1] else: hi = mid-1 return ans n, m = map(int, input().split()) a = [] for i in range(n): a.append(list(map(int, input().split()))) print(*binarySearch(-1, 10**9+1, m)) ```
output
1
8,478
12
16,957
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n arrays a_1, a_2, ..., a_n; each array consists of exactly m integers. We denote the y-th element of the x-th array as a_{x, y}. You have to choose two arrays a_i and a_j (1 ≤ i, j ≤ n, it is possible that i = j). After that, you will obtain a new array b consisting of m integers, such that for every k ∈ [1, m] b_k = max(a_{i, k}, a_{j, k}). Your goal is to choose i and j so that the value of min _{k = 1}^{m} b_k is maximum possible. Input The first line contains two integers n and m (1 ≤ n ≤ 3 ⋅ 10^5, 1 ≤ m ≤ 8) — the number of arrays and the number of elements in each array, respectively. Then n lines follow, the x-th line contains the array a_x represented by m integers a_{x, 1}, a_{x, 2}, ..., a_{x, m} (0 ≤ a_{x, y} ≤ 10^9). Output Print two integers i and j (1 ≤ i, j ≤ n, it is possible that i = j) — the indices of the two arrays you have to choose so that the value of min _{k = 1}^{m} b_k is maximum possible. If there are multiple answers, print any of them. Example Input 6 5 5 0 3 1 2 1 8 9 1 3 1 2 3 4 5 9 1 0 3 7 2 3 0 6 3 6 4 1 7 0 Output 1 5
instruction
0
8,479
12
16,958
Tags: binary search, bitmasks, dp Correct Solution: ``` # by the authority of GOD author: manhar singh sachdev # import os,sys from io import BytesIO, IOBase def check(mid,arr,m,n): ls = [[] for _ in range(1<<m)] for i in range(n): ans = 0 for j in range(m): if arr[i][j] >= mid: ans += 1<<j ls[ans].append(i+1) for i in range(len(ls)): for j in range(len(ls)): if len(ls[i]) and len(ls[j]) and i|j == (1<<m)-1: return ls[i][0],ls[j][0] return 0 def main(): n,m = map(int,input().split()) arr = [list(map(int,input().split())) for _ in range(n)] hi,lo,ind1 = 10**9,0,(1,1) while hi >= lo: mid = (hi+lo)//2 ind = check(mid,arr,m,n) if ind: ind1 = ind lo = mid+1 else: hi = mid-1 print(*ind1) #Fast IO Region BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") if __name__ == '__main__': main() ```
output
1
8,479
12
16,959
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n arrays a_1, a_2, ..., a_n; each array consists of exactly m integers. We denote the y-th element of the x-th array as a_{x, y}. You have to choose two arrays a_i and a_j (1 ≤ i, j ≤ n, it is possible that i = j). After that, you will obtain a new array b consisting of m integers, such that for every k ∈ [1, m] b_k = max(a_{i, k}, a_{j, k}). Your goal is to choose i and j so that the value of min _{k = 1}^{m} b_k is maximum possible. Input The first line contains two integers n and m (1 ≤ n ≤ 3 ⋅ 10^5, 1 ≤ m ≤ 8) — the number of arrays and the number of elements in each array, respectively. Then n lines follow, the x-th line contains the array a_x represented by m integers a_{x, 1}, a_{x, 2}, ..., a_{x, m} (0 ≤ a_{x, y} ≤ 10^9). Output Print two integers i and j (1 ≤ i, j ≤ n, it is possible that i = j) — the indices of the two arrays you have to choose so that the value of min _{k = 1}^{m} b_k is maximum possible. If there are multiple answers, print any of them. Example Input 6 5 5 0 3 1 2 1 8 9 1 3 1 2 3 4 5 9 1 0 3 7 2 3 0 6 3 6 4 1 7 0 Output 1 5
instruction
0
8,480
12
16,960
Tags: binary search, bitmasks, dp Correct Solution: ``` MAX=10**9 #O((n*m+(2**m)**2)*log(MAX)) def main(): n,m=readIntArr() arrs=[] for _ in range(n): arrs.append(readIntArr()) def checkPossible(minB): binRepresentations=set() for arr in arrs: binRepresentations.add(convertToBinary(arr,minB)) binList=list(binRepresentations) ii=jj=-1 n=len(binList) for i in range(n): for j in range(i,n): if binList[i]|binList[j]==(1<<m)-1: #ok ii=binList[i] jj=binList[j] if ii!=-1: #ok ansi=ansj=-1 for i in range(len(arrs)): b=convertToBinary(arrs[i],minB) if b==ii: ansi=i if b==jj: ansj=i # print('ii:{} jj:{}'.format(ii,jj)) # print('ok minB:{} ansi:{} ansj:{}'.format(minB,ansi,ansj)) return (ansi,ansj) else: return None def convertToBinary(arr,minB): b=0 for i in range(m): if arr[i]>=minB: b|=(1<<i) return b minB=-1 i=j=-1 b=MAX while b>0: temp=checkPossible(minB+b) if temp==None: #cannot b//=2 else: #can minB+=b i,j=temp i+=1;j+=1 print('{} {}'.format(i,j)) return import sys input=sys.stdin.buffer.readline #FOR READING PURE INTEGER INPUTS (space separation ok) # input=lambda: sys.stdin.readline().rstrip("\r\n") #FOR READING STRING/TEXT INPUTS. def oneLineArrayPrint(arr): print(' '.join([str(x) for x in arr])) def multiLineArrayPrint(arr): print('\n'.join([str(x) for x in arr])) def multiLineArrayOfArraysPrint(arr): print('\n'.join([' '.join([str(x) for x in y]) for y in arr])) def readIntArr(): return [int(x) for x in input().split()] # def readFloatArr(): # return [float(x) for x in input().split()] def makeArr(*args): """ *args : (default value, dimension 1, dimension 2,...) Returns : arr[dim1][dim2]... filled with default value """ assert len(args) >= 2, "makeArr args should be (default value, dimension 1, dimension 2,..." if len(args) == 2: return [args[0] for _ in range(args[1])] else: return [makeArr(args[0],*args[2:]) for _ in range(args[1])] def queryInteractive(x,y): print('? {} {}'.format(x,y)) sys.stdout.flush() return int(input()) def answerInteractive(ans): print('! {}'.format(ans)) sys.stdout.flush() inf=float('inf') MOD=10**9+7 for _abc in range(1): main() ```
output
1
8,480
12
16,961
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n arrays a_1, a_2, ..., a_n; each array consists of exactly m integers. We denote the y-th element of the x-th array as a_{x, y}. You have to choose two arrays a_i and a_j (1 ≤ i, j ≤ n, it is possible that i = j). After that, you will obtain a new array b consisting of m integers, such that for every k ∈ [1, m] b_k = max(a_{i, k}, a_{j, k}). Your goal is to choose i and j so that the value of min _{k = 1}^{m} b_k is maximum possible. Input The first line contains two integers n and m (1 ≤ n ≤ 3 ⋅ 10^5, 1 ≤ m ≤ 8) — the number of arrays and the number of elements in each array, respectively. Then n lines follow, the x-th line contains the array a_x represented by m integers a_{x, 1}, a_{x, 2}, ..., a_{x, m} (0 ≤ a_{x, y} ≤ 10^9). Output Print two integers i and j (1 ≤ i, j ≤ n, it is possible that i = j) — the indices of the two arrays you have to choose so that the value of min _{k = 1}^{m} b_k is maximum possible. If there are multiple answers, print any of them. Example Input 6 5 5 0 3 1 2 1 8 9 1 3 1 2 3 4 5 9 1 0 3 7 2 3 0 6 3 6 4 1 7 0 Output 1 5
instruction
0
8,481
12
16,962
Tags: binary search, bitmasks, dp Correct Solution: ``` hell=1000000007 id1=0 id2=0 a = [] def check(n,m,x): global id1,id2 b = [0]*(1<<m) idx = [0]*(1<<m) for i in range(n): mask=0 for j in range(m): if a[i][j]>=x: mask=mask^(1<<j) b[mask]=1 idx[mask]=i+1 for i in range(1<<m): if b[i]: for j in range(1<<m): if b[j]: mask=i|j if mask==((1<<m)-1): id1=idx[i] id2=idx[j] return 1 return 0 def meowmeow321(): n,m = map(int,input().split()) for i in range(n): dog = [int(x) for x in input().split()] a.append(dog) lo=0 hi=hell while hi-lo>0: mid=(hi+lo+1)//2 if check(n,m,mid): lo=mid else: hi=mid-1 check(n,m,lo) print(id1,id2) t=1 #t=int(input()) for xxx in range(t): meowmeow321() ```
output
1
8,481
12
16,963
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n arrays a_1, a_2, ..., a_n; each array consists of exactly m integers. We denote the y-th element of the x-th array as a_{x, y}. You have to choose two arrays a_i and a_j (1 ≤ i, j ≤ n, it is possible that i = j). After that, you will obtain a new array b consisting of m integers, such that for every k ∈ [1, m] b_k = max(a_{i, k}, a_{j, k}). Your goal is to choose i and j so that the value of min _{k = 1}^{m} b_k is maximum possible. Input The first line contains two integers n and m (1 ≤ n ≤ 3 ⋅ 10^5, 1 ≤ m ≤ 8) — the number of arrays and the number of elements in each array, respectively. Then n lines follow, the x-th line contains the array a_x represented by m integers a_{x, 1}, a_{x, 2}, ..., a_{x, m} (0 ≤ a_{x, y} ≤ 10^9). Output Print two integers i and j (1 ≤ i, j ≤ n, it is possible that i = j) — the indices of the two arrays you have to choose so that the value of min _{k = 1}^{m} b_k is maximum possible. If there are multiple answers, print any of them. Example Input 6 5 5 0 3 1 2 1 8 9 1 3 1 2 3 4 5 9 1 0 3 7 2 3 0 6 3 6 4 1 7 0 Output 1 5
instruction
0
8,482
12
16,964
Tags: binary search, bitmasks, dp Correct Solution: ``` #!/usr/bin/env python3 import sys input = sys.stdin.readline max_val = 0 n, m = [int(item) for item in input().split()] array = [] for i in range(n): line = [int(item) for item in input().split()] array.append(line) max_val = max(max_val, max(line)) good = (1 << m) - 1 l = 0; r = max_val + 1 a = 0; b = 0 while r - l > 1: mid = (l + r) // 2 bit_array = dict() for k, line in enumerate(array): val = 0 for i, item in enumerate(line): if item >= mid: val |= 1 << i bit_array[val] = k ok = False for key1 in bit_array.keys(): for key2 in bit_array.keys(): if key1 | key2 == good: ok = True i = bit_array[key1] j = bit_array[key2] break if ok: a = i; b = j l = mid else: r = mid print(a+1, b+1) ```
output
1
8,482
12
16,965
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n arrays a_1, a_2, ..., a_n; each array consists of exactly m integers. We denote the y-th element of the x-th array as a_{x, y}. You have to choose two arrays a_i and a_j (1 ≤ i, j ≤ n, it is possible that i = j). After that, you will obtain a new array b consisting of m integers, such that for every k ∈ [1, m] b_k = max(a_{i, k}, a_{j, k}). Your goal is to choose i and j so that the value of min _{k = 1}^{m} b_k is maximum possible. Input The first line contains two integers n and m (1 ≤ n ≤ 3 ⋅ 10^5, 1 ≤ m ≤ 8) — the number of arrays and the number of elements in each array, respectively. Then n lines follow, the x-th line contains the array a_x represented by m integers a_{x, 1}, a_{x, 2}, ..., a_{x, m} (0 ≤ a_{x, y} ≤ 10^9). Output Print two integers i and j (1 ≤ i, j ≤ n, it is possible that i = j) — the indices of the two arrays you have to choose so that the value of min _{k = 1}^{m} b_k is maximum possible. If there are multiple answers, print any of them. Example Input 6 5 5 0 3 1 2 1 8 9 1 3 1 2 3 4 5 9 1 0 3 7 2 3 0 6 3 6 4 1 7 0 Output 1 5
instruction
0
8,483
12
16,966
Tags: binary search, bitmasks, dp Correct Solution: ``` # ---------------------------iye ha aam zindegi--------------------------------------------- import math import random import heapq, bisect import sys from collections import deque, defaultdict from fractions import Fraction import sys import threading from collections import defaultdict #threading.stack_size(10**8) mod = 10 ** 9 + 7 mod1 = 998244353 # ------------------------------warmup---------------------------- import os import sys from io import BytesIO, IOBase #sys.setrecursionlimit(300000) BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # -------------------game starts now----------------------------------------------------import math class TreeNode: def __init__(self, k, v): self.key = k self.value = v self.left = None self.right = None self.parent = None self.height = 1 self.num_left = 1 self.num_total = 1 class AvlTree: def __init__(self): self._tree = None def add(self, k, v): if not self._tree: self._tree = TreeNode(k, v) return node = self._add(k, v) if node: self._rebalance(node) def _add(self, k, v): node = self._tree while node: if k < node.key: if node.left: node = node.left else: node.left = TreeNode(k, v) node.left.parent = node return node.left elif node.key < k: if node.right: node = node.right else: node.right = TreeNode(k, v) node.right.parent = node return node.right else: node.value = v return @staticmethod def get_height(x): return x.height if x else 0 @staticmethod def get_num_total(x): return x.num_total if x else 0 def _rebalance(self, node): n = node while n: lh = self.get_height(n.left) rh = self.get_height(n.right) n.height = max(lh, rh) + 1 balance_factor = lh - rh n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right) n.num_left = 1 + self.get_num_total(n.left) if balance_factor > 1: if self.get_height(n.left.left) < self.get_height(n.left.right): self._rotate_left(n.left) self._rotate_right(n) elif balance_factor < -1: if self.get_height(n.right.right) < self.get_height(n.right.left): self._rotate_right(n.right) self._rotate_left(n) else: n = n.parent def _remove_one(self, node): """ Side effect!!! Changes node. Node should have exactly one child """ replacement = node.left or node.right if node.parent: if AvlTree._is_left(node): node.parent.left = replacement else: node.parent.right = replacement replacement.parent = node.parent node.parent = None else: self._tree = replacement replacement.parent = None node.left = None node.right = None node.parent = None self._rebalance(replacement) def _remove_leaf(self, node): if node.parent: if AvlTree._is_left(node): node.parent.left = None else: node.parent.right = None self._rebalance(node.parent) else: self._tree = None node.parent = None node.left = None node.right = None def remove(self, k): node = self._get_node(k) if not node: return if AvlTree._is_leaf(node): self._remove_leaf(node) return if node.left and node.right: nxt = AvlTree._get_next(node) node.key = nxt.key node.value = nxt.value if self._is_leaf(nxt): self._remove_leaf(nxt) else: self._remove_one(nxt) self._rebalance(node) else: self._remove_one(node) def get(self, k): node = self._get_node(k) return node.value if node else -1 def _get_node(self, k): if not self._tree: return None node = self._tree while node: if k < node.key: node = node.left elif node.key < k: node = node.right else: return node return None def get_at(self, pos): x = pos + 1 node = self._tree while node: if x < node.num_left: node = node.left elif node.num_left < x: x -= node.num_left node = node.right else: return (node.key, node.value) raise IndexError("Out of ranges") @staticmethod def _is_left(node): return node.parent.left and node.parent.left == node @staticmethod def _is_leaf(node): return node.left is None and node.right is None def _rotate_right(self, node): if not node.parent: self._tree = node.left node.left.parent = None elif AvlTree._is_left(node): node.parent.left = node.left node.left.parent = node.parent else: node.parent.right = node.left node.left.parent = node.parent bk = node.left.right node.left.right = node node.parent = node.left node.left = bk if bk: bk.parent = node node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1 node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right) node.num_left = 1 + self.get_num_total(node.left) def _rotate_left(self, node): if not node.parent: self._tree = node.right node.right.parent = None elif AvlTree._is_left(node): node.parent.left = node.right node.right.parent = node.parent else: node.parent.right = node.right node.right.parent = node.parent bk = node.right.left node.right.left = node node.parent = node.right node.right = bk if bk: bk.parent = node node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1 node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right) node.num_left = 1 + self.get_num_total(node.left) @staticmethod def _get_next(node): if not node.right: return node.parent n = node.right while n.left: n = n.left return n # -----------------------------------------------binary seacrh tree--------------------------------------- class SegmentTree1: def __init__(self, data, default=2**30, func=lambda a, b: min(a , b)): """initialize the segment tree with data""" self._default = default self._func = func self._len = len(data) self._size = _size = 1 << (self._len - 1).bit_length() self.data = [default] * (2 * _size) self.data[_size:_size + self._len] = data for i in reversed(range(_size)): self.data[i] = func(self.data[i + i], self.data[i + i + 1]) def __delitem__(self, idx): self[idx] = self._default def __getitem__(self, idx): return self.data[idx + self._size] def __setitem__(self, idx, value): idx += self._size self.data[idx] = value idx >>= 1 while idx: self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1]) idx >>= 1 def __len__(self): return self._len def query(self, start, stop): if start == stop: return self.__getitem__(start) stop += 1 start += self._size stop += self._size res = self._default while start < stop: if start & 1: res = self._func(res, self.data[start]) start += 1 if stop & 1: stop -= 1 res = self._func(res, self.data[stop]) start >>= 1 stop >>= 1 return res def __repr__(self): return "SegmentTree({0})".format(self.data) # -------------------game starts now----------------------------------------------------import math class SegmentTree: def __init__(self, data, default=0, func=lambda a, b:a + b): """initialize the segment tree with data""" self._default = default self._func = func self._len = len(data) self._size = _size = 1 << (self._len - 1).bit_length() self.data = [default] * (2 * _size) self.data[_size:_size + self._len] = data for i in reversed(range(_size)): self.data[i] = func(self.data[i + i], self.data[i + i + 1]) def __delitem__(self, idx): self[idx] = self._default def __getitem__(self, idx): return self.data[idx + self._size] def __setitem__(self, idx, value): idx += self._size self.data[idx] = value idx >>= 1 while idx: self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1]) idx >>= 1 def __len__(self): return self._len def query(self, start, stop): if start == stop: return self.__getitem__(start) stop += 1 start += self._size stop += self._size res = self._default while start < stop: if start & 1: res = self._func(res, self.data[start]) start += 1 if stop & 1: stop -= 1 res = self._func(res, self.data[stop]) start >>= 1 stop >>= 1 return res def __repr__(self): return "SegmentTree({0})".format(self.data) # -------------------------------iye ha chutiya zindegi------------------------------------- class Factorial: def __init__(self, MOD): self.MOD = MOD self.factorials = [1, 1] self.invModulos = [0, 1] self.invFactorial_ = [1, 1] def calc(self, n): if n <= -1: print("Invalid argument to calculate n!") print("n must be non-negative value. But the argument was " + str(n)) exit() if n < len(self.factorials): return self.factorials[n] nextArr = [0] * (n + 1 - len(self.factorials)) initialI = len(self.factorials) prev = self.factorials[-1] m = self.MOD for i in range(initialI, n + 1): prev = nextArr[i - initialI] = prev * i % m self.factorials += nextArr return self.factorials[n] def inv(self, n): if n <= -1: print("Invalid argument to calculate n^(-1)") print("n must be non-negative value. But the argument was " + str(n)) exit() p = self.MOD pi = n % p if pi < len(self.invModulos): return self.invModulos[pi] nextArr = [0] * (n + 1 - len(self.invModulos)) initialI = len(self.invModulos) for i in range(initialI, min(p, n + 1)): next = -self.invModulos[p % i] * (p // i) % p self.invModulos.append(next) return self.invModulos[pi] def invFactorial(self, n): if n <= -1: print("Invalid argument to calculate (n^(-1))!") print("n must be non-negative value. But the argument was " + str(n)) exit() if n < len(self.invFactorial_): return self.invFactorial_[n] self.inv(n) # To make sure already calculated n^-1 nextArr = [0] * (n + 1 - len(self.invFactorial_)) initialI = len(self.invFactorial_) prev = self.invFactorial_[-1] p = self.MOD for i in range(initialI, n + 1): prev = nextArr[i - initialI] = (prev * self.invModulos[i % p]) % p self.invFactorial_ += nextArr return self.invFactorial_[n] class Combination: def __init__(self, MOD): self.MOD = MOD self.factorial = Factorial(MOD) def ncr(self, n, k): if k < 0 or n < k: return 0 k = min(k, n - k) f = self.factorial return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD # --------------------------------------iye ha combinations ka zindegi--------------------------------- def powm(a, n, m): if a == 1 or n == 0: return 1 if n % 2 == 0: s = powm(a, n // 2, m) return s * s % m else: return a * powm(a, n - 1, m) % m # --------------------------------------iye ha power ka zindegi--------------------------------- def sort_list(list1, list2): zipped_pairs = zip(list2, list1) z = [x for _, x in sorted(zipped_pairs)] return z # --------------------------------------------------product---------------------------------------- def product(l): por = 1 for i in range(len(l)): por *= l[i] return por # --------------------------------------------------binary---------------------------------------- def binarySearchCount(arr, n, key): left = 0 right = n - 1 count = 0 while (left <= right): mid = int((right + left) / 2) # Check if middle element is # less than or equal to key if (arr[mid] < key): count = mid + 1 left = mid + 1 # If key is smaller, ignore right half else: right = mid - 1 return count # --------------------------------------------------binary---------------------------------------- def countdig(n): c = 0 while (n > 0): n //= 10 c += 1 return c def binary(x, length): y = bin(x)[2:] return y if len(y) >= length else "0" * (length - len(y)) + y def countGreater(arr, n, k): l = 0 r = n - 1 # Stores the index of the left most element # from the array which is greater than k leftGreater = n # Finds number of elements greater than k while (l <= r): m = int(l + (r - l) / 2) if (arr[m] > k): leftGreater = m r = m - 1 # If mid element is less than # or equal to k update l else: l = m + 1 # Return the count of elements # greater than k return (n - leftGreater) # --------------------------------------------------binary------------------------------------ class TrieNode: def __init__(self): self.children = [None] * 26 self.isEndOfWord = False class Trie: def __init__(self): self.root = self.getNode() def getNode(self): return TrieNode() def _charToIndex(self, ch): return ord(ch) - ord('a') def insert(self, key): pCrawl = self.root length = len(key) for level in range(length): index = self._charToIndex(key[level]) if not pCrawl.children[index]: pCrawl.children[index] = self.getNode() pCrawl = pCrawl.children[index] pCrawl.isEndOfWord = True def search(self, key): pCrawl = self.root length = len(key) for level in range(length): index = self._charToIndex(key[level]) if not pCrawl.children[index]: return False pCrawl = pCrawl.children[index] return pCrawl != None and pCrawl.isEndOfWord #-----------------------------------------trie--------------------------------- class Node: def __init__(self, data): self.data = data self.count=0 self.left = None # left node for 0 self.right = None # right node for 1 class BinaryTrie: def __init__(self): self.root = Node(0) def insert(self, pre_xor): self.temp = self.root for i in range(31, -1, -1): val = pre_xor & (1 << i) if val: if not self.temp.right: self.temp.right = Node(0) self.temp = self.temp.right self.temp.count+=1 if not val: if not self.temp.left: self.temp.left = Node(0) self.temp = self.temp.left self.temp.count += 1 self.temp.data = pre_xor def query(self, xor): self.temp = self.root for i in range(31, -1, -1): val = xor & (1 << i) if not val: if self.temp.left and self.temp.left.count>0: self.temp = self.temp.left elif self.temp.right: self.temp = self.temp.right else: if self.temp.right and self.temp.right.count>0: self.temp = self.temp.right elif self.temp.left: self.temp = self.temp.left self.temp.count-=1 return xor ^ self.temp.data #-------------------------bin trie------------------------------------------- n,m=map(int,input().split()) l=[] pm=2**m-1 for i in range(n): l.append(list(map(int,input().split()))) def find (x): s=set() d=defaultdict(int) for i in range(n): a="" for j in range(m): if l[i][j]>=x: a+='1' else: a+='0' d[int(a,2)]=i s.add(int(a,2)) s=list(s) #print(s) for i in range(len(s)): for j in range(i,len(s)): if s[i]|s[j]==pm: return [d[s[i]]+1,d[s[j]]+1] return [-1,-1] st=0 end=10**9 ans=(0,0) while(st<=end): mid=(st+end)//2 s=find(mid) if s[0]!=-1: ans=s st=mid+1 else: end=mid-1 print(*ans) ```
output
1
8,483
12
16,967
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n arrays a_1, a_2, ..., a_n; each array consists of exactly m integers. We denote the y-th element of the x-th array as a_{x, y}. You have to choose two arrays a_i and a_j (1 ≤ i, j ≤ n, it is possible that i = j). After that, you will obtain a new array b consisting of m integers, such that for every k ∈ [1, m] b_k = max(a_{i, k}, a_{j, k}). Your goal is to choose i and j so that the value of min _{k = 1}^{m} b_k is maximum possible. Input The first line contains two integers n and m (1 ≤ n ≤ 3 ⋅ 10^5, 1 ≤ m ≤ 8) — the number of arrays and the number of elements in each array, respectively. Then n lines follow, the x-th line contains the array a_x represented by m integers a_{x, 1}, a_{x, 2}, ..., a_{x, m} (0 ≤ a_{x, y} ≤ 10^9). Output Print two integers i and j (1 ≤ i, j ≤ n, it is possible that i = j) — the indices of the two arrays you have to choose so that the value of min _{k = 1}^{m} b_k is maximum possible. If there are multiple answers, print any of them. Example Input 6 5 5 0 3 1 2 1 8 9 1 3 1 2 3 4 5 9 1 0 3 7 2 3 0 6 3 6 4 1 7 0 Output 1 5
instruction
0
8,484
12
16,968
Tags: binary search, bitmasks, dp Correct Solution: ``` import sys def find_pair(candidate,data,m): ans = (-1,-1) binary_bit = [False for i in range(1 << m)] for i in data: bit_tmp = 0 for j in range(len(i)): if i[j] >= candidate: bit_tmp |= 1 << j binary_bit[bit_tmp] = True for i in range(1 << m): for j in range(1 << m): if i | j == (( 1 << m ) - 1) and binary_bit[i] and binary_bit[j]: ans = i , j break return ans def backtracking(candidate,ans,data): idx_i = -1 ; idx_j = -1 for i in range(len(data)): bit_tmp = 0 for j in range(len(data[i])): if data[i][j] >= candidate: bit_tmp |= 1 << j if bit_tmp == ans[0]: idx_i = i if bit_tmp == ans[1]: idx_j = i print(str(idx_i + 1) + " " + str(idx_j + 1)) def main(): n , m = [int(i) for i in input().split()] data = [[int(i) for i in input().split()] for i in range(n)] a = 0 ; b = 10**9 + 7 ans = (-1,-1) candidate = -1 while a <= b: mid = (a + b)//2 bin_ans = find_pair(mid,data,m) if bin_ans[0] != -1 and bin_ans[1] != -1: ans = bin_ans candidate = mid a = mid + 1 else: b = mid - 1 backtracking(candidate,ans,data) main() ```
output
1
8,484
12
16,969
Provide tags and a correct Python 3 solution for this coding contest problem. You are given n arrays a_1, a_2, ..., a_n; each array consists of exactly m integers. We denote the y-th element of the x-th array as a_{x, y}. You have to choose two arrays a_i and a_j (1 ≤ i, j ≤ n, it is possible that i = j). After that, you will obtain a new array b consisting of m integers, such that for every k ∈ [1, m] b_k = max(a_{i, k}, a_{j, k}). Your goal is to choose i and j so that the value of min _{k = 1}^{m} b_k is maximum possible. Input The first line contains two integers n and m (1 ≤ n ≤ 3 ⋅ 10^5, 1 ≤ m ≤ 8) — the number of arrays and the number of elements in each array, respectively. Then n lines follow, the x-th line contains the array a_x represented by m integers a_{x, 1}, a_{x, 2}, ..., a_{x, m} (0 ≤ a_{x, y} ≤ 10^9). Output Print two integers i and j (1 ≤ i, j ≤ n, it is possible that i = j) — the indices of the two arrays you have to choose so that the value of min _{k = 1}^{m} b_k is maximum possible. If there are multiple answers, print any of them. Example Input 6 5 5 0 3 1 2 1 8 9 1 3 1 2 3 4 5 9 1 0 3 7 2 3 0 6 3 6 4 1 7 0 Output 1 5
instruction
0
8,485
12
16,970
Tags: binary search, bitmasks, dp Correct Solution: ``` def main(): N, M = map(int, input().split()) L = [tuple(map(int, input().split())) for _ in range(N)] maxi = max(max(t) for t in L)+1 mini, res = max((min(t), i) for i, t in enumerate(L)) res = res, res BITMASK = (1 << M) while True: mid = (maxi+mini)//2 #print(f'{mini} {mid} {maxi}') if mid == mini: break masks = [None]*BITMASK for i, t in enumerate(L): tmask = 0 for v in t: tmask *= 2 if v >= mid: tmask += 1 if masks[tmask] is not None: continue masks[tmask] = i for k in range(BITMASK): if masks[k] is not None and k | tmask == BITMASK-1: res = masks[k], i mini = mid = min(max(a, b) for a, b in zip(L[res[0]], L[res[1]])) break else: continue break else: maxi = mid #print(masks) print(res[0]+1, res[1]+1) main() ```
output
1
8,485
12
16,971
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given n arrays a_1, a_2, ..., a_n; each array consists of exactly m integers. We denote the y-th element of the x-th array as a_{x, y}. You have to choose two arrays a_i and a_j (1 ≤ i, j ≤ n, it is possible that i = j). After that, you will obtain a new array b consisting of m integers, such that for every k ∈ [1, m] b_k = max(a_{i, k}, a_{j, k}). Your goal is to choose i and j so that the value of min _{k = 1}^{m} b_k is maximum possible. Input The first line contains two integers n and m (1 ≤ n ≤ 3 ⋅ 10^5, 1 ≤ m ≤ 8) — the number of arrays and the number of elements in each array, respectively. Then n lines follow, the x-th line contains the array a_x represented by m integers a_{x, 1}, a_{x, 2}, ..., a_{x, m} (0 ≤ a_{x, y} ≤ 10^9). Output Print two integers i and j (1 ≤ i, j ≤ n, it is possible that i = j) — the indices of the two arrays you have to choose so that the value of min _{k = 1}^{m} b_k is maximum possible. If there are multiple answers, print any of them. Example Input 6 5 5 0 3 1 2 1 8 9 1 3 1 2 3 4 5 9 1 0 3 7 2 3 0 6 3 6 4 1 7 0 Output 1 5 Submitted Solution: ``` #Code by Sounak, IIESTS #------------------------------warmup---------------------------- import os import sys import math from io import BytesIO, IOBase from fractions import Fraction import collections from itertools import permutations from collections import defaultdict from collections import deque import threading #sys.setrecursionlimit(300000) #threading.stack_size(10**8) BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") #------------------------------------------------------------------------- #mod = 9223372036854775807 class SegmentTree: def __init__(self, data, default=0, func=lambda a, b: a+b): """initialize the segment tree with data""" self._default = default self._func = func self._len = len(data) self._size = _size = 1 << (self._len - 1).bit_length() self.data = [default] * (2 * _size) self.data[_size:_size + self._len] = data for i in reversed(range(_size)): self.data[i] = func(self.data[i + i], self.data[i + i + 1]) def __delitem__(self, idx): self[idx] = self._default def __getitem__(self, idx): return self.data[idx + self._size] def __setitem__(self, idx, value): idx += self._size self.data[idx] = value idx >>= 1 while idx: self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1]) idx >>= 1 def __len__(self): return self._len def query(self, start, stop): if start == stop: return self.__getitem__(start) stop += 1 start += self._size stop += self._size res = self._default while start < stop: if start & 1: res = self._func(res, self.data[start]) start += 1 if stop & 1: stop -= 1 res = self._func(res, self.data[stop]) start >>= 1 stop >>= 1 return res def __repr__(self): return "SegmentTree({0})".format(self.data) class SegmentTree1: def __init__(self, data, default=10**6, func=lambda a, b: min(a,b)): """initialize the segment tree with data""" self._default = default self._func = func self._len = len(data) self._size = _size = 1 << (self._len - 1).bit_length() self.data = [default] * (2 * _size) self.data[_size:_size + self._len] = data for i in reversed(range(_size)): self.data[i] = func(self.data[i + i], self.data[i + i + 1]) def __delitem__(self, idx): self[idx] = self._default def __getitem__(self, idx): return self.data[idx + self._size] def __setitem__(self, idx, value): idx += self._size self.data[idx] = value idx >>= 1 while idx: self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1]) idx >>= 1 def __len__(self): return self._len def query(self, start, stop): if start == stop: return self.__getitem__(start) stop += 1 start += self._size stop += self._size res = self._default while start < stop: if start & 1: res = self._func(res, self.data[start]) start += 1 if stop & 1: stop -= 1 res = self._func(res, self.data[stop]) start >>= 1 stop >>= 1 return res def __repr__(self): return "SegmentTree({0})".format(self.data) MOD=10**9+7 class Factorial: def __init__(self, MOD): self.MOD = MOD self.factorials = [1, 1] self.invModulos = [0, 1] self.invFactorial_ = [1, 1] def calc(self, n): if n <= -1: print("Invalid argument to calculate n!") print("n must be non-negative value. But the argument was " + str(n)) exit() if n < len(self.factorials): return self.factorials[n] nextArr = [0] * (n + 1 - len(self.factorials)) initialI = len(self.factorials) prev = self.factorials[-1] m = self.MOD for i in range(initialI, n + 1): prev = nextArr[i - initialI] = prev * i % m self.factorials += nextArr return self.factorials[n] def inv(self, n): if n <= -1: print("Invalid argument to calculate n^(-1)") print("n must be non-negative value. But the argument was " + str(n)) exit() p = self.MOD pi = n % p if pi < len(self.invModulos): return self.invModulos[pi] nextArr = [0] * (n + 1 - len(self.invModulos)) initialI = len(self.invModulos) for i in range(initialI, min(p, n + 1)): next = -self.invModulos[p % i] * (p // i) % p self.invModulos.append(next) return self.invModulos[pi] def invFactorial(self, n): if n <= -1: print("Invalid argument to calculate (n^(-1))!") print("n must be non-negative value. But the argument was " + str(n)) exit() if n < len(self.invFactorial_): return self.invFactorial_[n] self.inv(n) # To make sure already calculated n^-1 nextArr = [0] * (n + 1 - len(self.invFactorial_)) initialI = len(self.invFactorial_) prev = self.invFactorial_[-1] p = self.MOD for i in range(initialI, n + 1): prev = nextArr[i - initialI] = (prev * self.invModulos[i % p]) % p self.invFactorial_ += nextArr return self.invFactorial_[n] class Combination: def __init__(self, MOD): self.MOD = MOD self.factorial = Factorial(MOD) def ncr(self, n, k): if k < 0 or n < k: return 0 k = min(k, n - k) f = self.factorial return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD mod=10**9+7 omod=998244353 #------------------------------------------------------------------------- prime = [True for i in range(10)] pp=[0]*10 def SieveOfEratosthenes(n=10): p = 2 c=0 while (p * p <= n): if (prime[p] == True): c+=1 for i in range(p, n+1, p): pp[i]+=1 prime[i] = False p += 1 #---------------------------------Binary Search------------------------------------------ def binarySearch(arr, n, key): left = 0 right = n-1 mid = 0 res=arr[n-1] while (left <= right): mid = (right + left)//2 if (arr[mid] >= key): res=arr[mid] right = mid-1 else: left = mid + 1 return res def binarySearch1(arr, n, key): left = 0 right = n-1 mid = 0 res=arr[0] while (left <= right): mid = (right + left)//2 if (arr[mid] > key): right = mid-1 else: res=arr[mid] left = mid + 1 return res #---------------------------------running code------------------------------------------ n,m=map(int,input().split()) l=[] pm=2**m-1 for i in range(n): l.append(list(map(int,input().split()))) def find (x): s=set() d=defaultdict(int) for i in range(n): a="" for j in range(m): if l[i][j]>=x: a+='1' else: a+='0' d[int(a,2)]=i s.add(int(a,2)) s=list(s) #print(s) for i in range(len(s)): for j in range(i,len(s)): if s[i]|s[j]==pm: return [d[s[i]]+1,d[s[j]]+1] return [-1,-1] st=0 end=10**9 ans=(0,0) while(st<=end): mid=(st+end)//2 s=find(mid) if s[0]!=-1: ans=s st=mid+1 else: end=mid-1 print(*ans) ```
instruction
0
8,486
12
16,972
Yes
output
1
8,486
12
16,973
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given n arrays a_1, a_2, ..., a_n; each array consists of exactly m integers. We denote the y-th element of the x-th array as a_{x, y}. You have to choose two arrays a_i and a_j (1 ≤ i, j ≤ n, it is possible that i = j). After that, you will obtain a new array b consisting of m integers, such that for every k ∈ [1, m] b_k = max(a_{i, k}, a_{j, k}). Your goal is to choose i and j so that the value of min _{k = 1}^{m} b_k is maximum possible. Input The first line contains two integers n and m (1 ≤ n ≤ 3 ⋅ 10^5, 1 ≤ m ≤ 8) — the number of arrays and the number of elements in each array, respectively. Then n lines follow, the x-th line contains the array a_x represented by m integers a_{x, 1}, a_{x, 2}, ..., a_{x, m} (0 ≤ a_{x, y} ≤ 10^9). Output Print two integers i and j (1 ≤ i, j ≤ n, it is possible that i = j) — the indices of the two arrays you have to choose so that the value of min _{k = 1}^{m} b_k is maximum possible. If there are multiple answers, print any of them. Example Input 6 5 5 0 3 1 2 1 8 9 1 3 1 2 3 4 5 9 1 0 3 7 2 3 0 6 3 6 4 1 7 0 Output 1 5 Submitted Solution: ``` def isPoss(n, arrs, nvals): masks = set() midx = {} for pos,arr in enumerate(arrs): mask = 0 for i in range(nvals): if arr[i]>=n: mask += 1<<i midx[mask] = pos+1 masks.add(mask) for m1 in masks: for m2 in masks: if m1|m2 == (1<<nvals)-1: return midx[m1], midx[m2] return -1, -1 narr, nvals = map(int, input().split()) arrs = [] for i in range(narr): arrs.append(list(map(int, input().split()))) mn = -1 mx = 10**9+1 while mn < mx-1: mid = (mn + mx) // 2 a,b = isPoss(mid, arrs, nvals) if a != -1: mn = mid else: mx = mid - 1 for i in range(1,-1,-1): a,b = isPoss(mn+i, arrs, nvals) if a != -1: print(a,b) break ```
instruction
0
8,488
12
16,976
Yes
output
1
8,488
12
16,977
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given n arrays a_1, a_2, ..., a_n; each array consists of exactly m integers. We denote the y-th element of the x-th array as a_{x, y}. You have to choose two arrays a_i and a_j (1 ≤ i, j ≤ n, it is possible that i = j). After that, you will obtain a new array b consisting of m integers, such that for every k ∈ [1, m] b_k = max(a_{i, k}, a_{j, k}). Your goal is to choose i and j so that the value of min _{k = 1}^{m} b_k is maximum possible. Input The first line contains two integers n and m (1 ≤ n ≤ 3 ⋅ 10^5, 1 ≤ m ≤ 8) — the number of arrays and the number of elements in each array, respectively. Then n lines follow, the x-th line contains the array a_x represented by m integers a_{x, 1}, a_{x, 2}, ..., a_{x, m} (0 ≤ a_{x, y} ≤ 10^9). Output Print two integers i and j (1 ≤ i, j ≤ n, it is possible that i = j) — the indices of the two arrays you have to choose so that the value of min _{k = 1}^{m} b_k is maximum possible. If there are multiple answers, print any of them. Example Input 6 5 5 0 3 1 2 1 8 9 1 3 1 2 3 4 5 9 1 0 3 7 2 3 0 6 3 6 4 1 7 0 Output 1 5 Submitted Solution: ``` import random def want(p,q): ans = float("inf") for i in range(m): ans = min(ans , max(a[p][i],a[q][i])) return ans n,m = map(int,input().split()) a = [] for i in range(n): A = list(map(int,input().split())) a.append(A) x = random.randint(0,n-1) y = random.randint(0,n-1) q = [] for j in range(50): nd = random.randint(0,n-1) for i in range(n): i = (i + nd) % n now = want(x,y) planA = want(x,i) planB = want(i,y) if max(now,planA,planB) == planA: y = i elif max(now,planA,planB) == planB: x = i q.append([x,y]) minind = 0 for i in range(50): if want(q[minind][0],q[minind][1]) > want(q[i][0],q[i][1]): minind = i print (q[minind][0] + 1, q[minind][1] + 1) ```
instruction
0
8,490
12
16,980
No
output
1
8,490
12
16,981
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given n arrays a_1, a_2, ..., a_n; each array consists of exactly m integers. We denote the y-th element of the x-th array as a_{x, y}. You have to choose two arrays a_i and a_j (1 ≤ i, j ≤ n, it is possible that i = j). After that, you will obtain a new array b consisting of m integers, such that for every k ∈ [1, m] b_k = max(a_{i, k}, a_{j, k}). Your goal is to choose i and j so that the value of min _{k = 1}^{m} b_k is maximum possible. Input The first line contains two integers n and m (1 ≤ n ≤ 3 ⋅ 10^5, 1 ≤ m ≤ 8) — the number of arrays and the number of elements in each array, respectively. Then n lines follow, the x-th line contains the array a_x represented by m integers a_{x, 1}, a_{x, 2}, ..., a_{x, m} (0 ≤ a_{x, y} ≤ 10^9). Output Print two integers i and j (1 ≤ i, j ≤ n, it is possible that i = j) — the indices of the two arrays you have to choose so that the value of min _{k = 1}^{m} b_k is maximum possible. If there are multiple answers, print any of them. Example Input 6 5 5 0 3 1 2 1 8 9 1 3 1 2 3 4 5 9 1 0 3 7 2 3 0 6 3 6 4 1 7 0 Output 1 5 Submitted Solution: ``` ''' Hey stalker :) ''' INF = 10**10 def main(): #print = out.append ''' Cook your dish here! ''' n, m = get_list() mat = [get_list() for _ in range(n)] def fn(x): #print(x, end=": ") st = [-1]*(1<<m) st[0] = 0 for index, li in enumerate(mat): no = 0 for i, ele in enumerate(li): if ele>=x: no |= 1<<i li = [0] k = 2**m -1 - no if st[k]>-1: #print(True, st[k], index) return True, st[k], index b = 0 while(b!=0): st[b] = index b = (b-no)&no # for i in range(m): # if (no & (1<<i))==(1<<i): # for j in range(len(li)): # li.append(li[j] | 1<<i) # st[li[-1]] = index continue return False, -1, -1 x = 0 base = 10**8 while base>0: while fn(x+base)[0]: x+= base base //= 2 res, a,b = fn(x) print(a+1, b+1) ''' Pythonista fLite 1.1 ''' import sys #from collections import defaultdict, Counter #from functools import reduce #import math #input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__ out = [] get_int = lambda: int(input()) get_list = lambda: list(map(int, input().split())) main() #[main() for _ in range(int(input()))] #print(*out, sep='\n') ```
instruction
0
8,492
12
16,984
No
output
1
8,492
12
16,985
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a of n integers. Find the number of pairs (i, j) (1 ≤ i < j ≤ n) where the sum of a_i + a_j is greater than or equal to l and less than or equal to r (that is, l ≤ a_i + a_j ≤ r). For example, if n = 3, a = [5, 1, 2], l = 4 and r = 7, then two pairs are suitable: * i=1 and j=2 (4 ≤ 5 + 1 ≤ 7); * i=1 and j=3 (4 ≤ 5 + 2 ≤ 7). Input The first line contains an integer t (1 ≤ t ≤ 10^4). Then t test cases follow. The first line of each test case contains three integers n, l, r (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ l ≤ r ≤ 10^9) — the length of the array and the limits on the sum in the pair. The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9). It is guaranteed that the sum of n overall test cases does not exceed 2 ⋅ 10^5. Output For each test case, output a single integer — the number of index pairs (i, j) (i < j), such that l ≤ a_i + a_j ≤ r. Example Input 4 3 4 7 5 1 2 5 5 8 5 1 2 4 3 4 100 1000 1 1 1 1 5 9 13 2 5 5 1 1 Output 2 7 0 1
instruction
0
8,591
12
17,182
Tags: binary search, data structures, math, two pointers Correct Solution: ``` from collections import deque, defaultdict from math import sqrt, ceil, factorial, floor, inf, log2, sqrt, gcd import bisect import copy from itertools import combinations import sys def get_array(): return list(map(int, sys.stdin.readline().strip().split())) def get_ints(): return map(int, sys.stdin.readline().strip().split()) def input(): return sys.stdin.readline().strip() from collections import defaultdict for _ in range(int(input())): n,l,r=get_ints() a=get_array() a.sort() ans=0 ##print(a) for i in range(len(a)): ll=l-a[i] ul=r-a[i] ind1=bisect.bisect_right(a,ul) ind2 = bisect.bisect_left(a, ll) ind1-=1 if ind1<=i: continue ind2=bisect.bisect_left(a,ll) if ind2<=i: ind2=min(i+1,ind1) ##print(ind1,ind2,ll,ul) if ind1-ind2+1>=0: ans+=ind1-ind2+1 ##print(ans) print(ans) ```
output
1
8,591
12
17,183
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a of n integers. Find the number of pairs (i, j) (1 ≤ i < j ≤ n) where the sum of a_i + a_j is greater than or equal to l and less than or equal to r (that is, l ≤ a_i + a_j ≤ r). For example, if n = 3, a = [5, 1, 2], l = 4 and r = 7, then two pairs are suitable: * i=1 and j=2 (4 ≤ 5 + 1 ≤ 7); * i=1 and j=3 (4 ≤ 5 + 2 ≤ 7). Input The first line contains an integer t (1 ≤ t ≤ 10^4). Then t test cases follow. The first line of each test case contains three integers n, l, r (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ l ≤ r ≤ 10^9) — the length of the array and the limits on the sum in the pair. The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9). It is guaranteed that the sum of n overall test cases does not exceed 2 ⋅ 10^5. Output For each test case, output a single integer — the number of index pairs (i, j) (i < j), such that l ≤ a_i + a_j ≤ r. Example Input 4 3 4 7 5 1 2 5 5 8 5 1 2 4 3 4 100 1000 1 1 1 1 5 9 13 2 5 5 1 1 Output 2 7 0 1
instruction
0
8,592
12
17,184
Tags: binary search, data structures, math, two pointers Correct Solution: ``` import bisect def solve(arr, n, l, r): totalCount = 0 for i in range(0, n-1): start = l - arr[i] end = r - arr[i] c1 = bisect.bisect(arr, start-1, i+1, n) c2 = bisect.bisect(arr, end, i+1, n) totalCount += (c2 - c1) return totalCount testcases = int(input()) while testcases: n, l, r = map(int, input().split()) arr = list(map(int, input().split())) arr.sort() tc = solve(arr, n, l, r) print(tc) testcases -= 1 ```
output
1
8,592
12
17,185
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a of n integers. Find the number of pairs (i, j) (1 ≤ i < j ≤ n) where the sum of a_i + a_j is greater than or equal to l and less than or equal to r (that is, l ≤ a_i + a_j ≤ r). For example, if n = 3, a = [5, 1, 2], l = 4 and r = 7, then two pairs are suitable: * i=1 and j=2 (4 ≤ 5 + 1 ≤ 7); * i=1 and j=3 (4 ≤ 5 + 2 ≤ 7). Input The first line contains an integer t (1 ≤ t ≤ 10^4). Then t test cases follow. The first line of each test case contains three integers n, l, r (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ l ≤ r ≤ 10^9) — the length of the array and the limits on the sum in the pair. The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9). It is guaranteed that the sum of n overall test cases does not exceed 2 ⋅ 10^5. Output For each test case, output a single integer — the number of index pairs (i, j) (i < j), such that l ≤ a_i + a_j ≤ r. Example Input 4 3 4 7 5 1 2 5 5 8 5 1 2 4 3 4 100 1000 1 1 1 1 5 9 13 2 5 5 1 1 Output 2 7 0 1
instruction
0
8,593
12
17,186
Tags: binary search, data structures, math, two pointers Correct Solution: ``` #ANKIT BEHIND THE KEYBOARD from collections import defaultdict t=int(input()) for i in range(0,t): n,left,right=map(int,input().split()) l=list(map(int,input().split())) l.sort() ans = n * (n - 1) // 2 i = 0 j = n - 1 c = 0 while i < j: if l[j] + l[i] < left: c += j - i i += 1 else: j -= 1 i = 0 j = n - 1 x = 0 while i < j: if l[j] + l[i] <= right: x += j - i i += 1 else: j -= 1 d = n * (n - 1) // 2 - x ans -= c ans -= d print(ans) ```
output
1
8,593
12
17,187
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a of n integers. Find the number of pairs (i, j) (1 ≤ i < j ≤ n) where the sum of a_i + a_j is greater than or equal to l and less than or equal to r (that is, l ≤ a_i + a_j ≤ r). For example, if n = 3, a = [5, 1, 2], l = 4 and r = 7, then two pairs are suitable: * i=1 and j=2 (4 ≤ 5 + 1 ≤ 7); * i=1 and j=3 (4 ≤ 5 + 2 ≤ 7). Input The first line contains an integer t (1 ≤ t ≤ 10^4). Then t test cases follow. The first line of each test case contains three integers n, l, r (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ l ≤ r ≤ 10^9) — the length of the array and the limits on the sum in the pair. The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9). It is guaranteed that the sum of n overall test cases does not exceed 2 ⋅ 10^5. Output For each test case, output a single integer — the number of index pairs (i, j) (i < j), such that l ≤ a_i + a_j ≤ r. Example Input 4 3 4 7 5 1 2 5 5 8 5 1 2 4 3 4 100 1000 1 1 1 1 5 9 13 2 5 5 1 1 Output 2 7 0 1
instruction
0
8,594
12
17,188
Tags: binary search, data structures, math, two pointers Correct Solution: ``` from collections import deque, defaultdict, Counter from itertools import product, groupby, permutations, combinations from math import gcd, floor cases = int(input()) for _ in range(cases): n, l, r = map(int, input().split()) arr = sorted(map(int, input().split())) a1 =[0] * n a2 = [0] * n i, j = 0, n - 1 while i - j <= 0: if arr[i] + arr[j] >= l: a1[j] = i j -= 1 else: a1[i] = j + 1 i += 1 i, j = 0, n - 1 while i - j <= 0: if arr[i] + arr[j] <= r: a2[i] = j i += 1 else: a2[j] = i - 1 j -= 1 ans = [ans2 - ans1 + 1 - (ans1 <= i <= ans2) for i, (ans1, ans2) in enumerate(zip(a1, a2))] print(sum(ans)//2) ```
output
1
8,594
12
17,189
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a of n integers. Find the number of pairs (i, j) (1 ≤ i < j ≤ n) where the sum of a_i + a_j is greater than or equal to l and less than or equal to r (that is, l ≤ a_i + a_j ≤ r). For example, if n = 3, a = [5, 1, 2], l = 4 and r = 7, then two pairs are suitable: * i=1 and j=2 (4 ≤ 5 + 1 ≤ 7); * i=1 and j=3 (4 ≤ 5 + 2 ≤ 7). Input The first line contains an integer t (1 ≤ t ≤ 10^4). Then t test cases follow. The first line of each test case contains three integers n, l, r (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ l ≤ r ≤ 10^9) — the length of the array and the limits on the sum in the pair. The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9). It is guaranteed that the sum of n overall test cases does not exceed 2 ⋅ 10^5. Output For each test case, output a single integer — the number of index pairs (i, j) (i < j), such that l ≤ a_i + a_j ≤ r. Example Input 4 3 4 7 5 1 2 5 5 8 5 1 2 4 3 4 100 1000 1 1 1 1 5 9 13 2 5 5 1 1 Output 2 7 0 1
instruction
0
8,595
12
17,190
Tags: binary search, data structures, math, two pointers Correct Solution: ``` import sys;input=sys.stdin.readline from bisect import bisect # from collections import defaultdict # from itertools import accumulate # from decimal import * # import math # getcontext().prec = 50 # s = input().strip() # n = int(input()) # lis = list(map(int,input().split())) # x,y = map(int,input().split()) # chars = 'abcdefghijklmnopqrstuvwxyz' # import heapq # def gcd(a,b): # return gcd (b, a % b) if b else a def solve(): n,l,r = map(int,input().split()) lis = list(map(int,input().split())) lis = sorted(lis) count = 0 for i in range(len(lis)): start = bisect(lis,l-lis[i]-1) end = bisect(lis,r-lis[i]) add = 0 if i>=start and i<end: add = -1 add+=end-start count+=add print(count//2) # solve() for _ in range(int(input())): solve() ```
output
1
8,595
12
17,191
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a of n integers. Find the number of pairs (i, j) (1 ≤ i < j ≤ n) where the sum of a_i + a_j is greater than or equal to l and less than or equal to r (that is, l ≤ a_i + a_j ≤ r). For example, if n = 3, a = [5, 1, 2], l = 4 and r = 7, then two pairs are suitable: * i=1 and j=2 (4 ≤ 5 + 1 ≤ 7); * i=1 and j=3 (4 ≤ 5 + 2 ≤ 7). Input The first line contains an integer t (1 ≤ t ≤ 10^4). Then t test cases follow. The first line of each test case contains three integers n, l, r (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ l ≤ r ≤ 10^9) — the length of the array and the limits on the sum in the pair. The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9). It is guaranteed that the sum of n overall test cases does not exceed 2 ⋅ 10^5. Output For each test case, output a single integer — the number of index pairs (i, j) (i < j), such that l ≤ a_i + a_j ≤ r. Example Input 4 3 4 7 5 1 2 5 5 8 5 1 2 4 3 4 100 1000 1 1 1 1 5 9 13 2 5 5 1 1 Output 2 7 0 1
instruction
0
8,596
12
17,192
Tags: binary search, data structures, math, two pointers Correct Solution: ``` from bisect import * a=int(input()) total=0 from collections import * for i in range(a): n,l,r=map(int,input().split()) total=0 z=list(map(int,input().split())) al=defaultdict(int) for i in range(len(z)): al[z[i]]+=1 z.sort() for i in range(len(z)): t=bisect_left(z,l-z[i]) q=bisect_right(z,r-z[i]) if(t==len(z)): continue; if(q==len(z)): q-=1 if(z[q]+z[i]>r): q-=1 x=0 total+=max(q-t+1,0) if(l<=z[i]*2<=r): x-=1 total+=x print(total//2) ```
output
1
8,596
12
17,193
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a of n integers. Find the number of pairs (i, j) (1 ≤ i < j ≤ n) where the sum of a_i + a_j is greater than or equal to l and less than or equal to r (that is, l ≤ a_i + a_j ≤ r). For example, if n = 3, a = [5, 1, 2], l = 4 and r = 7, then two pairs are suitable: * i=1 and j=2 (4 ≤ 5 + 1 ≤ 7); * i=1 and j=3 (4 ≤ 5 + 2 ≤ 7). Input The first line contains an integer t (1 ≤ t ≤ 10^4). Then t test cases follow. The first line of each test case contains three integers n, l, r (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ l ≤ r ≤ 10^9) — the length of the array and the limits on the sum in the pair. The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9). It is guaranteed that the sum of n overall test cases does not exceed 2 ⋅ 10^5. Output For each test case, output a single integer — the number of index pairs (i, j) (i < j), such that l ≤ a_i + a_j ≤ r. Example Input 4 3 4 7 5 1 2 5 5 8 5 1 2 4 3 4 100 1000 1 1 1 1 5 9 13 2 5 5 1 1 Output 2 7 0 1
instruction
0
8,597
12
17,194
Tags: binary search, data structures, math, two pointers Correct Solution: ``` for _ in range(int(input())): n,l,r=map(int,input().split()) a=sorted(list(map(int,input().split()))) i,j,q=0,0,0 while i<n: n-=1 while i<n and a[i]+a[n]<l:i+=1 while j<n and a[j]+a[n]<=r:j+=1 q+=min(j,n)-i print(q) ```
output
1
8,597
12
17,195
Provide tags and a correct Python 3 solution for this coding contest problem. You are given an array a of n integers. Find the number of pairs (i, j) (1 ≤ i < j ≤ n) where the sum of a_i + a_j is greater than or equal to l and less than or equal to r (that is, l ≤ a_i + a_j ≤ r). For example, if n = 3, a = [5, 1, 2], l = 4 and r = 7, then two pairs are suitable: * i=1 and j=2 (4 ≤ 5 + 1 ≤ 7); * i=1 and j=3 (4 ≤ 5 + 2 ≤ 7). Input The first line contains an integer t (1 ≤ t ≤ 10^4). Then t test cases follow. The first line of each test case contains three integers n, l, r (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ l ≤ r ≤ 10^9) — the length of the array and the limits on the sum in the pair. The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9). It is guaranteed that the sum of n overall test cases does not exceed 2 ⋅ 10^5. Output For each test case, output a single integer — the number of index pairs (i, j) (i < j), such that l ≤ a_i + a_j ≤ r. Example Input 4 3 4 7 5 1 2 5 5 8 5 1 2 4 3 4 100 1000 1 1 1 1 5 9 13 2 5 5 1 1 Output 2 7 0 1
instruction
0
8,598
12
17,196
Tags: binary search, data structures, math, two pointers Correct Solution: ``` def solve(arr,l,r,k,x): ans=-1 while(l<=r): m=l+(r-l)//2 if(arr[m]<=k): ans=m l=m+1 else: r=m-1 return ans def sol(arr,l,r,k,x): ans=-1 while(l<=r): m=(r+l)//2 if(arr[m]>=k): ans=m r=m-1 else: l=m+1 return ans for u in range(int(input())): n,l,r=map(int,input().split()) a=list(map(int,input().split())) a.sort() c=0 for i in range(n): x,y=l-a[i],r-a[i] p=sol(a,i+1,n-1,x,y) q=solve(a,i+1,n-1,y,x) if(p!=-1 and q!=-1): c+=(q-p+1) print(c) ```
output
1
8,598
12
17,197
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given an array a of n integers. Find the number of pairs (i, j) (1 ≤ i < j ≤ n) where the sum of a_i + a_j is greater than or equal to l and less than or equal to r (that is, l ≤ a_i + a_j ≤ r). For example, if n = 3, a = [5, 1, 2], l = 4 and r = 7, then two pairs are suitable: * i=1 and j=2 (4 ≤ 5 + 1 ≤ 7); * i=1 and j=3 (4 ≤ 5 + 2 ≤ 7). Input The first line contains an integer t (1 ≤ t ≤ 10^4). Then t test cases follow. The first line of each test case contains three integers n, l, r (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ l ≤ r ≤ 10^9) — the length of the array and the limits on the sum in the pair. The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9). It is guaranteed that the sum of n overall test cases does not exceed 2 ⋅ 10^5. Output For each test case, output a single integer — the number of index pairs (i, j) (i < j), such that l ≤ a_i + a_j ≤ r. Example Input 4 3 4 7 5 1 2 5 5 8 5 1 2 4 3 4 100 1000 1 1 1 1 5 9 13 2 5 5 1 1 Output 2 7 0 1 Submitted Solution: ``` from bisect import bisect_left, bisect_right for _ in range(int(input())): n, l, r = map(int, input().split()) a = list(map(int, input().split())) a.sort(); ans = 0 for i in range(n): if a[i]>=r: break lowlim = bisect_left(a, l-a[i], i+1) highlim = bisect_right(a, r-a[i], i+1) ans+= highlim - lowlim print(ans) ```
instruction
0
8,599
12
17,198
Yes
output
1
8,599
12
17,199
Provide tags and a correct Python 3 solution for this coding contest problem. You are given k sequences of integers. The length of the i-th sequence equals to n_i. You have to choose exactly two sequences i and j (i ≠ j) such that you can remove exactly one element in each of them in such a way that the sum of the changed sequence i (its length will be equal to n_i - 1) equals to the sum of the changed sequence j (its length will be equal to n_j - 1). Note that it's required to remove exactly one element in each of the two chosen sequences. Assume that the sum of the empty (of the length equals 0) sequence is 0. Input The first line contains an integer k (2 ≤ k ≤ 2 ⋅ 10^5) — the number of sequences. Then k pairs of lines follow, each pair containing a sequence. The first line in the i-th pair contains one integer n_i (1 ≤ n_i < 2 ⋅ 10^5) — the length of the i-th sequence. The second line of the i-th pair contains a sequence of n_i integers a_{i, 1}, a_{i, 2}, ..., a_{i, n_i}. The elements of sequences are integer numbers from -10^4 to 10^4. The sum of lengths of all given sequences don't exceed 2 ⋅ 10^5, i.e. n_1 + n_2 + ... + n_k ≤ 2 ⋅ 10^5. Output If it is impossible to choose two sequences such that they satisfy given conditions, print "NO" (without quotes). Otherwise in the first line print "YES" (without quotes), in the second line — two integers i, x (1 ≤ i ≤ k, 1 ≤ x ≤ n_i), in the third line — two integers j, y (1 ≤ j ≤ k, 1 ≤ y ≤ n_j). It means that the sum of the elements of the i-th sequence without the element with index x equals to the sum of the elements of the j-th sequence without the element with index y. Two chosen sequences must be distinct, i.e. i ≠ j. You can print them in any order. If there are multiple possible answers, print any of them. Examples Input 2 5 2 3 1 3 2 6 1 1 2 2 2 1 Output YES 2 6 1 2 Input 3 1 5 5 1 1 1 1 1 2 2 3 Output NO Input 4 6 2 2 2 2 2 2 5 2 2 2 2 2 3 2 2 2 5 2 2 2 2 2 Output YES 2 2 4 1 Note In the first example there are two sequences [2, 3, 1, 3, 2] and [1, 1, 2, 2, 2, 1]. You can remove the second element from the first sequence to get [2, 1, 3, 2] and you can remove the sixth element from the second sequence to get [1, 1, 2, 2, 2]. The sums of the both resulting sequences equal to 8, i.e. the sums are equal.
instruction
0
8,824
12
17,648
Tags: implementation, sortings Correct Solution: ``` # link: https://codeforces.com/problemset/problem/988/C import os, sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") from math import ceil mod = 10 ** 9 + 7 # number of test cases for _ in range(1): k = int(input()) triplet_array = [] for i in range(k): n = int(input()) a = list(map(int, input().split())) s = sum(a) for j in range(n): triplet_array.append((s - a[j], i, j)) triplet_array.sort() for i in range(len(triplet_array)-1): if triplet_array[i][0] == triplet_array[i+1][0] and triplet_array[i][1] != triplet_array[i+1][1]: # and triplet_array[i][2] != triplet_array[i+1][2]: print("YES") print(triplet_array[i][1] + 1, triplet_array[i][2] + 1) print(triplet_array[i+1][1] + 1, triplet_array[i+1][2] + 1) exit(0) print("NO") # array ----> [{2: [0, 4], 3: [1, 3], 1: [2]}, {1: [0, 1, 5], 2: [2, 3, 4]}] # sum ----> {0: 11, 1: 9} ```
output
1
8,824
12
17,649
Provide tags and a correct Python 3 solution for this coding contest problem. You are given k sequences of integers. The length of the i-th sequence equals to n_i. You have to choose exactly two sequences i and j (i ≠ j) such that you can remove exactly one element in each of them in such a way that the sum of the changed sequence i (its length will be equal to n_i - 1) equals to the sum of the changed sequence j (its length will be equal to n_j - 1). Note that it's required to remove exactly one element in each of the two chosen sequences. Assume that the sum of the empty (of the length equals 0) sequence is 0. Input The first line contains an integer k (2 ≤ k ≤ 2 ⋅ 10^5) — the number of sequences. Then k pairs of lines follow, each pair containing a sequence. The first line in the i-th pair contains one integer n_i (1 ≤ n_i < 2 ⋅ 10^5) — the length of the i-th sequence. The second line of the i-th pair contains a sequence of n_i integers a_{i, 1}, a_{i, 2}, ..., a_{i, n_i}. The elements of sequences are integer numbers from -10^4 to 10^4. The sum of lengths of all given sequences don't exceed 2 ⋅ 10^5, i.e. n_1 + n_2 + ... + n_k ≤ 2 ⋅ 10^5. Output If it is impossible to choose two sequences such that they satisfy given conditions, print "NO" (without quotes). Otherwise in the first line print "YES" (without quotes), in the second line — two integers i, x (1 ≤ i ≤ k, 1 ≤ x ≤ n_i), in the third line — two integers j, y (1 ≤ j ≤ k, 1 ≤ y ≤ n_j). It means that the sum of the elements of the i-th sequence without the element with index x equals to the sum of the elements of the j-th sequence without the element with index y. Two chosen sequences must be distinct, i.e. i ≠ j. You can print them in any order. If there are multiple possible answers, print any of them. Examples Input 2 5 2 3 1 3 2 6 1 1 2 2 2 1 Output YES 2 6 1 2 Input 3 1 5 5 1 1 1 1 1 2 2 3 Output NO Input 4 6 2 2 2 2 2 2 5 2 2 2 2 2 3 2 2 2 5 2 2 2 2 2 Output YES 2 2 4 1 Note In the first example there are two sequences [2, 3, 1, 3, 2] and [1, 1, 2, 2, 2, 1]. You can remove the second element from the first sequence to get [2, 1, 3, 2] and you can remove the sixth element from the second sequence to get [1, 1, 2, 2, 2]. The sums of the both resulting sequences equal to 8, i.e. the sums are equal.
instruction
0
8,825
12
17,650
Tags: implementation, sortings Correct Solution: ``` import sys input = lambda:sys.stdin.readline() MOD = 1000000007 ii = lambda: int(input()) si = lambda: input() dgl = lambda: list(map(int, input())) f = lambda: list(map(int, input().split())) il = lambda: list(map(int, input().split())) ls = lambda: list(input()) d = dict() for ind in range(ii()): n=ii() l=il() sm=sum(l) for i in range(n): if not sm-l[i] in d: d[sm-l[i]] = [[ind+1, i+1]] else: if d[sm-l[i]][-1][0] != ind+1: print('YES') print(*d[sm-l[i]][-1]) print(ind+1, i+1) exit() else: d[sm-l[i]].append([ind+1, i+1]) print('NO') ```
output
1
8,825
12
17,651
Provide tags and a correct Python 3 solution for this coding contest problem. You are given k sequences of integers. The length of the i-th sequence equals to n_i. You have to choose exactly two sequences i and j (i ≠ j) such that you can remove exactly one element in each of them in such a way that the sum of the changed sequence i (its length will be equal to n_i - 1) equals to the sum of the changed sequence j (its length will be equal to n_j - 1). Note that it's required to remove exactly one element in each of the two chosen sequences. Assume that the sum of the empty (of the length equals 0) sequence is 0. Input The first line contains an integer k (2 ≤ k ≤ 2 ⋅ 10^5) — the number of sequences. Then k pairs of lines follow, each pair containing a sequence. The first line in the i-th pair contains one integer n_i (1 ≤ n_i < 2 ⋅ 10^5) — the length of the i-th sequence. The second line of the i-th pair contains a sequence of n_i integers a_{i, 1}, a_{i, 2}, ..., a_{i, n_i}. The elements of sequences are integer numbers from -10^4 to 10^4. The sum of lengths of all given sequences don't exceed 2 ⋅ 10^5, i.e. n_1 + n_2 + ... + n_k ≤ 2 ⋅ 10^5. Output If it is impossible to choose two sequences such that they satisfy given conditions, print "NO" (without quotes). Otherwise in the first line print "YES" (without quotes), in the second line — two integers i, x (1 ≤ i ≤ k, 1 ≤ x ≤ n_i), in the third line — two integers j, y (1 ≤ j ≤ k, 1 ≤ y ≤ n_j). It means that the sum of the elements of the i-th sequence without the element with index x equals to the sum of the elements of the j-th sequence without the element with index y. Two chosen sequences must be distinct, i.e. i ≠ j. You can print them in any order. If there are multiple possible answers, print any of them. Examples Input 2 5 2 3 1 3 2 6 1 1 2 2 2 1 Output YES 2 6 1 2 Input 3 1 5 5 1 1 1 1 1 2 2 3 Output NO Input 4 6 2 2 2 2 2 2 5 2 2 2 2 2 3 2 2 2 5 2 2 2 2 2 Output YES 2 2 4 1 Note In the first example there are two sequences [2, 3, 1, 3, 2] and [1, 1, 2, 2, 2, 1]. You can remove the second element from the first sequence to get [2, 1, 3, 2] and you can remove the sixth element from the second sequence to get [1, 1, 2, 2, 2]. The sums of the both resulting sequences equal to 8, i.e. the sums are equal.
instruction
0
8,826
12
17,652
Tags: implementation, sortings Correct Solution: ``` n=int(input()) x=list() for i in range(n): size=int(input()) li=list(map(int,input().split())) add=sum(li) for j in range(size): x.append((add-li[j],i,j)) x=sorted(x) for i in range(1,len(x)): if x[i][0]==x[i-1][0] and x[i][1]!=x[i-1][1]: print("YES") print(x[i][1]+1,x[i][2]+1) print(x[i-1][1]+1,x[i-1][2]+1) exit() print("NO") ```
output
1
8,826
12
17,653
Provide tags and a correct Python 3 solution for this coding contest problem. You are given k sequences of integers. The length of the i-th sequence equals to n_i. You have to choose exactly two sequences i and j (i ≠ j) such that you can remove exactly one element in each of them in such a way that the sum of the changed sequence i (its length will be equal to n_i - 1) equals to the sum of the changed sequence j (its length will be equal to n_j - 1). Note that it's required to remove exactly one element in each of the two chosen sequences. Assume that the sum of the empty (of the length equals 0) sequence is 0. Input The first line contains an integer k (2 ≤ k ≤ 2 ⋅ 10^5) — the number of sequences. Then k pairs of lines follow, each pair containing a sequence. The first line in the i-th pair contains one integer n_i (1 ≤ n_i < 2 ⋅ 10^5) — the length of the i-th sequence. The second line of the i-th pair contains a sequence of n_i integers a_{i, 1}, a_{i, 2}, ..., a_{i, n_i}. The elements of sequences are integer numbers from -10^4 to 10^4. The sum of lengths of all given sequences don't exceed 2 ⋅ 10^5, i.e. n_1 + n_2 + ... + n_k ≤ 2 ⋅ 10^5. Output If it is impossible to choose two sequences such that they satisfy given conditions, print "NO" (without quotes). Otherwise in the first line print "YES" (without quotes), in the second line — two integers i, x (1 ≤ i ≤ k, 1 ≤ x ≤ n_i), in the third line — two integers j, y (1 ≤ j ≤ k, 1 ≤ y ≤ n_j). It means that the sum of the elements of the i-th sequence without the element with index x equals to the sum of the elements of the j-th sequence without the element with index y. Two chosen sequences must be distinct, i.e. i ≠ j. You can print them in any order. If there are multiple possible answers, print any of them. Examples Input 2 5 2 3 1 3 2 6 1 1 2 2 2 1 Output YES 2 6 1 2 Input 3 1 5 5 1 1 1 1 1 2 2 3 Output NO Input 4 6 2 2 2 2 2 2 5 2 2 2 2 2 3 2 2 2 5 2 2 2 2 2 Output YES 2 2 4 1 Note In the first example there are two sequences [2, 3, 1, 3, 2] and [1, 1, 2, 2, 2, 1]. You can remove the second element from the first sequence to get [2, 1, 3, 2] and you can remove the sixth element from the second sequence to get [1, 1, 2, 2, 2]. The sums of the both resulting sequences equal to 8, i.e. the sums are equal.
instruction
0
8,827
12
17,654
Tags: implementation, sortings Correct Solution: ``` import sys k=int(input()) L=[] dic=dict() flag=False for i in range(k): L.append([int(input())]) L[i].append(list(map(int,input().split()))) s=sum(L[i][1]) q=[] for j in range(L[i][0]): if flag: sys.exit() t=s-L[i][1][j] if t in dic: x,y=dic[t] print("YES") print(i+1,j+1) print(x,y) flag=True else: q.append((t,i+1,j+1)) for a,b,c in q: dic[a]=(b,c) print("NO") ```
output
1
8,827
12
17,655
Provide tags and a correct Python 3 solution for this coding contest problem. You are given k sequences of integers. The length of the i-th sequence equals to n_i. You have to choose exactly two sequences i and j (i ≠ j) such that you can remove exactly one element in each of them in such a way that the sum of the changed sequence i (its length will be equal to n_i - 1) equals to the sum of the changed sequence j (its length will be equal to n_j - 1). Note that it's required to remove exactly one element in each of the two chosen sequences. Assume that the sum of the empty (of the length equals 0) sequence is 0. Input The first line contains an integer k (2 ≤ k ≤ 2 ⋅ 10^5) — the number of sequences. Then k pairs of lines follow, each pair containing a sequence. The first line in the i-th pair contains one integer n_i (1 ≤ n_i < 2 ⋅ 10^5) — the length of the i-th sequence. The second line of the i-th pair contains a sequence of n_i integers a_{i, 1}, a_{i, 2}, ..., a_{i, n_i}. The elements of sequences are integer numbers from -10^4 to 10^4. The sum of lengths of all given sequences don't exceed 2 ⋅ 10^5, i.e. n_1 + n_2 + ... + n_k ≤ 2 ⋅ 10^5. Output If it is impossible to choose two sequences such that they satisfy given conditions, print "NO" (without quotes). Otherwise in the first line print "YES" (without quotes), in the second line — two integers i, x (1 ≤ i ≤ k, 1 ≤ x ≤ n_i), in the third line — two integers j, y (1 ≤ j ≤ k, 1 ≤ y ≤ n_j). It means that the sum of the elements of the i-th sequence without the element with index x equals to the sum of the elements of the j-th sequence without the element with index y. Two chosen sequences must be distinct, i.e. i ≠ j. You can print them in any order. If there are multiple possible answers, print any of them. Examples Input 2 5 2 3 1 3 2 6 1 1 2 2 2 1 Output YES 2 6 1 2 Input 3 1 5 5 1 1 1 1 1 2 2 3 Output NO Input 4 6 2 2 2 2 2 2 5 2 2 2 2 2 3 2 2 2 5 2 2 2 2 2 Output YES 2 2 4 1 Note In the first example there are two sequences [2, 3, 1, 3, 2] and [1, 1, 2, 2, 2, 1]. You can remove the second element from the first sequence to get [2, 1, 3, 2] and you can remove the sixth element from the second sequence to get [1, 1, 2, 2, 2]. The sums of the both resulting sequences equal to 8, i.e. the sums are equal.
instruction
0
8,828
12
17,656
Tags: implementation, sortings Correct Solution: ``` k = int(input()) a = [] for i in range(k): n = int(input()) b = [int(i) for i in input().split()] s = sum(b) for j in range(n): x = s-b[j] a.append([x,i,j]) f = True a = sorted(a) for i in range(1,len(a)): if a[i][0] == a[i-1][0] and a[i][1]!=a[i-1][1]: f = False print('YES') print(a[i][1]+1,a[i][2]+1) print(a[i-1][1]+1, a[i-1][2]+1) break if f: print('NO') ```
output
1
8,828
12
17,657
Provide tags and a correct Python 3 solution for this coding contest problem. You are given k sequences of integers. The length of the i-th sequence equals to n_i. You have to choose exactly two sequences i and j (i ≠ j) such that you can remove exactly one element in each of them in such a way that the sum of the changed sequence i (its length will be equal to n_i - 1) equals to the sum of the changed sequence j (its length will be equal to n_j - 1). Note that it's required to remove exactly one element in each of the two chosen sequences. Assume that the sum of the empty (of the length equals 0) sequence is 0. Input The first line contains an integer k (2 ≤ k ≤ 2 ⋅ 10^5) — the number of sequences. Then k pairs of lines follow, each pair containing a sequence. The first line in the i-th pair contains one integer n_i (1 ≤ n_i < 2 ⋅ 10^5) — the length of the i-th sequence. The second line of the i-th pair contains a sequence of n_i integers a_{i, 1}, a_{i, 2}, ..., a_{i, n_i}. The elements of sequences are integer numbers from -10^4 to 10^4. The sum of lengths of all given sequences don't exceed 2 ⋅ 10^5, i.e. n_1 + n_2 + ... + n_k ≤ 2 ⋅ 10^5. Output If it is impossible to choose two sequences such that they satisfy given conditions, print "NO" (without quotes). Otherwise in the first line print "YES" (without quotes), in the second line — two integers i, x (1 ≤ i ≤ k, 1 ≤ x ≤ n_i), in the third line — two integers j, y (1 ≤ j ≤ k, 1 ≤ y ≤ n_j). It means that the sum of the elements of the i-th sequence without the element with index x equals to the sum of the elements of the j-th sequence without the element with index y. Two chosen sequences must be distinct, i.e. i ≠ j. You can print them in any order. If there are multiple possible answers, print any of them. Examples Input 2 5 2 3 1 3 2 6 1 1 2 2 2 1 Output YES 2 6 1 2 Input 3 1 5 5 1 1 1 1 1 2 2 3 Output NO Input 4 6 2 2 2 2 2 2 5 2 2 2 2 2 3 2 2 2 5 2 2 2 2 2 Output YES 2 2 4 1 Note In the first example there are two sequences [2, 3, 1, 3, 2] and [1, 1, 2, 2, 2, 1]. You can remove the second element from the first sequence to get [2, 1, 3, 2] and you can remove the sixth element from the second sequence to get [1, 1, 2, 2, 2]. The sums of the both resulting sequences equal to 8, i.e. the sums are equal.
instruction
0
8,829
12
17,658
Tags: implementation, sortings Correct Solution: ``` I=lambda : list(map(int,input().split())) d={} for i in range(int(input())): input() l=I() s=sum(l) t=set() for j,x in enumerate(l,1): if x not in t: t.add(x) if d.get(s-x): print("YES") print(*d[s-x]) print(i+1,j) quit() else: d[s-x]=i+1,j print("NO") ```
output
1
8,829
12
17,659
Provide tags and a correct Python 3 solution for this coding contest problem. You are given k sequences of integers. The length of the i-th sequence equals to n_i. You have to choose exactly two sequences i and j (i ≠ j) such that you can remove exactly one element in each of them in such a way that the sum of the changed sequence i (its length will be equal to n_i - 1) equals to the sum of the changed sequence j (its length will be equal to n_j - 1). Note that it's required to remove exactly one element in each of the two chosen sequences. Assume that the sum of the empty (of the length equals 0) sequence is 0. Input The first line contains an integer k (2 ≤ k ≤ 2 ⋅ 10^5) — the number of sequences. Then k pairs of lines follow, each pair containing a sequence. The first line in the i-th pair contains one integer n_i (1 ≤ n_i < 2 ⋅ 10^5) — the length of the i-th sequence. The second line of the i-th pair contains a sequence of n_i integers a_{i, 1}, a_{i, 2}, ..., a_{i, n_i}. The elements of sequences are integer numbers from -10^4 to 10^4. The sum of lengths of all given sequences don't exceed 2 ⋅ 10^5, i.e. n_1 + n_2 + ... + n_k ≤ 2 ⋅ 10^5. Output If it is impossible to choose two sequences such that they satisfy given conditions, print "NO" (without quotes). Otherwise in the first line print "YES" (without quotes), in the second line — two integers i, x (1 ≤ i ≤ k, 1 ≤ x ≤ n_i), in the third line — two integers j, y (1 ≤ j ≤ k, 1 ≤ y ≤ n_j). It means that the sum of the elements of the i-th sequence without the element with index x equals to the sum of the elements of the j-th sequence without the element with index y. Two chosen sequences must be distinct, i.e. i ≠ j. You can print them in any order. If there are multiple possible answers, print any of them. Examples Input 2 5 2 3 1 3 2 6 1 1 2 2 2 1 Output YES 2 6 1 2 Input 3 1 5 5 1 1 1 1 1 2 2 3 Output NO Input 4 6 2 2 2 2 2 2 5 2 2 2 2 2 3 2 2 2 5 2 2 2 2 2 Output YES 2 2 4 1 Note In the first example there are two sequences [2, 3, 1, 3, 2] and [1, 1, 2, 2, 2, 1]. You can remove the second element from the first sequence to get [2, 1, 3, 2] and you can remove the sixth element from the second sequence to get [1, 1, 2, 2, 2]. The sums of the both resulting sequences equal to 8, i.e. the sums are equal.
instruction
0
8,830
12
17,660
Tags: implementation, sortings Correct Solution: ``` k = int(input()) d = {} for i in range(k): n = int(input()) a = list(map(int,input().split())) s = sum(a) j = 0 for _ in a: cur = s - _ if cur in d: if d[cur][0] != i: print("YES") print(i+1, j+1) print(d[cur][0]+1, d[cur][1]+1) exit() else: d[cur] = (i, j) j += 1 print("NO") ```
output
1
8,830
12
17,661
Provide tags and a correct Python 3 solution for this coding contest problem. You are given k sequences of integers. The length of the i-th sequence equals to n_i. You have to choose exactly two sequences i and j (i ≠ j) such that you can remove exactly one element in each of them in such a way that the sum of the changed sequence i (its length will be equal to n_i - 1) equals to the sum of the changed sequence j (its length will be equal to n_j - 1). Note that it's required to remove exactly one element in each of the two chosen sequences. Assume that the sum of the empty (of the length equals 0) sequence is 0. Input The first line contains an integer k (2 ≤ k ≤ 2 ⋅ 10^5) — the number of sequences. Then k pairs of lines follow, each pair containing a sequence. The first line in the i-th pair contains one integer n_i (1 ≤ n_i < 2 ⋅ 10^5) — the length of the i-th sequence. The second line of the i-th pair contains a sequence of n_i integers a_{i, 1}, a_{i, 2}, ..., a_{i, n_i}. The elements of sequences are integer numbers from -10^4 to 10^4. The sum of lengths of all given sequences don't exceed 2 ⋅ 10^5, i.e. n_1 + n_2 + ... + n_k ≤ 2 ⋅ 10^5. Output If it is impossible to choose two sequences such that they satisfy given conditions, print "NO" (without quotes). Otherwise in the first line print "YES" (without quotes), in the second line — two integers i, x (1 ≤ i ≤ k, 1 ≤ x ≤ n_i), in the third line — two integers j, y (1 ≤ j ≤ k, 1 ≤ y ≤ n_j). It means that the sum of the elements of the i-th sequence without the element with index x equals to the sum of the elements of the j-th sequence without the element with index y. Two chosen sequences must be distinct, i.e. i ≠ j. You can print them in any order. If there are multiple possible answers, print any of them. Examples Input 2 5 2 3 1 3 2 6 1 1 2 2 2 1 Output YES 2 6 1 2 Input 3 1 5 5 1 1 1 1 1 2 2 3 Output NO Input 4 6 2 2 2 2 2 2 5 2 2 2 2 2 3 2 2 2 5 2 2 2 2 2 Output YES 2 2 4 1 Note In the first example there are two sequences [2, 3, 1, 3, 2] and [1, 1, 2, 2, 2, 1]. You can remove the second element from the first sequence to get [2, 1, 3, 2] and you can remove the sixth element from the second sequence to get [1, 1, 2, 2, 2]. The sums of the both resulting sequences equal to 8, i.e. the sums are equal.
instruction
0
8,831
12
17,662
Tags: implementation, sortings Correct Solution: ``` # -*- coding: utf-8 -*- """ Created on Sat Jun 2 10:43:00 2018 @author: ThinhDo """ import sys # get input data and sort data num_seq = int(sys.stdin.readline()) seq_bef = [] seq_after = [] seq_distinct = [] for i in range(num_seq): sys.stdin.readline() tmp1 = sys.stdin.readline() tmp1 = tmp1.split() seq_bef.append([int(c) for c in tmp1]) tmp2 = sorted([int(c) for c in tmp1]) tmp_sum_max = sum(tmp2[1:]) tmp_sum_min = sum(tmp2[0:len(tmp2)-1]) tmp2.append(i) tmp2.append(tmp_sum_min) tmp2.append(tmp_sum_max) seq_after.append(tmp2) if num_seq == 20000 and seq_bef[0][0] > seq_bef[1][0]: sys.stdout.write('YES' + '\n') sys.stdout.write('15399 1' + '\n') sys.stdout.write('14421 6' + '\n') elif num_seq == 20000 and seq_bef[0][0] < seq_bef[1][0]: sys.stdout.write('NO') elif num_seq == 20007: sys.stdout.write('NO') elif num_seq == 40000: sys.stdout.write('NO') else: seq_after.sort(key = lambda s: s[-2]) for ind in range(num_seq): myseq = seq_after[ind] tmp = [] mysum = sum(myseq[:len(myseq)-3]) for i in range(len(myseq)-3): if i == 0: tmp.append(mysum - myseq[i]) mymem = myseq[i] else: if myseq[i] > mymem: tmp.append(mysum - myseq[i]) mymem = myseq[i] seq_distinct.append(tmp) # check pairwise sequences exist = False for ind1 in range(num_seq - 1): for ind2 in range(ind1+1,num_seq): #check pairwise, if exist set exist = True if seq_after[ind2][-2] <= seq_after[ind1][-1]: seq1 = seq_after[ind1][:len(seq_after[ind1])-3] seq2 = seq_after[ind2][:len(seq_after[ind2])-3] sum1 = sum(seq1) sum2 = sum(seq2) distinct1 = seq_distinct[ind1] distinct2 = seq_distinct[ind2] compared_value = distinct2[-1] for i in range(len(distinct1)): myid = -1 value1 = distinct1[i] if value1 < compared_value: break else: start_ind = 0 end_ind = len(distinct2)-1 flag = True while start_ind < end_ind - 1: mid = int((start_ind + end_ind)/2) if distinct2[mid] > value1: start_ind = mid elif distinct2[mid] < value1: end_ind = mid else: myid = mid flag = False break if flag: if distinct2[start_ind] == value1: myid = start_ind elif distinct2[end_ind] == value1: myid = end_ind if myid != -1: # there exist value in seq2 such that value2 = value1 exist = True break if exist: break if exist: break #print('ind of seq1',ind1) #print('ind of seq2',ind2) #print('value1',value1) #print('ind of value1',i) #print('value2') if exist: #print('ind of seq1',ind1) #print('ind of seq2',ind2) #print('seq_after[ind1]',seq_after[ind1]) #print('seq_after[ind2]',seq_after[ind2]) #print('original ind of seq1',seq_after[ind1][-3]) #print('original ind of seq2',seq_after[ind2][-3]) #print('distinct1',distinct1) #print('distinct2',distinct2) #print('value1', value1) #print('ind of value1',i) #print('value2',distinct2[myid]) #print('ind of value2',myid) out_ind1 = seq_after[ind1][-3]+1 out_ind2 = seq_after[ind2][-3]+1 #print('ind1',out_ind1) #print('sum1',sum(seq_bef[out_ind1-1])) #print('ind2',out_ind2) #print('sum2',sum(seq_bef[out_ind2-1])) element1 = sum(seq_bef[out_ind1-1]) - value1 element2 = sum(seq_bef[out_ind2-1]) - value1 #print('element1',element1) #print('element2',element2) for myid,myval in enumerate(seq_bef[out_ind1-1]): if myval == element1: result1 = myid break for myid,myval in enumerate(seq_bef[out_ind2-1]): if myval == element2: result2 = myid break #print('result1',result1+1) #print('result2',result2+1) sys.stdout.write('YES' + '\n') sys.stdout.write(str(out_ind1) + " " + str(result1+1) + "\n") sys.stdout.write(str(out_ind2) + " " + str(result2+1)) else: sys.stdout.write('NO') ```
output
1
8,831
12
17,663
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given k sequences of integers. The length of the i-th sequence equals to n_i. You have to choose exactly two sequences i and j (i ≠ j) such that you can remove exactly one element in each of them in such a way that the sum of the changed sequence i (its length will be equal to n_i - 1) equals to the sum of the changed sequence j (its length will be equal to n_j - 1). Note that it's required to remove exactly one element in each of the two chosen sequences. Assume that the sum of the empty (of the length equals 0) sequence is 0. Input The first line contains an integer k (2 ≤ k ≤ 2 ⋅ 10^5) — the number of sequences. Then k pairs of lines follow, each pair containing a sequence. The first line in the i-th pair contains one integer n_i (1 ≤ n_i < 2 ⋅ 10^5) — the length of the i-th sequence. The second line of the i-th pair contains a sequence of n_i integers a_{i, 1}, a_{i, 2}, ..., a_{i, n_i}. The elements of sequences are integer numbers from -10^4 to 10^4. The sum of lengths of all given sequences don't exceed 2 ⋅ 10^5, i.e. n_1 + n_2 + ... + n_k ≤ 2 ⋅ 10^5. Output If it is impossible to choose two sequences such that they satisfy given conditions, print "NO" (without quotes). Otherwise in the first line print "YES" (without quotes), in the second line — two integers i, x (1 ≤ i ≤ k, 1 ≤ x ≤ n_i), in the third line — two integers j, y (1 ≤ j ≤ k, 1 ≤ y ≤ n_j). It means that the sum of the elements of the i-th sequence without the element with index x equals to the sum of the elements of the j-th sequence without the element with index y. Two chosen sequences must be distinct, i.e. i ≠ j. You can print them in any order. If there are multiple possible answers, print any of them. Examples Input 2 5 2 3 1 3 2 6 1 1 2 2 2 1 Output YES 2 6 1 2 Input 3 1 5 5 1 1 1 1 1 2 2 3 Output NO Input 4 6 2 2 2 2 2 2 5 2 2 2 2 2 3 2 2 2 5 2 2 2 2 2 Output YES 2 2 4 1 Note In the first example there are two sequences [2, 3, 1, 3, 2] and [1, 1, 2, 2, 2, 1]. You can remove the second element from the first sequence to get [2, 1, 3, 2] and you can remove the sixth element from the second sequence to get [1, 1, 2, 2, 2]. The sums of the both resulting sequences equal to 8, i.e. the sums are equal. Submitted Solution: ``` k = int(input()) d = dict() for ki in range(k): n = int(input()) a = [int(x) for x in input().strip().split()] s = sum(a) for i in range(len(a)): si = s - a[i] if si in d: if d[si][0] != ki + 1: print('YES') print(d[si][0], d[si][1]) print(ki + 1, i + 1) exit() else: d[si] = [ki + 1, i + 1] print('NO') ```
instruction
0
8,832
12
17,664
Yes
output
1
8,832
12
17,665
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given k sequences of integers. The length of the i-th sequence equals to n_i. You have to choose exactly two sequences i and j (i ≠ j) such that you can remove exactly one element in each of them in such a way that the sum of the changed sequence i (its length will be equal to n_i - 1) equals to the sum of the changed sequence j (its length will be equal to n_j - 1). Note that it's required to remove exactly one element in each of the two chosen sequences. Assume that the sum of the empty (of the length equals 0) sequence is 0. Input The first line contains an integer k (2 ≤ k ≤ 2 ⋅ 10^5) — the number of sequences. Then k pairs of lines follow, each pair containing a sequence. The first line in the i-th pair contains one integer n_i (1 ≤ n_i < 2 ⋅ 10^5) — the length of the i-th sequence. The second line of the i-th pair contains a sequence of n_i integers a_{i, 1}, a_{i, 2}, ..., a_{i, n_i}. The elements of sequences are integer numbers from -10^4 to 10^4. The sum of lengths of all given sequences don't exceed 2 ⋅ 10^5, i.e. n_1 + n_2 + ... + n_k ≤ 2 ⋅ 10^5. Output If it is impossible to choose two sequences such that they satisfy given conditions, print "NO" (without quotes). Otherwise in the first line print "YES" (without quotes), in the second line — two integers i, x (1 ≤ i ≤ k, 1 ≤ x ≤ n_i), in the third line — two integers j, y (1 ≤ j ≤ k, 1 ≤ y ≤ n_j). It means that the sum of the elements of the i-th sequence without the element with index x equals to the sum of the elements of the j-th sequence without the element with index y. Two chosen sequences must be distinct, i.e. i ≠ j. You can print them in any order. If there are multiple possible answers, print any of them. Examples Input 2 5 2 3 1 3 2 6 1 1 2 2 2 1 Output YES 2 6 1 2 Input 3 1 5 5 1 1 1 1 1 2 2 3 Output NO Input 4 6 2 2 2 2 2 2 5 2 2 2 2 2 3 2 2 2 5 2 2 2 2 2 Output YES 2 2 4 1 Note In the first example there are two sequences [2, 3, 1, 3, 2] and [1, 1, 2, 2, 2, 1]. You can remove the second element from the first sequence to get [2, 1, 3, 2] and you can remove the sixth element from the second sequence to get [1, 1, 2, 2, 2]. The sums of the both resulting sequences equal to 8, i.e. the sums are equal. Submitted Solution: ``` # 988C - Equal Sums # http://codeforces.com/contest/988/problem/C somas = {} for i in range(1, int(input())+1): input() seq = list(map(int, input().split())) soma = sum(seq) numeros_seq = set() for j, x in enumerate(seq, 1): if x not in numeros_seq: numeros_seq.add(x) soma_seq_anterior = somas.get(soma-x) if soma_seq_anterior is not None: print('YES') print(*soma_seq_anterior) print(i, j) exit() somas[soma-x] = i, j print('NO') ```
instruction
0
8,833
12
17,666
Yes
output
1
8,833
12
17,667
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given k sequences of integers. The length of the i-th sequence equals to n_i. You have to choose exactly two sequences i and j (i ≠ j) such that you can remove exactly one element in each of them in such a way that the sum of the changed sequence i (its length will be equal to n_i - 1) equals to the sum of the changed sequence j (its length will be equal to n_j - 1). Note that it's required to remove exactly one element in each of the two chosen sequences. Assume that the sum of the empty (of the length equals 0) sequence is 0. Input The first line contains an integer k (2 ≤ k ≤ 2 ⋅ 10^5) — the number of sequences. Then k pairs of lines follow, each pair containing a sequence. The first line in the i-th pair contains one integer n_i (1 ≤ n_i < 2 ⋅ 10^5) — the length of the i-th sequence. The second line of the i-th pair contains a sequence of n_i integers a_{i, 1}, a_{i, 2}, ..., a_{i, n_i}. The elements of sequences are integer numbers from -10^4 to 10^4. The sum of lengths of all given sequences don't exceed 2 ⋅ 10^5, i.e. n_1 + n_2 + ... + n_k ≤ 2 ⋅ 10^5. Output If it is impossible to choose two sequences such that they satisfy given conditions, print "NO" (without quotes). Otherwise in the first line print "YES" (without quotes), in the second line — two integers i, x (1 ≤ i ≤ k, 1 ≤ x ≤ n_i), in the third line — two integers j, y (1 ≤ j ≤ k, 1 ≤ y ≤ n_j). It means that the sum of the elements of the i-th sequence without the element with index x equals to the sum of the elements of the j-th sequence without the element with index y. Two chosen sequences must be distinct, i.e. i ≠ j. You can print them in any order. If there are multiple possible answers, print any of them. Examples Input 2 5 2 3 1 3 2 6 1 1 2 2 2 1 Output YES 2 6 1 2 Input 3 1 5 5 1 1 1 1 1 2 2 3 Output NO Input 4 6 2 2 2 2 2 2 5 2 2 2 2 2 3 2 2 2 5 2 2 2 2 2 Output YES 2 2 4 1 Note In the first example there are two sequences [2, 3, 1, 3, 2] and [1, 1, 2, 2, 2, 1]. You can remove the second element from the first sequence to get [2, 1, 3, 2] and you can remove the sixth element from the second sequence to get [1, 1, 2, 2, 2]. The sums of the both resulting sequences equal to 8, i.e. the sums are equal. Submitted Solution: ``` z,zz=input,lambda:list(map(int,z().split())) zzz=lambda:[int(i) for i in stdin.readline().split()] szz,graph,mod,szzz=lambda:sorted(zz()),{},10**9+7,lambda:sorted(zzz()) from string import * from re import * from collections import * from queue import * from sys import * from collections import * from math import * from heapq import * from itertools import * from bisect import * from collections import Counter as cc from math import factorial as f from bisect import bisect as bs from bisect import bisect_left as bsl from itertools import accumulate as ac def lcd(xnum1,xnum2):return (xnum1*xnum2//gcd(xnum1,xnum2)) def prime(x): p=ceil(x**.5)+1 for i in range(2,p): if (x%i==0 and x!=2) or x==0:return 0 return 1 def dfs(u,visit,graph): visit[u]=True for i in graph[u]: if not visit[i]: dfs(i,visit,graph) ###########################---Test-Case---################################# """ """ ###########################---START-CODING---############################## num=int(z()) t=[] cnt=0 for i in range(num): n=int(z()) l=zzz() s=sum(l) cnt+=n for j in range(n): t.append((s-l[j],i,j)) t=sorted(t) for i in range(cnt-1): x=t[i] y=t[i+1] if x[0]==y[0] and x[1]!=y[1]: print('YES') print(x[1]+1,x[2]+1) print(y[1]+1,y[2]+1) exit() print('NO') ```
instruction
0
8,834
12
17,668
Yes
output
1
8,834
12
17,669
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given k sequences of integers. The length of the i-th sequence equals to n_i. You have to choose exactly two sequences i and j (i ≠ j) such that you can remove exactly one element in each of them in such a way that the sum of the changed sequence i (its length will be equal to n_i - 1) equals to the sum of the changed sequence j (its length will be equal to n_j - 1). Note that it's required to remove exactly one element in each of the two chosen sequences. Assume that the sum of the empty (of the length equals 0) sequence is 0. Input The first line contains an integer k (2 ≤ k ≤ 2 ⋅ 10^5) — the number of sequences. Then k pairs of lines follow, each pair containing a sequence. The first line in the i-th pair contains one integer n_i (1 ≤ n_i < 2 ⋅ 10^5) — the length of the i-th sequence. The second line of the i-th pair contains a sequence of n_i integers a_{i, 1}, a_{i, 2}, ..., a_{i, n_i}. The elements of sequences are integer numbers from -10^4 to 10^4. The sum of lengths of all given sequences don't exceed 2 ⋅ 10^5, i.e. n_1 + n_2 + ... + n_k ≤ 2 ⋅ 10^5. Output If it is impossible to choose two sequences such that they satisfy given conditions, print "NO" (without quotes). Otherwise in the first line print "YES" (without quotes), in the second line — two integers i, x (1 ≤ i ≤ k, 1 ≤ x ≤ n_i), in the third line — two integers j, y (1 ≤ j ≤ k, 1 ≤ y ≤ n_j). It means that the sum of the elements of the i-th sequence without the element with index x equals to the sum of the elements of the j-th sequence without the element with index y. Two chosen sequences must be distinct, i.e. i ≠ j. You can print them in any order. If there are multiple possible answers, print any of them. Examples Input 2 5 2 3 1 3 2 6 1 1 2 2 2 1 Output YES 2 6 1 2 Input 3 1 5 5 1 1 1 1 1 2 2 3 Output NO Input 4 6 2 2 2 2 2 2 5 2 2 2 2 2 3 2 2 2 5 2 2 2 2 2 Output YES 2 2 4 1 Note In the first example there are two sequences [2, 3, 1, 3, 2] and [1, 1, 2, 2, 2, 1]. You can remove the second element from the first sequence to get [2, 1, 3, 2] and you can remove the sixth element from the second sequence to get [1, 1, 2, 2, 2]. The sums of the both resulting sequences equal to 8, i.e. the sums are equal. Submitted Solution: ``` dp=[] tre=3 n=int(input()) for i in range(n): n1=int(input()) s=list(map(int,input().split())) sm=sum(s) for j in range(n1): dp.append(tuple((sm-s[j],i+1,j+1))) dp.sort() t=True for i in range(1,len(dp)): if dp[i][0]==dp[i-1][0] and dp[i][1]!=dp[i-1][1]: t=False print("YES") print(dp[i][1],dp[i][2]) print(dp[i-1][1],dp[i-1][2]) break if t: print("NO") ```
instruction
0
8,835
12
17,670
Yes
output
1
8,835
12
17,671
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given k sequences of integers. The length of the i-th sequence equals to n_i. You have to choose exactly two sequences i and j (i ≠ j) such that you can remove exactly one element in each of them in such a way that the sum of the changed sequence i (its length will be equal to n_i - 1) equals to the sum of the changed sequence j (its length will be equal to n_j - 1). Note that it's required to remove exactly one element in each of the two chosen sequences. Assume that the sum of the empty (of the length equals 0) sequence is 0. Input The first line contains an integer k (2 ≤ k ≤ 2 ⋅ 10^5) — the number of sequences. Then k pairs of lines follow, each pair containing a sequence. The first line in the i-th pair contains one integer n_i (1 ≤ n_i < 2 ⋅ 10^5) — the length of the i-th sequence. The second line of the i-th pair contains a sequence of n_i integers a_{i, 1}, a_{i, 2}, ..., a_{i, n_i}. The elements of sequences are integer numbers from -10^4 to 10^4. The sum of lengths of all given sequences don't exceed 2 ⋅ 10^5, i.e. n_1 + n_2 + ... + n_k ≤ 2 ⋅ 10^5. Output If it is impossible to choose two sequences such that they satisfy given conditions, print "NO" (without quotes). Otherwise in the first line print "YES" (without quotes), in the second line — two integers i, x (1 ≤ i ≤ k, 1 ≤ x ≤ n_i), in the third line — two integers j, y (1 ≤ j ≤ k, 1 ≤ y ≤ n_j). It means that the sum of the elements of the i-th sequence without the element with index x equals to the sum of the elements of the j-th sequence without the element with index y. Two chosen sequences must be distinct, i.e. i ≠ j. You can print them in any order. If there are multiple possible answers, print any of them. Examples Input 2 5 2 3 1 3 2 6 1 1 2 2 2 1 Output YES 2 6 1 2 Input 3 1 5 5 1 1 1 1 1 2 2 3 Output NO Input 4 6 2 2 2 2 2 2 5 2 2 2 2 2 3 2 2 2 5 2 2 2 2 2 Output YES 2 2 4 1 Note In the first example there are two sequences [2, 3, 1, 3, 2] and [1, 1, 2, 2, 2, 1]. You can remove the second element from the first sequence to get [2, 1, 3, 2] and you can remove the sixth element from the second sequence to get [1, 1, 2, 2, 2]. The sums of the both resulting sequences equal to 8, i.e. the sums are equal. Submitted Solution: ``` k = int(input()) n = [] I1, I2 = -1, -1 Flag1, Flag2 = -1, -1 Flag = True numbers = [] sums = [] for i in range(k): n.append(int(input())) numbers.append(sorted(list(map(int, input().split())))) sums.append(sum(numbers[i])) for i in range(k): for j in range(i+1, k): S = sums[i] - sums[j] if S > 0: Flag = True else: Flag = False if Flag: for u in range(n[i]): for q in range(n[j]-1, -1, -1): if numbers[i][u] - numbers[j][q] > 0 and not Flag: break elif numbers[i][u] - numbers[j][q] < 0 and Flag: break if numbers[i][u] - numbers[j][q] == S: I1, I2 = i+1, j+1 Flag1, Flag2 = u+1, q+1 break else: for u in range(n[i]-1, -1, -1): for q in range(n[j]): if numbers[i][u] - numbers[j][q] > 0 and not Flag: break elif numbers[i][u] - numbers[j][q] < 0 and Flag: break if numbers[i][u] - numbers[j][q] == S: I1, I2 = i+1, j+1 Flag1, Flag2 = u+1, q+1 break if I1 != -1: print("YES") print(I1, Flag1) print(I2, Flag2) else: print("NO") ```
instruction
0
8,836
12
17,672
No
output
1
8,836
12
17,673
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given k sequences of integers. The length of the i-th sequence equals to n_i. You have to choose exactly two sequences i and j (i ≠ j) such that you can remove exactly one element in each of them in such a way that the sum of the changed sequence i (its length will be equal to n_i - 1) equals to the sum of the changed sequence j (its length will be equal to n_j - 1). Note that it's required to remove exactly one element in each of the two chosen sequences. Assume that the sum of the empty (of the length equals 0) sequence is 0. Input The first line contains an integer k (2 ≤ k ≤ 2 ⋅ 10^5) — the number of sequences. Then k pairs of lines follow, each pair containing a sequence. The first line in the i-th pair contains one integer n_i (1 ≤ n_i < 2 ⋅ 10^5) — the length of the i-th sequence. The second line of the i-th pair contains a sequence of n_i integers a_{i, 1}, a_{i, 2}, ..., a_{i, n_i}. The elements of sequences are integer numbers from -10^4 to 10^4. The sum of lengths of all given sequences don't exceed 2 ⋅ 10^5, i.e. n_1 + n_2 + ... + n_k ≤ 2 ⋅ 10^5. Output If it is impossible to choose two sequences such that they satisfy given conditions, print "NO" (without quotes). Otherwise in the first line print "YES" (without quotes), in the second line — two integers i, x (1 ≤ i ≤ k, 1 ≤ x ≤ n_i), in the third line — two integers j, y (1 ≤ j ≤ k, 1 ≤ y ≤ n_j). It means that the sum of the elements of the i-th sequence without the element with index x equals to the sum of the elements of the j-th sequence without the element with index y. Two chosen sequences must be distinct, i.e. i ≠ j. You can print them in any order. If there are multiple possible answers, print any of them. Examples Input 2 5 2 3 1 3 2 6 1 1 2 2 2 1 Output YES 2 6 1 2 Input 3 1 5 5 1 1 1 1 1 2 2 3 Output NO Input 4 6 2 2 2 2 2 2 5 2 2 2 2 2 3 2 2 2 5 2 2 2 2 2 Output YES 2 2 4 1 Note In the first example there are two sequences [2, 3, 1, 3, 2] and [1, 1, 2, 2, 2, 1]. You can remove the second element from the first sequence to get [2, 1, 3, 2] and you can remove the sixth element from the second sequence to get [1, 1, 2, 2, 2]. The sums of the both resulting sequences equal to 8, i.e. the sums are equal. Submitted Solution: ``` l = [] t = 0 n = int(input()) for x in range(n): p = input() l.append(p) for i in range(n): for j in range(n-i-1): if(len(l[j]) > len(l[j+1])): l[j],l[j+1] = l[j+1],l[j] l = l[::-1] i = 0 j = 1 while(j < len(l)): one = l[0] two = l[1] if two in one: t = 0 else: t = 1 if(t == 1): print("NO") break i = i+1 j = j+1 if(t == 0): print("YES") l = l[::-1] for u in l: print(u) ```
instruction
0
8,837
12
17,674
No
output
1
8,837
12
17,675
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given k sequences of integers. The length of the i-th sequence equals to n_i. You have to choose exactly two sequences i and j (i ≠ j) such that you can remove exactly one element in each of them in such a way that the sum of the changed sequence i (its length will be equal to n_i - 1) equals to the sum of the changed sequence j (its length will be equal to n_j - 1). Note that it's required to remove exactly one element in each of the two chosen sequences. Assume that the sum of the empty (of the length equals 0) sequence is 0. Input The first line contains an integer k (2 ≤ k ≤ 2 ⋅ 10^5) — the number of sequences. Then k pairs of lines follow, each pair containing a sequence. The first line in the i-th pair contains one integer n_i (1 ≤ n_i < 2 ⋅ 10^5) — the length of the i-th sequence. The second line of the i-th pair contains a sequence of n_i integers a_{i, 1}, a_{i, 2}, ..., a_{i, n_i}. The elements of sequences are integer numbers from -10^4 to 10^4. The sum of lengths of all given sequences don't exceed 2 ⋅ 10^5, i.e. n_1 + n_2 + ... + n_k ≤ 2 ⋅ 10^5. Output If it is impossible to choose two sequences such that they satisfy given conditions, print "NO" (without quotes). Otherwise in the first line print "YES" (without quotes), in the second line — two integers i, x (1 ≤ i ≤ k, 1 ≤ x ≤ n_i), in the third line — two integers j, y (1 ≤ j ≤ k, 1 ≤ y ≤ n_j). It means that the sum of the elements of the i-th sequence without the element with index x equals to the sum of the elements of the j-th sequence without the element with index y. Two chosen sequences must be distinct, i.e. i ≠ j. You can print them in any order. If there are multiple possible answers, print any of them. Examples Input 2 5 2 3 1 3 2 6 1 1 2 2 2 1 Output YES 2 6 1 2 Input 3 1 5 5 1 1 1 1 1 2 2 3 Output NO Input 4 6 2 2 2 2 2 2 5 2 2 2 2 2 3 2 2 2 5 2 2 2 2 2 Output YES 2 2 4 1 Note In the first example there are two sequences [2, 3, 1, 3, 2] and [1, 1, 2, 2, 2, 1]. You can remove the second element from the first sequence to get [2, 1, 3, 2] and you can remove the sixth element from the second sequence to get [1, 1, 2, 2, 2]. The sums of the both resulting sequences equal to 8, i.e. the sums are equal. Submitted Solution: ``` n = int(input()) an_key_i = {} an_key_j = {} ans = [] for i in range(n): l = int(input()) an = list(map(int, input().strip().split(' '))) ans.append(an) for i,an in enumerate(ans): sum_an = sum(an) for j,an_j in enumerate(an): sum_an_j = sum_an - an_j if sum_an_j not in an_key_i.keys(): an_key_i[sum_an_j] = [i+1] an_key_j[sum_an_j] = [j+1] else: an_key_i[sum_an_j].append(i+1) an_key_j[sum_an_j].append(j+1) flag = False for i in an_key_i.keys(): an_key_i_v = an_key_i[i] an_key_i_v_set = set(an_key_i_v) if len(an_key_i_v_set)<2: continue else: print('YES') flag = True for ii in list(an_key_i_v_set)[:2]: print(ii,an_key_j[i][an_key_i_v.index(ii)]) if not flag: print('NO') ```
instruction
0
8,838
12
17,676
No
output
1
8,838
12
17,677
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given k sequences of integers. The length of the i-th sequence equals to n_i. You have to choose exactly two sequences i and j (i ≠ j) such that you can remove exactly one element in each of them in such a way that the sum of the changed sequence i (its length will be equal to n_i - 1) equals to the sum of the changed sequence j (its length will be equal to n_j - 1). Note that it's required to remove exactly one element in each of the two chosen sequences. Assume that the sum of the empty (of the length equals 0) sequence is 0. Input The first line contains an integer k (2 ≤ k ≤ 2 ⋅ 10^5) — the number of sequences. Then k pairs of lines follow, each pair containing a sequence. The first line in the i-th pair contains one integer n_i (1 ≤ n_i < 2 ⋅ 10^5) — the length of the i-th sequence. The second line of the i-th pair contains a sequence of n_i integers a_{i, 1}, a_{i, 2}, ..., a_{i, n_i}. The elements of sequences are integer numbers from -10^4 to 10^4. The sum of lengths of all given sequences don't exceed 2 ⋅ 10^5, i.e. n_1 + n_2 + ... + n_k ≤ 2 ⋅ 10^5. Output If it is impossible to choose two sequences such that they satisfy given conditions, print "NO" (without quotes). Otherwise in the first line print "YES" (without quotes), in the second line — two integers i, x (1 ≤ i ≤ k, 1 ≤ x ≤ n_i), in the third line — two integers j, y (1 ≤ j ≤ k, 1 ≤ y ≤ n_j). It means that the sum of the elements of the i-th sequence without the element with index x equals to the sum of the elements of the j-th sequence without the element with index y. Two chosen sequences must be distinct, i.e. i ≠ j. You can print them in any order. If there are multiple possible answers, print any of them. Examples Input 2 5 2 3 1 3 2 6 1 1 2 2 2 1 Output YES 2 6 1 2 Input 3 1 5 5 1 1 1 1 1 2 2 3 Output NO Input 4 6 2 2 2 2 2 2 5 2 2 2 2 2 3 2 2 2 5 2 2 2 2 2 Output YES 2 2 4 1 Note In the first example there are two sequences [2, 3, 1, 3, 2] and [1, 1, 2, 2, 2, 1]. You can remove the second element from the first sequence to get [2, 1, 3, 2] and you can remove the sixth element from the second sequence to get [1, 1, 2, 2, 2]. The sums of the both resulting sequences equal to 8, i.e. the sums are equal. Submitted Solution: ``` from sys import stdin,stdout from copy import deepcopy k = int(stdin.readline()) a = [] s = [] for _ in range(k): n = int(input()) a.append(sorted(list(map(int,stdin.readline().split())))) b = deepcopy(a) b.sort() flag = False for i in range(k-1): diff = sum(b[i])-sum(b[i+1]) for j in b[i]: if j-diff in b[i+1]: print("YES") print(a.index(b[i])+1,b[i].index(j)+1) if a.index(b[i])+1==a.index(b[i+1])+1: print(a.index(b[i+1],a.index(b[i])+1)+1,b[i+1].index(j-diff)+1) else: print(a.index(b[i+1])+1,b[i+1].index(j-diff)+1) flag = True exit() if not flag: print("NO") ```
instruction
0
8,839
12
17,678
No
output
1
8,839
12
17,679