inputs
stringlengths
50
14k
targets
stringlengths
4
655k
The only difference between easy and hard versions is constraints. The BerTV channel every day broadcasts one episode of one of the $k$ TV shows. You know the schedule for the next $n$ days: a sequence of integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the show, the episode of which will be shown in $i$-th day. The subscription to the show is bought for the entire show (i.e. for all its episodes), for each show the subscription is bought separately. How many minimum subscriptions do you need to buy in order to have the opportunity to watch episodes of purchased shows $d$ ($1 \le d \le n$) days in a row? In other words, you want to buy the minimum number of TV shows so that there is some segment of $d$ consecutive days in which all episodes belong to the purchased shows. -----Input----- The first line contains an integer $t$ ($1 \le t \le 10000$) β€” the number of test cases in the input. Then $t$ test case descriptions follow. The first line of each test case contains three integers $n, k$ and $d$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le 10^6$, $1 \le d \le n$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the show that is broadcasted on the $i$-th day. It is guaranteed that the sum of the values ​​of $n$ for all test cases in the input does not exceed $2\cdot10^5$. -----Output----- Print $t$ integers β€” the answers to the test cases in the input in the order they follow. The answer to a test case is the minimum number of TV shows for which you need to purchase a subscription so that you can watch episodes of the purchased TV shows on BerTV for $d$ consecutive days. Please note that it is permissible that you will be able to watch more than $d$ days in a row. -----Example----- Input 4 5 2 2 1 2 1 2 1 9 3 3 3 3 3 2 2 2 1 1 1 4 10 4 10 8 6 4 16 9 8 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 Output 2 1 4 5 -----Note----- In the first test case to have an opportunity to watch shows for two consecutive days, you need to buy a subscription on show $1$ and on show $2$. So the answer is two. In the second test case, you can buy a subscription to any show because for each show you can find a segment of three consecutive days, consisting only of episodes of this show. In the third test case in the unique segment of four days, you have four different shows, so you need to buy a subscription to all these four shows. In the fourth test case, you can buy subscriptions to shows $3,5,7,8,9$, and you will be able to watch shows for the last eight days.
for i in range(int(input())): a = list(map(int, input().split())) n, k, d = a[0], a[1], a[2] a = list(map(int, input().split())) di = dict() m = 0 m2 = 1000000 n2 = 0 for j in range(len(a)): if n2 < d: if a[j] not in di: di[a[j]] = 1 m += 1 else: if di[a[j]] == 0: m += 1 di[a[j]] += 1 n2 += 1 else: if di[a[j - d]] == 1: di[a[j - d]] = 0 m -= 1 else: di[a[j - d]] -= 1 if a[j] not in di: di[a[j]] = 1 m += 1 else: if di[a[j]] == 0: m += 1 di[a[j]] += 1 if n2 == d and m < m2: m2 = m print(m2)
The only difference between easy and hard versions is constraints. The BerTV channel every day broadcasts one episode of one of the $k$ TV shows. You know the schedule for the next $n$ days: a sequence of integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the show, the episode of which will be shown in $i$-th day. The subscription to the show is bought for the entire show (i.e. for all its episodes), for each show the subscription is bought separately. How many minimum subscriptions do you need to buy in order to have the opportunity to watch episodes of purchased shows $d$ ($1 \le d \le n$) days in a row? In other words, you want to buy the minimum number of TV shows so that there is some segment of $d$ consecutive days in which all episodes belong to the purchased shows. -----Input----- The first line contains an integer $t$ ($1 \le t \le 10000$) β€” the number of test cases in the input. Then $t$ test case descriptions follow. The first line of each test case contains three integers $n, k$ and $d$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le 10^6$, $1 \le d \le n$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the show that is broadcasted on the $i$-th day. It is guaranteed that the sum of the values ​​of $n$ for all test cases in the input does not exceed $2\cdot10^5$. -----Output----- Print $t$ integers β€” the answers to the test cases in the input in the order they follow. The answer to a test case is the minimum number of TV shows for which you need to purchase a subscription so that you can watch episodes of the purchased TV shows on BerTV for $d$ consecutive days. Please note that it is permissible that you will be able to watch more than $d$ days in a row. -----Example----- Input 4 5 2 2 1 2 1 2 1 9 3 3 3 3 3 2 2 2 1 1 1 4 10 4 10 8 6 4 16 9 8 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 Output 2 1 4 5 -----Note----- In the first test case to have an opportunity to watch shows for two consecutive days, you need to buy a subscription on show $1$ and on show $2$. So the answer is two. In the second test case, you can buy a subscription to any show because for each show you can find a segment of three consecutive days, consisting only of episodes of this show. In the third test case in the unique segment of four days, you have four different shows, so you need to buy a subscription to all these four shows. In the fourth test case, you can buy subscriptions to shows $3,5,7,8,9$, and you will be able to watch shows for the last eight days.
c = [[0, 0] for i in range(round(1e6+1))] def ans(t) : a, k, s = [int(x) for x in input().split()] v = list([int(x) for x in input().split()]) y, x, a, m = 0, 0, 0, 1e12 for i in range(len(v)) : if c[v[i]][0] != t : c[v[i]] = [t, 0] c[v[i]][1] += 1 if c[v[i]][1] == 1 : a += 1 if i-y+1 > s : c[v[y]][1] -= 1 if c[v[y]][1] == 0 : a -= 1 y += 1 if a < m and i-y+1 == s : m = a return m t = int(input()) for i in range(t) : print(ans(i))
The only difference between easy and hard versions is constraints. The BerTV channel every day broadcasts one episode of one of the $k$ TV shows. You know the schedule for the next $n$ days: a sequence of integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the show, the episode of which will be shown in $i$-th day. The subscription to the show is bought for the entire show (i.e. for all its episodes), for each show the subscription is bought separately. How many minimum subscriptions do you need to buy in order to have the opportunity to watch episodes of purchased shows $d$ ($1 \le d \le n$) days in a row? In other words, you want to buy the minimum number of TV shows so that there is some segment of $d$ consecutive days in which all episodes belong to the purchased shows. -----Input----- The first line contains an integer $t$ ($1 \le t \le 10000$) β€” the number of test cases in the input. Then $t$ test case descriptions follow. The first line of each test case contains three integers $n, k$ and $d$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le 10^6$, $1 \le d \le n$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the show that is broadcasted on the $i$-th day. It is guaranteed that the sum of the values ​​of $n$ for all test cases in the input does not exceed $2\cdot10^5$. -----Output----- Print $t$ integers β€” the answers to the test cases in the input in the order they follow. The answer to a test case is the minimum number of TV shows for which you need to purchase a subscription so that you can watch episodes of the purchased TV shows on BerTV for $d$ consecutive days. Please note that it is permissible that you will be able to watch more than $d$ days in a row. -----Example----- Input 4 5 2 2 1 2 1 2 1 9 3 3 3 3 3 2 2 2 1 1 1 4 10 4 10 8 6 4 16 9 8 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 Output 2 1 4 5 -----Note----- In the first test case to have an opportunity to watch shows for two consecutive days, you need to buy a subscription on show $1$ and on show $2$. So the answer is two. In the second test case, you can buy a subscription to any show because for each show you can find a segment of three consecutive days, consisting only of episodes of this show. In the third test case in the unique segment of four days, you have four different shows, so you need to buy a subscription to all these four shows. In the fourth test case, you can buy subscriptions to shows $3,5,7,8,9$, and you will be able to watch shows for the last eight days.
from collections import deque t = int(input()) for jfrhg in range(t): n, k, d = list(map(int, input().split())) a = list(map(int, input().split())) l = deque(a[:d]) s = dict() for i in l: if i in s: s[i]+=1 else: s[i] = 1 minimum = len(list(s.keys())) for i in range(d, n): ref = l.popleft() l.append(a[i]) s[ref] -=1 if s[ref]<1: del s[ref] if a[i] in s: s[a[i]]+=1 else: s[a[i]] = 1 if len(list(s.keys()))< minimum: minimum = len(list(s.keys())) print(minimum)
The only difference between easy and hard versions is constraints. The BerTV channel every day broadcasts one episode of one of the $k$ TV shows. You know the schedule for the next $n$ days: a sequence of integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the show, the episode of which will be shown in $i$-th day. The subscription to the show is bought for the entire show (i.e. for all its episodes), for each show the subscription is bought separately. How many minimum subscriptions do you need to buy in order to have the opportunity to watch episodes of purchased shows $d$ ($1 \le d \le n$) days in a row? In other words, you want to buy the minimum number of TV shows so that there is some segment of $d$ consecutive days in which all episodes belong to the purchased shows. -----Input----- The first line contains an integer $t$ ($1 \le t \le 10000$) β€” the number of test cases in the input. Then $t$ test case descriptions follow. The first line of each test case contains three integers $n, k$ and $d$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le 10^6$, $1 \le d \le n$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the show that is broadcasted on the $i$-th day. It is guaranteed that the sum of the values ​​of $n$ for all test cases in the input does not exceed $2\cdot10^5$. -----Output----- Print $t$ integers β€” the answers to the test cases in the input in the order they follow. The answer to a test case is the minimum number of TV shows for which you need to purchase a subscription so that you can watch episodes of the purchased TV shows on BerTV for $d$ consecutive days. Please note that it is permissible that you will be able to watch more than $d$ days in a row. -----Example----- Input 4 5 2 2 1 2 1 2 1 9 3 3 3 3 3 2 2 2 1 1 1 4 10 4 10 8 6 4 16 9 8 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 Output 2 1 4 5 -----Note----- In the first test case to have an opportunity to watch shows for two consecutive days, you need to buy a subscription on show $1$ and on show $2$. So the answer is two. In the second test case, you can buy a subscription to any show because for each show you can find a segment of three consecutive days, consisting only of episodes of this show. In the third test case in the unique segment of four days, you have four different shows, so you need to buy a subscription to all these four shows. In the fourth test case, you can buy subscriptions to shows $3,5,7,8,9$, and you will be able to watch shows for the last eight days.
from collections import deque for _ in range(int(input())): n, k, d = list(map(int, input().split())) a = list(map(int, input().split())) ans = len(set(a[:d])) now = dict() for i in range(d): if a[i] in now: now[a[i]] += 1 else: now[a[i]] = 1 for i in range(d, n): ans = min(ans, len(now)) now[a[i - d]] -= 1 if now[a[i - d]] == 0: now.pop(a[i - d]) if a[i] in now: now[a[i]] += 1 else: now[a[i]] = 1 ans = min(ans, len(now)) print(ans)
The only difference between easy and hard versions is constraints. The BerTV channel every day broadcasts one episode of one of the $k$ TV shows. You know the schedule for the next $n$ days: a sequence of integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the show, the episode of which will be shown in $i$-th day. The subscription to the show is bought for the entire show (i.e. for all its episodes), for each show the subscription is bought separately. How many minimum subscriptions do you need to buy in order to have the opportunity to watch episodes of purchased shows $d$ ($1 \le d \le n$) days in a row? In other words, you want to buy the minimum number of TV shows so that there is some segment of $d$ consecutive days in which all episodes belong to the purchased shows. -----Input----- The first line contains an integer $t$ ($1 \le t \le 10000$) β€” the number of test cases in the input. Then $t$ test case descriptions follow. The first line of each test case contains three integers $n, k$ and $d$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le 10^6$, $1 \le d \le n$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the show that is broadcasted on the $i$-th day. It is guaranteed that the sum of the values ​​of $n$ for all test cases in the input does not exceed $2\cdot10^5$. -----Output----- Print $t$ integers β€” the answers to the test cases in the input in the order they follow. The answer to a test case is the minimum number of TV shows for which you need to purchase a subscription so that you can watch episodes of the purchased TV shows on BerTV for $d$ consecutive days. Please note that it is permissible that you will be able to watch more than $d$ days in a row. -----Example----- Input 4 5 2 2 1 2 1 2 1 9 3 3 3 3 3 2 2 2 1 1 1 4 10 4 10 8 6 4 16 9 8 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 Output 2 1 4 5 -----Note----- In the first test case to have an opportunity to watch shows for two consecutive days, you need to buy a subscription on show $1$ and on show $2$. So the answer is two. In the second test case, you can buy a subscription to any show because for each show you can find a segment of three consecutive days, consisting only of episodes of this show. In the third test case in the unique segment of four days, you have four different shows, so you need to buy a subscription to all these four shows. In the fourth test case, you can buy subscriptions to shows $3,5,7,8,9$, and you will be able to watch shows for the last eight days.
for _ in range(int(input())): _, _, n = list(map(int, input().split())) a = [int(x) for x in input().split()] cnt = dict() for i in a[:n]: cnt[i] = cnt.get(i, 0) + 1 mlen = len(cnt) for i in range(n, len(a)): if a[i - n] != a[i]: if cnt[a[i - n]] == 1: del cnt[a[i - n]] else: cnt[a[i - n]] -= 1 cnt[a[i]] = cnt.get(a[i], 0) + 1 mlen = min(mlen, len(cnt)) print(mlen)
The only difference between easy and hard versions is constraints. The BerTV channel every day broadcasts one episode of one of the $k$ TV shows. You know the schedule for the next $n$ days: a sequence of integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the show, the episode of which will be shown in $i$-th day. The subscription to the show is bought for the entire show (i.e. for all its episodes), for each show the subscription is bought separately. How many minimum subscriptions do you need to buy in order to have the opportunity to watch episodes of purchased shows $d$ ($1 \le d \le n$) days in a row? In other words, you want to buy the minimum number of TV shows so that there is some segment of $d$ consecutive days in which all episodes belong to the purchased shows. -----Input----- The first line contains an integer $t$ ($1 \le t \le 10000$) β€” the number of test cases in the input. Then $t$ test case descriptions follow. The first line of each test case contains three integers $n, k$ and $d$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le 10^6$, $1 \le d \le n$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the show that is broadcasted on the $i$-th day. It is guaranteed that the sum of the values ​​of $n$ for all test cases in the input does not exceed $2\cdot10^5$. -----Output----- Print $t$ integers β€” the answers to the test cases in the input in the order they follow. The answer to a test case is the minimum number of TV shows for which you need to purchase a subscription so that you can watch episodes of the purchased TV shows on BerTV for $d$ consecutive days. Please note that it is permissible that you will be able to watch more than $d$ days in a row. -----Example----- Input 4 5 2 2 1 2 1 2 1 9 3 3 3 3 3 2 2 2 1 1 1 4 10 4 10 8 6 4 16 9 8 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 Output 2 1 4 5 -----Note----- In the first test case to have an opportunity to watch shows for two consecutive days, you need to buy a subscription on show $1$ and on show $2$. So the answer is two. In the second test case, you can buy a subscription to any show because for each show you can find a segment of three consecutive days, consisting only of episodes of this show. In the third test case in the unique segment of four days, you have four different shows, so you need to buy a subscription to all these four shows. In the fourth test case, you can buy subscriptions to shows $3,5,7,8,9$, and you will be able to watch shows for the last eight days.
t = int(input()) for _ in range(t): n, k, d = list(map(int, input().split())) timetable = list(map(int, input().split())) i = 0 j = d used = {} for x in range(d): if timetable[x] in list(used.keys()): used[timetable[x]] += 1 else: used[timetable[x]] = 1 ans = len(used) while j < n: if timetable[i] in list(used.keys()): used[timetable[i]] -= 1 if used[timetable[i]] == 0: used.pop(timetable[i]) i += 1 if timetable[j] in list(used.keys()): used[timetable[j]] += 1 else: used[timetable[j]] = 1 j += 1 ans = min(ans, len(used)) print(ans)
The only difference between easy and hard versions is constraints. The BerTV channel every day broadcasts one episode of one of the $k$ TV shows. You know the schedule for the next $n$ days: a sequence of integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the show, the episode of which will be shown in $i$-th day. The subscription to the show is bought for the entire show (i.e. for all its episodes), for each show the subscription is bought separately. How many minimum subscriptions do you need to buy in order to have the opportunity to watch episodes of purchased shows $d$ ($1 \le d \le n$) days in a row? In other words, you want to buy the minimum number of TV shows so that there is some segment of $d$ consecutive days in which all episodes belong to the purchased shows. -----Input----- The first line contains an integer $t$ ($1 \le t \le 10000$) β€” the number of test cases in the input. Then $t$ test case descriptions follow. The first line of each test case contains three integers $n, k$ and $d$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le 10^6$, $1 \le d \le n$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the show that is broadcasted on the $i$-th day. It is guaranteed that the sum of the values ​​of $n$ for all test cases in the input does not exceed $2\cdot10^5$. -----Output----- Print $t$ integers β€” the answers to the test cases in the input in the order they follow. The answer to a test case is the minimum number of TV shows for which you need to purchase a subscription so that you can watch episodes of the purchased TV shows on BerTV for $d$ consecutive days. Please note that it is permissible that you will be able to watch more than $d$ days in a row. -----Example----- Input 4 5 2 2 1 2 1 2 1 9 3 3 3 3 3 2 2 2 1 1 1 4 10 4 10 8 6 4 16 9 8 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 Output 2 1 4 5 -----Note----- In the first test case to have an opportunity to watch shows for two consecutive days, you need to buy a subscription on show $1$ and on show $2$. So the answer is two. In the second test case, you can buy a subscription to any show because for each show you can find a segment of three consecutive days, consisting only of episodes of this show. In the third test case in the unique segment of four days, you have four different shows, so you need to buy a subscription to all these four shows. In the fourth test case, you can buy subscriptions to shows $3,5,7,8,9$, and you will be able to watch shows for the last eight days.
for i in range(int(input())): n,k,d = map(int,input().split()) a = [int(s) for s in input().split()] s = dict() for j in range(d): if s.get(a[j],0) == 0: s[a[j]] = 1 else: s[a[j]] += 1 m = len(s) for j in range(1,n-d+1): if s[a[j-1]] == 1: s.pop(a[j-1]) else: s[a[j-1]] -= 1 if s.get(a[j+d-1],0) == 0: s[a[j+d-1]] = 1 else: s[a[j+d-1]] += 1 if len(s) < m: m = len(s) print(m)
The only difference between easy and hard versions is constraints. The BerTV channel every day broadcasts one episode of one of the $k$ TV shows. You know the schedule for the next $n$ days: a sequence of integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the show, the episode of which will be shown in $i$-th day. The subscription to the show is bought for the entire show (i.e. for all its episodes), for each show the subscription is bought separately. How many minimum subscriptions do you need to buy in order to have the opportunity to watch episodes of purchased shows $d$ ($1 \le d \le n$) days in a row? In other words, you want to buy the minimum number of TV shows so that there is some segment of $d$ consecutive days in which all episodes belong to the purchased shows. -----Input----- The first line contains an integer $t$ ($1 \le t \le 10000$) β€” the number of test cases in the input. Then $t$ test case descriptions follow. The first line of each test case contains three integers $n, k$ and $d$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le 10^6$, $1 \le d \le n$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the show that is broadcasted on the $i$-th day. It is guaranteed that the sum of the values ​​of $n$ for all test cases in the input does not exceed $2\cdot10^5$. -----Output----- Print $t$ integers β€” the answers to the test cases in the input in the order they follow. The answer to a test case is the minimum number of TV shows for which you need to purchase a subscription so that you can watch episodes of the purchased TV shows on BerTV for $d$ consecutive days. Please note that it is permissible that you will be able to watch more than $d$ days in a row. -----Example----- Input 4 5 2 2 1 2 1 2 1 9 3 3 3 3 3 2 2 2 1 1 1 4 10 4 10 8 6 4 16 9 8 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 Output 2 1 4 5 -----Note----- In the first test case to have an opportunity to watch shows for two consecutive days, you need to buy a subscription on show $1$ and on show $2$. So the answer is two. In the second test case, you can buy a subscription to any show because for each show you can find a segment of three consecutive days, consisting only of episodes of this show. In the third test case in the unique segment of four days, you have four different shows, so you need to buy a subscription to all these four shows. In the fourth test case, you can buy subscriptions to shows $3,5,7,8,9$, and you will be able to watch shows for the last eight days.
t = int(input()) for i in range(t): n, k, d = map(int, input().split()) data = list(map(int, input().split())) dd = {} for j in range(d): elem = data[j] if elem in dd: dd[elem] += 1 else: dd[elem] = 1 m = len(dd) for j in range(d, n): elem = data[j] if elem in dd: dd[elem] += 1 else: dd[elem] = 1 dd[data[j - d]] -= 1 if dd[data[j - d]] == 0: dd.pop(data[j - d]) m = min(m, len(dd)) print(m)
The only difference between easy and hard versions is constraints. The BerTV channel every day broadcasts one episode of one of the $k$ TV shows. You know the schedule for the next $n$ days: a sequence of integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the show, the episode of which will be shown in $i$-th day. The subscription to the show is bought for the entire show (i.e. for all its episodes), for each show the subscription is bought separately. How many minimum subscriptions do you need to buy in order to have the opportunity to watch episodes of purchased shows $d$ ($1 \le d \le n$) days in a row? In other words, you want to buy the minimum number of TV shows so that there is some segment of $d$ consecutive days in which all episodes belong to the purchased shows. -----Input----- The first line contains an integer $t$ ($1 \le t \le 10000$) β€” the number of test cases in the input. Then $t$ test case descriptions follow. The first line of each test case contains three integers $n, k$ and $d$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le 10^6$, $1 \le d \le n$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the show that is broadcasted on the $i$-th day. It is guaranteed that the sum of the values ​​of $n$ for all test cases in the input does not exceed $2\cdot10^5$. -----Output----- Print $t$ integers β€” the answers to the test cases in the input in the order they follow. The answer to a test case is the minimum number of TV shows for which you need to purchase a subscription so that you can watch episodes of the purchased TV shows on BerTV for $d$ consecutive days. Please note that it is permissible that you will be able to watch more than $d$ days in a row. -----Example----- Input 4 5 2 2 1 2 1 2 1 9 3 3 3 3 3 2 2 2 1 1 1 4 10 4 10 8 6 4 16 9 8 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 Output 2 1 4 5 -----Note----- In the first test case to have an opportunity to watch shows for two consecutive days, you need to buy a subscription on show $1$ and on show $2$. So the answer is two. In the second test case, you can buy a subscription to any show because for each show you can find a segment of three consecutive days, consisting only of episodes of this show. In the third test case in the unique segment of four days, you have four different shows, so you need to buy a subscription to all these four shows. In the fourth test case, you can buy subscriptions to shows $3,5,7,8,9$, and you will be able to watch shows for the last eight days.
t = int(input()) for i in range(t): n, k, d = list(map(int, input().split())) a = list(map(int, input().split())) m = k s = dict() c = 0 for j in range(d): if a[j] in s: s[a[j]] += 1 else: c += 1 s[a[j]] = 1 mm = m = len(s) for j in range(d, n): if a[j-d] in s: s[a[j-d]] -= 1 if s[a[j-d]] == 0: del s[a[j-d]] m -= 1 else: s[a[j]] = 1 m += 1 if a[j] in s: s[a[j]] += 1 else: s[a[j]] = 1 m += 1 if m < mm: mm = m print(mm)
The only difference between easy and hard versions is constraints. The BerTV channel every day broadcasts one episode of one of the $k$ TV shows. You know the schedule for the next $n$ days: a sequence of integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the show, the episode of which will be shown in $i$-th day. The subscription to the show is bought for the entire show (i.e. for all its episodes), for each show the subscription is bought separately. How many minimum subscriptions do you need to buy in order to have the opportunity to watch episodes of purchased shows $d$ ($1 \le d \le n$) days in a row? In other words, you want to buy the minimum number of TV shows so that there is some segment of $d$ consecutive days in which all episodes belong to the purchased shows. -----Input----- The first line contains an integer $t$ ($1 \le t \le 10000$) β€” the number of test cases in the input. Then $t$ test case descriptions follow. The first line of each test case contains three integers $n, k$ and $d$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le 10^6$, $1 \le d \le n$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the show that is broadcasted on the $i$-th day. It is guaranteed that the sum of the values ​​of $n$ for all test cases in the input does not exceed $2\cdot10^5$. -----Output----- Print $t$ integers β€” the answers to the test cases in the input in the order they follow. The answer to a test case is the minimum number of TV shows for which you need to purchase a subscription so that you can watch episodes of the purchased TV shows on BerTV for $d$ consecutive days. Please note that it is permissible that you will be able to watch more than $d$ days in a row. -----Example----- Input 4 5 2 2 1 2 1 2 1 9 3 3 3 3 3 2 2 2 1 1 1 4 10 4 10 8 6 4 16 9 8 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 Output 2 1 4 5 -----Note----- In the first test case to have an opportunity to watch shows for two consecutive days, you need to buy a subscription on show $1$ and on show $2$. So the answer is two. In the second test case, you can buy a subscription to any show because for each show you can find a segment of three consecutive days, consisting only of episodes of this show. In the third test case in the unique segment of four days, you have four different shows, so you need to buy a subscription to all these four shows. In the fourth test case, you can buy subscriptions to shows $3,5,7,8,9$, and you will be able to watch shows for the last eight days.
def solve(n,k,d,a): #if n == d: # return len(set(a)) m = {} s = list(set(a)) ans = float("inf") for i in s: m[i] = 0 sm = 0 for i in range(d): m[a[i]] += 1 if m[a[i]] == 1: sm += 1 ans = sm #print(m) for i in range(d,n): # print(m) x = a[i-d] y = a[i] # print(x,y, i, d) m[x] -= 1 if m[x] == 0: sm -= 1 if m[y] == 0: sm += 1 m[y] += 1 ans = min(ans,sm) return ans def main(): t = int(input()) for i in range(t): n,k,d = list(map(int,input().split())) a = list(map(int,input().split())) print(solve(n,k,d,a)) main()
The only difference between easy and hard versions is constraints. The BerTV channel every day broadcasts one episode of one of the $k$ TV shows. You know the schedule for the next $n$ days: a sequence of integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the show, the episode of which will be shown in $i$-th day. The subscription to the show is bought for the entire show (i.e. for all its episodes), for each show the subscription is bought separately. How many minimum subscriptions do you need to buy in order to have the opportunity to watch episodes of purchased shows $d$ ($1 \le d \le n$) days in a row? In other words, you want to buy the minimum number of TV shows so that there is some segment of $d$ consecutive days in which all episodes belong to the purchased shows. -----Input----- The first line contains an integer $t$ ($1 \le t \le 10000$) β€” the number of test cases in the input. Then $t$ test case descriptions follow. The first line of each test case contains three integers $n, k$ and $d$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le 10^6$, $1 \le d \le n$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the show that is broadcasted on the $i$-th day. It is guaranteed that the sum of the values ​​of $n$ for all test cases in the input does not exceed $2\cdot10^5$. -----Output----- Print $t$ integers β€” the answers to the test cases in the input in the order they follow. The answer to a test case is the minimum number of TV shows for which you need to purchase a subscription so that you can watch episodes of the purchased TV shows on BerTV for $d$ consecutive days. Please note that it is permissible that you will be able to watch more than $d$ days in a row. -----Example----- Input 4 5 2 2 1 2 1 2 1 9 3 3 3 3 3 2 2 2 1 1 1 4 10 4 10 8 6 4 16 9 8 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 Output 2 1 4 5 -----Note----- In the first test case to have an opportunity to watch shows for two consecutive days, you need to buy a subscription on show $1$ and on show $2$. So the answer is two. In the second test case, you can buy a subscription to any show because for each show you can find a segment of three consecutive days, consisting only of episodes of this show. In the third test case in the unique segment of four days, you have four different shows, so you need to buy a subscription to all these four shows. In the fourth test case, you can buy subscriptions to shows $3,5,7,8,9$, and you will be able to watch shows for the last eight days.
3 def main(): t = int(input()) for _ in range(t): n1, k, d = [int(e) for e in input().split()] a = [int(e) for e in input().split()] s = dict() for e in a[:d]: s[e] = s.get(e, 0) + 1 b = len(s) n = b for i in range(d, n1): ai = a[i] aid = a[i-d] s[ai] = s.get(ai,0)+1 if s[ai] == 1: n += 1 s[aid] -= 1 if s[aid] == 0: n -= 1 b = min(n, b) print(b) def __starting_point(): main() __starting_point()
The only difference between easy and hard versions is constraints. The BerTV channel every day broadcasts one episode of one of the $k$ TV shows. You know the schedule for the next $n$ days: a sequence of integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the show, the episode of which will be shown in $i$-th day. The subscription to the show is bought for the entire show (i.e. for all its episodes), for each show the subscription is bought separately. How many minimum subscriptions do you need to buy in order to have the opportunity to watch episodes of purchased shows $d$ ($1 \le d \le n$) days in a row? In other words, you want to buy the minimum number of TV shows so that there is some segment of $d$ consecutive days in which all episodes belong to the purchased shows. -----Input----- The first line contains an integer $t$ ($1 \le t \le 10000$) β€” the number of test cases in the input. Then $t$ test case descriptions follow. The first line of each test case contains three integers $n, k$ and $d$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le 10^6$, $1 \le d \le n$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the show that is broadcasted on the $i$-th day. It is guaranteed that the sum of the values ​​of $n$ for all test cases in the input does not exceed $2\cdot10^5$. -----Output----- Print $t$ integers β€” the answers to the test cases in the input in the order they follow. The answer to a test case is the minimum number of TV shows for which you need to purchase a subscription so that you can watch episodes of the purchased TV shows on BerTV for $d$ consecutive days. Please note that it is permissible that you will be able to watch more than $d$ days in a row. -----Example----- Input 4 5 2 2 1 2 1 2 1 9 3 3 3 3 3 2 2 2 1 1 1 4 10 4 10 8 6 4 16 9 8 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 Output 2 1 4 5 -----Note----- In the first test case to have an opportunity to watch shows for two consecutive days, you need to buy a subscription on show $1$ and on show $2$. So the answer is two. In the second test case, you can buy a subscription to any show because for each show you can find a segment of three consecutive days, consisting only of episodes of this show. In the third test case in the unique segment of four days, you have four different shows, so you need to buy a subscription to all these four shows. In the fourth test case, you can buy subscriptions to shows $3,5,7,8,9$, and you will be able to watch shows for the last eight days.
t=int(input()) for q in range(t): n, k, d = list(map(int, input().split())) a = list(map(int, input().split())) g = {} m=k j=0 s=0 for i in range(0, n): f=a[i] l=g.get(f, 0) g[f]=l+1 j+=1 if l==0: s+=1 if j>d: vr=g[a[i-d]] g[a[i-d]]-=1 j-=1 if vr==1: s-=1 if j==d: m=min(m, s) print(m)
The only difference between easy and hard versions is constraints. The BerTV channel every day broadcasts one episode of one of the $k$ TV shows. You know the schedule for the next $n$ days: a sequence of integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the show, the episode of which will be shown in $i$-th day. The subscription to the show is bought for the entire show (i.e. for all its episodes), for each show the subscription is bought separately. How many minimum subscriptions do you need to buy in order to have the opportunity to watch episodes of purchased shows $d$ ($1 \le d \le n$) days in a row? In other words, you want to buy the minimum number of TV shows so that there is some segment of $d$ consecutive days in which all episodes belong to the purchased shows. -----Input----- The first line contains an integer $t$ ($1 \le t \le 10000$) β€” the number of test cases in the input. Then $t$ test case descriptions follow. The first line of each test case contains three integers $n, k$ and $d$ ($1 \le n \le 2\cdot10^5$, $1 \le k \le 10^6$, $1 \le d \le n$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le k$), where $a_i$ is the show that is broadcasted on the $i$-th day. It is guaranteed that the sum of the values ​​of $n$ for all test cases in the input does not exceed $2\cdot10^5$. -----Output----- Print $t$ integers β€” the answers to the test cases in the input in the order they follow. The answer to a test case is the minimum number of TV shows for which you need to purchase a subscription so that you can watch episodes of the purchased TV shows on BerTV for $d$ consecutive days. Please note that it is permissible that you will be able to watch more than $d$ days in a row. -----Example----- Input 4 5 2 2 1 2 1 2 1 9 3 3 3 3 3 2 2 2 1 1 1 4 10 4 10 8 6 4 16 9 8 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 Output 2 1 4 5 -----Note----- In the first test case to have an opportunity to watch shows for two consecutive days, you need to buy a subscription on show $1$ and on show $2$. So the answer is two. In the second test case, you can buy a subscription to any show because for each show you can find a segment of three consecutive days, consisting only of episodes of this show. In the third test case in the unique segment of four days, you have four different shows, so you need to buy a subscription to all these four shows. In the fourth test case, you can buy subscriptions to shows $3,5,7,8,9$, and you will be able to watch shows for the last eight days.
t=int(input()) for i1 in range(t): n,k,d=list(map(int,input().split())) a=list(map(int,input().split())) dic={} r=0 for i in range(d): e=a[i] if e in dic: dic[e]+=1 else: dic[e]=1 r+=1 m=r for i in range(1,n-d+1): e=a[i+d-1] if e in dic: dic[e]+=1 else: dic[e]=1 r+=1 e=a[i-1] if dic.get(e)>1: dic[e]-=1 else: dic.pop(e) r-=1 m=min(m,r) print(m)
Gildong owns a bulgogi restaurant. The restaurant has a lot of customers, so many of them like to make a reservation before visiting it. Gildong tries so hard to satisfy the customers that he even memorized all customers' preferred temperature ranges! Looking through the reservation list, he wants to satisfy all customers by controlling the temperature of the restaurant. The restaurant has an air conditioner that has 3 states: off, heating, and cooling. When it's off, the restaurant's temperature remains the same. When it's heating, the temperature increases by 1 in one minute. Lastly, when it's cooling, the temperature decreases by 1 in one minute. Gildong can change the state as many times as he wants, at any integer minutes. The air conditioner is off initially. Each customer is characterized by three values: $t_i$ β€” the time (in minutes) when the $i$-th customer visits the restaurant, $l_i$ β€” the lower bound of their preferred temperature range, and $h_i$ β€” the upper bound of their preferred temperature range. A customer is satisfied if the temperature is within the preferred range at the instant they visit the restaurant. Formally, the $i$-th customer is satisfied if and only if the temperature is between $l_i$ and $h_i$ (inclusive) in the $t_i$-th minute. Given the initial temperature, the list of reserved customers' visit times and their preferred temperature ranges, you're going to help him find if it's possible to satisfy all customers. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $q$ ($1 \le q \le 500$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 100$, $-10^9 \le m \le 10^9$), where $n$ is the number of reserved customers and $m$ is the initial temperature of the restaurant. Next, $n$ lines follow. The $i$-th line of them contains three integers $t_i$, $l_i$, and $h_i$ ($1 \le t_i \le 10^9$, $-10^9 \le l_i \le h_i \le 10^9$), where $t_i$ is the time when the $i$-th customer visits, $l_i$ is the lower bound of their preferred temperature range, and $h_i$ is the upper bound of their preferred temperature range. The preferred temperature ranges are inclusive. The customers are given in non-decreasing order of their visit time, and the current time is $0$. -----Output----- For each test case, print "YES" if it is possible to satisfy all customers. Otherwise, print "NO". You can print each letter in any case (upper or lower). -----Example----- Input 4 3 0 5 1 2 7 3 5 10 -1 0 2 12 5 7 10 10 16 20 3 -100 100 0 0 100 -50 50 200 100 100 1 100 99 -100 0 Output YES NO YES NO -----Note----- In the first case, Gildong can control the air conditioner to satisfy all customers in the following way: At $0$-th minute, change the state to heating (the temperature is 0). At $2$-nd minute, change the state to off (the temperature is 2). At $5$-th minute, change the state to heating (the temperature is 2, the $1$-st customer is satisfied). At $6$-th minute, change the state to off (the temperature is 3). At $7$-th minute, change the state to cooling (the temperature is 3, the $2$-nd customer is satisfied). At $10$-th minute, the temperature will be 0, which satisfies the last customer. In the third case, Gildong can change the state to heating at $0$-th minute and leave it be. Then all customers will be satisfied. Note that the $1$-st customer's visit time equals the $2$-nd customer's visit time. In the second and the fourth case, Gildong has to make at least one customer unsatisfied.
q = int(input()) for _ in range(q): n, m = list(map(int, input().split())) info = [list(map(int, input().split())) for i in range(n)] info = sorted(info) now =(m, m) time = 0 flag = True for i in range(n): t, l, h = info[i] l_now = now[0] - (t - time) h_now = now[1] + (t - time) time = t if h < l_now or h_now < l: flag = False else: l_now = max(l_now, l) h_now = min(h_now, h) now = (l_now, h_now) if flag: print("YES") else: print("NO")
Gildong owns a bulgogi restaurant. The restaurant has a lot of customers, so many of them like to make a reservation before visiting it. Gildong tries so hard to satisfy the customers that he even memorized all customers' preferred temperature ranges! Looking through the reservation list, he wants to satisfy all customers by controlling the temperature of the restaurant. The restaurant has an air conditioner that has 3 states: off, heating, and cooling. When it's off, the restaurant's temperature remains the same. When it's heating, the temperature increases by 1 in one minute. Lastly, when it's cooling, the temperature decreases by 1 in one minute. Gildong can change the state as many times as he wants, at any integer minutes. The air conditioner is off initially. Each customer is characterized by three values: $t_i$ β€” the time (in minutes) when the $i$-th customer visits the restaurant, $l_i$ β€” the lower bound of their preferred temperature range, and $h_i$ β€” the upper bound of their preferred temperature range. A customer is satisfied if the temperature is within the preferred range at the instant they visit the restaurant. Formally, the $i$-th customer is satisfied if and only if the temperature is between $l_i$ and $h_i$ (inclusive) in the $t_i$-th minute. Given the initial temperature, the list of reserved customers' visit times and their preferred temperature ranges, you're going to help him find if it's possible to satisfy all customers. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $q$ ($1 \le q \le 500$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 100$, $-10^9 \le m \le 10^9$), where $n$ is the number of reserved customers and $m$ is the initial temperature of the restaurant. Next, $n$ lines follow. The $i$-th line of them contains three integers $t_i$, $l_i$, and $h_i$ ($1 \le t_i \le 10^9$, $-10^9 \le l_i \le h_i \le 10^9$), where $t_i$ is the time when the $i$-th customer visits, $l_i$ is the lower bound of their preferred temperature range, and $h_i$ is the upper bound of their preferred temperature range. The preferred temperature ranges are inclusive. The customers are given in non-decreasing order of their visit time, and the current time is $0$. -----Output----- For each test case, print "YES" if it is possible to satisfy all customers. Otherwise, print "NO". You can print each letter in any case (upper or lower). -----Example----- Input 4 3 0 5 1 2 7 3 5 10 -1 0 2 12 5 7 10 10 16 20 3 -100 100 0 0 100 -50 50 200 100 100 1 100 99 -100 0 Output YES NO YES NO -----Note----- In the first case, Gildong can control the air conditioner to satisfy all customers in the following way: At $0$-th minute, change the state to heating (the temperature is 0). At $2$-nd minute, change the state to off (the temperature is 2). At $5$-th minute, change the state to heating (the temperature is 2, the $1$-st customer is satisfied). At $6$-th minute, change the state to off (the temperature is 3). At $7$-th minute, change the state to cooling (the temperature is 3, the $2$-nd customer is satisfied). At $10$-th minute, the temperature will be 0, which satisfies the last customer. In the third case, Gildong can change the state to heating at $0$-th minute and leave it be. Then all customers will be satisfied. Note that the $1$-st customer's visit time equals the $2$-nd customer's visit time. In the second and the fourth case, Gildong has to make at least one customer unsatisfied.
for _ in range(int(input())): n,m=map(int,input().split()) lm=hm=m pt=0 ans="YES" for i in range(n): t,l,h=map(int,input().split()) lm-=(t-pt) hm+=(t-pt) pt=t hm=min(h,hm) lm=max(l,lm) if hm<lm: ans="NO" print(ans)
Gildong owns a bulgogi restaurant. The restaurant has a lot of customers, so many of them like to make a reservation before visiting it. Gildong tries so hard to satisfy the customers that he even memorized all customers' preferred temperature ranges! Looking through the reservation list, he wants to satisfy all customers by controlling the temperature of the restaurant. The restaurant has an air conditioner that has 3 states: off, heating, and cooling. When it's off, the restaurant's temperature remains the same. When it's heating, the temperature increases by 1 in one minute. Lastly, when it's cooling, the temperature decreases by 1 in one minute. Gildong can change the state as many times as he wants, at any integer minutes. The air conditioner is off initially. Each customer is characterized by three values: $t_i$ β€” the time (in minutes) when the $i$-th customer visits the restaurant, $l_i$ β€” the lower bound of their preferred temperature range, and $h_i$ β€” the upper bound of their preferred temperature range. A customer is satisfied if the temperature is within the preferred range at the instant they visit the restaurant. Formally, the $i$-th customer is satisfied if and only if the temperature is between $l_i$ and $h_i$ (inclusive) in the $t_i$-th minute. Given the initial temperature, the list of reserved customers' visit times and their preferred temperature ranges, you're going to help him find if it's possible to satisfy all customers. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $q$ ($1 \le q \le 500$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 100$, $-10^9 \le m \le 10^9$), where $n$ is the number of reserved customers and $m$ is the initial temperature of the restaurant. Next, $n$ lines follow. The $i$-th line of them contains three integers $t_i$, $l_i$, and $h_i$ ($1 \le t_i \le 10^9$, $-10^9 \le l_i \le h_i \le 10^9$), where $t_i$ is the time when the $i$-th customer visits, $l_i$ is the lower bound of their preferred temperature range, and $h_i$ is the upper bound of their preferred temperature range. The preferred temperature ranges are inclusive. The customers are given in non-decreasing order of their visit time, and the current time is $0$. -----Output----- For each test case, print "YES" if it is possible to satisfy all customers. Otherwise, print "NO". You can print each letter in any case (upper or lower). -----Example----- Input 4 3 0 5 1 2 7 3 5 10 -1 0 2 12 5 7 10 10 16 20 3 -100 100 0 0 100 -50 50 200 100 100 1 100 99 -100 0 Output YES NO YES NO -----Note----- In the first case, Gildong can control the air conditioner to satisfy all customers in the following way: At $0$-th minute, change the state to heating (the temperature is 0). At $2$-nd minute, change the state to off (the temperature is 2). At $5$-th minute, change the state to heating (the temperature is 2, the $1$-st customer is satisfied). At $6$-th minute, change the state to off (the temperature is 3). At $7$-th minute, change the state to cooling (the temperature is 3, the $2$-nd customer is satisfied). At $10$-th minute, the temperature will be 0, which satisfies the last customer. In the third case, Gildong can change the state to heating at $0$-th minute and leave it be. Then all customers will be satisfied. Note that the $1$-st customer's visit time equals the $2$-nd customer's visit time. In the second and the fourth case, Gildong has to make at least one customer unsatisfied.
for _ in range(int(input())): n, m = list(map(int, input().split())) ar = [[0, -10 ** 9, 10 ** 9]] for ______ in range(n): ar.append(list(map(int, input().split()))) ar.sort() left, right = m, m ans = 'YES' for i in range(1, n + 1): left -= ar[i][0] - ar[i - 1][0] right += ar[i][0] - ar[i - 1][0] left, right = [max(left, ar[i][1]), min(right, ar[i][2])] if right - left < 0: ans = 'NO' print(ans)
Gildong owns a bulgogi restaurant. The restaurant has a lot of customers, so many of them like to make a reservation before visiting it. Gildong tries so hard to satisfy the customers that he even memorized all customers' preferred temperature ranges! Looking through the reservation list, he wants to satisfy all customers by controlling the temperature of the restaurant. The restaurant has an air conditioner that has 3 states: off, heating, and cooling. When it's off, the restaurant's temperature remains the same. When it's heating, the temperature increases by 1 in one minute. Lastly, when it's cooling, the temperature decreases by 1 in one minute. Gildong can change the state as many times as he wants, at any integer minutes. The air conditioner is off initially. Each customer is characterized by three values: $t_i$ β€” the time (in minutes) when the $i$-th customer visits the restaurant, $l_i$ β€” the lower bound of their preferred temperature range, and $h_i$ β€” the upper bound of their preferred temperature range. A customer is satisfied if the temperature is within the preferred range at the instant they visit the restaurant. Formally, the $i$-th customer is satisfied if and only if the temperature is between $l_i$ and $h_i$ (inclusive) in the $t_i$-th minute. Given the initial temperature, the list of reserved customers' visit times and their preferred temperature ranges, you're going to help him find if it's possible to satisfy all customers. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $q$ ($1 \le q \le 500$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 100$, $-10^9 \le m \le 10^9$), where $n$ is the number of reserved customers and $m$ is the initial temperature of the restaurant. Next, $n$ lines follow. The $i$-th line of them contains three integers $t_i$, $l_i$, and $h_i$ ($1 \le t_i \le 10^9$, $-10^9 \le l_i \le h_i \le 10^9$), where $t_i$ is the time when the $i$-th customer visits, $l_i$ is the lower bound of their preferred temperature range, and $h_i$ is the upper bound of their preferred temperature range. The preferred temperature ranges are inclusive. The customers are given in non-decreasing order of their visit time, and the current time is $0$. -----Output----- For each test case, print "YES" if it is possible to satisfy all customers. Otherwise, print "NO". You can print each letter in any case (upper or lower). -----Example----- Input 4 3 0 5 1 2 7 3 5 10 -1 0 2 12 5 7 10 10 16 20 3 -100 100 0 0 100 -50 50 200 100 100 1 100 99 -100 0 Output YES NO YES NO -----Note----- In the first case, Gildong can control the air conditioner to satisfy all customers in the following way: At $0$-th minute, change the state to heating (the temperature is 0). At $2$-nd minute, change the state to off (the temperature is 2). At $5$-th minute, change the state to heating (the temperature is 2, the $1$-st customer is satisfied). At $6$-th minute, change the state to off (the temperature is 3). At $7$-th minute, change the state to cooling (the temperature is 3, the $2$-nd customer is satisfied). At $10$-th minute, the temperature will be 0, which satisfies the last customer. In the third case, Gildong can change the state to heating at $0$-th minute and leave it be. Then all customers will be satisfied. Note that the $1$-st customer's visit time equals the $2$-nd customer's visit time. In the second and the fourth case, Gildong has to make at least one customer unsatisfied.
import sys input = sys.stdin.readline T = int(input()) for _ in range(T): n, m = map(int, input().split()) CUS = [tuple(map(int, input().split())) for _ in range(n)] CUS.sort() L = m R = m prv = 0 ans = True for t, l, r in CUS: d = t - prv L -= d R += d if R < l or r < L: ans = False break else: R = min(R, r) L = max(L, l) prv = t if ans: print("YES") else: print("NO")
Gildong owns a bulgogi restaurant. The restaurant has a lot of customers, so many of them like to make a reservation before visiting it. Gildong tries so hard to satisfy the customers that he even memorized all customers' preferred temperature ranges! Looking through the reservation list, he wants to satisfy all customers by controlling the temperature of the restaurant. The restaurant has an air conditioner that has 3 states: off, heating, and cooling. When it's off, the restaurant's temperature remains the same. When it's heating, the temperature increases by 1 in one minute. Lastly, when it's cooling, the temperature decreases by 1 in one minute. Gildong can change the state as many times as he wants, at any integer minutes. The air conditioner is off initially. Each customer is characterized by three values: $t_i$ β€” the time (in minutes) when the $i$-th customer visits the restaurant, $l_i$ β€” the lower bound of their preferred temperature range, and $h_i$ β€” the upper bound of their preferred temperature range. A customer is satisfied if the temperature is within the preferred range at the instant they visit the restaurant. Formally, the $i$-th customer is satisfied if and only if the temperature is between $l_i$ and $h_i$ (inclusive) in the $t_i$-th minute. Given the initial temperature, the list of reserved customers' visit times and their preferred temperature ranges, you're going to help him find if it's possible to satisfy all customers. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $q$ ($1 \le q \le 500$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 100$, $-10^9 \le m \le 10^9$), where $n$ is the number of reserved customers and $m$ is the initial temperature of the restaurant. Next, $n$ lines follow. The $i$-th line of them contains three integers $t_i$, $l_i$, and $h_i$ ($1 \le t_i \le 10^9$, $-10^9 \le l_i \le h_i \le 10^9$), where $t_i$ is the time when the $i$-th customer visits, $l_i$ is the lower bound of their preferred temperature range, and $h_i$ is the upper bound of their preferred temperature range. The preferred temperature ranges are inclusive. The customers are given in non-decreasing order of their visit time, and the current time is $0$. -----Output----- For each test case, print "YES" if it is possible to satisfy all customers. Otherwise, print "NO". You can print each letter in any case (upper or lower). -----Example----- Input 4 3 0 5 1 2 7 3 5 10 -1 0 2 12 5 7 10 10 16 20 3 -100 100 0 0 100 -50 50 200 100 100 1 100 99 -100 0 Output YES NO YES NO -----Note----- In the first case, Gildong can control the air conditioner to satisfy all customers in the following way: At $0$-th minute, change the state to heating (the temperature is 0). At $2$-nd minute, change the state to off (the temperature is 2). At $5$-th minute, change the state to heating (the temperature is 2, the $1$-st customer is satisfied). At $6$-th minute, change the state to off (the temperature is 3). At $7$-th minute, change the state to cooling (the temperature is 3, the $2$-nd customer is satisfied). At $10$-th minute, the temperature will be 0, which satisfies the last customer. In the third case, Gildong can change the state to heating at $0$-th minute and leave it be. Then all customers will be satisfied. Note that the $1$-st customer's visit time equals the $2$-nd customer's visit time. In the second and the fourth case, Gildong has to make at least one customer unsatisfied.
def solve(): n, m = list(map(int, input().split())) cust = [] for ___ in range(n): t, l, h = list(map(int, input().split())) cust.append((t, l, h)) cust.sort() lastT = 0 lastMaxT = m lastMinT = m for t, l, h in cust: nextMax = lastMaxT + t - lastT nextMin = lastMinT - t + lastT if nextMax < l or nextMin > h: print("NO") return lastMaxT = min(h, nextMax) lastMinT = max(l, nextMin) lastT = t print("YES") q = int(input()) for __ in range(q): solve()
Gildong owns a bulgogi restaurant. The restaurant has a lot of customers, so many of them like to make a reservation before visiting it. Gildong tries so hard to satisfy the customers that he even memorized all customers' preferred temperature ranges! Looking through the reservation list, he wants to satisfy all customers by controlling the temperature of the restaurant. The restaurant has an air conditioner that has 3 states: off, heating, and cooling. When it's off, the restaurant's temperature remains the same. When it's heating, the temperature increases by 1 in one minute. Lastly, when it's cooling, the temperature decreases by 1 in one minute. Gildong can change the state as many times as he wants, at any integer minutes. The air conditioner is off initially. Each customer is characterized by three values: $t_i$ β€” the time (in minutes) when the $i$-th customer visits the restaurant, $l_i$ β€” the lower bound of their preferred temperature range, and $h_i$ β€” the upper bound of their preferred temperature range. A customer is satisfied if the temperature is within the preferred range at the instant they visit the restaurant. Formally, the $i$-th customer is satisfied if and only if the temperature is between $l_i$ and $h_i$ (inclusive) in the $t_i$-th minute. Given the initial temperature, the list of reserved customers' visit times and their preferred temperature ranges, you're going to help him find if it's possible to satisfy all customers. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $q$ ($1 \le q \le 500$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 100$, $-10^9 \le m \le 10^9$), where $n$ is the number of reserved customers and $m$ is the initial temperature of the restaurant. Next, $n$ lines follow. The $i$-th line of them contains three integers $t_i$, $l_i$, and $h_i$ ($1 \le t_i \le 10^9$, $-10^9 \le l_i \le h_i \le 10^9$), where $t_i$ is the time when the $i$-th customer visits, $l_i$ is the lower bound of their preferred temperature range, and $h_i$ is the upper bound of their preferred temperature range. The preferred temperature ranges are inclusive. The customers are given in non-decreasing order of their visit time, and the current time is $0$. -----Output----- For each test case, print "YES" if it is possible to satisfy all customers. Otherwise, print "NO". You can print each letter in any case (upper or lower). -----Example----- Input 4 3 0 5 1 2 7 3 5 10 -1 0 2 12 5 7 10 10 16 20 3 -100 100 0 0 100 -50 50 200 100 100 1 100 99 -100 0 Output YES NO YES NO -----Note----- In the first case, Gildong can control the air conditioner to satisfy all customers in the following way: At $0$-th minute, change the state to heating (the temperature is 0). At $2$-nd minute, change the state to off (the temperature is 2). At $5$-th minute, change the state to heating (the temperature is 2, the $1$-st customer is satisfied). At $6$-th minute, change the state to off (the temperature is 3). At $7$-th minute, change the state to cooling (the temperature is 3, the $2$-nd customer is satisfied). At $10$-th minute, the temperature will be 0, which satisfies the last customer. In the third case, Gildong can change the state to heating at $0$-th minute and leave it be. Then all customers will be satisfied. Note that the $1$-st customer's visit time equals the $2$-nd customer's visit time. In the second and the fourth case, Gildong has to make at least one customer unsatisfied.
for t in range(int(input())): n, m = list(map(int, input().split())) a = [] for i in range(n): a.append(list(map(int, input().split()))) t0 = 0 mi, ma = m, m f = True for t, l, h in a: delta = t - t0 t0 = t mi -= delta ma += delta if mi <= l and ma >= h: mi = l ma = h elif l <= ma <= h and mi <= l: mi = l ma = ma elif l <= ma <= h and l <= mi <= h: mi = mi ma = ma elif ma >= h and l <= mi <= h: ma = h mi = mi else: f = False if f: print("YES") else: print("NO")
Gildong owns a bulgogi restaurant. The restaurant has a lot of customers, so many of them like to make a reservation before visiting it. Gildong tries so hard to satisfy the customers that he even memorized all customers' preferred temperature ranges! Looking through the reservation list, he wants to satisfy all customers by controlling the temperature of the restaurant. The restaurant has an air conditioner that has 3 states: off, heating, and cooling. When it's off, the restaurant's temperature remains the same. When it's heating, the temperature increases by 1 in one minute. Lastly, when it's cooling, the temperature decreases by 1 in one minute. Gildong can change the state as many times as he wants, at any integer minutes. The air conditioner is off initially. Each customer is characterized by three values: $t_i$ β€” the time (in minutes) when the $i$-th customer visits the restaurant, $l_i$ β€” the lower bound of their preferred temperature range, and $h_i$ β€” the upper bound of their preferred temperature range. A customer is satisfied if the temperature is within the preferred range at the instant they visit the restaurant. Formally, the $i$-th customer is satisfied if and only if the temperature is between $l_i$ and $h_i$ (inclusive) in the $t_i$-th minute. Given the initial temperature, the list of reserved customers' visit times and their preferred temperature ranges, you're going to help him find if it's possible to satisfy all customers. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $q$ ($1 \le q \le 500$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 100$, $-10^9 \le m \le 10^9$), where $n$ is the number of reserved customers and $m$ is the initial temperature of the restaurant. Next, $n$ lines follow. The $i$-th line of them contains three integers $t_i$, $l_i$, and $h_i$ ($1 \le t_i \le 10^9$, $-10^9 \le l_i \le h_i \le 10^9$), where $t_i$ is the time when the $i$-th customer visits, $l_i$ is the lower bound of their preferred temperature range, and $h_i$ is the upper bound of their preferred temperature range. The preferred temperature ranges are inclusive. The customers are given in non-decreasing order of their visit time, and the current time is $0$. -----Output----- For each test case, print "YES" if it is possible to satisfy all customers. Otherwise, print "NO". You can print each letter in any case (upper or lower). -----Example----- Input 4 3 0 5 1 2 7 3 5 10 -1 0 2 12 5 7 10 10 16 20 3 -100 100 0 0 100 -50 50 200 100 100 1 100 99 -100 0 Output YES NO YES NO -----Note----- In the first case, Gildong can control the air conditioner to satisfy all customers in the following way: At $0$-th minute, change the state to heating (the temperature is 0). At $2$-nd minute, change the state to off (the temperature is 2). At $5$-th minute, change the state to heating (the temperature is 2, the $1$-st customer is satisfied). At $6$-th minute, change the state to off (the temperature is 3). At $7$-th minute, change the state to cooling (the temperature is 3, the $2$-nd customer is satisfied). At $10$-th minute, the temperature will be 0, which satisfies the last customer. In the third case, Gildong can change the state to heating at $0$-th minute and leave it be. Then all customers will be satisfied. Note that the $1$-st customer's visit time equals the $2$-nd customer's visit time. In the second and the fourth case, Gildong has to make at least one customer unsatisfied.
T = int(input()) for _ in range(T): n, m = list(map(int, input().split())) time, mx, mn = 0, m, m flag = True for __ in range(n): x, y, z = list(map(int, input().split())) if not flag: continue mx += x - time mn -= x - time if mx < y or mn > z: flag = False if mx > z: mx = z if mn < y: mn = y time = x print('YES' if flag else 'NO')
Gildong owns a bulgogi restaurant. The restaurant has a lot of customers, so many of them like to make a reservation before visiting it. Gildong tries so hard to satisfy the customers that he even memorized all customers' preferred temperature ranges! Looking through the reservation list, he wants to satisfy all customers by controlling the temperature of the restaurant. The restaurant has an air conditioner that has 3 states: off, heating, and cooling. When it's off, the restaurant's temperature remains the same. When it's heating, the temperature increases by 1 in one minute. Lastly, when it's cooling, the temperature decreases by 1 in one minute. Gildong can change the state as many times as he wants, at any integer minutes. The air conditioner is off initially. Each customer is characterized by three values: $t_i$ β€” the time (in minutes) when the $i$-th customer visits the restaurant, $l_i$ β€” the lower bound of their preferred temperature range, and $h_i$ β€” the upper bound of their preferred temperature range. A customer is satisfied if the temperature is within the preferred range at the instant they visit the restaurant. Formally, the $i$-th customer is satisfied if and only if the temperature is between $l_i$ and $h_i$ (inclusive) in the $t_i$-th minute. Given the initial temperature, the list of reserved customers' visit times and their preferred temperature ranges, you're going to help him find if it's possible to satisfy all customers. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $q$ ($1 \le q \le 500$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 100$, $-10^9 \le m \le 10^9$), where $n$ is the number of reserved customers and $m$ is the initial temperature of the restaurant. Next, $n$ lines follow. The $i$-th line of them contains three integers $t_i$, $l_i$, and $h_i$ ($1 \le t_i \le 10^9$, $-10^9 \le l_i \le h_i \le 10^9$), where $t_i$ is the time when the $i$-th customer visits, $l_i$ is the lower bound of their preferred temperature range, and $h_i$ is the upper bound of their preferred temperature range. The preferred temperature ranges are inclusive. The customers are given in non-decreasing order of their visit time, and the current time is $0$. -----Output----- For each test case, print "YES" if it is possible to satisfy all customers. Otherwise, print "NO". You can print each letter in any case (upper or lower). -----Example----- Input 4 3 0 5 1 2 7 3 5 10 -1 0 2 12 5 7 10 10 16 20 3 -100 100 0 0 100 -50 50 200 100 100 1 100 99 -100 0 Output YES NO YES NO -----Note----- In the first case, Gildong can control the air conditioner to satisfy all customers in the following way: At $0$-th minute, change the state to heating (the temperature is 0). At $2$-nd minute, change the state to off (the temperature is 2). At $5$-th minute, change the state to heating (the temperature is 2, the $1$-st customer is satisfied). At $6$-th minute, change the state to off (the temperature is 3). At $7$-th minute, change the state to cooling (the temperature is 3, the $2$-nd customer is satisfied). At $10$-th minute, the temperature will be 0, which satisfies the last customer. In the third case, Gildong can change the state to heating at $0$-th minute and leave it be. Then all customers will be satisfied. Note that the $1$-st customer's visit time equals the $2$-nd customer's visit time. In the second and the fourth case, Gildong has to make at least one customer unsatisfied.
def getIntersect(a, b, c, d): return (max(a, c), min(b, d)) def solve(N, M, A): tHi = M tLo = M A.sort() lastT = 0 for t, l, h in A: deltaT = t - lastT tLo -= deltaT tHi += deltaT tLo, tHi = getIntersect(tLo, tHi, l, h) if tLo > tHi: return "NO" lastT = t return "YES" def __starting_point(): T, = list(map(int, input().split())) for t in range(T): N, M = list(map(int, input().split())) A = [] for i in range(N): tlh = [int(x) for x in input().split()] A.append(tlh) ans = solve(N, M, A) print(ans) __starting_point()
Gildong owns a bulgogi restaurant. The restaurant has a lot of customers, so many of them like to make a reservation before visiting it. Gildong tries so hard to satisfy the customers that he even memorized all customers' preferred temperature ranges! Looking through the reservation list, he wants to satisfy all customers by controlling the temperature of the restaurant. The restaurant has an air conditioner that has 3 states: off, heating, and cooling. When it's off, the restaurant's temperature remains the same. When it's heating, the temperature increases by 1 in one minute. Lastly, when it's cooling, the temperature decreases by 1 in one minute. Gildong can change the state as many times as he wants, at any integer minutes. The air conditioner is off initially. Each customer is characterized by three values: $t_i$ β€” the time (in minutes) when the $i$-th customer visits the restaurant, $l_i$ β€” the lower bound of their preferred temperature range, and $h_i$ β€” the upper bound of their preferred temperature range. A customer is satisfied if the temperature is within the preferred range at the instant they visit the restaurant. Formally, the $i$-th customer is satisfied if and only if the temperature is between $l_i$ and $h_i$ (inclusive) in the $t_i$-th minute. Given the initial temperature, the list of reserved customers' visit times and their preferred temperature ranges, you're going to help him find if it's possible to satisfy all customers. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $q$ ($1 \le q \le 500$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 100$, $-10^9 \le m \le 10^9$), where $n$ is the number of reserved customers and $m$ is the initial temperature of the restaurant. Next, $n$ lines follow. The $i$-th line of them contains three integers $t_i$, $l_i$, and $h_i$ ($1 \le t_i \le 10^9$, $-10^9 \le l_i \le h_i \le 10^9$), where $t_i$ is the time when the $i$-th customer visits, $l_i$ is the lower bound of their preferred temperature range, and $h_i$ is the upper bound of their preferred temperature range. The preferred temperature ranges are inclusive. The customers are given in non-decreasing order of their visit time, and the current time is $0$. -----Output----- For each test case, print "YES" if it is possible to satisfy all customers. Otherwise, print "NO". You can print each letter in any case (upper or lower). -----Example----- Input 4 3 0 5 1 2 7 3 5 10 -1 0 2 12 5 7 10 10 16 20 3 -100 100 0 0 100 -50 50 200 100 100 1 100 99 -100 0 Output YES NO YES NO -----Note----- In the first case, Gildong can control the air conditioner to satisfy all customers in the following way: At $0$-th minute, change the state to heating (the temperature is 0). At $2$-nd minute, change the state to off (the temperature is 2). At $5$-th minute, change the state to heating (the temperature is 2, the $1$-st customer is satisfied). At $6$-th minute, change the state to off (the temperature is 3). At $7$-th minute, change the state to cooling (the temperature is 3, the $2$-nd customer is satisfied). At $10$-th minute, the temperature will be 0, which satisfies the last customer. In the third case, Gildong can change the state to heating at $0$-th minute and leave it be. Then all customers will be satisfied. Note that the $1$-st customer's visit time equals the $2$-nd customer's visit time. In the second and the fourth case, Gildong has to make at least one customer unsatisfied.
Q = int(input()) for _ in range(Q): n, m = map(int, input().split()) lt, mn, mx = 0, m, m ok = True for i in range(n): if ok: t, l, h = map(int, input().split()) mn = max(mn - (t - lt), l) mx = min(mx + (t - lt), h) lt = t if mn>mx: ok=False else: input() print('YES' if ok else 'NO')
Gildong owns a bulgogi restaurant. The restaurant has a lot of customers, so many of them like to make a reservation before visiting it. Gildong tries so hard to satisfy the customers that he even memorized all customers' preferred temperature ranges! Looking through the reservation list, he wants to satisfy all customers by controlling the temperature of the restaurant. The restaurant has an air conditioner that has 3 states: off, heating, and cooling. When it's off, the restaurant's temperature remains the same. When it's heating, the temperature increases by 1 in one minute. Lastly, when it's cooling, the temperature decreases by 1 in one minute. Gildong can change the state as many times as he wants, at any integer minutes. The air conditioner is off initially. Each customer is characterized by three values: $t_i$ β€” the time (in minutes) when the $i$-th customer visits the restaurant, $l_i$ β€” the lower bound of their preferred temperature range, and $h_i$ β€” the upper bound of their preferred temperature range. A customer is satisfied if the temperature is within the preferred range at the instant they visit the restaurant. Formally, the $i$-th customer is satisfied if and only if the temperature is between $l_i$ and $h_i$ (inclusive) in the $t_i$-th minute. Given the initial temperature, the list of reserved customers' visit times and their preferred temperature ranges, you're going to help him find if it's possible to satisfy all customers. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $q$ ($1 \le q \le 500$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 100$, $-10^9 \le m \le 10^9$), where $n$ is the number of reserved customers and $m$ is the initial temperature of the restaurant. Next, $n$ lines follow. The $i$-th line of them contains three integers $t_i$, $l_i$, and $h_i$ ($1 \le t_i \le 10^9$, $-10^9 \le l_i \le h_i \le 10^9$), where $t_i$ is the time when the $i$-th customer visits, $l_i$ is the lower bound of their preferred temperature range, and $h_i$ is the upper bound of their preferred temperature range. The preferred temperature ranges are inclusive. The customers are given in non-decreasing order of their visit time, and the current time is $0$. -----Output----- For each test case, print "YES" if it is possible to satisfy all customers. Otherwise, print "NO". You can print each letter in any case (upper or lower). -----Example----- Input 4 3 0 5 1 2 7 3 5 10 -1 0 2 12 5 7 10 10 16 20 3 -100 100 0 0 100 -50 50 200 100 100 1 100 99 -100 0 Output YES NO YES NO -----Note----- In the first case, Gildong can control the air conditioner to satisfy all customers in the following way: At $0$-th minute, change the state to heating (the temperature is 0). At $2$-nd minute, change the state to off (the temperature is 2). At $5$-th minute, change the state to heating (the temperature is 2, the $1$-st customer is satisfied). At $6$-th minute, change the state to off (the temperature is 3). At $7$-th minute, change the state to cooling (the temperature is 3, the $2$-nd customer is satisfied). At $10$-th minute, the temperature will be 0, which satisfies the last customer. In the third case, Gildong can change the state to heating at $0$-th minute and leave it be. Then all customers will be satisfied. Note that the $1$-st customer's visit time equals the $2$-nd customer's visit time. In the second and the fourth case, Gildong has to make at least one customer unsatisfied.
q = int(input()) for rewrew in range(q): n, t0 = map(int,input().split()) t = [] l = [] h = [] for i in range(n): tt,ll,hh = map(int,input().split()) t.append(tt) l.append(ll) h.append(hh) possib = [[0,0]]*n dasie = True possib[0] = [-t[0]+t0,t[0]+t0] for i in range(n): if l[i]>possib[i][1] or h[i] < possib[i][0]: dasie = False break else: possib[i][0] = max(possib[i][0],l[i]) possib[i][1] = min(possib[i][1], h[i]) if i < n-1: possib[i+1][0] = possib[i][0] - (t[i+1]-t[i]) possib[i+1][1] = possib[i][1] + (t[i+1]-t[i]) if dasie: print("YES") else: print("NO")
Gildong owns a bulgogi restaurant. The restaurant has a lot of customers, so many of them like to make a reservation before visiting it. Gildong tries so hard to satisfy the customers that he even memorized all customers' preferred temperature ranges! Looking through the reservation list, he wants to satisfy all customers by controlling the temperature of the restaurant. The restaurant has an air conditioner that has 3 states: off, heating, and cooling. When it's off, the restaurant's temperature remains the same. When it's heating, the temperature increases by 1 in one minute. Lastly, when it's cooling, the temperature decreases by 1 in one minute. Gildong can change the state as many times as he wants, at any integer minutes. The air conditioner is off initially. Each customer is characterized by three values: $t_i$ β€” the time (in minutes) when the $i$-th customer visits the restaurant, $l_i$ β€” the lower bound of their preferred temperature range, and $h_i$ β€” the upper bound of their preferred temperature range. A customer is satisfied if the temperature is within the preferred range at the instant they visit the restaurant. Formally, the $i$-th customer is satisfied if and only if the temperature is between $l_i$ and $h_i$ (inclusive) in the $t_i$-th minute. Given the initial temperature, the list of reserved customers' visit times and their preferred temperature ranges, you're going to help him find if it's possible to satisfy all customers. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $q$ ($1 \le q \le 500$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 100$, $-10^9 \le m \le 10^9$), where $n$ is the number of reserved customers and $m$ is the initial temperature of the restaurant. Next, $n$ lines follow. The $i$-th line of them contains three integers $t_i$, $l_i$, and $h_i$ ($1 \le t_i \le 10^9$, $-10^9 \le l_i \le h_i \le 10^9$), where $t_i$ is the time when the $i$-th customer visits, $l_i$ is the lower bound of their preferred temperature range, and $h_i$ is the upper bound of their preferred temperature range. The preferred temperature ranges are inclusive. The customers are given in non-decreasing order of their visit time, and the current time is $0$. -----Output----- For each test case, print "YES" if it is possible to satisfy all customers. Otherwise, print "NO". You can print each letter in any case (upper or lower). -----Example----- Input 4 3 0 5 1 2 7 3 5 10 -1 0 2 12 5 7 10 10 16 20 3 -100 100 0 0 100 -50 50 200 100 100 1 100 99 -100 0 Output YES NO YES NO -----Note----- In the first case, Gildong can control the air conditioner to satisfy all customers in the following way: At $0$-th minute, change the state to heating (the temperature is 0). At $2$-nd minute, change the state to off (the temperature is 2). At $5$-th minute, change the state to heating (the temperature is 2, the $1$-st customer is satisfied). At $6$-th minute, change the state to off (the temperature is 3). At $7$-th minute, change the state to cooling (the temperature is 3, the $2$-nd customer is satisfied). At $10$-th minute, the temperature will be 0, which satisfies the last customer. In the third case, Gildong can change the state to heating at $0$-th minute and leave it be. Then all customers will be satisfied. Note that the $1$-st customer's visit time equals the $2$-nd customer's visit time. In the second and the fourth case, Gildong has to make at least one customer unsatisfied.
from collections import defaultdict def problemA(): t = int(input()) for _ in range(t): x, y, a, b = list(map(int, input().split())) if (y - x) % (a + b) == 0: print((y - x) // (a + b)) else: print(-1) def problemB(): n, m = list(map(int, input().split())) ss = set() res = [] for i in range(n): s = input() rs = s[::-1] if rs in ss: res.append(s) ss.remove(rs) else: ss.add(s) long = '' for s in ss: if s == s[::-1] and len(s) > len(int): long = s res = ''.join(res) res = res + int + res[::-1] print(len(res)) print(res) def problemC(): inf = 2 * 10 ** 9 q = int(input()) for _ in range(q): n, m = list(map(int, input().split())) a = defaultdict(lambda: (-inf, inf)) for _ in range(n): t, l, h = list(map(int, input().split())) pl, ph = a[t] a[t] = (max(l, pl), min(h, ph)) pt = 0 pl, ph = m, m res = 'YES' for t in sorted(a.keys()): l, h = a[t] delta = t - pt cl = pl - delta ch = ph + delta pl = max(l, cl) ph = min(h, ch) if pl > ph: res = 'NO' break pt = t print(res) def problemG(): t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) res = 0 print(a) def __starting_point(): problemC() __starting_point()
Gildong owns a bulgogi restaurant. The restaurant has a lot of customers, so many of them like to make a reservation before visiting it. Gildong tries so hard to satisfy the customers that he even memorized all customers' preferred temperature ranges! Looking through the reservation list, he wants to satisfy all customers by controlling the temperature of the restaurant. The restaurant has an air conditioner that has 3 states: off, heating, and cooling. When it's off, the restaurant's temperature remains the same. When it's heating, the temperature increases by 1 in one minute. Lastly, when it's cooling, the temperature decreases by 1 in one minute. Gildong can change the state as many times as he wants, at any integer minutes. The air conditioner is off initially. Each customer is characterized by three values: $t_i$ β€” the time (in minutes) when the $i$-th customer visits the restaurant, $l_i$ β€” the lower bound of their preferred temperature range, and $h_i$ β€” the upper bound of their preferred temperature range. A customer is satisfied if the temperature is within the preferred range at the instant they visit the restaurant. Formally, the $i$-th customer is satisfied if and only if the temperature is between $l_i$ and $h_i$ (inclusive) in the $t_i$-th minute. Given the initial temperature, the list of reserved customers' visit times and their preferred temperature ranges, you're going to help him find if it's possible to satisfy all customers. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $q$ ($1 \le q \le 500$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 100$, $-10^9 \le m \le 10^9$), where $n$ is the number of reserved customers and $m$ is the initial temperature of the restaurant. Next, $n$ lines follow. The $i$-th line of them contains three integers $t_i$, $l_i$, and $h_i$ ($1 \le t_i \le 10^9$, $-10^9 \le l_i \le h_i \le 10^9$), where $t_i$ is the time when the $i$-th customer visits, $l_i$ is the lower bound of their preferred temperature range, and $h_i$ is the upper bound of their preferred temperature range. The preferred temperature ranges are inclusive. The customers are given in non-decreasing order of their visit time, and the current time is $0$. -----Output----- For each test case, print "YES" if it is possible to satisfy all customers. Otherwise, print "NO". You can print each letter in any case (upper or lower). -----Example----- Input 4 3 0 5 1 2 7 3 5 10 -1 0 2 12 5 7 10 10 16 20 3 -100 100 0 0 100 -50 50 200 100 100 1 100 99 -100 0 Output YES NO YES NO -----Note----- In the first case, Gildong can control the air conditioner to satisfy all customers in the following way: At $0$-th minute, change the state to heating (the temperature is 0). At $2$-nd minute, change the state to off (the temperature is 2). At $5$-th minute, change the state to heating (the temperature is 2, the $1$-st customer is satisfied). At $6$-th minute, change the state to off (the temperature is 3). At $7$-th minute, change the state to cooling (the temperature is 3, the $2$-nd customer is satisfied). At $10$-th minute, the temperature will be 0, which satisfies the last customer. In the third case, Gildong can change the state to heating at $0$-th minute and leave it be. Then all customers will be satisfied. Note that the $1$-st customer's visit time equals the $2$-nd customer's visit time. In the second and the fourth case, Gildong has to make at least one customer unsatisfied.
q = int(input()) for _ in range(q): n,m = list(map(int, input().split())) customers = [[int(x) for x in input().split()] for _ in range(n)] now_l, now_h = m, m now = 0 for t,l,h in customers: dt = t - now # in area? next_h = min(now_h + dt, h) next_l = max(now_l - dt, l) if not next_l <= next_h: ok = False break now = t now_l, now_h = next_l, next_h else: ok = True print("YES" if ok else "NO")
Gildong owns a bulgogi restaurant. The restaurant has a lot of customers, so many of them like to make a reservation before visiting it. Gildong tries so hard to satisfy the customers that he even memorized all customers' preferred temperature ranges! Looking through the reservation list, he wants to satisfy all customers by controlling the temperature of the restaurant. The restaurant has an air conditioner that has 3 states: off, heating, and cooling. When it's off, the restaurant's temperature remains the same. When it's heating, the temperature increases by 1 in one minute. Lastly, when it's cooling, the temperature decreases by 1 in one minute. Gildong can change the state as many times as he wants, at any integer minutes. The air conditioner is off initially. Each customer is characterized by three values: $t_i$ β€” the time (in minutes) when the $i$-th customer visits the restaurant, $l_i$ β€” the lower bound of their preferred temperature range, and $h_i$ β€” the upper bound of their preferred temperature range. A customer is satisfied if the temperature is within the preferred range at the instant they visit the restaurant. Formally, the $i$-th customer is satisfied if and only if the temperature is between $l_i$ and $h_i$ (inclusive) in the $t_i$-th minute. Given the initial temperature, the list of reserved customers' visit times and their preferred temperature ranges, you're going to help him find if it's possible to satisfy all customers. -----Input----- Each test contains one or more test cases. The first line contains the number of test cases $q$ ($1 \le q \le 500$). Description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 100$, $-10^9 \le m \le 10^9$), where $n$ is the number of reserved customers and $m$ is the initial temperature of the restaurant. Next, $n$ lines follow. The $i$-th line of them contains three integers $t_i$, $l_i$, and $h_i$ ($1 \le t_i \le 10^9$, $-10^9 \le l_i \le h_i \le 10^9$), where $t_i$ is the time when the $i$-th customer visits, $l_i$ is the lower bound of their preferred temperature range, and $h_i$ is the upper bound of their preferred temperature range. The preferred temperature ranges are inclusive. The customers are given in non-decreasing order of their visit time, and the current time is $0$. -----Output----- For each test case, print "YES" if it is possible to satisfy all customers. Otherwise, print "NO". You can print each letter in any case (upper or lower). -----Example----- Input 4 3 0 5 1 2 7 3 5 10 -1 0 2 12 5 7 10 10 16 20 3 -100 100 0 0 100 -50 50 200 100 100 1 100 99 -100 0 Output YES NO YES NO -----Note----- In the first case, Gildong can control the air conditioner to satisfy all customers in the following way: At $0$-th minute, change the state to heating (the temperature is 0). At $2$-nd minute, change the state to off (the temperature is 2). At $5$-th minute, change the state to heating (the temperature is 2, the $1$-st customer is satisfied). At $6$-th minute, change the state to off (the temperature is 3). At $7$-th minute, change the state to cooling (the temperature is 3, the $2$-nd customer is satisfied). At $10$-th minute, the temperature will be 0, which satisfies the last customer. In the third case, Gildong can change the state to heating at $0$-th minute and leave it be. Then all customers will be satisfied. Note that the $1$-st customer's visit time equals the $2$-nd customer's visit time. In the second and the fourth case, Gildong has to make at least one customer unsatisfied.
import sys input = sys.stdin.readline Q = int(input()) Query = [] for _ in range(Q): N, M = map(int, input().split()) TLR = [list(map(int, input().split())) for _ in range(N)] Query.append((N, M, TLR)) for N, M, TLR in Query: TLR.sort() large = M small = M pret = 0 ok = True for t, l, r in TLR: delta = t - pret large += delta small -= delta if large < l or r < small: ok = False break large = min(large, r) small = max(small, l) pret = t print("YES" if ok else "NO")
Among Johnny's numerous hobbies, there are two seemingly harmless ones: applying bitwise operations and sneaking into his dad's office. As it is usually the case with small children, Johnny is unaware that combining these two activities can get him in a lot of trouble. There is a set $S$ containing very important numbers on his dad's desk. The minute Johnny heard about it, he decided that it's a good idea to choose a positive integer $k$ and replace each element $s$ of the set $S$ with $s \oplus k$ ($\oplus$ denotes the exclusive or operation). Help him choose such $k$ that Johnny's dad will not see any difference after his son is done playing (i.e. Johnny will get the same set as before playing). It is possible that no such number exists. It is also possible that there are many of them. In such a case, output the smallest one. Note that the order of elements in a set doesn't matter, i.e. set $\{1, 2, 3\}$ equals to set $\{2, 1, 3\}$. Formally, find the smallest positive integer $k$ such that $\{s \oplus k | s \in S\} = S$ or report that there is no such number. For example, if $S = \{1, 3, 4\}$ and $k = 2$, new set will be equal to $\{3, 1, 6\}$. If $S = \{0, 1, 2, 3\}$ and $k = 1$, after playing set will stay the same. -----Input----- In the first line of input, there is a single integer $t$ ($1 \leq t \leq 1024$), the number of test cases. In the next lines, $t$ test cases follow. Each of them consists of two lines. In the first line there is a single integer $n$ ($1 \leq n \leq 1024$) denoting the number of elements in set $S$. Second line consists of $n$ distinct integers $s_i$ ($0 \leq s_i < 1024$), elements of $S$. It is guaranteed that the sum of $n$ over all test cases will not exceed $1024$. -----Output----- Print $t$ lines; $i$-th line should contain the answer to the $i$-th test case, the minimal positive integer $k$ satisfying the conditions or $-1$ if no such $k$ exists. -----Example----- Input 6 4 1 0 2 3 6 10 7 14 8 3 12 2 0 2 3 1 2 3 6 1 4 6 10 11 12 2 0 1023 Output 1 4 2 -1 -1 1023 -----Note----- In the first test case, the answer is $1$ because it is a minimum positive integer and it satisfies all the conditions.
t = int(input()) for _ in range(t): n = list(input().strip()) s = list(map(int, input().strip().split())) check = set(s) found = False for i in range(1, 1025): newset = set([e^i for e in s]) if check == newset: print(i) found = True break if not found: print(-1)
Among Johnny's numerous hobbies, there are two seemingly harmless ones: applying bitwise operations and sneaking into his dad's office. As it is usually the case with small children, Johnny is unaware that combining these two activities can get him in a lot of trouble. There is a set $S$ containing very important numbers on his dad's desk. The minute Johnny heard about it, he decided that it's a good idea to choose a positive integer $k$ and replace each element $s$ of the set $S$ with $s \oplus k$ ($\oplus$ denotes the exclusive or operation). Help him choose such $k$ that Johnny's dad will not see any difference after his son is done playing (i.e. Johnny will get the same set as before playing). It is possible that no such number exists. It is also possible that there are many of them. In such a case, output the smallest one. Note that the order of elements in a set doesn't matter, i.e. set $\{1, 2, 3\}$ equals to set $\{2, 1, 3\}$. Formally, find the smallest positive integer $k$ such that $\{s \oplus k | s \in S\} = S$ or report that there is no such number. For example, if $S = \{1, 3, 4\}$ and $k = 2$, new set will be equal to $\{3, 1, 6\}$. If $S = \{0, 1, 2, 3\}$ and $k = 1$, after playing set will stay the same. -----Input----- In the first line of input, there is a single integer $t$ ($1 \leq t \leq 1024$), the number of test cases. In the next lines, $t$ test cases follow. Each of them consists of two lines. In the first line there is a single integer $n$ ($1 \leq n \leq 1024$) denoting the number of elements in set $S$. Second line consists of $n$ distinct integers $s_i$ ($0 \leq s_i < 1024$), elements of $S$. It is guaranteed that the sum of $n$ over all test cases will not exceed $1024$. -----Output----- Print $t$ lines; $i$-th line should contain the answer to the $i$-th test case, the minimal positive integer $k$ satisfying the conditions or $-1$ if no such $k$ exists. -----Example----- Input 6 4 1 0 2 3 6 10 7 14 8 3 12 2 0 2 3 1 2 3 6 1 4 6 10 11 12 2 0 1023 Output 1 4 2 -1 -1 1023 -----Note----- In the first test case, the answer is $1$ because it is a minimum positive integer and it satisfies all the conditions.
import sys input = sys.stdin.readline for _ in range(int(input())): N = int(input()) a = list(map(int, input().split())) a.sort() for x in range(1, 1024): b = [0] * N for i in range(N): b[i] = a[i] ^ x b.sort() if a == b: print(x) break else: print(-1)
Among Johnny's numerous hobbies, there are two seemingly harmless ones: applying bitwise operations and sneaking into his dad's office. As it is usually the case with small children, Johnny is unaware that combining these two activities can get him in a lot of trouble. There is a set $S$ containing very important numbers on his dad's desk. The minute Johnny heard about it, he decided that it's a good idea to choose a positive integer $k$ and replace each element $s$ of the set $S$ with $s \oplus k$ ($\oplus$ denotes the exclusive or operation). Help him choose such $k$ that Johnny's dad will not see any difference after his son is done playing (i.e. Johnny will get the same set as before playing). It is possible that no such number exists. It is also possible that there are many of them. In such a case, output the smallest one. Note that the order of elements in a set doesn't matter, i.e. set $\{1, 2, 3\}$ equals to set $\{2, 1, 3\}$. Formally, find the smallest positive integer $k$ such that $\{s \oplus k | s \in S\} = S$ or report that there is no such number. For example, if $S = \{1, 3, 4\}$ and $k = 2$, new set will be equal to $\{3, 1, 6\}$. If $S = \{0, 1, 2, 3\}$ and $k = 1$, after playing set will stay the same. -----Input----- In the first line of input, there is a single integer $t$ ($1 \leq t \leq 1024$), the number of test cases. In the next lines, $t$ test cases follow. Each of them consists of two lines. In the first line there is a single integer $n$ ($1 \leq n \leq 1024$) denoting the number of elements in set $S$. Second line consists of $n$ distinct integers $s_i$ ($0 \leq s_i < 1024$), elements of $S$. It is guaranteed that the sum of $n$ over all test cases will not exceed $1024$. -----Output----- Print $t$ lines; $i$-th line should contain the answer to the $i$-th test case, the minimal positive integer $k$ satisfying the conditions or $-1$ if no such $k$ exists. -----Example----- Input 6 4 1 0 2 3 6 10 7 14 8 3 12 2 0 2 3 1 2 3 6 1 4 6 10 11 12 2 0 1023 Output 1 4 2 -1 -1 1023 -----Note----- In the first test case, the answer is $1$ because it is a minimum positive integer and it satisfies all the conditions.
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n = int(input()) S = set(map(int, input().split())) ok = False for i in range(1, 1024): tmp = {i ^ val for val in S} if tmp == S: print(i) ok = True break if not ok: print(-1)
Among Johnny's numerous hobbies, there are two seemingly harmless ones: applying bitwise operations and sneaking into his dad's office. As it is usually the case with small children, Johnny is unaware that combining these two activities can get him in a lot of trouble. There is a set $S$ containing very important numbers on his dad's desk. The minute Johnny heard about it, he decided that it's a good idea to choose a positive integer $k$ and replace each element $s$ of the set $S$ with $s \oplus k$ ($\oplus$ denotes the exclusive or operation). Help him choose such $k$ that Johnny's dad will not see any difference after his son is done playing (i.e. Johnny will get the same set as before playing). It is possible that no such number exists. It is also possible that there are many of them. In such a case, output the smallest one. Note that the order of elements in a set doesn't matter, i.e. set $\{1, 2, 3\}$ equals to set $\{2, 1, 3\}$. Formally, find the smallest positive integer $k$ such that $\{s \oplus k | s \in S\} = S$ or report that there is no such number. For example, if $S = \{1, 3, 4\}$ and $k = 2$, new set will be equal to $\{3, 1, 6\}$. If $S = \{0, 1, 2, 3\}$ and $k = 1$, after playing set will stay the same. -----Input----- In the first line of input, there is a single integer $t$ ($1 \leq t \leq 1024$), the number of test cases. In the next lines, $t$ test cases follow. Each of them consists of two lines. In the first line there is a single integer $n$ ($1 \leq n \leq 1024$) denoting the number of elements in set $S$. Second line consists of $n$ distinct integers $s_i$ ($0 \leq s_i < 1024$), elements of $S$. It is guaranteed that the sum of $n$ over all test cases will not exceed $1024$. -----Output----- Print $t$ lines; $i$-th line should contain the answer to the $i$-th test case, the minimal positive integer $k$ satisfying the conditions or $-1$ if no such $k$ exists. -----Example----- Input 6 4 1 0 2 3 6 10 7 14 8 3 12 2 0 2 3 1 2 3 6 1 4 6 10 11 12 2 0 1023 Output 1 4 2 -1 -1 1023 -----Note----- In the first test case, the answer is $1$ because it is a minimum positive integer and it satisfies all the conditions.
for _ in range(int(input())): n=int(input()) s=list(map(int,input().split())) ans=-1 for k in range(1,1025): st=set(s) for i in s: val=i^k if val not in st: break st.remove(val) if not st: ans=k break print(ans)
Among Johnny's numerous hobbies, there are two seemingly harmless ones: applying bitwise operations and sneaking into his dad's office. As it is usually the case with small children, Johnny is unaware that combining these two activities can get him in a lot of trouble. There is a set $S$ containing very important numbers on his dad's desk. The minute Johnny heard about it, he decided that it's a good idea to choose a positive integer $k$ and replace each element $s$ of the set $S$ with $s \oplus k$ ($\oplus$ denotes the exclusive or operation). Help him choose such $k$ that Johnny's dad will not see any difference after his son is done playing (i.e. Johnny will get the same set as before playing). It is possible that no such number exists. It is also possible that there are many of them. In such a case, output the smallest one. Note that the order of elements in a set doesn't matter, i.e. set $\{1, 2, 3\}$ equals to set $\{2, 1, 3\}$. Formally, find the smallest positive integer $k$ such that $\{s \oplus k | s \in S\} = S$ or report that there is no such number. For example, if $S = \{1, 3, 4\}$ and $k = 2$, new set will be equal to $\{3, 1, 6\}$. If $S = \{0, 1, 2, 3\}$ and $k = 1$, after playing set will stay the same. -----Input----- In the first line of input, there is a single integer $t$ ($1 \leq t \leq 1024$), the number of test cases. In the next lines, $t$ test cases follow. Each of them consists of two lines. In the first line there is a single integer $n$ ($1 \leq n \leq 1024$) denoting the number of elements in set $S$. Second line consists of $n$ distinct integers $s_i$ ($0 \leq s_i < 1024$), elements of $S$. It is guaranteed that the sum of $n$ over all test cases will not exceed $1024$. -----Output----- Print $t$ lines; $i$-th line should contain the answer to the $i$-th test case, the minimal positive integer $k$ satisfying the conditions or $-1$ if no such $k$ exists. -----Example----- Input 6 4 1 0 2 3 6 10 7 14 8 3 12 2 0 2 3 1 2 3 6 1 4 6 10 11 12 2 0 1023 Output 1 4 2 -1 -1 1023 -----Note----- In the first test case, the answer is $1$ because it is a minimum positive integer and it satisfies all the conditions.
# from decorators import * from sys import stdin, stdout from math import ceil # @debug def solve(n,s): s.sort() for i in range(1,1025): b = [] for j in range(n): b.append(s[j]^i) b.sort() flag = True for j in range(n): if s[j]!=b[j]: flag = False break if flag: print(i) return True print(-1) for _ in range(int(input())): n = int(stdin.readline()) s = list(map(int,stdin.readline().split())) solve(n,s)
Among Johnny's numerous hobbies, there are two seemingly harmless ones: applying bitwise operations and sneaking into his dad's office. As it is usually the case with small children, Johnny is unaware that combining these two activities can get him in a lot of trouble. There is a set $S$ containing very important numbers on his dad's desk. The minute Johnny heard about it, he decided that it's a good idea to choose a positive integer $k$ and replace each element $s$ of the set $S$ with $s \oplus k$ ($\oplus$ denotes the exclusive or operation). Help him choose such $k$ that Johnny's dad will not see any difference after his son is done playing (i.e. Johnny will get the same set as before playing). It is possible that no such number exists. It is also possible that there are many of them. In such a case, output the smallest one. Note that the order of elements in a set doesn't matter, i.e. set $\{1, 2, 3\}$ equals to set $\{2, 1, 3\}$. Formally, find the smallest positive integer $k$ such that $\{s \oplus k | s \in S\} = S$ or report that there is no such number. For example, if $S = \{1, 3, 4\}$ and $k = 2$, new set will be equal to $\{3, 1, 6\}$. If $S = \{0, 1, 2, 3\}$ and $k = 1$, after playing set will stay the same. -----Input----- In the first line of input, there is a single integer $t$ ($1 \leq t \leq 1024$), the number of test cases. In the next lines, $t$ test cases follow. Each of them consists of two lines. In the first line there is a single integer $n$ ($1 \leq n \leq 1024$) denoting the number of elements in set $S$. Second line consists of $n$ distinct integers $s_i$ ($0 \leq s_i < 1024$), elements of $S$. It is guaranteed that the sum of $n$ over all test cases will not exceed $1024$. -----Output----- Print $t$ lines; $i$-th line should contain the answer to the $i$-th test case, the minimal positive integer $k$ satisfying the conditions or $-1$ if no such $k$ exists. -----Example----- Input 6 4 1 0 2 3 6 10 7 14 8 3 12 2 0 2 3 1 2 3 6 1 4 6 10 11 12 2 0 1023 Output 1 4 2 -1 -1 1023 -----Note----- In the first test case, the answer is $1$ because it is a minimum positive integer and it satisfies all the conditions.
t = int(input()) for k in range(t): n = int(input()) a = set(map(int, input().split())) for x in range(1, 1025): if set(x ^ q for q in a) == a: print(x) break else: print(-1)
Among Johnny's numerous hobbies, there are two seemingly harmless ones: applying bitwise operations and sneaking into his dad's office. As it is usually the case with small children, Johnny is unaware that combining these two activities can get him in a lot of trouble. There is a set $S$ containing very important numbers on his dad's desk. The minute Johnny heard about it, he decided that it's a good idea to choose a positive integer $k$ and replace each element $s$ of the set $S$ with $s \oplus k$ ($\oplus$ denotes the exclusive or operation). Help him choose such $k$ that Johnny's dad will not see any difference after his son is done playing (i.e. Johnny will get the same set as before playing). It is possible that no such number exists. It is also possible that there are many of them. In such a case, output the smallest one. Note that the order of elements in a set doesn't matter, i.e. set $\{1, 2, 3\}$ equals to set $\{2, 1, 3\}$. Formally, find the smallest positive integer $k$ such that $\{s \oplus k | s \in S\} = S$ or report that there is no such number. For example, if $S = \{1, 3, 4\}$ and $k = 2$, new set will be equal to $\{3, 1, 6\}$. If $S = \{0, 1, 2, 3\}$ and $k = 1$, after playing set will stay the same. -----Input----- In the first line of input, there is a single integer $t$ ($1 \leq t \leq 1024$), the number of test cases. In the next lines, $t$ test cases follow. Each of them consists of two lines. In the first line there is a single integer $n$ ($1 \leq n \leq 1024$) denoting the number of elements in set $S$. Second line consists of $n$ distinct integers $s_i$ ($0 \leq s_i < 1024$), elements of $S$. It is guaranteed that the sum of $n$ over all test cases will not exceed $1024$. -----Output----- Print $t$ lines; $i$-th line should contain the answer to the $i$-th test case, the minimal positive integer $k$ satisfying the conditions or $-1$ if no such $k$ exists. -----Example----- Input 6 4 1 0 2 3 6 10 7 14 8 3 12 2 0 2 3 1 2 3 6 1 4 6 10 11 12 2 0 1023 Output 1 4 2 -1 -1 1023 -----Note----- In the first test case, the answer is $1$ because it is a minimum positive integer and it satisfies all the conditions.
import sys ints = (int(x) for x in sys.stdin.read().split()) sys.setrecursionlimit(3000) def main(): ntc = next(ints) for tc in range(1,ntc+1): n = next(ints) s = [next(ints) for i in range(n)] P = None for x in s: p = set(x^y for y in s) if P==None: P = p else: P &= p ans = next(iter(sorted(P)[1:]), -1) print(ans) return main()
Among Johnny's numerous hobbies, there are two seemingly harmless ones: applying bitwise operations and sneaking into his dad's office. As it is usually the case with small children, Johnny is unaware that combining these two activities can get him in a lot of trouble. There is a set $S$ containing very important numbers on his dad's desk. The minute Johnny heard about it, he decided that it's a good idea to choose a positive integer $k$ and replace each element $s$ of the set $S$ with $s \oplus k$ ($\oplus$ denotes the exclusive or operation). Help him choose such $k$ that Johnny's dad will not see any difference after his son is done playing (i.e. Johnny will get the same set as before playing). It is possible that no such number exists. It is also possible that there are many of them. In such a case, output the smallest one. Note that the order of elements in a set doesn't matter, i.e. set $\{1, 2, 3\}$ equals to set $\{2, 1, 3\}$. Formally, find the smallest positive integer $k$ such that $\{s \oplus k | s \in S\} = S$ or report that there is no such number. For example, if $S = \{1, 3, 4\}$ and $k = 2$, new set will be equal to $\{3, 1, 6\}$. If $S = \{0, 1, 2, 3\}$ and $k = 1$, after playing set will stay the same. -----Input----- In the first line of input, there is a single integer $t$ ($1 \leq t \leq 1024$), the number of test cases. In the next lines, $t$ test cases follow. Each of them consists of two lines. In the first line there is a single integer $n$ ($1 \leq n \leq 1024$) denoting the number of elements in set $S$. Second line consists of $n$ distinct integers $s_i$ ($0 \leq s_i < 1024$), elements of $S$. It is guaranteed that the sum of $n$ over all test cases will not exceed $1024$. -----Output----- Print $t$ lines; $i$-th line should contain the answer to the $i$-th test case, the minimal positive integer $k$ satisfying the conditions or $-1$ if no such $k$ exists. -----Example----- Input 6 4 1 0 2 3 6 10 7 14 8 3 12 2 0 2 3 1 2 3 6 1 4 6 10 11 12 2 0 1023 Output 1 4 2 -1 -1 1023 -----Note----- In the first test case, the answer is $1$ because it is a minimum positive integer and it satisfies all the conditions.
T = int(input()) for t in range(T): n = int(input()) S = [int(_) for _ in input().split()] setS = set(S) for k in range(1, 1025): for el in setS: if el ^ k not in setS: break else: print(k) break else: print(-1)
Among Johnny's numerous hobbies, there are two seemingly harmless ones: applying bitwise operations and sneaking into his dad's office. As it is usually the case with small children, Johnny is unaware that combining these two activities can get him in a lot of trouble. There is a set $S$ containing very important numbers on his dad's desk. The minute Johnny heard about it, he decided that it's a good idea to choose a positive integer $k$ and replace each element $s$ of the set $S$ with $s \oplus k$ ($\oplus$ denotes the exclusive or operation). Help him choose such $k$ that Johnny's dad will not see any difference after his son is done playing (i.e. Johnny will get the same set as before playing). It is possible that no such number exists. It is also possible that there are many of them. In such a case, output the smallest one. Note that the order of elements in a set doesn't matter, i.e. set $\{1, 2, 3\}$ equals to set $\{2, 1, 3\}$. Formally, find the smallest positive integer $k$ such that $\{s \oplus k | s \in S\} = S$ or report that there is no such number. For example, if $S = \{1, 3, 4\}$ and $k = 2$, new set will be equal to $\{3, 1, 6\}$. If $S = \{0, 1, 2, 3\}$ and $k = 1$, after playing set will stay the same. -----Input----- In the first line of input, there is a single integer $t$ ($1 \leq t \leq 1024$), the number of test cases. In the next lines, $t$ test cases follow. Each of them consists of two lines. In the first line there is a single integer $n$ ($1 \leq n \leq 1024$) denoting the number of elements in set $S$. Second line consists of $n$ distinct integers $s_i$ ($0 \leq s_i < 1024$), elements of $S$. It is guaranteed that the sum of $n$ over all test cases will not exceed $1024$. -----Output----- Print $t$ lines; $i$-th line should contain the answer to the $i$-th test case, the minimal positive integer $k$ satisfying the conditions or $-1$ if no such $k$ exists. -----Example----- Input 6 4 1 0 2 3 6 10 7 14 8 3 12 2 0 2 3 1 2 3 6 1 4 6 10 11 12 2 0 1023 Output 1 4 2 -1 -1 1023 -----Note----- In the first test case, the answer is $1$ because it is a minimum positive integer and it satisfies all the conditions.
for _ in range(int(input())): n = int(input()) ans = -1 l = set(map(int,input().split())) for i in range(1,4 * (10 ** 3)): s1 = set() for j in l: s1.add(i ^ j) if(s1 == l): ans = i break print(ans)
Among Johnny's numerous hobbies, there are two seemingly harmless ones: applying bitwise operations and sneaking into his dad's office. As it is usually the case with small children, Johnny is unaware that combining these two activities can get him in a lot of trouble. There is a set $S$ containing very important numbers on his dad's desk. The minute Johnny heard about it, he decided that it's a good idea to choose a positive integer $k$ and replace each element $s$ of the set $S$ with $s \oplus k$ ($\oplus$ denotes the exclusive or operation). Help him choose such $k$ that Johnny's dad will not see any difference after his son is done playing (i.e. Johnny will get the same set as before playing). It is possible that no such number exists. It is also possible that there are many of them. In such a case, output the smallest one. Note that the order of elements in a set doesn't matter, i.e. set $\{1, 2, 3\}$ equals to set $\{2, 1, 3\}$. Formally, find the smallest positive integer $k$ such that $\{s \oplus k | s \in S\} = S$ or report that there is no such number. For example, if $S = \{1, 3, 4\}$ and $k = 2$, new set will be equal to $\{3, 1, 6\}$. If $S = \{0, 1, 2, 3\}$ and $k = 1$, after playing set will stay the same. -----Input----- In the first line of input, there is a single integer $t$ ($1 \leq t \leq 1024$), the number of test cases. In the next lines, $t$ test cases follow. Each of them consists of two lines. In the first line there is a single integer $n$ ($1 \leq n \leq 1024$) denoting the number of elements in set $S$. Second line consists of $n$ distinct integers $s_i$ ($0 \leq s_i < 1024$), elements of $S$. It is guaranteed that the sum of $n$ over all test cases will not exceed $1024$. -----Output----- Print $t$ lines; $i$-th line should contain the answer to the $i$-th test case, the minimal positive integer $k$ satisfying the conditions or $-1$ if no such $k$ exists. -----Example----- Input 6 4 1 0 2 3 6 10 7 14 8 3 12 2 0 2 3 1 2 3 6 1 4 6 10 11 12 2 0 1023 Output 1 4 2 -1 -1 1023 -----Note----- In the first test case, the answer is $1$ because it is a minimum positive integer and it satisfies all the conditions.
import sys def arr(): return list(map(int,input().split())) input=sys.stdin.readline from collections import defaultdict import math for _ in range(int(input())): N=int(input()) L=arr() L.sort() flag=False for i in range(1,1025): x=L[::] for j in range(N): x[j]=x[j]^i x.sort() x.sort() if x==L: ans=i flag=True break if flag: print(ans) else: print(-1)
Let's define the following recurrence: $$a_{n+1} = a_{n} + minDigit(a_{n}) \cdot maxDigit(a_{n}).$$ Here $minDigit(x)$ and $maxDigit(x)$ are the minimal and maximal digits in the decimal representation of $x$ without leading zeroes. For examples refer to notes. Your task is calculate $a_{K}$ for given $a_{1}$ and $K$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of independent test cases. Each test case consists of a single line containing two integers $a_{1}$ and $K$ ($1 \le a_{1} \le 10^{18}$, $1 \le K \le 10^{16}$) separated by a space. -----Output----- For each test case print one integer $a_{K}$ on a separate line. -----Example----- Input 8 1 4 487 1 487 2 487 3 487 4 487 5 487 6 487 7 Output 42 487 519 528 544 564 588 628 -----Note----- $a_{1} = 487$ $a_{2} = a_{1} + minDigit(a_{1}) \cdot maxDigit(a_{1}) = 487 + \min (4, 8, 7) \cdot \max (4, 8, 7) = 487 + 4 \cdot 8 = 519$ $a_{3} = a_{2} + minDigit(a_{2}) \cdot maxDigit(a_{2}) = 519 + \min (5, 1, 9) \cdot \max (5, 1, 9) = 519 + 1 \cdot 9 = 528$ $a_{4} = a_{3} + minDigit(a_{3}) \cdot maxDigit(a_{3}) = 528 + \min (5, 2, 8) \cdot \max (5, 2, 8) = 528 + 2 \cdot 8 = 544$ $a_{5} = a_{4} + minDigit(a_{4}) \cdot maxDigit(a_{4}) = 544 + \min (5, 4, 4) \cdot \max (5, 4, 4) = 544 + 4 \cdot 5 = 564$ $a_{6} = a_{5} + minDigit(a_{5}) \cdot maxDigit(a_{5}) = 564 + \min (5, 6, 4) \cdot \max (5, 6, 4) = 564 + 4 \cdot 6 = 588$ $a_{7} = a_{6} + minDigit(a_{6}) \cdot maxDigit(a_{6}) = 588 + \min (5, 8, 8) \cdot \max (5, 8, 8) = 588 + 5 \cdot 8 = 628$
import sys input = sys.stdin.readline for _ in range(int(input())): a, k = list(map(int, input().split())) for _ in range(k - 1): if '0' in str(a): break a += int(min(list(str(a)))) * int(max(list(str(a)))) print(a)
Let's define the following recurrence: $$a_{n+1} = a_{n} + minDigit(a_{n}) \cdot maxDigit(a_{n}).$$ Here $minDigit(x)$ and $maxDigit(x)$ are the minimal and maximal digits in the decimal representation of $x$ without leading zeroes. For examples refer to notes. Your task is calculate $a_{K}$ for given $a_{1}$ and $K$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of independent test cases. Each test case consists of a single line containing two integers $a_{1}$ and $K$ ($1 \le a_{1} \le 10^{18}$, $1 \le K \le 10^{16}$) separated by a space. -----Output----- For each test case print one integer $a_{K}$ on a separate line. -----Example----- Input 8 1 4 487 1 487 2 487 3 487 4 487 5 487 6 487 7 Output 42 487 519 528 544 564 588 628 -----Note----- $a_{1} = 487$ $a_{2} = a_{1} + minDigit(a_{1}) \cdot maxDigit(a_{1}) = 487 + \min (4, 8, 7) \cdot \max (4, 8, 7) = 487 + 4 \cdot 8 = 519$ $a_{3} = a_{2} + minDigit(a_{2}) \cdot maxDigit(a_{2}) = 519 + \min (5, 1, 9) \cdot \max (5, 1, 9) = 519 + 1 \cdot 9 = 528$ $a_{4} = a_{3} + minDigit(a_{3}) \cdot maxDigit(a_{3}) = 528 + \min (5, 2, 8) \cdot \max (5, 2, 8) = 528 + 2 \cdot 8 = 544$ $a_{5} = a_{4} + minDigit(a_{4}) \cdot maxDigit(a_{4}) = 544 + \min (5, 4, 4) \cdot \max (5, 4, 4) = 544 + 4 \cdot 5 = 564$ $a_{6} = a_{5} + minDigit(a_{5}) \cdot maxDigit(a_{5}) = 564 + \min (5, 6, 4) \cdot \max (5, 6, 4) = 564 + 4 \cdot 6 = 588$ $a_{7} = a_{6} + minDigit(a_{6}) \cdot maxDigit(a_{6}) = 588 + \min (5, 8, 8) \cdot \max (5, 8, 8) = 588 + 5 \cdot 8 = 628$
for _ in range(int(input())): n,k = map(int,input().split()) for i in range(k-1): n = str(n) if ("0" in n): break n = int(n) + int(min(n))*int(max(n)) print(n)
Let's define the following recurrence: $$a_{n+1} = a_{n} + minDigit(a_{n}) \cdot maxDigit(a_{n}).$$ Here $minDigit(x)$ and $maxDigit(x)$ are the minimal and maximal digits in the decimal representation of $x$ without leading zeroes. For examples refer to notes. Your task is calculate $a_{K}$ for given $a_{1}$ and $K$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of independent test cases. Each test case consists of a single line containing two integers $a_{1}$ and $K$ ($1 \le a_{1} \le 10^{18}$, $1 \le K \le 10^{16}$) separated by a space. -----Output----- For each test case print one integer $a_{K}$ on a separate line. -----Example----- Input 8 1 4 487 1 487 2 487 3 487 4 487 5 487 6 487 7 Output 42 487 519 528 544 564 588 628 -----Note----- $a_{1} = 487$ $a_{2} = a_{1} + minDigit(a_{1}) \cdot maxDigit(a_{1}) = 487 + \min (4, 8, 7) \cdot \max (4, 8, 7) = 487 + 4 \cdot 8 = 519$ $a_{3} = a_{2} + minDigit(a_{2}) \cdot maxDigit(a_{2}) = 519 + \min (5, 1, 9) \cdot \max (5, 1, 9) = 519 + 1 \cdot 9 = 528$ $a_{4} = a_{3} + minDigit(a_{3}) \cdot maxDigit(a_{3}) = 528 + \min (5, 2, 8) \cdot \max (5, 2, 8) = 528 + 2 \cdot 8 = 544$ $a_{5} = a_{4} + minDigit(a_{4}) \cdot maxDigit(a_{4}) = 544 + \min (5, 4, 4) \cdot \max (5, 4, 4) = 544 + 4 \cdot 5 = 564$ $a_{6} = a_{5} + minDigit(a_{5}) \cdot maxDigit(a_{5}) = 564 + \min (5, 6, 4) \cdot \max (5, 6, 4) = 564 + 4 \cdot 6 = 588$ $a_{7} = a_{6} + minDigit(a_{6}) \cdot maxDigit(a_{6}) = 588 + \min (5, 8, 8) \cdot \max (5, 8, 8) = 588 + 5 \cdot 8 = 628$
import sys INF = 10**20 MOD = 10**9 + 7 I = lambda:list(map(int,input().split())) from math import gcd from math import ceil from collections import defaultdict as dd, Counter from bisect import bisect_left as bl, bisect_right as br t, = I() while t: t -= 1 a, k = I() b = str(a) s = [] while b not in s: s.append(b) b = str(int(b) + int(min(b)) * int(max(b))) if k >= len(s): print(s[-1]) else: print(s[k - 1])
Let's define the following recurrence: $$a_{n+1} = a_{n} + minDigit(a_{n}) \cdot maxDigit(a_{n}).$$ Here $minDigit(x)$ and $maxDigit(x)$ are the minimal and maximal digits in the decimal representation of $x$ without leading zeroes. For examples refer to notes. Your task is calculate $a_{K}$ for given $a_{1}$ and $K$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of independent test cases. Each test case consists of a single line containing two integers $a_{1}$ and $K$ ($1 \le a_{1} \le 10^{18}$, $1 \le K \le 10^{16}$) separated by a space. -----Output----- For each test case print one integer $a_{K}$ on a separate line. -----Example----- Input 8 1 4 487 1 487 2 487 3 487 4 487 5 487 6 487 7 Output 42 487 519 528 544 564 588 628 -----Note----- $a_{1} = 487$ $a_{2} = a_{1} + minDigit(a_{1}) \cdot maxDigit(a_{1}) = 487 + \min (4, 8, 7) \cdot \max (4, 8, 7) = 487 + 4 \cdot 8 = 519$ $a_{3} = a_{2} + minDigit(a_{2}) \cdot maxDigit(a_{2}) = 519 + \min (5, 1, 9) \cdot \max (5, 1, 9) = 519 + 1 \cdot 9 = 528$ $a_{4} = a_{3} + minDigit(a_{3}) \cdot maxDigit(a_{3}) = 528 + \min (5, 2, 8) \cdot \max (5, 2, 8) = 528 + 2 \cdot 8 = 544$ $a_{5} = a_{4} + minDigit(a_{4}) \cdot maxDigit(a_{4}) = 544 + \min (5, 4, 4) \cdot \max (5, 4, 4) = 544 + 4 \cdot 5 = 564$ $a_{6} = a_{5} + minDigit(a_{5}) \cdot maxDigit(a_{5}) = 564 + \min (5, 6, 4) \cdot \max (5, 6, 4) = 564 + 4 \cdot 6 = 588$ $a_{7} = a_{6} + minDigit(a_{6}) \cdot maxDigit(a_{6}) = 588 + \min (5, 8, 8) \cdot \max (5, 8, 8) = 588 + 5 \cdot 8 = 628$
t = int(input()) for i in range(t): a, b = list(map(int, input().split())) last = -1 b -= 1 while last != a and b: b -= 1 last = a aa = a maks = 0 mini = 10 while aa: mini = min(mini, aa%10) maks = max(maks, aa%10) aa = aa//10 a += mini*maks print(a)
Let's define the following recurrence: $$a_{n+1} = a_{n} + minDigit(a_{n}) \cdot maxDigit(a_{n}).$$ Here $minDigit(x)$ and $maxDigit(x)$ are the minimal and maximal digits in the decimal representation of $x$ without leading zeroes. For examples refer to notes. Your task is calculate $a_{K}$ for given $a_{1}$ and $K$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of independent test cases. Each test case consists of a single line containing two integers $a_{1}$ and $K$ ($1 \le a_{1} \le 10^{18}$, $1 \le K \le 10^{16}$) separated by a space. -----Output----- For each test case print one integer $a_{K}$ on a separate line. -----Example----- Input 8 1 4 487 1 487 2 487 3 487 4 487 5 487 6 487 7 Output 42 487 519 528 544 564 588 628 -----Note----- $a_{1} = 487$ $a_{2} = a_{1} + minDigit(a_{1}) \cdot maxDigit(a_{1}) = 487 + \min (4, 8, 7) \cdot \max (4, 8, 7) = 487 + 4 \cdot 8 = 519$ $a_{3} = a_{2} + minDigit(a_{2}) \cdot maxDigit(a_{2}) = 519 + \min (5, 1, 9) \cdot \max (5, 1, 9) = 519 + 1 \cdot 9 = 528$ $a_{4} = a_{3} + minDigit(a_{3}) \cdot maxDigit(a_{3}) = 528 + \min (5, 2, 8) \cdot \max (5, 2, 8) = 528 + 2 \cdot 8 = 544$ $a_{5} = a_{4} + minDigit(a_{4}) \cdot maxDigit(a_{4}) = 544 + \min (5, 4, 4) \cdot \max (5, 4, 4) = 544 + 4 \cdot 5 = 564$ $a_{6} = a_{5} + minDigit(a_{5}) \cdot maxDigit(a_{5}) = 564 + \min (5, 6, 4) \cdot \max (5, 6, 4) = 564 + 4 \cdot 6 = 588$ $a_{7} = a_{6} + minDigit(a_{6}) \cdot maxDigit(a_{6}) = 588 + \min (5, 8, 8) \cdot \max (5, 8, 8) = 588 + 5 \cdot 8 = 628$
def read_int(): return int(input()) def read_ints(): return list(map(int, input().split(' '))) t = read_int() for case_num in range(t): a, k = read_ints() i = 1 while i < k: s = str(a) lo = int(min(s)) hi = int(max(s)) if lo == 0: break a += lo * hi i += 1 print(a)
Let's define the following recurrence: $$a_{n+1} = a_{n} + minDigit(a_{n}) \cdot maxDigit(a_{n}).$$ Here $minDigit(x)$ and $maxDigit(x)$ are the minimal and maximal digits in the decimal representation of $x$ without leading zeroes. For examples refer to notes. Your task is calculate $a_{K}$ for given $a_{1}$ and $K$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of independent test cases. Each test case consists of a single line containing two integers $a_{1}$ and $K$ ($1 \le a_{1} \le 10^{18}$, $1 \le K \le 10^{16}$) separated by a space. -----Output----- For each test case print one integer $a_{K}$ on a separate line. -----Example----- Input 8 1 4 487 1 487 2 487 3 487 4 487 5 487 6 487 7 Output 42 487 519 528 544 564 588 628 -----Note----- $a_{1} = 487$ $a_{2} = a_{1} + minDigit(a_{1}) \cdot maxDigit(a_{1}) = 487 + \min (4, 8, 7) \cdot \max (4, 8, 7) = 487 + 4 \cdot 8 = 519$ $a_{3} = a_{2} + minDigit(a_{2}) \cdot maxDigit(a_{2}) = 519 + \min (5, 1, 9) \cdot \max (5, 1, 9) = 519 + 1 \cdot 9 = 528$ $a_{4} = a_{3} + minDigit(a_{3}) \cdot maxDigit(a_{3}) = 528 + \min (5, 2, 8) \cdot \max (5, 2, 8) = 528 + 2 \cdot 8 = 544$ $a_{5} = a_{4} + minDigit(a_{4}) \cdot maxDigit(a_{4}) = 544 + \min (5, 4, 4) \cdot \max (5, 4, 4) = 544 + 4 \cdot 5 = 564$ $a_{6} = a_{5} + minDigit(a_{5}) \cdot maxDigit(a_{5}) = 564 + \min (5, 6, 4) \cdot \max (5, 6, 4) = 564 + 4 \cdot 6 = 588$ $a_{7} = a_{6} + minDigit(a_{6}) \cdot maxDigit(a_{6}) = 588 + \min (5, 8, 8) \cdot \max (5, 8, 8) = 588 + 5 \cdot 8 = 628$
t=int(input()) for _ in range(t): N,K=map(int,input().split()) while(K>1): x=list(str(N)) if('0' in x): break x=[int(i) for i in x] N=N+min(x)*max(x) K-=1 print(N)
Let's define the following recurrence: $$a_{n+1} = a_{n} + minDigit(a_{n}) \cdot maxDigit(a_{n}).$$ Here $minDigit(x)$ and $maxDigit(x)$ are the minimal and maximal digits in the decimal representation of $x$ without leading zeroes. For examples refer to notes. Your task is calculate $a_{K}$ for given $a_{1}$ and $K$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of independent test cases. Each test case consists of a single line containing two integers $a_{1}$ and $K$ ($1 \le a_{1} \le 10^{18}$, $1 \le K \le 10^{16}$) separated by a space. -----Output----- For each test case print one integer $a_{K}$ on a separate line. -----Example----- Input 8 1 4 487 1 487 2 487 3 487 4 487 5 487 6 487 7 Output 42 487 519 528 544 564 588 628 -----Note----- $a_{1} = 487$ $a_{2} = a_{1} + minDigit(a_{1}) \cdot maxDigit(a_{1}) = 487 + \min (4, 8, 7) \cdot \max (4, 8, 7) = 487 + 4 \cdot 8 = 519$ $a_{3} = a_{2} + minDigit(a_{2}) \cdot maxDigit(a_{2}) = 519 + \min (5, 1, 9) \cdot \max (5, 1, 9) = 519 + 1 \cdot 9 = 528$ $a_{4} = a_{3} + minDigit(a_{3}) \cdot maxDigit(a_{3}) = 528 + \min (5, 2, 8) \cdot \max (5, 2, 8) = 528 + 2 \cdot 8 = 544$ $a_{5} = a_{4} + minDigit(a_{4}) \cdot maxDigit(a_{4}) = 544 + \min (5, 4, 4) \cdot \max (5, 4, 4) = 544 + 4 \cdot 5 = 564$ $a_{6} = a_{5} + minDigit(a_{5}) \cdot maxDigit(a_{5}) = 564 + \min (5, 6, 4) \cdot \max (5, 6, 4) = 564 + 4 \cdot 6 = 588$ $a_{7} = a_{6} + minDigit(a_{6}) \cdot maxDigit(a_{6}) = 588 + \min (5, 8, 8) \cdot \max (5, 8, 8) = 588 + 5 \cdot 8 = 628$
def maxd(val) : mx = 0 while val > 0 : mx = max(mx, val % 10) val //= 10 return mx def mind(val) : mn = 9 while val > 0 : mn = min(mn, val % 10) val //= 10 return mn t = int(input()) while t > 0: t -= 1 a, k = map(int, input().split()) k -= 1 while k > 0 and mind(a) > 0 : a = a + mind(a) * maxd(a) k -= 1 print(a)
Let's define the following recurrence: $$a_{n+1} = a_{n} + minDigit(a_{n}) \cdot maxDigit(a_{n}).$$ Here $minDigit(x)$ and $maxDigit(x)$ are the minimal and maximal digits in the decimal representation of $x$ without leading zeroes. For examples refer to notes. Your task is calculate $a_{K}$ for given $a_{1}$ and $K$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of independent test cases. Each test case consists of a single line containing two integers $a_{1}$ and $K$ ($1 \le a_{1} \le 10^{18}$, $1 \le K \le 10^{16}$) separated by a space. -----Output----- For each test case print one integer $a_{K}$ on a separate line. -----Example----- Input 8 1 4 487 1 487 2 487 3 487 4 487 5 487 6 487 7 Output 42 487 519 528 544 564 588 628 -----Note----- $a_{1} = 487$ $a_{2} = a_{1} + minDigit(a_{1}) \cdot maxDigit(a_{1}) = 487 + \min (4, 8, 7) \cdot \max (4, 8, 7) = 487 + 4 \cdot 8 = 519$ $a_{3} = a_{2} + minDigit(a_{2}) \cdot maxDigit(a_{2}) = 519 + \min (5, 1, 9) \cdot \max (5, 1, 9) = 519 + 1 \cdot 9 = 528$ $a_{4} = a_{3} + minDigit(a_{3}) \cdot maxDigit(a_{3}) = 528 + \min (5, 2, 8) \cdot \max (5, 2, 8) = 528 + 2 \cdot 8 = 544$ $a_{5} = a_{4} + minDigit(a_{4}) \cdot maxDigit(a_{4}) = 544 + \min (5, 4, 4) \cdot \max (5, 4, 4) = 544 + 4 \cdot 5 = 564$ $a_{6} = a_{5} + minDigit(a_{5}) \cdot maxDigit(a_{5}) = 564 + \min (5, 6, 4) \cdot \max (5, 6, 4) = 564 + 4 \cdot 6 = 588$ $a_{7} = a_{6} + minDigit(a_{6}) \cdot maxDigit(a_{6}) = 588 + \min (5, 8, 8) \cdot \max (5, 8, 8) = 588 + 5 \cdot 8 = 628$
T = int(input()) n = [0]*T for t in range(T): # n = int(input()) n,k = [int(i) for i in input().split(' ')] n1 = str(n) while n1.count('0') <1 and k>1: n += int(min(n1))*int(max(n1)) n1 = str(n) k-=1 print(n1)
Let's define the following recurrence: $$a_{n+1} = a_{n} + minDigit(a_{n}) \cdot maxDigit(a_{n}).$$ Here $minDigit(x)$ and $maxDigit(x)$ are the minimal and maximal digits in the decimal representation of $x$ without leading zeroes. For examples refer to notes. Your task is calculate $a_{K}$ for given $a_{1}$ and $K$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of independent test cases. Each test case consists of a single line containing two integers $a_{1}$ and $K$ ($1 \le a_{1} \le 10^{18}$, $1 \le K \le 10^{16}$) separated by a space. -----Output----- For each test case print one integer $a_{K}$ on a separate line. -----Example----- Input 8 1 4 487 1 487 2 487 3 487 4 487 5 487 6 487 7 Output 42 487 519 528 544 564 588 628 -----Note----- $a_{1} = 487$ $a_{2} = a_{1} + minDigit(a_{1}) \cdot maxDigit(a_{1}) = 487 + \min (4, 8, 7) \cdot \max (4, 8, 7) = 487 + 4 \cdot 8 = 519$ $a_{3} = a_{2} + minDigit(a_{2}) \cdot maxDigit(a_{2}) = 519 + \min (5, 1, 9) \cdot \max (5, 1, 9) = 519 + 1 \cdot 9 = 528$ $a_{4} = a_{3} + minDigit(a_{3}) \cdot maxDigit(a_{3}) = 528 + \min (5, 2, 8) \cdot \max (5, 2, 8) = 528 + 2 \cdot 8 = 544$ $a_{5} = a_{4} + minDigit(a_{4}) \cdot maxDigit(a_{4}) = 544 + \min (5, 4, 4) \cdot \max (5, 4, 4) = 544 + 4 \cdot 5 = 564$ $a_{6} = a_{5} + minDigit(a_{5}) \cdot maxDigit(a_{5}) = 564 + \min (5, 6, 4) \cdot \max (5, 6, 4) = 564 + 4 \cdot 6 = 588$ $a_{7} = a_{6} + minDigit(a_{6}) \cdot maxDigit(a_{6}) = 588 + \min (5, 8, 8) \cdot \max (5, 8, 8) = 588 + 5 \cdot 8 = 628$
for _ in range(int(input())): n,k = map(int,input().split()) for i in range(k-1): n = str(n) if ("0" in n): break n = int(n)+int(min(n))*int(max(n)) print(n)
Let's define the following recurrence: $$a_{n+1} = a_{n} + minDigit(a_{n}) \cdot maxDigit(a_{n}).$$ Here $minDigit(x)$ and $maxDigit(x)$ are the minimal and maximal digits in the decimal representation of $x$ without leading zeroes. For examples refer to notes. Your task is calculate $a_{K}$ for given $a_{1}$ and $K$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of independent test cases. Each test case consists of a single line containing two integers $a_{1}$ and $K$ ($1 \le a_{1} \le 10^{18}$, $1 \le K \le 10^{16}$) separated by a space. -----Output----- For each test case print one integer $a_{K}$ on a separate line. -----Example----- Input 8 1 4 487 1 487 2 487 3 487 4 487 5 487 6 487 7 Output 42 487 519 528 544 564 588 628 -----Note----- $a_{1} = 487$ $a_{2} = a_{1} + minDigit(a_{1}) \cdot maxDigit(a_{1}) = 487 + \min (4, 8, 7) \cdot \max (4, 8, 7) = 487 + 4 \cdot 8 = 519$ $a_{3} = a_{2} + minDigit(a_{2}) \cdot maxDigit(a_{2}) = 519 + \min (5, 1, 9) \cdot \max (5, 1, 9) = 519 + 1 \cdot 9 = 528$ $a_{4} = a_{3} + minDigit(a_{3}) \cdot maxDigit(a_{3}) = 528 + \min (5, 2, 8) \cdot \max (5, 2, 8) = 528 + 2 \cdot 8 = 544$ $a_{5} = a_{4} + minDigit(a_{4}) \cdot maxDigit(a_{4}) = 544 + \min (5, 4, 4) \cdot \max (5, 4, 4) = 544 + 4 \cdot 5 = 564$ $a_{6} = a_{5} + minDigit(a_{5}) \cdot maxDigit(a_{5}) = 564 + \min (5, 6, 4) \cdot \max (5, 6, 4) = 564 + 4 \cdot 6 = 588$ $a_{7} = a_{6} + minDigit(a_{6}) \cdot maxDigit(a_{6}) = 588 + \min (5, 8, 8) \cdot \max (5, 8, 8) = 588 + 5 \cdot 8 = 628$
for _ in range(int(input())): a, k = list(map(int, input().split())) for _ in range(k-1): mn = min(str(a)) mx = max(str(a)) if mn == "0": break a += int(mn)*int(mx) print(a)
Let's define the following recurrence: $$a_{n+1} = a_{n} + minDigit(a_{n}) \cdot maxDigit(a_{n}).$$ Here $minDigit(x)$ and $maxDigit(x)$ are the minimal and maximal digits in the decimal representation of $x$ without leading zeroes. For examples refer to notes. Your task is calculate $a_{K}$ for given $a_{1}$ and $K$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of independent test cases. Each test case consists of a single line containing two integers $a_{1}$ and $K$ ($1 \le a_{1} \le 10^{18}$, $1 \le K \le 10^{16}$) separated by a space. -----Output----- For each test case print one integer $a_{K}$ on a separate line. -----Example----- Input 8 1 4 487 1 487 2 487 3 487 4 487 5 487 6 487 7 Output 42 487 519 528 544 564 588 628 -----Note----- $a_{1} = 487$ $a_{2} = a_{1} + minDigit(a_{1}) \cdot maxDigit(a_{1}) = 487 + \min (4, 8, 7) \cdot \max (4, 8, 7) = 487 + 4 \cdot 8 = 519$ $a_{3} = a_{2} + minDigit(a_{2}) \cdot maxDigit(a_{2}) = 519 + \min (5, 1, 9) \cdot \max (5, 1, 9) = 519 + 1 \cdot 9 = 528$ $a_{4} = a_{3} + minDigit(a_{3}) \cdot maxDigit(a_{3}) = 528 + \min (5, 2, 8) \cdot \max (5, 2, 8) = 528 + 2 \cdot 8 = 544$ $a_{5} = a_{4} + minDigit(a_{4}) \cdot maxDigit(a_{4}) = 544 + \min (5, 4, 4) \cdot \max (5, 4, 4) = 544 + 4 \cdot 5 = 564$ $a_{6} = a_{5} + minDigit(a_{5}) \cdot maxDigit(a_{5}) = 564 + \min (5, 6, 4) \cdot \max (5, 6, 4) = 564 + 4 \cdot 6 = 588$ $a_{7} = a_{6} + minDigit(a_{6}) \cdot maxDigit(a_{6}) = 588 + \min (5, 8, 8) \cdot \max (5, 8, 8) = 588 + 5 \cdot 8 = 628$
def main(): t = int(input()) for _ in range(t): n, k = [int(x) for x in input().split(" ")] for _ in range(k-1): nr = [int(x) for x in str(n)] min_d = min(nr) max_d = max(nr) if min_d == 0: break else: n += min_d * max_d print(n) main()
Let's define the following recurrence: $$a_{n+1} = a_{n} + minDigit(a_{n}) \cdot maxDigit(a_{n}).$$ Here $minDigit(x)$ and $maxDigit(x)$ are the minimal and maximal digits in the decimal representation of $x$ without leading zeroes. For examples refer to notes. Your task is calculate $a_{K}$ for given $a_{1}$ and $K$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of independent test cases. Each test case consists of a single line containing two integers $a_{1}$ and $K$ ($1 \le a_{1} \le 10^{18}$, $1 \le K \le 10^{16}$) separated by a space. -----Output----- For each test case print one integer $a_{K}$ on a separate line. -----Example----- Input 8 1 4 487 1 487 2 487 3 487 4 487 5 487 6 487 7 Output 42 487 519 528 544 564 588 628 -----Note----- $a_{1} = 487$ $a_{2} = a_{1} + minDigit(a_{1}) \cdot maxDigit(a_{1}) = 487 + \min (4, 8, 7) \cdot \max (4, 8, 7) = 487 + 4 \cdot 8 = 519$ $a_{3} = a_{2} + minDigit(a_{2}) \cdot maxDigit(a_{2}) = 519 + \min (5, 1, 9) \cdot \max (5, 1, 9) = 519 + 1 \cdot 9 = 528$ $a_{4} = a_{3} + minDigit(a_{3}) \cdot maxDigit(a_{3}) = 528 + \min (5, 2, 8) \cdot \max (5, 2, 8) = 528 + 2 \cdot 8 = 544$ $a_{5} = a_{4} + minDigit(a_{4}) \cdot maxDigit(a_{4}) = 544 + \min (5, 4, 4) \cdot \max (5, 4, 4) = 544 + 4 \cdot 5 = 564$ $a_{6} = a_{5} + minDigit(a_{5}) \cdot maxDigit(a_{5}) = 564 + \min (5, 6, 4) \cdot \max (5, 6, 4) = 564 + 4 \cdot 6 = 588$ $a_{7} = a_{6} + minDigit(a_{6}) \cdot maxDigit(a_{6}) = 588 + \min (5, 8, 8) \cdot \max (5, 8, 8) = 588 + 5 \cdot 8 = 628$
import os import sys if os.path.exists('/mnt/c/Users/Square/square/codeforces'): f = iter(open('A.txt').readlines()) def input(): return next(f) # input = lambda: sys.stdin.readline().strip() else: input = lambda: sys.stdin.readline().strip() fprint = lambda *args: print(*args, flush=True) def min_max(x): l = list(str(x)) return int(min(l)), int(max(l)) t = int(input()) for _ in range(t): a, K = map(int, input().split()) K -= 1 for _ in range(K): u, v = min_max(a) if u == 0: break a += u*v print(a)
Let's define the following recurrence: $$a_{n+1} = a_{n} + minDigit(a_{n}) \cdot maxDigit(a_{n}).$$ Here $minDigit(x)$ and $maxDigit(x)$ are the minimal and maximal digits in the decimal representation of $x$ without leading zeroes. For examples refer to notes. Your task is calculate $a_{K}$ for given $a_{1}$ and $K$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of independent test cases. Each test case consists of a single line containing two integers $a_{1}$ and $K$ ($1 \le a_{1} \le 10^{18}$, $1 \le K \le 10^{16}$) separated by a space. -----Output----- For each test case print one integer $a_{K}$ on a separate line. -----Example----- Input 8 1 4 487 1 487 2 487 3 487 4 487 5 487 6 487 7 Output 42 487 519 528 544 564 588 628 -----Note----- $a_{1} = 487$ $a_{2} = a_{1} + minDigit(a_{1}) \cdot maxDigit(a_{1}) = 487 + \min (4, 8, 7) \cdot \max (4, 8, 7) = 487 + 4 \cdot 8 = 519$ $a_{3} = a_{2} + minDigit(a_{2}) \cdot maxDigit(a_{2}) = 519 + \min (5, 1, 9) \cdot \max (5, 1, 9) = 519 + 1 \cdot 9 = 528$ $a_{4} = a_{3} + minDigit(a_{3}) \cdot maxDigit(a_{3}) = 528 + \min (5, 2, 8) \cdot \max (5, 2, 8) = 528 + 2 \cdot 8 = 544$ $a_{5} = a_{4} + minDigit(a_{4}) \cdot maxDigit(a_{4}) = 544 + \min (5, 4, 4) \cdot \max (5, 4, 4) = 544 + 4 \cdot 5 = 564$ $a_{6} = a_{5} + minDigit(a_{5}) \cdot maxDigit(a_{5}) = 564 + \min (5, 6, 4) \cdot \max (5, 6, 4) = 564 + 4 \cdot 6 = 588$ $a_{7} = a_{6} + minDigit(a_{6}) \cdot maxDigit(a_{6}) = 588 + \min (5, 8, 8) \cdot \max (5, 8, 8) = 588 + 5 \cdot 8 = 628$
import math for _ in range(int(input())): a,k=list(map(int,input().split())) for i in range(k-1): d=list(str(a)) l=int(min(d))*int(max(d)) if l==0: break a+=l print(a)
Let's define the following recurrence: $$a_{n+1} = a_{n} + minDigit(a_{n}) \cdot maxDigit(a_{n}).$$ Here $minDigit(x)$ and $maxDigit(x)$ are the minimal and maximal digits in the decimal representation of $x$ without leading zeroes. For examples refer to notes. Your task is calculate $a_{K}$ for given $a_{1}$ and $K$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of independent test cases. Each test case consists of a single line containing two integers $a_{1}$ and $K$ ($1 \le a_{1} \le 10^{18}$, $1 \le K \le 10^{16}$) separated by a space. -----Output----- For each test case print one integer $a_{K}$ on a separate line. -----Example----- Input 8 1 4 487 1 487 2 487 3 487 4 487 5 487 6 487 7 Output 42 487 519 528 544 564 588 628 -----Note----- $a_{1} = 487$ $a_{2} = a_{1} + minDigit(a_{1}) \cdot maxDigit(a_{1}) = 487 + \min (4, 8, 7) \cdot \max (4, 8, 7) = 487 + 4 \cdot 8 = 519$ $a_{3} = a_{2} + minDigit(a_{2}) \cdot maxDigit(a_{2}) = 519 + \min (5, 1, 9) \cdot \max (5, 1, 9) = 519 + 1 \cdot 9 = 528$ $a_{4} = a_{3} + minDigit(a_{3}) \cdot maxDigit(a_{3}) = 528 + \min (5, 2, 8) \cdot \max (5, 2, 8) = 528 + 2 \cdot 8 = 544$ $a_{5} = a_{4} + minDigit(a_{4}) \cdot maxDigit(a_{4}) = 544 + \min (5, 4, 4) \cdot \max (5, 4, 4) = 544 + 4 \cdot 5 = 564$ $a_{6} = a_{5} + minDigit(a_{5}) \cdot maxDigit(a_{5}) = 564 + \min (5, 6, 4) \cdot \max (5, 6, 4) = 564 + 4 \cdot 6 = 588$ $a_{7} = a_{6} + minDigit(a_{6}) \cdot maxDigit(a_{6}) = 588 + \min (5, 8, 8) \cdot \max (5, 8, 8) = 588 + 5 \cdot 8 = 628$
import sys # from collections import deque # import heapq # from math import inf # from math import gcd # print(help(deque)) # 26 pprint = lambda s: print(' '.join(map(str,s))) input = lambda: sys.stdin.readline().strip() ipnut = input for i in range(int(input())): a,k = map(int,input().split()) # n = int(input()) # s = list(map(int,input().split())) for i in range(k-1): x = list(map(int,str(a))) a_i = min(x) a_m = max(x) a = a+a_i*a_m if a_i==0: break print(a) """ 10 10 11 12 13 14 15 16 17 11 11 """
Let's define the following recurrence: $$a_{n+1} = a_{n} + minDigit(a_{n}) \cdot maxDigit(a_{n}).$$ Here $minDigit(x)$ and $maxDigit(x)$ are the minimal and maximal digits in the decimal representation of $x$ without leading zeroes. For examples refer to notes. Your task is calculate $a_{K}$ for given $a_{1}$ and $K$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of independent test cases. Each test case consists of a single line containing two integers $a_{1}$ and $K$ ($1 \le a_{1} \le 10^{18}$, $1 \le K \le 10^{16}$) separated by a space. -----Output----- For each test case print one integer $a_{K}$ on a separate line. -----Example----- Input 8 1 4 487 1 487 2 487 3 487 4 487 5 487 6 487 7 Output 42 487 519 528 544 564 588 628 -----Note----- $a_{1} = 487$ $a_{2} = a_{1} + minDigit(a_{1}) \cdot maxDigit(a_{1}) = 487 + \min (4, 8, 7) \cdot \max (4, 8, 7) = 487 + 4 \cdot 8 = 519$ $a_{3} = a_{2} + minDigit(a_{2}) \cdot maxDigit(a_{2}) = 519 + \min (5, 1, 9) \cdot \max (5, 1, 9) = 519 + 1 \cdot 9 = 528$ $a_{4} = a_{3} + minDigit(a_{3}) \cdot maxDigit(a_{3}) = 528 + \min (5, 2, 8) \cdot \max (5, 2, 8) = 528 + 2 \cdot 8 = 544$ $a_{5} = a_{4} + minDigit(a_{4}) \cdot maxDigit(a_{4}) = 544 + \min (5, 4, 4) \cdot \max (5, 4, 4) = 544 + 4 \cdot 5 = 564$ $a_{6} = a_{5} + minDigit(a_{5}) \cdot maxDigit(a_{5}) = 564 + \min (5, 6, 4) \cdot \max (5, 6, 4) = 564 + 4 \cdot 6 = 588$ $a_{7} = a_{6} + minDigit(a_{6}) \cdot maxDigit(a_{6}) = 588 + \min (5, 8, 8) \cdot \max (5, 8, 8) = 588 + 5 \cdot 8 = 628$
import sys input=sys.stdin.readline from collections import defaultdict as dd,deque as dq t=int(input()) while t: #n=int(input()) n,k=map(int,input().split()) #l=list(map(int,input().split()) k-=1 while k: l=str(n).strip() x=int(min(l))*int(max(l)) if(x==0): break n=n+x k-=1 print(n) t-=1
Let's define the following recurrence: $$a_{n+1} = a_{n} + minDigit(a_{n}) \cdot maxDigit(a_{n}).$$ Here $minDigit(x)$ and $maxDigit(x)$ are the minimal and maximal digits in the decimal representation of $x$ without leading zeroes. For examples refer to notes. Your task is calculate $a_{K}$ for given $a_{1}$ and $K$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of independent test cases. Each test case consists of a single line containing two integers $a_{1}$ and $K$ ($1 \le a_{1} \le 10^{18}$, $1 \le K \le 10^{16}$) separated by a space. -----Output----- For each test case print one integer $a_{K}$ on a separate line. -----Example----- Input 8 1 4 487 1 487 2 487 3 487 4 487 5 487 6 487 7 Output 42 487 519 528 544 564 588 628 -----Note----- $a_{1} = 487$ $a_{2} = a_{1} + minDigit(a_{1}) \cdot maxDigit(a_{1}) = 487 + \min (4, 8, 7) \cdot \max (4, 8, 7) = 487 + 4 \cdot 8 = 519$ $a_{3} = a_{2} + minDigit(a_{2}) \cdot maxDigit(a_{2}) = 519 + \min (5, 1, 9) \cdot \max (5, 1, 9) = 519 + 1 \cdot 9 = 528$ $a_{4} = a_{3} + minDigit(a_{3}) \cdot maxDigit(a_{3}) = 528 + \min (5, 2, 8) \cdot \max (5, 2, 8) = 528 + 2 \cdot 8 = 544$ $a_{5} = a_{4} + minDigit(a_{4}) \cdot maxDigit(a_{4}) = 544 + \min (5, 4, 4) \cdot \max (5, 4, 4) = 544 + 4 \cdot 5 = 564$ $a_{6} = a_{5} + minDigit(a_{5}) \cdot maxDigit(a_{5}) = 564 + \min (5, 6, 4) \cdot \max (5, 6, 4) = 564 + 4 \cdot 6 = 588$ $a_{7} = a_{6} + minDigit(a_{6}) \cdot maxDigit(a_{6}) = 588 + \min (5, 8, 8) \cdot \max (5, 8, 8) = 588 + 5 \cdot 8 = 628$
t=int(input()) def m(n): s=[int(i) for i in str(n)] s.sort() return (s[0],s[-1]) for _ in range(t): n,k=(map(int,input().split())) pre=n for i in range(2,k+1): a=m(n) n=n+a[0]*a[1] if pre==n: break else: pre=n print(n)
Let's define the following recurrence: $$a_{n+1} = a_{n} + minDigit(a_{n}) \cdot maxDigit(a_{n}).$$ Here $minDigit(x)$ and $maxDigit(x)$ are the minimal and maximal digits in the decimal representation of $x$ without leading zeroes. For examples refer to notes. Your task is calculate $a_{K}$ for given $a_{1}$ and $K$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of independent test cases. Each test case consists of a single line containing two integers $a_{1}$ and $K$ ($1 \le a_{1} \le 10^{18}$, $1 \le K \le 10^{16}$) separated by a space. -----Output----- For each test case print one integer $a_{K}$ on a separate line. -----Example----- Input 8 1 4 487 1 487 2 487 3 487 4 487 5 487 6 487 7 Output 42 487 519 528 544 564 588 628 -----Note----- $a_{1} = 487$ $a_{2} = a_{1} + minDigit(a_{1}) \cdot maxDigit(a_{1}) = 487 + \min (4, 8, 7) \cdot \max (4, 8, 7) = 487 + 4 \cdot 8 = 519$ $a_{3} = a_{2} + minDigit(a_{2}) \cdot maxDigit(a_{2}) = 519 + \min (5, 1, 9) \cdot \max (5, 1, 9) = 519 + 1 \cdot 9 = 528$ $a_{4} = a_{3} + minDigit(a_{3}) \cdot maxDigit(a_{3}) = 528 + \min (5, 2, 8) \cdot \max (5, 2, 8) = 528 + 2 \cdot 8 = 544$ $a_{5} = a_{4} + minDigit(a_{4}) \cdot maxDigit(a_{4}) = 544 + \min (5, 4, 4) \cdot \max (5, 4, 4) = 544 + 4 \cdot 5 = 564$ $a_{6} = a_{5} + minDigit(a_{5}) \cdot maxDigit(a_{5}) = 564 + \min (5, 6, 4) \cdot \max (5, 6, 4) = 564 + 4 \cdot 6 = 588$ $a_{7} = a_{6} + minDigit(a_{6}) \cdot maxDigit(a_{6}) = 588 + \min (5, 8, 8) \cdot \max (5, 8, 8) = 588 + 5 \cdot 8 = 628$
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): a1,k = map(int,input().split()) a = a1 k -= 1 while k and str(a).count("0") == 0: ls = [int(str(a)[i]) for i in range(len(str(a)))] a += max(ls)*min(ls) k -= 1 print(a)
Let's define the following recurrence: $$a_{n+1} = a_{n} + minDigit(a_{n}) \cdot maxDigit(a_{n}).$$ Here $minDigit(x)$ and $maxDigit(x)$ are the minimal and maximal digits in the decimal representation of $x$ without leading zeroes. For examples refer to notes. Your task is calculate $a_{K}$ for given $a_{1}$ and $K$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of independent test cases. Each test case consists of a single line containing two integers $a_{1}$ and $K$ ($1 \le a_{1} \le 10^{18}$, $1 \le K \le 10^{16}$) separated by a space. -----Output----- For each test case print one integer $a_{K}$ on a separate line. -----Example----- Input 8 1 4 487 1 487 2 487 3 487 4 487 5 487 6 487 7 Output 42 487 519 528 544 564 588 628 -----Note----- $a_{1} = 487$ $a_{2} = a_{1} + minDigit(a_{1}) \cdot maxDigit(a_{1}) = 487 + \min (4, 8, 7) \cdot \max (4, 8, 7) = 487 + 4 \cdot 8 = 519$ $a_{3} = a_{2} + minDigit(a_{2}) \cdot maxDigit(a_{2}) = 519 + \min (5, 1, 9) \cdot \max (5, 1, 9) = 519 + 1 \cdot 9 = 528$ $a_{4} = a_{3} + minDigit(a_{3}) \cdot maxDigit(a_{3}) = 528 + \min (5, 2, 8) \cdot \max (5, 2, 8) = 528 + 2 \cdot 8 = 544$ $a_{5} = a_{4} + minDigit(a_{4}) \cdot maxDigit(a_{4}) = 544 + \min (5, 4, 4) \cdot \max (5, 4, 4) = 544 + 4 \cdot 5 = 564$ $a_{6} = a_{5} + minDigit(a_{5}) \cdot maxDigit(a_{5}) = 564 + \min (5, 6, 4) \cdot \max (5, 6, 4) = 564 + 4 \cdot 6 = 588$ $a_{7} = a_{6} + minDigit(a_{6}) \cdot maxDigit(a_{6}) = 588 + \min (5, 8, 8) \cdot \max (5, 8, 8) = 588 + 5 \cdot 8 = 628$
def solve(): a, k = list(map(int,input().split())) seen = set() items = [a] for i in range(k-1): last = items[-1] min_dig = int(min(str(last))) max_dig = int(max(str(last))) nw = last + min_dig * max_dig items.append(nw) if '0' in str(items): break print(items[-1]) for i in range(int(input())): solve()
Let's define the following recurrence: $$a_{n+1} = a_{n} + minDigit(a_{n}) \cdot maxDigit(a_{n}).$$ Here $minDigit(x)$ and $maxDigit(x)$ are the minimal and maximal digits in the decimal representation of $x$ without leading zeroes. For examples refer to notes. Your task is calculate $a_{K}$ for given $a_{1}$ and $K$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of independent test cases. Each test case consists of a single line containing two integers $a_{1}$ and $K$ ($1 \le a_{1} \le 10^{18}$, $1 \le K \le 10^{16}$) separated by a space. -----Output----- For each test case print one integer $a_{K}$ on a separate line. -----Example----- Input 8 1 4 487 1 487 2 487 3 487 4 487 5 487 6 487 7 Output 42 487 519 528 544 564 588 628 -----Note----- $a_{1} = 487$ $a_{2} = a_{1} + minDigit(a_{1}) \cdot maxDigit(a_{1}) = 487 + \min (4, 8, 7) \cdot \max (4, 8, 7) = 487 + 4 \cdot 8 = 519$ $a_{3} = a_{2} + minDigit(a_{2}) \cdot maxDigit(a_{2}) = 519 + \min (5, 1, 9) \cdot \max (5, 1, 9) = 519 + 1 \cdot 9 = 528$ $a_{4} = a_{3} + minDigit(a_{3}) \cdot maxDigit(a_{3}) = 528 + \min (5, 2, 8) \cdot \max (5, 2, 8) = 528 + 2 \cdot 8 = 544$ $a_{5} = a_{4} + minDigit(a_{4}) \cdot maxDigit(a_{4}) = 544 + \min (5, 4, 4) \cdot \max (5, 4, 4) = 544 + 4 \cdot 5 = 564$ $a_{6} = a_{5} + minDigit(a_{5}) \cdot maxDigit(a_{5}) = 564 + \min (5, 6, 4) \cdot \max (5, 6, 4) = 564 + 4 \cdot 6 = 588$ $a_{7} = a_{6} + minDigit(a_{6}) \cdot maxDigit(a_{6}) = 588 + \min (5, 8, 8) \cdot \max (5, 8, 8) = 588 + 5 \cdot 8 = 628$
def digits(n): l=[] while(n>0): l.append(n%10) n=n//10 mina=min(l) maxa=max(l) return mina*maxa t=int(input()) for you in range(t): l=input().split() a=int(l[0]) k=int(l[1]) for i in range(k-1): if(digits(a)==0): break a+=digits(a) print(a)
Let's define the following recurrence: $$a_{n+1} = a_{n} + minDigit(a_{n}) \cdot maxDigit(a_{n}).$$ Here $minDigit(x)$ and $maxDigit(x)$ are the minimal and maximal digits in the decimal representation of $x$ without leading zeroes. For examples refer to notes. Your task is calculate $a_{K}$ for given $a_{1}$ and $K$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of independent test cases. Each test case consists of a single line containing two integers $a_{1}$ and $K$ ($1 \le a_{1} \le 10^{18}$, $1 \le K \le 10^{16}$) separated by a space. -----Output----- For each test case print one integer $a_{K}$ on a separate line. -----Example----- Input 8 1 4 487 1 487 2 487 3 487 4 487 5 487 6 487 7 Output 42 487 519 528 544 564 588 628 -----Note----- $a_{1} = 487$ $a_{2} = a_{1} + minDigit(a_{1}) \cdot maxDigit(a_{1}) = 487 + \min (4, 8, 7) \cdot \max (4, 8, 7) = 487 + 4 \cdot 8 = 519$ $a_{3} = a_{2} + minDigit(a_{2}) \cdot maxDigit(a_{2}) = 519 + \min (5, 1, 9) \cdot \max (5, 1, 9) = 519 + 1 \cdot 9 = 528$ $a_{4} = a_{3} + minDigit(a_{3}) \cdot maxDigit(a_{3}) = 528 + \min (5, 2, 8) \cdot \max (5, 2, 8) = 528 + 2 \cdot 8 = 544$ $a_{5} = a_{4} + minDigit(a_{4}) \cdot maxDigit(a_{4}) = 544 + \min (5, 4, 4) \cdot \max (5, 4, 4) = 544 + 4 \cdot 5 = 564$ $a_{6} = a_{5} + minDigit(a_{5}) \cdot maxDigit(a_{5}) = 564 + \min (5, 6, 4) \cdot \max (5, 6, 4) = 564 + 4 \cdot 6 = 588$ $a_{7} = a_{6} + minDigit(a_{6}) \cdot maxDigit(a_{6}) = 588 + \min (5, 8, 8) \cdot \max (5, 8, 8) = 588 + 5 \cdot 8 = 628$
t = int(input()) for ii in range(t): a, k = map(int, input().split()) cur = 0 while cur < k - 1 and '0' not in str(a): mi = 1000 ma = -1 for i in str(a): mi = min(int(i), mi) ma = max(int(i), ma) a += mi * ma cur += 1 print(a)
Let's define the following recurrence: $$a_{n+1} = a_{n} + minDigit(a_{n}) \cdot maxDigit(a_{n}).$$ Here $minDigit(x)$ and $maxDigit(x)$ are the minimal and maximal digits in the decimal representation of $x$ without leading zeroes. For examples refer to notes. Your task is calculate $a_{K}$ for given $a_{1}$ and $K$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of independent test cases. Each test case consists of a single line containing two integers $a_{1}$ and $K$ ($1 \le a_{1} \le 10^{18}$, $1 \le K \le 10^{16}$) separated by a space. -----Output----- For each test case print one integer $a_{K}$ on a separate line. -----Example----- Input 8 1 4 487 1 487 2 487 3 487 4 487 5 487 6 487 7 Output 42 487 519 528 544 564 588 628 -----Note----- $a_{1} = 487$ $a_{2} = a_{1} + minDigit(a_{1}) \cdot maxDigit(a_{1}) = 487 + \min (4, 8, 7) \cdot \max (4, 8, 7) = 487 + 4 \cdot 8 = 519$ $a_{3} = a_{2} + minDigit(a_{2}) \cdot maxDigit(a_{2}) = 519 + \min (5, 1, 9) \cdot \max (5, 1, 9) = 519 + 1 \cdot 9 = 528$ $a_{4} = a_{3} + minDigit(a_{3}) \cdot maxDigit(a_{3}) = 528 + \min (5, 2, 8) \cdot \max (5, 2, 8) = 528 + 2 \cdot 8 = 544$ $a_{5} = a_{4} + minDigit(a_{4}) \cdot maxDigit(a_{4}) = 544 + \min (5, 4, 4) \cdot \max (5, 4, 4) = 544 + 4 \cdot 5 = 564$ $a_{6} = a_{5} + minDigit(a_{5}) \cdot maxDigit(a_{5}) = 564 + \min (5, 6, 4) \cdot \max (5, 6, 4) = 564 + 4 \cdot 6 = 588$ $a_{7} = a_{6} + minDigit(a_{6}) \cdot maxDigit(a_{6}) = 588 + \min (5, 8, 8) \cdot \max (5, 8, 8) = 588 + 5 \cdot 8 = 628$
t = int(input()) for _ in range(t): a, k = list(map(int, input().split())) for i in range(k - 1): a += int(min(str(a))) * int(max(str(a))) if '0' in str(a): break print(a)
Let's define the following recurrence: $$a_{n+1} = a_{n} + minDigit(a_{n}) \cdot maxDigit(a_{n}).$$ Here $minDigit(x)$ and $maxDigit(x)$ are the minimal and maximal digits in the decimal representation of $x$ without leading zeroes. For examples refer to notes. Your task is calculate $a_{K}$ for given $a_{1}$ and $K$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of independent test cases. Each test case consists of a single line containing two integers $a_{1}$ and $K$ ($1 \le a_{1} \le 10^{18}$, $1 \le K \le 10^{16}$) separated by a space. -----Output----- For each test case print one integer $a_{K}$ on a separate line. -----Example----- Input 8 1 4 487 1 487 2 487 3 487 4 487 5 487 6 487 7 Output 42 487 519 528 544 564 588 628 -----Note----- $a_{1} = 487$ $a_{2} = a_{1} + minDigit(a_{1}) \cdot maxDigit(a_{1}) = 487 + \min (4, 8, 7) \cdot \max (4, 8, 7) = 487 + 4 \cdot 8 = 519$ $a_{3} = a_{2} + minDigit(a_{2}) \cdot maxDigit(a_{2}) = 519 + \min (5, 1, 9) \cdot \max (5, 1, 9) = 519 + 1 \cdot 9 = 528$ $a_{4} = a_{3} + minDigit(a_{3}) \cdot maxDigit(a_{3}) = 528 + \min (5, 2, 8) \cdot \max (5, 2, 8) = 528 + 2 \cdot 8 = 544$ $a_{5} = a_{4} + minDigit(a_{4}) \cdot maxDigit(a_{4}) = 544 + \min (5, 4, 4) \cdot \max (5, 4, 4) = 544 + 4 \cdot 5 = 564$ $a_{6} = a_{5} + minDigit(a_{5}) \cdot maxDigit(a_{5}) = 564 + \min (5, 6, 4) \cdot \max (5, 6, 4) = 564 + 4 \cdot 6 = 588$ $a_{7} = a_{6} + minDigit(a_{6}) \cdot maxDigit(a_{6}) = 588 + \min (5, 8, 8) \cdot \max (5, 8, 8) = 588 + 5 \cdot 8 = 628$
def f(x): s=str(x) mn=12 mx=0 for i in s: mx=max(mx,int(i)) mn=min(mn,int(i)) return mn*mx for _ in range(int(input())): a,k=list(map(int,input().split())) k-=1 prev=-1 while(k>0): if(prev==a): break prev=a a+=f(a) k-=1 print(a)
Let's define the following recurrence: $$a_{n+1} = a_{n} + minDigit(a_{n}) \cdot maxDigit(a_{n}).$$ Here $minDigit(x)$ and $maxDigit(x)$ are the minimal and maximal digits in the decimal representation of $x$ without leading zeroes. For examples refer to notes. Your task is calculate $a_{K}$ for given $a_{1}$ and $K$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of independent test cases. Each test case consists of a single line containing two integers $a_{1}$ and $K$ ($1 \le a_{1} \le 10^{18}$, $1 \le K \le 10^{16}$) separated by a space. -----Output----- For each test case print one integer $a_{K}$ on a separate line. -----Example----- Input 8 1 4 487 1 487 2 487 3 487 4 487 5 487 6 487 7 Output 42 487 519 528 544 564 588 628 -----Note----- $a_{1} = 487$ $a_{2} = a_{1} + minDigit(a_{1}) \cdot maxDigit(a_{1}) = 487 + \min (4, 8, 7) \cdot \max (4, 8, 7) = 487 + 4 \cdot 8 = 519$ $a_{3} = a_{2} + minDigit(a_{2}) \cdot maxDigit(a_{2}) = 519 + \min (5, 1, 9) \cdot \max (5, 1, 9) = 519 + 1 \cdot 9 = 528$ $a_{4} = a_{3} + minDigit(a_{3}) \cdot maxDigit(a_{3}) = 528 + \min (5, 2, 8) \cdot \max (5, 2, 8) = 528 + 2 \cdot 8 = 544$ $a_{5} = a_{4} + minDigit(a_{4}) \cdot maxDigit(a_{4}) = 544 + \min (5, 4, 4) \cdot \max (5, 4, 4) = 544 + 4 \cdot 5 = 564$ $a_{6} = a_{5} + minDigit(a_{5}) \cdot maxDigit(a_{5}) = 564 + \min (5, 6, 4) \cdot \max (5, 6, 4) = 564 + 4 \cdot 6 = 588$ $a_{7} = a_{6} + minDigit(a_{6}) \cdot maxDigit(a_{6}) = 588 + \min (5, 8, 8) \cdot \max (5, 8, 8) = 588 + 5 \cdot 8 = 628$
'''input 8 1 4 487 1 487 2 487 3 487 4 487 5 487 6 487 7 ''' import math def dig(x): mn = x%10 mx = x%10 while x>0: mn = min(mn,x%10) mx = max(mx,x%10) x//=10 return mn,mx def solve(): a,k = map(int,input().split()) l = [a] ln = 1 for i in range(1000): pv = l[ln-1] mn,mx = dig(pv) if mn ==0: break l.append(pv+mx*mn) ln+=1 k = min(k,len(l)) print(l[k-1]) return t = 1 t = int(input()) while t>0: t-=1 solve()
Let's define the following recurrence: $$a_{n+1} = a_{n} + minDigit(a_{n}) \cdot maxDigit(a_{n}).$$ Here $minDigit(x)$ and $maxDigit(x)$ are the minimal and maximal digits in the decimal representation of $x$ without leading zeroes. For examples refer to notes. Your task is calculate $a_{K}$ for given $a_{1}$ and $K$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of independent test cases. Each test case consists of a single line containing two integers $a_{1}$ and $K$ ($1 \le a_{1} \le 10^{18}$, $1 \le K \le 10^{16}$) separated by a space. -----Output----- For each test case print one integer $a_{K}$ on a separate line. -----Example----- Input 8 1 4 487 1 487 2 487 3 487 4 487 5 487 6 487 7 Output 42 487 519 528 544 564 588 628 -----Note----- $a_{1} = 487$ $a_{2} = a_{1} + minDigit(a_{1}) \cdot maxDigit(a_{1}) = 487 + \min (4, 8, 7) \cdot \max (4, 8, 7) = 487 + 4 \cdot 8 = 519$ $a_{3} = a_{2} + minDigit(a_{2}) \cdot maxDigit(a_{2}) = 519 + \min (5, 1, 9) \cdot \max (5, 1, 9) = 519 + 1 \cdot 9 = 528$ $a_{4} = a_{3} + minDigit(a_{3}) \cdot maxDigit(a_{3}) = 528 + \min (5, 2, 8) \cdot \max (5, 2, 8) = 528 + 2 \cdot 8 = 544$ $a_{5} = a_{4} + minDigit(a_{4}) \cdot maxDigit(a_{4}) = 544 + \min (5, 4, 4) \cdot \max (5, 4, 4) = 544 + 4 \cdot 5 = 564$ $a_{6} = a_{5} + minDigit(a_{5}) \cdot maxDigit(a_{5}) = 564 + \min (5, 6, 4) \cdot \max (5, 6, 4) = 564 + 4 \cdot 6 = 588$ $a_{7} = a_{6} + minDigit(a_{6}) \cdot maxDigit(a_{6}) = 588 + \min (5, 8, 8) \cdot \max (5, 8, 8) = 588 + 5 \cdot 8 = 628$
def main(): t = int(input()) for ti in range(t): a, k = map(int, input().split()) for i in range(k - 1): astr = str(a) mn, mx = int(min(astr)), int(max(astr)) if mn == 0 or mx == 0: break a += mn * mx print(a) main()
Let's define the following recurrence: $$a_{n+1} = a_{n} + minDigit(a_{n}) \cdot maxDigit(a_{n}).$$ Here $minDigit(x)$ and $maxDigit(x)$ are the minimal and maximal digits in the decimal representation of $x$ without leading zeroes. For examples refer to notes. Your task is calculate $a_{K}$ for given $a_{1}$ and $K$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of independent test cases. Each test case consists of a single line containing two integers $a_{1}$ and $K$ ($1 \le a_{1} \le 10^{18}$, $1 \le K \le 10^{16}$) separated by a space. -----Output----- For each test case print one integer $a_{K}$ on a separate line. -----Example----- Input 8 1 4 487 1 487 2 487 3 487 4 487 5 487 6 487 7 Output 42 487 519 528 544 564 588 628 -----Note----- $a_{1} = 487$ $a_{2} = a_{1} + minDigit(a_{1}) \cdot maxDigit(a_{1}) = 487 + \min (4, 8, 7) \cdot \max (4, 8, 7) = 487 + 4 \cdot 8 = 519$ $a_{3} = a_{2} + minDigit(a_{2}) \cdot maxDigit(a_{2}) = 519 + \min (5, 1, 9) \cdot \max (5, 1, 9) = 519 + 1 \cdot 9 = 528$ $a_{4} = a_{3} + minDigit(a_{3}) \cdot maxDigit(a_{3}) = 528 + \min (5, 2, 8) \cdot \max (5, 2, 8) = 528 + 2 \cdot 8 = 544$ $a_{5} = a_{4} + minDigit(a_{4}) \cdot maxDigit(a_{4}) = 544 + \min (5, 4, 4) \cdot \max (5, 4, 4) = 544 + 4 \cdot 5 = 564$ $a_{6} = a_{5} + minDigit(a_{5}) \cdot maxDigit(a_{5}) = 564 + \min (5, 6, 4) \cdot \max (5, 6, 4) = 564 + 4 \cdot 6 = 588$ $a_{7} = a_{6} + minDigit(a_{6}) \cdot maxDigit(a_{6}) = 588 + \min (5, 8, 8) \cdot \max (5, 8, 8) = 588 + 5 \cdot 8 = 628$
from sys import stdin, stdout input = stdin.readline #print = stdout.write for _ in range(int(input())): n, k = list(map(int, input().split())) have = n for i in range(k - 1): digits = list(map(int, str(have))) bf = min(digits) * max(digits) if bf == 0: break have += bf print(have)
Let's define the following recurrence: $$a_{n+1} = a_{n} + minDigit(a_{n}) \cdot maxDigit(a_{n}).$$ Here $minDigit(x)$ and $maxDigit(x)$ are the minimal and maximal digits in the decimal representation of $x$ without leading zeroes. For examples refer to notes. Your task is calculate $a_{K}$ for given $a_{1}$ and $K$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of independent test cases. Each test case consists of a single line containing two integers $a_{1}$ and $K$ ($1 \le a_{1} \le 10^{18}$, $1 \le K \le 10^{16}$) separated by a space. -----Output----- For each test case print one integer $a_{K}$ on a separate line. -----Example----- Input 8 1 4 487 1 487 2 487 3 487 4 487 5 487 6 487 7 Output 42 487 519 528 544 564 588 628 -----Note----- $a_{1} = 487$ $a_{2} = a_{1} + minDigit(a_{1}) \cdot maxDigit(a_{1}) = 487 + \min (4, 8, 7) \cdot \max (4, 8, 7) = 487 + 4 \cdot 8 = 519$ $a_{3} = a_{2} + minDigit(a_{2}) \cdot maxDigit(a_{2}) = 519 + \min (5, 1, 9) \cdot \max (5, 1, 9) = 519 + 1 \cdot 9 = 528$ $a_{4} = a_{3} + minDigit(a_{3}) \cdot maxDigit(a_{3}) = 528 + \min (5, 2, 8) \cdot \max (5, 2, 8) = 528 + 2 \cdot 8 = 544$ $a_{5} = a_{4} + minDigit(a_{4}) \cdot maxDigit(a_{4}) = 544 + \min (5, 4, 4) \cdot \max (5, 4, 4) = 544 + 4 \cdot 5 = 564$ $a_{6} = a_{5} + minDigit(a_{5}) \cdot maxDigit(a_{5}) = 564 + \min (5, 6, 4) \cdot \max (5, 6, 4) = 564 + 4 \cdot 6 = 588$ $a_{7} = a_{6} + minDigit(a_{6}) \cdot maxDigit(a_{6}) = 588 + \min (5, 8, 8) \cdot \max (5, 8, 8) = 588 + 5 \cdot 8 = 628$
cases = int(input()) for _ in range(cases): n, k = [int(s) for s in input().split()] mind = -1 for _ in range(k-1): if mind==0: break s = str(n) mind, maxd = int(s[0]), int(s[0]) for l in s: value = int(l) if value>maxd: maxd = value elif value<mind: mind = value if mind==0: break n += maxd*mind print(n)
Let's define the following recurrence: $$a_{n+1} = a_{n} + minDigit(a_{n}) \cdot maxDigit(a_{n}).$$ Here $minDigit(x)$ and $maxDigit(x)$ are the minimal and maximal digits in the decimal representation of $x$ without leading zeroes. For examples refer to notes. Your task is calculate $a_{K}$ for given $a_{1}$ and $K$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of independent test cases. Each test case consists of a single line containing two integers $a_{1}$ and $K$ ($1 \le a_{1} \le 10^{18}$, $1 \le K \le 10^{16}$) separated by a space. -----Output----- For each test case print one integer $a_{K}$ on a separate line. -----Example----- Input 8 1 4 487 1 487 2 487 3 487 4 487 5 487 6 487 7 Output 42 487 519 528 544 564 588 628 -----Note----- $a_{1} = 487$ $a_{2} = a_{1} + minDigit(a_{1}) \cdot maxDigit(a_{1}) = 487 + \min (4, 8, 7) \cdot \max (4, 8, 7) = 487 + 4 \cdot 8 = 519$ $a_{3} = a_{2} + minDigit(a_{2}) \cdot maxDigit(a_{2}) = 519 + \min (5, 1, 9) \cdot \max (5, 1, 9) = 519 + 1 \cdot 9 = 528$ $a_{4} = a_{3} + minDigit(a_{3}) \cdot maxDigit(a_{3}) = 528 + \min (5, 2, 8) \cdot \max (5, 2, 8) = 528 + 2 \cdot 8 = 544$ $a_{5} = a_{4} + minDigit(a_{4}) \cdot maxDigit(a_{4}) = 544 + \min (5, 4, 4) \cdot \max (5, 4, 4) = 544 + 4 \cdot 5 = 564$ $a_{6} = a_{5} + minDigit(a_{5}) \cdot maxDigit(a_{5}) = 564 + \min (5, 6, 4) \cdot \max (5, 6, 4) = 564 + 4 \cdot 6 = 588$ $a_{7} = a_{6} + minDigit(a_{6}) \cdot maxDigit(a_{6}) = 588 + \min (5, 8, 8) \cdot \max (5, 8, 8) = 588 + 5 \cdot 8 = 628$
t=int(input()) for i in range(t): a1,k=map(int,input().split()) an=a1 for j in range(1,k): astr=str(an) min=9 max=0 for r in range(len(astr)): if int(astr[r])<min: min=int(astr[r]) if int(astr[r])>max: max=int(astr[r]) an+=min*max if min==0: break print(an)
Let's define the following recurrence: $$a_{n+1} = a_{n} + minDigit(a_{n}) \cdot maxDigit(a_{n}).$$ Here $minDigit(x)$ and $maxDigit(x)$ are the minimal and maximal digits in the decimal representation of $x$ without leading zeroes. For examples refer to notes. Your task is calculate $a_{K}$ for given $a_{1}$ and $K$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of independent test cases. Each test case consists of a single line containing two integers $a_{1}$ and $K$ ($1 \le a_{1} \le 10^{18}$, $1 \le K \le 10^{16}$) separated by a space. -----Output----- For each test case print one integer $a_{K}$ on a separate line. -----Example----- Input 8 1 4 487 1 487 2 487 3 487 4 487 5 487 6 487 7 Output 42 487 519 528 544 564 588 628 -----Note----- $a_{1} = 487$ $a_{2} = a_{1} + minDigit(a_{1}) \cdot maxDigit(a_{1}) = 487 + \min (4, 8, 7) \cdot \max (4, 8, 7) = 487 + 4 \cdot 8 = 519$ $a_{3} = a_{2} + minDigit(a_{2}) \cdot maxDigit(a_{2}) = 519 + \min (5, 1, 9) \cdot \max (5, 1, 9) = 519 + 1 \cdot 9 = 528$ $a_{4} = a_{3} + minDigit(a_{3}) \cdot maxDigit(a_{3}) = 528 + \min (5, 2, 8) \cdot \max (5, 2, 8) = 528 + 2 \cdot 8 = 544$ $a_{5} = a_{4} + minDigit(a_{4}) \cdot maxDigit(a_{4}) = 544 + \min (5, 4, 4) \cdot \max (5, 4, 4) = 544 + 4 \cdot 5 = 564$ $a_{6} = a_{5} + minDigit(a_{5}) \cdot maxDigit(a_{5}) = 564 + \min (5, 6, 4) \cdot \max (5, 6, 4) = 564 + 4 \cdot 6 = 588$ $a_{7} = a_{6} + minDigit(a_{6}) \cdot maxDigit(a_{6}) = 588 + \min (5, 8, 8) \cdot \max (5, 8, 8) = 588 + 5 \cdot 8 = 628$
T = int(input()) for t in range(T): a, K = list(map(int, input().split())) for k in range(K - 1): a_ar = list(map(int, list(str(a)))) if min(a_ar) == 0: break a += min(a_ar) * max(a_ar) print(a)
Let's define the following recurrence: $$a_{n+1} = a_{n} + minDigit(a_{n}) \cdot maxDigit(a_{n}).$$ Here $minDigit(x)$ and $maxDigit(x)$ are the minimal and maximal digits in the decimal representation of $x$ without leading zeroes. For examples refer to notes. Your task is calculate $a_{K}$ for given $a_{1}$ and $K$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of independent test cases. Each test case consists of a single line containing two integers $a_{1}$ and $K$ ($1 \le a_{1} \le 10^{18}$, $1 \le K \le 10^{16}$) separated by a space. -----Output----- For each test case print one integer $a_{K}$ on a separate line. -----Example----- Input 8 1 4 487 1 487 2 487 3 487 4 487 5 487 6 487 7 Output 42 487 519 528 544 564 588 628 -----Note----- $a_{1} = 487$ $a_{2} = a_{1} + minDigit(a_{1}) \cdot maxDigit(a_{1}) = 487 + \min (4, 8, 7) \cdot \max (4, 8, 7) = 487 + 4 \cdot 8 = 519$ $a_{3} = a_{2} + minDigit(a_{2}) \cdot maxDigit(a_{2}) = 519 + \min (5, 1, 9) \cdot \max (5, 1, 9) = 519 + 1 \cdot 9 = 528$ $a_{4} = a_{3} + minDigit(a_{3}) \cdot maxDigit(a_{3}) = 528 + \min (5, 2, 8) \cdot \max (5, 2, 8) = 528 + 2 \cdot 8 = 544$ $a_{5} = a_{4} + minDigit(a_{4}) \cdot maxDigit(a_{4}) = 544 + \min (5, 4, 4) \cdot \max (5, 4, 4) = 544 + 4 \cdot 5 = 564$ $a_{6} = a_{5} + minDigit(a_{5}) \cdot maxDigit(a_{5}) = 564 + \min (5, 6, 4) \cdot \max (5, 6, 4) = 564 + 4 \cdot 6 = 588$ $a_{7} = a_{6} + minDigit(a_{6}) \cdot maxDigit(a_{6}) = 588 + \min (5, 8, 8) \cdot \max (5, 8, 8) = 588 + 5 \cdot 8 = 628$
for _ in range(int(input())): a, k = list(map(int, input().split())) if '0' in str(a): print(a) else: while '0' not in str(a) and k != 1: k -= 1 a += int(max(str(a))) * int(min(str(a))) print(a)
Let's define the following recurrence: $$a_{n+1} = a_{n} + minDigit(a_{n}) \cdot maxDigit(a_{n}).$$ Here $minDigit(x)$ and $maxDigit(x)$ are the minimal and maximal digits in the decimal representation of $x$ without leading zeroes. For examples refer to notes. Your task is calculate $a_{K}$ for given $a_{1}$ and $K$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of independent test cases. Each test case consists of a single line containing two integers $a_{1}$ and $K$ ($1 \le a_{1} \le 10^{18}$, $1 \le K \le 10^{16}$) separated by a space. -----Output----- For each test case print one integer $a_{K}$ on a separate line. -----Example----- Input 8 1 4 487 1 487 2 487 3 487 4 487 5 487 6 487 7 Output 42 487 519 528 544 564 588 628 -----Note----- $a_{1} = 487$ $a_{2} = a_{1} + minDigit(a_{1}) \cdot maxDigit(a_{1}) = 487 + \min (4, 8, 7) \cdot \max (4, 8, 7) = 487 + 4 \cdot 8 = 519$ $a_{3} = a_{2} + minDigit(a_{2}) \cdot maxDigit(a_{2}) = 519 + \min (5, 1, 9) \cdot \max (5, 1, 9) = 519 + 1 \cdot 9 = 528$ $a_{4} = a_{3} + minDigit(a_{3}) \cdot maxDigit(a_{3}) = 528 + \min (5, 2, 8) \cdot \max (5, 2, 8) = 528 + 2 \cdot 8 = 544$ $a_{5} = a_{4} + minDigit(a_{4}) \cdot maxDigit(a_{4}) = 544 + \min (5, 4, 4) \cdot \max (5, 4, 4) = 544 + 4 \cdot 5 = 564$ $a_{6} = a_{5} + minDigit(a_{5}) \cdot maxDigit(a_{5}) = 564 + \min (5, 6, 4) \cdot \max (5, 6, 4) = 564 + 4 \cdot 6 = 588$ $a_{7} = a_{6} + minDigit(a_{6}) \cdot maxDigit(a_{6}) = 588 + \min (5, 8, 8) \cdot \max (5, 8, 8) = 588 + 5 \cdot 8 = 628$
import sys input = lambda: sys.stdin.readline().strip() t = int(input()) while t: t-=1 a1,k = map(int,input().split()) while k>1: k-=1 val = list(map(int,list(str(a1)))) if min(val)==0: break # print(val) a1 = a1+min(val)*max(val) print(a1)
Let's define the following recurrence: $$a_{n+1} = a_{n} + minDigit(a_{n}) \cdot maxDigit(a_{n}).$$ Here $minDigit(x)$ and $maxDigit(x)$ are the minimal and maximal digits in the decimal representation of $x$ without leading zeroes. For examples refer to notes. Your task is calculate $a_{K}$ for given $a_{1}$ and $K$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of independent test cases. Each test case consists of a single line containing two integers $a_{1}$ and $K$ ($1 \le a_{1} \le 10^{18}$, $1 \le K \le 10^{16}$) separated by a space. -----Output----- For each test case print one integer $a_{K}$ on a separate line. -----Example----- Input 8 1 4 487 1 487 2 487 3 487 4 487 5 487 6 487 7 Output 42 487 519 528 544 564 588 628 -----Note----- $a_{1} = 487$ $a_{2} = a_{1} + minDigit(a_{1}) \cdot maxDigit(a_{1}) = 487 + \min (4, 8, 7) \cdot \max (4, 8, 7) = 487 + 4 \cdot 8 = 519$ $a_{3} = a_{2} + minDigit(a_{2}) \cdot maxDigit(a_{2}) = 519 + \min (5, 1, 9) \cdot \max (5, 1, 9) = 519 + 1 \cdot 9 = 528$ $a_{4} = a_{3} + minDigit(a_{3}) \cdot maxDigit(a_{3}) = 528 + \min (5, 2, 8) \cdot \max (5, 2, 8) = 528 + 2 \cdot 8 = 544$ $a_{5} = a_{4} + minDigit(a_{4}) \cdot maxDigit(a_{4}) = 544 + \min (5, 4, 4) \cdot \max (5, 4, 4) = 544 + 4 \cdot 5 = 564$ $a_{6} = a_{5} + minDigit(a_{5}) \cdot maxDigit(a_{5}) = 564 + \min (5, 6, 4) \cdot \max (5, 6, 4) = 564 + 4 \cdot 6 = 588$ $a_{7} = a_{6} + minDigit(a_{6}) \cdot maxDigit(a_{6}) = 588 + \min (5, 8, 8) \cdot \max (5, 8, 8) = 588 + 5 \cdot 8 = 628$
for i in range(int(input())): a, k = map(int, input().split()) while '0' not in str(a) and k > 1: mi = 10 ma = -1 for j in range(len(str(a))): if int(str(a)[j]) > ma: ma = int(str(a)[j]) if int(str(a)[j]) < mi: mi = int(str(a)[j]) a += ma * mi k -= 1 print(a)
Let's define the following recurrence: $$a_{n+1} = a_{n} + minDigit(a_{n}) \cdot maxDigit(a_{n}).$$ Here $minDigit(x)$ and $maxDigit(x)$ are the minimal and maximal digits in the decimal representation of $x$ without leading zeroes. For examples refer to notes. Your task is calculate $a_{K}$ for given $a_{1}$ and $K$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of independent test cases. Each test case consists of a single line containing two integers $a_{1}$ and $K$ ($1 \le a_{1} \le 10^{18}$, $1 \le K \le 10^{16}$) separated by a space. -----Output----- For each test case print one integer $a_{K}$ on a separate line. -----Example----- Input 8 1 4 487 1 487 2 487 3 487 4 487 5 487 6 487 7 Output 42 487 519 528 544 564 588 628 -----Note----- $a_{1} = 487$ $a_{2} = a_{1} + minDigit(a_{1}) \cdot maxDigit(a_{1}) = 487 + \min (4, 8, 7) \cdot \max (4, 8, 7) = 487 + 4 \cdot 8 = 519$ $a_{3} = a_{2} + minDigit(a_{2}) \cdot maxDigit(a_{2}) = 519 + \min (5, 1, 9) \cdot \max (5, 1, 9) = 519 + 1 \cdot 9 = 528$ $a_{4} = a_{3} + minDigit(a_{3}) \cdot maxDigit(a_{3}) = 528 + \min (5, 2, 8) \cdot \max (5, 2, 8) = 528 + 2 \cdot 8 = 544$ $a_{5} = a_{4} + minDigit(a_{4}) \cdot maxDigit(a_{4}) = 544 + \min (5, 4, 4) \cdot \max (5, 4, 4) = 544 + 4 \cdot 5 = 564$ $a_{6} = a_{5} + minDigit(a_{5}) \cdot maxDigit(a_{5}) = 564 + \min (5, 6, 4) \cdot \max (5, 6, 4) = 564 + 4 \cdot 6 = 588$ $a_{7} = a_{6} + minDigit(a_{6}) \cdot maxDigit(a_{6}) = 588 + \min (5, 8, 8) \cdot \max (5, 8, 8) = 588 + 5 \cdot 8 = 628$
import sys, os if 'local' in os.environ : sys.stdin = open('./input.txt', 'r') f = lambda:list(map(int, input().split())) midigit = lambda x: str(x) def solve(): t = f()[0] for _ in range(t): a, k = f() if k == 1: print(a) continue for i in range(k-1): an = a + int(min(str(a))) * int(max(str(a))) if a == an: break a = an print(a) solve()
Let's define the following recurrence: $$a_{n+1} = a_{n} + minDigit(a_{n}) \cdot maxDigit(a_{n}).$$ Here $minDigit(x)$ and $maxDigit(x)$ are the minimal and maximal digits in the decimal representation of $x$ without leading zeroes. For examples refer to notes. Your task is calculate $a_{K}$ for given $a_{1}$ and $K$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of independent test cases. Each test case consists of a single line containing two integers $a_{1}$ and $K$ ($1 \le a_{1} \le 10^{18}$, $1 \le K \le 10^{16}$) separated by a space. -----Output----- For each test case print one integer $a_{K}$ on a separate line. -----Example----- Input 8 1 4 487 1 487 2 487 3 487 4 487 5 487 6 487 7 Output 42 487 519 528 544 564 588 628 -----Note----- $a_{1} = 487$ $a_{2} = a_{1} + minDigit(a_{1}) \cdot maxDigit(a_{1}) = 487 + \min (4, 8, 7) \cdot \max (4, 8, 7) = 487 + 4 \cdot 8 = 519$ $a_{3} = a_{2} + minDigit(a_{2}) \cdot maxDigit(a_{2}) = 519 + \min (5, 1, 9) \cdot \max (5, 1, 9) = 519 + 1 \cdot 9 = 528$ $a_{4} = a_{3} + minDigit(a_{3}) \cdot maxDigit(a_{3}) = 528 + \min (5, 2, 8) \cdot \max (5, 2, 8) = 528 + 2 \cdot 8 = 544$ $a_{5} = a_{4} + minDigit(a_{4}) \cdot maxDigit(a_{4}) = 544 + \min (5, 4, 4) \cdot \max (5, 4, 4) = 544 + 4 \cdot 5 = 564$ $a_{6} = a_{5} + minDigit(a_{5}) \cdot maxDigit(a_{5}) = 564 + \min (5, 6, 4) \cdot \max (5, 6, 4) = 564 + 4 \cdot 6 = 588$ $a_{7} = a_{6} + minDigit(a_{6}) \cdot maxDigit(a_{6}) = 588 + \min (5, 8, 8) \cdot \max (5, 8, 8) = 588 + 5 \cdot 8 = 628$
for _ in range(int(input())): a, k = tuple(map(int, input().split())) for i in range(k - 1): nums = [i for i in str(a)] delta = int(min(nums)) * int(max(nums)) if delta == 0: break a += delta print(a)
Let's define the following recurrence: $$a_{n+1} = a_{n} + minDigit(a_{n}) \cdot maxDigit(a_{n}).$$ Here $minDigit(x)$ and $maxDigit(x)$ are the minimal and maximal digits in the decimal representation of $x$ without leading zeroes. For examples refer to notes. Your task is calculate $a_{K}$ for given $a_{1}$ and $K$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of independent test cases. Each test case consists of a single line containing two integers $a_{1}$ and $K$ ($1 \le a_{1} \le 10^{18}$, $1 \le K \le 10^{16}$) separated by a space. -----Output----- For each test case print one integer $a_{K}$ on a separate line. -----Example----- Input 8 1 4 487 1 487 2 487 3 487 4 487 5 487 6 487 7 Output 42 487 519 528 544 564 588 628 -----Note----- $a_{1} = 487$ $a_{2} = a_{1} + minDigit(a_{1}) \cdot maxDigit(a_{1}) = 487 + \min (4, 8, 7) \cdot \max (4, 8, 7) = 487 + 4 \cdot 8 = 519$ $a_{3} = a_{2} + minDigit(a_{2}) \cdot maxDigit(a_{2}) = 519 + \min (5, 1, 9) \cdot \max (5, 1, 9) = 519 + 1 \cdot 9 = 528$ $a_{4} = a_{3} + minDigit(a_{3}) \cdot maxDigit(a_{3}) = 528 + \min (5, 2, 8) \cdot \max (5, 2, 8) = 528 + 2 \cdot 8 = 544$ $a_{5} = a_{4} + minDigit(a_{4}) \cdot maxDigit(a_{4}) = 544 + \min (5, 4, 4) \cdot \max (5, 4, 4) = 544 + 4 \cdot 5 = 564$ $a_{6} = a_{5} + minDigit(a_{5}) \cdot maxDigit(a_{5}) = 564 + \min (5, 6, 4) \cdot \max (5, 6, 4) = 564 + 4 \cdot 6 = 588$ $a_{7} = a_{6} + minDigit(a_{6}) \cdot maxDigit(a_{6}) = 588 + \min (5, 8, 8) \cdot \max (5, 8, 8) = 588 + 5 \cdot 8 = 628$
t = int(input()) buf = [] for _ in range(t): a, k = input().split() k = int(k) - 1 for _ in range(k): c = min(a) d = max(a) a = str(int(a) + int(c) * int(d)) if '0' in a: break buf.append(a) print('\n'.join(buf))
Let's define the following recurrence: $$a_{n+1} = a_{n} + minDigit(a_{n}) \cdot maxDigit(a_{n}).$$ Here $minDigit(x)$ and $maxDigit(x)$ are the minimal and maximal digits in the decimal representation of $x$ without leading zeroes. For examples refer to notes. Your task is calculate $a_{K}$ for given $a_{1}$ and $K$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$)Β β€” the number of independent test cases. Each test case consists of a single line containing two integers $a_{1}$ and $K$ ($1 \le a_{1} \le 10^{18}$, $1 \le K \le 10^{16}$) separated by a space. -----Output----- For each test case print one integer $a_{K}$ on a separate line. -----Example----- Input 8 1 4 487 1 487 2 487 3 487 4 487 5 487 6 487 7 Output 42 487 519 528 544 564 588 628 -----Note----- $a_{1} = 487$ $a_{2} = a_{1} + minDigit(a_{1}) \cdot maxDigit(a_{1}) = 487 + \min (4, 8, 7) \cdot \max (4, 8, 7) = 487 + 4 \cdot 8 = 519$ $a_{3} = a_{2} + minDigit(a_{2}) \cdot maxDigit(a_{2}) = 519 + \min (5, 1, 9) \cdot \max (5, 1, 9) = 519 + 1 \cdot 9 = 528$ $a_{4} = a_{3} + minDigit(a_{3}) \cdot maxDigit(a_{3}) = 528 + \min (5, 2, 8) \cdot \max (5, 2, 8) = 528 + 2 \cdot 8 = 544$ $a_{5} = a_{4} + minDigit(a_{4}) \cdot maxDigit(a_{4}) = 544 + \min (5, 4, 4) \cdot \max (5, 4, 4) = 544 + 4 \cdot 5 = 564$ $a_{6} = a_{5} + minDigit(a_{5}) \cdot maxDigit(a_{5}) = 564 + \min (5, 6, 4) \cdot \max (5, 6, 4) = 564 + 4 \cdot 6 = 588$ $a_{7} = a_{6} + minDigit(a_{6}) \cdot maxDigit(a_{6}) = 588 + \min (5, 8, 8) \cdot \max (5, 8, 8) = 588 + 5 \cdot 8 = 628$
from sys import stdin, stdout import heapq import cProfile, math from collections import Counter, defaultdict, deque from bisect import bisect_left, bisect, bisect_right import itertools from copy import deepcopy from fractions import Fraction import sys, threading import operator as op from functools import reduce import sys def get_int(): return int(stdin.readline().strip()) def get_tuple(): return list(map(int, stdin.readline().split())) def get_list(): return list(map(int, stdin.readline().split())) def solve(): n,k = get_tuple() n = str(n) while '0' not in n and k>1: n = int(n) + int(max(n))*int(min(n)) n = str(n) k -= 1 return n def main(): ans = solve() print(ans) TestCases = True if TestCases: for i in range(get_int()): main() else: main()
The only difference between easy and hard versions is constraints. Now elections are held in Berland and you want to win them. More precisely, you want everyone to vote for you. There are $n$ voters, and two ways to convince each of them to vote for you. The first way to convince the $i$-th voter is to pay him $p_i$ coins. The second way is to make $m_i$ other voters vote for you, and the $i$-th voter will vote for free. Moreover, the process of such voting takes place in several steps. For example, if there are five voters with $m_1 = 1$, $m_2 = 2$, $m_3 = 2$, $m_4 = 4$, $m_5 = 5$, then you can buy the vote of the fifth voter, and eventually everyone will vote for you. Set of people voting for you will change as follows: ${5} \rightarrow {1, 5} \rightarrow {1, 2, 3, 5} \rightarrow {1, 2, 3, 4, 5}$. Calculate the minimum number of coins you have to spend so that everyone votes for you. -----Input----- The first line contains one integer $t$ ($1 \le t \le 5000$) β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 5000$) β€” the number of voters. The next $n$ lines contains the description of voters. $i$-th line contains two integers $m_i$ and $p_i$ ($1 \le p_i \le 10^9, 0 \le m_i < n$). It is guaranteed that the sum of all $n$ over all test cases does not exceed $5000$. -----Output----- For each test case print one integer β€” the minimum number of coins you have to spend so that everyone votes for you. -----Example----- Input 3 3 1 5 2 10 2 8 7 0 1 3 1 1 1 6 1 1 1 4 1 4 1 6 2 6 2 3 2 8 2 7 4 4 5 5 Output 8 0 7 -----Note----- In the first test case you have to buy vote of the third voter. Then the set of people voting for you will change as follows: ${3} \rightarrow {1, 3} \rightarrow {1, 2, 3}$. In the second example you don't need to buy votes. The set of people voting for you will change as follows: ${1} \rightarrow {1, 3, 5} \rightarrow {1, 2, 3, 5} \rightarrow {1, 2, 3, 5, 6, 7} \rightarrow {1, 2, 3, 4, 5, 6, 7}$. In the third test case you have to buy votes of the second and the fifth voters. Then the set of people voting for you will change as follows: ${2, 5} \rightarrow {1, 2, 3, 4, 5} \rightarrow {1, 2, 3, 4, 5, 6}$.
import heapq for _ in range(int(input())): n = int(input()) voters = [] for i in range(n): m,p = list(map(int, input().split())) voters.append((m, -p)) voters.sort() for i in range(n): voters[i] = (voters[i][0], -voters[i][1]) ans = 0 costs = [] heapq.heapify(costs) bought = 0 for i in range(n-1, -1, -1): buysNeeded = voters[i][0] - i - bought heapq.heappush(costs, voters[i][1]) while buysNeeded > 0 and len(costs) > 0: ans += heapq.heappop(costs) bought += 1 buysNeeded -= 1 print(ans)
The only difference between easy and hard versions is constraints. Now elections are held in Berland and you want to win them. More precisely, you want everyone to vote for you. There are $n$ voters, and two ways to convince each of them to vote for you. The first way to convince the $i$-th voter is to pay him $p_i$ coins. The second way is to make $m_i$ other voters vote for you, and the $i$-th voter will vote for free. Moreover, the process of such voting takes place in several steps. For example, if there are five voters with $m_1 = 1$, $m_2 = 2$, $m_3 = 2$, $m_4 = 4$, $m_5 = 5$, then you can buy the vote of the fifth voter, and eventually everyone will vote for you. Set of people voting for you will change as follows: ${5} \rightarrow {1, 5} \rightarrow {1, 2, 3, 5} \rightarrow {1, 2, 3, 4, 5}$. Calculate the minimum number of coins you have to spend so that everyone votes for you. -----Input----- The first line contains one integer $t$ ($1 \le t \le 5000$) β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 5000$) β€” the number of voters. The next $n$ lines contains the description of voters. $i$-th line contains two integers $m_i$ and $p_i$ ($1 \le p_i \le 10^9, 0 \le m_i < n$). It is guaranteed that the sum of all $n$ over all test cases does not exceed $5000$. -----Output----- For each test case print one integer β€” the minimum number of coins you have to spend so that everyone votes for you. -----Example----- Input 3 3 1 5 2 10 2 8 7 0 1 3 1 1 1 6 1 1 1 4 1 4 1 6 2 6 2 3 2 8 2 7 4 4 5 5 Output 8 0 7 -----Note----- In the first test case you have to buy vote of the third voter. Then the set of people voting for you will change as follows: ${3} \rightarrow {1, 3} \rightarrow {1, 2, 3}$. In the second example you don't need to buy votes. The set of people voting for you will change as follows: ${1} \rightarrow {1, 3, 5} \rightarrow {1, 2, 3, 5} \rightarrow {1, 2, 3, 5, 6, 7} \rightarrow {1, 2, 3, 4, 5, 6, 7}$. In the third test case you have to buy votes of the second and the fifth voters. Then the set of people voting for you will change as follows: ${2, 5} \rightarrow {1, 2, 3, 4, 5} \rightarrow {1, 2, 3, 4, 5, 6}$.
''' Created on 2019. 9. 21. @author: kkhh88 ''' #q = int(input()) #x, y = map(int,input().split(' ')) q = int(input()) for _ in range(q): n = int(input()) lr = [] for i in range(n): lr.append(list(map(int,input().split(' ')))) lr.sort(key=lambda x:x[1], reverse = True) lr.sort(key=lambda x:x[0]) cnt = [0]*n for i in range(n): if lr[i][0] > i: if lr[i][0] - i > cnt[lr[i][0]]: cnt[lr[i][0]] = lr[i][0] - i i = n - 1 tmp = 0 ans = 0 lst = [] while i >= 0: if i > 0 and lr[i][0] == lr[i-1][0]: lst.append(lr[i][1]) i = i - 1 else: lst.append(lr[i][1]) if cnt[lr[i][0]] > tmp: lst.sort() for _ in range(tmp, cnt[lr[i][0]]): ans = ans + lst.pop(0) tmp = cnt[lr[i][0]] i = i - 1 #print (cnt, lr) print (ans)
The only difference between easy and hard versions is constraints. Now elections are held in Berland and you want to win them. More precisely, you want everyone to vote for you. There are $n$ voters, and two ways to convince each of them to vote for you. The first way to convince the $i$-th voter is to pay him $p_i$ coins. The second way is to make $m_i$ other voters vote for you, and the $i$-th voter will vote for free. Moreover, the process of such voting takes place in several steps. For example, if there are five voters with $m_1 = 1$, $m_2 = 2$, $m_3 = 2$, $m_4 = 4$, $m_5 = 5$, then you can buy the vote of the fifth voter, and eventually everyone will vote for you. Set of people voting for you will change as follows: ${5} \rightarrow {1, 5} \rightarrow {1, 2, 3, 5} \rightarrow {1, 2, 3, 4, 5}$. Calculate the minimum number of coins you have to spend so that everyone votes for you. -----Input----- The first line contains one integer $t$ ($1 \le t \le 5000$) β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 5000$) β€” the number of voters. The next $n$ lines contains the description of voters. $i$-th line contains two integers $m_i$ and $p_i$ ($1 \le p_i \le 10^9, 0 \le m_i < n$). It is guaranteed that the sum of all $n$ over all test cases does not exceed $5000$. -----Output----- For each test case print one integer β€” the minimum number of coins you have to spend so that everyone votes for you. -----Example----- Input 3 3 1 5 2 10 2 8 7 0 1 3 1 1 1 6 1 1 1 4 1 4 1 6 2 6 2 3 2 8 2 7 4 4 5 5 Output 8 0 7 -----Note----- In the first test case you have to buy vote of the third voter. Then the set of people voting for you will change as follows: ${3} \rightarrow {1, 3} \rightarrow {1, 2, 3}$. In the second example you don't need to buy votes. The set of people voting for you will change as follows: ${1} \rightarrow {1, 3, 5} \rightarrow {1, 2, 3, 5} \rightarrow {1, 2, 3, 5, 6, 7} \rightarrow {1, 2, 3, 4, 5, 6, 7}$. In the third test case you have to buy votes of the second and the fifth voters. Then the set of people voting for you will change as follows: ${2, 5} \rightarrow {1, 2, 3, 4, 5} \rightarrow {1, 2, 3, 4, 5, 6}$.
import sys def I(): return sys.stdin.readline().rstrip() class Heap: def __init__( self ): self.l = [ -1 ] self.n = 0 def n( self ): return self.n def top( self ): return self.l[ 1 ] def ins( self, x ): self.l.append( x ) n = len( self.l ) - 1 i = n while i > 1: j = i // 2 if self.l[ j ] > self.l[ i ]: self.l[ j ], self.l[ i ] = self.l[ i ], self.l[ j ] i = j else: break def pop( self ): r = self.l[ 1 ] l = self.l.pop() n = len( self.l ) - 1 if n: self.l[ 1 ] = l i = 1 while True: j = i * 2 k = j + 1 if k < len( self.l ) and self.l[ i ] > max( self.l[ j ], self.l[ k ] ): if self.l[ j ] == min( self.l[ j ], self.l[ k ] ): self.l[ i ], self.l[ j ] = self.l[ j ], self.l[ i ] i = j else: self.l[ i ], self.l[ k ] = self.l[ k ], self.l[ i ] i = k elif k < len( self.l ) and self.l[ i ] > self.l[ k ]: self.l[ i ], self.l[ k ] = self.l[ k ], self.l[ i ] i = k elif j < len( self.l ) and self.l[ i ] > self.l[ j ]: self.l[ i ], self.l[ j ] = self.l[ j ], self.l[ i ] i = j else: break return r t = int( I() ) for _ in range( t ): n = int( I() ) voter = [ list( map( int, I().split() ) ) for _ in range( n ) ] h = Heap() d = {} for m, p in voter: if m not in d: d[ m ] = [] d[ m ].append( p ) need = {} c = 0 sk = sorted( d.keys() ) for m in sk: need[ m ] = max( 0, m - c ) c += len( d[ m ] ) c = 0 ans = 0 for m in sk[::-1]: for p in d[ m ]: h.ins( p ) while c < need[ m ]: c += 1 ans += h.pop() print( ans )
The only difference between easy and hard versions is constraints. Now elections are held in Berland and you want to win them. More precisely, you want everyone to vote for you. There are $n$ voters, and two ways to convince each of them to vote for you. The first way to convince the $i$-th voter is to pay him $p_i$ coins. The second way is to make $m_i$ other voters vote for you, and the $i$-th voter will vote for free. Moreover, the process of such voting takes place in several steps. For example, if there are five voters with $m_1 = 1$, $m_2 = 2$, $m_3 = 2$, $m_4 = 4$, $m_5 = 5$, then you can buy the vote of the fifth voter, and eventually everyone will vote for you. Set of people voting for you will change as follows: ${5} \rightarrow {1, 5} \rightarrow {1, 2, 3, 5} \rightarrow {1, 2, 3, 4, 5}$. Calculate the minimum number of coins you have to spend so that everyone votes for you. -----Input----- The first line contains one integer $t$ ($1 \le t \le 5000$) β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 5000$) β€” the number of voters. The next $n$ lines contains the description of voters. $i$-th line contains two integers $m_i$ and $p_i$ ($1 \le p_i \le 10^9, 0 \le m_i < n$). It is guaranteed that the sum of all $n$ over all test cases does not exceed $5000$. -----Output----- For each test case print one integer β€” the minimum number of coins you have to spend so that everyone votes for you. -----Example----- Input 3 3 1 5 2 10 2 8 7 0 1 3 1 1 1 6 1 1 1 4 1 4 1 6 2 6 2 3 2 8 2 7 4 4 5 5 Output 8 0 7 -----Note----- In the first test case you have to buy vote of the third voter. Then the set of people voting for you will change as follows: ${3} \rightarrow {1, 3} \rightarrow {1, 2, 3}$. In the second example you don't need to buy votes. The set of people voting for you will change as follows: ${1} \rightarrow {1, 3, 5} \rightarrow {1, 2, 3, 5} \rightarrow {1, 2, 3, 5, 6, 7} \rightarrow {1, 2, 3, 4, 5, 6, 7}$. In the third test case you have to buy votes of the second and the fifth voters. Then the set of people voting for you will change as follows: ${2, 5} \rightarrow {1, 2, 3, 4, 5} \rightarrow {1, 2, 3, 4, 5, 6}$.
import heapq t = int(input()) for _ in range(t): n = int(input()) info = [list(map(int, input().split())) for i in range(n)] info = sorted(info) cnt = [0] * n for i in range(n): ind = info[i][0] cnt[ind] += 1 ruiseki_cnt = [0] * (n+1) for i in range(n): ruiseki_cnt[i+1] = ruiseki_cnt[i] + cnt[i] # print(cnt) # print(ruiseki_cnt) need = [0] * n for i in range(1,n): if cnt[i] != 0 and i > ruiseki_cnt[i]: need[i] = min(i - ruiseki_cnt[i], i) # print(need) info = sorted(info, reverse = True) #print(info) num = n - 1 pos = 0 q = [] used_cnt = 0 ans = 0 while True: if num == -1: break while True: if pos < n and info[pos][0] >= num: heapq.heappush(q, info[pos][1]) pos += 1 else: break if need[num] - used_cnt > 0: tmp = need[num] - used_cnt for _ in range(tmp): ans += heapq.heappop(q) used_cnt += tmp num -= 1 print(ans)
The only difference between easy and hard versions is constraints. Now elections are held in Berland and you want to win them. More precisely, you want everyone to vote for you. There are $n$ voters, and two ways to convince each of them to vote for you. The first way to convince the $i$-th voter is to pay him $p_i$ coins. The second way is to make $m_i$ other voters vote for you, and the $i$-th voter will vote for free. Moreover, the process of such voting takes place in several steps. For example, if there are five voters with $m_1 = 1$, $m_2 = 2$, $m_3 = 2$, $m_4 = 4$, $m_5 = 5$, then you can buy the vote of the fifth voter, and eventually everyone will vote for you. Set of people voting for you will change as follows: ${5} \rightarrow {1, 5} \rightarrow {1, 2, 3, 5} \rightarrow {1, 2, 3, 4, 5}$. Calculate the minimum number of coins you have to spend so that everyone votes for you. -----Input----- The first line contains one integer $t$ ($1 \le t \le 5000$) β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 5000$) β€” the number of voters. The next $n$ lines contains the description of voters. $i$-th line contains two integers $m_i$ and $p_i$ ($1 \le p_i \le 10^9, 0 \le m_i < n$). It is guaranteed that the sum of all $n$ over all test cases does not exceed $5000$. -----Output----- For each test case print one integer β€” the minimum number of coins you have to spend so that everyone votes for you. -----Example----- Input 3 3 1 5 2 10 2 8 7 0 1 3 1 1 1 6 1 1 1 4 1 4 1 6 2 6 2 3 2 8 2 7 4 4 5 5 Output 8 0 7 -----Note----- In the first test case you have to buy vote of the third voter. Then the set of people voting for you will change as follows: ${3} \rightarrow {1, 3} \rightarrow {1, 2, 3}$. In the second example you don't need to buy votes. The set of people voting for you will change as follows: ${1} \rightarrow {1, 3, 5} \rightarrow {1, 2, 3, 5} \rightarrow {1, 2, 3, 5, 6, 7} \rightarrow {1, 2, 3, 4, 5, 6, 7}$. In the third test case you have to buy votes of the second and the fifth voters. Then the set of people voting for you will change as follows: ${2, 5} \rightarrow {1, 2, 3, 4, 5} \rightarrow {1, 2, 3, 4, 5, 6}$.
import sys input = sys.stdin.readline import heapq from itertools import accumulate t=int(input()) for test in range(t): n=int(input()) M=[[] for i in range(n)] MCOUNT=[0]*(n) for i in range(n): m,p=list(map(int,input().split())) M[m].append(p) MCOUNT[m]+=1 #print(M) #print(MCOUNT) ACC=list(accumulate(MCOUNT)) #print(ACC) HQ=[] ANS=0 use=0 for i in range(n-1,-1,-1): for j in M[i]: heapq.heappush(HQ,j) #print(HQ) while ACC[i-1]+use<i: x=heapq.heappop(HQ) ANS+=x use+=1 print(ANS)
The only difference between easy and hard versions is constraints. Now elections are held in Berland and you want to win them. More precisely, you want everyone to vote for you. There are $n$ voters, and two ways to convince each of them to vote for you. The first way to convince the $i$-th voter is to pay him $p_i$ coins. The second way is to make $m_i$ other voters vote for you, and the $i$-th voter will vote for free. Moreover, the process of such voting takes place in several steps. For example, if there are five voters with $m_1 = 1$, $m_2 = 2$, $m_3 = 2$, $m_4 = 4$, $m_5 = 5$, then you can buy the vote of the fifth voter, and eventually everyone will vote for you. Set of people voting for you will change as follows: ${5} \rightarrow {1, 5} \rightarrow {1, 2, 3, 5} \rightarrow {1, 2, 3, 4, 5}$. Calculate the minimum number of coins you have to spend so that everyone votes for you. -----Input----- The first line contains one integer $t$ ($1 \le t \le 5000$) β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 5000$) β€” the number of voters. The next $n$ lines contains the description of voters. $i$-th line contains two integers $m_i$ and $p_i$ ($1 \le p_i \le 10^9, 0 \le m_i < n$). It is guaranteed that the sum of all $n$ over all test cases does not exceed $5000$. -----Output----- For each test case print one integer β€” the minimum number of coins you have to spend so that everyone votes for you. -----Example----- Input 3 3 1 5 2 10 2 8 7 0 1 3 1 1 1 6 1 1 1 4 1 4 1 6 2 6 2 3 2 8 2 7 4 4 5 5 Output 8 0 7 -----Note----- In the first test case you have to buy vote of the third voter. Then the set of people voting for you will change as follows: ${3} \rightarrow {1, 3} \rightarrow {1, 2, 3}$. In the second example you don't need to buy votes. The set of people voting for you will change as follows: ${1} \rightarrow {1, 3, 5} \rightarrow {1, 2, 3, 5} \rightarrow {1, 2, 3, 5, 6, 7} \rightarrow {1, 2, 3, 4, 5, 6, 7}$. In the third test case you have to buy votes of the second and the fifth voters. Then the set of people voting for you will change as follows: ${2, 5} \rightarrow {1, 2, 3, 4, 5} \rightarrow {1, 2, 3, 4, 5, 6}$.
import sys import heapq def solve(pr, mm): omm = [] n = len(mm) for i in range(n + 1): omm.append([]) for i in range(n): omm[mm[i]].append(pr[i]) for i in range(n + 1): omm[i] = sorted(omm[i]) heap = [] c = 0 t = n p = 0 for i in range(n, -1, -1): for h in omm[i]: heapq.heappush(heap, h) t -= len(omm[i]) mn = max(i - c - t, 0) c += mn for j in range(mn): p += heapq.heappop(heap) return p def __starting_point(): t = int(input().strip()) for i in range(t): n = int(input().strip()) ms = [] ps = [] for j in range(n): arr = [int(v) for v in input().strip().split(' ')] ms.append(arr[0]) ps.append(arr[1]) print(solve(ps, ms)) __starting_point()
The only difference between easy and hard versions is constraints. Now elections are held in Berland and you want to win them. More precisely, you want everyone to vote for you. There are $n$ voters, and two ways to convince each of them to vote for you. The first way to convince the $i$-th voter is to pay him $p_i$ coins. The second way is to make $m_i$ other voters vote for you, and the $i$-th voter will vote for free. Moreover, the process of such voting takes place in several steps. For example, if there are five voters with $m_1 = 1$, $m_2 = 2$, $m_3 = 2$, $m_4 = 4$, $m_5 = 5$, then you can buy the vote of the fifth voter, and eventually everyone will vote for you. Set of people voting for you will change as follows: ${5} \rightarrow {1, 5} \rightarrow {1, 2, 3, 5} \rightarrow {1, 2, 3, 4, 5}$. Calculate the minimum number of coins you have to spend so that everyone votes for you. -----Input----- The first line contains one integer $t$ ($1 \le t \le 5000$) β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 5000$) β€” the number of voters. The next $n$ lines contains the description of voters. $i$-th line contains two integers $m_i$ and $p_i$ ($1 \le p_i \le 10^9, 0 \le m_i < n$). It is guaranteed that the sum of all $n$ over all test cases does not exceed $5000$. -----Output----- For each test case print one integer β€” the minimum number of coins you have to spend so that everyone votes for you. -----Example----- Input 3 3 1 5 2 10 2 8 7 0 1 3 1 1 1 6 1 1 1 4 1 4 1 6 2 6 2 3 2 8 2 7 4 4 5 5 Output 8 0 7 -----Note----- In the first test case you have to buy vote of the third voter. Then the set of people voting for you will change as follows: ${3} \rightarrow {1, 3} \rightarrow {1, 2, 3}$. In the second example you don't need to buy votes. The set of people voting for you will change as follows: ${1} \rightarrow {1, 3, 5} \rightarrow {1, 2, 3, 5} \rightarrow {1, 2, 3, 5, 6, 7} \rightarrow {1, 2, 3, 4, 5, 6, 7}$. In the third test case you have to buy votes of the second and the fifth voters. Then the set of people voting for you will change as follows: ${2, 5} \rightarrow {1, 2, 3, 4, 5} \rightarrow {1, 2, 3, 4, 5, 6}$.
import heapq for _ in range(int(input())): n = int(input()) voters = [] for i in range(n): m,p = list(map(int, input().split())) voters.append((m, -p)) voters.sort() for i in range(n): voters[i] = (voters[i][0], -voters[i][1]) ans = 0 costs = [] heapq.heapify(costs) bought = 0 for i in range(n-1, -1, -1): buysNeeded = voters[i][0] - i - bought heapq.heappush(costs, voters[i][1]) while buysNeeded > 0 and len(costs) > 0: ans += heapq.heappop(costs) bought += 1 buysNeeded -= 1 print(ans)
The only difference between easy and hard versions is constraints. Now elections are held in Berland and you want to win them. More precisely, you want everyone to vote for you. There are $n$ voters, and two ways to convince each of them to vote for you. The first way to convince the $i$-th voter is to pay him $p_i$ coins. The second way is to make $m_i$ other voters vote for you, and the $i$-th voter will vote for free. Moreover, the process of such voting takes place in several steps. For example, if there are five voters with $m_1 = 1$, $m_2 = 2$, $m_3 = 2$, $m_4 = 4$, $m_5 = 5$, then you can buy the vote of the fifth voter, and eventually everyone will vote for you. Set of people voting for you will change as follows: ${5} \rightarrow {1, 5} \rightarrow {1, 2, 3, 5} \rightarrow {1, 2, 3, 4, 5}$. Calculate the minimum number of coins you have to spend so that everyone votes for you. -----Input----- The first line contains one integer $t$ ($1 \le t \le 5000$) β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 5000$) β€” the number of voters. The next $n$ lines contains the description of voters. $i$-th line contains two integers $m_i$ and $p_i$ ($1 \le p_i \le 10^9, 0 \le m_i < n$). It is guaranteed that the sum of all $n$ over all test cases does not exceed $5000$. -----Output----- For each test case print one integer β€” the minimum number of coins you have to spend so that everyone votes for you. -----Example----- Input 3 3 1 5 2 10 2 8 7 0 1 3 1 1 1 6 1 1 1 4 1 4 1 6 2 6 2 3 2 8 2 7 4 4 5 5 Output 8 0 7 -----Note----- In the first test case you have to buy vote of the third voter. Then the set of people voting for you will change as follows: ${3} \rightarrow {1, 3} \rightarrow {1, 2, 3}$. In the second example you don't need to buy votes. The set of people voting for you will change as follows: ${1} \rightarrow {1, 3, 5} \rightarrow {1, 2, 3, 5} \rightarrow {1, 2, 3, 5, 6, 7} \rightarrow {1, 2, 3, 4, 5, 6, 7}$. In the third test case you have to buy votes of the second and the fifth voters. Then the set of people voting for you will change as follows: ${2, 5} \rightarrow {1, 2, 3, 4, 5} \rightarrow {1, 2, 3, 4, 5, 6}$.
import sys from heapq import heappop, heappush reader = (line.rstrip() for line in sys.stdin) input = reader.__next__ t = int(input()) for _ in range(t): n = int(input()) mp = [] for i in range(n): mi, pi = list(map(int, input().split())) mp.append((mi, pi)) mp.sort() prices = [] cost = 0 bribed = 0 i = n - 1 while i >= 0: currM = mp[i][0] heappush(prices, mp[i][1]) while i >= 1 and mp[i-1][0] == currM: i -= 1 heappush(prices, mp[i][1]) already = i + bribed for k in range(max(0, currM - already)): cost += heappop(prices) bribed += 1 i -= 1 print(cost)
The only difference between easy and hard versions is constraints. Now elections are held in Berland and you want to win them. More precisely, you want everyone to vote for you. There are $n$ voters, and two ways to convince each of them to vote for you. The first way to convince the $i$-th voter is to pay him $p_i$ coins. The second way is to make $m_i$ other voters vote for you, and the $i$-th voter will vote for free. Moreover, the process of such voting takes place in several steps. For example, if there are five voters with $m_1 = 1$, $m_2 = 2$, $m_3 = 2$, $m_4 = 4$, $m_5 = 5$, then you can buy the vote of the fifth voter, and eventually everyone will vote for you. Set of people voting for you will change as follows: ${5} \rightarrow {1, 5} \rightarrow {1, 2, 3, 5} \rightarrow {1, 2, 3, 4, 5}$. Calculate the minimum number of coins you have to spend so that everyone votes for you. -----Input----- The first line contains one integer $t$ ($1 \le t \le 5000$) β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 5000$) β€” the number of voters. The next $n$ lines contains the description of voters. $i$-th line contains two integers $m_i$ and $p_i$ ($1 \le p_i \le 10^9, 0 \le m_i < n$). It is guaranteed that the sum of all $n$ over all test cases does not exceed $5000$. -----Output----- For each test case print one integer β€” the minimum number of coins you have to spend so that everyone votes for you. -----Example----- Input 3 3 1 5 2 10 2 8 7 0 1 3 1 1 1 6 1 1 1 4 1 4 1 6 2 6 2 3 2 8 2 7 4 4 5 5 Output 8 0 7 -----Note----- In the first test case you have to buy vote of the third voter. Then the set of people voting for you will change as follows: ${3} \rightarrow {1, 3} \rightarrow {1, 2, 3}$. In the second example you don't need to buy votes. The set of people voting for you will change as follows: ${1} \rightarrow {1, 3, 5} \rightarrow {1, 2, 3, 5} \rightarrow {1, 2, 3, 5, 6, 7} \rightarrow {1, 2, 3, 4, 5, 6, 7}$. In the third test case you have to buy votes of the second and the fifth voters. Then the set of people voting for you will change as follows: ${2, 5} \rightarrow {1, 2, 3, 4, 5} \rightarrow {1, 2, 3, 4, 5, 6}$.
import sys input = sys.stdin.readline import heapq as hq t = int(input()) for _ in range(t): n = int(input()) vt = [list(map(int,input().split())) for i in range(n)] vt.sort(reverse=True) q = [] hq.heapify(q) ans = 0 cnt = 0 for i in range(n): hq.heappush(q,vt[i][1]) if vt[i][0] >= n-i+cnt: ans += hq.heappop(q) cnt += 1 print(ans)
The only difference between easy and hard versions is constraints. Now elections are held in Berland and you want to win them. More precisely, you want everyone to vote for you. There are $n$ voters, and two ways to convince each of them to vote for you. The first way to convince the $i$-th voter is to pay him $p_i$ coins. The second way is to make $m_i$ other voters vote for you, and the $i$-th voter will vote for free. Moreover, the process of such voting takes place in several steps. For example, if there are five voters with $m_1 = 1$, $m_2 = 2$, $m_3 = 2$, $m_4 = 4$, $m_5 = 5$, then you can buy the vote of the fifth voter, and eventually everyone will vote for you. Set of people voting for you will change as follows: ${5} \rightarrow {1, 5} \rightarrow {1, 2, 3, 5} \rightarrow {1, 2, 3, 4, 5}$. Calculate the minimum number of coins you have to spend so that everyone votes for you. -----Input----- The first line contains one integer $t$ ($1 \le t \le 5000$) β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 5000$) β€” the number of voters. The next $n$ lines contains the description of voters. $i$-th line contains two integers $m_i$ and $p_i$ ($1 \le p_i \le 10^9, 0 \le m_i < n$). It is guaranteed that the sum of all $n$ over all test cases does not exceed $5000$. -----Output----- For each test case print one integer β€” the minimum number of coins you have to spend so that everyone votes for you. -----Example----- Input 3 3 1 5 2 10 2 8 7 0 1 3 1 1 1 6 1 1 1 4 1 4 1 6 2 6 2 3 2 8 2 7 4 4 5 5 Output 8 0 7 -----Note----- In the first test case you have to buy vote of the third voter. Then the set of people voting for you will change as follows: ${3} \rightarrow {1, 3} \rightarrow {1, 2, 3}$. In the second example you don't need to buy votes. The set of people voting for you will change as follows: ${1} \rightarrow {1, 3, 5} \rightarrow {1, 2, 3, 5} \rightarrow {1, 2, 3, 5, 6, 7} \rightarrow {1, 2, 3, 4, 5, 6, 7}$. In the third test case you have to buy votes of the second and the fifth voters. Then the set of people voting for you will change as follows: ${2, 5} \rightarrow {1, 2, 3, 4, 5} \rightarrow {1, 2, 3, 4, 5, 6}$.
import sys import heapq as hq readline = sys.stdin.readline read = sys.stdin.read ns = lambda: readline().rstrip() ni = lambda: int(readline().rstrip()) nm = lambda: map(int, readline().split()) nl = lambda: list(map(int, readline().split())) prn = lambda x: print(*x, sep='\n') def solve(): n = ni() vot = [tuple(nm()) for _ in range(n)] vot.sort(key = lambda x: (-x[0], x[1])) q = list() c = 0 cost = 0 for i in range(n): hq.heappush(q, vot[i][1]) while n - i - 1 + c < vot[i][0]: cost += hq.heappop(q) c += 1 print(cost) return # solve() T = ni() for _ in range(T): solve()
The only difference between easy and hard versions is constraints. Now elections are held in Berland and you want to win them. More precisely, you want everyone to vote for you. There are $n$ voters, and two ways to convince each of them to vote for you. The first way to convince the $i$-th voter is to pay him $p_i$ coins. The second way is to make $m_i$ other voters vote for you, and the $i$-th voter will vote for free. Moreover, the process of such voting takes place in several steps. For example, if there are five voters with $m_1 = 1$, $m_2 = 2$, $m_3 = 2$, $m_4 = 4$, $m_5 = 5$, then you can buy the vote of the fifth voter, and eventually everyone will vote for you. Set of people voting for you will change as follows: ${5} \rightarrow {1, 5} \rightarrow {1, 2, 3, 5} \rightarrow {1, 2, 3, 4, 5}$. Calculate the minimum number of coins you have to spend so that everyone votes for you. -----Input----- The first line contains one integer $t$ ($1 \le t \le 5000$) β€” the number of test cases. The first line of each test case contains one integer $n$ ($1 \le n \le 5000$) β€” the number of voters. The next $n$ lines contains the description of voters. $i$-th line contains two integers $m_i$ and $p_i$ ($1 \le p_i \le 10^9, 0 \le m_i < n$). It is guaranteed that the sum of all $n$ over all test cases does not exceed $5000$. -----Output----- For each test case print one integer β€” the minimum number of coins you have to spend so that everyone votes for you. -----Example----- Input 3 3 1 5 2 10 2 8 7 0 1 3 1 1 1 6 1 1 1 4 1 4 1 6 2 6 2 3 2 8 2 7 4 4 5 5 Output 8 0 7 -----Note----- In the first test case you have to buy vote of the third voter. Then the set of people voting for you will change as follows: ${3} \rightarrow {1, 3} \rightarrow {1, 2, 3}$. In the second example you don't need to buy votes. The set of people voting for you will change as follows: ${1} \rightarrow {1, 3, 5} \rightarrow {1, 2, 3, 5} \rightarrow {1, 2, 3, 5, 6, 7} \rightarrow {1, 2, 3, 4, 5, 6, 7}$. In the third test case you have to buy votes of the second and the fifth voters. Then the set of people voting for you will change as follows: ${2, 5} \rightarrow {1, 2, 3, 4, 5} \rightarrow {1, 2, 3, 4, 5, 6}$.
import sys from heapq import * #sys.stdin = open('in', 'r') t = int(input()) for ti in range(t): n = int(input()) a = [] for i in range(n): mi, pi = list(map(int, input().split())) a.append((mi, -pi)) a.sort() c = 0 h = [] res = 0 for i in reversed(list(range(n))): heappush(h, -a[i][1]) while c + i < a[i][0]: res += heappop(h) c += 1 print(res) #sys.stdout.write('YES\n') #sys.stdout.write(f'{res}\n') #sys.stdout.write(f'{y1} {x1} {y2} {x2}\n')
Try guessing the statement from this picture: $3$ You are given a non-negative integer $d$. You have to find two non-negative real numbers $a$ and $b$ such that $a + b = d$ and $a \cdot b = d$. -----Input----- The first line contains $t$ ($1 \le t \le 10^3$) β€” the number of test cases. Each test case contains one integer $d$ $(0 \le d \le 10^3)$. -----Output----- For each test print one line. If there is an answer for the $i$-th test, print "Y", and then the numbers $a$ and $b$. If there is no answer for the $i$-th test, print "N". Your answer will be considered correct if $|(a + b) - a \cdot b| \le 10^{-6}$ and $|(a + b) - d| \le 10^{-6}$. -----Example----- Input 7 69 0 1 4 5 999 1000 Output Y 67.985071301 1.014928699 Y 0.000000000 0.000000000 N Y 2.000000000 2.000000000 Y 3.618033989 1.381966011 Y 997.998996990 1.001003010 Y 998.998997995 1.001002005
for _ in range(int(input())): d=int(input()) anws=False if d**2>=4*d: root=(d**2-4*d)**0.5 a=(d+root)/2 b=(d-root)/2 anws=True if anws: print("Y {:.9f} {:.9f}".format(a,b)) else: print("N")
Try guessing the statement from this picture: $3$ You are given a non-negative integer $d$. You have to find two non-negative real numbers $a$ and $b$ such that $a + b = d$ and $a \cdot b = d$. -----Input----- The first line contains $t$ ($1 \le t \le 10^3$) β€” the number of test cases. Each test case contains one integer $d$ $(0 \le d \le 10^3)$. -----Output----- For each test print one line. If there is an answer for the $i$-th test, print "Y", and then the numbers $a$ and $b$. If there is no answer for the $i$-th test, print "N". Your answer will be considered correct if $|(a + b) - a \cdot b| \le 10^{-6}$ and $|(a + b) - d| \le 10^{-6}$. -----Example----- Input 7 69 0 1 4 5 999 1000 Output Y 67.985071301 1.014928699 Y 0.000000000 0.000000000 N Y 2.000000000 2.000000000 Y 3.618033989 1.381966011 Y 997.998996990 1.001003010 Y 998.998997995 1.001002005
n=int(input()) for i in range(n): d=int(input()) #b**2-bd+d=0 D=d**2-4*d if D>=0: b1=(d+D**0.5)/2 b2=(d-D**0.5)/2 if D<0 or (b1<0 and b2<0): print("N") else: a1=d-b1 a2=d-b2 if a1>=0 and b1>=0: print("Y", "%.9f"%a1, "%.9f"%b1) elif a2>=0 and b2>=0: print("Y", "%.9f"%a2, "%.9f"%b2) else: print("N")
We are committed to the well being of all participants. Therefore, instead of the problem, we suggest you enjoy a piece of cake. Uh oh. Somebody cut the cake. We told them to wait for you, but they did it anyway. There is still some left, though, if you hurry back. Of course, before you taste the cake, you thought about how the cake was cut. It is known that the cake was originally a regular $n$-sided polygon, each vertex of which had a unique number from $1$ to $n$. The vertices were numbered in random order. Each piece of the cake is a triangle. The cake was cut into $n - 2$ pieces as follows: each time one cut was made with a knife (from one vertex to another) such that exactly one triangular piece was separated from the current cake, and the rest continued to be a convex polygon. In other words, each time three consecutive vertices of the polygon were selected and the corresponding triangle was cut off. A possible process of cutting the cake is presented in the picture below. [Image] Example of 6-sided cake slicing. You are given a set of $n-2$ triangular pieces in random order. The vertices of each piece are given in random order β€” clockwise or counterclockwise. Each piece is defined by three numbers β€” the numbers of the corresponding $n$-sided cake vertices. For example, for the situation in the picture above, you could be given a set of pieces: $[3, 6, 5], [5, 2, 4], [5, 4, 6], [6, 3, 1]$. You are interested in two questions. What was the enumeration of the $n$-sided cake vertices? In what order were the pieces cut? Formally, you have to find two permutations $p_1, p_2, \dots, p_n$ ($1 \le p_i \le n$) and $q_1, q_2, \dots, q_{n - 2}$ ($1 \le q_i \le n - 2$) such that if the cake vertices are numbered with the numbers $p_1, p_2, \dots, p_n$ in order clockwise or counterclockwise, then when cutting pieces of the cake in the order $q_1, q_2, \dots, q_{n - 2}$ always cuts off a triangular piece so that the remaining part forms one convex polygon. For example, in the picture above the answer permutations could be: $p=[2, 4, 6, 1, 3, 5]$ (or any of its cyclic shifts, or its reversal and after that any cyclic shift) and $q=[2, 4, 1, 3]$. Write a program that, based on the given triangular pieces, finds any suitable permutations $p$ and $q$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. Then there are $t$ independent sets of input data. The first line of each set consists of a single integer $n$ ($3 \le n \le 10^5$)Β β€” the number of vertices in the cake. The following $n - 2$ lines describe the numbers of the pieces vertices: each line consists of three different integers $a, b, c$ ($1 \le a, b, c \le n$)Β β€” the numbers of the pieces vertices of cake given in random order. The pieces are given in random order. It is guaranteed that the answer to each of the tests exists. It is also guaranteed that the sum of $n$ for all test cases does not exceed $10^5$. -----Output----- Print $2t$ lines β€” answers to given $t$ test cases in the order in which they are written in the input. Each answer should consist of $2$ lines. In the first line of an answer on a test case print $n$ distinct numbers $p_1, p_2, \dots, p_n$($1 \le p_i \le n$)Β β€” the numbers of the cake vertices in clockwise or counterclockwise order. In the second line of an answer on a test case print $n - 2$ distinct numbers $q_1, q_2, \dots, q_{n - 2}$($1 \le q_i \le n - 2$)Β β€” the order of cutting pieces of the cake. The number of a piece of the cake corresponds to its number in the input. If there are several answers, print any. It is guaranteed that the answer to each of the tests exists. -----Example----- Input 3 6 3 6 5 5 2 4 5 4 6 6 3 1 6 2 5 6 2 5 1 4 1 2 1 3 5 3 1 2 3 Output 1 6 4 2 5 3 4 2 3 1 1 4 2 6 5 3 3 4 2 1 1 3 2 1
import os from io import BytesIO import sys import threading sys.setrecursionlimit(10 ** 9) threading.stack_size(67108864) def main(): # input = BytesIO(os.read(0, os.fstat(0).st_size)).readline def ad(i, j): nonlocal g if j in g[i]: g[i].remove(j) g[j].remove(i) else: g[i].add(j) g[j].add(i) def dfs(v): nonlocal used, g, nans used[v] = True nans.append(v + 1) for el in g[v]: if not used[el]: dfs(el) for _ in range(int(input())): n = int(input()) cnt = [set() for i in range(n)] g = [set() for i in range(n)] used = [False] * n triangles = [] for i in range(n - 2): a, b, c = map(int, input().split()) a -= 1 b -= 1 c -= 1 cnt[a].add(i) cnt[b].add(i) cnt[c].add(i) triangles.append((a, b, c)) ad(a, b) ad(b, c) ad(a, c) q = [] ones = [] for i in range(n): if len(cnt[i]) == 1: ones.append(i) ans = [] nans = [] for i in range(n - 2): t = ones.pop() ind = cnt[t].pop() ans.append(ind + 1) cnt[triangles[ind][0]].discard(ind) cnt[triangles[ind][1]].discard(ind) cnt[triangles[ind][2]].discard(ind) if len(cnt[triangles[ind][0]]) == 1: ones.append(triangles[ind][0]) if len(cnt[triangles[ind][1]]) == 1: ones.append(triangles[ind][1]) if len(cnt[triangles[ind][2]]) == 1: ones.append(triangles[ind][2]) dfs(0) print(*nans) print(*ans) tt = threading.Thread(target = main) tt.start()
We are committed to the well being of all participants. Therefore, instead of the problem, we suggest you enjoy a piece of cake. Uh oh. Somebody cut the cake. We told them to wait for you, but they did it anyway. There is still some left, though, if you hurry back. Of course, before you taste the cake, you thought about how the cake was cut. It is known that the cake was originally a regular $n$-sided polygon, each vertex of which had a unique number from $1$ to $n$. The vertices were numbered in random order. Each piece of the cake is a triangle. The cake was cut into $n - 2$ pieces as follows: each time one cut was made with a knife (from one vertex to another) such that exactly one triangular piece was separated from the current cake, and the rest continued to be a convex polygon. In other words, each time three consecutive vertices of the polygon were selected and the corresponding triangle was cut off. A possible process of cutting the cake is presented in the picture below. [Image] Example of 6-sided cake slicing. You are given a set of $n-2$ triangular pieces in random order. The vertices of each piece are given in random order β€” clockwise or counterclockwise. Each piece is defined by three numbers β€” the numbers of the corresponding $n$-sided cake vertices. For example, for the situation in the picture above, you could be given a set of pieces: $[3, 6, 5], [5, 2, 4], [5, 4, 6], [6, 3, 1]$. You are interested in two questions. What was the enumeration of the $n$-sided cake vertices? In what order were the pieces cut? Formally, you have to find two permutations $p_1, p_2, \dots, p_n$ ($1 \le p_i \le n$) and $q_1, q_2, \dots, q_{n - 2}$ ($1 \le q_i \le n - 2$) such that if the cake vertices are numbered with the numbers $p_1, p_2, \dots, p_n$ in order clockwise or counterclockwise, then when cutting pieces of the cake in the order $q_1, q_2, \dots, q_{n - 2}$ always cuts off a triangular piece so that the remaining part forms one convex polygon. For example, in the picture above the answer permutations could be: $p=[2, 4, 6, 1, 3, 5]$ (or any of its cyclic shifts, or its reversal and after that any cyclic shift) and $q=[2, 4, 1, 3]$. Write a program that, based on the given triangular pieces, finds any suitable permutations $p$ and $q$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. Then there are $t$ independent sets of input data. The first line of each set consists of a single integer $n$ ($3 \le n \le 10^5$)Β β€” the number of vertices in the cake. The following $n - 2$ lines describe the numbers of the pieces vertices: each line consists of three different integers $a, b, c$ ($1 \le a, b, c \le n$)Β β€” the numbers of the pieces vertices of cake given in random order. The pieces are given in random order. It is guaranteed that the answer to each of the tests exists. It is also guaranteed that the sum of $n$ for all test cases does not exceed $10^5$. -----Output----- Print $2t$ lines β€” answers to given $t$ test cases in the order in which they are written in the input. Each answer should consist of $2$ lines. In the first line of an answer on a test case print $n$ distinct numbers $p_1, p_2, \dots, p_n$($1 \le p_i \le n$)Β β€” the numbers of the cake vertices in clockwise or counterclockwise order. In the second line of an answer on a test case print $n - 2$ distinct numbers $q_1, q_2, \dots, q_{n - 2}$($1 \le q_i \le n - 2$)Β β€” the order of cutting pieces of the cake. The number of a piece of the cake corresponds to its number in the input. If there are several answers, print any. It is guaranteed that the answer to each of the tests exists. -----Example----- Input 3 6 3 6 5 5 2 4 5 4 6 6 3 1 6 2 5 6 2 5 1 4 1 2 1 3 5 3 1 2 3 Output 1 6 4 2 5 3 4 2 3 1 1 4 2 6 5 3 3 4 2 1 1 3 2 1
import sys from heapq import heappush, heappop from collections import Counter, defaultdict # inf = open('input.txt', 'r') # reader = (map(int, line.split()) for line in inf) reader = (list(map(int, line.split())) for line in sys.stdin) def insert(pq, value, entry_finder, push_id): entry = [value, push_id] entry_finder[push_id] = entry heappush(pq, entry) def remove(entry_finder, push_id): entry = entry_finder.pop(push_id) entry[-1] = -1 def extract_min(pq, entry_finder): while pq: value, push_id = heappop(pq) if push_id > 0: del entry_finder[push_id] return (push_id, value) return (-1, '*') t, = next(reader) for test in range(t): n, = next(reader) pq = [] entry_finder = {} triangle = [tuple(next(reader)) for _ in range(n-2)] deg = Counter() v_tri = defaultdict(list) used = set() for i, tri in enumerate(triangle): for v in tri: deg[v] += 1 v_tri[v].append(i) for v, value in list(deg.items()): insert(pq, value, entry_finder, push_id=v) g = [set() for _ in range(n+1)] ansQ = [] for _ in range(n-2): v, value = extract_min(pq, entry_finder) while True: i = v_tri[v].pop() if i not in used: break used.add(i) ansQ.append(i+1) tri = triangle[i] tos = [to for to in tri if to != v] for to in tos: if to in g[v]: g[v].remove(to) g[to].remove(v) else: g[v].add(to) g[to].add(v) deg[to] -= 1 remove(entry_finder, push_id=to) insert(pq, deg[to], entry_finder, push_id=to) to1, to2 = tos if to1 in g[to2]: g[to1].remove(to2) g[to2].remove(to1) else: g[to1].add(to2) g[to2].add(to1) ansP = [] visited = [False] * (n+1) s = 1 stack = [s] # print(g) while stack: v = stack.pop() if not visited[v]: visited[v] = True ansP.append(v) for to in g[v]: stack.append(to) print(*ansP) print(*ansQ) # inf.close()
We are committed to the well being of all participants. Therefore, instead of the problem, we suggest you enjoy a piece of cake. Uh oh. Somebody cut the cake. We told them to wait for you, but they did it anyway. There is still some left, though, if you hurry back. Of course, before you taste the cake, you thought about how the cake was cut. It is known that the cake was originally a regular $n$-sided polygon, each vertex of which had a unique number from $1$ to $n$. The vertices were numbered in random order. Each piece of the cake is a triangle. The cake was cut into $n - 2$ pieces as follows: each time one cut was made with a knife (from one vertex to another) such that exactly one triangular piece was separated from the current cake, and the rest continued to be a convex polygon. In other words, each time three consecutive vertices of the polygon were selected and the corresponding triangle was cut off. A possible process of cutting the cake is presented in the picture below. [Image] Example of 6-sided cake slicing. You are given a set of $n-2$ triangular pieces in random order. The vertices of each piece are given in random order β€” clockwise or counterclockwise. Each piece is defined by three numbers β€” the numbers of the corresponding $n$-sided cake vertices. For example, for the situation in the picture above, you could be given a set of pieces: $[3, 6, 5], [5, 2, 4], [5, 4, 6], [6, 3, 1]$. You are interested in two questions. What was the enumeration of the $n$-sided cake vertices? In what order were the pieces cut? Formally, you have to find two permutations $p_1, p_2, \dots, p_n$ ($1 \le p_i \le n$) and $q_1, q_2, \dots, q_{n - 2}$ ($1 \le q_i \le n - 2$) such that if the cake vertices are numbered with the numbers $p_1, p_2, \dots, p_n$ in order clockwise or counterclockwise, then when cutting pieces of the cake in the order $q_1, q_2, \dots, q_{n - 2}$ always cuts off a triangular piece so that the remaining part forms one convex polygon. For example, in the picture above the answer permutations could be: $p=[2, 4, 6, 1, 3, 5]$ (or any of its cyclic shifts, or its reversal and after that any cyclic shift) and $q=[2, 4, 1, 3]$. Write a program that, based on the given triangular pieces, finds any suitable permutations $p$ and $q$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. Then there are $t$ independent sets of input data. The first line of each set consists of a single integer $n$ ($3 \le n \le 10^5$)Β β€” the number of vertices in the cake. The following $n - 2$ lines describe the numbers of the pieces vertices: each line consists of three different integers $a, b, c$ ($1 \le a, b, c \le n$)Β β€” the numbers of the pieces vertices of cake given in random order. The pieces are given in random order. It is guaranteed that the answer to each of the tests exists. It is also guaranteed that the sum of $n$ for all test cases does not exceed $10^5$. -----Output----- Print $2t$ lines β€” answers to given $t$ test cases in the order in which they are written in the input. Each answer should consist of $2$ lines. In the first line of an answer on a test case print $n$ distinct numbers $p_1, p_2, \dots, p_n$($1 \le p_i \le n$)Β β€” the numbers of the cake vertices in clockwise or counterclockwise order. In the second line of an answer on a test case print $n - 2$ distinct numbers $q_1, q_2, \dots, q_{n - 2}$($1 \le q_i \le n - 2$)Β β€” the order of cutting pieces of the cake. The number of a piece of the cake corresponds to its number in the input. If there are several answers, print any. It is guaranteed that the answer to each of the tests exists. -----Example----- Input 3 6 3 6 5 5 2 4 5 4 6 6 3 1 6 2 5 6 2 5 1 4 1 2 1 3 5 3 1 2 3 Output 1 6 4 2 5 3 4 2 3 1 1 4 2 6 5 3 3 4 2 1 1 3 2 1
class Union: def __init__(self, n): self.p = [i for i in range(n+1)] self.rank = [0] * (n+1) def find(self, x): if self.p[x] != x: self.p[x] = self.find(self.p[x]) return self.p[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x != y: if self.rank[x] < self.rank[y]: self.p[x] = y self.rank[y] += self.rank[x] else: self.p[y] = x self.rank[x] += self.rank[y] def push(g, u, v): if u not in g: g[u] = [] if v not in g: g[v] = [] g[u].append(v) g[v].append(u) def push_c(cnt, u, i): if u not in cnt: cnt[u] = set() cnt[u].add(i) def process(cnt, tup, deg0, order, g, U, u): if len(cnt[u]) > 0: i = next(iter(cnt[u])) else: return for v in tup[i]: cnt[v].remove(i) if len(cnt[v]) == 1: deg0.append(v) v, w = None, None for x in tup[i]: if x == u: continue if v is None: v = x else: w = x order.append(i) if U.find(u) != U.find(v): U.union(u, v) push(g, u, v) if U.find(u) != U.find(w): U.union(u, w) push(g, u, w) def solve(): n = int(input()) tup = [list(map(int, input().split())) for _ in range(n-2)] g = {} cnt={} order = [] for i, [u,v,w] in enumerate(tup): push_c(cnt, u, i) push_c(cnt, v, i) push_c(cnt, w, i) U = Union(n) deg0 = [x for x, num in list(cnt.items()) if len(num) == 1] while len(deg0) > 0: u = deg0.pop() process(cnt, tup, deg0, order, g, U, u) used = [0] * (n-2) for i in order: used[i] = 1 for i, x in enumerate(used): if x == 0: order.append(i) circle=[] used = [0] * (n+1) for u in g: if len(g[u]) == 1: circle.append(u) used[u]=1 break i=0 while i<len(circle): u=circle[i] for v in g[u]: if used[v]==0: used[v]=1 circle.append(v) i+=1 print(' '.join([str(x) for x in circle])) print(' '.join([str(x+1) for x in order])) for _ in range(int(input())): solve()
We are committed to the well being of all participants. Therefore, instead of the problem, we suggest you enjoy a piece of cake. Uh oh. Somebody cut the cake. We told them to wait for you, but they did it anyway. There is still some left, though, if you hurry back. Of course, before you taste the cake, you thought about how the cake was cut. It is known that the cake was originally a regular $n$-sided polygon, each vertex of which had a unique number from $1$ to $n$. The vertices were numbered in random order. Each piece of the cake is a triangle. The cake was cut into $n - 2$ pieces as follows: each time one cut was made with a knife (from one vertex to another) such that exactly one triangular piece was separated from the current cake, and the rest continued to be a convex polygon. In other words, each time three consecutive vertices of the polygon were selected and the corresponding triangle was cut off. A possible process of cutting the cake is presented in the picture below. [Image] Example of 6-sided cake slicing. You are given a set of $n-2$ triangular pieces in random order. The vertices of each piece are given in random order β€” clockwise or counterclockwise. Each piece is defined by three numbers β€” the numbers of the corresponding $n$-sided cake vertices. For example, for the situation in the picture above, you could be given a set of pieces: $[3, 6, 5], [5, 2, 4], [5, 4, 6], [6, 3, 1]$. You are interested in two questions. What was the enumeration of the $n$-sided cake vertices? In what order were the pieces cut? Formally, you have to find two permutations $p_1, p_2, \dots, p_n$ ($1 \le p_i \le n$) and $q_1, q_2, \dots, q_{n - 2}$ ($1 \le q_i \le n - 2$) such that if the cake vertices are numbered with the numbers $p_1, p_2, \dots, p_n$ in order clockwise or counterclockwise, then when cutting pieces of the cake in the order $q_1, q_2, \dots, q_{n - 2}$ always cuts off a triangular piece so that the remaining part forms one convex polygon. For example, in the picture above the answer permutations could be: $p=[2, 4, 6, 1, 3, 5]$ (or any of its cyclic shifts, or its reversal and after that any cyclic shift) and $q=[2, 4, 1, 3]$. Write a program that, based on the given triangular pieces, finds any suitable permutations $p$ and $q$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. Then there are $t$ independent sets of input data. The first line of each set consists of a single integer $n$ ($3 \le n \le 10^5$)Β β€” the number of vertices in the cake. The following $n - 2$ lines describe the numbers of the pieces vertices: each line consists of three different integers $a, b, c$ ($1 \le a, b, c \le n$)Β β€” the numbers of the pieces vertices of cake given in random order. The pieces are given in random order. It is guaranteed that the answer to each of the tests exists. It is also guaranteed that the sum of $n$ for all test cases does not exceed $10^5$. -----Output----- Print $2t$ lines β€” answers to given $t$ test cases in the order in which they are written in the input. Each answer should consist of $2$ lines. In the first line of an answer on a test case print $n$ distinct numbers $p_1, p_2, \dots, p_n$($1 \le p_i \le n$)Β β€” the numbers of the cake vertices in clockwise or counterclockwise order. In the second line of an answer on a test case print $n - 2$ distinct numbers $q_1, q_2, \dots, q_{n - 2}$($1 \le q_i \le n - 2$)Β β€” the order of cutting pieces of the cake. The number of a piece of the cake corresponds to its number in the input. If there are several answers, print any. It is guaranteed that the answer to each of the tests exists. -----Example----- Input 3 6 3 6 5 5 2 4 5 4 6 6 3 1 6 2 5 6 2 5 1 4 1 2 1 3 5 3 1 2 3 Output 1 6 4 2 5 3 4 2 3 1 1 4 2 6 5 3 3 4 2 1 1 3 2 1
def get_edge(vertex1, vertex2): return (vertex1, vertex2) if vertex1 < vertex2 else (vertex2, vertex1) def swap(arr, i, j): temp = arr[i] arr[i] = arr[j] arr[j] = temp def __starting_point(): T = int(input()) for t in range(T): n = int(input()) pieces = [] for c in range(n-2): inp = input().rstrip().split(" ") pieces.append([int(inp[0]), int(inp[1]), int(inp[2])]) # Preparing the graph G = {} piece_index = 0 while piece_index < len(pieces): for vertex in pieces[piece_index]: if vertex not in G: G[vertex] = {} G[vertex][piece_index] = True piece_index += 1 # prepare list of vertices associated with only one piece # That piece can be safely removed next_vertices = [] for vertex in G: if len(G[vertex]) == 1: next_vertices.append(vertex) q = [] border_edges = {} non_border_edges = {} while len(next_vertices) > 0: v = next_vertices.pop() if len(G[v]) > 0: piece_index = list(G[v].keys()).pop() q.append(str(piece_index+1)) piece = pieces[piece_index] G.pop(v) for vertex_index in range(3): vertex = piece[vertex_index] if vertex != v: G[vertex].pop(piece_index) if len(G[vertex]) == 1: next_vertices.append(vertex) edge = get_edge(v, vertex) if edge not in non_border_edges: border_edges[edge] = True else: swap(piece, 0, vertex_index) edge = get_edge(piece[1], piece[2]) non_border_edges[edge] = True border_edges = list(border_edges.keys()) vertices = {} for a, b in border_edges: if a not in vertices: vertices[a] = {} if b not in vertices: vertices[b] = {} vertices[a][b] = True vertices[b][a] = True start = None start_val = 5000000000 for vertex in vertices: if len(vertices[vertex]) < start_val: start = vertex start_val = len(vertices[vertex]) v = start p = [] while len(p) < n: p.append(str(v)) assert len(vertices[v]) <= 1 if len(vertices[v]) == 1: neighbor = list(vertices[v].keys()).pop() vertices[neighbor].pop(v) v = neighbor print(" ".join(p)) print(" ".join(q)) __starting_point()
We are committed to the well being of all participants. Therefore, instead of the problem, we suggest you enjoy a piece of cake. Uh oh. Somebody cut the cake. We told them to wait for you, but they did it anyway. There is still some left, though, if you hurry back. Of course, before you taste the cake, you thought about how the cake was cut. It is known that the cake was originally a regular $n$-sided polygon, each vertex of which had a unique number from $1$ to $n$. The vertices were numbered in random order. Each piece of the cake is a triangle. The cake was cut into $n - 2$ pieces as follows: each time one cut was made with a knife (from one vertex to another) such that exactly one triangular piece was separated from the current cake, and the rest continued to be a convex polygon. In other words, each time three consecutive vertices of the polygon were selected and the corresponding triangle was cut off. A possible process of cutting the cake is presented in the picture below. [Image] Example of 6-sided cake slicing. You are given a set of $n-2$ triangular pieces in random order. The vertices of each piece are given in random order β€” clockwise or counterclockwise. Each piece is defined by three numbers β€” the numbers of the corresponding $n$-sided cake vertices. For example, for the situation in the picture above, you could be given a set of pieces: $[3, 6, 5], [5, 2, 4], [5, 4, 6], [6, 3, 1]$. You are interested in two questions. What was the enumeration of the $n$-sided cake vertices? In what order were the pieces cut? Formally, you have to find two permutations $p_1, p_2, \dots, p_n$ ($1 \le p_i \le n$) and $q_1, q_2, \dots, q_{n - 2}$ ($1 \le q_i \le n - 2$) such that if the cake vertices are numbered with the numbers $p_1, p_2, \dots, p_n$ in order clockwise or counterclockwise, then when cutting pieces of the cake in the order $q_1, q_2, \dots, q_{n - 2}$ always cuts off a triangular piece so that the remaining part forms one convex polygon. For example, in the picture above the answer permutations could be: $p=[2, 4, 6, 1, 3, 5]$ (or any of its cyclic shifts, or its reversal and after that any cyclic shift) and $q=[2, 4, 1, 3]$. Write a program that, based on the given triangular pieces, finds any suitable permutations $p$ and $q$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. Then there are $t$ independent sets of input data. The first line of each set consists of a single integer $n$ ($3 \le n \le 10^5$)Β β€” the number of vertices in the cake. The following $n - 2$ lines describe the numbers of the pieces vertices: each line consists of three different integers $a, b, c$ ($1 \le a, b, c \le n$)Β β€” the numbers of the pieces vertices of cake given in random order. The pieces are given in random order. It is guaranteed that the answer to each of the tests exists. It is also guaranteed that the sum of $n$ for all test cases does not exceed $10^5$. -----Output----- Print $2t$ lines β€” answers to given $t$ test cases in the order in which they are written in the input. Each answer should consist of $2$ lines. In the first line of an answer on a test case print $n$ distinct numbers $p_1, p_2, \dots, p_n$($1 \le p_i \le n$)Β β€” the numbers of the cake vertices in clockwise or counterclockwise order. In the second line of an answer on a test case print $n - 2$ distinct numbers $q_1, q_2, \dots, q_{n - 2}$($1 \le q_i \le n - 2$)Β β€” the order of cutting pieces of the cake. The number of a piece of the cake corresponds to its number in the input. If there are several answers, print any. It is guaranteed that the answer to each of the tests exists. -----Example----- Input 3 6 3 6 5 5 2 4 5 4 6 6 3 1 6 2 5 6 2 5 1 4 1 2 1 3 5 3 1 2 3 Output 1 6 4 2 5 3 4 2 3 1 1 4 2 6 5 3 3 4 2 1 1 3 2 1
import heapq t = int(input()) for _ in range(t): n = int(input()) counts = [0] * n triangles = [set() for _ in range(n)] assign_order = {} for i in range(n - 2): a, b, c = [x - 1 for x in list(map(int, input().split()))] t = (a, b, c) assign_order[t] = i for x in t: counts[x] += 1 triangles[x].add(t) not_edges = set() edges = set() order = [] que = [i for i in range(n) if counts[i] == 1] index = 0 while index < n - 2: curr = que[index] tt = triangles[curr].pop() # should remain one order.append(assign_order[tt]) t = set(tt) t.remove(curr) a, b = t.pop(), t.pop() for e in (curr, a), (curr, b): if e not in not_edges: edges.add(e) if index < n - 3: not_edges.add((a, b)) not_edges.add((b, a)) else: if (a, b) not in not_edges: edges.add((a, b)) for x in a, b: counts[x] -= 1 if counts[x] == 1: que.append(x) triangles[x].remove(tt) index += 1 e = [[] for _ in range(n)] for a, b in edges: e[a].append(b) e[b].append(a) visited = [False] * n a = 0 answer = [] for i in range(n): visited[a] = True answer.append(a) for b in e[a]: if not visited[b]: a = b break print(' '.join(map(str, [x + 1 for x in answer]))) print(' '.join(map(str, [x + 1 for x in order])))
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$ The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece. Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle. -----Input----- The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β€” the number of test cases. Next $t$ lines contain descriptions of test cases. Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$). -----Output----- For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower). -----Example----- Input 3 1 3 100000 100000 2 2 Output YES NO YES -----Note----- For the first test case, this is an example solution: [Image] For the second test case, we can show that no solution exists. For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
for _ in range(int(input())): n, m = list(map(int, input().split())) if n < m: n, m = m, n # n > m if m == 1: print("YES") continue if m == 2 and n == 2: print("YES") continue print("NO")
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$ The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece. Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle. -----Input----- The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β€” the number of test cases. Next $t$ lines contain descriptions of test cases. Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$). -----Output----- For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower). -----Example----- Input 3 1 3 100000 100000 2 2 Output YES NO YES -----Note----- For the first test case, this is an example solution: [Image] For the second test case, we can show that no solution exists. For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
for zz in range(int(input())): n, m = list(map(int, input().split())) if n == 1 or m == 1 or (n <= 2 and m <= 2): print('YES') else: print('NO')
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$ The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece. Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle. -----Input----- The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β€” the number of test cases. Next $t$ lines contain descriptions of test cases. Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$). -----Output----- For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower). -----Example----- Input 3 1 3 100000 100000 2 2 Output YES NO YES -----Note----- For the first test case, this is an example solution: [Image] For the second test case, we can show that no solution exists. For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
for i in range(int(input())): a, b = list(map(int,input().split())) if a > 2 and b >= 2 or b > 2 and a >= 2: print("NO") else: print("YES")
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$ The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece. Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle. -----Input----- The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β€” the number of test cases. Next $t$ lines contain descriptions of test cases. Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$). -----Output----- For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower). -----Example----- Input 3 1 3 100000 100000 2 2 Output YES NO YES -----Note----- For the first test case, this is an example solution: [Image] For the second test case, we can show that no solution exists. For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
t=int(input()) for i in range(t): a,b=map(int,input().split()) if a==2 and b==2: print('YES') elif a==1: print('YES') elif b==1: print('YES') else: print('NO')
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$ The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece. Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle. -----Input----- The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β€” the number of test cases. Next $t$ lines contain descriptions of test cases. Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$). -----Output----- For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower). -----Example----- Input 3 1 3 100000 100000 2 2 Output YES NO YES -----Note----- For the first test case, this is an example solution: [Image] For the second test case, we can show that no solution exists. For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
for _ in range(int(input())): a, b = list(map(int, input().split())) if (a == 1 or b == 1) or (a == 2 and b == 2): print("YES") else: print("NO")
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$ The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece. Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle. -----Input----- The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β€” the number of test cases. Next $t$ lines contain descriptions of test cases. Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$). -----Output----- For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower). -----Example----- Input 3 1 3 100000 100000 2 2 Output YES NO YES -----Note----- For the first test case, this is an example solution: [Image] For the second test case, we can show that no solution exists. For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
t=int(input()) for _ in range(t): n,m=list(map(int, input().split())) print('YES' if n == 1 or m == 1 or n == 2 and m == 2 else 'NO')
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$ The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece. Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle. -----Input----- The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β€” the number of test cases. Next $t$ lines contain descriptions of test cases. Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$). -----Output----- For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower). -----Example----- Input 3 1 3 100000 100000 2 2 Output YES NO YES -----Note----- For the first test case, this is an example solution: [Image] For the second test case, we can show that no solution exists. For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
t = int(input()) for _ in range(t): n, m = list(map(int, input().split())) if n == 1 or m == 1 or (m == 2 and n == 2): print('YES') else: print('NO')
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$ The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece. Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle. -----Input----- The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β€” the number of test cases. Next $t$ lines contain descriptions of test cases. Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$). -----Output----- For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower). -----Example----- Input 3 1 3 100000 100000 2 2 Output YES NO YES -----Note----- For the first test case, this is an example solution: [Image] For the second test case, we can show that no solution exists. For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
import sys # from collections import deque # print(help(deque)) # 26 input = lambda: sys.stdin.readline().strip() ipnut = input for i in range(int(input())): n,m = map(int,ipnut().split()) if n==m==2 or min(n,m)==1: print("YES") else: print("NO") # n = int(input()) # s = list(map(int,input())) """ 10 10 11 12 13 14 15 16 17 11 11 """
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$ The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece. Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle. -----Input----- The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β€” the number of test cases. Next $t$ lines contain descriptions of test cases. Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$). -----Output----- For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower). -----Example----- Input 3 1 3 100000 100000 2 2 Output YES NO YES -----Note----- For the first test case, this is an example solution: [Image] For the second test case, we can show that no solution exists. For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
t = int(input()) for q in range(0, t): n, k = map(int, input().split()) # a = list(map(int, input().split())) # n = int(input()) # print(n) if n == k == 2: print("YES") elif n == 1 or k == 1: print("YES") else: print("NO")
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$ The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece. Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle. -----Input----- The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β€” the number of test cases. Next $t$ lines contain descriptions of test cases. Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$). -----Output----- For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower). -----Example----- Input 3 1 3 100000 100000 2 2 Output YES NO YES -----Note----- For the first test case, this is an example solution: [Image] For the second test case, we can show that no solution exists. For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
import sys ints = (int(x) for x in sys.stdin.read().split()) sys.setrecursionlimit(3000) def main(): ntc = next(ints) for tc in range(ntc): n, m = (next(ints) for i in range(2)) print('YES' if n==1 or m==1 or n==m==2 else 'NO') return main()
You are given a special jigsaw puzzle consisting of $n\cdot m$ identical pieces. Every piece has three tabs and one blank, as pictured below. $\{3$ The jigsaw puzzle is considered solved if the following conditions hold: The pieces are arranged into a grid with $n$ rows and $m$ columns. For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece. Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle. -----Input----- The test consists of multiple test cases. The first line contains a single integer $t$ ($1\le t\le 1000$)Β β€” the number of test cases. Next $t$ lines contain descriptions of test cases. Each test case contains two integers $n$ and $m$ ($1 \le n,m \le 10^5$). -----Output----- For each test case output a single line containing "YES" if it is possible to solve the jigsaw puzzle, or "NO" otherwise. You can print each letter in any case (upper or lower). -----Example----- Input 3 1 3 100000 100000 2 2 Output YES NO YES -----Note----- For the first test case, this is an example solution: [Image] For the second test case, we can show that no solution exists. For the third test case, this is an example solution: $\left\{\begin{array}{l}{3} \\{3} \end{array} \right\}$
from sys import stdin,stdout # import math # import heapq # # t = 1 # def aint(): # return int(input().strip()) # def lint(): # return list(map(int,input().split())) # def fint(): # return list(map(int,stdin.readline().split())) # # ######################################################## def main(): n,m=lint() if n==1 or m==1: print("YES") elif n==2 and m==2: print("YES") else: print("NO") #solve t=int(input()) ######################################################## for i in range(t): # #print("Case #"+str(i+1)+":",end=" ") # main() #