description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order. According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai. Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date. Input The first line contains a single positive integer n (1 ≀ n ≀ 5000) β€” the number of exams Valera will take. Each of the next n lines contains two positive space-separated integers ai and bi (1 ≀ bi < ai ≀ 109) β€” the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly. Output Print a single integer β€” the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date. Examples Input 3 5 2 3 1 4 2 Output 2 Input 3 6 1 5 2 4 3 Output 6 Note In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5. In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.
n = int(input()) arr = [] for i in range(n): temp = input() temp = temp.split() f = int(temp[0]) s = int(temp[1]) arr.append((f, s)) arr = sorted(arr) if len(arr) == 1: print(arr[0][1]) else: for i in range(n): if i == 0: a = arr[0][0] b = arr[0][1] continue cur_a = arr[i][0] cur_b = arr[i][1] if cur_b >= a or cur_b >= b: a = cur_b b = cur_b else: a = cur_a b = cur_a print(a)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order. According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai. Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date. Input The first line contains a single positive integer n (1 ≀ n ≀ 5000) β€” the number of exams Valera will take. Each of the next n lines contains two positive space-separated integers ai and bi (1 ≀ bi < ai ≀ 109) β€” the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly. Output Print a single integer β€” the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date. Examples Input 3 5 2 3 1 4 2 Output 2 Input 3 6 1 5 2 4 3 Output 6 Note In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5. In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.
n = int(input()) di = {} for i in range(n): a, b = [int(i) for i in input().split()] di[i] = [a, b] z = sorted(di, key=lambda i: di[i][0] * 100000 + di[i][1]) ans = 0 for i in z: if ans > di[i][1]: ans = max(di[i][0], ans) else: ans = di[i][1] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order. According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai. Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date. Input The first line contains a single positive integer n (1 ≀ n ≀ 5000) β€” the number of exams Valera will take. Each of the next n lines contains two positive space-separated integers ai and bi (1 ≀ bi < ai ≀ 109) β€” the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly. Output Print a single integer β€” the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date. Examples Input 3 5 2 3 1 4 2 Output 2 Input 3 6 1 5 2 4 3 Output 6 Note In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5. In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.
f = [] for i in range(int(input())): f.append(tuple(int(x) for x in input().split())) cur = 0 for t in sorted(f): cur = t[1] if cur <= t[1] else t[0] print(cur)
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order. According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai. Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date. Input The first line contains a single positive integer n (1 ≀ n ≀ 5000) β€” the number of exams Valera will take. Each of the next n lines contains two positive space-separated integers ai and bi (1 ≀ bi < ai ≀ 109) β€” the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly. Output Print a single integer β€” the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date. Examples Input 3 5 2 3 1 4 2 Output 2 Input 3 6 1 5 2 4 3 Output 6 Note In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5. In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.
n = int(input()) arr = [] for i in range(n): arr.append([int(x) for x in input().split()]) arr.sort() pre = min(arr[0][0], arr[0][1]) for i in range(1, n): temp = min(arr[i][0], arr[i][1]) if temp < pre: pre = max(arr[i][0], arr[i][1]) else: pre = temp print(pre)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order. According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai. Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date. Input The first line contains a single positive integer n (1 ≀ n ≀ 5000) β€” the number of exams Valera will take. Each of the next n lines contains two positive space-separated integers ai and bi (1 ≀ bi < ai ≀ 109) β€” the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly. Output Print a single integer β€” the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date. Examples Input 3 5 2 3 1 4 2 Output 2 Input 3 6 1 5 2 4 3 Output 6 Note In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5. In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.
def soln(arr): arr.sort(key=lambda tup: (tup[0], tup[1])) curr = prev = arr[0][1] for a, b in arr: curr = b if b >= prev else a prev = curr return curr def main(): n = int(input()) arr = [] for _ in range(n): arr.append([int(x) for x in input().split()]) result = soln(arr) print(result) main()
FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order. According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai. Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date. Input The first line contains a single positive integer n (1 ≀ n ≀ 5000) β€” the number of exams Valera will take. Each of the next n lines contains two positive space-separated integers ai and bi (1 ≀ bi < ai ≀ 109) β€” the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly. Output Print a single integer β€” the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date. Examples Input 3 5 2 3 1 4 2 Output 2 Input 3 6 1 5 2 4 3 Output 6 Note In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5. In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.
a, b = [], [] for _ in range(int(input())): x, y = map(int, input().split()) a.append((x, y)) a.sort() ans = a[0][1] for i in range(1, len(a)): x, y = a[i] if y >= ans: ans = y else: ans = x print(ans)
ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order. According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai. Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date. Input The first line contains a single positive integer n (1 ≀ n ≀ 5000) β€” the number of exams Valera will take. Each of the next n lines contains two positive space-separated integers ai and bi (1 ≀ bi < ai ≀ 109) β€” the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly. Output Print a single integer β€” the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date. Examples Input 3 5 2 3 1 4 2 Output 2 Input 3 6 1 5 2 4 3 Output 6 Note In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5. In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.
n = int(input()) arr = list() for i in range(n): a, b = input().split() a, b = int(a), int(b) arr += [[a, b]] arr.sort() ans = 0 for [i, j] in arr: if j >= ans: ans = j else: ans = i print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR LIST LIST VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR LIST VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order. According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai. Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date. Input The first line contains a single positive integer n (1 ≀ n ≀ 5000) β€” the number of exams Valera will take. Each of the next n lines contains two positive space-separated integers ai and bi (1 ≀ bi < ai ≀ 109) β€” the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly. Output Print a single integer β€” the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date. Examples Input 3 5 2 3 1 4 2 Output 2 Input 3 6 1 5 2 4 3 Output 6 Note In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5. In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.
n = int(input()) a = [] maxd = 0 maxa = 0 for i in range(n): c, d = map(int, input().split()) a.append([c, d]) a.sort(key=lambda a: a[0]) maxa = a[0][0] maxd = a[0][1] for i in range(1, n): if a[i - 1][0] == a[i][0] and maxd > a[i][1]: maxd += 0 elif a[i][1] >= maxd: maxd = a[i][1] else: maxd = a[i][0] print(maxd)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order. According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai. Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date. Input The first line contains a single positive integer n (1 ≀ n ≀ 5000) β€” the number of exams Valera will take. Each of the next n lines contains two positive space-separated integers ai and bi (1 ≀ bi < ai ≀ 109) β€” the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly. Output Print a single integer β€” the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date. Examples Input 3 5 2 3 1 4 2 Output 2 Input 3 6 1 5 2 4 3 Output 6 Note In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5. In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.
n = int(input()) dates = [] for i in range(n): a, b = [int(i) for i in input().split()] dates.append([a, b]) dates = sorted(sorted(dates, key=lambda x: x[1]), key=lambda x: x[0]) ans = dates[0][1] assigned = [float("inf")] * n assigned[0] = ans for i in range(1, n): if dates[i][0] < assigned[i] and dates[i][0] >= ans: assigned[i] = dates[i][0] if dates[i][1] < assigned[i] and dates[i][1] >= ans: assigned[i] = dates[i][1] ans = assigned[i] print(assigned[-1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order. According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai. Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date. Input The first line contains a single positive integer n (1 ≀ n ≀ 5000) β€” the number of exams Valera will take. Each of the next n lines contains two positive space-separated integers ai and bi (1 ≀ bi < ai ≀ 109) β€” the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly. Output Print a single integer β€” the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date. Examples Input 3 5 2 3 1 4 2 Output 2 Input 3 6 1 5 2 4 3 Output 6 Note In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5. In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.
num = int(input()) l = list() for i in range(num): f = list(map(int, input().split())) a = f[0] b = f[1] tup = a, b l.append(tup) l.sort() c = 0 for el in l: a = el[0] b = el[1] if b >= c: c = b else: c = a print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order. According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai. Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date. Input The first line contains a single positive integer n (1 ≀ n ≀ 5000) β€” the number of exams Valera will take. Each of the next n lines contains two positive space-separated integers ai and bi (1 ≀ bi < ai ≀ 109) β€” the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly. Output Print a single integer β€” the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date. Examples Input 3 5 2 3 1 4 2 Output 2 Input 3 6 1 5 2 4 3 Output 6 Note In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5. In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.
def rl(T=str): return list(map(T, input().split())) def main(): (n,) = rl(int) rec = [] for _ in range(n): rec.append(rl(int)) rec.sort() d = 0 for a, b in rec: if d <= b: d = b else: d = a print(d) main()
FUNC_DEF VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order. According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai. Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date. Input The first line contains a single positive integer n (1 ≀ n ≀ 5000) β€” the number of exams Valera will take. Each of the next n lines contains two positive space-separated integers ai and bi (1 ≀ bi < ai ≀ 109) β€” the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly. Output Print a single integer β€” the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date. Examples Input 3 5 2 3 1 4 2 Output 2 Input 3 6 1 5 2 4 3 Output 6 Note In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5. In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.
n = int(input()) Ar = [] ar = [] for i in range(n): a, b = input().split() Ar.append(int(a)) ar.append(int(b)) A = list(zip(Ar, ar)) A.sort() Ar = [x for x, y in A] ar = [y for x, y in A] R = list() R.append(min(Ar[0], ar[0])) for i in range(1, n): k = min(Ar[i], ar[i]) l = max(Ar[i], ar[i]) if k >= R[i - 1]: R.append(k) else: R.append(l) print(R[-1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order. According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai. Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date. Input The first line contains a single positive integer n (1 ≀ n ≀ 5000) β€” the number of exams Valera will take. Each of the next n lines contains two positive space-separated integers ai and bi (1 ≀ bi < ai ≀ 109) β€” the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly. Output Print a single integer β€” the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date. Examples Input 3 5 2 3 1 4 2 Output 2 Input 3 6 1 5 2 4 3 Output 6 Note In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5. In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.
import sys input = sys.stdin.readline n = int(input()) a = [] b = [] early = [(0) for _ in range(n + 1)] for i in range(n): x, y = map(int, input().split()) a.append((x, y)) a.sort() best = -1 for i in range(n): if best <= a[i][1]: best = a[i][1] else: best = a[i][0] print(best)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order. According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai. Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date. Input The first line contains a single positive integer n (1 ≀ n ≀ 5000) β€” the number of exams Valera will take. Each of the next n lines contains two positive space-separated integers ai and bi (1 ≀ bi < ai ≀ 109) β€” the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly. Output Print a single integer β€” the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date. Examples Input 3 5 2 3 1 4 2 Output 2 Input 3 6 1 5 2 4 3 Output 6 Note In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5. In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.
n = int(input()) a = [] d = -1 for i in range(n): ai, bi = [int(i) for i in input().split()] a.append([ai, bi]) a.sort() for i in a: d = i[1] if i[1] >= d else i[0] print(d)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order. According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai. Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date. Input The first line contains a single positive integer n (1 ≀ n ≀ 5000) β€” the number of exams Valera will take. Each of the next n lines contains two positive space-separated integers ai and bi (1 ≀ bi < ai ≀ 109) β€” the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly. Output Print a single integer β€” the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date. Examples Input 3 5 2 3 1 4 2 Output 2 Input 3 6 1 5 2 4 3 Output 6 Note In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5. In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.
n = int(input()) l1 = [] l2 = [] for i in range(n): a, b = map(int, input().split()) l1.append(a) l2.append(b) l3 = [x for _, x in sorted(zip(l1, l2))] l1.sort() c = l3[0] for i in range(n - 1): if c <= l3[i + 1]: c = l3[i + 1] else: c = l1[i + 1] print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order. According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai. Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date. Input The first line contains a single positive integer n (1 ≀ n ≀ 5000) β€” the number of exams Valera will take. Each of the next n lines contains two positive space-separated integers ai and bi (1 ≀ bi < ai ≀ 109) β€” the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly. Output Print a single integer β€” the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date. Examples Input 3 5 2 3 1 4 2 Output 2 Input 3 6 1 5 2 4 3 Output 6 Note In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5. In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.
n = int(input()) exs = [] for _ in range(n): exs.append(tuple(map(int, input().split()))) exs = sorted(exs) min_d = 0 for e in exs: min_d = e[0] if e[1] < min_d else max(min_d, e[1]) print(min_d)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order. According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai. Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date. Input The first line contains a single positive integer n (1 ≀ n ≀ 5000) β€” the number of exams Valera will take. Each of the next n lines contains two positive space-separated integers ai and bi (1 ≀ bi < ai ≀ 109) β€” the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly. Output Print a single integer β€” the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date. Examples Input 3 5 2 3 1 4 2 Output 2 Input 3 6 1 5 2 4 3 Output 6 Note In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5. In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.
def find_minimum_last_day(a_days, b_days): ab_pairs = zip(a_days, b_days) sorted_a_days, sorted_b_days = (list(t) for t in zip(*sorted(ab_pairs))) curr = sorted_a_days[0] if sorted_a_days[0] < sorted_b_days[0] else sorted_b_days[0] for i in range(1, len(sorted_a_days)): a = sorted_a_days[i] b = sorted_b_days[i] if curr <= a and curr <= b: curr = a if a < b else b elif a < curr <= b: curr = b else: curr = a return curr def run_alg(): num_exams = int(input()) a_days = list() b_days = list() for _ in range(num_exams): line_data = input().split(" ") a_days.append(int(line_data[0])) b_days.append(int(line_data[1])) print(find_minimum_last_day(a_days, b_days)) run_alg()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order. According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai. Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date. Input The first line contains a single positive integer n (1 ≀ n ≀ 5000) β€” the number of exams Valera will take. Each of the next n lines contains two positive space-separated integers ai and bi (1 ≀ bi < ai ≀ 109) β€” the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly. Output Print a single integer β€” the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date. Examples Input 3 5 2 3 1 4 2 Output 2 Input 3 6 1 5 2 4 3 Output 6 Note In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5. In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.
from sys import stdin, stdout cin = stdin.readline cout = stdout.write mp = lambda: list(map(int, cin().split())) def chars(): s = cin() return list(s[: len(s) - 1]) def pl(a): for val in a: cout(val + "\n") (n,) = mp() l = [] for _ in range(n): l += [mp()] l.sort() day = l[0][1] for i in range(1, n): day = (l[i][0], l[i][1])[day <= l[i][1]] cout(str(day))
ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR LIST FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order. According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai. Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date. Input The first line contains a single positive integer n (1 ≀ n ≀ 5000) β€” the number of exams Valera will take. Each of the next n lines contains two positive space-separated integers ai and bi (1 ≀ bi < ai ≀ 109) β€” the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly. Output Print a single integer β€” the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date. Examples Input 3 5 2 3 1 4 2 Output 2 Input 3 6 1 5 2 4 3 Output 6 Note In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5. In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.
n = int(input()) d = [] maxval = 0 for i in range(n): s = input() x = s.split(" ") x[0] = int(x[0]) x[1] = int(x[1]) if x[1] > maxval: maxval = x[1] d.append(x) d.sort() f = -1 for i in range(n): if f <= d[i][1]: f = d[i][1] else: f = d[i][0] print(f)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order. According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai. Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date. Input The first line contains a single positive integer n (1 ≀ n ≀ 5000) β€” the number of exams Valera will take. Each of the next n lines contains two positive space-separated integers ai and bi (1 ≀ bi < ai ≀ 109) β€” the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly. Output Print a single integer β€” the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date. Examples Input 3 5 2 3 1 4 2 Output 2 Input 3 6 1 5 2 4 3 Output 6 Note In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5. In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.
def main(): n = int(input()) tab = [] inc = 0 for x in range(n): a, b = map(int, input().split()) tab.append((a, b)) tab.sort() for x in range(n): if tab[x][1] >= inc: inc = tab[x][1] else: inc = tab[x][0] print(inc) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order. According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai. Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date. Input The first line contains a single positive integer n (1 ≀ n ≀ 5000) β€” the number of exams Valera will take. Each of the next n lines contains two positive space-separated integers ai and bi (1 ≀ bi < ai ≀ 109) β€” the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly. Output Print a single integer β€” the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date. Examples Input 3 5 2 3 1 4 2 Output 2 Input 3 6 1 5 2 4 3 Output 6 Note In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5. In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.
n = int(input()) dates = [0] * n for i in range(n): a, b = [int(k) for k in input().split()] dates[i] = a, b dates.sort() best = -1 for i in range(n): if best <= dates[i][1]: best = dates[i][1] else: best = dates[i][0] print(best)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order. According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai. Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date. Input The first line contains a single positive integer n (1 ≀ n ≀ 5000) β€” the number of exams Valera will take. Each of the next n lines contains two positive space-separated integers ai and bi (1 ≀ bi < ai ≀ 109) β€” the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly. Output Print a single integer β€” the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date. Examples Input 3 5 2 3 1 4 2 Output 2 Input 3 6 1 5 2 4 3 Output 6 Note In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5. In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.
n = int(input()) a = [] d = {} final = 0 final = float(final) for _ in range(n): x, y = map(int, input().split()) a.append([x, y]) a.sort() for _ in range(len(a)): i = a[_] if i[1] >= final: final = i[1] else: final = i[0] print(final)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order. According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai. Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date. Input The first line contains a single positive integer n (1 ≀ n ≀ 5000) β€” the number of exams Valera will take. Each of the next n lines contains two positive space-separated integers ai and bi (1 ≀ bi < ai ≀ 109) β€” the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly. Output Print a single integer β€” the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date. Examples Input 3 5 2 3 1 4 2 Output 2 Input 3 6 1 5 2 4 3 Output 6 Note In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5. In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.
n = int(input()) a = [] b = [] d = {} d2 = {} for _ in range(n): z = input().split() a.append(z[0]) b.append(int(z[1])) for i in range(n): a[i] = "0" * i + a[i] d = dict(zip(a, b)) lst = sorted(d, key=int) day = None for index, i in enumerate(lst): if index > 0: if int(lst[index - 1]) != int(lst[index]): if index - 1 == 0: day = d[lst[index - 1]] else: day = int(i) if d[i] < day else d[i] elif not day: day = max(d[lst[index - 1]], d[lst[index]]) else: day = max(day, d[i]) else: day = d[i] print(day)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP STRING VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NONE FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order. According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai. Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date. Input The first line contains a single positive integer n (1 ≀ n ≀ 5000) β€” the number of exams Valera will take. Each of the next n lines contains two positive space-separated integers ai and bi (1 ≀ bi < ai ≀ 109) β€” the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly. Output Print a single integer β€” the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date. Examples Input 3 5 2 3 1 4 2 Output 2 Input 3 6 1 5 2 4 3 Output 6 Note In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5. In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.
n = int(input()) a = [[int(x) for x in input().split()] for i in range(n)] a.sort(key=lambda x: x[0]) k = [a[0][0], a[0][1]] for i in range(1, n): if a[i][0] > k[0]: k[0] = a[i][0] if a[i][1] >= k[1]: k[1] = a[i][1] else: k[1] = a[i][0] elif a[i][0] == k[0]: k[1] = max(k[1], a[i][1]) print(k[1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order. According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai. Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date. Input The first line contains a single positive integer n (1 ≀ n ≀ 5000) β€” the number of exams Valera will take. Each of the next n lines contains two positive space-separated integers ai and bi (1 ≀ bi < ai ≀ 109) β€” the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly. Output Print a single integer β€” the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date. Examples Input 3 5 2 3 1 4 2 Output 2 Input 3 6 1 5 2 4 3 Output 6 Note In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5. In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.
read = lambda: map(int, input().split()) n = int(input()) t = 0 data = [] for _ in range(n): data.append(tuple(read())) for a, b in sorted(data): if t > b: t = a else: t = b print(t)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order. According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai. Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date. Input The first line contains a single positive integer n (1 ≀ n ≀ 5000) β€” the number of exams Valera will take. Each of the next n lines contains two positive space-separated integers ai and bi (1 ≀ bi < ai ≀ 109) β€” the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly. Output Print a single integer β€” the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date. Examples Input 3 5 2 3 1 4 2 Output 2 Input 3 6 1 5 2 4 3 Output 6 Note In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5. In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.
import sys input = sys.stdin.readline def getInt(): return int(input()) def getVars(): return map(int, input().split()) def getList(): return list(map(int, input().split())) def getStr(): return input().strip() n = getInt() d = {} bb = [] for i in range(n): a, b = getVars() if a not in d: d[a] = [] d[a].append(b) keys = list(d.keys()) last = max(keys) keys.sort() res = min(d[keys[0]]) for day in keys: if res <= min(d[day]): res = max(d[day]) else: res = day print(res)
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order. According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai. Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date. Input The first line contains a single positive integer n (1 ≀ n ≀ 5000) β€” the number of exams Valera will take. Each of the next n lines contains two positive space-separated integers ai and bi (1 ≀ bi < ai ≀ 109) β€” the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly. Output Print a single integer β€” the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date. Examples Input 3 5 2 3 1 4 2 Output 2 Input 3 6 1 5 2 4 3 Output 6 Note In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5. In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.
n = int(input()) date = [[(0) for j in range(2)] for i in range(n)] for i in range(n): date[i][0], date[i][1] = map(int, input().split()) date.sort() prev = date[0][1] for i in range(1, n): if date[i][1] >= prev: prev = date[i][1] else: prev = date[i][0] print(prev)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order. According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai. Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date. Input The first line contains a single positive integer n (1 ≀ n ≀ 5000) β€” the number of exams Valera will take. Each of the next n lines contains two positive space-separated integers ai and bi (1 ≀ bi < ai ≀ 109) β€” the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly. Output Print a single integer β€” the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date. Examples Input 3 5 2 3 1 4 2 Output 2 Input 3 6 1 5 2 4 3 Output 6 Note In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5. In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.
n = int(input()) a = [] for i in range(n): b = list(map(int, input().split())) a.append(b) a.sort(key=lambda i: i[0]) indici = [0] cur_index = a[0][0] for k in range(n): if a[k][0] != cur_index: indici.append(k) cur_index = a[k][0] indici.append(n) ans = [] for m in range(len(indici) - 1): add_array = sorted(a[indici[m] : indici[m + 1]], key=lambda i: i[1]) for z in range(len(add_array)): ans.append(add_array[z]) cur_date = 0 for j in range(n): if j == 0: cur_date = min(ans[j]) elif cur_date <= min(ans[j]): cur_date = min(ans[j]) elif cur_date > min(ans[j]): cur_date = max(ans[j]) print(cur_date)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order. According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai. Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date. Input The first line contains a single positive integer n (1 ≀ n ≀ 5000) β€” the number of exams Valera will take. Each of the next n lines contains two positive space-separated integers ai and bi (1 ≀ bi < ai ≀ 109) β€” the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly. Output Print a single integer β€” the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date. Examples Input 3 5 2 3 1 4 2 Output 2 Input 3 6 1 5 2 4 3 Output 6 Note In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5. In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.
def fir(x): return x[0] def sec(x): return x[1] N = int(input()) exams = [] for i in range(0, N): exam = [int(x) for x in input().split()] exams.append(exam) exams.sort(key=sec) exams.sort(key=fir) temp = exams[0][1] for i in range(1, N): if exams[1][1] >= temp: temp = exams[1][1] else: temp = exams[1][0] del exams[1] print(temp)
FUNC_DEF RETURN VAR NUMBER FUNC_DEF RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
The elections to Berland parliament are happening today. Voting is in full swing! Totally there are n candidates, they are numbered from 1 to n. Based on election results k (1 ≀ k ≀ n) top candidates will take seats in the parliament. After the end of the voting the number of votes for each candidate is calculated. In the resulting table the candidates are ordered by the number of votes. In case of tie (equal number of votes) they are ordered by the time of the last vote given. The candidate with ealier last vote stands higher in the resulting table. So in the resulting table candidates are sorted by the number of votes (more votes stand for the higher place) and if two candidates have equal number of votes they are sorted by the time of last vote (earlier last vote stands for the higher place). There is no way for a candidate with zero votes to take a seat in the parliament. So it is possible that less than k candidates will take a seat in the parliament. In Berland there are m citizens who can vote. Each of them will vote for some candidate. Each citizen will give a vote to exactly one of n candidates. There is no option "against everyone" on the elections. It is not accepted to spoil bulletins or not to go to elections. So each of m citizens will vote for exactly one of n candidates. At the moment a citizens have voted already (1 ≀ a ≀ m). This is an open election, so for each citizen it is known the candidate for which the citizen has voted. Formally, the j-th citizen voted for the candidate g_{j}. The citizens who already voted are numbered in chronological order; i.e. the (j + 1)-th citizen voted after the j-th. The remaining m - a citizens will vote before the end of elections, each of them will vote for one of n candidates. Your task is to determine for each of n candidates one of the three possible outcomes: a candidate will be elected to the parliament regardless of votes of the remaining m - a citizens; a candidate has chance to be elected to the parliament after all n citizens have voted; a candidate has no chances to be elected to the parliament regardless of votes of the remaining m - a citizens. -----Input----- The first line contains four integers n, k, m and a (1 ≀ k ≀ n ≀ 100, 1 ≀ m ≀ 100, 1 ≀ a ≀ m) β€” the number of candidates, the number of seats in the parliament, the number of Berland citizens and the number of citizens who already have voted. The second line contains a sequence of a integers g_1, g_2, ..., g_{a} (1 ≀ g_{j} ≀ n), where g_{j} is the candidate for which the j-th citizen has voted. Citizens who already voted are numbered in increasing order of voting times. -----Output----- Print the sequence consisting of n integers r_1, r_2, ..., r_{n} where: r_{i} = 1 means that the i-th candidate is guaranteed to take seat in the parliament regardless of votes of the remaining m - a citizens; r_{i} = 2 means that the i-th candidate has a chance to take a seat in the parliament, i.e. the remaining m - a citizens can vote in such a way that the candidate will take a seat in the parliament; r_{i} = 3 means that the i-th candidate will not take a seat in the parliament regardless of votes of the remaining m - a citizens. -----Examples----- Input 3 1 5 4 1 2 1 3 Output 1 3 3 Input 3 1 5 3 1 3 1 Output 2 3 2 Input 3 2 5 3 1 3 1 Output 1 2 2
class State: __slots__ = ["candidate", "votes", "last_vote"] def __init__(self, cand, votes, last): self.candidate = cand self.votes = votes self.last_vote = last def beats(self, other, extra): return self.votes + extra > other.votes def main(): candidates, seats, people, voted = map(int, input().split()) votes = [(0) for i in range(candidates)] last_vote = [(0) for i in range(candidates)] if candidates == 1: print(1) return v = list(map(int, input().split())) for t in range(voted): cand = v[t] - 1 votes[cand] += 1 last_vote[cand] = t states = [State(i, votes[i], last_vote[i]) for i in range(candidates)] states = sorted(states, key=lambda x: (x.votes, -x.last_vote)) res = [(0) for i in range(candidates)] for i in range(candidates): if i < candidates - seats: low = candidates - seats if states[i].beats(states[low], people - voted): res[states[i].candidate] = 2 else: res[states[i].candidate] = 3 else: extra = people - voted other = i - 1 place = i if extra == 0 and states[i].votes == 0: res[states[i].candidate] = 3 continue while other >= 0 and extra > 0: needed = states[i].votes - states[other].votes + 1 if needed <= extra: extra -= needed place -= 1 other -= 1 else: break res[states[i].candidate] = ( 1 if place + seats >= candidates and states[i].votes > 0 else 2 ) for i in res: print(i, end=" ") main()
CLASS_DEF ASSIGN VAR LIST STRING STRING STRING FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF RETURN BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
One day Polycarpus stopped by a supermarket on his way home. It turns out that the supermarket is having a special offer for stools. The offer is as follows: if a customer's shopping cart contains at least one stool, the customer gets a 50% discount on the cheapest item in the cart (that is, it becomes two times cheaper). If there are several items with the same minimum price, the discount is available for only one of them! Polycarpus has k carts, and he wants to buy up all stools and pencils from the supermarket. Help him distribute the stools and the pencils among the shopping carts, so that the items' total price (including the discounts) is the least possible. Polycarpus must use all k carts to purchase the items, no shopping cart can remain empty. Each shopping cart can contain an arbitrary number of stools and/or pencils. Input The first input line contains two integers n and k (1 ≀ k ≀ n ≀ 103) β€” the number of items in the supermarket and the number of carts, correspondingly. Next n lines describe the items as "ci ti" (without the quotes), where ci (1 ≀ ci ≀ 109) is an integer denoting the price of the i-th item, ti (1 ≀ ti ≀ 2) is an integer representing the type of item i (1 for a stool and 2 for a pencil). The numbers in the lines are separated by single spaces. Output In the first line print a single real number with exactly one decimal place β€” the minimum total price of the items, including the discounts. In the following k lines print the descriptions of the items in the carts. In the i-th line print the description of the i-th cart as "t b1 b2 ... bt" (without the quotes), where t is the number of items in the i-th cart, and the sequence b1, b2, ..., bt (1 ≀ bj ≀ n) gives the indices of items to put in this cart in the optimal distribution. All indices of items in all carts should be pairwise different, each item must belong to exactly one cart. You can print the items in carts and the carts themselves in any order. The items are numbered from 1 to n in the order in which they are specified in the input. If there are multiple optimal distributions, you are allowed to print any of them. Examples Input 3 2 2 1 3 2 3 1 Output 5.5 2 1 2 1 3 Input 4 3 4 1 1 2 2 2 3 2 Output 8.0 1 1 2 4 2 1 3 Note In the first sample case the first cart should contain the 1st and 2nd items, and the second cart should contain the 3rd item. This way each cart has a stool and each cart has a 50% discount for the cheapest item. The total price of all items will be: 2Β·0.5 + (3 + 3Β·0.5) = 1 + 4.5 = 5.5.
l = input().split() n = int(l[0]) k = int(l[1]) qofp = [] qofs = [] for i in range(n): l = input().split() if int(l[1]) == 1: qofs.append((int(l[0]), i + 1)) else: qofp.append((int(l[0]), i + 1)) qofs.sort() qofs.reverse() fina = [[] for i in range(k)] if len(qofs) < k: price = 0 i = 0 while qofs != []: fina[i].append(qofs[0][1]) price += qofs[0][0] * 0.5 qofs.pop(0) i += 1 for j in range(i, k - 1): fina[j].append(qofp[0][1]) price += qofp[0][0] qofp.pop(0) for i in qofp: fina[k - 1].append(i[1]) price += i[0] print("{:.1f}".format(price)) for i in fina: print(len(i), end=" ") for j in i: print(j, end=" ") print() else: price = 0 for i in range(k - 1): fina[i].append(qofs[0][1]) price += 0.5 * qofs[0][0] qofs.pop(0) fool = [] while qofs != []: fina[k - 1].append(qofs[0][1]) fool.append(qofs[0][0]) price += qofs[0][0] qofs.pop(0) while qofp != []: fina[k - 1].append(qofp[0][1]) fool.append(qofp[0][0]) price += qofp[0][0] qofp.pop(0) z = min(fool) price -= z / 2 print("{:.1f}".format(price)) for i in fina: print(len(i), end=" ") for j in i: print(j, end=" ") print()
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR LIST EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR BIN_OP NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST WHILE VAR LIST EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR LIST EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
One day Polycarpus stopped by a supermarket on his way home. It turns out that the supermarket is having a special offer for stools. The offer is as follows: if a customer's shopping cart contains at least one stool, the customer gets a 50% discount on the cheapest item in the cart (that is, it becomes two times cheaper). If there are several items with the same minimum price, the discount is available for only one of them! Polycarpus has k carts, and he wants to buy up all stools and pencils from the supermarket. Help him distribute the stools and the pencils among the shopping carts, so that the items' total price (including the discounts) is the least possible. Polycarpus must use all k carts to purchase the items, no shopping cart can remain empty. Each shopping cart can contain an arbitrary number of stools and/or pencils. Input The first input line contains two integers n and k (1 ≀ k ≀ n ≀ 103) β€” the number of items in the supermarket and the number of carts, correspondingly. Next n lines describe the items as "ci ti" (without the quotes), where ci (1 ≀ ci ≀ 109) is an integer denoting the price of the i-th item, ti (1 ≀ ti ≀ 2) is an integer representing the type of item i (1 for a stool and 2 for a pencil). The numbers in the lines are separated by single spaces. Output In the first line print a single real number with exactly one decimal place β€” the minimum total price of the items, including the discounts. In the following k lines print the descriptions of the items in the carts. In the i-th line print the description of the i-th cart as "t b1 b2 ... bt" (without the quotes), where t is the number of items in the i-th cart, and the sequence b1, b2, ..., bt (1 ≀ bj ≀ n) gives the indices of items to put in this cart in the optimal distribution. All indices of items in all carts should be pairwise different, each item must belong to exactly one cart. You can print the items in carts and the carts themselves in any order. The items are numbered from 1 to n in the order in which they are specified in the input. If there are multiple optimal distributions, you are allowed to print any of them. Examples Input 3 2 2 1 3 2 3 1 Output 5.5 2 1 2 1 3 Input 4 3 4 1 1 2 2 2 3 2 Output 8.0 1 1 2 4 2 1 3 Note In the first sample case the first cart should contain the 1st and 2nd items, and the second cart should contain the 3rd item. This way each cart has a stool and each cart has a 50% discount for the cheapest item. The total price of all items will be: 2Β·0.5 + (3 + 3Β·0.5) = 1 + 4.5 = 5.5.
n, k = list(map(int, input().split())) p = [[], []] for i in range(1, n + 1): c, t = map(int, input().split()) p[t > 1].append((c, i)) if k > len(p[0]): l = k - len(p[0]) - 1 print(sum(c for c, i in p[0]) / 2 + sum(c for c, i in p[1])) print("\n".join("1 " + str(i) for c, i in p[0])) print("\n".join("1 " + str(i) for c, i in p[1][:l])) print(len(p[1]) - l, " ".join(str(i) for c, i in p[1][l:])) else: p[1].sort() p[0].sort(reverse=True) print( sum(c for c, i in p[0][: k - 1]) / 2 + sum(c for c, i in p[0][k - 1 :]) + sum(c for c, i in p[1]) - min(c for c, i in p[1] + p[0][k - 1 :]) / 2 ) print("\n".join("1 " + str(i) for c, i in p[0][: k - 1])) print( n - k + 1, " ".join(str(i) for c, i in p[0][k - 1 :]), " ".join(str(i) for c, i in p[1]), )
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST LIST LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR IF VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP STRING FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP STRING FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP STRING FUNC_CALL VAR VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR NUMBER
One day Polycarpus stopped by a supermarket on his way home. It turns out that the supermarket is having a special offer for stools. The offer is as follows: if a customer's shopping cart contains at least one stool, the customer gets a 50% discount on the cheapest item in the cart (that is, it becomes two times cheaper). If there are several items with the same minimum price, the discount is available for only one of them! Polycarpus has k carts, and he wants to buy up all stools and pencils from the supermarket. Help him distribute the stools and the pencils among the shopping carts, so that the items' total price (including the discounts) is the least possible. Polycarpus must use all k carts to purchase the items, no shopping cart can remain empty. Each shopping cart can contain an arbitrary number of stools and/or pencils. Input The first input line contains two integers n and k (1 ≀ k ≀ n ≀ 103) β€” the number of items in the supermarket and the number of carts, correspondingly. Next n lines describe the items as "ci ti" (without the quotes), where ci (1 ≀ ci ≀ 109) is an integer denoting the price of the i-th item, ti (1 ≀ ti ≀ 2) is an integer representing the type of item i (1 for a stool and 2 for a pencil). The numbers in the lines are separated by single spaces. Output In the first line print a single real number with exactly one decimal place β€” the minimum total price of the items, including the discounts. In the following k lines print the descriptions of the items in the carts. In the i-th line print the description of the i-th cart as "t b1 b2 ... bt" (without the quotes), where t is the number of items in the i-th cart, and the sequence b1, b2, ..., bt (1 ≀ bj ≀ n) gives the indices of items to put in this cart in the optimal distribution. All indices of items in all carts should be pairwise different, each item must belong to exactly one cart. You can print the items in carts and the carts themselves in any order. The items are numbered from 1 to n in the order in which they are specified in the input. If there are multiple optimal distributions, you are allowed to print any of them. Examples Input 3 2 2 1 3 2 3 1 Output 5.5 2 1 2 1 3 Input 4 3 4 1 1 2 2 2 3 2 Output 8.0 1 1 2 4 2 1 3 Note In the first sample case the first cart should contain the 1st and 2nd items, and the second cart should contain the 3rd item. This way each cart has a stool and each cart has a 50% discount for the cheapest item. The total price of all items will be: 2Β·0.5 + (3 + 3Β·0.5) = 1 + 4.5 = 5.5.
I = lambda: map(int, input().split()) n, k = I() fs = [] sc = [] Min = 1000000000.0 for i in range(n): c, t = I() Min = min(c, Min) if t == 1: fs += [(c, i + 1)] else: sc += [(c, i + 1)] fs = sorted(fs, reverse=True) z = [[] for i in range(k)] sz = 0 ans = 0 for c, i in fs: z[sz] += [i] if sz < k - 1: ans += c sz += 1 else: ans += 2 * c for c, i in sc: z[sz] += [i] ans += 2 * c if sz < k - 1: sz += 1 if len(fs) >= k: ans -= Min print("%.1f" % (1.0 * ans / 2.0)) for i in z: print(len(i), end=" ") for j in range(len(i)): if j == len(i) - 1: print(i[j]) else: print(i[j], end=" ")
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR LIST VAR BIN_OP VAR NUMBER VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR LIST VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP NUMBER VAR FOR VAR VAR VAR VAR VAR LIST VAR VAR BIN_OP NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
Professor GukiZ is concerned about making his way to school, because massive piles of boxes are blocking his way. In total there are n piles of boxes, arranged in a line, from left to right, i-th pile (1 ≀ i ≀ n) containing ai boxes. Luckily, m students are willing to help GukiZ by removing all the boxes from his way. Students are working simultaneously. At time 0, all students are located left of the first pile. It takes one second for every student to move from this position to the first pile, and after that, every student must start performing sequence of two possible operations, each taking one second to complete. Possible operations are: 1. If i β‰  n, move from pile i to pile i + 1; 2. If pile located at the position of student is not empty, remove one box from it. GukiZ's students aren't smart at all, so they need you to tell them how to remove boxes before professor comes (he is very impatient man, and doesn't want to wait). They ask you to calculate minumum time t in seconds for which they can remove all the boxes from GukiZ's way. Note that students can be positioned in any manner after t seconds, but all the boxes must be removed. Input The first line contains two integers n and m (1 ≀ n, m ≀ 105), the number of piles of boxes and the number of GukiZ's students. The second line contains n integers a1, a2, ... an (0 ≀ ai ≀ 109) where ai represents the number of boxes on i-th pile. It's guaranteed that at least one pile of is non-empty. Output In a single line, print one number, minimum time needed to remove all the boxes in seconds. Examples Input 2 1 1 1 Output 4 Input 3 2 1 0 2 Output 5 Input 4 100 3 4 5 4 Output 5 Note First sample: Student will first move to the first pile (1 second), then remove box from first pile (1 second), then move to the second pile (1 second) and finally remove the box from second pile (1 second). Second sample: One of optimal solutions is to send one student to remove a box from the first pile and a box from the third pile, and send another student to remove a box from the third pile. Overall, 5 seconds. Third sample: With a lot of available students, send three of them to remove boxes from the first pile, four of them to remove boxes from the second pile, five of them to remove boxes from the third pile, and four of them to remove boxes from the fourth pile. Process will be over in 5 seconds, when removing the boxes from the last pile is finished.
n, m = map(int, input().split()) a = list(map(int, input().split())) def c(t): s, r, p, b = m, 0, n, 0 while 1: while b == 0: if p == 0: return 1 p -= 1 b = a[p] if r == 0: if s == 0: return 0 r = t - p - 1 s -= 1 d = min(b, r) b -= d r -= d l, h = 0, n + sum(a) + 9 while h - l > 1: md = (l + h) // 2 if c(md): h = md else: l = md print(h)
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 FUNC_DEF ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE NUMBER WHILE VAR NUMBER IF VAR NUMBER RETURN NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Professor GukiZ is concerned about making his way to school, because massive piles of boxes are blocking his way. In total there are n piles of boxes, arranged in a line, from left to right, i-th pile (1 ≀ i ≀ n) containing ai boxes. Luckily, m students are willing to help GukiZ by removing all the boxes from his way. Students are working simultaneously. At time 0, all students are located left of the first pile. It takes one second for every student to move from this position to the first pile, and after that, every student must start performing sequence of two possible operations, each taking one second to complete. Possible operations are: 1. If i β‰  n, move from pile i to pile i + 1; 2. If pile located at the position of student is not empty, remove one box from it. GukiZ's students aren't smart at all, so they need you to tell them how to remove boxes before professor comes (he is very impatient man, and doesn't want to wait). They ask you to calculate minumum time t in seconds for which they can remove all the boxes from GukiZ's way. Note that students can be positioned in any manner after t seconds, but all the boxes must be removed. Input The first line contains two integers n and m (1 ≀ n, m ≀ 105), the number of piles of boxes and the number of GukiZ's students. The second line contains n integers a1, a2, ... an (0 ≀ ai ≀ 109) where ai represents the number of boxes on i-th pile. It's guaranteed that at least one pile of is non-empty. Output In a single line, print one number, minimum time needed to remove all the boxes in seconds. Examples Input 2 1 1 1 Output 4 Input 3 2 1 0 2 Output 5 Input 4 100 3 4 5 4 Output 5 Note First sample: Student will first move to the first pile (1 second), then remove box from first pile (1 second), then move to the second pile (1 second) and finally remove the box from second pile (1 second). Second sample: One of optimal solutions is to send one student to remove a box from the first pile and a box from the third pile, and send another student to remove a box from the third pile. Overall, 5 seconds. Third sample: With a lot of available students, send three of them to remove boxes from the first pile, four of them to remove boxes from the second pile, five of them to remove boxes from the third pile, and four of them to remove boxes from the fourth pile. Process will be over in 5 seconds, when removing the boxes from the last pile is finished.
N, M = map(int, input().split()) ara = list(map(int, input().split())) while ara[-1] == 0: ara.pop() ara.insert(0, 0) def check(Time): piles = ara[:] last_pile_no = len(piles) - 1 for i in range(M): i_time = Time - last_pile_no while True: if i_time >= piles[last_pile_no]: i_time -= piles[last_pile_no] last_pile_no -= 1 if last_pile_no == 0: return True else: piles[last_pile_no] -= i_time break return False lo = 0 hi = int(sum(ara) / M) + len(ara) + 1 ans = 0 while lo <= hi: mid = int((lo + hi) / 2) if check(mid): ans = mid hi = mid - 1 else: lo = mid + 1 print(ans)
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 WHILE VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Professor GukiZ is concerned about making his way to school, because massive piles of boxes are blocking his way. In total there are n piles of boxes, arranged in a line, from left to right, i-th pile (1 ≀ i ≀ n) containing ai boxes. Luckily, m students are willing to help GukiZ by removing all the boxes from his way. Students are working simultaneously. At time 0, all students are located left of the first pile. It takes one second for every student to move from this position to the first pile, and after that, every student must start performing sequence of two possible operations, each taking one second to complete. Possible operations are: 1. If i β‰  n, move from pile i to pile i + 1; 2. If pile located at the position of student is not empty, remove one box from it. GukiZ's students aren't smart at all, so they need you to tell them how to remove boxes before professor comes (he is very impatient man, and doesn't want to wait). They ask you to calculate minumum time t in seconds for which they can remove all the boxes from GukiZ's way. Note that students can be positioned in any manner after t seconds, but all the boxes must be removed. Input The first line contains two integers n and m (1 ≀ n, m ≀ 105), the number of piles of boxes and the number of GukiZ's students. The second line contains n integers a1, a2, ... an (0 ≀ ai ≀ 109) where ai represents the number of boxes on i-th pile. It's guaranteed that at least one pile of is non-empty. Output In a single line, print one number, minimum time needed to remove all the boxes in seconds. Examples Input 2 1 1 1 Output 4 Input 3 2 1 0 2 Output 5 Input 4 100 3 4 5 4 Output 5 Note First sample: Student will first move to the first pile (1 second), then remove box from first pile (1 second), then move to the second pile (1 second) and finally remove the box from second pile (1 second). Second sample: One of optimal solutions is to send one student to remove a box from the first pile and a box from the third pile, and send another student to remove a box from the third pile. Overall, 5 seconds. Third sample: With a lot of available students, send three of them to remove boxes from the first pile, four of them to remove boxes from the second pile, five of them to remove boxes from the third pile, and four of them to remove boxes from the fourth pile. Process will be over in 5 seconds, when removing the boxes from the last pile is finished.
NUM_OF_PILES, NUM_OF_STUDENTS = map(int, input().split()) boxes = list(map(int, input().split())) lower = 2 upper = sum(boxes) + NUM_OF_PILES while lower < upper: guess = (lower + upper) // 2 b = boxes.copy() pile = NUM_OF_PILES - 1 for i in range(NUM_OF_STUDENTS): while pile >= 0 and b[pile] == 0: pile -= 1 time = guess - pile - 1 if time <= 0: break while pile >= 0 and b[pile] <= time: time -= b[pile] pile -= 1 if pile >= 0: b[pile] -= time if pile < 0: upper = guess else: lower = guess + 1 print(upper)
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 ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Professor GukiZ is concerned about making his way to school, because massive piles of boxes are blocking his way. In total there are n piles of boxes, arranged in a line, from left to right, i-th pile (1 ≀ i ≀ n) containing ai boxes. Luckily, m students are willing to help GukiZ by removing all the boxes from his way. Students are working simultaneously. At time 0, all students are located left of the first pile. It takes one second for every student to move from this position to the first pile, and after that, every student must start performing sequence of two possible operations, each taking one second to complete. Possible operations are: 1. If i β‰  n, move from pile i to pile i + 1; 2. If pile located at the position of student is not empty, remove one box from it. GukiZ's students aren't smart at all, so they need you to tell them how to remove boxes before professor comes (he is very impatient man, and doesn't want to wait). They ask you to calculate minumum time t in seconds for which they can remove all the boxes from GukiZ's way. Note that students can be positioned in any manner after t seconds, but all the boxes must be removed. Input The first line contains two integers n and m (1 ≀ n, m ≀ 105), the number of piles of boxes and the number of GukiZ's students. The second line contains n integers a1, a2, ... an (0 ≀ ai ≀ 109) where ai represents the number of boxes on i-th pile. It's guaranteed that at least one pile of is non-empty. Output In a single line, print one number, minimum time needed to remove all the boxes in seconds. Examples Input 2 1 1 1 Output 4 Input 3 2 1 0 2 Output 5 Input 4 100 3 4 5 4 Output 5 Note First sample: Student will first move to the first pile (1 second), then remove box from first pile (1 second), then move to the second pile (1 second) and finally remove the box from second pile (1 second). Second sample: One of optimal solutions is to send one student to remove a box from the first pile and a box from the third pile, and send another student to remove a box from the third pile. Overall, 5 seconds. Third sample: With a lot of available students, send three of them to remove boxes from the first pile, four of them to remove boxes from the second pile, five of them to remove boxes from the third pile, and four of them to remove boxes from the fourth pile. Process will be over in 5 seconds, when removing the boxes from the last pile is finished.
f = lambda: map(int, input().split()) n, m = f() h = list(f())[::-1] def g(t, m): t -= n d = 0 for u in h: t += 1 if u > d: if t < 1: return 1 u -= d d = -u % t m -= (u + d) // t if m < 0: return 1 else: d -= u return 0 a, b = 0, int(110000000000000.0) while b - a > 1: c = (b + a) // 2 if g(c, m): a = c else: b = c print(b + 1)
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 NUMBER FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR VAR IF VAR NUMBER RETURN NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER RETURN NUMBER VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Professor GukiZ is concerned about making his way to school, because massive piles of boxes are blocking his way. In total there are n piles of boxes, arranged in a line, from left to right, i-th pile (1 ≀ i ≀ n) containing ai boxes. Luckily, m students are willing to help GukiZ by removing all the boxes from his way. Students are working simultaneously. At time 0, all students are located left of the first pile. It takes one second for every student to move from this position to the first pile, and after that, every student must start performing sequence of two possible operations, each taking one second to complete. Possible operations are: 1. If i β‰  n, move from pile i to pile i + 1; 2. If pile located at the position of student is not empty, remove one box from it. GukiZ's students aren't smart at all, so they need you to tell them how to remove boxes before professor comes (he is very impatient man, and doesn't want to wait). They ask you to calculate minumum time t in seconds for which they can remove all the boxes from GukiZ's way. Note that students can be positioned in any manner after t seconds, but all the boxes must be removed. Input The first line contains two integers n and m (1 ≀ n, m ≀ 105), the number of piles of boxes and the number of GukiZ's students. The second line contains n integers a1, a2, ... an (0 ≀ ai ≀ 109) where ai represents the number of boxes on i-th pile. It's guaranteed that at least one pile of is non-empty. Output In a single line, print one number, minimum time needed to remove all the boxes in seconds. Examples Input 2 1 1 1 Output 4 Input 3 2 1 0 2 Output 5 Input 4 100 3 4 5 4 Output 5 Note First sample: Student will first move to the first pile (1 second), then remove box from first pile (1 second), then move to the second pile (1 second) and finally remove the box from second pile (1 second). Second sample: One of optimal solutions is to send one student to remove a box from the first pile and a box from the third pile, and send another student to remove a box from the third pile. Overall, 5 seconds. Third sample: With a lot of available students, send three of them to remove boxes from the first pile, four of them to remove boxes from the second pile, five of them to remove boxes from the third pile, and four of them to remove boxes from the fourth pile. Process will be over in 5 seconds, when removing the boxes from the last pile is finished.
def check(time): box = A[:] now = last for student in range(m): rest = time - now - 1 if rest <= 0: return False while now >= 0 and rest >= 0: if box[now] <= rest: rest -= box[now] now -= 1 else: box[now] -= rest break if now == -1: return True return False n, m = map(int, input().split()) A = list(map(int, input().split())) last = n - 1 while A[last] == 0: last -= 1 low, high = 1, n + sum(A) + 1 while high - low > 1: mid = (high + low) // 2 if check(mid) == False: low = mid else: high = mid print(high)
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER 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 VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Professor GukiZ is concerned about making his way to school, because massive piles of boxes are blocking his way. In total there are n piles of boxes, arranged in a line, from left to right, i-th pile (1 ≀ i ≀ n) containing ai boxes. Luckily, m students are willing to help GukiZ by removing all the boxes from his way. Students are working simultaneously. At time 0, all students are located left of the first pile. It takes one second for every student to move from this position to the first pile, and after that, every student must start performing sequence of two possible operations, each taking one second to complete. Possible operations are: 1. If i β‰  n, move from pile i to pile i + 1; 2. If pile located at the position of student is not empty, remove one box from it. GukiZ's students aren't smart at all, so they need you to tell them how to remove boxes before professor comes (he is very impatient man, and doesn't want to wait). They ask you to calculate minumum time t in seconds for which they can remove all the boxes from GukiZ's way. Note that students can be positioned in any manner after t seconds, but all the boxes must be removed. Input The first line contains two integers n and m (1 ≀ n, m ≀ 105), the number of piles of boxes and the number of GukiZ's students. The second line contains n integers a1, a2, ... an (0 ≀ ai ≀ 109) where ai represents the number of boxes on i-th pile. It's guaranteed that at least one pile of is non-empty. Output In a single line, print one number, minimum time needed to remove all the boxes in seconds. Examples Input 2 1 1 1 Output 4 Input 3 2 1 0 2 Output 5 Input 4 100 3 4 5 4 Output 5 Note First sample: Student will first move to the first pile (1 second), then remove box from first pile (1 second), then move to the second pile (1 second) and finally remove the box from second pile (1 second). Second sample: One of optimal solutions is to send one student to remove a box from the first pile and a box from the third pile, and send another student to remove a box from the third pile. Overall, 5 seconds. Third sample: With a lot of available students, send three of them to remove boxes from the first pile, four of them to remove boxes from the second pile, five of them to remove boxes from the third pile, and four of them to remove boxes from the fourth pile. Process will be over in 5 seconds, when removing the boxes from the last pile is finished.
def read_data(): n, m = map(int, input().split()) A = list(map(int, input().split())) while A and not A[-1]: del A[-1] return len(A), m, A def solve(n, m, A): total = sum(A) upper = n + (total + m - 1) // m lower = n while lower + 1 < upper: mid = (lower + upper) // 2 if is_enough(mid, n, m, A): upper = mid else: lower = mid return lower + 1 def is_enough(t, n, m, A): pool = 0 for i in range(n - 1, -1, -1): a = A[i] delta = t - i - 1 b = (a - pool + delta - 1) // delta m -= b if m < 0: return False pool += b * delta - a return True n, m, A = read_data() print(solve(n, m, A))
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 WHILE VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER RETURN NUMBER VAR BIN_OP BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Professor GukiZ is concerned about making his way to school, because massive piles of boxes are blocking his way. In total there are n piles of boxes, arranged in a line, from left to right, i-th pile (1 ≀ i ≀ n) containing ai boxes. Luckily, m students are willing to help GukiZ by removing all the boxes from his way. Students are working simultaneously. At time 0, all students are located left of the first pile. It takes one second for every student to move from this position to the first pile, and after that, every student must start performing sequence of two possible operations, each taking one second to complete. Possible operations are: 1. If i β‰  n, move from pile i to pile i + 1; 2. If pile located at the position of student is not empty, remove one box from it. GukiZ's students aren't smart at all, so they need you to tell them how to remove boxes before professor comes (he is very impatient man, and doesn't want to wait). They ask you to calculate minumum time t in seconds for which they can remove all the boxes from GukiZ's way. Note that students can be positioned in any manner after t seconds, but all the boxes must be removed. Input The first line contains two integers n and m (1 ≀ n, m ≀ 105), the number of piles of boxes and the number of GukiZ's students. The second line contains n integers a1, a2, ... an (0 ≀ ai ≀ 109) where ai represents the number of boxes on i-th pile. It's guaranteed that at least one pile of is non-empty. Output In a single line, print one number, minimum time needed to remove all the boxes in seconds. Examples Input 2 1 1 1 Output 4 Input 3 2 1 0 2 Output 5 Input 4 100 3 4 5 4 Output 5 Note First sample: Student will first move to the first pile (1 second), then remove box from first pile (1 second), then move to the second pile (1 second) and finally remove the box from second pile (1 second). Second sample: One of optimal solutions is to send one student to remove a box from the first pile and a box from the third pile, and send another student to remove a box from the third pile. Overall, 5 seconds. Third sample: With a lot of available students, send three of them to remove boxes from the first pile, four of them to remove boxes from the second pile, five of them to remove boxes from the third pile, and four of them to remove boxes from the fourth pile. Process will be over in 5 seconds, when removing the boxes from the last pile is finished.
from sys import stdin n, m = map(int, stdin.buffer.readline().split()) a = list(map(int, stdin.buffer.readline().split())) max_not_zero = 0 for index, value in enumerate(a): if value > 0: max_not_zero = index total = sum(a) def remove(t): boxes = 0 s = m for i in range(max_not_zero + 1): boxes += a[i] while boxes + (i + 1) >= t: boxes -= t - i - 1 s -= 1 if s < 0: return False if s == 0: if boxes > 0: return False return True def binary_search(left, right): mid = int((left + right) // 2) if right - left <= 1 and remove(left): return left if remove(mid): return binary_search(left, mid) else: return binary_search(mid + 1, right) print(binary_search(2, total + n))
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 VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR WHILE BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR RETURN VAR IF FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR
You have an array a[1], a[2], ..., a[n], containing distinct integers from 1 to n. Your task is to sort this array in increasing order with the following operation (you may need to apply it multiple times): choose two indexes, i and j (1 ≀ i < j ≀ n; (j - i + 1) is a prime number); swap the elements on positions i and j; in other words, you are allowed to apply the following sequence of assignments: tmp = a[i], a[i] = a[j], a[j] = tmp (tmp is a temporary variable). You do not need to minimize the number of used operations. However, you need to make sure that there are at most 5n operations. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5). The next line contains n distinct integers a[1], a[2], ..., a[n] (1 ≀ a[i] ≀ n). -----Output----- In the first line, print integer k (0 ≀ k ≀ 5n) β€” the number of used operations. Next, print the operations. Each operation must be printed as "i j" (1 ≀ i < j ≀ n; (j - i + 1) is a prime). If there are multiple answers, you can print any of them. -----Examples----- Input 3 3 2 1 Output 1 1 3 Input 2 1 2 Output 0 Input 4 4 2 3 1 Output 3 2 4 1 2 2 4
def sieve(n): p = 2 while p * p <= n: if prime[p] == False: for i in range(p * p, n + 1, p): prime[i] = True p += 1 n = int(input()) prime = [(False) for i in range(n + 2)] prime[0] = 1 prime[1] = 1 sieve(n + 1) lis = [0] + list(map(int, input().split())) ind = [0] * (n + 1) ct = 0 for i in range(1, n + 1): ind[lis[i]] = i ans = [] for i in range(1, n + 1): j = ind[i] while j > i: t = i while prime[j - t + 1]: t += 1 ct += 1 ind[lis[j]] = t ind[lis[t]] = j ans.append([t, j]) lis[j], lis[t] = lis[t], lis[j] j = t print(ct) for i in ans: print(*i)
FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR WHILE VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
You have an array a[1], a[2], ..., a[n], containing distinct integers from 1 to n. Your task is to sort this array in increasing order with the following operation (you may need to apply it multiple times): choose two indexes, i and j (1 ≀ i < j ≀ n; (j - i + 1) is a prime number); swap the elements on positions i and j; in other words, you are allowed to apply the following sequence of assignments: tmp = a[i], a[i] = a[j], a[j] = tmp (tmp is a temporary variable). You do not need to minimize the number of used operations. However, you need to make sure that there are at most 5n operations. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5). The next line contains n distinct integers a[1], a[2], ..., a[n] (1 ≀ a[i] ≀ n). -----Output----- In the first line, print integer k (0 ≀ k ≀ 5n) β€” the number of used operations. Next, print the operations. Each operation must be printed as "i j" (1 ≀ i < j ≀ n; (j - i + 1) is a prime). If there are multiple answers, you can print any of them. -----Examples----- Input 3 3 2 1 Output 1 1 3 Input 2 1 2 Output 0 Input 4 4 2 3 1 Output 3 2 4 1 2 2 4
def prime(n): n += 1 p = [False] * n for i in range(3, int(n**0.5) + 1, 2): u, v = i * i, 2 * i if not p[i]: p[u::v] = [True] * ((n - u - 1) // v + 1) p[4::2] = [True] * ((n - 3) // 2) return p n = int(input()) t = list(enumerate(list(map(int, input().split())), 1)) t.sort(key=lambda x: x[1]) t = [x[0] for x in t] u, v, q = list(range(n + 1)), list(range(n + 1)), [] p = prime(n + 1) for i, j in enumerate(t, 1): while i != u[j]: k = i while p[u[j] - k + 1]: k += 1 q.append(str(k) + " " + str(u[j])) a, b = u[j], v[k] u[b], u[j], v[a], v[k] = a, k, b, j print(len(q)) print("\n".join(q))
FUNC_DEF VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR BIN_OP LIST NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP LIST NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER LIST ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR VAR WHILE VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You have an array a[1], a[2], ..., a[n], containing distinct integers from 1 to n. Your task is to sort this array in increasing order with the following operation (you may need to apply it multiple times): choose two indexes, i and j (1 ≀ i < j ≀ n; (j - i + 1) is a prime number); swap the elements on positions i and j; in other words, you are allowed to apply the following sequence of assignments: tmp = a[i], a[i] = a[j], a[j] = tmp (tmp is a temporary variable). You do not need to minimize the number of used operations. However, you need to make sure that there are at most 5n operations. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5). The next line contains n distinct integers a[1], a[2], ..., a[n] (1 ≀ a[i] ≀ n). -----Output----- In the first line, print integer k (0 ≀ k ≀ 5n) β€” the number of used operations. Next, print the operations. Each operation must be printed as "i j" (1 ≀ i < j ≀ n; (j - i + 1) is a prime). If there are multiple answers, you can print any of them. -----Examples----- Input 3 3 2 1 Output 1 1 3 Input 2 1 2 Output 0 Input 4 4 2 3 1 Output 3 2 4 1 2 2 4
import sys input = sys.stdin.readline I = lambda: list(map(int, input().split())) (n,) = I() l = I() an = [] sv = [0] * (10**5 + 10) for i in range(2, len(sv)): if not sv[i]: sv[i] = i for j in range(2 * i, len(sv), i): sv[j] = 1 sv[0] = sv[1] = 1 for i in range(2, len(sv)): if not sv[i] - 1: sv[i] = sv[i - 1] ix = [0] * (n + 1) for i in range(n): ix[l[i]] = i for i in range(n): ct = 0 while l[i] != i + 1 and ct < 5: cr = ix[i + 1] pr = sv[cr - i + 1] an.append(sorted([cr + 1, cr - pr + 2])) l[cr], l[cr - pr + 1] = l[cr - pr + 1], l[cr] ix[l[cr]], ix[l[cr - pr + 1]] = ix[l[cr - pr + 1]], ix[l[cr]] ct += 1 print(len(an)) for i in an: print(*i)
IMPORT ASSIGN VAR 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 LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
You have an array a[1], a[2], ..., a[n], containing distinct integers from 1 to n. Your task is to sort this array in increasing order with the following operation (you may need to apply it multiple times): choose two indexes, i and j (1 ≀ i < j ≀ n; (j - i + 1) is a prime number); swap the elements on positions i and j; in other words, you are allowed to apply the following sequence of assignments: tmp = a[i], a[i] = a[j], a[j] = tmp (tmp is a temporary variable). You do not need to minimize the number of used operations. However, you need to make sure that there are at most 5n operations. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5). The next line contains n distinct integers a[1], a[2], ..., a[n] (1 ≀ a[i] ≀ n). -----Output----- In the first line, print integer k (0 ≀ k ≀ 5n) β€” the number of used operations. Next, print the operations. Each operation must be printed as "i j" (1 ≀ i < j ≀ n; (j - i + 1) is a prime). If there are multiple answers, you can print any of them. -----Examples----- Input 3 3 2 1 Output 1 1 3 Input 2 1 2 Output 0 Input 4 4 2 3 1 Output 3 2 4 1 2 2 4
n = int(input()) ar = list(map(int, input().split())) x = 10**5 + 3 seva_nahui_esli_ne_paidet_tebe_pizda = [True] * x seva_nahui_esli_ne_paidet_tebe_pizda[1] = False for i in range(2, x): if seva_nahui_esli_ne_paidet_tebe_pizda[i]: for j in range(i * 2, x, i): seva_nahui_esli_ne_paidet_tebe_pizda[j] = False ans = [] lol = [0] * (n + 1) for i in range(n): lol[ar[i]] = i def kinda_swap(l, r): if l >= r: return len = r - l + 1 if seva_nahui_esli_ne_paidet_tebe_pizda[len]: ar[l], ar[r] = ar[r], ar[l] ans.append([l + 1, r + 1]) return if len % 2 == 0: kinda_swap(l, l + 1) kinda_swap(l + 1, r) kinda_swap(l, l + 1) return for s in range(2, len + 1): if ( seva_nahui_esli_ne_paidet_tebe_pizda[s] and seva_nahui_esli_ne_paidet_tebe_pizda[r - l - s + 1 + 1] ): kinda_swap(l, l + s - 1) kinda_swap(l + s - 1, r) kinda_swap(l, l + s - 1) return for i in range(1, n): x = lol[i] lol[ar[x]] = i - 1 lol[ar[i - 1]] = x kinda_swap(i - 1, x) print(len(ans)) for elem in ans: print(elem[0], elem[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 BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_DEF IF VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
You have an array a[1], a[2], ..., a[n], containing distinct integers from 1 to n. Your task is to sort this array in increasing order with the following operation (you may need to apply it multiple times): choose two indexes, i and j (1 ≀ i < j ≀ n; (j - i + 1) is a prime number); swap the elements on positions i and j; in other words, you are allowed to apply the following sequence of assignments: tmp = a[i], a[i] = a[j], a[j] = tmp (tmp is a temporary variable). You do not need to minimize the number of used operations. However, you need to make sure that there are at most 5n operations. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5). The next line contains n distinct integers a[1], a[2], ..., a[n] (1 ≀ a[i] ≀ n). -----Output----- In the first line, print integer k (0 ≀ k ≀ 5n) β€” the number of used operations. Next, print the operations. Each operation must be printed as "i j" (1 ≀ i < j ≀ n; (j - i + 1) is a prime). If there are multiple answers, you can print any of them. -----Examples----- Input 3 3 2 1 Output 1 1 3 Input 2 1 2 Output 0 Input 4 4 2 3 1 Output 3 2 4 1 2 2 4
from sys import stdin, stdout nmbr = lambda: int(stdin.readline()) lst = lambda: list(map(int, stdin.readline().split())) N = 10**5 + 5 primes = [1] * N p = 2 while p * p <= N: if primes[p]: for j in range(p * p, N, p): primes[j] = 0 p += 1 primes[0] = primes[1] = 0 for i in range(2, N): if primes[i] == 0: primes[i] = primes[i - 1] else: primes[i] = i for _ in range(1): n = nmbr() a = lst() ind = [0] * (1 + n) for i in range(1, 1 + n): ind[a[i - 1]] = i p = 1 ans = [] while p <= n: pos = ind[p] while pos > p: near = primes[pos - p + 1] ans += [[pos - near + 1, pos]] ind[a[pos - 1]], ind[a[pos - near + 1 - 1]] = ( ind[a[pos - near + 1 - 1]], ind[a[pos - 1]], ) a[pos - 1], a[pos - near + 1 - 1] = a[pos - near + 1 - 1], a[pos - 1] pos = pos - near + 1 p += 1 print(len(ans)) for k, v in ans: print(k, v)
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 BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR LIST LIST BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
You have an array a[1], a[2], ..., a[n], containing distinct integers from 1 to n. Your task is to sort this array in increasing order with the following operation (you may need to apply it multiple times): choose two indexes, i and j (1 ≀ i < j ≀ n; (j - i + 1) is a prime number); swap the elements on positions i and j; in other words, you are allowed to apply the following sequence of assignments: tmp = a[i], a[i] = a[j], a[j] = tmp (tmp is a temporary variable). You do not need to minimize the number of used operations. However, you need to make sure that there are at most 5n operations. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5). The next line contains n distinct integers a[1], a[2], ..., a[n] (1 ≀ a[i] ≀ n). -----Output----- In the first line, print integer k (0 ≀ k ≀ 5n) β€” the number of used operations. Next, print the operations. Each operation must be printed as "i j" (1 ≀ i < j ≀ n; (j - i + 1) is a prime). If there are multiple answers, you can print any of them. -----Examples----- Input 3 3 2 1 Output 1 1 3 Input 2 1 2 Output 0 Input 4 4 2 3 1 Output 3 2 4 1 2 2 4
def sieve(n): prime = [-1] * (n + 1) for i in range(2, n + 1): if prime[i] == -1: for j in range(i, n + 1, i): prime[j] = i return prime p = sieve(100010) prime = [] for i in range(len(p)): if p[i] == i: prime.append(p[i]) def find_prime(num): low = 0 high = len(prime) - 1 while low < high: mid = (low + high) // 2 if prime[mid] < num: low = mid + 1 else: high = mid - 1 if prime[low] <= num: return prime[low] else: return prime[low - 1] def calc(ind): ans = [] num = loc[arr[ind][0]] while num - ind > 0: p = find_prime(num - ind + 1) loc[a[num - p + 1]], loc[a[num]] = loc[a[num]], loc[a[num - p + 1]] a[num], a[num - p + 1] = a[num - p + 1], a[num] ans.append([num - p + 2, num + 1]) num -= p - 1 return ans n = int(input()) a = list(map(int, input().split())) loc = {} for i in range(n): loc[a[i]] = i arr = [] for i in range(n): arr.append([a[i], i]) arr.sort() i = 0 ans = [] while i < n: if arr[i][0] == a[i]: i += 1 continue ans.extend(calc(i)) i += 1 print(len(ans)) for i in ans: print(*i)
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR RETURN VAR VAR RETURN VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN 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 FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
You have an array a[1], a[2], ..., a[n], containing distinct integers from 1 to n. Your task is to sort this array in increasing order with the following operation (you may need to apply it multiple times): choose two indexes, i and j (1 ≀ i < j ≀ n; (j - i + 1) is a prime number); swap the elements on positions i and j; in other words, you are allowed to apply the following sequence of assignments: tmp = a[i], a[i] = a[j], a[j] = tmp (tmp is a temporary variable). You do not need to minimize the number of used operations. However, you need to make sure that there are at most 5n operations. -----Input----- The first line contains integer n (1 ≀ n ≀ 10^5). The next line contains n distinct integers a[1], a[2], ..., a[n] (1 ≀ a[i] ≀ n). -----Output----- In the first line, print integer k (0 ≀ k ≀ 5n) β€” the number of used operations. Next, print the operations. Each operation must be printed as "i j" (1 ≀ i < j ≀ n; (j - i + 1) is a prime). If there are multiple answers, you can print any of them. -----Examples----- Input 3 3 2 1 Output 1 1 3 Input 2 1 2 Output 0 Input 4 4 2 3 1 Output 3 2 4 1 2 2 4
N = int(200000.0 + 3) primes = [] is_prime = [1] * N is_prime[0] = is_prime[1] = 0 for i in range(2, N): if is_prime[i] == 0: continue primes.append(i) for j in range(i + i, N, i): is_prime[j] = 0 n = int(input()) arr = list(map(int, input().split())) prt = [0] * n for i in range(n): arr[i] -= 1 prt[arr[i]] = i def binary_search(k): b, e = 0, len(primes) while b <= e: mid = b + e >> 1 if primes[mid] <= k: b = mid + 1 else: e = mid - 1 return b res = [] p = 0 while p < n: while prt[p] != p: d = prt[p] - p + 1 d = primes[binary_search(d) - 1] - 1 r = prt[p] q = r - d prt[arr[q]], prt[arr[r]] = prt[arr[r]], prt[arr[q]] arr[q], arr[r] = arr[r], arr[q] res.append(str(q + 1) + " " + str(r + 1)) p += 1 print(len(res)) print("\n".join(res))
ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR 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 BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
There are k sensors located in the rectangular room of size n Γ— m meters. The i-th sensor is located at point (x_{i}, y_{i}). All sensors are located at distinct points strictly inside the rectangle. Opposite corners of the room are located at points (0, 0) and (n, m). Walls of the room are parallel to coordinate axes. At the moment 0, from the point (0, 0) the laser ray is released in the direction of point (1, 1). The ray travels with a speed of $\sqrt{2}$ meters per second. Thus, the ray will reach the point (1, 1) in exactly one second after the start. When the ray meets the wall it's reflected by the rule that the angle of incidence is equal to the angle of reflection. If the ray reaches any of the four corners, it immediately stops. For each sensor you have to determine the first moment of time when the ray will pass through the point where this sensor is located. If the ray will never pass through this point, print - 1 for such sensors. -----Input----- The first line of the input contains three integers n, m and k (2 ≀ n, m ≀ 100 000, 1 ≀ k ≀ 100 000)Β β€” lengths of the room's walls and the number of sensors. Each of the following k lines contains two integers x_{i} and y_{i} (1 ≀ x_{i} ≀ n - 1, 1 ≀ y_{i} ≀ m - 1)Β β€” coordinates of the sensors. It's guaranteed that no two sensors are located at the same point. -----Output----- Print k integers. The i-th of them should be equal to the number of seconds when the ray first passes through the point where the i-th sensor is located, or - 1 if this will never happen. -----Examples----- Input 3 3 4 1 1 1 2 2 1 2 2 Output 1 -1 -1 2 Input 3 4 6 1 1 2 1 1 2 2 2 1 3 2 3 Output 1 -1 -1 2 5 -1 Input 7 4 5 1 3 2 2 5 1 5 3 4 3 Output 13 2 9 5 -1 -----Note----- In the first sample, the ray will consequently pass through the points (0, 0), (1, 1), (2, 2), (3, 3). Thus, it will stop at the point (3, 3) after 3 seconds. [Image] In the second sample, the ray will consequently pass through the following points: (0, 0), (1, 1), (2, 2), (3, 3), (2, 4), (1, 3), (0, 2), (1, 1), (2, 0), (3, 1), (2, 2), (1, 3), (0, 4). The ray will stop at the point (0, 4) after 12 seconds. It will reflect at the points (3, 3), (2, 4), (0, 2), (2, 0) and (3, 1). [Image]
from sys import * f = lambda: map(int, stdin.readline().split()) n, m, k = f() w, h = 2 * n, 2 * m inf = 100000000000.0 s = [inf] * (w + h) a = b = t = 0 while 1: if s[b - a] == inf: s[b - a] = t - a d = min(w - a, h - b) t += d a = (a + d) % w b = (b + d) % h if a % n == b % m: break for i in range(k): x, y = f() q = min(s[b - a] + a for a in (x, w - x) for b in (y, h - y)) print(q if q < inf else -1)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE NUMBER IF VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER
There are k sensors located in the rectangular room of size n Γ— m meters. The i-th sensor is located at point (x_{i}, y_{i}). All sensors are located at distinct points strictly inside the rectangle. Opposite corners of the room are located at points (0, 0) and (n, m). Walls of the room are parallel to coordinate axes. At the moment 0, from the point (0, 0) the laser ray is released in the direction of point (1, 1). The ray travels with a speed of $\sqrt{2}$ meters per second. Thus, the ray will reach the point (1, 1) in exactly one second after the start. When the ray meets the wall it's reflected by the rule that the angle of incidence is equal to the angle of reflection. If the ray reaches any of the four corners, it immediately stops. For each sensor you have to determine the first moment of time when the ray will pass through the point where this sensor is located. If the ray will never pass through this point, print - 1 for such sensors. -----Input----- The first line of the input contains three integers n, m and k (2 ≀ n, m ≀ 100 000, 1 ≀ k ≀ 100 000)Β β€” lengths of the room's walls and the number of sensors. Each of the following k lines contains two integers x_{i} and y_{i} (1 ≀ x_{i} ≀ n - 1, 1 ≀ y_{i} ≀ m - 1)Β β€” coordinates of the sensors. It's guaranteed that no two sensors are located at the same point. -----Output----- Print k integers. The i-th of them should be equal to the number of seconds when the ray first passes through the point where the i-th sensor is located, or - 1 if this will never happen. -----Examples----- Input 3 3 4 1 1 1 2 2 1 2 2 Output 1 -1 -1 2 Input 3 4 6 1 1 2 1 1 2 2 2 1 3 2 3 Output 1 -1 -1 2 5 -1 Input 7 4 5 1 3 2 2 5 1 5 3 4 3 Output 13 2 9 5 -1 -----Note----- In the first sample, the ray will consequently pass through the points (0, 0), (1, 1), (2, 2), (3, 3). Thus, it will stop at the point (3, 3) after 3 seconds. [Image] In the second sample, the ray will consequently pass through the following points: (0, 0), (1, 1), (2, 2), (3, 3), (2, 4), (1, 3), (0, 2), (1, 1), (2, 0), (3, 1), (2, 2), (1, 3), (0, 4). The ray will stop at the point (0, 4) after 12 seconds. It will reflect at the points (3, 3), (2, 4), (0, 2), (2, 0) and (3, 1). [Image]
import sys inp = sys.stdin.readline def input(): return inp().strip() def iin(): return int(input()) def lin(): return list(map(int, input().split())) def main(): T = 1 def fn(ch, x, y): return x * ch[0] + y * ch[1] + ch[2] == 0 def fnx(ch, y): return (-ch[1] * y - ch[2]) / ch[0] def fny(ch, x): return (-ch[0] * x - ch[2]) / ch[1] def int_val(x): return int(x * 10000) == int(x) * 10000 while T: T -= 1 n, m, k = lin() h = m w = n end = {(0, h), (0, 0), (w, h), (w, 0)} sensors = [lin() for _ in range(k)] lines = {(1, -1, 0): [(0, 0), 0]} ch = [1, -1, 0] ch1 = 1 ch2 = 0 st = [0, 0] while 1: dn = 0 y1 = h x1 = fnx(ch, y1) if int_val(x1) and 0 <= int(x1) <= w and [int(x1), int(y1)] != st: x1 = int(x1) if x1 == w: break dn = 1 if dn == 0: y1 = 0 x1 = fnx(ch, 0) if int_val(x1) and 0 <= int(x1) <= w and [int(x1), int(y1)] != st: x1 = int(x1) if x1 == 0: break dn = 1 if dn == 0: x1 = 0 y1 = fny(ch, x1) if int_val(y1) and 0 <= int(y1) <= h and [int(x1), int(y1)] != st: y1 = int(y1) if y1 == 0: break dn = 1 if dn == 0: x1 = w y1 = fny(ch, x1) if ( int_val(y1) and 0 <= int(y1) <= h and [int(x1), int(y1)] != st ): y1 = int(y1) if y1 == h: break dn = 1 if dn: ch2 += abs(st[0] - x1) ch1 = -1 if ch1 == 1 else 1 ch = [ch1, -1, -ch1 * x1 + y1] if tuple(ch) in lines: continue lines[tuple(ch)] = [[x1, y1], ch2] if (x1, y1) in end: break st = [x1, y1] else: break for i, j in sensors: ch1, ch2 = (1, -1, -i + j), (-1, -1, i + j) ans = -1 if ch1 in lines: p, c1 = lines[ch1] ans = abs(p[0] - i) + c1 if ch2 in lines: p, c1 = lines[ch2] ans = abs(p[0] - i) + c1 if ans == -1 else min(abs(p[0] - i) + c1, ans) print(ans) main()
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST VAR NUMBER BIN_OP BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR LIST LIST VAR VAR VAR IF VAR VAR VAR ASSIGN VAR LIST VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
There are k sensors located in the rectangular room of size n Γ— m meters. The i-th sensor is located at point (x_{i}, y_{i}). All sensors are located at distinct points strictly inside the rectangle. Opposite corners of the room are located at points (0, 0) and (n, m). Walls of the room are parallel to coordinate axes. At the moment 0, from the point (0, 0) the laser ray is released in the direction of point (1, 1). The ray travels with a speed of $\sqrt{2}$ meters per second. Thus, the ray will reach the point (1, 1) in exactly one second after the start. When the ray meets the wall it's reflected by the rule that the angle of incidence is equal to the angle of reflection. If the ray reaches any of the four corners, it immediately stops. For each sensor you have to determine the first moment of time when the ray will pass through the point where this sensor is located. If the ray will never pass through this point, print - 1 for such sensors. -----Input----- The first line of the input contains three integers n, m and k (2 ≀ n, m ≀ 100 000, 1 ≀ k ≀ 100 000)Β β€” lengths of the room's walls and the number of sensors. Each of the following k lines contains two integers x_{i} and y_{i} (1 ≀ x_{i} ≀ n - 1, 1 ≀ y_{i} ≀ m - 1)Β β€” coordinates of the sensors. It's guaranteed that no two sensors are located at the same point. -----Output----- Print k integers. The i-th of them should be equal to the number of seconds when the ray first passes through the point where the i-th sensor is located, or - 1 if this will never happen. -----Examples----- Input 3 3 4 1 1 1 2 2 1 2 2 Output 1 -1 -1 2 Input 3 4 6 1 1 2 1 1 2 2 2 1 3 2 3 Output 1 -1 -1 2 5 -1 Input 7 4 5 1 3 2 2 5 1 5 3 4 3 Output 13 2 9 5 -1 -----Note----- In the first sample, the ray will consequently pass through the points (0, 0), (1, 1), (2, 2), (3, 3). Thus, it will stop at the point (3, 3) after 3 seconds. [Image] In the second sample, the ray will consequently pass through the following points: (0, 0), (1, 1), (2, 2), (3, 3), (2, 4), (1, 3), (0, 2), (1, 1), (2, 0), (3, 1), (2, 2), (1, 3), (0, 4). The ray will stop at the point (0, 4) after 12 seconds. It will reflect at the points (3, 3), (2, 4), (0, 2), (2, 0) and (3, 1). [Image]
f = lambda: map(int, input().split()) n, m, k = f() w, h = 2 * n, 2 * m s = [[] for i in range(w + h)] p = [-1] * k for i in range(k): x, y = f() if x - y & 1: continue for a in (x, w - x): for b in (y, h - y): s[b - a].append((a, i)) a = b = t = 0 while 1: for x, i in s[b - a]: if p[i] < 0: p[i] = t + x - a d = min(w - a, h - b) t += d a = (a + d) % w b = (b + d) % h if a % n == b % m: break for q in p: print(q)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR IF BIN_OP BIN_OP VAR VAR NUMBER FOR VAR VAR BIN_OP VAR VAR FOR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE NUMBER FOR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
There are k sensors located in the rectangular room of size n Γ— m meters. The i-th sensor is located at point (x_{i}, y_{i}). All sensors are located at distinct points strictly inside the rectangle. Opposite corners of the room are located at points (0, 0) and (n, m). Walls of the room are parallel to coordinate axes. At the moment 0, from the point (0, 0) the laser ray is released in the direction of point (1, 1). The ray travels with a speed of $\sqrt{2}$ meters per second. Thus, the ray will reach the point (1, 1) in exactly one second after the start. When the ray meets the wall it's reflected by the rule that the angle of incidence is equal to the angle of reflection. If the ray reaches any of the four corners, it immediately stops. For each sensor you have to determine the first moment of time when the ray will pass through the point where this sensor is located. If the ray will never pass through this point, print - 1 for such sensors. -----Input----- The first line of the input contains three integers n, m and k (2 ≀ n, m ≀ 100 000, 1 ≀ k ≀ 100 000)Β β€” lengths of the room's walls and the number of sensors. Each of the following k lines contains two integers x_{i} and y_{i} (1 ≀ x_{i} ≀ n - 1, 1 ≀ y_{i} ≀ m - 1)Β β€” coordinates of the sensors. It's guaranteed that no two sensors are located at the same point. -----Output----- Print k integers. The i-th of them should be equal to the number of seconds when the ray first passes through the point where the i-th sensor is located, or - 1 if this will never happen. -----Examples----- Input 3 3 4 1 1 1 2 2 1 2 2 Output 1 -1 -1 2 Input 3 4 6 1 1 2 1 1 2 2 2 1 3 2 3 Output 1 -1 -1 2 5 -1 Input 7 4 5 1 3 2 2 5 1 5 3 4 3 Output 13 2 9 5 -1 -----Note----- In the first sample, the ray will consequently pass through the points (0, 0), (1, 1), (2, 2), (3, 3). Thus, it will stop at the point (3, 3) after 3 seconds. [Image] In the second sample, the ray will consequently pass through the following points: (0, 0), (1, 1), (2, 2), (3, 3), (2, 4), (1, 3), (0, 2), (1, 1), (2, 0), (3, 1), (2, 2), (1, 3), (0, 4). The ray will stop at the point (0, 4) after 12 seconds. It will reflect at the points (3, 3), (2, 4), (0, 2), (2, 0) and (3, 1). [Image]
def main(): nx, my, k = list(map(int, input().strip().split())) my *= 2 nx *= 2 diags = [[] for i in range(nx + my)] answers = [-1] * k for i in range(k): x, y = list(map(int, input().strip().split())) def add(x, y, i): diag_index = nx + (y - x) diags[diag_index].append((x, y, i)) add(x, y, i) add(x, my - y, i) add(nx - x, y, i) add(nx - x, my - y, i) cur_t = 0 cur_x = 0 cur_y = 0 while True: diag_index = nx + (cur_y - cur_x) for x, y, i in diags[diag_index]: if answers[i] == -1: t = cur_t + (x - cur_x) assert x - cur_x == y - cur_y answers[i] = t diff_x = nx - cur_x diff_y = my - cur_y diff = min(diff_x, diff_y) cur_t += diff cur_x = (cur_x + diff) % nx cur_y = (cur_y + diff) % my if cur_x % (nx // 2) + cur_y % (my // 2) == 0: break for a in answers: print(a) main()
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR FOR VAR VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
There are k sensors located in the rectangular room of size n Γ— m meters. The i-th sensor is located at point (x_{i}, y_{i}). All sensors are located at distinct points strictly inside the rectangle. Opposite corners of the room are located at points (0, 0) and (n, m). Walls of the room are parallel to coordinate axes. At the moment 0, from the point (0, 0) the laser ray is released in the direction of point (1, 1). The ray travels with a speed of $\sqrt{2}$ meters per second. Thus, the ray will reach the point (1, 1) in exactly one second after the start. When the ray meets the wall it's reflected by the rule that the angle of incidence is equal to the angle of reflection. If the ray reaches any of the four corners, it immediately stops. For each sensor you have to determine the first moment of time when the ray will pass through the point where this sensor is located. If the ray will never pass through this point, print - 1 for such sensors. -----Input----- The first line of the input contains three integers n, m and k (2 ≀ n, m ≀ 100 000, 1 ≀ k ≀ 100 000)Β β€” lengths of the room's walls and the number of sensors. Each of the following k lines contains two integers x_{i} and y_{i} (1 ≀ x_{i} ≀ n - 1, 1 ≀ y_{i} ≀ m - 1)Β β€” coordinates of the sensors. It's guaranteed that no two sensors are located at the same point. -----Output----- Print k integers. The i-th of them should be equal to the number of seconds when the ray first passes through the point where the i-th sensor is located, or - 1 if this will never happen. -----Examples----- Input 3 3 4 1 1 1 2 2 1 2 2 Output 1 -1 -1 2 Input 3 4 6 1 1 2 1 1 2 2 2 1 3 2 3 Output 1 -1 -1 2 5 -1 Input 7 4 5 1 3 2 2 5 1 5 3 4 3 Output 13 2 9 5 -1 -----Note----- In the first sample, the ray will consequently pass through the points (0, 0), (1, 1), (2, 2), (3, 3). Thus, it will stop at the point (3, 3) after 3 seconds. [Image] In the second sample, the ray will consequently pass through the following points: (0, 0), (1, 1), (2, 2), (3, 3), (2, 4), (1, 3), (0, 2), (1, 1), (2, 0), (3, 1), (2, 2), (1, 3), (0, 4). The ray will stop at the point (0, 4) after 12 seconds. It will reflect at the points (3, 3), (2, 4), (0, 2), (2, 0) and (3, 1). [Image]
n, m, k = list(map(int, input().split())) dm, dp = {}, {} vis = {} sensors = [] border = set() for el in [(0, m), (n, 0), (0, 0), (n, m)]: border.add(el) for _ in range(k): x, y = list(map(int, input().split())) if not x - y in dm: dm[x - y] = [] dm[x - y].append((x, y)) if not x + y in dp: dp[x + y] = [] dp[x + y].append((x, y)) vis[x, y] = -1 sensors.append((x, y)) x, y = 0, 0 time = 0 move = 1, 1 while True: if move == (1, 1): v = min(n - x, m - y) nxt = x + v, y + v if nxt[0] == n: move = -1, 1 else: move = 1, -1 if x - y in dm: for sensor in dm[x - y]: if vis[sensor] == -1: vis[sensor] = time + sensor[0] - x time += v elif move == (-1, -1): v = min(x, y) nxt = x - v, y - v if nxt[0] == 0: move = 1, -1 else: move = -1, 1 if x - y in dm: for sensor in dm[x - y]: if vis[sensor] == -1: vis[sensor] = time + x - sensor[0] time += v elif move == (-1, 1): v = min(x, m - y) nxt = x - v, y + v if nxt[0] == 0: move = 1, 1 else: move = -1, -1 if x + y in dp: for sensor in dp[x + y]: if vis[sensor] == -1: vis[sensor] = time + x - sensor[0] time += v else: v = min(n - x, y) nxt = x + v, y - v if nxt[0] == n: move = -1, -1 else: move = 1, 1 if x + y in dp: for sensor in dp[x + y]: if vis[sensor] == -1: vis[sensor] = time + sensor[0] - x time += v if nxt in border: break else: border.add(nxt) x, y = nxt for i in range(k): print(vis[sensors[i]])
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR DICT DICT ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR LIST NUMBER VAR VAR NUMBER NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR LIST EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR LIST EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER WHILE NUMBER IF VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR FOR VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR FOR VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR FOR VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR FOR VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR
There are k sensors located in the rectangular room of size n Γ— m meters. The i-th sensor is located at point (x_{i}, y_{i}). All sensors are located at distinct points strictly inside the rectangle. Opposite corners of the room are located at points (0, 0) and (n, m). Walls of the room are parallel to coordinate axes. At the moment 0, from the point (0, 0) the laser ray is released in the direction of point (1, 1). The ray travels with a speed of $\sqrt{2}$ meters per second. Thus, the ray will reach the point (1, 1) in exactly one second after the start. When the ray meets the wall it's reflected by the rule that the angle of incidence is equal to the angle of reflection. If the ray reaches any of the four corners, it immediately stops. For each sensor you have to determine the first moment of time when the ray will pass through the point where this sensor is located. If the ray will never pass through this point, print - 1 for such sensors. -----Input----- The first line of the input contains three integers n, m and k (2 ≀ n, m ≀ 100 000, 1 ≀ k ≀ 100 000)Β β€” lengths of the room's walls and the number of sensors. Each of the following k lines contains two integers x_{i} and y_{i} (1 ≀ x_{i} ≀ n - 1, 1 ≀ y_{i} ≀ m - 1)Β β€” coordinates of the sensors. It's guaranteed that no two sensors are located at the same point. -----Output----- Print k integers. The i-th of them should be equal to the number of seconds when the ray first passes through the point where the i-th sensor is located, or - 1 if this will never happen. -----Examples----- Input 3 3 4 1 1 1 2 2 1 2 2 Output 1 -1 -1 2 Input 3 4 6 1 1 2 1 1 2 2 2 1 3 2 3 Output 1 -1 -1 2 5 -1 Input 7 4 5 1 3 2 2 5 1 5 3 4 3 Output 13 2 9 5 -1 -----Note----- In the first sample, the ray will consequently pass through the points (0, 0), (1, 1), (2, 2), (3, 3). Thus, it will stop at the point (3, 3) after 3 seconds. [Image] In the second sample, the ray will consequently pass through the following points: (0, 0), (1, 1), (2, 2), (3, 3), (2, 4), (1, 3), (0, 2), (1, 1), (2, 0), (3, 1), (2, 2), (1, 3), (0, 4). The ray will stop at the point (0, 4) after 12 seconds. It will reflect at the points (3, 3), (2, 4), (0, 2), (2, 0) and (3, 1). [Image]
n, m, k = map(int, input().split()) n = 2 * n m = 2 * m a = min(m, n) b = max(m, n) tracker = [b] while a > 0: b = b % a tracker.append(a) a, b = b, a g = b prod = m * n // g if m != n: if len(tracker) >= 3: pair = 1, -(tracker[-3] // tracker[-2]) for i in range(len(tracker) - 4, -1, -1): new = -(tracker[i] // tracker[i + 1]) pair = pair[1], pair[0] + pair[1] * new if min(m, n) == n: pair = pair[1], pair[0] a, b = pair elif m > n: a = 1 b = 0 else: a = 0 b = 1 for i in range(k): x, y = [int(i) for i in input().split()] if (x - y) % g != 0 and (x + y) % g != 0: print(-1) continue else: shortlist = [] for nx in (x, -x): if (nx - y) % g == 0: new = (nx + a * n * ((y - nx) // g)) % prod shortlist.append(new) shortlist.append(prod - new) if len(shortlist) > 0: print(min(shortlist)) else: print(-1) else: for i in range(k): x, y = [int(i) for i in input().split()] if x != y: print(-1) else: print(x)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
q = int(input()) for i in range(q): n = int(input()) a = [int(i) for i in input().split(" ")] counter = [0] * (len(a) + 1) for i in range(len(a)): counter[a[i]] += 1 counter.sort(reverse=True) curr = 10**9 ans = 0 for i in counter: curr = max(min(curr - 1, i), 0) ans += curr print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
import sys import time start = time.time() cases = int(input()) for i in range(0, cases): l = int(input()) ln = [int(j) for j in sys.stdin.readline().rstrip().split(" ")] if len(ln) == 1: print(1) else: ct = [] ln = sorted(ln) t = 1 for j in range(1, len(ln)): if ln[j] != ln[j - 1]: ct.append(t) t = 1 else: t += 1 ct.append(t) ct = sorted(ct) n = ct[-1] s = n j = len(ct) - 2 while j >= 0: n = min(n - 1, ct[j]) if n <= 0: break s += n j -= 1 print(s)
IMPORT IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
def make(list): dic = {} n = len(list) for i in range(n): if list[i] in dic: dic[list[i]] += 1 else: dic[list[i]] = 1 l = [] for i in dic: l.append(dic[i]) l.sort() s = 0 prev = n + 1 for i in range(len(l) - 1, -1, -1): if l[i] < prev: s += l[i] prev = l[i] elif prev - 1 >= 0: s += prev - 1 prev = prev - 1 return s n = int(input()) for i in range(n): m = int(input()) l = list(map(int, input().strip().split())) print(make(l))
FUNC_DEF ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR 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 FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
def get_number(A): B = {} for i in A: B[i] = B.get(i, 0) + 1 return sorted(list(B.values()), reverse=True) def Candy_Box(A): B = get_number(A) D = [B[0]] for i in range(1, len(B)): if D[i - 1] > 0: D.append(min(D[i - 1] - 1, B[i])) else: break return sum(D) q = int(input()) C = [] for i in range(q): n = int(input()) A = [] for x in input().split(): A.append(int(x)) C.append(Candy_Box(A)) for i in C: print(i)
FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
SS = [] a = int(input()) for i in range(a): x = int(input()) A = list(map(int, input().split())) D = {} for j in range(x): if A[j] in D: D[A[j]] += 1 else: D[A[j]] = 1 B = [] for key in D: B.append(D[key]) B.sort(reverse=True) w = B[0] for j in range(1, len(B)): if B[j] < B[j - 1] - 1: w += max(0, B[j]) else: B[j] = B[j - 1] - 1 w += max(0, B[j - 1] - 1) SS.append(w) for s in SS: print(s)
ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR 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 DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
import sys input = sys.stdin.readline for q in range(int(input())): n = int(input()) num = list(map(int, input().split())) cal = [] p = (n + 1) * [0] for i in range(n): p[num[i]] += 1 for j in range(n + 1): cal.append(p[j]) cal.sort() l = len(cal) x = cal[l - 1] ans = 0 for i in range(l): ans += x if i == l - 1: break x = max(0, min(x - 1, cal[l - i - 2])) print(ans)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 ASSIGN VAR BIN_OP BIN_OP VAR NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
def candies(lst): n = len(lst) count = [0] * (n + 1) for elem in lst: count[elem] += 1 count = sorted(count, reverse=True) result, temp = count[0], count[0] for i in range(1, n + 1): if temp == 0: break if count[i] >= temp: result += temp - 1 temp -= 1 else: result += count[i] temp = count[i] return result m = int(input()) a = list() for j in range(m): k = int(input()) b = [int(x) for x in input().split()] a.append(candies(b)) for elem in a: print(elem)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR 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 FOR VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
t = int(input()) sol = [] while t: t -= 1 n = int(input()) a = [int(i) for i in input().split()] d = {} for i in a: if not d.get(i): d[i] = 1 else: d[i] += 1 a = [] for i in d.keys(): a.append(d[i]) a.sort(reverse=True) d = set() for i in a: if not i in d: d.add(i) else: j = i while j and j in d: j -= 1 d.add(j) a = d sol.append(sum(a)) print(*sol, sep="\n")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST WHILE VAR VAR NUMBER 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 IF FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
q = int(input()) ans = [] for w in range(q): n = int(input()) a = [int(x) for x in input().split()] mas = [0] * n for i in a: mas[i - 1] += 1 kol = [] for i in mas: if i > 0: kol.append(i) kol.sort() kol = kol[::-1] pred = 10**10 res = 0 for i in kol: res += max(0, min(pred - 1, i)) pred = min(pred - 1, i) ans.append(res) for i in ans: print(i)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR 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 VAR FOR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
q = int(input()) for i in range(q): n = int(input()) a = [int(x) for x in input().split()] dic = {} b = [] counter = 0 for item in a: if item not in dic: dic[item] = 1 else: dic[item] += 1 for item in dic: b.append(dic[item]) b.sort(reverse=True) x = b[0] + 1 for item in b: x = min(x - 1, item) counter += max(x, 0) print(counter)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
import sys class GCandyBoxHardVersion: def solve(self): q = int(input()) for _ in range(q): n = int(input()) a = [0] * n f = [0] * n for i in range(n): a[i], f[i] = [int(_) for _ in input().split()] d = {key: [0, 0] for key in a} for i in range(n): d[a[i]][f[i]] += 1 rev_d = {sum(key): [] for key in d.values()} for x in d: rev_d[d[x][0] + d[x][1]] += [d[x]] for x in rev_d: rev_d[x].sort(key=lambda item: item[1]) cur = max(rev_d) cnt = max(rev_d) nb_candies = 0 given_away = 0 while 1: if cnt == 0 or cur == 0: break if cur > cnt: cur -= 1 continue if cnt not in rev_d or not rev_d[cnt]: cnt -= 1 continue mx_f = -1 v = -1 for max_cnt in range(cur, cnt + 1): if ( max_cnt in rev_d and rev_d[max_cnt] and rev_d[max_cnt][-1][1] > mx_f ): v = max_cnt mx_f = rev_d[max_cnt][-1][1] to_take = rev_d[v].pop() nb_candies += cur given_away += min(to_take[1], cur) cur -= 1 print(nb_candies, given_away) solver = GCandyBoxHardVersion() input = sys.stdin.readline solver.solve()
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST NUMBER NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR LIST VAR FUNC_CALL VAR FOR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER LIST VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
for i in range(int(input())): n = int(input()) sort_col = [0] * (n + 1) col_csort = [0] * (n + 1) t = 0 for el in map(int, input().split()): col_csort[sort_col[el]] -= 1 sort_col[el] += 1 col_csort[sort_col[el]] += 1 t = max(sort_col[el], t) cou = 0 for j in range(t, 0, -1): if col_csort[j]: cou += j col_csort[j] -= 1 if col_csort[j]: col_csort[j - 1] += col_csort[j] print(cou)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR 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 FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
from sys import * t = int(stdin.readline()) for _ in range(t): n = int(stdin.readline()) a = list(map(int, stdin.readline().split())) b = [0] * max(a) for i in a: b[i - 1] += 1 b.sort() b = b[::-1] m = b[0] cnt = b[0] for i in range(1, len(b)): if b[i] >= m: b[i] = b[i - 1] - 1 b[i] = max(0, b[i]) m = b[i] cnt += b[i] print(cnt)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR 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 BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
for i in " " * int(input()): d = {} input() (*a,) = map(int, input().split()) for i in a: d[i] = d.get(i, 0) + 1 s = 0 for i in sorted(list(d.values()), reverse=True): if s == 0: b = i s += min(i, b) b = min(i, b) - 1 if b < 0: b = 0 print(s)
FOR VAR BIN_OP STRING FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
from sys import stdin, stdout q = int(input()) while q != 0: q -= 1 n = int(stdin.readline()) lt = [int(x) for x in stdin.readline().split()] cnt = [0] * (n + 1) for c in lt: cnt[c] += 1 cnt.sort(reverse=True) lst, ans = cnt[0], cnt[0] for i in range(1, n + 1): if lst == 0: break if cnt[i] < lst: ans += cnt[i] lst = cnt[i] else: lst -= 1 ans += lst print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER 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 BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
t = int(input()) for _ in range(t): n = int(input()) l = [int(x) for x in input().split()] d = {} for i in l: if d.get(i) == None: d[i] = 1 else: d[i] += 1 dict = {} for key in d: if dict.get(d[key]) == None: dict[d[key]] = 1 else: dict[d[key]] += 1 d = {} ct = 0 for key in sorted(dict): i = dict[key] j = key while j > 0 and i > 0: if d.get(j) == None: d[j] = 1 ct += j j -= 1 i -= 1 else: j -= 1 print(ct)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR 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 IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF FUNC_CALL VAR VAR VAR NONE ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
t = int(input()) for i in range(t): n = int(input()) a = list(map(int, input().split())) m = {} for x in a: if x in m: m[x] += 1 else: m[x] = 1 lst = list(m.values()) lst.sort(reverse=True) m = {} for x in lst: if x in m: m[x] += 1 else: m[x] = 1 val = 0 res = set() for x in m: left = x right = m[x] tmp = left while right > 0: while tmp > 0: if tmp not in res: val += tmp res.add(tmp) break tmp -= 1 right -= 1 print(val)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR 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 DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
from sys import stdin for _ in range(int(stdin.readline())): n = int(stdin.readline()) s = [0] * (n + 1) a = list(map(int, stdin.readline().split())) x = 0 for i in range(n): s[a[i]] += 1 if s[a[i]] > x: x = s[a[i]] z = [0] * (x + 1) p = 0 for i in range(1, n + 1): x = s[i] if not z[x]: p += x z[x] = 1 else: while x: if not z[x]: p += x z[x] = 1 break x -= 1 print(p)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR 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 BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER WHILE VAR IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
t = int(input()) for v in range(t): n = int(input()) arr = list(map(int, input().split())) if n == 1: print(arr[0]) continue l = [0] * n for i in range(n): l[arr[i] - 1] += 1 l.sort() s = 0 i = n - 1 pr = l[n - 1] while i >= 0 and pr > 0: pr = min(pr, l[i]) s += pr pr -= 1 i -= 1 print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR 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 IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
from sys import stdin, stdout for _ in range(int(stdin.readline())): n = int(stdin.readline()) v = stdin.readline().split(" ") freq = [0] * (n + 2) for x in v: freq[int(x)] += 1 freq.sort(reverse=True) lst = int(1000000000.0) s = 0 for i in freq: if i < lst: lst = i if lst > 0: s += lst lst -= 1 print(s)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
from sys import stdin, stdout I = stdin.readline print = stdout.write for _ in range(int(I())): n = int(I()) a = list(map(int, I().split())) d = dict() for i in a: if i in d: d[i] += 1 else: d[i] = 1 l = list(d.values()) l.sort(reverse=True) ans = [l[0]] al = 1 t = l[0] for i in range(1, len(l)): if t - l[i] > 0: ans.append(l[i]) t = l[i] else: temp = l[i] - t temp += 1 if l[i] - temp <= 0: break else: t = l[i] - temp ans.append(t) print(str(sum(ans)) + "\n")
ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR STRING
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
from sys import stdin, stdout def INI(): return int(stdin.readline()) def INL(): return [int(_) for _ in stdin.readline().split()] def INS(): return stdin.readline() def MOD(): return pow(10, 9) + 7 def OPS(ans): stdout.write(str(ans) + "\n") def OPL(ans): [stdout.write(str(_) + " ") for _ in ans] stdout.write("\n") for _ in range(INI()): n = INI() X = sorted(INL()) x = X[0] c = 0 s = set() ans = 0 add = 0 for _ in range(n): if x == X[_]: c += 1 else: ans += add s.add(add) x = X[_] add = 0 c = 1 if c not in s: add = c if add not in s: ans += add OPS(ans)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
for _ in range(int(input())): n = int(input()) b = [0] * n for i in map(int, input().split()): b[i - 1] += 1 b.sort(reverse=True) init = b[0] cnt = init for i in range(1, len(b)): if b[i] >= init: init -= 1 if init <= 0: break cnt += init else: init = b[i] cnt += init if init <= 0: break print(cnt)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
t = int(input()) for z in range(t): n = int(input()) a = [] max = 0 count = 0 b = [] a = [int(x) for x in input().split()] fq = [0] * (n + 1) for i in range(n): fq[a[i]] += 1 fq.sort(reverse=True) f = 0 for i in range(n + 1): if fq[i] == 0: break if fq[i] not in b: b.append(fq[i]) count += fq[i] else: while fq[i] in b: fq[i] -= 1 if fq[i] == 0: f = 1 break if f == 1: break b.append(fq[i]) count += fq[i] print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) d = {} for i in a: if i in d: d[i] += 1 else: d[i] = 1 freq = list(d.values()) freq.sort() freq = freq[::-1] s = freq[0] for i in range(1, len(freq)): if freq[i] >= freq[i - 1]: freq[i] = freq[i - 1] - 1 if freq[i] <= 0: pass else: s += freq[i] else: s += freq[i] print(s)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
N = int(input()) answer = [] for i in range(N): n = int(input()) a = list(map(int, input().split())) dict_a = {} for x in a: if x in dict_a: dict_a[x] += 1 else: dict_a[x] = 1 s = set() for value in dict_a.values(): while value > 0: if value in s: value -= 1 else: s.add(value) break answer.append(sum(s)) for x in answer: print(x)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR 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 DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR WHILE VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
def main(): for _ in range(int(input())): n = int(input()) a = [int(i) for i in input().split()] d = {} for i in range(n): if a[i] not in d: d[a[i]] = 0 d[a[i]] += 1 arr = sorted(d.items(), key=lambda kv: kv[1], reverse=True) last = arr[0][1] r = arr[0][1] for i in range(1, len(arr)): if arr[i - 1][1] == arr[i][1]: last = max(last - 1, 0) r += last elif arr[i][1] < last: last = arr[i][1] r += last else: last = max(last - 1, 0) r += last if last == 0: break print(r) main()
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR 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 FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
for _ in range(int(input())): n = int(input()) fre = [0] * n for i in map(int, input().split()): fre[i - 1] += 1 ans = 0 val = 10**6 fre.sort(reverse=True) for i in fre: val = max(0, min(val - 1, i)) ans += val print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
q = int(input()) for _ in range(q): n = int(input()) d = {} for x in input().split(): d[int(x)] = d.get(int(x), 0) + 1 mini = 200006 ans = 0 for x in sorted(d.values(), reverse=True): if mini == 0: break t = min(mini, x) ans += t mini = t - 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
q = int(input()) for _ in range(q): n = int(input()) gifts = map(int, input().split()) candyDict = {} for i in gifts: try: candyDict[i] += 1 except: candyDict[i] = 1 xd = list(candyDict.values()) xd.sort() toCheck = max(xd) total = 0 for i in range(len(xd) - 1, -1, -1): if toCheck > 0: tmp = min(xd[i], toCheck) total += tmp toCheck = tmp - 1 else: break print(total)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
import sys inf = float("inf") abc = "abcdefghijklmnopqrstuvwxyz" abd = { "a": 0, "b": 1, "c": 2, "d": 3, "e": 4, "f": 5, "g": 6, "h": 7, "i": 8, "j": 9, "k": 10, "l": 11, "m": 12, "n": 13, "o": 14, "p": 15, "q": 16, "r": 17, "s": 18, "t": 19, "u": 20, "v": 21, "w": 22, "x": 23, "y": 24, "z": 25, } mod, MOD = 1000000007, 998244353 vow = ["a", "e", "i", "o", "u"] dx, dy = [-1, 1, 0, 0], [0, 0, 1, -1] def get_array(): return list(map(int, sys.stdin.readline().strip().split())) def get_ints(): return map(int, sys.stdin.readline().strip().split()) def input(): return sys.stdin.readline().strip() q = int(input()) while q > 0: n = int(input()) Arr = get_array() mydict = dict() for i in Arr: mydict[i] = mydict.get(i, 0) + 1 myset = set() count = 0 for i in mydict: if mydict[i] not in myset: count += mydict[i] myset.add(mydict[i]) else: z = mydict[i] for i in range(z, 0, -1): if i not in myset: count += i myset.add(i) break print(count) q -= 1
IMPORT ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR STRING ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST STRING STRING STRING STRING STRING ASSIGN VAR VAR LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
import sys for _ in range(int(input())): n = int(sys.stdin.readline()) a = list(map(int, sys.stdin.readline().split())) d = {} for i in range(n): if a[i] in d: d[a[i]] += 1 else: d[a[i]] = 1 b = list(dict.values(d)) c = [0] * n ans = 0 if b[0] == n: print(n) else: for i in range(len(b) - 1, -1, -1): if c[b[i]] == 0: ans += b[i] c[b[i]] = 1 else: while c[b[i]] == 1: b[i] -= 1 c[b[i]] = 1 c[0] = 0 ans += b[i] print(ans)
IMPORT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
q = int(input()) for test in range(q): n = int(input()) a = list(map(int, input().split(" "))) if n == 1: print(1) continue count = [0] * (n + 1) for i in range(n): count[a[i]] += 1 count = sorted(count, reverse=True) check = count[0] res = count[0] j = 1 while count[j] != 0 and check != 0: if count[j] < check: res += count[j] check = count[j] elif count[j] == check: res += count[j] - 1 check = count[j] - 1 elif count[j] > check: res += check - 1 check -= 1 j += 1 print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR 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 STRING IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
from sys import stdin, stdout q = int(input()) for i in range(q): n = int(stdin.readline()[:-1]) a = [int(i) for i in stdin.readline()[:-1].split()] d = [0] * (max(a) + 1) for el in a: d[el] += 1 d.sort(reverse=True) s = d[0] j = 1 curr = d[0] while d[j] != 0: if d[j] >= curr: curr -= 1 if curr == 0: break s += curr else: s += d[j] curr = d[j] j += 1 stdout.write(f"{s}\n")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
from sys import stdin, stdout input = stdin.readline print = stdout.write for Q in range(int(input())): n = int(input()) A = list(map(int, input().split())) B = [0] * n cunt = 0 for i in A: B[i - 1] += 1 B.sort(reverse=True) for i in range(1, len(B)): if B[i] - B[i - 1] >= 0: B[i] = B[i - 1] - 1 for i in B: if i > 0: cunt = cunt + i else: break print(str(cunt) + "\n")
ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
from sys import stdin, stdout def input(): return stdin.readline() def print(x, end="\n"): return stdout.write(str(x) + str(end)) q = int(input()) for t in range(q): n = int(input()) a = [int(x) for x in input().split()] aList = [0] * (n + 1) for i in a: aList[i] += 1 aList.sort(reverse=True) sum = 0 lowest = aList[0] + 1 for i in aList: lowest = max(min(lowest - 1, i), 0) sum += lowest print(sum)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF STRING RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR 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 BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
q = int(input()) for _ in range(q): n = int(input()) a = [int(x) for x in input().split()] d = {} for x in a: if x not in d: d[x] = 1 else: d[x] += 1 thing = sorted(d.values(), reverse=True) sum = thing[0] for i in range(1, len(thing)): if thing[i] > thing[i - 1] - 1: thing[i] = thing[i - 1] - 1 if thing[i] < 0: thing[i] = 0 sum += thing[i] print(sum)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR 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 IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
t = int(input()) while t: n = int(input()) a = list(map(int, input().split())) b = (n + 1) * [0] for i in a: b[i] += 1 b.sort(reverse=True) ans = b[0] x = b[0] - 1 for i in range(1, n + 1): if b[i] == 0 or x == 0: break if b[i] >= x: ans += x x -= 1 elif b[i] < b[i - 1]: ans += b[i] x = b[i] - 1 print(ans) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE 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 BIN_OP BIN_OP VAR NUMBER LIST NUMBER FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
for i in range(int(input())): n = int(input()) ar = input().split() arr = dict() for k in ar: if arr.get(k) == None: arr[k] = 0 arr[k] += 1 s = [] for l in arr: s.append(arr[l]) s = sorted(s) q = s[len(s) - 1] sumy = q for k in range(len(s) - 2, -1, -1): if q > s[k]: sumy += max(s[k], 0) q = s[k] else: sumy += max(q - 1, 0) q -= 1 if q <= 0: break print(sumy)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
q = int(input()) for j in range(q): n = int(input()) a = list(map(int, input().split())) counter = [0] * (n + 1) for i in range(n): counter[a[i]] += 1 counter = sorted(counter) ans = counter[n] count = counter[n] for i in range(n - 1, -1, -1): if count <= 0: break if counter[i] >= count: count -= 1 if counter[i] < count: count = counter[i] ans += count print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR 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 BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
from sys import stdin, stdout for _ in range(int(stdin.readline())): n = int(stdin.readline()) l = list(map(int, stdin.readline().split())) cnt = [0] * n for i in l: cnt[i - 1] += 1 cnt.sort(reverse=True) curr = int(1000000000.0) ans = 0 for i in cnt: curr = max(min(curr - 1, i), 0) ans += curr stdout.write(str(ans) + "\n")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 BIN_OP LIST NUMBER VAR FOR VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
t = int(input()) while t: t -= 1 n = int(input()) ar = list(map(int, input().split())) d = {} for i in ar: if i in d: d[i] += 1 else: d[i] = 1 br = set() for i in d: if d[i] in br: counter = 1 while d[i] - counter > 0: if d[i] - counter not in br: br.add(d[i] - counter) break counter += 1 elif d[i] not in br: br.add(d[i]) print(sum(br))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR 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 DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
for t in range(int(input())): n = int(input()) a = list(map(int, input().split())) di = {} for i in a: if i not in di: di[i] = 1 else: di[i] = di[i] + 1 count = list(di.values()) count.sort(reverse=True) ans = 0 lc = len(count) prev = count[0] + 1 for i in range(lc): if prev <= count[i]: prev = prev - 1 ans = ans + prev else: ans = ans + count[i] prev = count[i] if prev == 0: break print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
import sys def main(): n = int(sys.stdin.readline()) for i in range(n): idict = {} isum = 0 k = int(sys.stdin.readline()) lst = list(map(int, sys.stdin.readline().split())) for i in lst: if idict.get(i) == None: idict[i] = 1 else: idict[i] += 1 idict = list(idict.values()) idict.sort(key=lambda x: -x) for i in range(len(idict)): if i != 0 and idict[i - 1] <= idict[i]: idict[i] = idict[i - 1] - 1 if idict[i] <= 0: break isum += idict[i] print(isum) main()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
def merge(arr, l, m, r): n1 = m - l + 1 n2 = r - m L = [0] * n1 R = [0] * n2 for i in range(0, n1): L[i] = arr[l + i] for j in range(0, n2): R[j] = arr[m + 1 + j] i = 0 j = 0 k = l while i < n1 and j < n2: if L[i] <= R[j]: arr[k] = L[i] i += 1 else: arr[k] = R[j] j += 1 k += 1 while i < n1: arr[k] = L[i] i += 1 k += 1 while j < n2: arr[k] = R[j] j += 1 k += 1 def mergeSort(arr, l, r): if l < r: m = (l + (r - 1)) // 2 mergeSort(arr, l, m) mergeSort(arr, m + 1, r) merge(arr, l, m, r) t = int(input()) for i in range(t): n = int(input()) arr = list(map(int, input().split())) count = [0] * n for j in range(n): count[arr[j] - 1] += 1 realCount = [] for j in range(n): if count[j] != 0: realCount.append(count[j]) l = len(realCount) mergeSort(realCount, 0, l - 1) answer = [realCount[-1]] ans = 0 for j in range(2, l + 1): if realCount[-j] >= answer[-1]: if answer[-1] - 1 != 0: answer.append(answer[-1] - 1) else: answer.append(realCount[-j]) print(sum(answer))
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR 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 BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
q = int(input()) for i in range(q): n = int(input()) a = list(map(int, input().split())) f = {} mx = 0 mxi = 0 for j in a: if j not in f: f[j] = 0 f[j] += 1 ans = 0 d = sorted(f.items(), key=lambda kv: kv[1], reverse=True) mx = d[0][1] ans += mx k = 1 for j in range(mx - 1, 0, -1): if k < len(d) and d[k][1] >= j: k += 1 ans += j if k == len(d): break print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR 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 DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le n$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type $1$ and two candies of type $2$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have. You have to answer $q$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains one integer $q$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----Output----- For each query print one integer β€” the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement. -----Example----- Input 3 8 1 4 8 4 5 6 3 8 16 2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1 9 2 2 4 4 4 7 7 7 7 Output 3 10 9 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
import sys for _ in range(int(input())): n = [0] * int(input()) for i in [int(i) for i in sys.stdin.readline().split()]: n[i - 1] += 1 n.sort(reverse=True) S = 0 curr = n[0] + 1 for a in n: if a < curr: curr = a else: curr -= 1 S += curr if curr == 1 or a == 0: break print(S)
IMPORT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR