description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) u = [int(i) for i in input().split()] cnt = [(0) for i in range(10)] ans = 0 for i in range(n): cnt[u[i] - 1] += 1 cm = max(cnt) c0 = cnt.count(0) gen = cm - 1 if cnt.count(gen) == 10 - c0 - 1: ans = i + 1 else: temp = [] for x in cnt: if x != 0: temp.append(x) if len(set(temp)) == 2 and temp.count(1) == 1: ans = i + 1 elif len(set(temp)) == 1 and temp.count(1) > 0: ans = i + 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
import sys n = int(input()) nums = list(map(int, input().split())) eleCount = {} possibleMax = 1 for i in range(n): if nums[i] in eleCount.keys(): eleCount[nums[i]] += 1 if len(eleCount.keys()) == 1: possibleMax = i + 1 else: x = list(eleCount.values()) x.sort() if x[0] == 1 and x[-1] == 1: possibleMax = i + 1 elif x[0] == 1 and x[1] != 1: if len(set(x[1:])) == 1: possibleMax = i + 1 else: x[-1] = x[-1] - 1 if len(set(x)) == 1: possibleMax = i + 1 else: eleCount[nums[i]] = 1 if len(eleCount.keys()) == 1: possibleMax = i + 1 else: x = list(eleCount.values()) x.sort() if x[0] == 1 and x[-1] == 1: possibleMax = i + 1 elif x[0] == 1 and x[1] != 1: if len(set(x[1:])) == 1: possibleMax = i + 1 else: x[-1] = x[-1] - 1 if len(set(x)) == 1: possibleMax = i + 1 print(possibleMax)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
def equal(a): l = len(a) for i in range(1, l): if a[i - 1] != a[i]: return 0 return 1 def equal1(a): b = a[:] if b[0] <= b[1]: try: posi1 = b.index(b[0] + 1) b[posi1] = b[0] return equal(b) except ValueError: return 0 else: b[0] = b[0] - 1 return equal(b) def equalinlist(a): l = len(a) if l == 1: return 1 b = a[:] try: posi = b.index(1) if posi > 0: b[posi] = b[posi - 1] else: b[posi] = b[posi + 1] if equal(b): return 1 if equal(b) == 0: b[posi] = 1 return equal1(b) except ValueError: return equal1(b) n = int(input()) u = list(map(int, input().split())) a = [1] b = [u[0]] x = 1 for i in range(1, n): try: posi = b.index(u[i]) a[posi] += 1 except ValueError: b.append(u[i]) a.append(1) if equalinlist(a): x = i + 1 print(x)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) a = list(map(int, input().split())) def ok(pos): freq = list(filter(lambda x: x, count[pos])) if len(freq) == 1: return True s = set(freq) if len(s) == 1: return freq[0] == 1 if len(s) > 2: return False _min, _max = sorted(s) return ( _max - _min == 1 and freq.count(_max) == 1 or _min == 1 and freq.count(_min) == 1 ) count = [[(0) for _ in range(11)] for _ in range(n)] for i in range(n): count[i] = count[i - 1][:] count[i][a[i]] += 1 for i in range(n - 1, -1, -1): if ok(i): print(i + 1) exit()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) a = list(map(int, input().split())) gg = [0] * 15 ans = 1 for i in range(n): gg[a[i]] += 1 cnt1 = 0 s = set([]) for j in gg: if not j: continue if j == 1: cnt1 += 1 else: s.add(j) mkc = [] for j in s: mkc.append(j) if len(s) == 0 or i + 1 in gg: ans = max(ans, i + 1) if len(s) == 1 and cnt1 == 1: ans = max(ans, i + 1) if len(s) == 1 and cnt1 == i - 1 and mkc[0] == 2: ans = max(ans, i + 1) if len(s) == 2 and cnt1 == 0: mkc.sort() if mkc[1] == mkc[0] + 1: bak = 0 for j in gg: if j == mkc[1]: bak += 1 if bak == 1: ans = max(ans, i + 1) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST FOR VAR VAR IF VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
import sys n = int(sys.stdin.readline().strip()) u = sys.stdin.readline().split() for i in range(len(u)): u[i] = int(u[i]) freq = [0] * 100005 freq_num = [0] * 100005 ans = 1 for i in range(1, len(u) + 1): freq[u[i - 1]] += 1 freq_num[freq[u[i - 1]]] += 1 if not i == n and freq_num[freq[u[i - 1]]] * freq[u[i - 1]] == i: ans = i + 1 elif freq_num[freq[u[i - 1]]] * freq[u[i - 1]] == i - 1: ans = i print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
NUMS = [(0) for i in range(10)] def check(nums): maxi = max(nums) category = nums.count(maxi) if category == 1: oneof = 0 for n in nums: if n == maxi: continue if n == 0: continue if oneof != 0 and (n != oneof or n != maxi - 1): return False oneof = n return True elif maxi != 1: if nums.count(1) != 1: return False if nums.count(0) != len(nums) - category - 1: return False return True else: return True n = int(input()) colors = list(map(int, input().split())) res = 0 for i in range(n): NUMS[colors[i] - 1] += 1 if check(NUMS): res = i + 1 print(res)
ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR IF VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR VAR RETURN NUMBER IF VAR NUMBER IF FUNC_CALL VAR NUMBER NUMBER RETURN NUMBER IF FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) A = list(map(int, input().split())) f = [0] * int(100000.0 + 1) cnt = [0] * int(100000.0 + 1) mx = 0 max_idx = 0 valid = False for i in range(1, len(A) + 1): a = A[i - 1] cnt[f[a]] -= 1 f[a] += 1 cnt[f[a]] += 1 mx = max(mx, f[a]) if cnt[1] == i: valid = True elif cnt[i] == 1: valid = True elif cnt[1] == 1 and cnt[mx] * mx == i - 1: valid = True elif cnt[mx] == 1 and cnt[mx - 1] * (mx - 1) == i - mx: valid = True else: valid = False if valid: max_idx = i print(max_idx)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input().strip()) numbers = tuple(map(int, input().strip().split())) occ = [(0) for i in range(100001)] places = [] d = {} for k in range(0, len(numbers)): occ[numbers[k]] += 1 if occ[numbers[k]] - 1 in d.keys(): if d[occ[numbers[k]] - 1] == 1: d.pop(occ[numbers[k]] - 1) else: d[occ[numbers[k]] - 1] -= 1 if occ[numbers[k]] in d.keys(): d[occ[numbers[k]]] += 1 else: d[occ[numbers[k]]] = 1 if len(d.keys()) == 2 and 1 in d.keys() and 2 in d.keys(): if d[2] == 1: places.append(k) if d[1] == 1: places.append(k) elif len(d.keys()) == 2 and 1 in d.keys(): if d[1] == 1: places.append(k) elif len(d.keys()) == 2: lll = list(d.keys()) if lll[0] - lll[1] == 1: if d[lll[0]] == 1: places.append(k) if lll[1] - lll[0] == 1: if d[lll[1]] == 1: places.append(k) elif len(d.keys()) == 1: jo = 1 for i in d.keys(): if d[i] == 1: places.append(k) jo = 0 if jo: if 1 in d.keys(): places.append(k) if len(places) > 0: print(places[-1] + 1) else: print(0)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR IF VAR BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR NUMBER FUNC_CALL VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR IF NUMBER FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) a = [(int(x) - 1) for x in input().split()] counts = [0] * 10 for ind, i in enumerate(a): counts[i] += 1 k = sorted([i for i in counts if i]) if ( len(k) == 1 or k[0] == k[-2] and k[-1] - k[-2] == 1 or k[0] == 1 and k[1] == k[-1] ): best = ind print(best + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) a = list(map(int, input().split())) dic = {a[0]: 1} su = 1 kq = 1 for i in range(1, n): if dic.get(a[i], 0) == 0: dic.update({a[i]: 1}) else: dic[a[i]] += 1 su += 1 cach = len(list(dic.keys())) c = 0 c_ = 0 for z in list(dic.keys()): if cach == 1: continue if cach == su: kq = su continue if dic[z] == (su - 1) / (cach - 1): c += 1 if dic[z] == (su - 1) / cach: c_ += 1 if c == cach - 1 or c_ == cach - 1: kq = su print(kq)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR DICT VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
def get_num(): return int(input()) def print_arr(arr: list): for val in arr: print(val) def read_num_list(): num_str = input() return [int(v) for v in num_str.split(" ")] def read_nums(k): lst = [] for _ in range(k): lst.append(get_num()) return lst n = get_num() colors = read_num_list() def is_streak(counts: dict) -> bool: count_len = len(counts) if count_len == 1: k = list(counts.keys())[0] if k == 1 or len(counts[k]) == 1: return True else: return False elif count_len == 2: k1, k2 = sorted(list(counts.keys())) if not (len(counts[k1]) == 1 or len(counts[k2])): return False if k2 - k1 == 1 and len(counts[k2]) == 1 or k1 == 1 and len(counts[1]) == 1: return True else: return False else: return False if n <= 2: print(n) else: streak = -1 possible_colors = set(range(1, 11)) seen = {c: (0) for c in possible_colors} inverted_seen = {} for i, color in enumerate(colors): prev_count = seen[color] new_count = prev_count + 1 seen[color] = new_count if prev_count != 0: inverted_seen[prev_count].remove(color) if len(inverted_seen[prev_count]) == 0: inverted_seen.pop(prev_count) if new_count not in inverted_seen: inverted_seen[new_count] = set() inverted_seen[new_count].add(color) if is_streak(inverted_seen): streak = i + 1 print(max(streak, 2))
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR RETURN NUMBER IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER RETURN NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) a = list(map(int, input().split())) b = [(0) for i in range(10)] pos = 1 for i in range(n): b[a[i] - 1] += 1 c = [] for j in range(10): if b[j] > 0: c.append(b[j]) c.sort() if ( c[len(c) - 1] == i + 1 or c[len(c) - 1] == c[len(c) - 2] + 1 and c[len(c) - 2] == c[0] or c[len(c) - 1] == c[1] and c[0] == 1 ): pos = i + 1 print(pos)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
counts = {} rcount = {} input() m = 0 for i, x in enumerate(map(int, input().split())): if x in counts: rcount[counts[x]].remove(x) if not rcount[counts[x]]: del rcount[counts[x]] counts[x] += 1 if counts[x] not in rcount: rcount[counts[x]] = set() rcount[counts[x]].add(x) else: counts[x] = 1 if 1 not in rcount: rcount[1] = set() rcount[1].add(x) keys = list(rcount) if ( len(keys) == 2 and max(keys) - min(keys) == 1 and len(rcount[max(keys)]) == 1 or len(keys) == 2 and min(keys) == 1 and len(rcount[1]) == 1 or len(keys) == 1 and (len(rcount[keys[0]]) == 1 or keys[0] == 1) ): m = max(m, i) print(m + 1)
ASSIGN VAR DICT ASSIGN VAR DICT EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER IF NUMBER VAR ASSIGN VAR NUMBER FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
d = {} n = int(input()) x = c = z = m = p = 0 y = 1 a = list(map(int, input().split())) for i in range(n): d[a[i]] = k = d.get(a[i], 0) + 1 if k == 1: z += y m += 1 p += 1 elif k == 2: p -= 1 if k == y + 1: if c == 0: z = y * m - i c = 1 else: y = k z = y * (m - 1) - i c = 0 elif k == y + 2: y = k - 1 z = y * m - i c = 1 else: z -= 1 if z == 0: if c == 1 or p == 1 or y == 1: x = i print(x + 1)
ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) u = input().split() d = {} for i in set(u): d[i] = u.count(i) for i in range(1, n + 1): l = list(set(dict.values(d))) if len(l) == 2: warn = 0 warning = 0 for j in d: if d[j] == max(l): warning += 1 elif d[j] == min(l): warn += 1 if warning == 1 and max(l) - min(l) == 1 or warn == 1 and min(l) == 1: res = n - i + 1 print(res) exit(0) elif len(l) == 1 and (l[0] == 1 or len(dict.keys(d)) == 1): res = n - i + 1 print(res) exit(0) d[u[-i]] -= 1 if d[u[-i]] == 0: d.pop(u[-i])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) u = list(map(int, input().split())) nu = 100000 cl = [0] * nu cl[u[0] - 1] += 1 md = 1 mcl = 1 cmcl = 1 clcl = 0 cocl = 1 czcl = nu - 1 for i in range(1, n): cc = u[i] - 1 cl[cc] += 1 if cl[cc] == 1: czcl -= 1 cocl += 1 if cl[cc] == 2: cocl -= 1 if cl[cc] == mcl: cmcl += 1 clcl = max(0, clcl - 1) if cl[cc] == mcl - 1: clcl += 1 if cl[cc] > mcl: clcl = cmcl - 1 mcl = cl[cc] cmcl = 1 if ( cmcl == 1 and clcl == nu - 1 - czcl or cocl == 1 and cmcl == nu - 1 - czcl or cocl == nu - czcl ): md = i + 1 print(md)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) p = input().rstrip().split(" ") l = [0] * 10 for i in range(0, len(p)): l[int(p[i]) - 1] += 1 T = list(set(l)) T.sort(key=int, reverse=True) if 0 in T: if len(T) == 2: if T[0] == 1: q = i elif l.count(T[0]) == 1: q = i elif len(T) == 3: A = T[0] B = T[1] if A == 1 and l.count(A) == 1: q = i elif B == 1 and l.count(B) == 1: q = i elif l.count(A) == 1: if A - 1 == B: q = i elif len(T) == 2: A = T[0] B = T[1] if A == 1 and l.count(A) == 1: q = i elif B == 1 and l.count(B) == 1: q = i elif l.count(A) == 1: if A - 1 == B: q = i print(q + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF NUMBER VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) colors = list(map(int, input().split())) cnt = [(0) for _ in range(100001)] vals = dict() ind = 1 for i in range(n): el = colors[i] if cnt[el] != 0: vals[cnt[el]] -= 1 if vals[cnt[el]] == 0: del vals[cnt[el]] cnt[el] += 1 if cnt[el] in vals: vals[cnt[el]] += 1 else: vals[cnt[el]] = 1 if len(vals) == 2: tmp = list(vals.keys()) tmp.sort() if ( tmp[0] == 1 and vals[tmp[0]] == 1 or tmp[1] - tmp[0] == 1 and vals[tmp[1]] == 1 ): ind = i elif len(vals) == 1 and list(vals.keys())[0] == 1: ind = i if cnt[colors[0]] == n: print(n) else: print(ind + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
import sys input = sys.stdin.readline n = int(input()) a = list(map(int, input().split())) b = {} c = {} d = [] for i in range(n): b[a[i]] = b.get(a[i], 0) + 1 c[b[a[i]]] = c.get(b[a[i]], 0) + 1 if c[b[a[i]]] * b[a[i]] == i + 1: if i + 2 <= n: d.append(i + 2) if c[b[a[i]]] * b[a[i]] == i: if i + 1 <= n: d.append(i + 1) if len(d) == 0: print(1) exit() print(max(d))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) u = list(map(int, input().split())) cnts = [(0) for _ in range(11)] b = 1 for i in range(n): cnts[u[i]] += 1 nz = 11 - cnts.count(0) j = i + 1 if nz == 1: b = j continue if j % nz == 1: c = j // nz if cnts.count(c) + 1 == nz: b = j continue if ( cnts.count(1) and (j - 1) % (nz - 1) == 0 and cnts.count((j - 1) // (nz - 1)) == nz - 1 + (j == nz) ): b = j print(b)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR IF FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) arr = list(map(int, input().split())) fr = {} nmbr = {} days = 0 M = 0 for i in range(1, n + 1): fr[arr[i - 1]] = fr.get(arr[i - 1], 0) nmbr[fr[arr[i - 1]]] = nmbr.get(fr[arr[i - 1]], 0) - 1 fr[arr[i - 1]] = fr.get(arr[i - 1], 0) + 1 nmbr[fr[arr[i - 1]]] = nmbr.get(fr[arr[i - 1]], 0) + 1 M = max(M, fr[arr[i - 1]]) T_F = False if nmbr[1] == i: T_F = True elif i <= M and nmbr[i] == 1: T_F = True elif nmbr[1] == 1 and nmbr[M] * M == i - 1: T_F = True elif M > 1 and nmbr[M - 1] * (M - 1) == i - M and nmbr[M] == 1: T_F = True if T_F: days = i print(days)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) a = [int(x) for x in input().split()] arr = [0] * 100001 freqs = [0] * 100001 freqs[0] = n minf, maxf = 0, 0 for ind, i in enumerate(a): arr[i] += 1 freqs[arr[i]] += 1 freqs[arr[i] - 1] -= 1 if not freqs[arr[i] - 1] and minf == arr[i] - 1: minf += 1 maxf = max(maxf, arr[i]) if ( maxf == 1 or freqs[maxf] + freqs[0] == n - 1 and freqs[1] == 1 or freqs[maxf] == 1 and freqs[maxf - 1] + freqs[0] == n - 1 ): best = ind + 1 print(best)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) arr = list(map(int, input().split())) count = [0] * 10 ans = 1 for i in range(n): count[arr[i] - 1] += 1 cp_count = list.copy(count) cp_count.sort() r = 0 for j in range(1, 10): if cp_count[j] != cp_count[j - 1]: r += 1 if r == 0 and cp_count[0] == 1: ans = i + 1 elif r == 1: li = [] qw = [0, 0] for k in range(10): if (cp_count[k] in li) == False: li.append(cp_count[k]) qw[li.index(cp_count[k])] += 1 if li[0] == 0 and qw[1] == 1: ans = i + 1 elif li[0] == 0 and li[1] == 1: ans = i + 1 elif li[1] - li[0] == 1 and qw[1] == 1: ans = i + 1 elif li[0] == 1 and qw[0] == 1: ans = i + 1 elif r == 2: li = [] qw = [0, 0, 0] for k in range(10): if (cp_count[k] in li) == False: li.append(cp_count[k]) qw[li.index(cp_count[k])] += 1 if li[0] == 0 and li[2] - li[1] == 1 and qw[2] == 1: ans = i + 1 elif li[1] == 1 and qw[1] == 1: ans = i + 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) u = [int(x) for x in input().split()] colors = [(0) for _ in range(10)] streak = 1 def check1(d): c = [d[i] for i in range(10)] a = max(c) f1 = False for i in range(10): if c[i] == 1: c[i] = 0 f1 = True break if not f1: return False diff = 0 for e in c: if e != a and e != 0: diff += 1 return diff == 0 def check(d): c = [d[i] for i in range(10)] index = 0 a = c[0] for i in range(1, 10): if a < c[i]: index = i a = c[i] c[index] -= 1 a -= 1 diff = 0 for e in c: if e != a and e != 0: diff += 1 return diff == 0 for i in range(n): colors[u[i] - 1] += 1 if check1(colors) or check(colors): streak = i + 1 print(streak)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER RETURN VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER RETURN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
from sys import setcheckinterval, setrecursionlimit, stdin setcheckinterval(1000) setrecursionlimit(10**6) iin = lambda: int(stdin.readline()) lin = lambda: list(map(int, stdin.readline().split())) n = iin() a = lin() a = [(i - 1) for i in a] sol = [[(0) for i in range(10)] for i in range(n)] for i in range(n): sol[i][a[i]] += 1 for i in range(1, n): for j in range(10): sol[i][j] += sol[i - 1][j] for i in range(n - 1, -1, -1): ans = {} for j in sol[i]: if j == 0: continue try: ans[j] += 1 except: ans[j] = 1 l = len(ans) if l == 1: if ans[list(ans.keys())[0]] != 1 and list(ans.keys())[0] != 1: continue print(i + 1) exit() elif l == 2: a1 = sorted(ans.keys(), key=lambda x: ans[x]) if ans[a1[0]] != 1: continue if a1[0] == 1 or a1[1] > a1[0] and ans[a1[1]] == 1 and a1[0] == a1[1] - 1: print(i + 1) exit() else: if a1[1] < a1[0] and a1[0] - 1 == a1[1]: print(i + 1) exit() continue else: continue
EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR DICT FOR VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
def Maximum_Streak(a): counts = [0] * 11 for index, v in enumerate(a): counts[v] += 1 k = sorted([i for i in counts if i]) if ( len(k) == 1 or k[0] == k[-2] and k[-1] - k[-2] == 1 or k[0] == 1 and k[1] == k[-1] ): ans = index return ans + 1 n = int(input()) a = list(map(int, input().split())) print(Maximum_Streak(a))
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR RETURN BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) l = list(map(int, input().split())) res = 0 c = [0] * 10 for j in range(n): c[l[j] - 1] += 1 lo = [i for i in c if i] lo.sort() if ( len(lo) == 1 or lo[0] == lo[-2] and lo[-1] - 1 == lo[-2] or lo[1] == lo[-1] and lo[0] == 1 ): res = j print(res + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
import sys allnums = (int(x) for x in sys.stdin.readlines()[1].split()) maxx = 0 counts = [(0) for i in range(0, 10)] def isok(): global counts nonzeroes = [c for c in counts if c != 0] if len(nonzeroes) == 1: return True if any(x == 1 for x in nonzeroes): s = sum(nonzeroes) n = len(nonzeroes) - 1 base = (s - 1) // n if sum([x for x in nonzeroes if x != base]) <= 1: return True if any(x != 1 for x in nonzeroes): s = sum(nonzeroes) n = len(nonzeroes) base = s // n if any(x < base or x > base + 1 for x in nonzeroes): return False if s == n * base + 1: return True return False for i, num in enumerate(allnums): counts[num - 1] += 1 if isok(): maxx = i + 1 print(f"{maxx}")
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) u = [int(x) for x in input().split()] cnt = {} for x in u: cnt[x] = cnt.get(x, 0) + 1 cnt2 = {} for v in cnt.values(): cnt2[v] = cnt2.get(v, 0) + 1 k = len(u) while True: if len(cnt2) == 1: if list(cnt2.values())[0] == 1 or list(cnt2.keys())[0] == 1: print(k) break if len(cnt2) == 2: [k1, k2] = list(cnt2.keys()) if abs(k1 - k2) == 1: mk = max(k1, k2) if cnt2[mk] == 1: print(k) break if min(k1, k2) == 1 and cnt2[1] == 1: print(k) break x = u[k - 1] cnt2[cnt[x]] -= 1 if cnt2[cnt[x]] == 0: del cnt2[cnt[x]] cnt[x] -= 1 if cnt[x] == 0: del cnt[x] else: cnt2[cnt[x]] = cnt2.get(cnt[x], 0) + 1 k -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE NUMBER IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN LIST VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR NUMBER
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) lis = [0] + list(map(int, input().split())) a = [0] * 100004 b = [0] * 100004 ans = 1 for i in range(1, n + 1): a[lis[i]] += 1 b[a[lis[i]]] += 1 if a[lis[i]] * b[a[lis[i]]] == i and i != n: ans = i + 1 elif a[lis[i]] * b[a[lis[i]]] == i - 1: ans = i print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) u = list(map(int, input().split())) cnt = [0] * 10 x = 0 for k in range(n): i = u[k] cnt[i - 1] += 1 f = 1 ch = 0 f1 = 0 oc = 0 cnt1 = sorted(cnt) for p in range(len(cnt1)): j = cnt1[p] if not j: continue if j == 1: q = p + 1 tm = 1 while 1: if q == 10: break if cnt1[q] == 1: q += 1 continue if q == 9 and cnt1[q] == 2: q += 1 continue tm = 0 break if tm: x = k + 1 break l = list(set(cnt1[p + 1 :])) if len(l) < 2: x = k + 1 break else: f = 0 break if not ch: ch = 1 q = p + 1 if q == 10: x = k + 1 break while 1: if q == 10: f = 0 break if cnt1[q] != cnt1[q - 1]: break q += 1 if not f: break if q == 9 and cnt1[q] == cnt1[p] + 1: f = 1 break else: f = 0 break if f: x = k + 1 print(x)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR IF VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) temp = list(map(int, input().split())) x = 0 hold = [0] * 20 hold[temp[0]] = 1 for j in range(1, n): hold[temp[j]] = hold[temp[j]] + 1 anmol = set(hold) if len(anmol) == 2: anmol = sorted(anmol) t = anmol[1] + anmol[0] if 1 in anmol: x = j elif hold.count(t) == 1: x = j elif len(anmol) == 3: diksha = sorted(anmol) if hold.count(1) == 1 and diksha[1] == 1: x = j elif hold.count(diksha[2]) == 1 and diksha[2] - diksha[1] == 1: x = j print(x + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF NUMBER VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) ui = list(map(int, input().split())) ar = [0] * 100001 ar2 = [0] * 100001 maxi = 1 mini = 1 ans = 1 for i in range(n): if ar[ui[i]] == 0: ar[ui[i]] = 1 mini = 1 ar2[1] += 1 else: ar2[ar[ui[i]]] -= 1 if ar2[ar[ui[i]]] == 0 and mini == ar[ui[i]]: mini += 1 ar[ui[i]] += 1 ar2[ar[ui[i]]] += 1 maxi = max(maxi, ar[ui[i]]) if maxi == mini and mini == 1 or mini == i + 1: ans = i + 1 elif maxi == mini + 1 and ar2[maxi] == 1: ans = i + 1 elif mini == 1 and ar2[maxi] * maxi == i: ans = i + 1 if ar2[maxi] * maxi == i + 1: if i != n - 1: ans = i + 2 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
from sys import stdin, stdout n = int(stdin.readline()) arr = list(map(int, stdin.readline().strip().split(" "))) farr = [(0) for i in range(10**5 + 2)] carr = [(0) for i in range(n + 1)] colorseen = [(0) for i in range(10**5 + 2)] seentillnow = 0 ansind = -1 for i in range(len(arr)): if colorseen[arr[i]] == 0: colorseen[arr[i]] = 1 carr[0] += 1 seentillnow += 1 carr[farr[arr[i]]] -= 1 farr[arr[i]] += 1 carr[farr[arr[i]]] += 1 if carr[farr[arr[i]]] == 1 and carr[farr[arr[i]] - 1] == seentillnow - 1: ansind = i + 1 elif carr[1] == 1 and carr[(i + 1 - 1) // (seentillnow - 1)] == seentillnow - 1: ansind = i + 1 elif carr[1] == seentillnow or carr[1] == seentillnow - 1 and carr[2] == 1: ansind = i + 1 elif carr[farr[arr[i]]] == seentillnow - 1 and carr[farr[arr[i]] + 1] == 1: ansind = i + 1 stdout.write(str(ansind) + "\n")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
d1, d2 = {}, {} n = int(input()) s = list(map(int, ("0 " + input()).split())) r = 1 for i in range(1, n + 1): d1[s[i]] = d1.get(s[i], 0) + 1 d2[d1[s[i]]] = d2.get(d1[s[i]], 0) + 1 if d2[d1[s[i]]] * d1[s[i]] == i and i != n: r = i + 1 if d2[d1[s[i]]] * d1[s[i]] == i - 1: r = i print(r)
ASSIGN VAR VAR DICT DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL BIN_OP STRING FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
import sys def is_party_time(d): for item in d.keys(): sub_d = d.copy() sub_d[item] -= 1 if sub_d[item] == 0: del sub_d[item] if len(set(sub_d.values())) <= 1: return True return False def cat_party_house(n, a): result = None d = dict() for idx, item in enumerate(a): if d.get(item) is None: d[item] = 1 else: d[item] += 1 if is_party_time(d): result = idx return result + 1 n = int(input()) a = [int(item) for item in input().split()] print(cat_party_house(n, a))
IMPORT FUNC_DEF FOR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) a = map(int, input().split()) c = {} d = 0 for i, x in enumerate(a): c[x] = c.get(x, 0) + 1 if len(c) > 1: if 1 in c.values(): x = len(c) - 1 if x and 0 == i % x: y = i / x if max(c.values()) == y: d = max(d, i + 1) if 0 == i % len(c): y = i / len(c) if min(c.values()) == y and max(c.values()) == y + 1: d = max(d, i + 1) else: d = max(d, i + 1) print(d)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER IF NUMBER FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF NUMBER BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) arr = list(map(int, input().split())) lis = [0] * 10 ans = 0 for j in range(n): lis[arr[j] - 1] += 1 num = [] for i in range(10): if lis[i] > 0: num.append(lis[i]) a = max(num) if a == 1: ans = max(ans, sum(num)) elif len(num) == 1: ans = max(ans, sum(num)) else: b = len(num) c = num.count(a) d = min(num) e = num.count(d) if c == 1 and e == b - 1 and d == a - 1: ans = max(ans, sum(num)) elif c == b - 1 and d == 1: ans = max(ans, sum(num)) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) mark = [(0) for _ in range(11)] a = list(map(int, input().split())) minus = 0 for v in a: mark[v] += 1 for v in a[::-1]: temp = [] done = [] for value in mark: if value != 0: temp.append(value) temp.sort() if temp.count(1) == 1 and (len(set(temp)) == 1 or len(set(temp)) == 2): print(n - minus) break temp[-1] -= 1 if len(set(temp)) == 1 or len(set(temp)) == 2 and 0 in temp: print(n - minus) break mark[v] -= 1 minus += 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
def gns(): return [int(x) for x in input().split()] def gn(): return int(input()) n = gn() ns = gns() cl = {} nn = {} ans = -1 i = 0 for c in ns: i += 1 if i == 2: xxxx = 0 if c in cl: cl[c] += 1 else: cl[c] = 1 t = cl[c] if t in nn: nn[t] += 1 else: nn[t] = 1 if t != 1: nn[t - 1] -= 1 if nn[t - 1] == 0: nn.pop(t - 1) if len(nn) > 2: continue elif len(nn) == 1: if list(nn.values())[0] == 1 or list(nn.keys())[0] == 1: ans = i else: tem = list(nn.keys()) tem.sort() s, l = tem if s == 1 and nn[s] == 1: ans = i continue if nn[l] == 1 and l - s == 1: ans = i continue print(ans)
FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
N = int(input()) A = [int(a) for a in input().split()] C = [0] * 101010 ma = 0 macnt = 0 poscnt = 0 cntofone = 0 ans = 0 for i in range(N): C[A[i]] += 1 if C[A[i]] > ma: ma = C[A[i]] macnt = 1 elif ma == C[A[i]]: macnt += 1 if C[A[i]] == 1: poscnt += 1 cntofone += 1 elif C[A[i]] == 2: cntofone -= 1 if ( i <= 1 or cntofone >= 1 and (poscnt - 1) * ma == i or macnt == 1 and poscnt * (ma - 1) == i ): ans = i + 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
m = [0] * 100001 t = dict() n = int(input()) (*l,) = map(int, input().split()) minI = 1 maxI = 1 x = 0 for i in range(n): if m[l[i]] != 0: t[m[l[i]]].remove(l[i]) if len(t[m[l[i]]]) == 0: t.pop(m[l[i]]) m[l[i]] += 1 if t.get(m[l[i]]) == None: t[m[l[i]]] = {l[i]} else: t[m[l[i]]].add(l[i]) if len(t) == 1: (k1,) = t.keys() if len(t[k1]) == 1 or k1 == 1: x = i elif len(t) == 2: k1, k2 = t.keys() if k1 == 1 and len(t[k1]) == 1 or k2 == 1 and len(t[k2]) == 1: x = i elif k2 - k1 == 1 and len(t[k2]) == 1 or k1 - k2 == 1 and len(t[k1]) == 1: x = i print(x + 1)
ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NONE ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR IF VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) a = [int(s) for s in input().split()] b = [] c = [] x = 1 for i in range(n): if a[i] not in b: b.append(a[i]) c.append(1) else: for j in range(len(b)): if b[j] == a[i]: c[j] += 1 count = 0 k = 0 lenb = len(b) if lenb > 5 and ((i - 1) // lenb or (i - 1) // (lenb - 1)) or lenb <= 5: for j in range(lenb): k += c[j] for j in range(1, lenb): d = [] + c if c[j] == 1: del d[j] if d == [(k - 1) // (lenb - 1)] * (lenb - 1): x = i + 1 if (k - 1) % lenb == 0: for q in range(lenb): c[q] -= 1 if c == [(k - 1) // lenb] * lenb: x = i + 1 c[q] += 1 else: continue print(x)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP LIST VAR IF VAR VAR NUMBER VAR VAR IF VAR BIN_OP LIST BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR BIN_OP LIST BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) L = [int(i) for i in input().split()] D = {} def check(D): G = list(D.values()) G = list(reversed(sorted(G))) f = len(G) - 1 if f == 0: return 1 elif G[0] == G[1] + 1 == G[f] + 1: return 1 elif G[0] == G[f - 1] and G[f] == 1: return 1 else: return 0 for i in L: if i in D: D[i] += 1 else: D[i] = 1 for i in range(n - 1, -1, -1): t = 0 t = check(D) if t or i == 0: print(i + 1) break else: D[L[i]] -= 1 if D[L[i]] == 0: del D[L[i]]
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) a = list(map(int, input().split())) dic = {i: (0) for i in range(1, 11)} maxIndex = 0 for i in range(n): dic[a[i]] += 1 flag = False occurence = set(dic.values()).difference({0}) if len(occurence) > 2: continue occurence = list(occurence) occurence.sort() if ( len(occurence) == 1 and (occurence[0] == 1 or list(dic.values()).count(0) == 9) or len(occurence) == 2 and ( list(dic.values()).count(1) == 1 or list(dic.values()).count(occurence[1]) == 1 and occurence[1] - occurence[0] == 1 ) ): maxIndex = i print(maxIndex + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) a = [int(i) for i in input().split()] b = [] for i in range(11): b.append(0) m = 0 b[a[0]] = 1 for i in range(1, n): b[a[i]] += 1 c = set(b) if len(c) == 2: c = sorted(c) t = c[0] + c[1] if 1 in c: m = i elif b.count(t) == 1: m = i elif len(c) == 3: d = sorted(c) if d[1] == 1 and b.count(1) == 1: m = i elif d[2] - d[1] == 1 and b.count(d[2]) == 1: m = i print(m + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF NUMBER VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) nums = list(map(int, input().split())) cnt = [0] * (10**5 + 10) f = [0] * (10**5 + 10) ans = 1 mx = 1 for i in range(1, n + 1): if cnt[f[nums[i - 1]]] != 0: cnt[f[nums[i - 1]]] -= 1 f[nums[i - 1]] += 1 cnt[f[nums[i - 1]]] += 1 mx = max(mx, f[nums[i - 1]]) ok = False if cnt[1] == i: ok = True elif cnt[i] == 1: ok = True elif cnt[1] == 1 and cnt[mx] * mx == i - 1: ok = True elif cnt[mx - 1] * (mx - 1) == i - mx and cnt[mx] == 1: ok = True if ok: ans = i print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
def checkEqual(array): while 0 in array: array.remove(0) return len(list(set(array))) == 1 or len(array) == 0 def check(dic): v = dic.values() v = list(v) while 0 in v: v.remove(0) v = sorted(v) if len(v) > 1: for i in range(len(v)): arr = [(v[k] if k != i else v[k] - 1) for k in range(len(v))] if checkEqual(arr): return True return False else: return True n = int(input()) array = list(map(int, input().split())) dictionary = {i: (0) for i in range(1, 11)} answer = 0 for x in range(len(array)): dictionary[array[x]] += 1 if check(dictionary): answer = x + 1 print(answer)
FUNC_DEF WHILE NUMBER VAR EXPR FUNC_CALL VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
from sys import stdin def main(): n = int(stdin.readline()) ar = list(map(int, stdin.readline().split())) c = [0] * (10**5 + 1) f = [0] * (10**5 + 1) ans = 1 c[ar[0]] += 1 f[c[ar[0]]] += 1 df = 1 dn = 1 for i in range(1, n): curr = ar[i] if c[curr] > 0: f[c[curr]] -= 1 if f[c[curr]] == 0: df -= 1 c[curr] += 1 f[c[curr]] += 1 if f[c[curr]] == 1: df += 1 else: dn += 1 c[curr] += 1 f[c[curr]] += 1 if f[c[curr]] == 1: df += 1 if df == 1 and f[c[curr]] > 0 and (dn == 1 or dn == i + 1): ans = i + 1 elif df == 2 and f[1] == 1: ans = i + 1 elif df == 2: if c[curr] < 10**5 and f[c[curr] + 1] == 1: ans = i + 1 if c[curr] > 1 and f[c[curr] - 1] > 0 and f[c[curr]] == 1: ans = i + 1 print(ans) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR BIN_OP NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) arr = [int(x) for x in input().split()] counts = [0] * 100000 count_to_color = {} mx = 0 for i in range(n): color = arr[i] - 1 if counts[color] > 0: count_to_color[counts[color]].remove(color) if not count_to_color[counts[color]]: del count_to_color[counts[color]] counts[color] += 1 count = counts[color] if count not in count_to_color: count_to_color[count] = set() count_to_color[count].add(color) if len(count_to_color) == 1: if 1 in count_to_color: mx = max(mx, i) if len(list(count_to_color.values())[0]) == 1: mx = max(mx, i) if len(count_to_color) == 2: count_keys = sorted(list(count_to_color.keys())) if count_keys[0] == 1 and len(count_to_color[count_keys[0]]) == 1: mx = max(mx, i) if ( count_keys[0] + 1 == count_keys[1] and len(count_to_color[count_keys[1]]) == 1 ): mx = max(mx, i) print(mx + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER IF NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
This problem is same as the next one, but has smaller constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $n$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $i$-th day has a ribbon with color $u_i$. Shiro wants to know the largest number $x$, such that if we consider the streak of the first $x$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $x - 1$ will have the same number of occurrences. For example, consider the following sequence of $u_i$: $[2, 2, 1, 1, 5, 4, 4, 5]$. Then $x = 7$ makes a streak, since if we remove the leftmost $u_i = 5$, each ribbon color will appear exactly twice in the prefix of $x - 1$ days. Note that $x = 8$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 10^5$)Β β€” the total number of days. The second line contains $n$ integers $u_1, u_2, \ldots, u_n$ ($1 \leq u_i \leq 10$)Β β€” the colors of the ribbons the cats wear. -----Output----- Print a single integer $x$Β β€” the largest possible streak of days. -----Examples----- Input 13 1 1 1 2 2 2 3 3 3 4 4 4 5 Output 13 Input 5 10 2 5 4 1 Output 5 Input 1 10 Output 1 Input 7 3 2 1 1 4 5 1 Output 6 Input 6 1 1 1 2 2 2 Output 5 -----Note----- In the first example, we can choose the longest streak of $13$ days, since upon removing the last day out of the streak, all of the remaining colors $1$, $2$, $3$, and $4$ will have the same number of occurrences of $3$. Note that the streak can also be $10$ days (by removing the $10$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $6$ days, we can remove the third day from this streak then all of the remaining colors $1$, $2$, $3$, $4$ and $5$ will occur exactly once.
n = int(input()) otv = 1 a = list(map(int, input().split())) kol = dict() for i in range(n): o = a[i] if o not in kol: kol[o] = 1 else: kol[o] += 1 sv = set(kol.values()) if sv == {1} or len(set(kol.keys())) == 1: otv = i + 1 elif len(sv) == 2 and min(sv) == 1 and list(kol.values()).count(min(sv)) == 1: otv = i + 1 elif ( len(sv) == 2 and list(kol.values()).count(max(sv)) == 1 and max(sv) - min(sv) == 1 ): otv = i + 1 print(otv)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Given an undirected connected graph of n vertices and list of m edges in a graph and for each pair of vertices that are connected by an edge. There are two edges between them, one curved edge and one straight edge i.e. the tuple (x, y, w1, w2) means that between vertices x and y, there is a straight edge with weight w1 and a curved edge with weight w2. You are given two vertices a and b and you have to go from a to b through a series of edges such that in the entire path you can use at most 1 curved edge. Your task is to find the shortest path from a to b satisfying the above condition. If there is no path from a to b, return -1. Example 1: Input: n = 4, m = 4 a = 2, b = 4 edges = {{1, 2, 1, 4}, {1, 3, 2, 4}, {1, 4, 3, 1}, {2, 4, 6, 5}} Output: 2 Explanation: We can follow the path 2 -> 1 -> 4. This gives a distance of 1+3 = 4 if we follow all straight paths. But we can take the curved path from 1 -> 4, which costs 1. This will result in a cost of 1+1 = 2 Example 2: Input: n = 2, m = 1 a = 1, b = 2 edges = {{1, 2, 4, 1}} Output : 1 Explanation: Take the curved path from 1 to 2 which costs 1. Your Task: You don't need to read input or print anything. Your task is to complete the function shortestPath() which takes 4 integers n, m, a, and b, and a list of lists named edges of size m as input and returns the cost of shortest path from a to b. Expected Time Complexity: O((m+n)log(n)) Expected Auxiliary Space: O(n+m) Constraints: 1 ≀ n,m ≀ 10^{5} 1 ≀ a,b ≀ n weight of edges ≀ 10^{4}
class Solution: def dijkstra(self, u, b, n, adj): unused = [] dis = [float("inf")] * (n + 1) dis[u] = 0 unused.append((0, u)) heapify(unused) while unused: x, v = heappop(unused) if x > dis[v]: continue for edge in adj[v]: u = edge[0] w = edge[1] if dis[v] + w < dis[u]: dis[u] = dis[v] + w heappush(unused, (dis[u], u)) return dis def shortestPath(self, n, m, a, b, edges): adj = [[] for i in range(n + 1)] curved = [] for i in range(m): u = edges[i][0] v = edges[i][1] w = edges[i][2] cw = edges[i][3] adj[u].append((v, w)) adj[v].append((u, w)) curved.append([u, v, cw]) da = self.dijkstra(a, b, n, adj) db = self.dijkstra(b, a, n, adj) ans = da[b] for i in range(m): u = curved[i][0] v = curved[i][1] cw = curved[i][2] ans = min(ans, da[u] + cw + db[v]) ans = min(ans, da[v] + cw + db[u]) if ans >= 1000000001: return -1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN VAR
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
import sys input = sys.stdin.readline n, q = map(int, input().split()) a = list(map(int, input().split())) query = [int(input()) for i in range(q)] max_a = max(a) for i in range(n): if a[i] == max_a: max_ind = i break ans = [0] * (n + 1) tmp_max = -1 if max_ind == 0: i = -1 else: b = [] for i in range(n - 1): tmp_max = max(tmp_max, a[i]) ans[i] = [tmp_max, a[i + 1]] tmp_min = min(tmp_max, a[i + 1]) b.append(tmp_min) if a[i + 1] == max_a: break a = a[i + 1 :] + b for j in query: if j - 1 <= i: print(" ".join(map(str, ans[j - 1]))) else: print(max_a, a[(j - 2 - i) % (n - 1) + 1])
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR LIST VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FOR VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
from sys import setcheckinterval, stdin setcheckinterval(1000) iin = lambda: int(stdin.readline()) lin = lambda: list(map(int, stdin.readline().split())) n, q = lin() a = lin() if q == 0: exit() Q = [iin() for i in range(q)] sq = set(Q) mx = max(Q) d = dict() ch = 1 for i in range(min(mx, n + 1)): if ch == n: ch = 1 if i + 1 in sq: d[i + 1] = [a[0], a[ch]] if a[0] < a[ch]: a[0], a[ch] = a[ch], a[0] ch += 1 for i in Q: if i > n: x = n - 1 if i % (n - 1) == 0 else i % (n - 1) print(a[0], a[x]) else: print(*d[i])
EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER LIST VAR NUMBER VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
n, q = [int(x) for x in input().split()] a = [int(x) for x in input().split()] mx = max(a) ans = [] idx = a.index(mx) bk = [] if idx == 0: pass else: cur = max(a[0], a[1]) ans.append([a[0], a[1]]) bk.append(min(a[0], a[1])) for i in range(2, idx + 1): ans.append([cur, a[i]]) bk.append(min(cur, a[i])) cur = max(cur, a[i]) num = len(ans) c = a[idx + 1 :] + bk for i in range(q): m = int(input()) if m <= num: print(*ans[m - 1]) else: sec = m - num print(mx, c[(sec - 1) % (n - 1)])
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
M = 10**9 + 7 R = lambda: map(int, input().split()) n, q = R() L = list(R()) ma = max(L) ind = L.index(ma) res = [[L[0], L[1]]] p = max(L[0], L[1]) A = [min(L[0], L[1])] for i in range(2, n): res.append([p, L[i]]) if p < L[i]: A.append(p) p = L[i] else: A.append(L[i]) for i in range(ind): res.append([ma, A[i]]) for i in range(q): m = int(input()) m -= 1 if m < ind: print(*res[m]) else: t = m m -= ind k = m % (n - 1) print(*res[k + ind])
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST LIST VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
n, q = map(int, input().split()) ai = list(map(int, input().split())) ar = [] ar3 = [] num = 1 nummm = max(ai) if ai[0] != nummm: num2 = ai[0] for i in range(1, n): ar3 += [[num2, ai[i]]] if ai[i] == nummm: ar += [num2] num = i + 1 break if ai[i] > num2: ar += [num2] num2 = ai[i] else: ar += [ai[i]] ar2 = [] for i in range(num, n): ar2 += [ai[i]] for i in range(len(ar)): ar2 += [ar[i]] num = len(ar3) for i in range(q): m = int(input()) if m <= num: print(ar3[m - 1][0], ar3[m - 1][1]) else: m -= num m -= 1 print(nummm, ar2[m % (n - 1)])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR LIST LIST VAR VAR VAR IF VAR VAR VAR VAR LIST VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR LIST VAR ASSIGN VAR VAR VAR VAR LIST VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR VAR LIST VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
a = [int(i) for i in input().split(" ")] b = [int(i) for i in input().split(" ")] c = [] maxnum = max(b) index = 0 for i in range(a[0]): c.append([b[0], b[1]]) if b[0] < b[1]: small = b.pop(0) else: small = b.pop(1) b.insert(a[0] - 1, small) if b[0] == maxnum: index = i + 1 break if a[1] != 0: for i in range(a[1]): count = int(input()) if count <= index: output = c[count - 1] else: output = [maxnum, 0] output[1] = b[(count - index - 1) % (a[0] - 1) + 1] print(" ".join([str(i) for i in output]))
ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR IF VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
nq = input().split() n = int(nq[0]) q = int(nq[1]) arr = list(map(int, input().split())) arr_q = [] for _ in range(q): arr_q.append(int(input())) largest1 = max(arr) new_arr = [] for i in arr: new_arr.append(i) for i in range(n - 1): if new_arr[i] > new_arr[i + 1]: new_arr[i : i + 2] = [new_arr[i + 1], new_arr[i]] for m in arr_q: if m < n: largest = max(arr[:m]) print(f"{largest} {arr[m]}") else: m = (m - n) % (n - 1) + n print(f"{largest1} {new_arr[m - n]}")
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER LIST VAR BIN_OP VAR NUMBER VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR STRING VAR BIN_OP VAR VAR
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
n, q = map(int, input().split()) a = list(map(int, input().split())) l = [a[0]] for i in range(n - 1): A, B = l[i], a[i + 1] if A < B: A, B = B, A l.append(A) a.append(B) for i in range(q): m = int(input()) if m <= n: print(l[m - 1], a[m]) else: print(l[-1], a[n + (m - n) % (n - 1)])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
n, q = map(int, input().split()) ls = list(map(int, input().split())) M = [ls[0]] for i in ls[1:]: M.append(max(M[-1], i)) ans = [] for i in range(n - 1): ans.append((M[i], ls[i + 1])) for i in range(n - 1): ans.append((M[-1], min(ans[i][0], ans[i][1]))) for i in range(q): m = int(input()) try: print(ans[m - 1][0], ans[m - 1][1]) except: print(ans[m % (n - 1) + n - 2][0], ans[m % (n - 1) + n - 2][1])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
n, q = map(int, input().split()) a = [int(x) for x in input().split()] mx = max(a) d = {} i = 0 while a[i] != mx: x, y = a[i], a[i + 1] a.append(min(x, y)) a[i + 1] = max(x, y) i += 1 d[i] = x, y la = len(a) for qi in range(q): mj = int(input()) if mj <= i: print(d[mj][0], d[mj][1]) else: dif = mj - i - 1 print(mx, a[i + 1 + dif % (n - 1)])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
import sys n, q = list(map(int, input().split())) a = list(map(int, input().split())) mx1 = max(a) a1 = [] dp = {} for i in range(q): a1.append(int(input())) if a1 == []: mx = 0 else: mx = max(a1) count = 0 while 1: count += 1 val1 = a[0] val2 = a[1] if val1 == mx1: break if val1 > val2: a.remove(val2) a.append(val2) else: a.remove(val1) a.append(val1) dp[count] = val1, val2 for x in a1: if x in dp: print(dp[x][0], dp[x][1]) else: xx = (x - count) % (n - 1) print(a[0], a[xx + 1])
IMPORT ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
n, q = map(int, input().split()) dek = list(map(int, input().split())) b = [0] * (n - 1) c = [0] * (n - 1) l = 0 r = 1 for i in range(1, n): b[i - 1] = dek[l] c[i - 1] = dek[r] if dek[l] >= dek[r]: r += 1 else: l = r r += 1 if r == n: r = 0 for i in range(q): m = int(input()) if m < n: print(b[m - 1], c[m - 1]) else: print(max(b[n - 2], c[n - 2]), min(b[(m - 1) % (n - 1)], c[(m - 1) % (n - 1)]))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
arr = input() N, Q = [int(x) for x in arr.split(" ")] arr = input() arr = [int(x) for x in arr.split(" ")] ans = {} maxi = max(arr) A = arr[0] B = arr[1] idx = 0 for i in range(1, N): B = arr[i] C = min(A, B) ans[i] = [A, B, C] A = max(A, B) if A == maxi: idx = i break check = [0] * (N - 1) j = 0 for i in range(idx + 1, N): check[j] = arr[i] j += 1 for i in range(1, idx + 1): check[j] = ans[i][2] j += 1 test = 0 while test < Q: m = int(input()) if m <= idx: A = ans[m][0] B = ans[m][1] print(A, B) else: m = (m - idx) % (N - 1) - 1 A = maxi B = check[m] print(A, B) test += 1
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
n, x = map(int, input().split()) m = list(map(int, input().split())) ind = 0 for i in range(n): if m[ind] < m[i]: ind = i z = m[ind] r = m[0] ans = [] l = [] for i in range(ind): ans.append([r, m[i + 1]]) l.append(min(m[i + 1], r)) if r < m[i + 1]: r = m[i + 1] for i in range(ind + 1, n): l.append(m[i]) for i in range(x): q = int(input()) if q <= ind: print(ans[q - 1][0], ans[q - 1][1]) elif q >= n: print(z, l[(q - n) % (n - 1)]) else: print(z, m[q])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
n, q = map(int, input().split()) a = list(map(int, input().split())) MAX = max(a) max_idx = a.index(MAX) pulled = [[-1, -1]] for i in range(max_idx): pulled.append([a[i], a[i + 1]]) a[i], a[i + 1] = (a[i + 1], a[i]) if a[i] > a[i + 1] else (a[i], a[i + 1]) k = max_idx l = [] for i in range(k + 1, len(a)): l.append(a[i]) for i in range(k): l.append(a[i]) def pair(m): ans = [] if m <= max_idx: ans = pulled[m] else: ans = [MAX, l[(m - max_idx - 1) % (n - 1)]] return ans for _ in range(q): m = int(input()) print(*pair(m))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
def func(a, n, maxa, index): first = a[0] ans = [] arr = [] for i in range(1, index + 1): second = a[i] ans.append([first, second]) if first > second: arr.append(second) else: arr.append(first) first = second if index == n - 1: return [ans, arr] return [ans, a[index + 1 :] + arr] a = [int(i) for i in input().split()] n = a[0] q = a[1] a = [int(i) for i in input().split()] maxa = max(a) index = a.index(maxa) ans, arr = func(a, n, maxa, index) for i in range(q): x = int(input()) if x <= index: print(ans[x - 1][0], ans[x - 1][1]) else: first = maxa second = (x - 1 - index) % (n - 1) print(first, arr[second])
FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER RETURN LIST VAR VAR RETURN LIST VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
n, m = map(int, input().split()) arr = list(map(int, input().split())) fr = [0] * n br = [0] * n cyc = [0] * (n - 1) maxind = 0 for i in range(n): if arr[i] > arr[maxind]: maxind = i pick = arr[0] last = -1 for i in range(n): if i >= maxind: fr[i] = arr[maxind] if i != n - 1: br[i] = arr[i + 1] cyc[i] = arr[i + 1] else: br[i] = cyc[0] else: fr[i] = pick br[i] = arr[i + 1] if pick < arr[i + 1]: cyc[i] = pick pick = arr[i + 1] else: cyc[i] = br[i] for i in range(m): k = int(input()) if k <= n: print(fr[k - 1], br[k - 1]) else: print(arr[maxind], cyc[k % (n - 1) - 1])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
n, k = map(int, input().split()) a = list(map(int, input().split())) mx = max(a) ind = a.index(mx) f = 0 s = 1 ans = [[] for i in range(ind)] for i in range(ind): ans[i].append(a[f]) ans[i].append(a[s]) if a[f] >= a[s]: a.append(a[s]) s += 1 else: a.append(a[f]) f = s s += 1 a = a[ind:] for i in range(k): m = int(input()) if m <= ind: print(ans[m - 1][0], ans[m - 1][1]) else: m -= ind m -= 1 m %= n - 1 print(a[0], a[1 + m])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR BIN_OP NUMBER VAR
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
[n, q] = list(map(int, input().split())) a = list(map(int, input().split())) qry = [(0) for i in range(q)] for i in range(q): qry[i] = int(input()) ans = [[-1, -1]] m = max(a) idx = a.index(m) t1 = a[0] for i in range(idx): ans.append([t1, a[i + 1]]) a.append(min(t1, a[i + 1])) t1 = max(a[i + 1], t1) for i in range(q): if qry[i] <= idx: print(ans[qry[i]][0], end=" ") print(ans[qry[i]][1]) else: print(t1, end=" ") print(a[idx + 1 + (qry[i] - idx - 1) % (n - 1)])
ASSIGN LIST VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
n, q = map(int, input().strip().split()) d = list(map(int, input().strip().split())) if q: m = -1 m_i = -1 m_p = [] for i in range(n): if d[i] > m: m = d[i] m_i = i m_p.append(m) new_d = [] new_d[m_i + 1 :] = d[m_i + 1 :] for i in range(m_i): if d[i + 1] < m_p[i]: new_d.append(d[i + 1]) else: new_d.append(m_p[i]) for i in range(q): qi = int(input()) - 1 if qi < m_i: print(m_p[qi], d[qi + 1], sep=" ") else: qi -= m_i print(m, new_d[qi % (n - 1)])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR IF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER STRING VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
n, q = map(int, input().split()) a = list(map(int, input().split())) b = [-1] * (3 * n) s = 0 d = [] e = n - 1 if q == 0: exit() c = [[a[0], a[1]]] for i in range(n): b[i] = a[i] m = max(a) while b[s] != m: if b[s] > b[s + 1]: b[s + 1], b[e + 1] = b[s], b[s + 1] s += 1 e += 1 else: b[e + 1] = b[s] s += 1 e += 1 c.append([b[s], b[s + 1]]) A = m for i in range(s, e + 1): d.append(b[i]) for i in range(q): l = int(input()) if l <= len(c): print(c[l - 1][0], c[l - 1][1]) else: ll = len(c) B = d[(l - ll) % (n - 1) + 1] print(A, B)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST LIST VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
n, q = map(int, input().split()) l = list(map(int, input().split())) m = max(l) tab = [0] * 2 * n for i in range(n): tab[i] = l[i] odp = [[0, 0]] * n pocz = 0 kon = n - 1 for j in range(n): A = tab[pocz] B = tab[pocz + 1] odp[j] = [A, B] pocz += 1 kon += 1 tab[pocz] = max(A, B) tab[kon] = min(A, B) for i in range(q): query = int(input()) if query <= n: print(odp[query - 1][0], odp[query - 1][1]) else: print(m, tab[(query - 2) % (n - 1) + n + 1])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST LIST NUMBER NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR LIST VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
from sys import stdin, stdout n, q = map(int, stdin.readline().split()) arr = [int(el) for el in stdin.readline().split()] query = [] for i in range(q): query.append(int(stdin.readline())) last = [] mx = max(arr) left = 0 right = 1 count = 1 initial = {} while arr[left] != mx: initial[count] = [arr[left], arr[right]] count += 1 if arr[left] > arr[right]: last.append(arr[right]) right += 1 else: last.append(arr[left]) left = right right += 1 take1 = arr[left + 1 : n] new_arr = take1 + last for i in range(q): if query[i] <= len(initial): print(*initial[query[i]]) else: rem = query[i] - len(initial) if rem % len(new_arr) == 0: print(mx, new_arr[-1]) else: print(mx, new_arr[rem % len(new_arr) - 1])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT WHILE VAR VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
n, t = map(int, input().split()) a = list(map(int, input().split())) b, c = [], [] u = a[0] for v in a[1:]: b.append(u) if v > u: u, v = v, u c.append(v) for _ in range(t): x = int(input()) if x < n: print(b[x - 1], a[x]) else: print(u, c[(x - 1) % (n - 1)])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
n, q = list(map(int, input().split())) arr = list(map(int, input().split())) maxi = max(arr[0], arr[1]) reste = [min(arr[0], arr[1])] hist_maxi = [maxi] for i in range(2, n): if arr[i] > maxi: reste.append(maxi) maxi = arr[i] else: reste.append(arr[i]) hist_maxi.append(maxi) for qi in range(q): r = int(input()) - 1 if r == 0: print(arr[0], arr[1]) continue if r > n - 2: print(maxi, reste[r % len(reste)]) elif hist_maxi[r - 1] == hist_maxi[r]: print(hist_maxi[r], reste[r]) else: print(reste[r], hist_maxi[r])
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
import sys IS_LOCAL = False def readMultiple(f): return f(map(int, input().split())) def main(): n, q = 5, 10 xs = [2, 3, 1, 4, 5] qs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] if not IS_LOCAL: n, q = readMultiple(tuple) xs = readMultiple(list) qs = [] for _ in range(q): qs.append(int(input())) mxs = [xs[0]] new_xs = [] for b in xs[1:]: a = mxs[-1] if b > a: t = a a = b b = t mxs.append(a) new_xs.append(b) for ind in qs: if ind < n: print(mxs[ind - 1], xs[ind]) else: ind = (ind - 1) % (n - 1) print(mxs[-1], new_xs[ind]) if len(sys.argv) > 1 and sys.argv[1] == "True": IS_LOCAL = True main()
IMPORT ASSIGN VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST FOR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
o = input().rstrip().split(" ") p = input().rstrip().split(" ") C = list(p) C.sort(key=int, reverse=True) t = int(C[0]) l = [] q = [] w = [] S = [] D = [] i = 0 while i < len(p) - 1: if int(p[i]) != t: l.append(int(p[i])) q.append(int(p[i + 1])) if int(p[i]) > int(p[i + 1]): p.append(p[i + 1]) del p[i + 1] else: p.append(p[i]) del p[i] else: break C = list(p) C.sort(key=int, reverse=True) V = p.index(str(C[0])) F = p[V : len(p)] for i in range(0, int(o[1])): o = int(input()) if o <= len(l): print(l[o - 1], q[o - 1]) else: o = o - len(l) B = o % (len(F) - 1) if B != 0: print(F[0], F[B]) else: print(F[0], F[len(F) - 1])
ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
x, y = map(int, input().split()) arr = list(map(int, input().split())) arr1 = [] arr2 = [] if y == 0: print("") exit() for i in range(0, y): arr1.append(int(input())) maxi = max(arr) i = 1 while 1: if arr[0] == maxi: break a = arr[0] b = arr[1] if a > b: del arr[1] arr.append(b) else: del arr[0] arr.append(a) arr2.append([i, a, b]) i += 1 q = i for i in range(0, y): if arr1[i] < q: print(arr2[arr1[i] - 1][1], arr2[arr1[i] - 1][2]) else: y1 = (arr1[i] - q + 1) % (x - 1) if y1 == 0: y1 = x - 1 print(maxi, arr[y1])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
n, e = map(int, input().split()) d = list(map(int, input().split())) ind = d.index(max(d)) maxs = d[ind] q = [] while True: if d[0] > d[1]: q.append((d[0], d[1])) r = d.pop(1) d.append(r) else: q.append((d[0], d[1])) t = d.pop(0) d.append(t) if d[0] == maxs: break for _ in range(e): f = int(input()) if f <= len(q): print(*q[f - 1]) else: p = f - len(q) i = p % (n - 1) if i == 0: print(d[0], d[n - 1]) else: print(d[0], d[i])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST WHILE NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
def main(): n, q = map(int, input().split()) arr = list(map(int, input().split())) dp = [] max_val = max(arr) while arr[0] != max_val: dp.append((arr[0], arr[1])) if arr[1] >= arr[0]: arr.append(arr.pop(0)) else: arr.append(arr.pop(1)) for i in range(q): m = int(input()) if m <= len(dp): print(dp[m - 1][0], dp[m - 1][1]) else: m -= len(dp) m -= 1 print(arr[0], arr[1 + m % (n - 1)]) main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
n_q = list(map(int, input().split(" "))) arr = list(map(int, input().split(" "))) arr_ind = [i for i in range(n_q[0])] max_val, max_ind = -1000, -1 for i in range(n_q[0]): if arr[i] > max_val: max_val = arr[i] max_ind = i pull = [] for i in range(max_ind + 1): pull.append([arr[0], arr[1]]) if arr[0] > arr[1]: temp = arr.pop(1) arr.append(temp) else: temp = arr.pop(0) arr.append(temp) for i in range(n_q[1]): query = int(input()) if query <= len(pull): print(*pull[query - 1]) else: query -= len(pull) + 1 query = query % (len(arr) - 1) print(str(arr[0]) + " " + str(arr[1 + query]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL VAR VAR BIN_OP NUMBER VAR
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
str1 = input().split() n = int(str1[0]) q = int(str1[1]) str2 = input().split() arr = [] for num in str2: arr.append(int(num)) dict = {} q_list = [] max_num = max(arr) max_index = arr.index(max_num) for i in range(q): mj = int(input()) if mj <= max_index: dict[mj] = "" q_list.append(mj) for i in range(1, max_index + 1): a = arr[0] b = arr[1] if i in dict: dict[i] = a, b if a > b: arr.remove(b) arr.append(b) else: arr.remove(b) arr[0] = b arr.append(a) for mj in q_list: if mj <= max_index: print(dict[mj][0], dict[mj][1]) else: a = arr[0] bi = 1 + (mj - max_index - 1) % (n - 1) b = arr[bi] print(a, b)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
def op(arr, num): for i in range(0, num - 1): if arr[0] > arr[1]: z = arr.pop(1) arr.append(z) else: z = arr.pop(0) arr.append(z) print(arr[0], arr[1]) def opmod(arr, num): count = 0 while arr[0] != num: if arr[0] > arr[1]: z = arr.pop(1) arr.append(z) else: z = arr.pop(0) arr.append(z) count = count + 1 return count a = input() a = a.split() p, q = int(a[0]), int(a[1]) a = input() arr1 = a.split() for i in range(0, p): arr1[i] = int(arr1[i]) s = max(arr1) arr2 = [] for i in range(0, q): a = int(input()) arr2.append(a) arr3 = [] for j in range(0, p): arr3.append(arr1[j]) rounds = opmod(arr3, s) + 1 for i in range(0, q): if arr2[i] < rounds: arr4 = [] for j in range(0, p): arr4.append(arr1[j]) op(arr4, arr2[i]) else: rec = (arr2[i] - rounds) % (p - 1) print(arr3[0], arr3[1 + rec])
FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR BIN_OP NUMBER VAR
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
import sys n, q = map(int, sys.stdin.readline().split()) a = list(map(int, sys.stdin.readline().split())) d = {} d[0] = a[0] b = [] b.append(0) for i in range(1, n): d[i] = max(d[i - 1], a[i]) b.append(min(d[i - 1], a[i])) b[0] = d[n - 1] for _ in range(q): m = int(sys.stdin.readline()) if m > n - 1: first = d[n - 1] m = (m - 1) % (n - 1) second = b[m + 1] else: first = d[m - 1] second = a[m] print(first, second)
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
n, q = [int(x) for x in input().split(" ")] a = [int(x) for x in input().split(" ")] C = a[0] resp = [] before = [] maxn = a[0] pos = 0 for i in range(1, n): if a[i] > maxn: maxn = a[i] pos = i for i in range(pos + 1, n): resp.append(a[i]) for i in range(1, pos + 1): A = C B = a[i] before.append([A, B]) C = max(A, B) resp.append(min(A, B)) while q > 0: query = int(input()) if query <= pos: a, b = [x for x in before[query - 1]] print(str(a) + " " + str(b)) else: a = maxn bpos = (query - pos - 1) % len(resp) b = resp[bpos] print(str(a) + " " + str(b)) q -= 1
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR VAR NUMBER
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
a, b = map(int, input().split()) A = list(map(int, input().split())) A.append(-1) B = [] Z = [] AN = [] x, y = A[0], A[1] for i in range(a - 1): Z.append((x, y)) if x > y: B.append(y) y = A[i + 2] else: B.append(x) x, y = y, A[i + 2] for i in range(b): w = int(input()) if w <= len(Z): AN.append(Z[w - 1]) else: w = w % len(B) AN.append((x, B[w - 1])) for W in AN: print(*W)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
n, queries = list(map(int, input().split())) l = list(map(int, input().split())) if queries == 0: exit() maxval = max(l) pairs = [] count = 0 f = l[0] secix = 1 while f != maxval: count += 1 f = l[0] s = l[secix] pairs.append([f, s]) f, s = max(f, s), min(f, s) l[0] = f l.append(s) secix += 1 l = [l[0]] + l[secix:] for i in range(n - 1): pairs.append([maxval, l[1 + i]]) for m in range(queries): q = int(input()) if q <= count: print(str(pairs[q - 1][0]), str(pairs[q - 1][1])) else: q -= count + 1 pos = count + q % (n - 1) print(str(pairs[pos][0]), str(pairs[pos][1]))
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
n, q = input().split() if q == "0": exit(0) l = list(map(int, input().split())) m = max(l) f = [] while 1: if l[0] == m: break a = l[0] b = l[1] f.append([a, b]) if a > b: del l[1] l.append(b) else: del l[0] l.append(a) for i in range(int(q)): m = int(input()) if len(f) == 0: if m % (len(l) - 1): print(l[0], l[m % (len(l) - 1)]) else: print(l[0], l[(m - 1) % (len(l) - 1) + 1]) elif m <= len(f): print(*f[m - 1]) elif (m - len(f)) % (len(l) - 1): print(l[0], l[(m - len(f)) % (len(l) - 1)]) else: print(l[0], l[(m - len(f) - 1) % (len(l) - 1) + 1])
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
import sys n, q = [int(i) for i in sys.stdin.readline().split()] data = [int(i) for i in sys.stdin.readline().split()] mx = max(data) ans = [(-1, -1)] while data[0] != mx: ans.append((data[0], data[1])) if data[0] > data[1]: data.append(data.pop(1)) else: data.append(data.pop(0)) cnt = len(ans) m = n - 1 data.pop(0) for _ in range(q): qu = int(sys.stdin.readline()) if qu < cnt: print(ans[qu][0], ans[qu][1]) else: extra = (qu - cnt) % m print(mx, data[extra])
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER WHILE VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
import sys n, q = list(map(int, sys.stdin.readline().strip().split())) a = list(map(int, sys.stdin.readline().strip().split())) m = [0] * q M = max(a) i = 0 x = a[0] L = [] L1 = [] L2 = [] while x != M: L1.append(x) L2.append(a[i + 1]) i = i + 1 if x < a[i]: L.append(x) x = a[i] else: L.append(a[i]) b = a[i + 1 :] + L for j in range(0, q): m = int(sys.stdin.readline().strip()) if m <= i: print(str(L1[m - 1]) + " " + str(L2[m - 1])) else: print(str(x) + " " + str(b[(m - i - 1) % (n - 1)]))
IMPORT ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST WHILE VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER STRING FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
t = list(map(int, input().split())) n, q = t[0], t[1] a = list(map(int, input().split())) list_m = [] A, B = 0, 0 for i in range(q): l = len(list_m) m = int(input()) t = int(m / (n - 1)) - 1 if t < 0: t = 0 m = m - t * (n - 1) if m <= l: A, B = list_m[m - 1][0], list_m[m - 1][1] else: for j in range(l, m): A = a[0] B = a[1] t = [A, B] list_m.append(t) if A > B: B = a.pop(1) a.append(B) else: A = a.pop(0) a.append(A) A, B = list_m[m - 1][0], list_m[m - 1][1] print(A, B)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
n, q = map(int, input().split()) l = list(map(int, input().split())) l0 = l[:] q_array = [] M = -float("inf") a_array = [] back_array = [] chain = [] for i in range(n): if l[i] >= M: q_array.append(i) a_array.append(M) back_array += chain back_array.append(M) chain = [] M = l[i] pos = i else: chain.append(l[i]) back_array = chain + back_array for i in range(len(back_array)): if back_array[i] < 0: back_array.pop(i) break back_array = [back_array.pop()] + back_array length = len(q_array) for i in range(q): query = int(input()) if query <= pos: s, e = 0, length - 2 while s <= e: mid = (s + e) // 2 if q_array[mid] < query and query <= q_array[mid + 1]: print(a_array[mid + 1], l0[query]) break if q_array[mid] >= query: e = mid - 1 elif q_array[mid + 1] < query: s = mid + 1 else: print(M, back_array[(query - pos) % (n - 1)])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
from sys import stdin, stdout input = stdin.readline n, q = map(int, input().split()) a = list(map(int, input().split())) b = max(a) c = a.index(b) d = [] for i in range(c): d.append([a[0], a[1]]) if a[0] > a[1]: x = a.pop(1) a.append(x) else: x = a.pop(0) a.append(x) y = a[1:] for i in range(q): x = int(input()) if x <= len(d): stdout.write(str(d[x - 1][0]) + " " + str(d[x - 1][1]) + "\n") else: print(str(b) + " " + str(y[(x - len(d)) % (n - 1) - 1]) + "\n")
ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER STRING FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER STRING
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
n, q = map(int, input().split()) arr = list(map(int, input().split())) ret = [] maxItem = 0 for item in arr: maxItem = max(maxItem, item) for _ in range(n): ret.append([arr[0], arr[1]]) arr[0], arr[1] = min(arr[0], arr[1]), max(arr[0], arr[1]) z = arr.pop(0) arr.append(z) if arr[0] == maxItem: break for _ in range(q): qj = int(input()) if qj - 1 < len(ret): print(str(ret[qj - 1][0]) + " " + str(ret[qj - 1][1])) else: indexS = (qj - (len(ret) + 1)) % (len(arr) - 1) + 1 print(str(maxItem) + " " + str(arr[indexS]))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER STRING FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR VAR
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is a_i (i = 1, 2, …, n). He gradually takes the first two leftmost elements from the deque (let's call them A and B, respectively), and then does the following: if A > B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation. For example, if deque was [2, 3, 4, 5, 1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3, 4, 5, 1, 2]. The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number m_j (j = 1, 2, …, q). It is required for each query to answer which two elements he will pull out on the m_j-th operation. Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque. Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides. Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 0 ≀ q ≀ 3 β‹… 10^5) β€” the number of elements in the deque and the number of queries. The second line contains n integers a_1, a_2, ..., a_n, where a_i (0 ≀ a_i ≀ 10^9) β€” the deque element in i-th position. The next q lines contain one number each, meaning m_j (1 ≀ m_j ≀ 10^{18}). Output For each teacher's query, output two numbers A and B β€” the numbers that Valeriy pulls out of the deque for the m_j-th operation. Examples Input 5 3 1 2 3 4 5 1 2 10 Output 1 2 2 3 5 2 Input 2 0 0 0 Output Note Consider all 10 steps for the first test in detail: 1. [1, 2, 3, 4, 5] β€” on the first operation, A and B are 1 and 2, respectively. So, 2 we write to the beginning of the deque, and 1 β€” to the end. We get the following status of the deque: [2, 3, 4, 5, 1]. 2. [2, 3, 4, 5, 1] β‡’ A = 2, B = 3. 3. [3, 4, 5, 1, 2] 4. [4, 5, 1, 2, 3] 5. [5, 1, 2, 3, 4] 6. [5, 2, 3, 4, 1] 7. [5, 3, 4, 1, 2] 8. [5, 4, 1, 2, 3] 9. [5, 1, 2, 3, 4] 10. [5, 2, 3, 4, 1] β‡’ A = 5, B = 2.
class Dek: def __init__(self, n): self.items = [] self.len = n - 1 def op(self): a = self.items[0] b = self.items[1] self.items.remove(a) if a > b: self.items[0] = a self.items.append(b) else: self.items.append(a) return a, b n, q = map(int, input().split()) dek = Dek(n) dek.items = list(map(int, input().split())) first = {} i = 0 _max = max(dek.items) while dek.items[0] != _max: a, b = dek.op() first.update({(i + 1): (a, b)}) i += 1 for d in range(q): j = int(input()) if j > i: j -= i num_el = j % (n - 1) if num_el == 0: num_el = n - 1 print(dek.items[0], dek.items[num_el], sep=" ") else: print(first[j][0], first[j][1], sep=" ")
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR DICT BIN_OP VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER STRING
Chef found a strange string yesterday - a string of signs s, where each sign is either a '<', '=' or a '>'. Let N be the length of this string. Chef wants to insert N + 1 positive integers into this sequence and make it valid. A valid sequence is a sequence where every sign is preceded and followed by an integer, and the signs are correct. That is, if a sign '<' is preceded by the integer a and followed by an integer b, then a should be less than b. Likewise for the other two signs as well. Chef can take some positive integers in the range [1, P] and use a number in the range as many times as he wants. Help Chef find the minimum possible P with which he can create a valid sequence. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains the string of signs s, where each sign is either '<', '=' or a '>'. -----Output----- For each test case, output a single line containing an integer corresponding to the minimum possible P. -----Constraints----- - 1 ≀ T, |s| ≀ 105 - 1 ≀ Sum of |s| over all test cases in a single test file ≀ 106 -----Subtasks----- Subtask #1 (30 points) - 1 ≀ T, |s| ≀ 103 - 1 ≀ Sum of |s| over all test cases in a single test file ≀ 104 Subtask #2 (70 points) - Original constraints -----Example----- Input: 4 <<< <>< <=> <=< Output: 4 2 2 3 -----Explanation----- Here are some possible valid sequences which can be formed with the minimum P for each of the test cases: 1 < 2 < 3 < 4 1 < 2 > 1 < 2 1 < 2 = 2 > 1 1 < 2 = 2 < 3
for _ in range(0, int(input())): a = input().strip().replace("=", "") m = 0 temp = 0 for i in a: if i == ">": temp += 1 else: temp = 0 m = max(m, temp) temp = 0 for i in a: if i == "<": temp += 1 else: temp = 0 m = max(m, temp) print(m + 1)
FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Chef found a strange string yesterday - a string of signs s, where each sign is either a '<', '=' or a '>'. Let N be the length of this string. Chef wants to insert N + 1 positive integers into this sequence and make it valid. A valid sequence is a sequence where every sign is preceded and followed by an integer, and the signs are correct. That is, if a sign '<' is preceded by the integer a and followed by an integer b, then a should be less than b. Likewise for the other two signs as well. Chef can take some positive integers in the range [1, P] and use a number in the range as many times as he wants. Help Chef find the minimum possible P with which he can create a valid sequence. -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains the string of signs s, where each sign is either '<', '=' or a '>'. -----Output----- For each test case, output a single line containing an integer corresponding to the minimum possible P. -----Constraints----- - 1 ≀ T, |s| ≀ 105 - 1 ≀ Sum of |s| over all test cases in a single test file ≀ 106 -----Subtasks----- Subtask #1 (30 points) - 1 ≀ T, |s| ≀ 103 - 1 ≀ Sum of |s| over all test cases in a single test file ≀ 104 Subtask #2 (70 points) - Original constraints -----Example----- Input: 4 <<< <>< <=> <=< Output: 4 2 2 3 -----Explanation----- Here are some possible valid sequences which can be formed with the minimum P for each of the test cases: 1 < 2 < 3 < 4 1 < 2 > 1 < 2 1 < 2 = 2 > 1 1 < 2 = 2 < 3
x = int(input()) for _ in range(x): n = input() lol = [] for i in n: if i != "=": lol.append(i) n = "".join(lol) l = 0 if len(n) > 0: l += 1 c = 1 for i in range(1, len(n)): if n[i] == n[i - 1]: c += 1 else: l = max(l, c) c = 1 l = max(l, c) print(l + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER