Datasets:

inputs
stringlengths
50
14k
targets
stringlengths
4
655k
โ€” Hey folks, how do you like this problem? โ€” That'll do it. BThero is a powerful magician. He has got $n$ piles of candies, the $i$-th pile initially contains $a_i$ candies. BThero can cast a copy-paste spell as follows: He chooses two piles $(i, j)$ such that $1 \le i, j \le n$ and $i \ne j$. All candies from pile $i$ are copied into pile $j$. Formally, the operation $a_j := a_j + a_i$ is performed. BThero can cast this spell any number of times he wants to โ€” but unfortunately, if some pile contains strictly more than $k$ candies, he loses his magic power. What is the maximum number of times BThero can cast the spell without losing his power? -----Input----- The first line contains one integer $T$ ($1 \le T \le 500$) โ€” the number of test cases. Each test case consists of two lines: the first line contains two integers $n$ and $k$ ($2 \le n \le 1000$, $2 \le k \le 10^4$); the second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le k$). It is guaranteed that the sum of $n$ over all test cases does not exceed $1000$, and the sum of $k$ over all test cases does not exceed $10^4$. -----Output----- For each test case, print one integer โ€” the maximum number of times BThero can cast the spell without losing his magic power. -----Example----- Input 3 2 2 1 1 3 5 1 2 3 3 7 3 2 2 Output 1 5 4 -----Note----- In the first test case we get either $a = [1, 2]$ or $a = [2, 1]$ after casting the spell for the first time, and it is impossible to cast it again.
import sys import random from fractions import Fraction from math import * from decimal import * def input(): return sys.stdin.readline().strip() def iinput(): return int(input()) def finput(): return float(input()) def tinput(): return input().split() def linput(): return list(input()) def rinput(): return list(map(int, tinput())) def fiinput(): return list(map(float, tinput())) def rlinput(): return list(map(int, input().split())) def trinput(): return tuple(rinput()) def srlinput(): return sorted(list(map(int, input().split()))) def NOYES(fl): if fl: print("NO") else: print("YES") def YESNO(fl): if fl: print("YES") else: print("NO") def modst(a, s): res = 1 while s: if s % 2: res =res * a %998244353 a *= a a = a % 998244353 s //= 2 return res def main(): #n = iinput() #k = iinput() #m = iinput() #n = int(sys.stdin.readline().strip()) n, k = rinput() #n, m = rinput() #m, k = rinput() #n, k, m = rinput() #n, m, k = rinput() #k, n, m = rinput() #k, m, n = rinput() #m, k, n = rinput() #m, n, k = rinput() q = srlinput() #q = linput() res = 0 for i in range(1, n): res += (k - q[i]) // q[0] print(res) for i in range(iinput()): main()
โ€” Hey folks, how do you like this problem? โ€” That'll do it. BThero is a powerful magician. He has got $n$ piles of candies, the $i$-th pile initially contains $a_i$ candies. BThero can cast a copy-paste spell as follows: He chooses two piles $(i, j)$ such that $1 \le i, j \le n$ and $i \ne j$. All candies from pile $i$ are copied into pile $j$. Formally, the operation $a_j := a_j + a_i$ is performed. BThero can cast this spell any number of times he wants to โ€” but unfortunately, if some pile contains strictly more than $k$ candies, he loses his magic power. What is the maximum number of times BThero can cast the spell without losing his power? -----Input----- The first line contains one integer $T$ ($1 \le T \le 500$) โ€” the number of test cases. Each test case consists of two lines: the first line contains two integers $n$ and $k$ ($2 \le n \le 1000$, $2 \le k \le 10^4$); the second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le k$). It is guaranteed that the sum of $n$ over all test cases does not exceed $1000$, and the sum of $k$ over all test cases does not exceed $10^4$. -----Output----- For each test case, print one integer โ€” the maximum number of times BThero can cast the spell without losing his magic power. -----Example----- Input 3 2 2 1 1 3 5 1 2 3 3 7 3 2 2 Output 1 5 4 -----Note----- In the first test case we get either $a = [1, 2]$ or $a = [2, 1]$ after casting the spell for the first time, and it is impossible to cast it again.
from sys import stdin ############################################################### def iinput(): return int(stdin.readline()) def minput(): return list(map(int, stdin.readline().split())) def linput(): return list(map(int, stdin.readline().split())) ############################################################### t = iinput() while t: t -= 1 n, k = minput() a = linput() a.sort() ans = 0 for i in range(1, n): ans += max(0, k - a[i]) // a[0] print(ans)
โ€” Hey folks, how do you like this problem? โ€” That'll do it. BThero is a powerful magician. He has got $n$ piles of candies, the $i$-th pile initially contains $a_i$ candies. BThero can cast a copy-paste spell as follows: He chooses two piles $(i, j)$ such that $1 \le i, j \le n$ and $i \ne j$. All candies from pile $i$ are copied into pile $j$. Formally, the operation $a_j := a_j + a_i$ is performed. BThero can cast this spell any number of times he wants to โ€” but unfortunately, if some pile contains strictly more than $k$ candies, he loses his magic power. What is the maximum number of times BThero can cast the spell without losing his power? -----Input----- The first line contains one integer $T$ ($1 \le T \le 500$) โ€” the number of test cases. Each test case consists of two lines: the first line contains two integers $n$ and $k$ ($2 \le n \le 1000$, $2 \le k \le 10^4$); the second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le k$). It is guaranteed that the sum of $n$ over all test cases does not exceed $1000$, and the sum of $k$ over all test cases does not exceed $10^4$. -----Output----- For each test case, print one integer โ€” the maximum number of times BThero can cast the spell without losing his magic power. -----Example----- Input 3 2 2 1 1 3 5 1 2 3 3 7 3 2 2 Output 1 5 4 -----Note----- In the first test case we get either $a = [1, 2]$ or $a = [2, 1]$ after casting the spell for the first time, and it is impossible to cast it again.
def solve(n,k): s=list(map(int,input().split())) s.sort() ans=0 p=s[0] for i in range(1,n): ans+=((k-s[i])//p) print(ans) for _ in range(int(input())): n,k=map(int,input().split()) solve(n,k)
โ€” Hey folks, how do you like this problem? โ€” That'll do it. BThero is a powerful magician. He has got $n$ piles of candies, the $i$-th pile initially contains $a_i$ candies. BThero can cast a copy-paste spell as follows: He chooses two piles $(i, j)$ such that $1 \le i, j \le n$ and $i \ne j$. All candies from pile $i$ are copied into pile $j$. Formally, the operation $a_j := a_j + a_i$ is performed. BThero can cast this spell any number of times he wants to โ€” but unfortunately, if some pile contains strictly more than $k$ candies, he loses his magic power. What is the maximum number of times BThero can cast the spell without losing his power? -----Input----- The first line contains one integer $T$ ($1 \le T \le 500$) โ€” the number of test cases. Each test case consists of two lines: the first line contains two integers $n$ and $k$ ($2 \le n \le 1000$, $2 \le k \le 10^4$); the second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le k$). It is guaranteed that the sum of $n$ over all test cases does not exceed $1000$, and the sum of $k$ over all test cases does not exceed $10^4$. -----Output----- For each test case, print one integer โ€” the maximum number of times BThero can cast the spell without losing his magic power. -----Example----- Input 3 2 2 1 1 3 5 1 2 3 3 7 3 2 2 Output 1 5 4 -----Note----- In the first test case we get either $a = [1, 2]$ or $a = [2, 1]$ after casting the spell for the first time, and it is impossible to cast it again.
for _ in range(int(input())): n, k = map(int, input().split()) a = list(map(int, input().split())) m = min(a) ind = a.index(m) ans = 0 for i in range(n): if i == ind: continue ans += (k - a[i]) // m print(ans)
โ€” Hey folks, how do you like this problem? โ€” That'll do it. BThero is a powerful magician. He has got $n$ piles of candies, the $i$-th pile initially contains $a_i$ candies. BThero can cast a copy-paste spell as follows: He chooses two piles $(i, j)$ such that $1 \le i, j \le n$ and $i \ne j$. All candies from pile $i$ are copied into pile $j$. Formally, the operation $a_j := a_j + a_i$ is performed. BThero can cast this spell any number of times he wants to โ€” but unfortunately, if some pile contains strictly more than $k$ candies, he loses his magic power. What is the maximum number of times BThero can cast the spell without losing his power? -----Input----- The first line contains one integer $T$ ($1 \le T \le 500$) โ€” the number of test cases. Each test case consists of two lines: the first line contains two integers $n$ and $k$ ($2 \le n \le 1000$, $2 \le k \le 10^4$); the second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le k$). It is guaranteed that the sum of $n$ over all test cases does not exceed $1000$, and the sum of $k$ over all test cases does not exceed $10^4$. -----Output----- For each test case, print one integer โ€” the maximum number of times BThero can cast the spell without losing his magic power. -----Example----- Input 3 2 2 1 1 3 5 1 2 3 3 7 3 2 2 Output 1 5 4 -----Note----- In the first test case we get either $a = [1, 2]$ or $a = [2, 1]$ after casting the spell for the first time, and it is impossible to cast it again.
import sys import math def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def MI(): return list(map(int, sys.stdin.readline().split())) def SI(): return sys.stdin.readline().strip() t = II() for q in range(t): n,k = MI() a = LI() a.sort() count = 0 for i in range(1,n): b = max(k-a[i],0) count+=b//a[0] print(count)
โ€” Hey folks, how do you like this problem? โ€” That'll do it. BThero is a powerful magician. He has got $n$ piles of candies, the $i$-th pile initially contains $a_i$ candies. BThero can cast a copy-paste spell as follows: He chooses two piles $(i, j)$ such that $1 \le i, j \le n$ and $i \ne j$. All candies from pile $i$ are copied into pile $j$. Formally, the operation $a_j := a_j + a_i$ is performed. BThero can cast this spell any number of times he wants to โ€” but unfortunately, if some pile contains strictly more than $k$ candies, he loses his magic power. What is the maximum number of times BThero can cast the spell without losing his power? -----Input----- The first line contains one integer $T$ ($1 \le T \le 500$) โ€” the number of test cases. Each test case consists of two lines: the first line contains two integers $n$ and $k$ ($2 \le n \le 1000$, $2 \le k \le 10^4$); the second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le k$). It is guaranteed that the sum of $n$ over all test cases does not exceed $1000$, and the sum of $k$ over all test cases does not exceed $10^4$. -----Output----- For each test case, print one integer โ€” the maximum number of times BThero can cast the spell without losing his magic power. -----Example----- Input 3 2 2 1 1 3 5 1 2 3 3 7 3 2 2 Output 1 5 4 -----Note----- In the first test case we get either $a = [1, 2]$ or $a = [2, 1]$ after casting the spell for the first time, and it is impossible to cast it again.
#Codeforces Round #673 #Problem A import sys # #BEGIN TEMPLATE # def input(): return sys.stdin.readline()[:-1] def getInt(): return int(input()) def getIntIter(): return list(map(int, input().split())) def getIntList(): return list(getIntIter()) def flush(): sys.stdout.flush() # #END TEMPLATE # for _ in range(getInt()): n,k = getIntIter() nums = getIntList() m = min(nums) ans = 0 for num in nums: ans += (k-num)//m ans -= (k-m)//m print(ans)
โ€” Hey folks, how do you like this problem? โ€” That'll do it. BThero is a powerful magician. He has got $n$ piles of candies, the $i$-th pile initially contains $a_i$ candies. BThero can cast a copy-paste spell as follows: He chooses two piles $(i, j)$ such that $1 \le i, j \le n$ and $i \ne j$. All candies from pile $i$ are copied into pile $j$. Formally, the operation $a_j := a_j + a_i$ is performed. BThero can cast this spell any number of times he wants to โ€” but unfortunately, if some pile contains strictly more than $k$ candies, he loses his magic power. What is the maximum number of times BThero can cast the spell without losing his power? -----Input----- The first line contains one integer $T$ ($1 \le T \le 500$) โ€” the number of test cases. Each test case consists of two lines: the first line contains two integers $n$ and $k$ ($2 \le n \le 1000$, $2 \le k \le 10^4$); the second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le k$). It is guaranteed that the sum of $n$ over all test cases does not exceed $1000$, and the sum of $k$ over all test cases does not exceed $10^4$. -----Output----- For each test case, print one integer โ€” the maximum number of times BThero can cast the spell without losing his magic power. -----Example----- Input 3 2 2 1 1 3 5 1 2 3 3 7 3 2 2 Output 1 5 4 -----Note----- In the first test case we get either $a = [1, 2]$ or $a = [2, 1]$ after casting the spell for the first time, and it is impossible to cast it again.
for _ in range(int(input())): n, k = list(map(int, input().split())) *arr, = list(map(int, input().split())) cnt = 0 mn = min(arr) arr.remove(mn) for v in arr: cnt += (k - v) // mn print(cnt)
โ€” Hey folks, how do you like this problem? โ€” That'll do it. BThero is a powerful magician. He has got $n$ piles of candies, the $i$-th pile initially contains $a_i$ candies. BThero can cast a copy-paste spell as follows: He chooses two piles $(i, j)$ such that $1 \le i, j \le n$ and $i \ne j$. All candies from pile $i$ are copied into pile $j$. Formally, the operation $a_j := a_j + a_i$ is performed. BThero can cast this spell any number of times he wants to โ€” but unfortunately, if some pile contains strictly more than $k$ candies, he loses his magic power. What is the maximum number of times BThero can cast the spell without losing his power? -----Input----- The first line contains one integer $T$ ($1 \le T \le 500$) โ€” the number of test cases. Each test case consists of two lines: the first line contains two integers $n$ and $k$ ($2 \le n \le 1000$, $2 \le k \le 10^4$); the second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le k$). It is guaranteed that the sum of $n$ over all test cases does not exceed $1000$, and the sum of $k$ over all test cases does not exceed $10^4$. -----Output----- For each test case, print one integer โ€” the maximum number of times BThero can cast the spell without losing his magic power. -----Example----- Input 3 2 2 1 1 3 5 1 2 3 3 7 3 2 2 Output 1 5 4 -----Note----- In the first test case we get either $a = [1, 2]$ or $a = [2, 1]$ after casting the spell for the first time, and it is impossible to cast it again.
t = int(input()) for _ in range(t): n,k = [int(x) for x in input().split()] l = [int(x) for x in input().split()] ans = 0 m = min(l) mi = l.index(m) for i in range(n): if i!=mi: ans+=max((k-l[i])//m,0) print(ans)
โ€” Hey folks, how do you like this problem? โ€” That'll do it. BThero is a powerful magician. He has got $n$ piles of candies, the $i$-th pile initially contains $a_i$ candies. BThero can cast a copy-paste spell as follows: He chooses two piles $(i, j)$ such that $1 \le i, j \le n$ and $i \ne j$. All candies from pile $i$ are copied into pile $j$. Formally, the operation $a_j := a_j + a_i$ is performed. BThero can cast this spell any number of times he wants to โ€” but unfortunately, if some pile contains strictly more than $k$ candies, he loses his magic power. What is the maximum number of times BThero can cast the spell without losing his power? -----Input----- The first line contains one integer $T$ ($1 \le T \le 500$) โ€” the number of test cases. Each test case consists of two lines: the first line contains two integers $n$ and $k$ ($2 \le n \le 1000$, $2 \le k \le 10^4$); the second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le k$). It is guaranteed that the sum of $n$ over all test cases does not exceed $1000$, and the sum of $k$ over all test cases does not exceed $10^4$. -----Output----- For each test case, print one integer โ€” the maximum number of times BThero can cast the spell without losing his magic power. -----Example----- Input 3 2 2 1 1 3 5 1 2 3 3 7 3 2 2 Output 1 5 4 -----Note----- In the first test case we get either $a = [1, 2]$ or $a = [2, 1]$ after casting the spell for the first time, and it is impossible to cast it again.
'''Author- Akshit Monga''' t=int(input()) for _ in range(t): n,k=map(int,input().split()) arr=[int(x) for x in input().split()] m=min(arr) ans=0 for i in arr: ans+=(k-i)//m print(ans-(k-m)//m)
โ€” Hey folks, how do you like this problem? โ€” That'll do it. BThero is a powerful magician. He has got $n$ piles of candies, the $i$-th pile initially contains $a_i$ candies. BThero can cast a copy-paste spell as follows: He chooses two piles $(i, j)$ such that $1 \le i, j \le n$ and $i \ne j$. All candies from pile $i$ are copied into pile $j$. Formally, the operation $a_j := a_j + a_i$ is performed. BThero can cast this spell any number of times he wants to โ€” but unfortunately, if some pile contains strictly more than $k$ candies, he loses his magic power. What is the maximum number of times BThero can cast the spell without losing his power? -----Input----- The first line contains one integer $T$ ($1 \le T \le 500$) โ€” the number of test cases. Each test case consists of two lines: the first line contains two integers $n$ and $k$ ($2 \le n \le 1000$, $2 \le k \le 10^4$); the second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le k$). It is guaranteed that the sum of $n$ over all test cases does not exceed $1000$, and the sum of $k$ over all test cases does not exceed $10^4$. -----Output----- For each test case, print one integer โ€” the maximum number of times BThero can cast the spell without losing his magic power. -----Example----- Input 3 2 2 1 1 3 5 1 2 3 3 7 3 2 2 Output 1 5 4 -----Note----- In the first test case we get either $a = [1, 2]$ or $a = [2, 1]$ after casting the spell for the first time, and it is impossible to cast it again.
from sys import stdin input = stdin.readline for _ in range(int(input())): n,x = list(map(int,input().split())) a = sorted(list(map(int,input().split())),reverse=True) g = a.pop() ans = 0 for i in a: ans += max(0,(x-i)//g) print(ans)
There are $n$ segments $[l_i, r_i]$ for $1 \le i \le n$. You should divide all segments into two non-empty groups in such way that there is no pair of segments from different groups which have at least one common point, or say that it's impossible to do it. Each segment should belong to exactly one group. To optimize testing process you will be given multitest. -----Input----- The first line contains one integer $T$ ($1 \le T \le 50000$) โ€” the number of queries. Each query contains description of the set of segments. Queries are independent. First line of each query contains single integer $n$ ($2 \le n \le 10^5$) โ€” number of segments. It is guaranteed that $\sum{n}$ over all queries does not exceed $10^5$. The next $n$ lines contains two integers $l_i$, $r_i$ per line ($1 \le l_i \le r_i \le 2 \cdot 10^5$) โ€” the $i$-th segment. -----Output----- For each query print $n$ integers $t_1, t_2, \dots, t_n$ ($t_i \in \{1, 2\}$) โ€” for each segment (in the same order as in the input) $t_i$ equals $1$ if the $i$-th segment will belongs to the first group and $2$ otherwise. If there are multiple answers, you can print any of them. If there is no answer, print $-1$. -----Example----- Input 3 2 5 5 2 3 3 3 5 2 3 2 3 3 3 3 4 4 5 5 Output 2 1 -1 1 1 2 -----Note----- In the first query the first and the second segments should be in different groups, but exact numbers don't matter. In the second query the third segment intersects with the first and the second segments, so they should be in the same group, but then the other group becomes empty, so answer is $-1$. In the third query we can distribute segments in any way that makes groups non-empty, so any answer of $6$ possible is correct.
t = int(input()) for ti in range(t): n = int(input()) lri = [None for _ in range(n)] for _ in range(n): li, ri = list(map(int, input().split())) lri[_] = (li, ri, _) lri.sort() t = [None for _ in range(n)] ct, t[lri[0][2]], eg = 1, 1, lri[0][1] for i in range(1, n): if lri[i][0] <= eg: t[lri[i][2]] = ct eg = max(eg, lri[i][1]) else: ct = 3 - ct t[lri[i][2]] = ct eg = lri[i][1] if all(ti == 1 for ti in t): print(-1) else: print(*t)
There are $n$ segments $[l_i, r_i]$ for $1 \le i \le n$. You should divide all segments into two non-empty groups in such way that there is no pair of segments from different groups which have at least one common point, or say that it's impossible to do it. Each segment should belong to exactly one group. To optimize testing process you will be given multitest. -----Input----- The first line contains one integer $T$ ($1 \le T \le 50000$) โ€” the number of queries. Each query contains description of the set of segments. Queries are independent. First line of each query contains single integer $n$ ($2 \le n \le 10^5$) โ€” number of segments. It is guaranteed that $\sum{n}$ over all queries does not exceed $10^5$. The next $n$ lines contains two integers $l_i$, $r_i$ per line ($1 \le l_i \le r_i \le 2 \cdot 10^5$) โ€” the $i$-th segment. -----Output----- For each query print $n$ integers $t_1, t_2, \dots, t_n$ ($t_i \in \{1, 2\}$) โ€” for each segment (in the same order as in the input) $t_i$ equals $1$ if the $i$-th segment will belongs to the first group and $2$ otherwise. If there are multiple answers, you can print any of them. If there is no answer, print $-1$. -----Example----- Input 3 2 5 5 2 3 3 3 5 2 3 2 3 3 3 3 4 4 5 5 Output 2 1 -1 1 1 2 -----Note----- In the first query the first and the second segments should be in different groups, but exact numbers don't matter. In the second query the third segment intersects with the first and the second segments, so they should be in the same group, but then the other group becomes empty, so answer is $-1$. In the third query we can distribute segments in any way that makes groups non-empty, so any answer of $6$ possible is correct.
def ii(): return int(input()) def mi(): return list(map(int, input().split())) def li(): return list(mi()) for _ in range(ii()): n = ii() a = [(li() + [i]) for i in range(n)] a.sort() ans = [2] * n pr = a[0][0] for l, r, i in a: if l > pr: break ans[i] = 1 pr = max(pr, r) if 2 in ans: print(*ans) else: print(-1)
There are $n$ segments $[l_i, r_i]$ for $1 \le i \le n$. You should divide all segments into two non-empty groups in such way that there is no pair of segments from different groups which have at least one common point, or say that it's impossible to do it. Each segment should belong to exactly one group. To optimize testing process you will be given multitest. -----Input----- The first line contains one integer $T$ ($1 \le T \le 50000$) โ€” the number of queries. Each query contains description of the set of segments. Queries are independent. First line of each query contains single integer $n$ ($2 \le n \le 10^5$) โ€” number of segments. It is guaranteed that $\sum{n}$ over all queries does not exceed $10^5$. The next $n$ lines contains two integers $l_i$, $r_i$ per line ($1 \le l_i \le r_i \le 2 \cdot 10^5$) โ€” the $i$-th segment. -----Output----- For each query print $n$ integers $t_1, t_2, \dots, t_n$ ($t_i \in \{1, 2\}$) โ€” for each segment (in the same order as in the input) $t_i$ equals $1$ if the $i$-th segment will belongs to the first group and $2$ otherwise. If there are multiple answers, you can print any of them. If there is no answer, print $-1$. -----Example----- Input 3 2 5 5 2 3 3 3 5 2 3 2 3 3 3 3 4 4 5 5 Output 2 1 -1 1 1 2 -----Note----- In the first query the first and the second segments should be in different groups, but exact numbers don't matter. In the second query the third segment intersects with the first and the second segments, so they should be in the same group, but then the other group becomes empty, so answer is $-1$. In the third query we can distribute segments in any way that makes groups non-empty, so any answer of $6$ possible is correct.
t = int(input()) for i in range(t): n = int(input()) sobs = [] for j in range(n): a, b = list(map(int, input().split())) sobs.append([[a, -1], j]) sobs.append([[b, 1], j]) sobs.sort() counts = 0 passed = [] success = [] alls = [0 for q in range(n)] succeed = False for sob in sobs: if succeed: if sob[0][1] == -1: pass else: success.append(sob[1]) continue if sob[0][1] == -1: counts += 1 else: counts -= 1 passed.append(sob[1]) if counts == 0: succeed = True if succeed and success: for a in passed: alls[a] = 1 for b in success: alls[b] = 2 print(*alls) else: print(-1)
There are $n$ segments $[l_i, r_i]$ for $1 \le i \le n$. You should divide all segments into two non-empty groups in such way that there is no pair of segments from different groups which have at least one common point, or say that it's impossible to do it. Each segment should belong to exactly one group. To optimize testing process you will be given multitest. -----Input----- The first line contains one integer $T$ ($1 \le T \le 50000$) โ€” the number of queries. Each query contains description of the set of segments. Queries are independent. First line of each query contains single integer $n$ ($2 \le n \le 10^5$) โ€” number of segments. It is guaranteed that $\sum{n}$ over all queries does not exceed $10^5$. The next $n$ lines contains two integers $l_i$, $r_i$ per line ($1 \le l_i \le r_i \le 2 \cdot 10^5$) โ€” the $i$-th segment. -----Output----- For each query print $n$ integers $t_1, t_2, \dots, t_n$ ($t_i \in \{1, 2\}$) โ€” for each segment (in the same order as in the input) $t_i$ equals $1$ if the $i$-th segment will belongs to the first group and $2$ otherwise. If there are multiple answers, you can print any of them. If there is no answer, print $-1$. -----Example----- Input 3 2 5 5 2 3 3 3 5 2 3 2 3 3 3 3 4 4 5 5 Output 2 1 -1 1 1 2 -----Note----- In the first query the first and the second segments should be in different groups, but exact numbers don't matter. In the second query the third segment intersects with the first and the second segments, so they should be in the same group, but then the other group becomes empty, so answer is $-1$. In the third query we can distribute segments in any way that makes groups non-empty, so any answer of $6$ possible is correct.
T=int(input()) for i in range(0,T): n=int(input()) L=[] dp=[0]*n for j in range(0,n): l,r=map(int,input().split()) L.append((l,r,j)) L=sorted(L) temp=-1 ed=L[0][1] for j in range(1,len(L)): if(L[j][0]>ed): temp=j break ed=max(ed,L[j][1]) if(temp==-1 or n==1): print(-1) else: for j in range(0,len(L)): if(j<temp): dp[L[j][2]]=1 else: dp[L[j][2]]=2 for j in range(0,n): print(dp[j],end=" ") print(" ")
There are $n$ segments $[l_i, r_i]$ for $1 \le i \le n$. You should divide all segments into two non-empty groups in such way that there is no pair of segments from different groups which have at least one common point, or say that it's impossible to do it. Each segment should belong to exactly one group. To optimize testing process you will be given multitest. -----Input----- The first line contains one integer $T$ ($1 \le T \le 50000$) โ€” the number of queries. Each query contains description of the set of segments. Queries are independent. First line of each query contains single integer $n$ ($2 \le n \le 10^5$) โ€” number of segments. It is guaranteed that $\sum{n}$ over all queries does not exceed $10^5$. The next $n$ lines contains two integers $l_i$, $r_i$ per line ($1 \le l_i \le r_i \le 2 \cdot 10^5$) โ€” the $i$-th segment. -----Output----- For each query print $n$ integers $t_1, t_2, \dots, t_n$ ($t_i \in \{1, 2\}$) โ€” for each segment (in the same order as in the input) $t_i$ equals $1$ if the $i$-th segment will belongs to the first group and $2$ otherwise. If there are multiple answers, you can print any of them. If there is no answer, print $-1$. -----Example----- Input 3 2 5 5 2 3 3 3 5 2 3 2 3 3 3 3 4 4 5 5 Output 2 1 -1 1 1 2 -----Note----- In the first query the first and the second segments should be in different groups, but exact numbers don't matter. In the second query the third segment intersects with the first and the second segments, so they should be in the same group, but then the other group becomes empty, so answer is $-1$. In the third query we can distribute segments in any way that makes groups non-empty, so any answer of $6$ possible is correct.
for _ in range(int(input())): n=int(input()) a=[] f=0 for i in range(n): l,r=map(int,input().split()) a.append([l,r,i]) a.sort(key=lambda x:x[0]) rm=a[0][1] for i in range(n): if a[i][0]>rm: b=a[i] f=1 break if a[i][1]>rm: rm=a[i][1] if f==0: print(-1) else: a.sort(key=lambda x:x[2]) for i in range(n): if a[i][0]<b[0]: print(1,end=" ") else: print(2,end=" ") print()
There are $n$ segments $[l_i, r_i]$ for $1 \le i \le n$. You should divide all segments into two non-empty groups in such way that there is no pair of segments from different groups which have at least one common point, or say that it's impossible to do it. Each segment should belong to exactly one group. To optimize testing process you will be given multitest. -----Input----- The first line contains one integer $T$ ($1 \le T \le 50000$) โ€” the number of queries. Each query contains description of the set of segments. Queries are independent. First line of each query contains single integer $n$ ($2 \le n \le 10^5$) โ€” number of segments. It is guaranteed that $\sum{n}$ over all queries does not exceed $10^5$. The next $n$ lines contains two integers $l_i$, $r_i$ per line ($1 \le l_i \le r_i \le 2 \cdot 10^5$) โ€” the $i$-th segment. -----Output----- For each query print $n$ integers $t_1, t_2, \dots, t_n$ ($t_i \in \{1, 2\}$) โ€” for each segment (in the same order as in the input) $t_i$ equals $1$ if the $i$-th segment will belongs to the first group and $2$ otherwise. If there are multiple answers, you can print any of them. If there is no answer, print $-1$. -----Example----- Input 3 2 5 5 2 3 3 3 5 2 3 2 3 3 3 3 4 4 5 5 Output 2 1 -1 1 1 2 -----Note----- In the first query the first and the second segments should be in different groups, but exact numbers don't matter. In the second query the third segment intersects with the first and the second segments, so they should be in the same group, but then the other group becomes empty, so answer is $-1$. In the third query we can distribute segments in any way that makes groups non-empty, so any answer of $6$ possible is correct.
T = int(input()) for i in range(T): n = int(input()) X = [] for j in range(n): l, r = list(map(int, input().split())) X.append([j, l, r]) X = sorted(X, key = lambda x: x[1]) # print(X) Y = ["2"] * n s = -1 rmax = X[0][2] Y[X[0][0]] = "1" for i in range(1, n): if X[i][1] > rmax: s = i break rmax = max(rmax, X[i][2]) Y[X[i][0]] = "1" if s < 0: print(-1) else: print(" ".join(Y))
There are $n$ segments $[l_i, r_i]$ for $1 \le i \le n$. You should divide all segments into two non-empty groups in such way that there is no pair of segments from different groups which have at least one common point, or say that it's impossible to do it. Each segment should belong to exactly one group. To optimize testing process you will be given multitest. -----Input----- The first line contains one integer $T$ ($1 \le T \le 50000$) โ€” the number of queries. Each query contains description of the set of segments. Queries are independent. First line of each query contains single integer $n$ ($2 \le n \le 10^5$) โ€” number of segments. It is guaranteed that $\sum{n}$ over all queries does not exceed $10^5$. The next $n$ lines contains two integers $l_i$, $r_i$ per line ($1 \le l_i \le r_i \le 2 \cdot 10^5$) โ€” the $i$-th segment. -----Output----- For each query print $n$ integers $t_1, t_2, \dots, t_n$ ($t_i \in \{1, 2\}$) โ€” for each segment (in the same order as in the input) $t_i$ equals $1$ if the $i$-th segment will belongs to the first group and $2$ otherwise. If there are multiple answers, you can print any of them. If there is no answer, print $-1$. -----Example----- Input 3 2 5 5 2 3 3 3 5 2 3 2 3 3 3 3 4 4 5 5 Output 2 1 -1 1 1 2 -----Note----- In the first query the first and the second segments should be in different groups, but exact numbers don't matter. In the second query the third segment intersects with the first and the second segments, so they should be in the same group, but then the other group becomes empty, so answer is $-1$. In the third query we can distribute segments in any way that makes groups non-empty, so any answer of $6$ possible is correct.
for c in range(int(input())): N = int(input()) counter = 0 segments = [] for n in range(N): l, r = map(int, input().split()) segments.append([[l, r], counter]) counter += 1 segments.sort() ans = [-1] * N rightBound = segments[0][0][1] valid = -1 for i in range(1, len(segments)): if segments[i][0][0] > rightBound: valid = i break else: rightBound = max(rightBound, segments[i][0][1]) if valid == -1: print(-1) else: for i in range(valid): ans[segments[i][1]] = 1 for i in range(valid, len(segments)): ans[segments[i][1]] = 2 for a in ans: print(a, end= ' ') print() ''' 3 2 5 5 2 3 3 3 5 2 3 2 3 3 3 3 4 4 5 5 '''
There are $n$ segments $[l_i, r_i]$ for $1 \le i \le n$. You should divide all segments into two non-empty groups in such way that there is no pair of segments from different groups which have at least one common point, or say that it's impossible to do it. Each segment should belong to exactly one group. To optimize testing process you will be given multitest. -----Input----- The first line contains one integer $T$ ($1 \le T \le 50000$) โ€” the number of queries. Each query contains description of the set of segments. Queries are independent. First line of each query contains single integer $n$ ($2 \le n \le 10^5$) โ€” number of segments. It is guaranteed that $\sum{n}$ over all queries does not exceed $10^5$. The next $n$ lines contains two integers $l_i$, $r_i$ per line ($1 \le l_i \le r_i \le 2 \cdot 10^5$) โ€” the $i$-th segment. -----Output----- For each query print $n$ integers $t_1, t_2, \dots, t_n$ ($t_i \in \{1, 2\}$) โ€” for each segment (in the same order as in the input) $t_i$ equals $1$ if the $i$-th segment will belongs to the first group and $2$ otherwise. If there are multiple answers, you can print any of them. If there is no answer, print $-1$. -----Example----- Input 3 2 5 5 2 3 3 3 5 2 3 2 3 3 3 3 4 4 5 5 Output 2 1 -1 1 1 2 -----Note----- In the first query the first and the second segments should be in different groups, but exact numbers don't matter. In the second query the third segment intersects with the first and the second segments, so they should be in the same group, but then the other group becomes empty, so answer is $-1$. In the third query we can distribute segments in any way that makes groups non-empty, so any answer of $6$ possible is correct.
MOD = 10**9 + 7 I = lambda:list(map(int,input().split())) t, = I() while t: t -= 1 n, = I() a = [2]*(n) l = [] for i in range(n): l.append(I()+[i]) l.sort() mn = l[0][0] mx = l[0][1] i = 0 while i < n and l[i][0] <= mx: mx = max(mx, l[i][1]) a[l[i][2]] = 1 i += 1 if all([i == 1 for i in a]) or all([i == 2 for i in a]): print(-1) else: print(*a)
There are $n$ segments $[l_i, r_i]$ for $1 \le i \le n$. You should divide all segments into two non-empty groups in such way that there is no pair of segments from different groups which have at least one common point, or say that it's impossible to do it. Each segment should belong to exactly one group. To optimize testing process you will be given multitest. -----Input----- The first line contains one integer $T$ ($1 \le T \le 50000$) โ€” the number of queries. Each query contains description of the set of segments. Queries are independent. First line of each query contains single integer $n$ ($2 \le n \le 10^5$) โ€” number of segments. It is guaranteed that $\sum{n}$ over all queries does not exceed $10^5$. The next $n$ lines contains two integers $l_i$, $r_i$ per line ($1 \le l_i \le r_i \le 2 \cdot 10^5$) โ€” the $i$-th segment. -----Output----- For each query print $n$ integers $t_1, t_2, \dots, t_n$ ($t_i \in \{1, 2\}$) โ€” for each segment (in the same order as in the input) $t_i$ equals $1$ if the $i$-th segment will belongs to the first group and $2$ otherwise. If there are multiple answers, you can print any of them. If there is no answer, print $-1$. -----Example----- Input 3 2 5 5 2 3 3 3 5 2 3 2 3 3 3 3 4 4 5 5 Output 2 1 -1 1 1 2 -----Note----- In the first query the first and the second segments should be in different groups, but exact numbers don't matter. In the second query the third segment intersects with the first and the second segments, so they should be in the same group, but then the other group becomes empty, so answer is $-1$. In the third query we can distribute segments in any way that makes groups non-empty, so any answer of $6$ possible is correct.
t = int(input()) for tc in range(t): n = int(input()) l = [] for i in range( n ): a,b = map(int,input().split()) l.append([i,a,b]) l = sorted(l, key=lambda x: x[1]) last = l[0][1] i = 0 while i < n: if l[ i ][ 1 ] > last: break last = max( last, l[ i ][ 2 ] ) i += 1 if i == n: print( -1 ) else: ind = [2] * n for j in range( i ): ind[ l[ j ][ 0 ] ] = 1 for i in ind: print( i,end=' ') print("")
There are $n$ segments $[l_i, r_i]$ for $1 \le i \le n$. You should divide all segments into two non-empty groups in such way that there is no pair of segments from different groups which have at least one common point, or say that it's impossible to do it. Each segment should belong to exactly one group. To optimize testing process you will be given multitest. -----Input----- The first line contains one integer $T$ ($1 \le T \le 50000$) โ€” the number of queries. Each query contains description of the set of segments. Queries are independent. First line of each query contains single integer $n$ ($2 \le n \le 10^5$) โ€” number of segments. It is guaranteed that $\sum{n}$ over all queries does not exceed $10^5$. The next $n$ lines contains two integers $l_i$, $r_i$ per line ($1 \le l_i \le r_i \le 2 \cdot 10^5$) โ€” the $i$-th segment. -----Output----- For each query print $n$ integers $t_1, t_2, \dots, t_n$ ($t_i \in \{1, 2\}$) โ€” for each segment (in the same order as in the input) $t_i$ equals $1$ if the $i$-th segment will belongs to the first group and $2$ otherwise. If there are multiple answers, you can print any of them. If there is no answer, print $-1$. -----Example----- Input 3 2 5 5 2 3 3 3 5 2 3 2 3 3 3 3 4 4 5 5 Output 2 1 -1 1 1 2 -----Note----- In the first query the first and the second segments should be in different groups, but exact numbers don't matter. In the second query the third segment intersects with the first and the second segments, so they should be in the same group, but then the other group becomes empty, so answer is $-1$. In the third query we can distribute segments in any way that makes groups non-empty, so any answer of $6$ possible is correct.
t = int(input()) final = [] for k in range(t): n = int(input()) skl = [] for i in range(n): a, b = map(int, input().split()) skl.append((a, -1, i)) skl.append((b, 1, i)) skl.sort() m = 0 ans = ['0']*n for i, p in enumerate(skl): if m == 0 and i != 0: for j in range(i, 2*n): ans[skl[j][2]] = '2' break m -= p[1] if p[1] == -1: ans[skl[i][2]] = '1' if not '2' in ans: final.append('-1') else: final.append(' '.join(ans)) print('\n'.join(final))
There are $n$ segments $[l_i, r_i]$ for $1 \le i \le n$. You should divide all segments into two non-empty groups in such way that there is no pair of segments from different groups which have at least one common point, or say that it's impossible to do it. Each segment should belong to exactly one group. To optimize testing process you will be given multitest. -----Input----- The first line contains one integer $T$ ($1 \le T \le 50000$) โ€” the number of queries. Each query contains description of the set of segments. Queries are independent. First line of each query contains single integer $n$ ($2 \le n \le 10^5$) โ€” number of segments. It is guaranteed that $\sum{n}$ over all queries does not exceed $10^5$. The next $n$ lines contains two integers $l_i$, $r_i$ per line ($1 \le l_i \le r_i \le 2 \cdot 10^5$) โ€” the $i$-th segment. -----Output----- For each query print $n$ integers $t_1, t_2, \dots, t_n$ ($t_i \in \{1, 2\}$) โ€” for each segment (in the same order as in the input) $t_i$ equals $1$ if the $i$-th segment will belongs to the first group and $2$ otherwise. If there are multiple answers, you can print any of them. If there is no answer, print $-1$. -----Example----- Input 3 2 5 5 2 3 3 3 5 2 3 2 3 3 3 3 4 4 5 5 Output 2 1 -1 1 1 2 -----Note----- In the first query the first and the second segments should be in different groups, but exact numbers don't matter. In the second query the third segment intersects with the first and the second segments, so they should be in the same group, but then the other group becomes empty, so answer is $-1$. In the third query we can distribute segments in any way that makes groups non-empty, so any answer of $6$ possible is correct.
def check(data): n = len(data) s = sorted(zip(data, range(n))) m = s[0][0][1] left = set() for i, r in enumerate(s): left.add(r[1]) if i == len(s)-1: return '-1' m = max(m, r[0][1]) if s[i+1][0][0] > m: break res = ['1' if j in left else '2' for j in range(n)] return ' '.join(res) T = int(input()) for i in range(T): n = int(input()) data = [] for j in range(n): l, r = map(int, input().split()) data.append((l, r)) print(check(data))
There are $n$ segments $[l_i, r_i]$ for $1 \le i \le n$. You should divide all segments into two non-empty groups in such way that there is no pair of segments from different groups which have at least one common point, or say that it's impossible to do it. Each segment should belong to exactly one group. To optimize testing process you will be given multitest. -----Input----- The first line contains one integer $T$ ($1 \le T \le 50000$) โ€” the number of queries. Each query contains description of the set of segments. Queries are independent. First line of each query contains single integer $n$ ($2 \le n \le 10^5$) โ€” number of segments. It is guaranteed that $\sum{n}$ over all queries does not exceed $10^5$. The next $n$ lines contains two integers $l_i$, $r_i$ per line ($1 \le l_i \le r_i \le 2 \cdot 10^5$) โ€” the $i$-th segment. -----Output----- For each query print $n$ integers $t_1, t_2, \dots, t_n$ ($t_i \in \{1, 2\}$) โ€” for each segment (in the same order as in the input) $t_i$ equals $1$ if the $i$-th segment will belongs to the first group and $2$ otherwise. If there are multiple answers, you can print any of them. If there is no answer, print $-1$. -----Example----- Input 3 2 5 5 2 3 3 3 5 2 3 2 3 3 3 3 4 4 5 5 Output 2 1 -1 1 1 2 -----Note----- In the first query the first and the second segments should be in different groups, but exact numbers don't matter. In the second query the third segment intersects with the first and the second segments, so they should be in the same group, but then the other group becomes empty, so answer is $-1$. In the third query we can distribute segments in any way that makes groups non-empty, so any answer of $6$ possible is correct.
T = int(input()) for _ in range(T): n = int(input()) seg = [] for s in range(n): l,r = [int(x) for x in input().split()] seg.append((l,r)) pos = {} for i in range(n): if seg[i] in pos: pos[seg[i]].append(i) else: pos[seg[i]] = [i] seg.sort() right = seg[0][1] goodindex = -1 for j in range(1,n): if seg[j][0] > right: goodindex = j break right = max(right,seg[j][1]) if goodindex == -1: print(-1) else: ans = ['2']*n for i in range(goodindex): ans[pos[seg[i]][-1]] = '1' pos[seg[i]].pop() print(' '.join(ans))
There are $n$ segments $[l_i, r_i]$ for $1 \le i \le n$. You should divide all segments into two non-empty groups in such way that there is no pair of segments from different groups which have at least one common point, or say that it's impossible to do it. Each segment should belong to exactly one group. To optimize testing process you will be given multitest. -----Input----- The first line contains one integer $T$ ($1 \le T \le 50000$) โ€” the number of queries. Each query contains description of the set of segments. Queries are independent. First line of each query contains single integer $n$ ($2 \le n \le 10^5$) โ€” number of segments. It is guaranteed that $\sum{n}$ over all queries does not exceed $10^5$. The next $n$ lines contains two integers $l_i$, $r_i$ per line ($1 \le l_i \le r_i \le 2 \cdot 10^5$) โ€” the $i$-th segment. -----Output----- For each query print $n$ integers $t_1, t_2, \dots, t_n$ ($t_i \in \{1, 2\}$) โ€” for each segment (in the same order as in the input) $t_i$ equals $1$ if the $i$-th segment will belongs to the first group and $2$ otherwise. If there are multiple answers, you can print any of them. If there is no answer, print $-1$. -----Example----- Input 3 2 5 5 2 3 3 3 5 2 3 2 3 3 3 3 4 4 5 5 Output 2 1 -1 1 1 2 -----Note----- In the first query the first and the second segments should be in different groups, but exact numbers don't matter. In the second query the third segment intersects with the first and the second segments, so they should be in the same group, but then the other group becomes empty, so answer is $-1$. In the third query we can distribute segments in any way that makes groups non-empty, so any answer of $6$ possible is correct.
import sys input = sys.stdin.readline Q=int(input()) for test in range(Q): n=int(input()) LR=[list(map(int,input().split()))+[i] for i in range(n)] LR.sort() GR1=[LR[0][0],LR[0][1]] for i in range(1,n): l,r,_=LR[i] if r<GR1[0] or l>GR1[1]: ANS=i break else: GR1=[min(GR1[0],l),max(GR1[1],r)] else: print(-1) continue ANSLIST=[1]*n for j in range(ANS,n): ANSLIST[LR[j][2]]=2 for a in ANSLIST: print(a,end=" ") print()
There are $n$ segments $[l_i, r_i]$ for $1 \le i \le n$. You should divide all segments into two non-empty groups in such way that there is no pair of segments from different groups which have at least one common point, or say that it's impossible to do it. Each segment should belong to exactly one group. To optimize testing process you will be given multitest. -----Input----- The first line contains one integer $T$ ($1 \le T \le 50000$) โ€” the number of queries. Each query contains description of the set of segments. Queries are independent. First line of each query contains single integer $n$ ($2 \le n \le 10^5$) โ€” number of segments. It is guaranteed that $\sum{n}$ over all queries does not exceed $10^5$. The next $n$ lines contains two integers $l_i$, $r_i$ per line ($1 \le l_i \le r_i \le 2 \cdot 10^5$) โ€” the $i$-th segment. -----Output----- For each query print $n$ integers $t_1, t_2, \dots, t_n$ ($t_i \in \{1, 2\}$) โ€” for each segment (in the same order as in the input) $t_i$ equals $1$ if the $i$-th segment will belongs to the first group and $2$ otherwise. If there are multiple answers, you can print any of them. If there is no answer, print $-1$. -----Example----- Input 3 2 5 5 2 3 3 3 5 2 3 2 3 3 3 3 4 4 5 5 Output 2 1 -1 1 1 2 -----Note----- In the first query the first and the second segments should be in different groups, but exact numbers don't matter. In the second query the third segment intersects with the first and the second segments, so they should be in the same group, but then the other group becomes empty, so answer is $-1$. In the third query we can distribute segments in any way that makes groups non-empty, so any answer of $6$ possible is correct.
L = 0 R = 1 def main(): buf = input() T = int(buf) n = [] lr = [] for i in range(T): buf = input() n.append(int(buf)) lr.append([]) for j in range(n[i]): buf = input() buflist = buf.split() lr[i].append([int(buflist[0]), int(buflist[1])]) for i in range(T): lr_s = list(sorted(lr[i])) threshold = lr_s[0][R] threshold_final = None for j in range(1, n[i]): if threshold < lr_s[j][L]: threshold_final = threshold break elif threshold < lr_s[j][R]: threshold = lr_s[j][R] if threshold_final == None: print(-1) # impossible continue answer = "" for j in range(n[i]): if lr[i][j][L] <= threshold_final: answer += "1" else: answer += "2" if j < n[i] - 1: answer += " " print(answer) def __starting_point(): main() __starting_point()
There are $n$ segments $[l_i, r_i]$ for $1 \le i \le n$. You should divide all segments into two non-empty groups in such way that there is no pair of segments from different groups which have at least one common point, or say that it's impossible to do it. Each segment should belong to exactly one group. To optimize testing process you will be given multitest. -----Input----- The first line contains one integer $T$ ($1 \le T \le 50000$) โ€” the number of queries. Each query contains description of the set of segments. Queries are independent. First line of each query contains single integer $n$ ($2 \le n \le 10^5$) โ€” number of segments. It is guaranteed that $\sum{n}$ over all queries does not exceed $10^5$. The next $n$ lines contains two integers $l_i$, $r_i$ per line ($1 \le l_i \le r_i \le 2 \cdot 10^5$) โ€” the $i$-th segment. -----Output----- For each query print $n$ integers $t_1, t_2, \dots, t_n$ ($t_i \in \{1, 2\}$) โ€” for each segment (in the same order as in the input) $t_i$ equals $1$ if the $i$-th segment will belongs to the first group and $2$ otherwise. If there are multiple answers, you can print any of them. If there is no answer, print $-1$. -----Example----- Input 3 2 5 5 2 3 3 3 5 2 3 2 3 3 3 3 4 4 5 5 Output 2 1 -1 1 1 2 -----Note----- In the first query the first and the second segments should be in different groups, but exact numbers don't matter. In the second query the third segment intersects with the first and the second segments, so they should be in the same group, but then the other group becomes empty, so answer is $-1$. In the third query we can distribute segments in any way that makes groups non-empty, so any answer of $6$ possible is correct.
T = int(input()) for i in range(T): n = int(input()) inp=[] temp = [] for j in range(n): a,b = [int(u) for u in input().split()] inp.append([a,b]) temp.append([a,b]) inp.sort() check=0 begin = inp[0][0] end = inp[0][1] for j in range(n): if(inp[j][0]>end): check=1 break if(inp[j][1]>end): end = inp[j][1] ans = [] if(check==0): print(-1) else: for j in range(n): if(temp[j][0]>=begin and temp[j][1]<=end): ans.append("1") else: ans.append("2") print(" ".join(ans))
There are $n$ segments $[l_i, r_i]$ for $1 \le i \le n$. You should divide all segments into two non-empty groups in such way that there is no pair of segments from different groups which have at least one common point, or say that it's impossible to do it. Each segment should belong to exactly one group. To optimize testing process you will be given multitest. -----Input----- The first line contains one integer $T$ ($1 \le T \le 50000$) โ€” the number of queries. Each query contains description of the set of segments. Queries are independent. First line of each query contains single integer $n$ ($2 \le n \le 10^5$) โ€” number of segments. It is guaranteed that $\sum{n}$ over all queries does not exceed $10^5$. The next $n$ lines contains two integers $l_i$, $r_i$ per line ($1 \le l_i \le r_i \le 2 \cdot 10^5$) โ€” the $i$-th segment. -----Output----- For each query print $n$ integers $t_1, t_2, \dots, t_n$ ($t_i \in \{1, 2\}$) โ€” for each segment (in the same order as in the input) $t_i$ equals $1$ if the $i$-th segment will belongs to the first group and $2$ otherwise. If there are multiple answers, you can print any of them. If there is no answer, print $-1$. -----Example----- Input 3 2 5 5 2 3 3 3 5 2 3 2 3 3 3 3 4 4 5 5 Output 2 1 -1 1 1 2 -----Note----- In the first query the first and the second segments should be in different groups, but exact numbers don't matter. In the second query the third segment intersects with the first and the second segments, so they should be in the same group, but then the other group becomes empty, so answer is $-1$. In the third query we can distribute segments in any way that makes groups non-empty, so any answer of $6$ possible is correct.
q=int(input()) for i in range(q): n=int(input()) ilist=[] for j in range(n): ilist.append(list(map(int, input().rstrip().split()))) ilist[j].append(j) ilist.sort() seglist=[2]*n seglist[ilist[0][2]]=1 #print(seglist) #print(ilist) #print(ilist) goodvalue=-1 currentmax=ilist[0][1] for k in range(n-1): if currentmax>=ilist[k+1][0]: seglist[ilist[k+1][2]]=1 currentmax=max([currentmax,ilist[k+1][1]]) if currentmax<ilist[k+1][0]: break #for k in range(goodvalue+1,n): # seglist[ilist[k][2]]=2 # print(k) #print(seglist) if sum(seglist)==n: print(-1) else: print(*seglist)
There are $n$ segments $[l_i, r_i]$ for $1 \le i \le n$. You should divide all segments into two non-empty groups in such way that there is no pair of segments from different groups which have at least one common point, or say that it's impossible to do it. Each segment should belong to exactly one group. To optimize testing process you will be given multitest. -----Input----- The first line contains one integer $T$ ($1 \le T \le 50000$) โ€” the number of queries. Each query contains description of the set of segments. Queries are independent. First line of each query contains single integer $n$ ($2 \le n \le 10^5$) โ€” number of segments. It is guaranteed that $\sum{n}$ over all queries does not exceed $10^5$. The next $n$ lines contains two integers $l_i$, $r_i$ per line ($1 \le l_i \le r_i \le 2 \cdot 10^5$) โ€” the $i$-th segment. -----Output----- For each query print $n$ integers $t_1, t_2, \dots, t_n$ ($t_i \in \{1, 2\}$) โ€” for each segment (in the same order as in the input) $t_i$ equals $1$ if the $i$-th segment will belongs to the first group and $2$ otherwise. If there are multiple answers, you can print any of them. If there is no answer, print $-1$. -----Example----- Input 3 2 5 5 2 3 3 3 5 2 3 2 3 3 3 3 4 4 5 5 Output 2 1 -1 1 1 2 -----Note----- In the first query the first and the second segments should be in different groups, but exact numbers don't matter. In the second query the third segment intersects with the first and the second segments, so they should be in the same group, but then the other group becomes empty, so answer is $-1$. In the third query we can distribute segments in any way that makes groups non-empty, so any answer of $6$ possible is correct.
n = int(input()) for t in range(n): k = int(input()) samples = [] for i in range(k): samples.append(tuple(map(int, input().split()))) samples = sorted(enumerate(samples), key=lambda x: x[1]) tick = 1 ans = [1] group_end = samples[0][1][1] for si in range(1, len(samples)): now = samples[si][1] if now[0] > group_end: tick = 2 else: group_end = max(now[1], group_end) ans.append(1) if tick == 2: ans.extend([2] * (len(samples) - si)) break ans = sorted(zip(samples, ans)) ans = list([x[1] for x in ans]) if 2 not in ans: print(-1) else: print(' '.join(map(str, ans)))
There are $n$ segments $[l_i, r_i]$ for $1 \le i \le n$. You should divide all segments into two non-empty groups in such way that there is no pair of segments from different groups which have at least one common point, or say that it's impossible to do it. Each segment should belong to exactly one group. To optimize testing process you will be given multitest. -----Input----- The first line contains one integer $T$ ($1 \le T \le 50000$) โ€” the number of queries. Each query contains description of the set of segments. Queries are independent. First line of each query contains single integer $n$ ($2 \le n \le 10^5$) โ€” number of segments. It is guaranteed that $\sum{n}$ over all queries does not exceed $10^5$. The next $n$ lines contains two integers $l_i$, $r_i$ per line ($1 \le l_i \le r_i \le 2 \cdot 10^5$) โ€” the $i$-th segment. -----Output----- For each query print $n$ integers $t_1, t_2, \dots, t_n$ ($t_i \in \{1, 2\}$) โ€” for each segment (in the same order as in the input) $t_i$ equals $1$ if the $i$-th segment will belongs to the first group and $2$ otherwise. If there are multiple answers, you can print any of them. If there is no answer, print $-1$. -----Example----- Input 3 2 5 5 2 3 3 3 5 2 3 2 3 3 3 3 4 4 5 5 Output 2 1 -1 1 1 2 -----Note----- In the first query the first and the second segments should be in different groups, but exact numbers don't matter. In the second query the third segment intersects with the first and the second segments, so they should be in the same group, but then the other group becomes empty, so answer is $-1$. In the third query we can distribute segments in any way that makes groups non-empty, so any answer of $6$ possible is correct.
import math def is_intersect(l1, r1, l2, r2): return (l1 < l2 and r1 >= l2) or (l1 >= l2 and l1 <= r2) def get_groups(ranges): ranges.sort(key=lambda x: x[1]) ranges.sort(key=lambda x: x[0]) ranges[0][3] = 1 group1 = ranges[0][:2] group2 = None for i, rng in enumerate(ranges[1:]): l, r = rng[:2] if is_intersect(l, r, *group1) and ((group2 is None) or not is_intersect(l, r, group2)): rng[3] = 1 group1[0] = min(group1[0], l) group1[1] = max(group1[1], r) elif not is_intersect(l, r, *group1): if group2 is None: group2 = [l, r] else: group2[0] = min(group2[0], l) group2[1] = max(group2[1], r) rng[3] = 2 else: return -1 if group2 is None: return -1 ranges.sort(key=lambda x: x[2]) return ' '.join(list(map(str, (rng[3] for rng in ranges)))) def __starting_point(): n = int(input()) for i in range(n): k = int(input()) arr = [None] * k for j in range(k): arr[j] = list(map(int, input().split())) + [j, -1] print(get_groups(arr)) __starting_point()
There are $n$ segments $[l_i, r_i]$ for $1 \le i \le n$. You should divide all segments into two non-empty groups in such way that there is no pair of segments from different groups which have at least one common point, or say that it's impossible to do it. Each segment should belong to exactly one group. To optimize testing process you will be given multitest. -----Input----- The first line contains one integer $T$ ($1 \le T \le 50000$) โ€” the number of queries. Each query contains description of the set of segments. Queries are independent. First line of each query contains single integer $n$ ($2 \le n \le 10^5$) โ€” number of segments. It is guaranteed that $\sum{n}$ over all queries does not exceed $10^5$. The next $n$ lines contains two integers $l_i$, $r_i$ per line ($1 \le l_i \le r_i \le 2 \cdot 10^5$) โ€” the $i$-th segment. -----Output----- For each query print $n$ integers $t_1, t_2, \dots, t_n$ ($t_i \in \{1, 2\}$) โ€” for each segment (in the same order as in the input) $t_i$ equals $1$ if the $i$-th segment will belongs to the first group and $2$ otherwise. If there are multiple answers, you can print any of them. If there is no answer, print $-1$. -----Example----- Input 3 2 5 5 2 3 3 3 5 2 3 2 3 3 3 3 4 4 5 5 Output 2 1 -1 1 1 2 -----Note----- In the first query the first and the second segments should be in different groups, but exact numbers don't matter. In the second query the third segment intersects with the first and the second segments, so they should be in the same group, but then the other group becomes empty, so answer is $-1$. In the third query we can distribute segments in any way that makes groups non-empty, so any answer of $6$ possible is correct.
q=int(input()) for i in range(q): n=int(input()) arr=[0 for i in range(n)] for i in range(n): temp=list(map(int,input().split())) temp.append(i) arr[i]=temp arr=sorted(arr,key=lambda l:l[0]) #print(arr) ans=[1 for i in range(n)] # if(arr[0][1]<arr[1][0]): # ans[0]=2 # for i in range(n): # print(ans[i],end=' ') # print() # continue # if(arr[n-1][0]>arr[n-2][1]): # ans[n-1]=2 # for i in range(n): # print(ans[i],end=' ') # print() # continue yoyo=-1 maxa=arr[0][1] for i in range(1,n): if(arr[i][0]>maxa): yoyo=i break if(arr[i][1]>maxa): maxa=arr[i][1] if(yoyo==-1): print(-1) continue else: for i in range(yoyo,n): ans[arr[i][2]]=2 for i in range(n): print(ans[i],end=' ') print()
There are $n$ segments $[l_i, r_i]$ for $1 \le i \le n$. You should divide all segments into two non-empty groups in such way that there is no pair of segments from different groups which have at least one common point, or say that it's impossible to do it. Each segment should belong to exactly one group. To optimize testing process you will be given multitest. -----Input----- The first line contains one integer $T$ ($1 \le T \le 50000$) โ€” the number of queries. Each query contains description of the set of segments. Queries are independent. First line of each query contains single integer $n$ ($2 \le n \le 10^5$) โ€” number of segments. It is guaranteed that $\sum{n}$ over all queries does not exceed $10^5$. The next $n$ lines contains two integers $l_i$, $r_i$ per line ($1 \le l_i \le r_i \le 2 \cdot 10^5$) โ€” the $i$-th segment. -----Output----- For each query print $n$ integers $t_1, t_2, \dots, t_n$ ($t_i \in \{1, 2\}$) โ€” for each segment (in the same order as in the input) $t_i$ equals $1$ if the $i$-th segment will belongs to the first group and $2$ otherwise. If there are multiple answers, you can print any of them. If there is no answer, print $-1$. -----Example----- Input 3 2 5 5 2 3 3 3 5 2 3 2 3 3 3 3 4 4 5 5 Output 2 1 -1 1 1 2 -----Note----- In the first query the first and the second segments should be in different groups, but exact numbers don't matter. In the second query the third segment intersects with the first and the second segments, so they should be in the same group, but then the other group becomes empty, so answer is $-1$. In the third query we can distribute segments in any way that makes groups non-empty, so any answer of $6$ possible is correct.
T = int(input()) for _ in range(T): n = int(input()) events = [] results = [0 for i in range(n)] fail = False for i in range(n): l, r = map(int, input().split()) events.append((l, 0, i)) events.append((r, 1, i)) events.sort() cnt = 0 cur_seg = 1 for _, t, i in events: if t == 0: cnt += 1 results[i] = cur_seg else: cnt -= 1 if cnt == 0: cur_seg = 1 + cur_seg % 2 if len(set(results)) == 2: print(*results) else: print(-1)
There are $n$ segments $[l_i, r_i]$ for $1 \le i \le n$. You should divide all segments into two non-empty groups in such way that there is no pair of segments from different groups which have at least one common point, or say that it's impossible to do it. Each segment should belong to exactly one group. To optimize testing process you will be given multitest. -----Input----- The first line contains one integer $T$ ($1 \le T \le 50000$) โ€” the number of queries. Each query contains description of the set of segments. Queries are independent. First line of each query contains single integer $n$ ($2 \le n \le 10^5$) โ€” number of segments. It is guaranteed that $\sum{n}$ over all queries does not exceed $10^5$. The next $n$ lines contains two integers $l_i$, $r_i$ per line ($1 \le l_i \le r_i \le 2 \cdot 10^5$) โ€” the $i$-th segment. -----Output----- For each query print $n$ integers $t_1, t_2, \dots, t_n$ ($t_i \in \{1, 2\}$) โ€” for each segment (in the same order as in the input) $t_i$ equals $1$ if the $i$-th segment will belongs to the first group and $2$ otherwise. If there are multiple answers, you can print any of them. If there is no answer, print $-1$. -----Example----- Input 3 2 5 5 2 3 3 3 5 2 3 2 3 3 3 3 4 4 5 5 Output 2 1 -1 1 1 2 -----Note----- In the first query the first and the second segments should be in different groups, but exact numbers don't matter. In the second query the third segment intersects with the first and the second segments, so they should be in the same group, but then the other group becomes empty, so answer is $-1$. In the third query we can distribute segments in any way that makes groups non-empty, so any answer of $6$ possible is correct.
q = int(input()) while q > 0: q = q-1 L = [] n = int(input()) for i in range(n): L.append(tuple(map(int, input().split()))) d = {} ind = 0 for i in L: if i not in d: d[i] = [] d[i].append(ind) ind += 1 S = sorted(L) r = S[0][1] i = 1 while i < n: if S[i][0] > r: break r = max(r,S[i][1]) i += 1 #print(S,i) if i == n: print(-1) else: while i < n: d[S[i]].append(-2) i += 1 for i in L: if d[i][-1] == -2: print(2,end=' ') else: print(1,end=' ') print()
There are $n$ segments $[l_i, r_i]$ for $1 \le i \le n$. You should divide all segments into two non-empty groups in such way that there is no pair of segments from different groups which have at least one common point, or say that it's impossible to do it. Each segment should belong to exactly one group. To optimize testing process you will be given multitest. -----Input----- The first line contains one integer $T$ ($1 \le T \le 50000$) โ€” the number of queries. Each query contains description of the set of segments. Queries are independent. First line of each query contains single integer $n$ ($2 \le n \le 10^5$) โ€” number of segments. It is guaranteed that $\sum{n}$ over all queries does not exceed $10^5$. The next $n$ lines contains two integers $l_i$, $r_i$ per line ($1 \le l_i \le r_i \le 2 \cdot 10^5$) โ€” the $i$-th segment. -----Output----- For each query print $n$ integers $t_1, t_2, \dots, t_n$ ($t_i \in \{1, 2\}$) โ€” for each segment (in the same order as in the input) $t_i$ equals $1$ if the $i$-th segment will belongs to the first group and $2$ otherwise. If there are multiple answers, you can print any of them. If there is no answer, print $-1$. -----Example----- Input 3 2 5 5 2 3 3 3 5 2 3 2 3 3 3 3 4 4 5 5 Output 2 1 -1 1 1 2 -----Note----- In the first query the first and the second segments should be in different groups, but exact numbers don't matter. In the second query the third segment intersects with the first and the second segments, so they should be in the same group, but then the other group becomes empty, so answer is $-1$. In the third query we can distribute segments in any way that makes groups non-empty, so any answer of $6$ possible is correct.
T = int(input()) for _ in range(T): n = int(input()) s = [] for k in range(n): l, r = [int(i) for i in input().split()] s.append([l, 1, k]) s.append([r, 2, k]) s.sort() u = [2] * n o = set() for i in s: u[i[2]] = 1 if i[2] not in o: o.add(i[2]) else: o.remove(i[2]) if not o: if i != s[-1]: print(*u) break else: print(-1)
There are $n$ segments $[l_i, r_i]$ for $1 \le i \le n$. You should divide all segments into two non-empty groups in such way that there is no pair of segments from different groups which have at least one common point, or say that it's impossible to do it. Each segment should belong to exactly one group. To optimize testing process you will be given multitest. -----Input----- The first line contains one integer $T$ ($1 \le T \le 50000$) โ€” the number of queries. Each query contains description of the set of segments. Queries are independent. First line of each query contains single integer $n$ ($2 \le n \le 10^5$) โ€” number of segments. It is guaranteed that $\sum{n}$ over all queries does not exceed $10^5$. The next $n$ lines contains two integers $l_i$, $r_i$ per line ($1 \le l_i \le r_i \le 2 \cdot 10^5$) โ€” the $i$-th segment. -----Output----- For each query print $n$ integers $t_1, t_2, \dots, t_n$ ($t_i \in \{1, 2\}$) โ€” for each segment (in the same order as in the input) $t_i$ equals $1$ if the $i$-th segment will belongs to the first group and $2$ otherwise. If there are multiple answers, you can print any of them. If there is no answer, print $-1$. -----Example----- Input 3 2 5 5 2 3 3 3 5 2 3 2 3 3 3 3 4 4 5 5 Output 2 1 -1 1 1 2 -----Note----- In the first query the first and the second segments should be in different groups, but exact numbers don't matter. In the second query the third segment intersects with the first and the second segments, so they should be in the same group, but then the other group becomes empty, so answer is $-1$. In the third query we can distribute segments in any way that makes groups non-empty, so any answer of $6$ possible is correct.
t = int(input()) for test in range(t): n = int(input()) ans = ['1' for i in range(n)] start, end = dict(), dict() for i in range(n): a, b = list(map(int, input().split())) if a in start: start[a].append(i + 1) else: start[a] = [i + 1] if (i + 1) in end: end[i + 1].append(b) else: end[i + 1] = [b] st_sorted = sorted(list(start.keys())) m = 0 ok = False ans_pos = -1 for pos in range(len(st_sorted) - 1): for i in start[st_sorted[pos]]: m = max(m, max(end[i])) if m < st_sorted[pos + 1]: ok = True ans_pos = pos break if ok: for i in range(pos + 1, len(st_sorted)): for pos in start[st_sorted[i]]: ans[pos - 1] = '2' print(' '.join(ans)) else: print(-1)
Chaneka has a hobby of playing with animal toys. Every toy has a different fun value, a real number. Chaneka has four boxes to store the toys with specification: The first box stores toys with fun values in range of $(-\infty,-1]$. The second box stores toys with fun values in range of $(-1, 0)$. The third box stores toys with fun values in range of $(0, 1)$. The fourth box stores toys with fun value in range of $[1, \infty)$. Chaneka has $A$, $B$, $C$, $D$ toys in the first, second, third, and fourth box, respectively. One day she decides that she only wants one toy, a super toy. So she begins to create this super toy by sewing all the toys she has. While the number of toys Chaneka has is more than 1, she takes two different toys randomly and then sews them together, creating a new toy. The fun value of this new toy is equal to the multiplication of fun values of the sewn toys. She then puts this new toy in the appropriate box. She repeats this process until she only has one toy. This last toy is the super toy, and the box that stores this toy is the special box. As an observer, you only know the number of toys in each box initially but do not know their fun values. You also don't see the sequence of Chaneka's sewing. Determine which boxes can be the special box after Chaneka found her super toy. -----Input----- The first line has an integer $T$ $(1 \le T \le 5 \cdot 10^4)$, the number of test cases. Every case contains a line with four space-separated integers $A$ $B$ $C$ $D$ $(0 \le A, B, C, D \le 10^6, A + B + C + D > 0)$, which denotes the number of toys in the first, second, third, and fourth box, respectively. -----Output----- For each case, print four space-separated strings. Each string represents the possibility that the first, second, third, and fourth box can be the special box from left to right. For each box, print "Ya" (Without quotes, Indonesian for yes) if that box can be the special box. Print "Tidak" (Without quotes, Indonesian for No) otherwise. -----Example----- Input 2 1 2 0 1 0 1 0 0 Output Ya Ya Tidak Tidak Tidak Ya Tidak Tidak -----Note----- For the first case, here is a scenario where the first box is the special box: The first box had toys with fun values $\{-3\}$. The second box had toys with fun values $\{ -0.5, -0.5 \}$ The fourth box had toys with fun values $\{ 3 \}$ The sewing sequence: Chaneka sews the toy with fun $-0.5$ and $-0.5$ to a toy with fun $0.25$ and then put it in the third box. Chaneka sews the toy with fun $-3$ and $0.25$ to a toy with fun $-0.75$ and then put it in the second box. Chaneka sews the toy with fun $-0.75$ and $3$ to a toy with fun $-1.25$ and then put it in the first box, which then became the special box. Here is a scenario where the second box ends up being the special box: The first box had toys with fun values $\{-3\}$ The second box had toys with fun values $\{ -0.33, -0.25 \}$. The fourth box had toys with fun values $\{ 3 \}$. The sewing sequence: Chaneka sews the toy with fun $-3$ and $-0.33$ to a toy with fun $0.99$ and then put it in the third box. Chaneka sews the toy with fun $0.99$ and $3$ to a toy with fun $2.97$ and then put in it the fourth box. Chaneka sews the toy with fun $2.97$ and $-0.25$ to a toy with fun $-0.7425$ and then put it in the second box, which then became the special box. There is only one toy for the second case, so Chaneka does not have to sew anything because that toy, by definition, is the super toy.
t = int(input()) for _ in range(t): a, b, c, d = [int(i) for i in input().split(" ")] sgn = (a+b)%2 small = False large = False if a == 0 and d == 0: small = True if b == 0 and c == 0: large = True okay = [True] * 4 if sgn == 0: okay[0] = False okay[1] = False else: okay[2] = False okay[3] = False if small: okay[0] = False okay[3] = False if large: okay[1] = False okay[2] = False print(" ".join(["Ya" if okay[i] else "Tidak" for i in range(4)]))
Chaneka has a hobby of playing with animal toys. Every toy has a different fun value, a real number. Chaneka has four boxes to store the toys with specification: The first box stores toys with fun values in range of $(-\infty,-1]$. The second box stores toys with fun values in range of $(-1, 0)$. The third box stores toys with fun values in range of $(0, 1)$. The fourth box stores toys with fun value in range of $[1, \infty)$. Chaneka has $A$, $B$, $C$, $D$ toys in the first, second, third, and fourth box, respectively. One day she decides that she only wants one toy, a super toy. So she begins to create this super toy by sewing all the toys she has. While the number of toys Chaneka has is more than 1, she takes two different toys randomly and then sews them together, creating a new toy. The fun value of this new toy is equal to the multiplication of fun values of the sewn toys. She then puts this new toy in the appropriate box. She repeats this process until she only has one toy. This last toy is the super toy, and the box that stores this toy is the special box. As an observer, you only know the number of toys in each box initially but do not know their fun values. You also don't see the sequence of Chaneka's sewing. Determine which boxes can be the special box after Chaneka found her super toy. -----Input----- The first line has an integer $T$ $(1 \le T \le 5 \cdot 10^4)$, the number of test cases. Every case contains a line with four space-separated integers $A$ $B$ $C$ $D$ $(0 \le A, B, C, D \le 10^6, A + B + C + D > 0)$, which denotes the number of toys in the first, second, third, and fourth box, respectively. -----Output----- For each case, print four space-separated strings. Each string represents the possibility that the first, second, third, and fourth box can be the special box from left to right. For each box, print "Ya" (Without quotes, Indonesian for yes) if that box can be the special box. Print "Tidak" (Without quotes, Indonesian for No) otherwise. -----Example----- Input 2 1 2 0 1 0 1 0 0 Output Ya Ya Tidak Tidak Tidak Ya Tidak Tidak -----Note----- For the first case, here is a scenario where the first box is the special box: The first box had toys with fun values $\{-3\}$. The second box had toys with fun values $\{ -0.5, -0.5 \}$ The fourth box had toys with fun values $\{ 3 \}$ The sewing sequence: Chaneka sews the toy with fun $-0.5$ and $-0.5$ to a toy with fun $0.25$ and then put it in the third box. Chaneka sews the toy with fun $-3$ and $0.25$ to a toy with fun $-0.75$ and then put it in the second box. Chaneka sews the toy with fun $-0.75$ and $3$ to a toy with fun $-1.25$ and then put it in the first box, which then became the special box. Here is a scenario where the second box ends up being the special box: The first box had toys with fun values $\{-3\}$ The second box had toys with fun values $\{ -0.33, -0.25 \}$. The fourth box had toys with fun values $\{ 3 \}$. The sewing sequence: Chaneka sews the toy with fun $-3$ and $-0.33$ to a toy with fun $0.99$ and then put it in the third box. Chaneka sews the toy with fun $0.99$ and $3$ to a toy with fun $2.97$ and then put in it the fourth box. Chaneka sews the toy with fun $2.97$ and $-0.25$ to a toy with fun $-0.7425$ and then put it in the second box, which then became the special box. There is only one toy for the second case, so Chaneka does not have to sew anything because that toy, by definition, is the super toy.
t=int(input()) for you in range(t): l=input().split() a=int(l[0]) b=int(l[1]) c=int(l[2]) d=int(l[3]) z=a+b if(z%2==0): print("Tidak Tidak",end=" ") if(b>0 or c>0): print("Ya",end=" ") else: print("Tidak",end=" ") if(a>0 or d>0): print("Ya") else: print("Tidak") else: if(a>0 or d>0): print("Ya",end=" ") else: print("Tidak",end=" ") if(b>0 or c>0): print("Ya",end=" ") else: print("Tidak",end=" ") print("Tidak Tidak")
Chaneka has a hobby of playing with animal toys. Every toy has a different fun value, a real number. Chaneka has four boxes to store the toys with specification: The first box stores toys with fun values in range of $(-\infty,-1]$. The second box stores toys with fun values in range of $(-1, 0)$. The third box stores toys with fun values in range of $(0, 1)$. The fourth box stores toys with fun value in range of $[1, \infty)$. Chaneka has $A$, $B$, $C$, $D$ toys in the first, second, third, and fourth box, respectively. One day she decides that she only wants one toy, a super toy. So she begins to create this super toy by sewing all the toys she has. While the number of toys Chaneka has is more than 1, she takes two different toys randomly and then sews them together, creating a new toy. The fun value of this new toy is equal to the multiplication of fun values of the sewn toys. She then puts this new toy in the appropriate box. She repeats this process until she only has one toy. This last toy is the super toy, and the box that stores this toy is the special box. As an observer, you only know the number of toys in each box initially but do not know their fun values. You also don't see the sequence of Chaneka's sewing. Determine which boxes can be the special box after Chaneka found her super toy. -----Input----- The first line has an integer $T$ $(1 \le T \le 5 \cdot 10^4)$, the number of test cases. Every case contains a line with four space-separated integers $A$ $B$ $C$ $D$ $(0 \le A, B, C, D \le 10^6, A + B + C + D > 0)$, which denotes the number of toys in the first, second, third, and fourth box, respectively. -----Output----- For each case, print four space-separated strings. Each string represents the possibility that the first, second, third, and fourth box can be the special box from left to right. For each box, print "Ya" (Without quotes, Indonesian for yes) if that box can be the special box. Print "Tidak" (Without quotes, Indonesian for No) otherwise. -----Example----- Input 2 1 2 0 1 0 1 0 0 Output Ya Ya Tidak Tidak Tidak Ya Tidak Tidak -----Note----- For the first case, here is a scenario where the first box is the special box: The first box had toys with fun values $\{-3\}$. The second box had toys with fun values $\{ -0.5, -0.5 \}$ The fourth box had toys with fun values $\{ 3 \}$ The sewing sequence: Chaneka sews the toy with fun $-0.5$ and $-0.5$ to a toy with fun $0.25$ and then put it in the third box. Chaneka sews the toy with fun $-3$ and $0.25$ to a toy with fun $-0.75$ and then put it in the second box. Chaneka sews the toy with fun $-0.75$ and $3$ to a toy with fun $-1.25$ and then put it in the first box, which then became the special box. Here is a scenario where the second box ends up being the special box: The first box had toys with fun values $\{-3\}$ The second box had toys with fun values $\{ -0.33, -0.25 \}$. The fourth box had toys with fun values $\{ 3 \}$. The sewing sequence: Chaneka sews the toy with fun $-3$ and $-0.33$ to a toy with fun $0.99$ and then put it in the third box. Chaneka sews the toy with fun $0.99$ and $3$ to a toy with fun $2.97$ and then put in it the fourth box. Chaneka sews the toy with fun $2.97$ and $-0.25$ to a toy with fun $-0.7425$ and then put it in the second box, which then became the special box. There is only one toy for the second case, so Chaneka does not have to sew anything because that toy, by definition, is the super toy.
import sys t = int(input()) for i in range(t): a1, a2, a3, a4 = list(map(int, input().split())) neg = (a1 + a2) % 2 == 1 large = (a1 == 0 and a4 == 0) small = (a2 == 0 and a3 == 0) r1, r2, r3, r4 = True, True, True, True if(neg): r3, r4 = False, False else: r1, r2 = False, False if large: r1, r4 = False,False if small: r2, r3 = False, False res = '' for j in [r1, r2, r3, r4]: if (j): res += 'Ya ' else: res += 'Tidak ' print(res[:-1])
Chaneka has a hobby of playing with animal toys. Every toy has a different fun value, a real number. Chaneka has four boxes to store the toys with specification: The first box stores toys with fun values in range of $(-\infty,-1]$. The second box stores toys with fun values in range of $(-1, 0)$. The third box stores toys with fun values in range of $(0, 1)$. The fourth box stores toys with fun value in range of $[1, \infty)$. Chaneka has $A$, $B$, $C$, $D$ toys in the first, second, third, and fourth box, respectively. One day she decides that she only wants one toy, a super toy. So she begins to create this super toy by sewing all the toys she has. While the number of toys Chaneka has is more than 1, she takes two different toys randomly and then sews them together, creating a new toy. The fun value of this new toy is equal to the multiplication of fun values of the sewn toys. She then puts this new toy in the appropriate box. She repeats this process until she only has one toy. This last toy is the super toy, and the box that stores this toy is the special box. As an observer, you only know the number of toys in each box initially but do not know their fun values. You also don't see the sequence of Chaneka's sewing. Determine which boxes can be the special box after Chaneka found her super toy. -----Input----- The first line has an integer $T$ $(1 \le T \le 5 \cdot 10^4)$, the number of test cases. Every case contains a line with four space-separated integers $A$ $B$ $C$ $D$ $(0 \le A, B, C, D \le 10^6, A + B + C + D > 0)$, which denotes the number of toys in the first, second, third, and fourth box, respectively. -----Output----- For each case, print four space-separated strings. Each string represents the possibility that the first, second, third, and fourth box can be the special box from left to right. For each box, print "Ya" (Without quotes, Indonesian for yes) if that box can be the special box. Print "Tidak" (Without quotes, Indonesian for No) otherwise. -----Example----- Input 2 1 2 0 1 0 1 0 0 Output Ya Ya Tidak Tidak Tidak Ya Tidak Tidak -----Note----- For the first case, here is a scenario where the first box is the special box: The first box had toys with fun values $\{-3\}$. The second box had toys with fun values $\{ -0.5, -0.5 \}$ The fourth box had toys with fun values $\{ 3 \}$ The sewing sequence: Chaneka sews the toy with fun $-0.5$ and $-0.5$ to a toy with fun $0.25$ and then put it in the third box. Chaneka sews the toy with fun $-3$ and $0.25$ to a toy with fun $-0.75$ and then put it in the second box. Chaneka sews the toy with fun $-0.75$ and $3$ to a toy with fun $-1.25$ and then put it in the first box, which then became the special box. Here is a scenario where the second box ends up being the special box: The first box had toys with fun values $\{-3\}$ The second box had toys with fun values $\{ -0.33, -0.25 \}$. The fourth box had toys with fun values $\{ 3 \}$. The sewing sequence: Chaneka sews the toy with fun $-3$ and $-0.33$ to a toy with fun $0.99$ and then put it in the third box. Chaneka sews the toy with fun $0.99$ and $3$ to a toy with fun $2.97$ and then put in it the fourth box. Chaneka sews the toy with fun $2.97$ and $-0.25$ to a toy with fun $-0.7425$ and then put it in the second box, which then became the special box. There is only one toy for the second case, so Chaneka does not have to sew anything because that toy, by definition, is the super toy.
from sys import stdin, stdout from collections import defaultdict input = stdin.readline for _ in range(int(input())): a, b, c, d = map(int, input().split()) small , large, positive = 0, 0, 1 if a>0 or d>0: large = 1 if b>0 or c>0: small = 1 if (a+b)%2: positive = 0 l = list() if large and not positive: l.append('Ya') else: l.append('Tidak') if small and not positive: l.append('Ya') else: l.append('Tidak') if small and positive: l.append('Ya') else: l.append('Tidak') if large and positive: l.append('Ya') else: l.append('Tidak') print(*l)
Chaneka has a hobby of playing with animal toys. Every toy has a different fun value, a real number. Chaneka has four boxes to store the toys with specification: The first box stores toys with fun values in range of $(-\infty,-1]$. The second box stores toys with fun values in range of $(-1, 0)$. The third box stores toys with fun values in range of $(0, 1)$. The fourth box stores toys with fun value in range of $[1, \infty)$. Chaneka has $A$, $B$, $C$, $D$ toys in the first, second, third, and fourth box, respectively. One day she decides that she only wants one toy, a super toy. So she begins to create this super toy by sewing all the toys she has. While the number of toys Chaneka has is more than 1, she takes two different toys randomly and then sews them together, creating a new toy. The fun value of this new toy is equal to the multiplication of fun values of the sewn toys. She then puts this new toy in the appropriate box. She repeats this process until she only has one toy. This last toy is the super toy, and the box that stores this toy is the special box. As an observer, you only know the number of toys in each box initially but do not know their fun values. You also don't see the sequence of Chaneka's sewing. Determine which boxes can be the special box after Chaneka found her super toy. -----Input----- The first line has an integer $T$ $(1 \le T \le 5 \cdot 10^4)$, the number of test cases. Every case contains a line with four space-separated integers $A$ $B$ $C$ $D$ $(0 \le A, B, C, D \le 10^6, A + B + C + D > 0)$, which denotes the number of toys in the first, second, third, and fourth box, respectively. -----Output----- For each case, print four space-separated strings. Each string represents the possibility that the first, second, third, and fourth box can be the special box from left to right. For each box, print "Ya" (Without quotes, Indonesian for yes) if that box can be the special box. Print "Tidak" (Without quotes, Indonesian for No) otherwise. -----Example----- Input 2 1 2 0 1 0 1 0 0 Output Ya Ya Tidak Tidak Tidak Ya Tidak Tidak -----Note----- For the first case, here is a scenario where the first box is the special box: The first box had toys with fun values $\{-3\}$. The second box had toys with fun values $\{ -0.5, -0.5 \}$ The fourth box had toys with fun values $\{ 3 \}$ The sewing sequence: Chaneka sews the toy with fun $-0.5$ and $-0.5$ to a toy with fun $0.25$ and then put it in the third box. Chaneka sews the toy with fun $-3$ and $0.25$ to a toy with fun $-0.75$ and then put it in the second box. Chaneka sews the toy with fun $-0.75$ and $3$ to a toy with fun $-1.25$ and then put it in the first box, which then became the special box. Here is a scenario where the second box ends up being the special box: The first box had toys with fun values $\{-3\}$ The second box had toys with fun values $\{ -0.33, -0.25 \}$. The fourth box had toys with fun values $\{ 3 \}$. The sewing sequence: Chaneka sews the toy with fun $-3$ and $-0.33$ to a toy with fun $0.99$ and then put it in the third box. Chaneka sews the toy with fun $0.99$ and $3$ to a toy with fun $2.97$ and then put in it the fourth box. Chaneka sews the toy with fun $2.97$ and $-0.25$ to a toy with fun $-0.7425$ and then put it in the second box, which then became the special box. There is only one toy for the second case, so Chaneka does not have to sew anything because that toy, by definition, is the super toy.
t = int(input()) for _ in range(t): a, b, c, d = list(map(int, input().split())) possible = ['Ya', 'Ya', 'Ya', 'Ya'] if (a+b)%2 == 0: possible[0] = 'Tidak' possible[1] = 'Tidak' else: possible[2] = 'Tidak' possible[3] = 'Tidak' if (a+d) == 0: possible[0] = 'Tidak' possible[3] = 'Tidak' if (b+c) == 0: possible[1] = 'Tidak' possible[2] = 'Tidak' print(' '.join(possible))
Chaneka has a hobby of playing with animal toys. Every toy has a different fun value, a real number. Chaneka has four boxes to store the toys with specification: The first box stores toys with fun values in range of $(-\infty,-1]$. The second box stores toys with fun values in range of $(-1, 0)$. The third box stores toys with fun values in range of $(0, 1)$. The fourth box stores toys with fun value in range of $[1, \infty)$. Chaneka has $A$, $B$, $C$, $D$ toys in the first, second, third, and fourth box, respectively. One day she decides that she only wants one toy, a super toy. So she begins to create this super toy by sewing all the toys she has. While the number of toys Chaneka has is more than 1, she takes two different toys randomly and then sews them together, creating a new toy. The fun value of this new toy is equal to the multiplication of fun values of the sewn toys. She then puts this new toy in the appropriate box. She repeats this process until she only has one toy. This last toy is the super toy, and the box that stores this toy is the special box. As an observer, you only know the number of toys in each box initially but do not know their fun values. You also don't see the sequence of Chaneka's sewing. Determine which boxes can be the special box after Chaneka found her super toy. -----Input----- The first line has an integer $T$ $(1 \le T \le 5 \cdot 10^4)$, the number of test cases. Every case contains a line with four space-separated integers $A$ $B$ $C$ $D$ $(0 \le A, B, C, D \le 10^6, A + B + C + D > 0)$, which denotes the number of toys in the first, second, third, and fourth box, respectively. -----Output----- For each case, print four space-separated strings. Each string represents the possibility that the first, second, third, and fourth box can be the special box from left to right. For each box, print "Ya" (Without quotes, Indonesian for yes) if that box can be the special box. Print "Tidak" (Without quotes, Indonesian for No) otherwise. -----Example----- Input 2 1 2 0 1 0 1 0 0 Output Ya Ya Tidak Tidak Tidak Ya Tidak Tidak -----Note----- For the first case, here is a scenario where the first box is the special box: The first box had toys with fun values $\{-3\}$. The second box had toys with fun values $\{ -0.5, -0.5 \}$ The fourth box had toys with fun values $\{ 3 \}$ The sewing sequence: Chaneka sews the toy with fun $-0.5$ and $-0.5$ to a toy with fun $0.25$ and then put it in the third box. Chaneka sews the toy with fun $-3$ and $0.25$ to a toy with fun $-0.75$ and then put it in the second box. Chaneka sews the toy with fun $-0.75$ and $3$ to a toy with fun $-1.25$ and then put it in the first box, which then became the special box. Here is a scenario where the second box ends up being the special box: The first box had toys with fun values $\{-3\}$ The second box had toys with fun values $\{ -0.33, -0.25 \}$. The fourth box had toys with fun values $\{ 3 \}$. The sewing sequence: Chaneka sews the toy with fun $-3$ and $-0.33$ to a toy with fun $0.99$ and then put it in the third box. Chaneka sews the toy with fun $0.99$ and $3$ to a toy with fun $2.97$ and then put in it the fourth box. Chaneka sews the toy with fun $2.97$ and $-0.25$ to a toy with fun $-0.7425$ and then put it in the second box, which then became the special box. There is only one toy for the second case, so Chaneka does not have to sew anything because that toy, by definition, is the super toy.
t=int(input()) while(t>0): t=t-1 l=input().split() a=int(l[0]) b=int(l[1]) c=int(l[2]) d=int(l[3]) # print(a,b,c,d) #for a if(a!=0): if((a+b)%2==1): print("Ya",end=" ") else: print("Tidak",end=" ") else: if(d>=1 and (a+b)%2==1): print("Ya",end=" ") else: print("Tidak",end=" ") if(b!=0): if((a+b)%2): print("Ya",end=" ") else: print("Tidak",end=" ") else: if(c>=1 and (a+b)%2==1): print("Ya",end=" ") else: print("Tidak",end=" ") if(c!=0): if((a+b)%2==0): print("Ya",end=" ") else: print("Tidak",end=" ") else: if(b>=1 and (a+b)%2==0): print("Ya",end=" ") else: print("Tidak",end=" ") if(d!=0): if((a+b)%2==0): print("Ya",end=" ") else: print("Tidak",end=" ") else: if(a>=1 and (a+b)%2==0): print("Ya",end=" ") else: print("Tidak",end=" ") print()
Chaneka has a hobby of playing with animal toys. Every toy has a different fun value, a real number. Chaneka has four boxes to store the toys with specification: The first box stores toys with fun values in range of $(-\infty,-1]$. The second box stores toys with fun values in range of $(-1, 0)$. The third box stores toys with fun values in range of $(0, 1)$. The fourth box stores toys with fun value in range of $[1, \infty)$. Chaneka has $A$, $B$, $C$, $D$ toys in the first, second, third, and fourth box, respectively. One day she decides that she only wants one toy, a super toy. So she begins to create this super toy by sewing all the toys she has. While the number of toys Chaneka has is more than 1, she takes two different toys randomly and then sews them together, creating a new toy. The fun value of this new toy is equal to the multiplication of fun values of the sewn toys. She then puts this new toy in the appropriate box. She repeats this process until she only has one toy. This last toy is the super toy, and the box that stores this toy is the special box. As an observer, you only know the number of toys in each box initially but do not know their fun values. You also don't see the sequence of Chaneka's sewing. Determine which boxes can be the special box after Chaneka found her super toy. -----Input----- The first line has an integer $T$ $(1 \le T \le 5 \cdot 10^4)$, the number of test cases. Every case contains a line with four space-separated integers $A$ $B$ $C$ $D$ $(0 \le A, B, C, D \le 10^6, A + B + C + D > 0)$, which denotes the number of toys in the first, second, third, and fourth box, respectively. -----Output----- For each case, print four space-separated strings. Each string represents the possibility that the first, second, third, and fourth box can be the special box from left to right. For each box, print "Ya" (Without quotes, Indonesian for yes) if that box can be the special box. Print "Tidak" (Without quotes, Indonesian for No) otherwise. -----Example----- Input 2 1 2 0 1 0 1 0 0 Output Ya Ya Tidak Tidak Tidak Ya Tidak Tidak -----Note----- For the first case, here is a scenario where the first box is the special box: The first box had toys with fun values $\{-3\}$. The second box had toys with fun values $\{ -0.5, -0.5 \}$ The fourth box had toys with fun values $\{ 3 \}$ The sewing sequence: Chaneka sews the toy with fun $-0.5$ and $-0.5$ to a toy with fun $0.25$ and then put it in the third box. Chaneka sews the toy with fun $-3$ and $0.25$ to a toy with fun $-0.75$ and then put it in the second box. Chaneka sews the toy with fun $-0.75$ and $3$ to a toy with fun $-1.25$ and then put it in the first box, which then became the special box. Here is a scenario where the second box ends up being the special box: The first box had toys with fun values $\{-3\}$ The second box had toys with fun values $\{ -0.33, -0.25 \}$. The fourth box had toys with fun values $\{ 3 \}$. The sewing sequence: Chaneka sews the toy with fun $-3$ and $-0.33$ to a toy with fun $0.99$ and then put it in the third box. Chaneka sews the toy with fun $0.99$ and $3$ to a toy with fun $2.97$ and then put in it the fourth box. Chaneka sews the toy with fun $2.97$ and $-0.25$ to a toy with fun $-0.7425$ and then put it in the second box, which then became the special box. There is only one toy for the second case, so Chaneka does not have to sew anything because that toy, by definition, is the super toy.
for _ in range(int(input())): a,b,c,d=list(map(int,input().split())) if (a+b)%2==0: if (b!=0 or c!=0) and (a!=0 or d!=0): print("Tidak Tidak Ya Ya") elif d!=0 or a!=0: print("Tidak Tidak Tidak Ya") elif b!=0 or c!=0: print("Tidak Tidak Ya Tidak") else: if (b!=0 or c!=0) and (a!=0 or d!=0): print("Ya Ya Tidak Tidak") elif d!=0 or a!=0: print("Ya Tidak Tidak Tidak") elif b!=0 or c!=0: print("Tidak Ya Tidak Tidak")
Chaneka has a hobby of playing with animal toys. Every toy has a different fun value, a real number. Chaneka has four boxes to store the toys with specification: The first box stores toys with fun values in range of $(-\infty,-1]$. The second box stores toys with fun values in range of $(-1, 0)$. The third box stores toys with fun values in range of $(0, 1)$. The fourth box stores toys with fun value in range of $[1, \infty)$. Chaneka has $A$, $B$, $C$, $D$ toys in the first, second, third, and fourth box, respectively. One day she decides that she only wants one toy, a super toy. So she begins to create this super toy by sewing all the toys she has. While the number of toys Chaneka has is more than 1, she takes two different toys randomly and then sews them together, creating a new toy. The fun value of this new toy is equal to the multiplication of fun values of the sewn toys. She then puts this new toy in the appropriate box. She repeats this process until she only has one toy. This last toy is the super toy, and the box that stores this toy is the special box. As an observer, you only know the number of toys in each box initially but do not know their fun values. You also don't see the sequence of Chaneka's sewing. Determine which boxes can be the special box after Chaneka found her super toy. -----Input----- The first line has an integer $T$ $(1 \le T \le 5 \cdot 10^4)$, the number of test cases. Every case contains a line with four space-separated integers $A$ $B$ $C$ $D$ $(0 \le A, B, C, D \le 10^6, A + B + C + D > 0)$, which denotes the number of toys in the first, second, third, and fourth box, respectively. -----Output----- For each case, print four space-separated strings. Each string represents the possibility that the first, second, third, and fourth box can be the special box from left to right. For each box, print "Ya" (Without quotes, Indonesian for yes) if that box can be the special box. Print "Tidak" (Without quotes, Indonesian for No) otherwise. -----Example----- Input 2 1 2 0 1 0 1 0 0 Output Ya Ya Tidak Tidak Tidak Ya Tidak Tidak -----Note----- For the first case, here is a scenario where the first box is the special box: The first box had toys with fun values $\{-3\}$. The second box had toys with fun values $\{ -0.5, -0.5 \}$ The fourth box had toys with fun values $\{ 3 \}$ The sewing sequence: Chaneka sews the toy with fun $-0.5$ and $-0.5$ to a toy with fun $0.25$ and then put it in the third box. Chaneka sews the toy with fun $-3$ and $0.25$ to a toy with fun $-0.75$ and then put it in the second box. Chaneka sews the toy with fun $-0.75$ and $3$ to a toy with fun $-1.25$ and then put it in the first box, which then became the special box. Here is a scenario where the second box ends up being the special box: The first box had toys with fun values $\{-3\}$ The second box had toys with fun values $\{ -0.33, -0.25 \}$. The fourth box had toys with fun values $\{ 3 \}$. The sewing sequence: Chaneka sews the toy with fun $-3$ and $-0.33$ to a toy with fun $0.99$ and then put it in the third box. Chaneka sews the toy with fun $0.99$ and $3$ to a toy with fun $2.97$ and then put in it the fourth box. Chaneka sews the toy with fun $2.97$ and $-0.25$ to a toy with fun $-0.7425$ and then put it in the second box, which then became the special box. There is only one toy for the second case, so Chaneka does not have to sew anything because that toy, by definition, is the super toy.
t = int(input()) for i in range(t): a, b, c, d = list(map(int, input().split())) ans = ['Tidak', 'Tidak', 'Tidak', 'Tidak'] if ((a + b) % 2 == 1) and ((a + d) > 0): ans[0] = 'Ya' if ((a + b) % 2 == 1) and (((a + d) == 0) or ((b + c) > 0)): ans[1] = 'Ya' if ((a + b) % 2 == 0) and (((a + d) == 0) or ((b + c) > 0)): ans[2] = 'Ya' if ((a + b) % 2 == 0) and ((a + d) > 0): ans[3] = 'Ya' print(' '.join(ans))
You're given an array $a_1, \ldots, a_n$ of $n$ non-negative integers. Let's call it sharpened if and only if there exists an integer $1 \le k \le n$ such that $a_1 < a_2 < \ldots < a_k$ and $a_k > a_{k+1} > \ldots > a_n$. In particular, any strictly increasing or strictly decreasing array is sharpened. For example: The arrays $[4]$, $[0, 1]$, $[12, 10, 8]$ and $[3, 11, 15, 9, 7, 4]$ are sharpened; The arrays $[2, 8, 2, 8, 6, 5]$, $[0, 1, 1, 0]$ and $[2, 5, 6, 9, 8, 8]$ are not sharpened. You can do the following operation as many times as you want: choose any strictly positive element of the array, and decrease it by one. Formally, you can choose any $i$ ($1 \le i \le n$) such that $a_i>0$ and assign $a_i := a_i - 1$. Tell if it's possible to make the given array sharpened using some number (possibly zero) of these operations. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 15\ 000$) ย โ€” the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3 \cdot 10^5$). The second line of each test case contains a sequence of $n$ non-negative integers $a_1, \ldots, a_n$ ($0 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$. -----Output----- For each test case, output a single line containing "Yes" (without quotes) if it's possible to make the given array sharpened using the described operations, or "No" (without quotes) otherwise. -----Example----- Input 10 1 248618 3 12 10 8 6 100 11 15 9 7 8 4 0 1 1 0 2 0 0 2 0 1 2 1 0 2 1 1 3 0 1 0 3 1 0 1 Output Yes Yes Yes No No Yes Yes Yes Yes No -----Note----- In the first and the second test case of the first test, the given array is already sharpened. In the third test case of the first test, we can transform the array into $[3, 11, 15, 9, 7, 4]$ (decrease the first element $97$ times and decrease the last element $4$ times). It is sharpened because $3 < 11 < 15$ and $15 > 9 > 7 > 4$. In the fourth test case of the first test, it's impossible to make the given array sharpened.
for _ in range(int(input())): n=int(input()) li=list(map(int,input().split())) ans=0 for i in range(n): if li[i]>=i: ans+=1 else: break for i in range(n): if li[n-1-i]>=i: ans+=1 else: break if ans>n: print("Yes") else: print("No")
You're given an array $a_1, \ldots, a_n$ of $n$ non-negative integers. Let's call it sharpened if and only if there exists an integer $1 \le k \le n$ such that $a_1 < a_2 < \ldots < a_k$ and $a_k > a_{k+1} > \ldots > a_n$. In particular, any strictly increasing or strictly decreasing array is sharpened. For example: The arrays $[4]$, $[0, 1]$, $[12, 10, 8]$ and $[3, 11, 15, 9, 7, 4]$ are sharpened; The arrays $[2, 8, 2, 8, 6, 5]$, $[0, 1, 1, 0]$ and $[2, 5, 6, 9, 8, 8]$ are not sharpened. You can do the following operation as many times as you want: choose any strictly positive element of the array, and decrease it by one. Formally, you can choose any $i$ ($1 \le i \le n$) such that $a_i>0$ and assign $a_i := a_i - 1$. Tell if it's possible to make the given array sharpened using some number (possibly zero) of these operations. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 15\ 000$) ย โ€” the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3 \cdot 10^5$). The second line of each test case contains a sequence of $n$ non-negative integers $a_1, \ldots, a_n$ ($0 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$. -----Output----- For each test case, output a single line containing "Yes" (without quotes) if it's possible to make the given array sharpened using the described operations, or "No" (without quotes) otherwise. -----Example----- Input 10 1 248618 3 12 10 8 6 100 11 15 9 7 8 4 0 1 1 0 2 0 0 2 0 1 2 1 0 2 1 1 3 0 1 0 3 1 0 1 Output Yes Yes Yes No No Yes Yes Yes Yes No -----Note----- In the first and the second test case of the first test, the given array is already sharpened. In the third test case of the first test, we can transform the array into $[3, 11, 15, 9, 7, 4]$ (decrease the first element $97$ times and decrease the last element $4$ times). It is sharpened because $3 < 11 < 15$ and $15 > 9 > 7 > 4$. In the fourth test case of the first test, it's impossible to make the given array sharpened.
for nt in range(int(input())): n=int(input()) l=list(map(int,input().split())) point = -1 for i in range(n): if l[i]<i: point = i-1 break if point == -1: print ("Yes") else: flag=0 for i in range(n-1,point-1,-1): if l[i]<(n-1-i): flag=1 print ("No") break if flag==0: print ("Yes")
You're given an array $a_1, \ldots, a_n$ of $n$ non-negative integers. Let's call it sharpened if and only if there exists an integer $1 \le k \le n$ such that $a_1 < a_2 < \ldots < a_k$ and $a_k > a_{k+1} > \ldots > a_n$. In particular, any strictly increasing or strictly decreasing array is sharpened. For example: The arrays $[4]$, $[0, 1]$, $[12, 10, 8]$ and $[3, 11, 15, 9, 7, 4]$ are sharpened; The arrays $[2, 8, 2, 8, 6, 5]$, $[0, 1, 1, 0]$ and $[2, 5, 6, 9, 8, 8]$ are not sharpened. You can do the following operation as many times as you want: choose any strictly positive element of the array, and decrease it by one. Formally, you can choose any $i$ ($1 \le i \le n$) such that $a_i>0$ and assign $a_i := a_i - 1$. Tell if it's possible to make the given array sharpened using some number (possibly zero) of these operations. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 15\ 000$) ย โ€” the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3 \cdot 10^5$). The second line of each test case contains a sequence of $n$ non-negative integers $a_1, \ldots, a_n$ ($0 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$. -----Output----- For each test case, output a single line containing "Yes" (without quotes) if it's possible to make the given array sharpened using the described operations, or "No" (without quotes) otherwise. -----Example----- Input 10 1 248618 3 12 10 8 6 100 11 15 9 7 8 4 0 1 1 0 2 0 0 2 0 1 2 1 0 2 1 1 3 0 1 0 3 1 0 1 Output Yes Yes Yes No No Yes Yes Yes Yes No -----Note----- In the first and the second test case of the first test, the given array is already sharpened. In the third test case of the first test, we can transform the array into $[3, 11, 15, 9, 7, 4]$ (decrease the first element $97$ times and decrease the last element $4$ times). It is sharpened because $3 < 11 < 15$ and $15 > 9 > 7 > 4$. In the fourth test case of the first test, it's impossible to make the given array sharpened.
t = int(input().rstrip()) for i in range(t): n = int(input().rstrip()) nums = list(map(int, input().rstrip().split())) forw = 0 back = n-1 for j in range(n): if nums[j] >= j: forw = j else: break for j in range(1, n+1): if nums[-j] >= j-1: back = n-j else: break if forw >= back: print("Yes") else: print("No")
You're given an array $a_1, \ldots, a_n$ of $n$ non-negative integers. Let's call it sharpened if and only if there exists an integer $1 \le k \le n$ such that $a_1 < a_2 < \ldots < a_k$ and $a_k > a_{k+1} > \ldots > a_n$. In particular, any strictly increasing or strictly decreasing array is sharpened. For example: The arrays $[4]$, $[0, 1]$, $[12, 10, 8]$ and $[3, 11, 15, 9, 7, 4]$ are sharpened; The arrays $[2, 8, 2, 8, 6, 5]$, $[0, 1, 1, 0]$ and $[2, 5, 6, 9, 8, 8]$ are not sharpened. You can do the following operation as many times as you want: choose any strictly positive element of the array, and decrease it by one. Formally, you can choose any $i$ ($1 \le i \le n$) such that $a_i>0$ and assign $a_i := a_i - 1$. Tell if it's possible to make the given array sharpened using some number (possibly zero) of these operations. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 15\ 000$) ย โ€” the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3 \cdot 10^5$). The second line of each test case contains a sequence of $n$ non-negative integers $a_1, \ldots, a_n$ ($0 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$. -----Output----- For each test case, output a single line containing "Yes" (without quotes) if it's possible to make the given array sharpened using the described operations, or "No" (without quotes) otherwise. -----Example----- Input 10 1 248618 3 12 10 8 6 100 11 15 9 7 8 4 0 1 1 0 2 0 0 2 0 1 2 1 0 2 1 1 3 0 1 0 3 1 0 1 Output Yes Yes Yes No No Yes Yes Yes Yes No -----Note----- In the first and the second test case of the first test, the given array is already sharpened. In the third test case of the first test, we can transform the array into $[3, 11, 15, 9, 7, 4]$ (decrease the first element $97$ times and decrease the last element $4$ times). It is sharpened because $3 < 11 < 15$ and $15 > 9 > 7 > 4$. In the fourth test case of the first test, it's impossible to make the given array sharpened.
def f(a): for i in range(len(a)): if a[i] < i: return i-1 return len(a)-1 def solve(a): i = f(a) j = len(a) - 1 - f(a[::-1]) return "Yes" if i >= j else "No" n = int(input()) for i in range(n): input() a = list(map(int, input().strip().split())) print(solve(a))
You're given an array $a_1, \ldots, a_n$ of $n$ non-negative integers. Let's call it sharpened if and only if there exists an integer $1 \le k \le n$ such that $a_1 < a_2 < \ldots < a_k$ and $a_k > a_{k+1} > \ldots > a_n$. In particular, any strictly increasing or strictly decreasing array is sharpened. For example: The arrays $[4]$, $[0, 1]$, $[12, 10, 8]$ and $[3, 11, 15, 9, 7, 4]$ are sharpened; The arrays $[2, 8, 2, 8, 6, 5]$, $[0, 1, 1, 0]$ and $[2, 5, 6, 9, 8, 8]$ are not sharpened. You can do the following operation as many times as you want: choose any strictly positive element of the array, and decrease it by one. Formally, you can choose any $i$ ($1 \le i \le n$) such that $a_i>0$ and assign $a_i := a_i - 1$. Tell if it's possible to make the given array sharpened using some number (possibly zero) of these operations. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 15\ 000$) ย โ€” the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3 \cdot 10^5$). The second line of each test case contains a sequence of $n$ non-negative integers $a_1, \ldots, a_n$ ($0 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$. -----Output----- For each test case, output a single line containing "Yes" (without quotes) if it's possible to make the given array sharpened using the described operations, or "No" (without quotes) otherwise. -----Example----- Input 10 1 248618 3 12 10 8 6 100 11 15 9 7 8 4 0 1 1 0 2 0 0 2 0 1 2 1 0 2 1 1 3 0 1 0 3 1 0 1 Output Yes Yes Yes No No Yes Yes Yes Yes No -----Note----- In the first and the second test case of the first test, the given array is already sharpened. In the third test case of the first test, we can transform the array into $[3, 11, 15, 9, 7, 4]$ (decrease the first element $97$ times and decrease the last element $4$ times). It is sharpened because $3 < 11 < 15$ and $15 > 9 > 7 > 4$. In the fourth test case of the first test, it's impossible to make the given array sharpened.
import sys input = sys.stdin.readline t = int(input()) for testcase in range(t): n = int(input()) a = list(map(int,input().split())) left = n-1 for i in range(n): if a[i] <= i-1: left = i-1 break right = 0 for i in range(n-1,-1,-1): if a[i] < n-1-i: right = i+1 break if right <= left: print('Yes') else: print('No')
You're given an array $a_1, \ldots, a_n$ of $n$ non-negative integers. Let's call it sharpened if and only if there exists an integer $1 \le k \le n$ such that $a_1 < a_2 < \ldots < a_k$ and $a_k > a_{k+1} > \ldots > a_n$. In particular, any strictly increasing or strictly decreasing array is sharpened. For example: The arrays $[4]$, $[0, 1]$, $[12, 10, 8]$ and $[3, 11, 15, 9, 7, 4]$ are sharpened; The arrays $[2, 8, 2, 8, 6, 5]$, $[0, 1, 1, 0]$ and $[2, 5, 6, 9, 8, 8]$ are not sharpened. You can do the following operation as many times as you want: choose any strictly positive element of the array, and decrease it by one. Formally, you can choose any $i$ ($1 \le i \le n$) such that $a_i>0$ and assign $a_i := a_i - 1$. Tell if it's possible to make the given array sharpened using some number (possibly zero) of these operations. -----Input----- The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 15\ 000$) ย โ€” the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 3 \cdot 10^5$). The second line of each test case contains a sequence of $n$ non-negative integers $a_1, \ldots, a_n$ ($0 \le a_i \le 10^9$). It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$. -----Output----- For each test case, output a single line containing "Yes" (without quotes) if it's possible to make the given array sharpened using the described operations, or "No" (without quotes) otherwise. -----Example----- Input 10 1 248618 3 12 10 8 6 100 11 15 9 7 8 4 0 1 1 0 2 0 0 2 0 1 2 1 0 2 1 1 3 0 1 0 3 1 0 1 Output Yes Yes Yes No No Yes Yes Yes Yes No -----Note----- In the first and the second test case of the first test, the given array is already sharpened. In the third test case of the first test, we can transform the array into $[3, 11, 15, 9, 7, 4]$ (decrease the first element $97$ times and decrease the last element $4$ times). It is sharpened because $3 < 11 < 15$ and $15 > 9 > 7 > 4$. In the fourth test case of the first test, it's impossible to make the given array sharpened.
T = int(input()) for _ in range(T): N = int(input()) A = list(map(int,input().split())) if N%2: X = [i if i<N//2 else N-i-1 for i in range(N)] if all(a>=x for a,x in zip(A,X)): print('Yes') else: print('No') else: X = [i if i<N//2+1 else N-i-1 for i in range(N)] if all(a>=x for a,x in zip(A,X)): print('Yes') continue X[N//2-1], X[N//2] = X[N//2], X[N//2-1] if all(a>=x for a,x in zip(A,X)): print('Yes') else: print('No')
You have a bag of size $n$. Also you have $m$ boxes. The size of $i$-th box is $a_i$, where each $a_i$ is an integer non-negative power of two. You can divide boxes into two parts of equal size. Your goal is to fill the bag completely. For example, if $n = 10$ and $a = [1, 1, 32]$ then you have to divide the box of size $32$ into two parts of size $16$, and then divide the box of size $16$. So you can fill the bag with boxes of size $1$, $1$ and $8$. Calculate the minimum number of divisions required to fill the bag of size $n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) โ€” the number of test cases. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 10^{18}, 1 \le m \le 10^5$) โ€” the size of bag and the number of boxes, respectively. The second line of each test case contains $m$ integers $a_1, a_2, \dots , a_m$ ($1 \le a_i \le 10^9$) โ€” the sizes of boxes. It is guaranteed that each $a_i$ is a power of two. It is also guaranteed that sum of all $m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer โ€” the minimum number of divisions required to fill the bag of size $n$ (or $-1$, if it is impossible). -----Example----- Input 3 10 3 1 32 1 23 4 16 1 4 1 20 5 2 1 16 1 8 Output 2 -1 0
import sys import math from collections import defaultdict from collections import deque from itertools import combinations from itertools import permutations input = lambda : sys.stdin.readline().rstrip() read = lambda : list(map(int, input().split())) go = lambda : 1/0 def write(*args, sep="\n"): for i in args: sys.stdout.write("{}{}".format(i, sep)) INF = float('inf') MOD = int(1e9 + 7) YES = "YES" NO = -1 for _ in range(int(input())): try: n, m = read() arr = read() x = [0] * 65 if sum(arr) < n: print(NO) go() for i in arr: x[int(math.log2(i))] += 1 ans = 0 for i in range(65): if (1 << i) & n: if x[i] != 0: x[i] -= 1 continue total = 0 for j in range(i): total += (1 << j) * x[j] if total >= (1 << i): temp = 1 << i for j in reversed(range(i)): while temp - (1 << j) >= 0 and x[j] > 0: temp -= 1 << j x[j] -= 1 continue j = i while j < 65 and x[j] == 0: j += 1 if j == 65: print(NO) go() else: x[j] -= 1 for k in range(i, j): x[k] += 1 ans += (j - i) print(ans) except ZeroDivisionError: continue except Exception as e: print(e) continue
You have a bag of size $n$. Also you have $m$ boxes. The size of $i$-th box is $a_i$, where each $a_i$ is an integer non-negative power of two. You can divide boxes into two parts of equal size. Your goal is to fill the bag completely. For example, if $n = 10$ and $a = [1, 1, 32]$ then you have to divide the box of size $32$ into two parts of size $16$, and then divide the box of size $16$. So you can fill the bag with boxes of size $1$, $1$ and $8$. Calculate the minimum number of divisions required to fill the bag of size $n$. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) โ€” the number of test cases. The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 10^{18}, 1 \le m \le 10^5$) โ€” the size of bag and the number of boxes, respectively. The second line of each test case contains $m$ integers $a_1, a_2, \dots , a_m$ ($1 \le a_i \le 10^9$) โ€” the sizes of boxes. It is guaranteed that each $a_i$ is a power of two. It is also guaranteed that sum of all $m$ over all test cases does not exceed $10^5$. -----Output----- For each test case print one integer โ€” the minimum number of divisions required to fill the bag of size $n$ (or $-1$, if it is impossible). -----Example----- Input 3 10 3 1 32 1 23 4 16 1 4 1 20 5 2 1 16 1 8 Output 2 -1 0
import math t = int(input()) M2 = [1] for i in range(35): M2.append(M2[-1]*2) for i in range(t): n, m = map(int,input().split()) A = list(map(int,input().split())) if sum(A) < n: print(-1) else: B = [0] * 33 for i in range(m): B[int(math.log2(A[i]))] += 1 # print(B[:10]) C = [0] * 33 nn = n for i in range(33): C[i] = nn%2 nn//=2 if nn==0: break # print(C) b = 0 c = 0 i = 0 ans = 0 ok = 0 while i < len(B): while i < len(B) and b >= c: b += B[i] * M2[i] c += C[i] * M2[i] B[i]=0 i += 1 if i == len(B) and b >= c: print(ans) ok = 1 break else: i-=1 while B[i] == 0: i += 1 ans += 1 # print("ansplus",i) B[i] -= 1 b=0 c=0 if ok==1: break
On February 14 Denis decided to give Valentine to Nastya and did not come up with anything better than to draw a huge red heart on the door of the length $k$ ($k \ge 3$). Nastya was very confused by this present, so she decided to break the door, throwing it on the mountains. Mountains are described by a sequence of heights $a_1, a_2, \dots, a_n$ in order from left to right ($k \le n$). It is guaranteed that neighboring heights are not equal to each other (that is, $a_i \ne a_{i+1}$ for all $i$ from $1$ to $n-1$). Peaks of mountains on the segment $[l,r]$ (from $l$ to $r$) are called indexes $i$ such that $l < i < r$, $a_{i - 1} < a_i$ and $a_i > a_{i + 1}$. It is worth noting that the boundary indexes $l$ and $r$ for the segment are not peaks. For example, if $n=8$ and $a=[3,1,4,1,5,9,2,6]$, then the segment $[1,8]$ has only two peaks (with indexes $3$ and $6$), and there are no peaks on the segment $[3, 6]$. To break the door, Nastya throws it to a segment $[l,l+k-1]$ of consecutive mountains of length $k$ ($1 \le l \le n-k+1$). When the door touches the peaks of the mountains, it breaks into two parts, after that these parts will continue to fall in different halves and also break into pieces when touching the peaks of the mountains, and so on. Formally, the number of parts that the door will break into will be equal to $p+1$, where $p$ is the number of peaks on the segment $[l,l+k-1]$. Nastya wants to break it into as many pieces as possible. Help her choose such a segment of mountains $[l, l+k-1]$ that the number of peaks on it is maximum. If there are several optimal segments, Nastya wants to find one for which the value $l$ is minimal. Formally, you need to choose a segment of mountains $[l, l+k-1]$ that has the maximum number of peaks. Among all such segments, you need to find the segment that has the minimum possible value $l$. -----Input----- The first line contains an integer $t$ ($1 \leq t \leq 10^4$) ย โ€” the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains two integers $n$ and $k$ ($3 \leq k \leq n \leq 2 \cdot 10^5$) ย โ€” the number of mountains and the length of the door. The second line of the input data set contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \leq a_i \leq 10 ^ 9$, $a_i \neq a_{i + 1}$) ย โ€” the heights of mountains. It is guaranteed that the sum of $n$ over all the test cases will not exceed $2 \cdot 10^5$. -----Output----- For each test case, output two integers $t$ and $l$ ย โ€” the maximum number of parts that the door can split into, and the left border of the segment of length $k$ that the door should be reset to. -----Example----- Input 5 8 6 1 2 4 1 2 4 1 2 5 3 3 2 3 2 1 10 4 4 3 4 3 2 3 2 1 0 1 15 7 3 7 4 8 2 3 4 5 21 2 3 4 2 1 3 7 5 1 2 3 4 5 6 1 Output 3 2 2 2 2 1 3 1 2 3 -----Note----- In the first example, you need to select a segment of mountains from $2$ to $7$. In this segment, the indexes $3$ and $6$ are peaks, so the answer is $3$ (only $2$ peaks, so the door will break into $3$ parts). It is not difficult to notice that the mountain segments $[1, 6]$ and $[3, 8]$ are not suitable since they only have a $1$ peak (for the first segment, the $6$ index is not a peak, and for the second segment, the $3$ index is not a peak). In the second example, you need to select a segment of mountains from $2$ to $4$. In this segment, the index $3$ is a peak, so the answer is $2$ (only $1$ peak, so the door will break into $2$ parts). In the third example, you need to select a segment of mountains from $1$ to $4$. In this segment, the index $3$ is a peak, so the answer is $2$ (only $1$ peak, so the door will break into $2$ parts). You can see that on the segments $[2, 5]$, $[4, 7]$ and $[5, 8]$ the number of peaks is also $1$, but these segments have a left border greater than the segment $[1, 4]$, so they are not the correct answer.
for _ in range(int(input())): n, k = tuple(map(int, input().split())) arr = list(map(int, input().split())) peaks = [0 for i in range(n)] for i in range(1, n - 1): if arr[i] > arr[i - 1] and arr[i] > arr[i + 1]: peaks[i] = 1 cnt = 0 max_peaks = 0 answer = 0 for i in range(k - 1): cnt += peaks[i] max_peaks += peaks[i] for i in range(k - 1, n - 1): cnt -= peaks[i - k + 2] cnt += peaks[i] if cnt > max_peaks: max_peaks = cnt answer = i - k + 2 print(max_peaks + 1, answer + 1)
On February 14 Denis decided to give Valentine to Nastya and did not come up with anything better than to draw a huge red heart on the door of the length $k$ ($k \ge 3$). Nastya was very confused by this present, so she decided to break the door, throwing it on the mountains. Mountains are described by a sequence of heights $a_1, a_2, \dots, a_n$ in order from left to right ($k \le n$). It is guaranteed that neighboring heights are not equal to each other (that is, $a_i \ne a_{i+1}$ for all $i$ from $1$ to $n-1$). Peaks of mountains on the segment $[l,r]$ (from $l$ to $r$) are called indexes $i$ such that $l < i < r$, $a_{i - 1} < a_i$ and $a_i > a_{i + 1}$. It is worth noting that the boundary indexes $l$ and $r$ for the segment are not peaks. For example, if $n=8$ and $a=[3,1,4,1,5,9,2,6]$, then the segment $[1,8]$ has only two peaks (with indexes $3$ and $6$), and there are no peaks on the segment $[3, 6]$. To break the door, Nastya throws it to a segment $[l,l+k-1]$ of consecutive mountains of length $k$ ($1 \le l \le n-k+1$). When the door touches the peaks of the mountains, it breaks into two parts, after that these parts will continue to fall in different halves and also break into pieces when touching the peaks of the mountains, and so on. Formally, the number of parts that the door will break into will be equal to $p+1$, where $p$ is the number of peaks on the segment $[l,l+k-1]$. Nastya wants to break it into as many pieces as possible. Help her choose such a segment of mountains $[l, l+k-1]$ that the number of peaks on it is maximum. If there are several optimal segments, Nastya wants to find one for which the value $l$ is minimal. Formally, you need to choose a segment of mountains $[l, l+k-1]$ that has the maximum number of peaks. Among all such segments, you need to find the segment that has the minimum possible value $l$. -----Input----- The first line contains an integer $t$ ($1 \leq t \leq 10^4$) ย โ€” the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains two integers $n$ and $k$ ($3 \leq k \leq n \leq 2 \cdot 10^5$) ย โ€” the number of mountains and the length of the door. The second line of the input data set contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \leq a_i \leq 10 ^ 9$, $a_i \neq a_{i + 1}$) ย โ€” the heights of mountains. It is guaranteed that the sum of $n$ over all the test cases will not exceed $2 \cdot 10^5$. -----Output----- For each test case, output two integers $t$ and $l$ ย โ€” the maximum number of parts that the door can split into, and the left border of the segment of length $k$ that the door should be reset to. -----Example----- Input 5 8 6 1 2 4 1 2 4 1 2 5 3 3 2 3 2 1 10 4 4 3 4 3 2 3 2 1 0 1 15 7 3 7 4 8 2 3 4 5 21 2 3 4 2 1 3 7 5 1 2 3 4 5 6 1 Output 3 2 2 2 2 1 3 1 2 3 -----Note----- In the first example, you need to select a segment of mountains from $2$ to $7$. In this segment, the indexes $3$ and $6$ are peaks, so the answer is $3$ (only $2$ peaks, so the door will break into $3$ parts). It is not difficult to notice that the mountain segments $[1, 6]$ and $[3, 8]$ are not suitable since they only have a $1$ peak (for the first segment, the $6$ index is not a peak, and for the second segment, the $3$ index is not a peak). In the second example, you need to select a segment of mountains from $2$ to $4$. In this segment, the index $3$ is a peak, so the answer is $2$ (only $1$ peak, so the door will break into $2$ parts). In the third example, you need to select a segment of mountains from $1$ to $4$. In this segment, the index $3$ is a peak, so the answer is $2$ (only $1$ peak, so the door will break into $2$ parts). You can see that on the segments $[2, 5]$, $[4, 7]$ and $[5, 8]$ the number of peaks is also $1$, but these segments have a left border greater than the segment $[1, 4]$, so they are not the correct answer.
from math import * from random import * for t in range(int(input())): n, k = map(int, input().split()) mas = list(map(int, input().split())) pick = [0 for i in range(n)] for i in range(1, n - 1): pick[i] = pick[i - 1] if mas[i] > mas[i - 1] and mas[i] > mas[i + 1]: pick[i] += 1 if n > 1: pick[n - 1] = pick[n - 2] mx = 0 mxotv = 0 for i in range(0, n - k + 1): if i + k - 2 < 0: continue res = pick[i + k - 2] res -= pick[i] if res > mx: mx = res mxotv = i print(mx + 1, mxotv + 1)
On February 14 Denis decided to give Valentine to Nastya and did not come up with anything better than to draw a huge red heart on the door of the length $k$ ($k \ge 3$). Nastya was very confused by this present, so she decided to break the door, throwing it on the mountains. Mountains are described by a sequence of heights $a_1, a_2, \dots, a_n$ in order from left to right ($k \le n$). It is guaranteed that neighboring heights are not equal to each other (that is, $a_i \ne a_{i+1}$ for all $i$ from $1$ to $n-1$). Peaks of mountains on the segment $[l,r]$ (from $l$ to $r$) are called indexes $i$ such that $l < i < r$, $a_{i - 1} < a_i$ and $a_i > a_{i + 1}$. It is worth noting that the boundary indexes $l$ and $r$ for the segment are not peaks. For example, if $n=8$ and $a=[3,1,4,1,5,9,2,6]$, then the segment $[1,8]$ has only two peaks (with indexes $3$ and $6$), and there are no peaks on the segment $[3, 6]$. To break the door, Nastya throws it to a segment $[l,l+k-1]$ of consecutive mountains of length $k$ ($1 \le l \le n-k+1$). When the door touches the peaks of the mountains, it breaks into two parts, after that these parts will continue to fall in different halves and also break into pieces when touching the peaks of the mountains, and so on. Formally, the number of parts that the door will break into will be equal to $p+1$, where $p$ is the number of peaks on the segment $[l,l+k-1]$. Nastya wants to break it into as many pieces as possible. Help her choose such a segment of mountains $[l, l+k-1]$ that the number of peaks on it is maximum. If there are several optimal segments, Nastya wants to find one for which the value $l$ is minimal. Formally, you need to choose a segment of mountains $[l, l+k-1]$ that has the maximum number of peaks. Among all such segments, you need to find the segment that has the minimum possible value $l$. -----Input----- The first line contains an integer $t$ ($1 \leq t \leq 10^4$) ย โ€” the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains two integers $n$ and $k$ ($3 \leq k \leq n \leq 2 \cdot 10^5$) ย โ€” the number of mountains and the length of the door. The second line of the input data set contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \leq a_i \leq 10 ^ 9$, $a_i \neq a_{i + 1}$) ย โ€” the heights of mountains. It is guaranteed that the sum of $n$ over all the test cases will not exceed $2 \cdot 10^5$. -----Output----- For each test case, output two integers $t$ and $l$ ย โ€” the maximum number of parts that the door can split into, and the left border of the segment of length $k$ that the door should be reset to. -----Example----- Input 5 8 6 1 2 4 1 2 4 1 2 5 3 3 2 3 2 1 10 4 4 3 4 3 2 3 2 1 0 1 15 7 3 7 4 8 2 3 4 5 21 2 3 4 2 1 3 7 5 1 2 3 4 5 6 1 Output 3 2 2 2 2 1 3 1 2 3 -----Note----- In the first example, you need to select a segment of mountains from $2$ to $7$. In this segment, the indexes $3$ and $6$ are peaks, so the answer is $3$ (only $2$ peaks, so the door will break into $3$ parts). It is not difficult to notice that the mountain segments $[1, 6]$ and $[3, 8]$ are not suitable since they only have a $1$ peak (for the first segment, the $6$ index is not a peak, and for the second segment, the $3$ index is not a peak). In the second example, you need to select a segment of mountains from $2$ to $4$. In this segment, the index $3$ is a peak, so the answer is $2$ (only $1$ peak, so the door will break into $2$ parts). In the third example, you need to select a segment of mountains from $1$ to $4$. In this segment, the index $3$ is a peak, so the answer is $2$ (only $1$ peak, so the door will break into $2$ parts). You can see that on the segments $[2, 5]$, $[4, 7]$ and $[5, 8]$ the number of peaks is also $1$, but these segments have a left border greater than the segment $[1, 4]$, so they are not the correct answer.
#list(map(int,input().split())) t=int(input()) for _ in range(t): n,k=list(map(int,input().split())) aa=list(map(int,input().split())) tot=0 ind=1 for i in range(1,k-1): if(aa[i]>aa[i-1] and aa[i]>aa[i+1]): tot+=1 # print(tot) ma=tot+1 for i in range(1,n): if(i+k-1>=n): continue if(aa[i]>aa[i-1] and aa[i]>aa[i+1]): tot-=1 if(aa[i+k-2]>aa[i+k-3] and aa[i+k-2]>aa[i+k-1]): tot+=1 if(tot+1>ma): ma=tot+1 ind=i+1 # print(tot) print(ma,ind)
On February 14 Denis decided to give Valentine to Nastya and did not come up with anything better than to draw a huge red heart on the door of the length $k$ ($k \ge 3$). Nastya was very confused by this present, so she decided to break the door, throwing it on the mountains. Mountains are described by a sequence of heights $a_1, a_2, \dots, a_n$ in order from left to right ($k \le n$). It is guaranteed that neighboring heights are not equal to each other (that is, $a_i \ne a_{i+1}$ for all $i$ from $1$ to $n-1$). Peaks of mountains on the segment $[l,r]$ (from $l$ to $r$) are called indexes $i$ such that $l < i < r$, $a_{i - 1} < a_i$ and $a_i > a_{i + 1}$. It is worth noting that the boundary indexes $l$ and $r$ for the segment are not peaks. For example, if $n=8$ and $a=[3,1,4,1,5,9,2,6]$, then the segment $[1,8]$ has only two peaks (with indexes $3$ and $6$), and there are no peaks on the segment $[3, 6]$. To break the door, Nastya throws it to a segment $[l,l+k-1]$ of consecutive mountains of length $k$ ($1 \le l \le n-k+1$). When the door touches the peaks of the mountains, it breaks into two parts, after that these parts will continue to fall in different halves and also break into pieces when touching the peaks of the mountains, and so on. Formally, the number of parts that the door will break into will be equal to $p+1$, where $p$ is the number of peaks on the segment $[l,l+k-1]$. Nastya wants to break it into as many pieces as possible. Help her choose such a segment of mountains $[l, l+k-1]$ that the number of peaks on it is maximum. If there are several optimal segments, Nastya wants to find one for which the value $l$ is minimal. Formally, you need to choose a segment of mountains $[l, l+k-1]$ that has the maximum number of peaks. Among all such segments, you need to find the segment that has the minimum possible value $l$. -----Input----- The first line contains an integer $t$ ($1 \leq t \leq 10^4$) ย โ€” the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains two integers $n$ and $k$ ($3 \leq k \leq n \leq 2 \cdot 10^5$) ย โ€” the number of mountains and the length of the door. The second line of the input data set contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \leq a_i \leq 10 ^ 9$, $a_i \neq a_{i + 1}$) ย โ€” the heights of mountains. It is guaranteed that the sum of $n$ over all the test cases will not exceed $2 \cdot 10^5$. -----Output----- For each test case, output two integers $t$ and $l$ ย โ€” the maximum number of parts that the door can split into, and the left border of the segment of length $k$ that the door should be reset to. -----Example----- Input 5 8 6 1 2 4 1 2 4 1 2 5 3 3 2 3 2 1 10 4 4 3 4 3 2 3 2 1 0 1 15 7 3 7 4 8 2 3 4 5 21 2 3 4 2 1 3 7 5 1 2 3 4 5 6 1 Output 3 2 2 2 2 1 3 1 2 3 -----Note----- In the first example, you need to select a segment of mountains from $2$ to $7$. In this segment, the indexes $3$ and $6$ are peaks, so the answer is $3$ (only $2$ peaks, so the door will break into $3$ parts). It is not difficult to notice that the mountain segments $[1, 6]$ and $[3, 8]$ are not suitable since they only have a $1$ peak (for the first segment, the $6$ index is not a peak, and for the second segment, the $3$ index is not a peak). In the second example, you need to select a segment of mountains from $2$ to $4$. In this segment, the index $3$ is a peak, so the answer is $2$ (only $1$ peak, so the door will break into $2$ parts). In the third example, you need to select a segment of mountains from $1$ to $4$. In this segment, the index $3$ is a peak, so the answer is $2$ (only $1$ peak, so the door will break into $2$ parts). You can see that on the segments $[2, 5]$, $[4, 7]$ and $[5, 8]$ the number of peaks is also $1$, but these segments have a left border greater than the segment $[1, 4]$, so they are not the correct answer.
t = int(input()) for qq in range(t): n, k = list(map(int, input().split())) m = list(map(int, input().split())) p = 0 for i in range(n - k + 1, n - 1): if m[i] > m[i - 1] and m[i] > m[i + 1]: p += 1 mp = p ii = n - k + 1 for i in range(n - k, 0, -1): if m[i] > m[i - 1] and m[i] > m[i + 1]: p += 1 if m[i + k - 2] > m[i + k - 3] and m[i + k - 2] > m[i + k - 1]: p -= 1 if p >= mp: mp = p ii = i print(mp + 1, ii)
On February 14 Denis decided to give Valentine to Nastya and did not come up with anything better than to draw a huge red heart on the door of the length $k$ ($k \ge 3$). Nastya was very confused by this present, so she decided to break the door, throwing it on the mountains. Mountains are described by a sequence of heights $a_1, a_2, \dots, a_n$ in order from left to right ($k \le n$). It is guaranteed that neighboring heights are not equal to each other (that is, $a_i \ne a_{i+1}$ for all $i$ from $1$ to $n-1$). Peaks of mountains on the segment $[l,r]$ (from $l$ to $r$) are called indexes $i$ such that $l < i < r$, $a_{i - 1} < a_i$ and $a_i > a_{i + 1}$. It is worth noting that the boundary indexes $l$ and $r$ for the segment are not peaks. For example, if $n=8$ and $a=[3,1,4,1,5,9,2,6]$, then the segment $[1,8]$ has only two peaks (with indexes $3$ and $6$), and there are no peaks on the segment $[3, 6]$. To break the door, Nastya throws it to a segment $[l,l+k-1]$ of consecutive mountains of length $k$ ($1 \le l \le n-k+1$). When the door touches the peaks of the mountains, it breaks into two parts, after that these parts will continue to fall in different halves and also break into pieces when touching the peaks of the mountains, and so on. Formally, the number of parts that the door will break into will be equal to $p+1$, where $p$ is the number of peaks on the segment $[l,l+k-1]$. Nastya wants to break it into as many pieces as possible. Help her choose such a segment of mountains $[l, l+k-1]$ that the number of peaks on it is maximum. If there are several optimal segments, Nastya wants to find one for which the value $l$ is minimal. Formally, you need to choose a segment of mountains $[l, l+k-1]$ that has the maximum number of peaks. Among all such segments, you need to find the segment that has the minimum possible value $l$. -----Input----- The first line contains an integer $t$ ($1 \leq t \leq 10^4$) ย โ€” the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains two integers $n$ and $k$ ($3 \leq k \leq n \leq 2 \cdot 10^5$) ย โ€” the number of mountains and the length of the door. The second line of the input data set contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \leq a_i \leq 10 ^ 9$, $a_i \neq a_{i + 1}$) ย โ€” the heights of mountains. It is guaranteed that the sum of $n$ over all the test cases will not exceed $2 \cdot 10^5$. -----Output----- For each test case, output two integers $t$ and $l$ ย โ€” the maximum number of parts that the door can split into, and the left border of the segment of length $k$ that the door should be reset to. -----Example----- Input 5 8 6 1 2 4 1 2 4 1 2 5 3 3 2 3 2 1 10 4 4 3 4 3 2 3 2 1 0 1 15 7 3 7 4 8 2 3 4 5 21 2 3 4 2 1 3 7 5 1 2 3 4 5 6 1 Output 3 2 2 2 2 1 3 1 2 3 -----Note----- In the first example, you need to select a segment of mountains from $2$ to $7$. In this segment, the indexes $3$ and $6$ are peaks, so the answer is $3$ (only $2$ peaks, so the door will break into $3$ parts). It is not difficult to notice that the mountain segments $[1, 6]$ and $[3, 8]$ are not suitable since they only have a $1$ peak (for the first segment, the $6$ index is not a peak, and for the second segment, the $3$ index is not a peak). In the second example, you need to select a segment of mountains from $2$ to $4$. In this segment, the index $3$ is a peak, so the answer is $2$ (only $1$ peak, so the door will break into $2$ parts). In the third example, you need to select a segment of mountains from $1$ to $4$. In this segment, the index $3$ is a peak, so the answer is $2$ (only $1$ peak, so the door will break into $2$ parts). You can see that on the segments $[2, 5]$, $[4, 7]$ and $[5, 8]$ the number of peaks is also $1$, but these segments have a left border greater than the segment $[1, 4]$, so they are not the correct answer.
t=int(input()) for _ in range(t): n,k=map(int,input().split()) arr=list(map(int,input().split())) peaks=[0]*(n) for i in range(1,n-1): if arr[i]>arr[i-1] and arr[i]>arr[i+1]: peaks[i]=1 acum=[0] for i in range(1,n): acum.append(acum[-1]+peaks[i]) maxs=0 pos=-1 for i in range(n-k,-1,-1): tmp=acum[i+k-1]-acum[i] if peaks[i+k-1]==1: tmp-=1 if tmp>=maxs: maxs=tmp pos=i print(maxs+1,pos+1)
On February 14 Denis decided to give Valentine to Nastya and did not come up with anything better than to draw a huge red heart on the door of the length $k$ ($k \ge 3$). Nastya was very confused by this present, so she decided to break the door, throwing it on the mountains. Mountains are described by a sequence of heights $a_1, a_2, \dots, a_n$ in order from left to right ($k \le n$). It is guaranteed that neighboring heights are not equal to each other (that is, $a_i \ne a_{i+1}$ for all $i$ from $1$ to $n-1$). Peaks of mountains on the segment $[l,r]$ (from $l$ to $r$) are called indexes $i$ such that $l < i < r$, $a_{i - 1} < a_i$ and $a_i > a_{i + 1}$. It is worth noting that the boundary indexes $l$ and $r$ for the segment are not peaks. For example, if $n=8$ and $a=[3,1,4,1,5,9,2,6]$, then the segment $[1,8]$ has only two peaks (with indexes $3$ and $6$), and there are no peaks on the segment $[3, 6]$. To break the door, Nastya throws it to a segment $[l,l+k-1]$ of consecutive mountains of length $k$ ($1 \le l \le n-k+1$). When the door touches the peaks of the mountains, it breaks into two parts, after that these parts will continue to fall in different halves and also break into pieces when touching the peaks of the mountains, and so on. Formally, the number of parts that the door will break into will be equal to $p+1$, where $p$ is the number of peaks on the segment $[l,l+k-1]$. Nastya wants to break it into as many pieces as possible. Help her choose such a segment of mountains $[l, l+k-1]$ that the number of peaks on it is maximum. If there are several optimal segments, Nastya wants to find one for which the value $l$ is minimal. Formally, you need to choose a segment of mountains $[l, l+k-1]$ that has the maximum number of peaks. Among all such segments, you need to find the segment that has the minimum possible value $l$. -----Input----- The first line contains an integer $t$ ($1 \leq t \leq 10^4$) ย โ€” the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains two integers $n$ and $k$ ($3 \leq k \leq n \leq 2 \cdot 10^5$) ย โ€” the number of mountains and the length of the door. The second line of the input data set contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \leq a_i \leq 10 ^ 9$, $a_i \neq a_{i + 1}$) ย โ€” the heights of mountains. It is guaranteed that the sum of $n$ over all the test cases will not exceed $2 \cdot 10^5$. -----Output----- For each test case, output two integers $t$ and $l$ ย โ€” the maximum number of parts that the door can split into, and the left border of the segment of length $k$ that the door should be reset to. -----Example----- Input 5 8 6 1 2 4 1 2 4 1 2 5 3 3 2 3 2 1 10 4 4 3 4 3 2 3 2 1 0 1 15 7 3 7 4 8 2 3 4 5 21 2 3 4 2 1 3 7 5 1 2 3 4 5 6 1 Output 3 2 2 2 2 1 3 1 2 3 -----Note----- In the first example, you need to select a segment of mountains from $2$ to $7$. In this segment, the indexes $3$ and $6$ are peaks, so the answer is $3$ (only $2$ peaks, so the door will break into $3$ parts). It is not difficult to notice that the mountain segments $[1, 6]$ and $[3, 8]$ are not suitable since they only have a $1$ peak (for the first segment, the $6$ index is not a peak, and for the second segment, the $3$ index is not a peak). In the second example, you need to select a segment of mountains from $2$ to $4$. In this segment, the index $3$ is a peak, so the answer is $2$ (only $1$ peak, so the door will break into $2$ parts). In the third example, you need to select a segment of mountains from $1$ to $4$. In this segment, the index $3$ is a peak, so the answer is $2$ (only $1$ peak, so the door will break into $2$ parts). You can see that on the segments $[2, 5]$, $[4, 7]$ and $[5, 8]$ the number of peaks is also $1$, but these segments have a left border greater than the segment $[1, 4]$, so they are not the correct answer.
from _collections import deque for _ in range(int(input())): n, k = list(map(int, input().split())) ar = list(map(int, input().split())) picks = 0 lol = deque([]) for i in range(1, k - 1): if ar[i - 1] < ar[i] > ar[i + 1]: picks += 1 lol.append(i) max_picks = picks ans = 0 for i in range(k - 1, n - 1): if len(lol) > 0 and lol[0] == i - k + 2: lol.popleft() picks -= 1 if ar[i - 1] < ar[i] > ar[i + 1]: picks += 1 lol.append(i) if picks > max_picks: max_picks = picks ans = i - k + 2 print(max_picks + 1, ans + 1)
On February 14 Denis decided to give Valentine to Nastya and did not come up with anything better than to draw a huge red heart on the door of the length $k$ ($k \ge 3$). Nastya was very confused by this present, so she decided to break the door, throwing it on the mountains. Mountains are described by a sequence of heights $a_1, a_2, \dots, a_n$ in order from left to right ($k \le n$). It is guaranteed that neighboring heights are not equal to each other (that is, $a_i \ne a_{i+1}$ for all $i$ from $1$ to $n-1$). Peaks of mountains on the segment $[l,r]$ (from $l$ to $r$) are called indexes $i$ such that $l < i < r$, $a_{i - 1} < a_i$ and $a_i > a_{i + 1}$. It is worth noting that the boundary indexes $l$ and $r$ for the segment are not peaks. For example, if $n=8$ and $a=[3,1,4,1,5,9,2,6]$, then the segment $[1,8]$ has only two peaks (with indexes $3$ and $6$), and there are no peaks on the segment $[3, 6]$. To break the door, Nastya throws it to a segment $[l,l+k-1]$ of consecutive mountains of length $k$ ($1 \le l \le n-k+1$). When the door touches the peaks of the mountains, it breaks into two parts, after that these parts will continue to fall in different halves and also break into pieces when touching the peaks of the mountains, and so on. Formally, the number of parts that the door will break into will be equal to $p+1$, where $p$ is the number of peaks on the segment $[l,l+k-1]$. Nastya wants to break it into as many pieces as possible. Help her choose such a segment of mountains $[l, l+k-1]$ that the number of peaks on it is maximum. If there are several optimal segments, Nastya wants to find one for which the value $l$ is minimal. Formally, you need to choose a segment of mountains $[l, l+k-1]$ that has the maximum number of peaks. Among all such segments, you need to find the segment that has the minimum possible value $l$. -----Input----- The first line contains an integer $t$ ($1 \leq t \leq 10^4$) ย โ€” the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains two integers $n$ and $k$ ($3 \leq k \leq n \leq 2 \cdot 10^5$) ย โ€” the number of mountains and the length of the door. The second line of the input data set contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \leq a_i \leq 10 ^ 9$, $a_i \neq a_{i + 1}$) ย โ€” the heights of mountains. It is guaranteed that the sum of $n$ over all the test cases will not exceed $2 \cdot 10^5$. -----Output----- For each test case, output two integers $t$ and $l$ ย โ€” the maximum number of parts that the door can split into, and the left border of the segment of length $k$ that the door should be reset to. -----Example----- Input 5 8 6 1 2 4 1 2 4 1 2 5 3 3 2 3 2 1 10 4 4 3 4 3 2 3 2 1 0 1 15 7 3 7 4 8 2 3 4 5 21 2 3 4 2 1 3 7 5 1 2 3 4 5 6 1 Output 3 2 2 2 2 1 3 1 2 3 -----Note----- In the first example, you need to select a segment of mountains from $2$ to $7$. In this segment, the indexes $3$ and $6$ are peaks, so the answer is $3$ (only $2$ peaks, so the door will break into $3$ parts). It is not difficult to notice that the mountain segments $[1, 6]$ and $[3, 8]$ are not suitable since they only have a $1$ peak (for the first segment, the $6$ index is not a peak, and for the second segment, the $3$ index is not a peak). In the second example, you need to select a segment of mountains from $2$ to $4$. In this segment, the index $3$ is a peak, so the answer is $2$ (only $1$ peak, so the door will break into $2$ parts). In the third example, you need to select a segment of mountains from $1$ to $4$. In this segment, the index $3$ is a peak, so the answer is $2$ (only $1$ peak, so the door will break into $2$ parts). You can see that on the segments $[2, 5]$, $[4, 7]$ and $[5, 8]$ the number of peaks is also $1$, but these segments have a left border greater than the segment $[1, 4]$, so they are not the correct answer.
def mult_input(): return map(int,input().split()) def list_input(): return list(map(int,input().split())) for nt in range(int(input())): n,k=mult_input() l=list(map(int,input().split())) ans=0 for i in range(1,k-1): if l[i]>l[i-1] and l[i]>l[i+1]: ans+=1 ind=1 i=1 count=ans while i<n-k+1: if l[i]>l[i-1] and l[i]>l[i+1]: count-=1 if l[i+k-2]>l[i+k-3] and l[i+k-2]>l[i+k-1]: count+=1 if count>ans: ans=count ind=i+1 i+=1 print (ans+1,ind)
On February 14 Denis decided to give Valentine to Nastya and did not come up with anything better than to draw a huge red heart on the door of the length $k$ ($k \ge 3$). Nastya was very confused by this present, so she decided to break the door, throwing it on the mountains. Mountains are described by a sequence of heights $a_1, a_2, \dots, a_n$ in order from left to right ($k \le n$). It is guaranteed that neighboring heights are not equal to each other (that is, $a_i \ne a_{i+1}$ for all $i$ from $1$ to $n-1$). Peaks of mountains on the segment $[l,r]$ (from $l$ to $r$) are called indexes $i$ such that $l < i < r$, $a_{i - 1} < a_i$ and $a_i > a_{i + 1}$. It is worth noting that the boundary indexes $l$ and $r$ for the segment are not peaks. For example, if $n=8$ and $a=[3,1,4,1,5,9,2,6]$, then the segment $[1,8]$ has only two peaks (with indexes $3$ and $6$), and there are no peaks on the segment $[3, 6]$. To break the door, Nastya throws it to a segment $[l,l+k-1]$ of consecutive mountains of length $k$ ($1 \le l \le n-k+1$). When the door touches the peaks of the mountains, it breaks into two parts, after that these parts will continue to fall in different halves and also break into pieces when touching the peaks of the mountains, and so on. Formally, the number of parts that the door will break into will be equal to $p+1$, where $p$ is the number of peaks on the segment $[l,l+k-1]$. Nastya wants to break it into as many pieces as possible. Help her choose such a segment of mountains $[l, l+k-1]$ that the number of peaks on it is maximum. If there are several optimal segments, Nastya wants to find one for which the value $l$ is minimal. Formally, you need to choose a segment of mountains $[l, l+k-1]$ that has the maximum number of peaks. Among all such segments, you need to find the segment that has the minimum possible value $l$. -----Input----- The first line contains an integer $t$ ($1 \leq t \leq 10^4$) ย โ€” the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains two integers $n$ and $k$ ($3 \leq k \leq n \leq 2 \cdot 10^5$) ย โ€” the number of mountains and the length of the door. The second line of the input data set contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \leq a_i \leq 10 ^ 9$, $a_i \neq a_{i + 1}$) ย โ€” the heights of mountains. It is guaranteed that the sum of $n$ over all the test cases will not exceed $2 \cdot 10^5$. -----Output----- For each test case, output two integers $t$ and $l$ ย โ€” the maximum number of parts that the door can split into, and the left border of the segment of length $k$ that the door should be reset to. -----Example----- Input 5 8 6 1 2 4 1 2 4 1 2 5 3 3 2 3 2 1 10 4 4 3 4 3 2 3 2 1 0 1 15 7 3 7 4 8 2 3 4 5 21 2 3 4 2 1 3 7 5 1 2 3 4 5 6 1 Output 3 2 2 2 2 1 3 1 2 3 -----Note----- In the first example, you need to select a segment of mountains from $2$ to $7$. In this segment, the indexes $3$ and $6$ are peaks, so the answer is $3$ (only $2$ peaks, so the door will break into $3$ parts). It is not difficult to notice that the mountain segments $[1, 6]$ and $[3, 8]$ are not suitable since they only have a $1$ peak (for the first segment, the $6$ index is not a peak, and for the second segment, the $3$ index is not a peak). In the second example, you need to select a segment of mountains from $2$ to $4$. In this segment, the index $3$ is a peak, so the answer is $2$ (only $1$ peak, so the door will break into $2$ parts). In the third example, you need to select a segment of mountains from $1$ to $4$. In this segment, the index $3$ is a peak, so the answer is $2$ (only $1$ peak, so the door will break into $2$ parts). You can see that on the segments $[2, 5]$, $[4, 7]$ and $[5, 8]$ the number of peaks is also $1$, but these segments have a left border greater than the segment $[1, 4]$, so they are not the correct answer.
from collections import defaultdict as dd def ri(): return int(input()) def rl(): return list(map(int, input().split())) def solve(): n, k = rl() A = rl() peaks = [] for i in range(1, n - 1): if A[i] > max(A[i - 1], A[i + 1]): peaks.append(1) else: peaks.append(0) best = sum(peaks[:k-2]) curr = best best_l = 0 for i in range(1, n - (k - 1)): curr -= peaks[i - 1] curr += peaks[i + k - 3] if curr > best: best = curr best_l = i return best + 1, best_l + 1 t = ri() for i in range(t): print(*solve())
On February 14 Denis decided to give Valentine to Nastya and did not come up with anything better than to draw a huge red heart on the door of the length $k$ ($k \ge 3$). Nastya was very confused by this present, so she decided to break the door, throwing it on the mountains. Mountains are described by a sequence of heights $a_1, a_2, \dots, a_n$ in order from left to right ($k \le n$). It is guaranteed that neighboring heights are not equal to each other (that is, $a_i \ne a_{i+1}$ for all $i$ from $1$ to $n-1$). Peaks of mountains on the segment $[l,r]$ (from $l$ to $r$) are called indexes $i$ such that $l < i < r$, $a_{i - 1} < a_i$ and $a_i > a_{i + 1}$. It is worth noting that the boundary indexes $l$ and $r$ for the segment are not peaks. For example, if $n=8$ and $a=[3,1,4,1,5,9,2,6]$, then the segment $[1,8]$ has only two peaks (with indexes $3$ and $6$), and there are no peaks on the segment $[3, 6]$. To break the door, Nastya throws it to a segment $[l,l+k-1]$ of consecutive mountains of length $k$ ($1 \le l \le n-k+1$). When the door touches the peaks of the mountains, it breaks into two parts, after that these parts will continue to fall in different halves and also break into pieces when touching the peaks of the mountains, and so on. Formally, the number of parts that the door will break into will be equal to $p+1$, where $p$ is the number of peaks on the segment $[l,l+k-1]$. Nastya wants to break it into as many pieces as possible. Help her choose such a segment of mountains $[l, l+k-1]$ that the number of peaks on it is maximum. If there are several optimal segments, Nastya wants to find one for which the value $l$ is minimal. Formally, you need to choose a segment of mountains $[l, l+k-1]$ that has the maximum number of peaks. Among all such segments, you need to find the segment that has the minimum possible value $l$. -----Input----- The first line contains an integer $t$ ($1 \leq t \leq 10^4$) ย โ€” the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains two integers $n$ and $k$ ($3 \leq k \leq n \leq 2 \cdot 10^5$) ย โ€” the number of mountains and the length of the door. The second line of the input data set contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \leq a_i \leq 10 ^ 9$, $a_i \neq a_{i + 1}$) ย โ€” the heights of mountains. It is guaranteed that the sum of $n$ over all the test cases will not exceed $2 \cdot 10^5$. -----Output----- For each test case, output two integers $t$ and $l$ ย โ€” the maximum number of parts that the door can split into, and the left border of the segment of length $k$ that the door should be reset to. -----Example----- Input 5 8 6 1 2 4 1 2 4 1 2 5 3 3 2 3 2 1 10 4 4 3 4 3 2 3 2 1 0 1 15 7 3 7 4 8 2 3 4 5 21 2 3 4 2 1 3 7 5 1 2 3 4 5 6 1 Output 3 2 2 2 2 1 3 1 2 3 -----Note----- In the first example, you need to select a segment of mountains from $2$ to $7$. In this segment, the indexes $3$ and $6$ are peaks, so the answer is $3$ (only $2$ peaks, so the door will break into $3$ parts). It is not difficult to notice that the mountain segments $[1, 6]$ and $[3, 8]$ are not suitable since they only have a $1$ peak (for the first segment, the $6$ index is not a peak, and for the second segment, the $3$ index is not a peak). In the second example, you need to select a segment of mountains from $2$ to $4$. In this segment, the index $3$ is a peak, so the answer is $2$ (only $1$ peak, so the door will break into $2$ parts). In the third example, you need to select a segment of mountains from $1$ to $4$. In this segment, the index $3$ is a peak, so the answer is $2$ (only $1$ peak, so the door will break into $2$ parts). You can see that on the segments $[2, 5]$, $[4, 7]$ and $[5, 8]$ the number of peaks is also $1$, but these segments have a left border greater than the segment $[1, 4]$, so they are not the correct answer.
import sys lines = sys.stdin.readlines() # nums = lists(map(int, lines[0].strip().split(" "))) T = int(lines[0].strip()) for t in range(T): (n, k) = list(map(int, lines[2*t+1].strip().split(" "))) nums = list(map(int, lines[2*t+2].strip().split(" "))) peaks = [0 for _ in range(n)] for i in range(1, n-1): if nums[i] > nums[i-1] and nums[i] > nums[i+1]: peaks[i] = 1 for i in range(1, n): peaks[i] += peaks[i-1] maxP = -1 maxIndex = -1 for i in range(n-k+1): if peaks[i+k-2] - peaks[i] > maxP: maxP = peaks[i+k-2] - peaks[i] maxIndex = i print("{} {}".format(maxP+1, maxIndex+1))
On February 14 Denis decided to give Valentine to Nastya and did not come up with anything better than to draw a huge red heart on the door of the length $k$ ($k \ge 3$). Nastya was very confused by this present, so she decided to break the door, throwing it on the mountains. Mountains are described by a sequence of heights $a_1, a_2, \dots, a_n$ in order from left to right ($k \le n$). It is guaranteed that neighboring heights are not equal to each other (that is, $a_i \ne a_{i+1}$ for all $i$ from $1$ to $n-1$). Peaks of mountains on the segment $[l,r]$ (from $l$ to $r$) are called indexes $i$ such that $l < i < r$, $a_{i - 1} < a_i$ and $a_i > a_{i + 1}$. It is worth noting that the boundary indexes $l$ and $r$ for the segment are not peaks. For example, if $n=8$ and $a=[3,1,4,1,5,9,2,6]$, then the segment $[1,8]$ has only two peaks (with indexes $3$ and $6$), and there are no peaks on the segment $[3, 6]$. To break the door, Nastya throws it to a segment $[l,l+k-1]$ of consecutive mountains of length $k$ ($1 \le l \le n-k+1$). When the door touches the peaks of the mountains, it breaks into two parts, after that these parts will continue to fall in different halves and also break into pieces when touching the peaks of the mountains, and so on. Formally, the number of parts that the door will break into will be equal to $p+1$, where $p$ is the number of peaks on the segment $[l,l+k-1]$. Nastya wants to break it into as many pieces as possible. Help her choose such a segment of mountains $[l, l+k-1]$ that the number of peaks on it is maximum. If there are several optimal segments, Nastya wants to find one for which the value $l$ is minimal. Formally, you need to choose a segment of mountains $[l, l+k-1]$ that has the maximum number of peaks. Among all such segments, you need to find the segment that has the minimum possible value $l$. -----Input----- The first line contains an integer $t$ ($1 \leq t \leq 10^4$) ย โ€” the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains two integers $n$ and $k$ ($3 \leq k \leq n \leq 2 \cdot 10^5$) ย โ€” the number of mountains and the length of the door. The second line of the input data set contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \leq a_i \leq 10 ^ 9$, $a_i \neq a_{i + 1}$) ย โ€” the heights of mountains. It is guaranteed that the sum of $n$ over all the test cases will not exceed $2 \cdot 10^5$. -----Output----- For each test case, output two integers $t$ and $l$ ย โ€” the maximum number of parts that the door can split into, and the left border of the segment of length $k$ that the door should be reset to. -----Example----- Input 5 8 6 1 2 4 1 2 4 1 2 5 3 3 2 3 2 1 10 4 4 3 4 3 2 3 2 1 0 1 15 7 3 7 4 8 2 3 4 5 21 2 3 4 2 1 3 7 5 1 2 3 4 5 6 1 Output 3 2 2 2 2 1 3 1 2 3 -----Note----- In the first example, you need to select a segment of mountains from $2$ to $7$. In this segment, the indexes $3$ and $6$ are peaks, so the answer is $3$ (only $2$ peaks, so the door will break into $3$ parts). It is not difficult to notice that the mountain segments $[1, 6]$ and $[3, 8]$ are not suitable since they only have a $1$ peak (for the first segment, the $6$ index is not a peak, and for the second segment, the $3$ index is not a peak). In the second example, you need to select a segment of mountains from $2$ to $4$. In this segment, the index $3$ is a peak, so the answer is $2$ (only $1$ peak, so the door will break into $2$ parts). In the third example, you need to select a segment of mountains from $1$ to $4$. In this segment, the index $3$ is a peak, so the answer is $2$ (only $1$ peak, so the door will break into $2$ parts). You can see that on the segments $[2, 5]$, $[4, 7]$ and $[5, 8]$ the number of peaks is also $1$, but these segments have a left border greater than the segment $[1, 4]$, so they are not the correct answer.
import sys input = sys.stdin.readline for _ in range(int(input())): n, k = list(map(int, input().split())) a = list(map(int, input().split())) peak = [0] + [1 if a[i - 1] < a[i] and a[i] > a[i + 1] else 0 for i in range(1, n - 1)] + [0] b = [None] * (n - k + 1) b[0] = sum(peak[1 : k - 1]) for i in range(1, n - k + 1): b[i] = b[i - 1] - peak[i] + peak[i + k - 2] p = max(b) print(p + 1, b.index(p) + 1)
On February 14 Denis decided to give Valentine to Nastya and did not come up with anything better than to draw a huge red heart on the door of the length $k$ ($k \ge 3$). Nastya was very confused by this present, so she decided to break the door, throwing it on the mountains. Mountains are described by a sequence of heights $a_1, a_2, \dots, a_n$ in order from left to right ($k \le n$). It is guaranteed that neighboring heights are not equal to each other (that is, $a_i \ne a_{i+1}$ for all $i$ from $1$ to $n-1$). Peaks of mountains on the segment $[l,r]$ (from $l$ to $r$) are called indexes $i$ such that $l < i < r$, $a_{i - 1} < a_i$ and $a_i > a_{i + 1}$. It is worth noting that the boundary indexes $l$ and $r$ for the segment are not peaks. For example, if $n=8$ and $a=[3,1,4,1,5,9,2,6]$, then the segment $[1,8]$ has only two peaks (with indexes $3$ and $6$), and there are no peaks on the segment $[3, 6]$. To break the door, Nastya throws it to a segment $[l,l+k-1]$ of consecutive mountains of length $k$ ($1 \le l \le n-k+1$). When the door touches the peaks of the mountains, it breaks into two parts, after that these parts will continue to fall in different halves and also break into pieces when touching the peaks of the mountains, and so on. Formally, the number of parts that the door will break into will be equal to $p+1$, where $p$ is the number of peaks on the segment $[l,l+k-1]$. Nastya wants to break it into as many pieces as possible. Help her choose such a segment of mountains $[l, l+k-1]$ that the number of peaks on it is maximum. If there are several optimal segments, Nastya wants to find one for which the value $l$ is minimal. Formally, you need to choose a segment of mountains $[l, l+k-1]$ that has the maximum number of peaks. Among all such segments, you need to find the segment that has the minimum possible value $l$. -----Input----- The first line contains an integer $t$ ($1 \leq t \leq 10^4$) ย โ€” the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains two integers $n$ and $k$ ($3 \leq k \leq n \leq 2 \cdot 10^5$) ย โ€” the number of mountains and the length of the door. The second line of the input data set contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \leq a_i \leq 10 ^ 9$, $a_i \neq a_{i + 1}$) ย โ€” the heights of mountains. It is guaranteed that the sum of $n$ over all the test cases will not exceed $2 \cdot 10^5$. -----Output----- For each test case, output two integers $t$ and $l$ ย โ€” the maximum number of parts that the door can split into, and the left border of the segment of length $k$ that the door should be reset to. -----Example----- Input 5 8 6 1 2 4 1 2 4 1 2 5 3 3 2 3 2 1 10 4 4 3 4 3 2 3 2 1 0 1 15 7 3 7 4 8 2 3 4 5 21 2 3 4 2 1 3 7 5 1 2 3 4 5 6 1 Output 3 2 2 2 2 1 3 1 2 3 -----Note----- In the first example, you need to select a segment of mountains from $2$ to $7$. In this segment, the indexes $3$ and $6$ are peaks, so the answer is $3$ (only $2$ peaks, so the door will break into $3$ parts). It is not difficult to notice that the mountain segments $[1, 6]$ and $[3, 8]$ are not suitable since they only have a $1$ peak (for the first segment, the $6$ index is not a peak, and for the second segment, the $3$ index is not a peak). In the second example, you need to select a segment of mountains from $2$ to $4$. In this segment, the index $3$ is a peak, so the answer is $2$ (only $1$ peak, so the door will break into $2$ parts). In the third example, you need to select a segment of mountains from $1$ to $4$. In this segment, the index $3$ is a peak, so the answer is $2$ (only $1$ peak, so the door will break into $2$ parts). You can see that on the segments $[2, 5]$, $[4, 7]$ and $[5, 8]$ the number of peaks is also $1$, but these segments have a left border greater than the segment $[1, 4]$, so they are not the correct answer.
import sys # from collections import defaultdict # t=1 t=int(input()) for i in range(t): # n=int(input()) # n,m=list(map(int,sys.stdin.readline().strip().split())) # a,b,c,d=list(sys.stdin.readline().strip().split()) n,k=list(map(int,sys.stdin.readline().strip().split())) # if(n*(a+b)>=(c-d) and n*(a-b)<=(c+d)): # print("YES") # else: # print("NO") a=list(map(int,sys.stdin.readline().strip().split())) x=[0]*n for j in range(1,n-1): if(a[j]>a[j-1] and a[j]>a[j+1]): x[j]=1 # print(a) # print(x) k=k-2 op=0 curr=0 curr=sum(x[:k]) # print(x) # print(curr) op=curr op1=1 for j in range(k,n): # op=max(op,curr) curr=curr+x[j]-x[j-k] if(curr>op): # print("here") op1=j-k+1 op=curr # op=max(op,curr) op=max(op,curr) print(op+1,op1)
On February 14 Denis decided to give Valentine to Nastya and did not come up with anything better than to draw a huge red heart on the door of the length $k$ ($k \ge 3$). Nastya was very confused by this present, so she decided to break the door, throwing it on the mountains. Mountains are described by a sequence of heights $a_1, a_2, \dots, a_n$ in order from left to right ($k \le n$). It is guaranteed that neighboring heights are not equal to each other (that is, $a_i \ne a_{i+1}$ for all $i$ from $1$ to $n-1$). Peaks of mountains on the segment $[l,r]$ (from $l$ to $r$) are called indexes $i$ such that $l < i < r$, $a_{i - 1} < a_i$ and $a_i > a_{i + 1}$. It is worth noting that the boundary indexes $l$ and $r$ for the segment are not peaks. For example, if $n=8$ and $a=[3,1,4,1,5,9,2,6]$, then the segment $[1,8]$ has only two peaks (with indexes $3$ and $6$), and there are no peaks on the segment $[3, 6]$. To break the door, Nastya throws it to a segment $[l,l+k-1]$ of consecutive mountains of length $k$ ($1 \le l \le n-k+1$). When the door touches the peaks of the mountains, it breaks into two parts, after that these parts will continue to fall in different halves and also break into pieces when touching the peaks of the mountains, and so on. Formally, the number of parts that the door will break into will be equal to $p+1$, where $p$ is the number of peaks on the segment $[l,l+k-1]$. Nastya wants to break it into as many pieces as possible. Help her choose such a segment of mountains $[l, l+k-1]$ that the number of peaks on it is maximum. If there are several optimal segments, Nastya wants to find one for which the value $l$ is minimal. Formally, you need to choose a segment of mountains $[l, l+k-1]$ that has the maximum number of peaks. Among all such segments, you need to find the segment that has the minimum possible value $l$. -----Input----- The first line contains an integer $t$ ($1 \leq t \leq 10^4$) ย โ€” the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains two integers $n$ and $k$ ($3 \leq k \leq n \leq 2 \cdot 10^5$) ย โ€” the number of mountains and the length of the door. The second line of the input data set contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \leq a_i \leq 10 ^ 9$, $a_i \neq a_{i + 1}$) ย โ€” the heights of mountains. It is guaranteed that the sum of $n$ over all the test cases will not exceed $2 \cdot 10^5$. -----Output----- For each test case, output two integers $t$ and $l$ ย โ€” the maximum number of parts that the door can split into, and the left border of the segment of length $k$ that the door should be reset to. -----Example----- Input 5 8 6 1 2 4 1 2 4 1 2 5 3 3 2 3 2 1 10 4 4 3 4 3 2 3 2 1 0 1 15 7 3 7 4 8 2 3 4 5 21 2 3 4 2 1 3 7 5 1 2 3 4 5 6 1 Output 3 2 2 2 2 1 3 1 2 3 -----Note----- In the first example, you need to select a segment of mountains from $2$ to $7$. In this segment, the indexes $3$ and $6$ are peaks, so the answer is $3$ (only $2$ peaks, so the door will break into $3$ parts). It is not difficult to notice that the mountain segments $[1, 6]$ and $[3, 8]$ are not suitable since they only have a $1$ peak (for the first segment, the $6$ index is not a peak, and for the second segment, the $3$ index is not a peak). In the second example, you need to select a segment of mountains from $2$ to $4$. In this segment, the index $3$ is a peak, so the answer is $2$ (only $1$ peak, so the door will break into $2$ parts). In the third example, you need to select a segment of mountains from $1$ to $4$. In this segment, the index $3$ is a peak, so the answer is $2$ (only $1$ peak, so the door will break into $2$ parts). You can see that on the segments $[2, 5]$, $[4, 7]$ and $[5, 8]$ the number of peaks is also $1$, but these segments have a left border greater than the segment $[1, 4]$, so they are not the correct answer.
t = int(input()) for ii in range(t): n, k = map(int, input().split()) a = list(map(int, input().split())) p = [0] * n for i in range(1, n - 1): if a[i] > a[i - 1] and a[i] > a[i + 1]: p[i] = 1 cur = 0 ind = k - 1 for i in range(k): if i != 0 and i != k - 1: cur += p[i] ans = cur ans_ind = k - 1 while ind < n: if p[ind - k + 2]: cur -= 1 if p[ind]: cur += 1 if cur > ans: ans = cur ans_ind = ind + 1 ind += 1 print(ans + 1, ans_ind - k + 2)
On February 14 Denis decided to give Valentine to Nastya and did not come up with anything better than to draw a huge red heart on the door of the length $k$ ($k \ge 3$). Nastya was very confused by this present, so she decided to break the door, throwing it on the mountains. Mountains are described by a sequence of heights $a_1, a_2, \dots, a_n$ in order from left to right ($k \le n$). It is guaranteed that neighboring heights are not equal to each other (that is, $a_i \ne a_{i+1}$ for all $i$ from $1$ to $n-1$). Peaks of mountains on the segment $[l,r]$ (from $l$ to $r$) are called indexes $i$ such that $l < i < r$, $a_{i - 1} < a_i$ and $a_i > a_{i + 1}$. It is worth noting that the boundary indexes $l$ and $r$ for the segment are not peaks. For example, if $n=8$ and $a=[3,1,4,1,5,9,2,6]$, then the segment $[1,8]$ has only two peaks (with indexes $3$ and $6$), and there are no peaks on the segment $[3, 6]$. To break the door, Nastya throws it to a segment $[l,l+k-1]$ of consecutive mountains of length $k$ ($1 \le l \le n-k+1$). When the door touches the peaks of the mountains, it breaks into two parts, after that these parts will continue to fall in different halves and also break into pieces when touching the peaks of the mountains, and so on. Formally, the number of parts that the door will break into will be equal to $p+1$, where $p$ is the number of peaks on the segment $[l,l+k-1]$. Nastya wants to break it into as many pieces as possible. Help her choose such a segment of mountains $[l, l+k-1]$ that the number of peaks on it is maximum. If there are several optimal segments, Nastya wants to find one for which the value $l$ is minimal. Formally, you need to choose a segment of mountains $[l, l+k-1]$ that has the maximum number of peaks. Among all such segments, you need to find the segment that has the minimum possible value $l$. -----Input----- The first line contains an integer $t$ ($1 \leq t \leq 10^4$) ย โ€” the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains two integers $n$ and $k$ ($3 \leq k \leq n \leq 2 \cdot 10^5$) ย โ€” the number of mountains and the length of the door. The second line of the input data set contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \leq a_i \leq 10 ^ 9$, $a_i \neq a_{i + 1}$) ย โ€” the heights of mountains. It is guaranteed that the sum of $n$ over all the test cases will not exceed $2 \cdot 10^5$. -----Output----- For each test case, output two integers $t$ and $l$ ย โ€” the maximum number of parts that the door can split into, and the left border of the segment of length $k$ that the door should be reset to. -----Example----- Input 5 8 6 1 2 4 1 2 4 1 2 5 3 3 2 3 2 1 10 4 4 3 4 3 2 3 2 1 0 1 15 7 3 7 4 8 2 3 4 5 21 2 3 4 2 1 3 7 5 1 2 3 4 5 6 1 Output 3 2 2 2 2 1 3 1 2 3 -----Note----- In the first example, you need to select a segment of mountains from $2$ to $7$. In this segment, the indexes $3$ and $6$ are peaks, so the answer is $3$ (only $2$ peaks, so the door will break into $3$ parts). It is not difficult to notice that the mountain segments $[1, 6]$ and $[3, 8]$ are not suitable since they only have a $1$ peak (for the first segment, the $6$ index is not a peak, and for the second segment, the $3$ index is not a peak). In the second example, you need to select a segment of mountains from $2$ to $4$. In this segment, the index $3$ is a peak, so the answer is $2$ (only $1$ peak, so the door will break into $2$ parts). In the third example, you need to select a segment of mountains from $1$ to $4$. In this segment, the index $3$ is a peak, so the answer is $2$ (only $1$ peak, so the door will break into $2$ parts). You can see that on the segments $[2, 5]$, $[4, 7]$ and $[5, 8]$ the number of peaks is also $1$, but these segments have a left border greater than the segment $[1, 4]$, so they are not the correct answer.
t = int(input()) for i in range(t): n, k = list(map(int, input().split())) a = list(map(int, input().split())) cnt = 0 for j in range(k): if 0 < j < k - 1 and a[j - 1] < a[j] > a[j + 1]: cnt += 1 ans = cnt l = 0 for j in range(n - k): if a[j + 2] < a[j + 1] > a[j]: cnt -= 1 if a[j + k - 2] < a[j + k - 1] > a[j + k]: cnt += 1 if cnt > ans: ans = cnt l = j + 1 print(ans + 1, l + 1)
You are given an image, that can be represented with a 2-d n by m grid of pixels. Each pixel of the image is either on or off, denoted by the characters "0" or "1", respectively. You would like to compress this image. You want to choose an integer k > 1 and split the image into k by k blocks. If n and m are not divisible by k, the image is padded with only zeros on the right and bottom so that they are divisible by k. Each pixel in each individual block must have the same value. The given image may not be compressible in its current state. Find the minimum number of pixels you need to toggle (after padding) in order for the image to be compressible for some k. More specifically, the steps are to first choose k, then the image is padded with zeros, then, we can toggle the pixels so it is compressible for this k. The image must be compressible in that state. -----Input----- The first line of input will contain two integers n, m (2 โ‰ค n, m โ‰ค 2 500), the dimensions of the image. The next n lines of input will contain a binary string with exactly m characters, representing the image. -----Output----- Print a single integer, the minimum number of pixels needed to toggle to make the image compressible. -----Example----- Input 3 5 00100 10110 11001 Output 5 -----Note----- We first choose k = 2. The image is padded as follows: 001000 101100 110010 000000 We can toggle the image to look as follows: 001100 001100 000000 000000 We can see that this image is compressible for k = 2.
#!/usr/bin/env python # coding:utf-8 # Copyright (C) dirlt from sys import stdin def run(n, m, pixels): ans = 1 << 30 acc = [[0] * (m + 1) for _ in range(n + 1)] for i in range(n): for j in range(m): acc[i + 1][j + 1] = acc[i + 1][j] + int(pixels[i][j]) for j in range(m): acc[i + 1][j + 1] += acc[i][j + 1] # print(acc) for k in range(2, max(n, m) + 1): r, c = (n + k - 1) // k, (m + k - 1) // k res = 0 for i in range(r): for j in range(c): x, y = i * k, j * k x2, y2 = min(x + k - 1, n - 1), min(y + k - 1, m - 1) zero = acc[x2 + 1][y2 + 1] - acc[x][y2 + 1] - acc[x2 + 1][y] + acc[x][y] # print(x, y, k, zero, k * k - zero) res += min(zero, k * k - zero) # print(k, res) ans = min(ans, res) print(ans) def main(): n, m = [int(x) for x in stdin.readline().split()] pixels = [] for i in range(n): pixels.append(stdin.readline().strip()) run(n, m, pixels) def __starting_point(): import os if os.path.exists('tmp.in'): stdin = open('tmp.in') main() __starting_point()
Now that Heidi has made sure her Zombie Contamination level checker works, it's time to strike! This time, the zombie lair is a strictly convex polygon on the lattice. Each vertex of the polygon occupies a point on the lattice. For each cell of the lattice, Heidi knows the level of Zombie Contamination โ€“ the number of corners of the cell that are inside or on the border of the lair. Given this information, Heidi wants to know the exact shape of the lair to rain destruction on the zombies. Help her! [Image] -----Input----- The input contains multiple test cases. The first line of each test case contains one integer N, the size of the lattice grid (5 โ‰ค N โ‰ค 500). The next N lines each contain N characters, describing the level of Zombie Contamination of each cell in the lattice. Every character of every line is a digit between 0 and 4. Cells are given in the same order as they are shown in the picture above: rows go in the decreasing value of y coordinate, and in one row cells go in the order of increasing x coordinate. This means that the first row corresponds to cells with coordinates (1, N), ..., (N, N) and the last row corresponds to cells with coordinates (1, 1), ..., (N, 1). The last line of the file contains a zero. This line should not be treated as a test case. The sum of the N values for all tests in one file will not exceed 5000. -----Output----- For each test case, give the following output: The first line of the output should contain one integer V, the number of vertices of the polygon that is the secret lair. The next V lines each should contain two integers, denoting the vertices of the polygon in the clockwise order, starting from the lexicographically smallest vertex. -----Examples----- Input 8 00000000 00000110 00012210 01234200 02444200 01223200 00001100 00000000 5 00000 01210 02420 01210 00000 7 0000000 0122100 0134200 0013200 0002200 0001100 0000000 0 Output 4 2 3 2 4 6 6 5 2 4 2 2 2 3 3 3 3 2 3 2 5 4 5 4 2 -----Note----- It is guaranteed that the solution always exists and is unique. It is guaranteed that in the correct solution the coordinates of the polygon vertices are between 2 and N - 2. A vertex (x_1, y_1) is lexicographically smaller than vertex (x_2, y_2) if x_1 < x_2 or $x_{1} = x_{2} \wedge y_{1} < y_{2}$.
import math def lexComp(a, b): if a[0] != b[0]: return -1 if a[0] < b[0] else 1 if a[1] != b[1]: return -1 if a[1] < b[1] else 1 return 0 def turn(a, b, c): return (b[0] - a[0]) * (c[1] - b[1]) - (b[1] - a[1]) * (c[0] - b[0]) def dist2(a, b): return (a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2 def solve(n): a = [list(map(int, input())) for _ in range(n)] points = [] for i in range(n): for j in range(n): if a[i][j] == 1: curPoints = [] for dx in range(0, 2): for dy in range(0, 2): ok = True for ddx in range(0, 2): for ddy in range(0, 2): x, y = i - 1 + dx + ddx, j - 1 + dy + ddy if 0 <= x < n and 0 <= y < n and a[x][y] == 0: ok = False if ok: curPoints.append((i + dx, j + dy)) points.append(curPoints[0]) points = list(set(points)) for i in range(1, len(points)): if lexComp(points[0], points[i]) > 0: points[0], points[i] = points[i], points[0] points[1:] = sorted(points[1:], key=lambda p: (math.atan2(p[1] - points[0][1], p[0] - points[0][0]), dist2(p, points[0]))) hull = [] for p in points: while len(hull) >= 2 and turn(hull[-2], hull[-1], p) <= 0: hull.pop() hull.append(p) hull = [(p[1], n - p[0]) for p in hull] hull = hull[::-1] start = 0 for i in range(1, len(hull)): if lexComp(hull[i], hull[start]) < 0: start = i newHull = hull[start:] newHull.extend(hull[:start]) hull = newHull print(len(hull)) for p in hull: print(p[0], p[1]) while True: n = int(input()) if n == 0: break solve(n)
Bob watches TV every day. He always sets the volume of his TV to $b$. However, today he is angry to find out someone has changed the volume to $a$. Of course, Bob has a remote control that can change the volume. There are six buttons ($-5, -2, -1, +1, +2, +5$) on the control, which in one press can either increase or decrease the current volume by $1$, $2$, or $5$. The volume can be arbitrarily large, but can never be negative. In other words, Bob cannot press the button if it causes the volume to be lower than $0$. As Bob is so angry, he wants to change the volume to $b$ using as few button presses as possible. However, he forgets how to do such simple calculations, so he asks you for help. Write a program that given $a$ and $b$, finds the minimum number of presses to change the TV volume from $a$ to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $T$ ($1 \le T \le 1\,000$). Then the descriptions of the test cases follow. Each test case consists of one line containing two integers $a$ and $b$ ($0 \le a, b \le 10^{9}$)ย โ€” the current volume and Bob's desired volume, respectively. -----Output----- For each test case, output a single integerย โ€” the minimum number of presses to change the TV volume from $a$ to $b$. If Bob does not need to change the volume (i.e. $a=b$), then print $0$. -----Example----- Input 3 4 0 5 14 3 9 Output 2 3 2 -----Note----- In the first example, Bob can press the $-2$ button twice to reach $0$. Note that Bob can not press $-5$ when the volume is $4$ since it will make the volume negative. In the second example, one of the optimal ways for Bob is to press the $+5$ twice, then press $-1$ once. In the last example, Bob can press the $+5$ once, then press $+1$.
import math from decimal import Decimal import heapq from collections import deque def na(): n = int(input()) b = [int(x) for x in input().split()] return n,b def nab(): n = int(input()) b = [int(x) for x in input().split()] c = [int(x) for x in input().split()] return n,b,c def dv(): n, m = list(map(int, input().split())) return n,m def dva(): n, m = list(map(int, input().split())) a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] return n,m,b def eratosthenes(n): sieve = list(range(n + 1)) for i in sieve: if i > 1: for j in range(i + i, len(sieve), i): sieve[j] = 0 return sorted(set(sieve)) def lol(lst,k): k=k%len(lst) ret=[0]*len(lst) for i in range(len(lst)): if i+k<len(lst) and i+k>=0: ret[i]=lst[i+k] if i+k>=len(lst): ret[i]=lst[i+k-len(lst)] if i+k<0: ret[i]=lst[i+k+len(lst)] return(ret) def nm(): n = int(input()) b = [int(x) for x in input().split()] m = int(input()) c = [int(x) for x in input().split()] return n,b,m,c def dvs(): n = int(input()) m = int(input()) return n, m def fact(a, b): c = [] ans = 0 f = int(math.sqrt(a)) for i in range(1, f + 1): if a % i == 0: c.append(i) l = len(c) for i in range(l): c.append(a // c[i]) for i in range(len(c)): if c[i] <= b: ans += 1 if a / f == f and b >= f: return ans - 1 return ans t = int(input()) for i in range(t): a ,b = list(map(int, input().split())) if a == b: print(0) else: d = abs(a - b) k1 = d//5 d -= k1 *5 k2 = d // 2 d -= k2 * 2 print(d + k1 + k2)
Bob watches TV every day. He always sets the volume of his TV to $b$. However, today he is angry to find out someone has changed the volume to $a$. Of course, Bob has a remote control that can change the volume. There are six buttons ($-5, -2, -1, +1, +2, +5$) on the control, which in one press can either increase or decrease the current volume by $1$, $2$, or $5$. The volume can be arbitrarily large, but can never be negative. In other words, Bob cannot press the button if it causes the volume to be lower than $0$. As Bob is so angry, he wants to change the volume to $b$ using as few button presses as possible. However, he forgets how to do such simple calculations, so he asks you for help. Write a program that given $a$ and $b$, finds the minimum number of presses to change the TV volume from $a$ to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $T$ ($1 \le T \le 1\,000$). Then the descriptions of the test cases follow. Each test case consists of one line containing two integers $a$ and $b$ ($0 \le a, b \le 10^{9}$)ย โ€” the current volume and Bob's desired volume, respectively. -----Output----- For each test case, output a single integerย โ€” the minimum number of presses to change the TV volume from $a$ to $b$. If Bob does not need to change the volume (i.e. $a=b$), then print $0$. -----Example----- Input 3 4 0 5 14 3 9 Output 2 3 2 -----Note----- In the first example, Bob can press the $-2$ button twice to reach $0$. Note that Bob can not press $-5$ when the volume is $4$ since it will make the volume negative. In the second example, one of the optimal ways for Bob is to press the $+5$ twice, then press $-1$ once. In the last example, Bob can press the $+5$ once, then press $+1$.
import sys import math # sys.stdin = open("in.txt") for _ in range(int(input())): a, b = map(int, input().split()) x = abs(a - b) res = x // 5 x %= 5 res += x // 2 x %= 2 print(res + x)
Bob watches TV every day. He always sets the volume of his TV to $b$. However, today he is angry to find out someone has changed the volume to $a$. Of course, Bob has a remote control that can change the volume. There are six buttons ($-5, -2, -1, +1, +2, +5$) on the control, which in one press can either increase or decrease the current volume by $1$, $2$, or $5$. The volume can be arbitrarily large, but can never be negative. In other words, Bob cannot press the button if it causes the volume to be lower than $0$. As Bob is so angry, he wants to change the volume to $b$ using as few button presses as possible. However, he forgets how to do such simple calculations, so he asks you for help. Write a program that given $a$ and $b$, finds the minimum number of presses to change the TV volume from $a$ to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $T$ ($1 \le T \le 1\,000$). Then the descriptions of the test cases follow. Each test case consists of one line containing two integers $a$ and $b$ ($0 \le a, b \le 10^{9}$)ย โ€” the current volume and Bob's desired volume, respectively. -----Output----- For each test case, output a single integerย โ€” the minimum number of presses to change the TV volume from $a$ to $b$. If Bob does not need to change the volume (i.e. $a=b$), then print $0$. -----Example----- Input 3 4 0 5 14 3 9 Output 2 3 2 -----Note----- In the first example, Bob can press the $-2$ button twice to reach $0$. Note that Bob can not press $-5$ when the volume is $4$ since it will make the volume negative. In the second example, one of the optimal ways for Bob is to press the $+5$ twice, then press $-1$ once. In the last example, Bob can press the $+5$ once, then press $+1$.
def main(): t = int(input()) for i in range(t): a,b = list(map(int,input().split())) moves = 0 diff = abs(a-b) x = diff//5 moves += x diff -= (5*x) x = diff//2 moves += x diff -= (2*x) x = diff moves += x print(moves) main()
Bob watches TV every day. He always sets the volume of his TV to $b$. However, today he is angry to find out someone has changed the volume to $a$. Of course, Bob has a remote control that can change the volume. There are six buttons ($-5, -2, -1, +1, +2, +5$) on the control, which in one press can either increase or decrease the current volume by $1$, $2$, or $5$. The volume can be arbitrarily large, but can never be negative. In other words, Bob cannot press the button if it causes the volume to be lower than $0$. As Bob is so angry, he wants to change the volume to $b$ using as few button presses as possible. However, he forgets how to do such simple calculations, so he asks you for help. Write a program that given $a$ and $b$, finds the minimum number of presses to change the TV volume from $a$ to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $T$ ($1 \le T \le 1\,000$). Then the descriptions of the test cases follow. Each test case consists of one line containing two integers $a$ and $b$ ($0 \le a, b \le 10^{9}$)ย โ€” the current volume and Bob's desired volume, respectively. -----Output----- For each test case, output a single integerย โ€” the minimum number of presses to change the TV volume from $a$ to $b$. If Bob does not need to change the volume (i.e. $a=b$), then print $0$. -----Example----- Input 3 4 0 5 14 3 9 Output 2 3 2 -----Note----- In the first example, Bob can press the $-2$ button twice to reach $0$. Note that Bob can not press $-5$ when the volume is $4$ since it will make the volume negative. In the second example, one of the optimal ways for Bob is to press the $+5$ twice, then press $-1$ once. In the last example, Bob can press the $+5$ once, then press $+1$.
t = int(input()) for _ in range (t): a, b = list(map(int, input().split())) d = abs(b - a) ans = 0 ans += d // 5 d = d % 5 ans += d // 2 d %= 2 ans += d // 1 d %= 1 print(ans)
Bob watches TV every day. He always sets the volume of his TV to $b$. However, today he is angry to find out someone has changed the volume to $a$. Of course, Bob has a remote control that can change the volume. There are six buttons ($-5, -2, -1, +1, +2, +5$) on the control, which in one press can either increase or decrease the current volume by $1$, $2$, or $5$. The volume can be arbitrarily large, but can never be negative. In other words, Bob cannot press the button if it causes the volume to be lower than $0$. As Bob is so angry, he wants to change the volume to $b$ using as few button presses as possible. However, he forgets how to do such simple calculations, so he asks you for help. Write a program that given $a$ and $b$, finds the minimum number of presses to change the TV volume from $a$ to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $T$ ($1 \le T \le 1\,000$). Then the descriptions of the test cases follow. Each test case consists of one line containing two integers $a$ and $b$ ($0 \le a, b \le 10^{9}$)ย โ€” the current volume and Bob's desired volume, respectively. -----Output----- For each test case, output a single integerย โ€” the minimum number of presses to change the TV volume from $a$ to $b$. If Bob does not need to change the volume (i.e. $a=b$), then print $0$. -----Example----- Input 3 4 0 5 14 3 9 Output 2 3 2 -----Note----- In the first example, Bob can press the $-2$ button twice to reach $0$. Note that Bob can not press $-5$ when the volume is $4$ since it will make the volume negative. In the second example, one of the optimal ways for Bob is to press the $+5$ twice, then press $-1$ once. In the last example, Bob can press the $+5$ once, then press $+1$.
''' ุจูุณู’ู…ู ุงู„ู„ูŽู‘ู‡ู ุงู„ุฑูŽู‘ุญู’ู…ูŽูฐู†ู ุงู„ุฑูŽู‘ุญููŠู…ู ''' #codeforces gi = lambda : list(map(int,input().split())) for j in range(gi()[0]): a, b = gi() d = abs(a - b) print(d // 5 + (d % 5) // 2 + ((d % 5) % 2))
Bob watches TV every day. He always sets the volume of his TV to $b$. However, today he is angry to find out someone has changed the volume to $a$. Of course, Bob has a remote control that can change the volume. There are six buttons ($-5, -2, -1, +1, +2, +5$) on the control, which in one press can either increase or decrease the current volume by $1$, $2$, or $5$. The volume can be arbitrarily large, but can never be negative. In other words, Bob cannot press the button if it causes the volume to be lower than $0$. As Bob is so angry, he wants to change the volume to $b$ using as few button presses as possible. However, he forgets how to do such simple calculations, so he asks you for help. Write a program that given $a$ and $b$, finds the minimum number of presses to change the TV volume from $a$ to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $T$ ($1 \le T \le 1\,000$). Then the descriptions of the test cases follow. Each test case consists of one line containing two integers $a$ and $b$ ($0 \le a, b \le 10^{9}$)ย โ€” the current volume and Bob's desired volume, respectively. -----Output----- For each test case, output a single integerย โ€” the minimum number of presses to change the TV volume from $a$ to $b$. If Bob does not need to change the volume (i.e. $a=b$), then print $0$. -----Example----- Input 3 4 0 5 14 3 9 Output 2 3 2 -----Note----- In the first example, Bob can press the $-2$ button twice to reach $0$. Note that Bob can not press $-5$ when the volume is $4$ since it will make the volume negative. In the second example, one of the optimal ways for Bob is to press the $+5$ twice, then press $-1$ once. In the last example, Bob can press the $+5$ once, then press $+1$.
t = int(input()) for gg in range(t): a, b = list(map(int, input().split())) d = abs(a-b) if d == 0: print(0) else: ans = 0 ans += d//5 d%=5 ans+=d//2 d%=2 ans+=d print(ans)
Bob watches TV every day. He always sets the volume of his TV to $b$. However, today he is angry to find out someone has changed the volume to $a$. Of course, Bob has a remote control that can change the volume. There are six buttons ($-5, -2, -1, +1, +2, +5$) on the control, which in one press can either increase or decrease the current volume by $1$, $2$, or $5$. The volume can be arbitrarily large, but can never be negative. In other words, Bob cannot press the button if it causes the volume to be lower than $0$. As Bob is so angry, he wants to change the volume to $b$ using as few button presses as possible. However, he forgets how to do such simple calculations, so he asks you for help. Write a program that given $a$ and $b$, finds the minimum number of presses to change the TV volume from $a$ to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $T$ ($1 \le T \le 1\,000$). Then the descriptions of the test cases follow. Each test case consists of one line containing two integers $a$ and $b$ ($0 \le a, b \le 10^{9}$)ย โ€” the current volume and Bob's desired volume, respectively. -----Output----- For each test case, output a single integerย โ€” the minimum number of presses to change the TV volume from $a$ to $b$. If Bob does not need to change the volume (i.e. $a=b$), then print $0$. -----Example----- Input 3 4 0 5 14 3 9 Output 2 3 2 -----Note----- In the first example, Bob can press the $-2$ button twice to reach $0$. Note that Bob can not press $-5$ when the volume is $4$ since it will make the volume negative. In the second example, one of the optimal ways for Bob is to press the $+5$ twice, then press $-1$ once. In the last example, Bob can press the $+5$ once, then press $+1$.
from bisect import * from collections import * from itertools import * import functools import sys import math from decimal import * from copy import * from heapq import * from fractions import * getcontext().prec = 30 MAX = sys.maxsize MAXN = 1000010 MOD = 10**9+7 spf = [i for i in range(MAXN)] def sieve(): for i in range(2,MAXN,2): spf[i] = 2 for i in range(3,int(MAXN**0.5)+1): if spf[i]==i: for j in range(i*i,MAXN,i): if spf[j]==j: spf[j]=i def fib(n,m): if n == 0: return [0, 1] else: a, b = fib(n // 2) c = ((a%m) * ((b%m) * 2 - (a%m)))%m d = ((a%m) * (a%m))%m + ((b)%m * (b)%m)%m if n % 2 == 0: return [c, d] else: return [d, c + d] def charIN(x= ' '): return(sys.stdin.readline().strip().split(x)) def arrIN(x = ' '): return list(map(int,sys.stdin.readline().strip().split(x))) def ncr(n,r): num=den=1 for i in range(r): num = (num*(n-i))%MOD den = (den*(i+1))%MOD return (num*(pow(den,MOD-2,MOD)))%MOD def flush(): return sys.stdout.flush() '''*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*''' for _ in range(int(input())): a,b = arrIN() d = abs(a-b) ans = 0 x,y = divmod(d,5) ans+=x z,w = divmod(y,2) ans+=z ans+=w print(ans)
Bob watches TV every day. He always sets the volume of his TV to $b$. However, today he is angry to find out someone has changed the volume to $a$. Of course, Bob has a remote control that can change the volume. There are six buttons ($-5, -2, -1, +1, +2, +5$) on the control, which in one press can either increase or decrease the current volume by $1$, $2$, or $5$. The volume can be arbitrarily large, but can never be negative. In other words, Bob cannot press the button if it causes the volume to be lower than $0$. As Bob is so angry, he wants to change the volume to $b$ using as few button presses as possible. However, he forgets how to do such simple calculations, so he asks you for help. Write a program that given $a$ and $b$, finds the minimum number of presses to change the TV volume from $a$ to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $T$ ($1 \le T \le 1\,000$). Then the descriptions of the test cases follow. Each test case consists of one line containing two integers $a$ and $b$ ($0 \le a, b \le 10^{9}$)ย โ€” the current volume and Bob's desired volume, respectively. -----Output----- For each test case, output a single integerย โ€” the minimum number of presses to change the TV volume from $a$ to $b$. If Bob does not need to change the volume (i.e. $a=b$), then print $0$. -----Example----- Input 3 4 0 5 14 3 9 Output 2 3 2 -----Note----- In the first example, Bob can press the $-2$ button twice to reach $0$. Note that Bob can not press $-5$ when the volume is $4$ since it will make the volume negative. In the second example, one of the optimal ways for Bob is to press the $+5$ twice, then press $-1$ once. In the last example, Bob can press the $+5$ once, then press $+1$.
t=int(input()) while(t): t-=1 a,b=map(int,input().split()) if a>b: a,b=b,a d=b-a ans=0 if(d>=5): ans+=d//5 d%=5 if(d>=2): ans+=d//2 d%=2 if(d>=1): ans+=d print(ans)
Bob watches TV every day. He always sets the volume of his TV to $b$. However, today he is angry to find out someone has changed the volume to $a$. Of course, Bob has a remote control that can change the volume. There are six buttons ($-5, -2, -1, +1, +2, +5$) on the control, which in one press can either increase or decrease the current volume by $1$, $2$, or $5$. The volume can be arbitrarily large, but can never be negative. In other words, Bob cannot press the button if it causes the volume to be lower than $0$. As Bob is so angry, he wants to change the volume to $b$ using as few button presses as possible. However, he forgets how to do such simple calculations, so he asks you for help. Write a program that given $a$ and $b$, finds the minimum number of presses to change the TV volume from $a$ to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $T$ ($1 \le T \le 1\,000$). Then the descriptions of the test cases follow. Each test case consists of one line containing two integers $a$ and $b$ ($0 \le a, b \le 10^{9}$)ย โ€” the current volume and Bob's desired volume, respectively. -----Output----- For each test case, output a single integerย โ€” the minimum number of presses to change the TV volume from $a$ to $b$. If Bob does not need to change the volume (i.e. $a=b$), then print $0$. -----Example----- Input 3 4 0 5 14 3 9 Output 2 3 2 -----Note----- In the first example, Bob can press the $-2$ button twice to reach $0$. Note that Bob can not press $-5$ when the volume is $4$ since it will make the volume negative. In the second example, one of the optimal ways for Bob is to press the $+5$ twice, then press $-1$ once. In the last example, Bob can press the $+5$ once, then press $+1$.
def ii(): return int(input()) def si(): return input() def mi(): return list(map(int,input().strip().split(" "))) def li(): return list(mi()) mod=1e9 t=ii() while(t): t-=1 a,b=mi() x=abs(b-a) c=x//5 x=x%5 c+=x//2 x%=2 c+=x print(c)
Bob watches TV every day. He always sets the volume of his TV to $b$. However, today he is angry to find out someone has changed the volume to $a$. Of course, Bob has a remote control that can change the volume. There are six buttons ($-5, -2, -1, +1, +2, +5$) on the control, which in one press can either increase or decrease the current volume by $1$, $2$, or $5$. The volume can be arbitrarily large, but can never be negative. In other words, Bob cannot press the button if it causes the volume to be lower than $0$. As Bob is so angry, he wants to change the volume to $b$ using as few button presses as possible. However, he forgets how to do such simple calculations, so he asks you for help. Write a program that given $a$ and $b$, finds the minimum number of presses to change the TV volume from $a$ to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $T$ ($1 \le T \le 1\,000$). Then the descriptions of the test cases follow. Each test case consists of one line containing two integers $a$ and $b$ ($0 \le a, b \le 10^{9}$)ย โ€” the current volume and Bob's desired volume, respectively. -----Output----- For each test case, output a single integerย โ€” the minimum number of presses to change the TV volume from $a$ to $b$. If Bob does not need to change the volume (i.e. $a=b$), then print $0$. -----Example----- Input 3 4 0 5 14 3 9 Output 2 3 2 -----Note----- In the first example, Bob can press the $-2$ button twice to reach $0$. Note that Bob can not press $-5$ when the volume is $4$ since it will make the volume negative. In the second example, one of the optimal ways for Bob is to press the $+5$ twice, then press $-1$ once. In the last example, Bob can press the $+5$ once, then press $+1$.
for i in ' '*int(input()): a,b=map(int,input().split()) k=abs(b-a) c=0 c+=k//5 k%=5 if k>3: c+=1 k=5-k c+=k//2 k%=2 c+=k print(c)
Bob watches TV every day. He always sets the volume of his TV to $b$. However, today he is angry to find out someone has changed the volume to $a$. Of course, Bob has a remote control that can change the volume. There are six buttons ($-5, -2, -1, +1, +2, +5$) on the control, which in one press can either increase or decrease the current volume by $1$, $2$, or $5$. The volume can be arbitrarily large, but can never be negative. In other words, Bob cannot press the button if it causes the volume to be lower than $0$. As Bob is so angry, he wants to change the volume to $b$ using as few button presses as possible. However, he forgets how to do such simple calculations, so he asks you for help. Write a program that given $a$ and $b$, finds the minimum number of presses to change the TV volume from $a$ to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $T$ ($1 \le T \le 1\,000$). Then the descriptions of the test cases follow. Each test case consists of one line containing two integers $a$ and $b$ ($0 \le a, b \le 10^{9}$)ย โ€” the current volume and Bob's desired volume, respectively. -----Output----- For each test case, output a single integerย โ€” the minimum number of presses to change the TV volume from $a$ to $b$. If Bob does not need to change the volume (i.e. $a=b$), then print $0$. -----Example----- Input 3 4 0 5 14 3 9 Output 2 3 2 -----Note----- In the first example, Bob can press the $-2$ button twice to reach $0$. Note that Bob can not press $-5$ when the volume is $4$ since it will make the volume negative. In the second example, one of the optimal ways for Bob is to press the $+5$ twice, then press $-1$ once. In the last example, Bob can press the $+5$ once, then press $+1$.
import sys import math import itertools import collections def sieve(n): if n < 2: return list() prime = [True for _ in range(n + 1)] p = 3 while p * p <= n: if prime[p]: for i in range(p * 2, n + 1, p): prime[i] = False p += 2 r = [2] for p in range(3, n + 1, 2): if prime[p]: r.append(p) return r def divs(n, start=1): r = [] for i in range(start, int(math.sqrt(n) + 1)): if (n % i == 0): if (n / i == i): r.append(i) else: r.extend([i, n // i]) return r def cdiv(n, k): return n // k + (n % k != 0) def ii(): return int(input()) def mi(): return list(map(int, input().split())) def li(): return list(map(int, input().split())) def lcm(a, b): return abs(a * b) // math.gcd(a, b) def wr(arr): return ''.join(map(str, arr)) def revn(n): return str(n)[::-1] def prime(n): if n == 2: return True if n % 2 == 0 or n <= 1: return False sqr = int(math.sqrt(n)) + 1 for d in range(3, sqr, 2): if n % d == 0: return False return True def convn(number, base): newnumber = 0 while number > 0: newnumber += number % base number //= base return newnumber t = ii() for _ in range(t): a, b = mi() d = abs(b - a) ans = 0 ans += d // 5 d %= 5 ans += d // 2 d %= 2 ans += d print(ans)
Bob watches TV every day. He always sets the volume of his TV to $b$. However, today he is angry to find out someone has changed the volume to $a$. Of course, Bob has a remote control that can change the volume. There are six buttons ($-5, -2, -1, +1, +2, +5$) on the control, which in one press can either increase or decrease the current volume by $1$, $2$, or $5$. The volume can be arbitrarily large, but can never be negative. In other words, Bob cannot press the button if it causes the volume to be lower than $0$. As Bob is so angry, he wants to change the volume to $b$ using as few button presses as possible. However, he forgets how to do such simple calculations, so he asks you for help. Write a program that given $a$ and $b$, finds the minimum number of presses to change the TV volume from $a$ to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $T$ ($1 \le T \le 1\,000$). Then the descriptions of the test cases follow. Each test case consists of one line containing two integers $a$ and $b$ ($0 \le a, b \le 10^{9}$)ย โ€” the current volume and Bob's desired volume, respectively. -----Output----- For each test case, output a single integerย โ€” the minimum number of presses to change the TV volume from $a$ to $b$. If Bob does not need to change the volume (i.e. $a=b$), then print $0$. -----Example----- Input 3 4 0 5 14 3 9 Output 2 3 2 -----Note----- In the first example, Bob can press the $-2$ button twice to reach $0$. Note that Bob can not press $-5$ when the volume is $4$ since it will make the volume negative. In the second example, one of the optimal ways for Bob is to press the $+5$ twice, then press $-1$ once. In the last example, Bob can press the $+5$ once, then press $+1$.
for i in range(int(input())): a,b=list(map(int,input().split())) a=-min(a,b)+max(a,b) ans=0 ans= a//5 a%=5 ans+=a//2 a%=2 ans+=a print(ans)
Bob watches TV every day. He always sets the volume of his TV to $b$. However, today he is angry to find out someone has changed the volume to $a$. Of course, Bob has a remote control that can change the volume. There are six buttons ($-5, -2, -1, +1, +2, +5$) on the control, which in one press can either increase or decrease the current volume by $1$, $2$, or $5$. The volume can be arbitrarily large, but can never be negative. In other words, Bob cannot press the button if it causes the volume to be lower than $0$. As Bob is so angry, he wants to change the volume to $b$ using as few button presses as possible. However, he forgets how to do such simple calculations, so he asks you for help. Write a program that given $a$ and $b$, finds the minimum number of presses to change the TV volume from $a$ to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $T$ ($1 \le T \le 1\,000$). Then the descriptions of the test cases follow. Each test case consists of one line containing two integers $a$ and $b$ ($0 \le a, b \le 10^{9}$)ย โ€” the current volume and Bob's desired volume, respectively. -----Output----- For each test case, output a single integerย โ€” the minimum number of presses to change the TV volume from $a$ to $b$. If Bob does not need to change the volume (i.e. $a=b$), then print $0$. -----Example----- Input 3 4 0 5 14 3 9 Output 2 3 2 -----Note----- In the first example, Bob can press the $-2$ button twice to reach $0$. Note that Bob can not press $-5$ when the volume is $4$ since it will make the volume negative. In the second example, one of the optimal ways for Bob is to press the $+5$ twice, then press $-1$ once. In the last example, Bob can press the $+5$ once, then press $+1$.
for _ in range(int(input())): a, b = list(map(int, input().split())) a = abs( a - b ) c = a // 5 a -= c * 5 print( c + a // 2 + a % 2 )
Bob watches TV every day. He always sets the volume of his TV to $b$. However, today he is angry to find out someone has changed the volume to $a$. Of course, Bob has a remote control that can change the volume. There are six buttons ($-5, -2, -1, +1, +2, +5$) on the control, which in one press can either increase or decrease the current volume by $1$, $2$, or $5$. The volume can be arbitrarily large, but can never be negative. In other words, Bob cannot press the button if it causes the volume to be lower than $0$. As Bob is so angry, he wants to change the volume to $b$ using as few button presses as possible. However, he forgets how to do such simple calculations, so he asks you for help. Write a program that given $a$ and $b$, finds the minimum number of presses to change the TV volume from $a$ to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $T$ ($1 \le T \le 1\,000$). Then the descriptions of the test cases follow. Each test case consists of one line containing two integers $a$ and $b$ ($0 \le a, b \le 10^{9}$)ย โ€” the current volume and Bob's desired volume, respectively. -----Output----- For each test case, output a single integerย โ€” the minimum number of presses to change the TV volume from $a$ to $b$. If Bob does not need to change the volume (i.e. $a=b$), then print $0$. -----Example----- Input 3 4 0 5 14 3 9 Output 2 3 2 -----Note----- In the first example, Bob can press the $-2$ button twice to reach $0$. Note that Bob can not press $-5$ when the volume is $4$ since it will make the volume negative. In the second example, one of the optimal ways for Bob is to press the $+5$ twice, then press $-1$ once. In the last example, Bob can press the $+5$ once, then press $+1$.
for _ in range(int(input())): a,b=list(map(int,input().split())) target=abs(a-b) res=0 res+=target//5;target=target%5 res+=target//2;target=target%2 res+=target//1;target=target%1 print(res)
Bob watches TV every day. He always sets the volume of his TV to $b$. However, today he is angry to find out someone has changed the volume to $a$. Of course, Bob has a remote control that can change the volume. There are six buttons ($-5, -2, -1, +1, +2, +5$) on the control, which in one press can either increase or decrease the current volume by $1$, $2$, or $5$. The volume can be arbitrarily large, but can never be negative. In other words, Bob cannot press the button if it causes the volume to be lower than $0$. As Bob is so angry, he wants to change the volume to $b$ using as few button presses as possible. However, he forgets how to do such simple calculations, so he asks you for help. Write a program that given $a$ and $b$, finds the minimum number of presses to change the TV volume from $a$ to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $T$ ($1 \le T \le 1\,000$). Then the descriptions of the test cases follow. Each test case consists of one line containing two integers $a$ and $b$ ($0 \le a, b \le 10^{9}$)ย โ€” the current volume and Bob's desired volume, respectively. -----Output----- For each test case, output a single integerย โ€” the minimum number of presses to change the TV volume from $a$ to $b$. If Bob does not need to change the volume (i.e. $a=b$), then print $0$. -----Example----- Input 3 4 0 5 14 3 9 Output 2 3 2 -----Note----- In the first example, Bob can press the $-2$ button twice to reach $0$. Note that Bob can not press $-5$ when the volume is $4$ since it will make the volume negative. In the second example, one of the optimal ways for Bob is to press the $+5$ twice, then press $-1$ once. In the last example, Bob can press the $+5$ once, then press $+1$.
#JMD #Nagendra Jha-4096 import sys import math #import fractions #import numpy ###File Operations### fileoperation=0 if(fileoperation): orig_stdout = sys.stdout orig_stdin = sys.stdin inputfile = open('W:/Competitive Programming/input.txt', 'r') outputfile = open('W:/Competitive Programming/output.txt', 'w') sys.stdin = inputfile sys.stdout = outputfile ###Defines...### mod=1000000007 ###FUF's...### def nospace(l): ans=''.join(str(i) for i in l) return ans ##### Main #### t=int(input()) for tt in range(t): #n=int(input()) a,b= map(int, sys.stdin.readline().split(' ')) ans=0 if(a>b): temp=b b=a a=temp diff=b-a ans+=(diff//5) diff%=5 ans+=(diff//2) diff%=2 ans+=diff print(ans) #a=list(map(int,sys.stdin.readline().split(' '))) #####File Operations##### if(fileoperation): sys.stdout = orig_stdout sys.stdin = orig_stdin inputfile.close() outputfile.close()
Bob watches TV every day. He always sets the volume of his TV to $b$. However, today he is angry to find out someone has changed the volume to $a$. Of course, Bob has a remote control that can change the volume. There are six buttons ($-5, -2, -1, +1, +2, +5$) on the control, which in one press can either increase or decrease the current volume by $1$, $2$, or $5$. The volume can be arbitrarily large, but can never be negative. In other words, Bob cannot press the button if it causes the volume to be lower than $0$. As Bob is so angry, he wants to change the volume to $b$ using as few button presses as possible. However, he forgets how to do such simple calculations, so he asks you for help. Write a program that given $a$ and $b$, finds the minimum number of presses to change the TV volume from $a$ to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $T$ ($1 \le T \le 1\,000$). Then the descriptions of the test cases follow. Each test case consists of one line containing two integers $a$ and $b$ ($0 \le a, b \le 10^{9}$)ย โ€” the current volume and Bob's desired volume, respectively. -----Output----- For each test case, output a single integerย โ€” the minimum number of presses to change the TV volume from $a$ to $b$. If Bob does not need to change the volume (i.e. $a=b$), then print $0$. -----Example----- Input 3 4 0 5 14 3 9 Output 2 3 2 -----Note----- In the first example, Bob can press the $-2$ button twice to reach $0$. Note that Bob can not press $-5$ when the volume is $4$ since it will make the volume negative. In the second example, one of the optimal ways for Bob is to press the $+5$ twice, then press $-1$ once. In the last example, Bob can press the $+5$ once, then press $+1$.
for _ in range(int(input())): a,b=list(map(int,input().split())) x=min(a,b) y=max(a,b) c=0 if x==y: print(0) else: c+=(y-x)//5 rem=(y-x)%5 c+=rem//2 rem=rem%2 c+=rem print(c)
Bob watches TV every day. He always sets the volume of his TV to $b$. However, today he is angry to find out someone has changed the volume to $a$. Of course, Bob has a remote control that can change the volume. There are six buttons ($-5, -2, -1, +1, +2, +5$) on the control, which in one press can either increase or decrease the current volume by $1$, $2$, or $5$. The volume can be arbitrarily large, but can never be negative. In other words, Bob cannot press the button if it causes the volume to be lower than $0$. As Bob is so angry, he wants to change the volume to $b$ using as few button presses as possible. However, he forgets how to do such simple calculations, so he asks you for help. Write a program that given $a$ and $b$, finds the minimum number of presses to change the TV volume from $a$ to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $T$ ($1 \le T \le 1\,000$). Then the descriptions of the test cases follow. Each test case consists of one line containing two integers $a$ and $b$ ($0 \le a, b \le 10^{9}$)ย โ€” the current volume and Bob's desired volume, respectively. -----Output----- For each test case, output a single integerย โ€” the minimum number of presses to change the TV volume from $a$ to $b$. If Bob does not need to change the volume (i.e. $a=b$), then print $0$. -----Example----- Input 3 4 0 5 14 3 9 Output 2 3 2 -----Note----- In the first example, Bob can press the $-2$ button twice to reach $0$. Note that Bob can not press $-5$ when the volume is $4$ since it will make the volume negative. In the second example, one of the optimal ways for Bob is to press the $+5$ twice, then press $-1$ once. In the last example, Bob can press the $+5$ once, then press $+1$.
n = int(input()) for i in range(n): x, y = list(map(int, input().split())) if x > y: x, y = y, x a = (y - x) // 5 x += a * 5 b = (y - x) // 2 x += b * 2 c = (y - x) print(a + b + c)
Bob watches TV every day. He always sets the volume of his TV to $b$. However, today he is angry to find out someone has changed the volume to $a$. Of course, Bob has a remote control that can change the volume. There are six buttons ($-5, -2, -1, +1, +2, +5$) on the control, which in one press can either increase or decrease the current volume by $1$, $2$, or $5$. The volume can be arbitrarily large, but can never be negative. In other words, Bob cannot press the button if it causes the volume to be lower than $0$. As Bob is so angry, he wants to change the volume to $b$ using as few button presses as possible. However, he forgets how to do such simple calculations, so he asks you for help. Write a program that given $a$ and $b$, finds the minimum number of presses to change the TV volume from $a$ to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $T$ ($1 \le T \le 1\,000$). Then the descriptions of the test cases follow. Each test case consists of one line containing two integers $a$ and $b$ ($0 \le a, b \le 10^{9}$)ย โ€” the current volume and Bob's desired volume, respectively. -----Output----- For each test case, output a single integerย โ€” the minimum number of presses to change the TV volume from $a$ to $b$. If Bob does not need to change the volume (i.e. $a=b$), then print $0$. -----Example----- Input 3 4 0 5 14 3 9 Output 2 3 2 -----Note----- In the first example, Bob can press the $-2$ button twice to reach $0$. Note that Bob can not press $-5$ when the volume is $4$ since it will make the volume negative. In the second example, one of the optimal ways for Bob is to press the $+5$ twice, then press $-1$ once. In the last example, Bob can press the $+5$ once, then press $+1$.
t = int(input()) for _ in range(t): a, b = map(int, input().split()) diff = abs(a-b) fives = diff//5 diff %= 5 twos = diff//2 diff %= 2 ones = diff print(fives+twos+ones)
Bob watches TV every day. He always sets the volume of his TV to $b$. However, today he is angry to find out someone has changed the volume to $a$. Of course, Bob has a remote control that can change the volume. There are six buttons ($-5, -2, -1, +1, +2, +5$) on the control, which in one press can either increase or decrease the current volume by $1$, $2$, or $5$. The volume can be arbitrarily large, but can never be negative. In other words, Bob cannot press the button if it causes the volume to be lower than $0$. As Bob is so angry, he wants to change the volume to $b$ using as few button presses as possible. However, he forgets how to do such simple calculations, so he asks you for help. Write a program that given $a$ and $b$, finds the minimum number of presses to change the TV volume from $a$ to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $T$ ($1 \le T \le 1\,000$). Then the descriptions of the test cases follow. Each test case consists of one line containing two integers $a$ and $b$ ($0 \le a, b \le 10^{9}$)ย โ€” the current volume and Bob's desired volume, respectively. -----Output----- For each test case, output a single integerย โ€” the minimum number of presses to change the TV volume from $a$ to $b$. If Bob does not need to change the volume (i.e. $a=b$), then print $0$. -----Example----- Input 3 4 0 5 14 3 9 Output 2 3 2 -----Note----- In the first example, Bob can press the $-2$ button twice to reach $0$. Note that Bob can not press $-5$ when the volume is $4$ since it will make the volume negative. In the second example, one of the optimal ways for Bob is to press the $+5$ twice, then press $-1$ once. In the last example, Bob can press the $+5$ once, then press $+1$.
n=int(input()) for i in range(n): a,b=[int(x) for x in input().split(' ')] c=abs(a-b) d=0 d+=int(c/5) c=c%5 d+=int(c/2) c=c%2 d+=c print(d)
Bob watches TV every day. He always sets the volume of his TV to $b$. However, today he is angry to find out someone has changed the volume to $a$. Of course, Bob has a remote control that can change the volume. There are six buttons ($-5, -2, -1, +1, +2, +5$) on the control, which in one press can either increase or decrease the current volume by $1$, $2$, or $5$. The volume can be arbitrarily large, but can never be negative. In other words, Bob cannot press the button if it causes the volume to be lower than $0$. As Bob is so angry, he wants to change the volume to $b$ using as few button presses as possible. However, he forgets how to do such simple calculations, so he asks you for help. Write a program that given $a$ and $b$, finds the minimum number of presses to change the TV volume from $a$ to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $T$ ($1 \le T \le 1\,000$). Then the descriptions of the test cases follow. Each test case consists of one line containing two integers $a$ and $b$ ($0 \le a, b \le 10^{9}$)ย โ€” the current volume and Bob's desired volume, respectively. -----Output----- For each test case, output a single integerย โ€” the minimum number of presses to change the TV volume from $a$ to $b$. If Bob does not need to change the volume (i.e. $a=b$), then print $0$. -----Example----- Input 3 4 0 5 14 3 9 Output 2 3 2 -----Note----- In the first example, Bob can press the $-2$ button twice to reach $0$. Note that Bob can not press $-5$ when the volume is $4$ since it will make the volume negative. In the second example, one of the optimal ways for Bob is to press the $+5$ twice, then press $-1$ once. In the last example, Bob can press the $+5$ once, then press $+1$.
import io, sys, atexit, os import math as ma from decimal import Decimal as dec from itertools import permutations from itertools import combinations def li(): return list(map(int, sys.stdin.readline().split())) def num(): return map(int, sys.stdin.readline().split()) def nu(): return int(input()) def find_gcd(x, y): while (y): x, y = y, x % y return x def lcm(x, y): gg = find_gcd(x, y) return (x * y // gg) mm = 1000000007 def solve(): t = nu() for tt in range(t): a,b=num() if(a<=b): dd=b-a pq=dd//5 yp=dd%5 yo=yp//2 yu=yp%2 print(pq+yu+yo) else: a,b=b,a dd = b - a pq = dd // 5 yp = dd % 5 yo = yp // 2 yu = yp % 2 print(pq + yu + yo) def __starting_point(): solve() __starting_point()
Bob watches TV every day. He always sets the volume of his TV to $b$. However, today he is angry to find out someone has changed the volume to $a$. Of course, Bob has a remote control that can change the volume. There are six buttons ($-5, -2, -1, +1, +2, +5$) on the control, which in one press can either increase or decrease the current volume by $1$, $2$, or $5$. The volume can be arbitrarily large, but can never be negative. In other words, Bob cannot press the button if it causes the volume to be lower than $0$. As Bob is so angry, he wants to change the volume to $b$ using as few button presses as possible. However, he forgets how to do such simple calculations, so he asks you for help. Write a program that given $a$ and $b$, finds the minimum number of presses to change the TV volume from $a$ to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $T$ ($1 \le T \le 1\,000$). Then the descriptions of the test cases follow. Each test case consists of one line containing two integers $a$ and $b$ ($0 \le a, b \le 10^{9}$)ย โ€” the current volume and Bob's desired volume, respectively. -----Output----- For each test case, output a single integerย โ€” the minimum number of presses to change the TV volume from $a$ to $b$. If Bob does not need to change the volume (i.e. $a=b$), then print $0$. -----Example----- Input 3 4 0 5 14 3 9 Output 2 3 2 -----Note----- In the first example, Bob can press the $-2$ button twice to reach $0$. Note that Bob can not press $-5$ when the volume is $4$ since it will make the volume negative. In the second example, one of the optimal ways for Bob is to press the $+5$ twice, then press $-1$ once. In the last example, Bob can press the $+5$ once, then press $+1$.
n = int(input()) for i in range(n): a, b = map(int, input().split()) s = abs(a - b) x = s // 5 s %= 5 x += s // 2 s %= 2 x += s print(x)
Bob watches TV every day. He always sets the volume of his TV to $b$. However, today he is angry to find out someone has changed the volume to $a$. Of course, Bob has a remote control that can change the volume. There are six buttons ($-5, -2, -1, +1, +2, +5$) on the control, which in one press can either increase or decrease the current volume by $1$, $2$, or $5$. The volume can be arbitrarily large, but can never be negative. In other words, Bob cannot press the button if it causes the volume to be lower than $0$. As Bob is so angry, he wants to change the volume to $b$ using as few button presses as possible. However, he forgets how to do such simple calculations, so he asks you for help. Write a program that given $a$ and $b$, finds the minimum number of presses to change the TV volume from $a$ to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $T$ ($1 \le T \le 1\,000$). Then the descriptions of the test cases follow. Each test case consists of one line containing two integers $a$ and $b$ ($0 \le a, b \le 10^{9}$)ย โ€” the current volume and Bob's desired volume, respectively. -----Output----- For each test case, output a single integerย โ€” the minimum number of presses to change the TV volume from $a$ to $b$. If Bob does not need to change the volume (i.e. $a=b$), then print $0$. -----Example----- Input 3 4 0 5 14 3 9 Output 2 3 2 -----Note----- In the first example, Bob can press the $-2$ button twice to reach $0$. Note that Bob can not press $-5$ when the volume is $4$ since it will make the volume negative. In the second example, one of the optimal ways for Bob is to press the $+5$ twice, then press $-1$ once. In the last example, Bob can press the $+5$ once, then press $+1$.
T = int(input()) for _ in range(T): a, b = map(int, input().split()) d = abs(a - b) ans = (d // 5) d = d % 5 if d == 1 or d == 2: ans += 1 if d == 3 or d == 4: ans += 2 print(ans)
Bob watches TV every day. He always sets the volume of his TV to $b$. However, today he is angry to find out someone has changed the volume to $a$. Of course, Bob has a remote control that can change the volume. There are six buttons ($-5, -2, -1, +1, +2, +5$) on the control, which in one press can either increase or decrease the current volume by $1$, $2$, or $5$. The volume can be arbitrarily large, but can never be negative. In other words, Bob cannot press the button if it causes the volume to be lower than $0$. As Bob is so angry, he wants to change the volume to $b$ using as few button presses as possible. However, he forgets how to do such simple calculations, so he asks you for help. Write a program that given $a$ and $b$, finds the minimum number of presses to change the TV volume from $a$ to $b$. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $T$ ($1 \le T \le 1\,000$). Then the descriptions of the test cases follow. Each test case consists of one line containing two integers $a$ and $b$ ($0 \le a, b \le 10^{9}$)ย โ€” the current volume and Bob's desired volume, respectively. -----Output----- For each test case, output a single integerย โ€” the minimum number of presses to change the TV volume from $a$ to $b$. If Bob does not need to change the volume (i.e. $a=b$), then print $0$. -----Example----- Input 3 4 0 5 14 3 9 Output 2 3 2 -----Note----- In the first example, Bob can press the $-2$ button twice to reach $0$. Note that Bob can not press $-5$ when the volume is $4$ since it will make the volume negative. In the second example, one of the optimal ways for Bob is to press the $+5$ twice, then press $-1$ once. In the last example, Bob can press the $+5$ once, then press $+1$.
for _ in range(int(input())): a,b=map(int,input().split()) d=abs(a-b) ans=0 sm=0 ans+=(d//5) d%=5 ans+=(d//2) d%=2 ans+=d print(ans)
You play a computer game. In this game, you lead a party of $m$ heroes, and you have to clear a dungeon with $n$ monsters. Each monster is characterized by its power $a_i$. Each hero is characterized by his power $p_i$ and endurance $s_i$. The heroes clear the dungeon day by day. In the beginning of each day, you choose a hero (exactly one) who is going to enter the dungeon this day. When the hero enters the dungeon, he is challenged by the first monster which was not defeated during the previous days (so, if the heroes have already defeated $k$ monsters, the hero fights with the monster $k + 1$). When the hero fights the monster, there are two possible outcomes: if the monster's power is strictly greater than the hero's power, the hero retreats from the dungeon. The current day ends; otherwise, the monster is defeated. After defeating a monster, the hero either continues fighting with the next monster or leaves the dungeon. He leaves the dungeon either if he has already defeated the number of monsters equal to his endurance during this day (so, the $i$-th hero cannot defeat more than $s_i$ monsters during each day), or if all monsters are defeated โ€” otherwise, he fights with the next monster. When the hero leaves the dungeon, the current day ends. Your goal is to defeat the last monster. What is the minimum number of days that you need to achieve your goal? Each day you have to use exactly one hero; it is possible that some heroes don't fight the monsters at all. Each hero can be used arbitrary number of times. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^5$) โ€” the number of test cases. Then the test cases follow. The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) โ€” the number of monsters in the dungeon. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the power of the $i$-th monster. The third line contains one integer $m$ ($1 \le m \le 2 \cdot 10^5$) โ€” the number of heroes in your party. Then $m$ lines follow, each describing a hero. Each line contains two integers $p_i$ and $s_i$ ($1 \le p_i \le 10^9$, $1 \le s_i \le n$) โ€” the power and the endurance of the $i$-th hero. It is guaranteed that the sum of $n + m$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case print one integer โ€” the minimum number of days you have to spend to defeat all of the monsters (or $-1$ if it is impossible). -----Example----- Input 2 6 2 3 11 14 1 8 2 3 2 100 1 5 3 5 100 2 3 2 30 5 90 1 Output 5 -1
import sys input = sys.stdin.readline import bisect t=int(input()) for testcases in range(t): n=int(input()) A=list(map(int,input().split())) m=int(input()) PS=[tuple(map(int,input().split())) for i in range(m)] PS.sort() K=[PS[-1]] for a,b in PS[::-1][1:]: if b<=K[-1][1]: continue else: K.append((a,b)) K.reverse() ANS=1 count=0 countmax=n+1 LEN=len(K) for a in A: x=bisect.bisect_left(K,(a,0)) if x==LEN: print(-1) break elif K[x][1]>=count+1 and countmax>=count+1: count+=1 countmax=min(countmax,K[x][1]) else: ANS+=1 count=1 countmax=K[x][1] #print(a,count,countmax,ANS) else: print(ANS)
You play a computer game. In this game, you lead a party of $m$ heroes, and you have to clear a dungeon with $n$ monsters. Each monster is characterized by its power $a_i$. Each hero is characterized by his power $p_i$ and endurance $s_i$. The heroes clear the dungeon day by day. In the beginning of each day, you choose a hero (exactly one) who is going to enter the dungeon this day. When the hero enters the dungeon, he is challenged by the first monster which was not defeated during the previous days (so, if the heroes have already defeated $k$ monsters, the hero fights with the monster $k + 1$). When the hero fights the monster, there are two possible outcomes: if the monster's power is strictly greater than the hero's power, the hero retreats from the dungeon. The current day ends; otherwise, the monster is defeated. After defeating a monster, the hero either continues fighting with the next monster or leaves the dungeon. He leaves the dungeon either if he has already defeated the number of monsters equal to his endurance during this day (so, the $i$-th hero cannot defeat more than $s_i$ monsters during each day), or if all monsters are defeated โ€” otherwise, he fights with the next monster. When the hero leaves the dungeon, the current day ends. Your goal is to defeat the last monster. What is the minimum number of days that you need to achieve your goal? Each day you have to use exactly one hero; it is possible that some heroes don't fight the monsters at all. Each hero can be used arbitrary number of times. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^5$) โ€” the number of test cases. Then the test cases follow. The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) โ€” the number of monsters in the dungeon. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the power of the $i$-th monster. The third line contains one integer $m$ ($1 \le m \le 2 \cdot 10^5$) โ€” the number of heroes in your party. Then $m$ lines follow, each describing a hero. Each line contains two integers $p_i$ and $s_i$ ($1 \le p_i \le 10^9$, $1 \le s_i \le n$) โ€” the power and the endurance of the $i$-th hero. It is guaranteed that the sum of $n + m$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case print one integer โ€” the minimum number of days you have to spend to defeat all of the monsters (or $-1$ if it is impossible). -----Example----- Input 2 6 2 3 11 14 1 8 2 3 2 100 1 5 3 5 100 2 3 2 30 5 90 1 Output 5 -1
import sys input = sys.stdin.readline T = int(input()) Ans = [] for _ in range(T): N = int(input()) # ใƒขใƒณใ‚นใ‚ฟใƒผใฎๆ•ฐ A = list(map(int, input().split())) # ใƒขใƒณใ‚นใ‚ฟใƒผใฎใƒ‘ใƒฏใƒผ M = int(input()) # ใƒ’ใƒผใƒญใƒผใฎๆ•ฐ PS = [list(map(int, input().split())) for _ in range(M)] # ใƒ‘ใƒฏใƒผใจ่€ไน… # ใƒขใƒณใ‚นใ‚ฟใƒผใฎใƒ‘ใƒฏใƒผใŒใƒ’ใƒผใƒญใƒผใฎใƒ‘ใƒฏใƒผใ‚ˆใ‚Šๅคงใใ„ใจใƒ’ใƒผใƒญใƒผใฎ่ฒ ใ‘ # S ใฏ 1 ๆ—ฅใซๅ€’ใ›ใ‚‹ใƒขใƒณใ‚นใ‚ฟใƒผใฎๆ•ฐใฎไธŠ้™ # L[n] := n ไฝ“ๅ€’ใ›ใ‚‹ใƒ’ใƒผใƒญใƒผใฎๆœ€ๅคงใƒ‘ใƒฏใƒผ L = [0] * (N+1) for p, s in PS: L[s] = max(L[s], p) for i in range(N-1, -1, -1): L[i] = max(L[i], L[i+1]) ans = 1 cnt = 1 ma = 0 if L[1] < max(A): Ans.append(-1) continue for a in A: ma = max(ma, a) if L[cnt] < ma: cnt = 1 ans += 1 ma = a cnt += 1 Ans.append(ans) print("\n".join(map(str, Ans)))
You play a computer game. In this game, you lead a party of $m$ heroes, and you have to clear a dungeon with $n$ monsters. Each monster is characterized by its power $a_i$. Each hero is characterized by his power $p_i$ and endurance $s_i$. The heroes clear the dungeon day by day. In the beginning of each day, you choose a hero (exactly one) who is going to enter the dungeon this day. When the hero enters the dungeon, he is challenged by the first monster which was not defeated during the previous days (so, if the heroes have already defeated $k$ monsters, the hero fights with the monster $k + 1$). When the hero fights the monster, there are two possible outcomes: if the monster's power is strictly greater than the hero's power, the hero retreats from the dungeon. The current day ends; otherwise, the monster is defeated. After defeating a monster, the hero either continues fighting with the next monster or leaves the dungeon. He leaves the dungeon either if he has already defeated the number of monsters equal to his endurance during this day (so, the $i$-th hero cannot defeat more than $s_i$ monsters during each day), or if all monsters are defeated โ€” otherwise, he fights with the next monster. When the hero leaves the dungeon, the current day ends. Your goal is to defeat the last monster. What is the minimum number of days that you need to achieve your goal? Each day you have to use exactly one hero; it is possible that some heroes don't fight the monsters at all. Each hero can be used arbitrary number of times. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^5$) โ€” the number of test cases. Then the test cases follow. The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) โ€” the number of monsters in the dungeon. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the power of the $i$-th monster. The third line contains one integer $m$ ($1 \le m \le 2 \cdot 10^5$) โ€” the number of heroes in your party. Then $m$ lines follow, each describing a hero. Each line contains two integers $p_i$ and $s_i$ ($1 \le p_i \le 10^9$, $1 \le s_i \le n$) โ€” the power and the endurance of the $i$-th hero. It is guaranteed that the sum of $n + m$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case print one integer โ€” the minimum number of days you have to spend to defeat all of the monsters (or $-1$ if it is impossible). -----Example----- Input 2 6 2 3 11 14 1 8 2 3 2 100 1 5 3 5 100 2 3 2 30 5 90 1 Output 5 -1
3 import os import sys def main(): T = read_int() for _ in range(T): N = read_int() A = read_ints() M = read_int() H = [tuple(read_ints()) for _ in range(M)] print(solve(N, A, M, H)) def solve(N, A, M, H): H.sort(key=lambda h: (h[1], -h[0])) spow = [0] * (N + 1) s0 = 0 for p, s in H: if s0 == s: continue spow[s] = p s0 = s maxp = 0 for d in range(N, -1, -1): maxp = max(maxp, spow[d]) spow[d] = maxp ans = 0 maxa = A[0] if A[0] > spow[1]: return -1 start = 0 for i, a in enumerate(A[1:]): if a > spow[1]: return -1 i += 1 days = i - start + 1 maxa = max(maxa, a) if spow[days] < maxa: ans += 1 maxa = a start = i return ans + 1 ############################################################################### # AUXILIARY FUNCTIONS DEBUG = 'DEBUG' in os.environ def inp(): return sys.stdin.readline().rstrip() def read_int(): return int(inp()) def read_ints(): return [int(e) for e in inp().split()] def dprint(*value, sep=' ', end='\n'): if DEBUG: print(*value, sep=sep, end=end) def __starting_point(): main() __starting_point()
You play a computer game. In this game, you lead a party of $m$ heroes, and you have to clear a dungeon with $n$ monsters. Each monster is characterized by its power $a_i$. Each hero is characterized by his power $p_i$ and endurance $s_i$. The heroes clear the dungeon day by day. In the beginning of each day, you choose a hero (exactly one) who is going to enter the dungeon this day. When the hero enters the dungeon, he is challenged by the first monster which was not defeated during the previous days (so, if the heroes have already defeated $k$ monsters, the hero fights with the monster $k + 1$). When the hero fights the monster, there are two possible outcomes: if the monster's power is strictly greater than the hero's power, the hero retreats from the dungeon. The current day ends; otherwise, the monster is defeated. After defeating a monster, the hero either continues fighting with the next monster or leaves the dungeon. He leaves the dungeon either if he has already defeated the number of monsters equal to his endurance during this day (so, the $i$-th hero cannot defeat more than $s_i$ monsters during each day), or if all monsters are defeated โ€” otherwise, he fights with the next monster. When the hero leaves the dungeon, the current day ends. Your goal is to defeat the last monster. What is the minimum number of days that you need to achieve your goal? Each day you have to use exactly one hero; it is possible that some heroes don't fight the monsters at all. Each hero can be used arbitrary number of times. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^5$) โ€” the number of test cases. Then the test cases follow. The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) โ€” the number of monsters in the dungeon. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the power of the $i$-th monster. The third line contains one integer $m$ ($1 \le m \le 2 \cdot 10^5$) โ€” the number of heroes in your party. Then $m$ lines follow, each describing a hero. Each line contains two integers $p_i$ and $s_i$ ($1 \le p_i \le 10^9$, $1 \le s_i \le n$) โ€” the power and the endurance of the $i$-th hero. It is guaranteed that the sum of $n + m$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case print one integer โ€” the minimum number of days you have to spend to defeat all of the monsters (or $-1$ if it is impossible). -----Example----- Input 2 6 2 3 11 14 1 8 2 3 2 100 1 5 3 5 100 2 3 2 30 5 90 1 Output 5 -1
from sys import stdin input = stdin.readline q = int(input()) for rew in range(q): n = int(input()) monster = list(map(int,input().split())) m = int(input()) rycerz = [list(map(int,input().split())) for i in range(m)] rycerz.sort() rycerz.reverse() p = [a[0] for a in rycerz] s = [a[1] for a in rycerz] maxendu = [-1] * m maxendu[0] = s[0] if max(p) < max(monster): print(-1) else: for i in range(1, m): maxendu[i] = max(maxendu[i-1], s[i]) days = 0 poz = 0 while True: if poz >= n: print(days) break best_potwor = -1 kroki = 0 while True: if poz + kroki >= n: break best_potwor = max(monster[poz + kroki], best_potwor) #tyle krokow robimy i taki best potwor l = 0 pr = m - 1 while abs(pr-l) > 0: sr = (l + pr + 1) // 2 if p[sr] >= best_potwor: l = sr else: pr = sr - 1 sr = (pr + l) // 2 if maxendu[sr] >= kroki + 1: kroki += 1 else: kroki -= 1 break days += 1 poz += kroki poz += 1
You play a computer game. In this game, you lead a party of $m$ heroes, and you have to clear a dungeon with $n$ monsters. Each monster is characterized by its power $a_i$. Each hero is characterized by his power $p_i$ and endurance $s_i$. The heroes clear the dungeon day by day. In the beginning of each day, you choose a hero (exactly one) who is going to enter the dungeon this day. When the hero enters the dungeon, he is challenged by the first monster which was not defeated during the previous days (so, if the heroes have already defeated $k$ monsters, the hero fights with the monster $k + 1$). When the hero fights the monster, there are two possible outcomes: if the monster's power is strictly greater than the hero's power, the hero retreats from the dungeon. The current day ends; otherwise, the monster is defeated. After defeating a monster, the hero either continues fighting with the next monster or leaves the dungeon. He leaves the dungeon either if he has already defeated the number of monsters equal to his endurance during this day (so, the $i$-th hero cannot defeat more than $s_i$ monsters during each day), or if all monsters are defeated โ€” otherwise, he fights with the next monster. When the hero leaves the dungeon, the current day ends. Your goal is to defeat the last monster. What is the minimum number of days that you need to achieve your goal? Each day you have to use exactly one hero; it is possible that some heroes don't fight the monsters at all. Each hero can be used arbitrary number of times. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^5$) โ€” the number of test cases. Then the test cases follow. The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) โ€” the number of monsters in the dungeon. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the power of the $i$-th monster. The third line contains one integer $m$ ($1 \le m \le 2 \cdot 10^5$) โ€” the number of heroes in your party. Then $m$ lines follow, each describing a hero. Each line contains two integers $p_i$ and $s_i$ ($1 \le p_i \le 10^9$, $1 \le s_i \le n$) โ€” the power and the endurance of the $i$-th hero. It is guaranteed that the sum of $n + m$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case print one integer โ€” the minimum number of days you have to spend to defeat all of the monsters (or $-1$ if it is impossible). -----Example----- Input 2 6 2 3 11 14 1 8 2 3 2 100 1 5 3 5 100 2 3 2 30 5 90 1 Output 5 -1
import sys import bisect sys.setrecursionlimit(10**8) input = sys.stdin.readline INF = 10**9 class RMQ: def __init__(self, a): self.n = len(a) self.size = 2**(self.n - 1).bit_length() self.data = [0] * (2*self.size-1) self.initialize(a) # Initialize data def initialize(self, a): for i in range(self.n): self.data[self.size + i - 1] = a[i] for i in range(self.size-2, -1, -1): self.data[i] = max(self.data[i*2 + 1], self.data[i*2 + 2]) # Update ak as x def update(self, k, x): k += self.size - 1 self.data[k] = x while k > 0: k = (k - 1) // 2 self.data[k] = max(self.data[2*k+1], self.data[2*k+2]) # max value in [l, r) def query(self, l, r): L = l + self.size; R = r + self.size s = 0 while L < R: if R & 1: R -= 1 s = max(s, self.data[R-1]) if L & 1: s = max(s, self.data[L-1]) L += 1 L >>= 1; R >>= 1 return s t = int(input()) for i in range(t): n = int(input()) a = [int(item) for item in input().split()] m = int(input()) brave = [] for j in range(m): pp, ss = [int(item) for item in input().split()] brave.append((pp, ss)) brave.sort(reverse=True) p = [] s = [] for pp, ss in brave: p.append(pp) s.append(ss) s_rmq = RMQ(s) a_rmq = RMQ(a) p.reverse() s.reverse() max_step = max(s) days = 0 curr = 0 while curr < n: # Search step size l = 0; r = min(n - curr, max_step) + 1 while r - l > 1: mid = (l + r) // 2 max_monster = a_rmq.query(curr, curr+mid) index = m - bisect.bisect_left(p, max_monster) walkable = s_rmq.query(0, index) if walkable >= mid: l = mid else: r = mid if l == 0: days = -1 break else: days += 1 curr += l print(days)
You play a computer game. In this game, you lead a party of $m$ heroes, and you have to clear a dungeon with $n$ monsters. Each monster is characterized by its power $a_i$. Each hero is characterized by his power $p_i$ and endurance $s_i$. The heroes clear the dungeon day by day. In the beginning of each day, you choose a hero (exactly one) who is going to enter the dungeon this day. When the hero enters the dungeon, he is challenged by the first monster which was not defeated during the previous days (so, if the heroes have already defeated $k$ monsters, the hero fights with the monster $k + 1$). When the hero fights the monster, there are two possible outcomes: if the monster's power is strictly greater than the hero's power, the hero retreats from the dungeon. The current day ends; otherwise, the monster is defeated. After defeating a monster, the hero either continues fighting with the next monster or leaves the dungeon. He leaves the dungeon either if he has already defeated the number of monsters equal to his endurance during this day (so, the $i$-th hero cannot defeat more than $s_i$ monsters during each day), or if all monsters are defeated โ€” otherwise, he fights with the next monster. When the hero leaves the dungeon, the current day ends. Your goal is to defeat the last monster. What is the minimum number of days that you need to achieve your goal? Each day you have to use exactly one hero; it is possible that some heroes don't fight the monsters at all. Each hero can be used arbitrary number of times. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^5$) โ€” the number of test cases. Then the test cases follow. The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) โ€” the number of monsters in the dungeon. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the power of the $i$-th monster. The third line contains one integer $m$ ($1 \le m \le 2 \cdot 10^5$) โ€” the number of heroes in your party. Then $m$ lines follow, each describing a hero. Each line contains two integers $p_i$ and $s_i$ ($1 \le p_i \le 10^9$, $1 \le s_i \le n$) โ€” the power and the endurance of the $i$-th hero. It is guaranteed that the sum of $n + m$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case print one integer โ€” the minimum number of days you have to spend to defeat all of the monsters (or $-1$ if it is impossible). -----Example----- Input 2 6 2 3 11 14 1 8 2 3 2 100 1 5 3 5 100 2 3 2 30 5 90 1 Output 5 -1
import sys for _ in range(int(input())): n = int(sys.stdin.readline()) mons = list(map(int, sys.stdin.readline().split())) m = int(sys.stdin.readline()) _heros = sorted((tuple(map(int, sys.stdin.readline().split())) for _ in range(m)), reverse=True) max_s = 0 pows = [] endu = [] for i in range(m): if max_s >= _heros[i][1]: continue max_s = max(max_s, _heros[i][1]) pows.append(_heros[i][0]) endu.append(_heros[i][1]) pows.append(0) endu.append(10**9) i = 0 for ans in range(1, 10**9): hero_i = 0 power = pows[0] mons_power = 0 if power < mons[i]: print(-1) break for j in range(1, n-i+1): if endu[hero_i] < j: hero_i += 1 power = pows[hero_i] mons_power = max(mons_power, mons[i]) if power < mons_power: break i += 1 else: print(ans) break
You play a computer game. In this game, you lead a party of $m$ heroes, and you have to clear a dungeon with $n$ monsters. Each monster is characterized by its power $a_i$. Each hero is characterized by his power $p_i$ and endurance $s_i$. The heroes clear the dungeon day by day. In the beginning of each day, you choose a hero (exactly one) who is going to enter the dungeon this day. When the hero enters the dungeon, he is challenged by the first monster which was not defeated during the previous days (so, if the heroes have already defeated $k$ monsters, the hero fights with the monster $k + 1$). When the hero fights the monster, there are two possible outcomes: if the monster's power is strictly greater than the hero's power, the hero retreats from the dungeon. The current day ends; otherwise, the monster is defeated. After defeating a monster, the hero either continues fighting with the next monster or leaves the dungeon. He leaves the dungeon either if he has already defeated the number of monsters equal to his endurance during this day (so, the $i$-th hero cannot defeat more than $s_i$ monsters during each day), or if all monsters are defeated โ€” otherwise, he fights with the next monster. When the hero leaves the dungeon, the current day ends. Your goal is to defeat the last monster. What is the minimum number of days that you need to achieve your goal? Each day you have to use exactly one hero; it is possible that some heroes don't fight the monsters at all. Each hero can be used arbitrary number of times. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^5$) โ€” the number of test cases. Then the test cases follow. The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) โ€” the number of monsters in the dungeon. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the power of the $i$-th monster. The third line contains one integer $m$ ($1 \le m \le 2 \cdot 10^5$) โ€” the number of heroes in your party. Then $m$ lines follow, each describing a hero. Each line contains two integers $p_i$ and $s_i$ ($1 \le p_i \le 10^9$, $1 \le s_i \le n$) โ€” the power and the endurance of the $i$-th hero. It is guaranteed that the sum of $n + m$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case print one integer โ€” the minimum number of days you have to spend to defeat all of the monsters (or $-1$ if it is impossible). -----Example----- Input 2 6 2 3 11 14 1 8 2 3 2 100 1 5 3 5 100 2 3 2 30 5 90 1 Output 5 -1
""" NTC here """ from sys import stdin def iin(): return int(stdin.readline()) def lin(): return list(map(int, stdin.readline().split())) # range = xrange # input = raw_input def main(): t=iin() while t: t-=1 n=iin() a=lin() m=iin() h=[lin()[::-1] for i in range(m)] h.sort(reverse=True) a1=[[j,i] for i,j in enumerate(a)] a2=[-1]*n a1.sort() i=0 j=0 while j<n and i<m: if h[i][1]>=a1[j][0]: a2[a1[j][1]]=i j+=1 else: i+=1 if -1 in a2: print(-1) else: dp=[1]*n for i in range(1,n): ad=[0] ch=0 if h[a2[i]][0]>dp[i-1]: if h[a2[i]][1]>=h[a2[i-1]][1]: ad.append(dp[i-1]) ch+=1 if h[a2[i-1]][0]>dp[i-1]: if h[a2[i-1]][1]>=h[a2[i]][1]: ad.append(dp[i-1]) if ch==0: a2[i]=a2[i-1] dp[i]+=max(ad) print(dp.count(1)) # print(dp,a2,h) main() # try: # main() # except Exception as e: print(e)
You play a computer game. In this game, you lead a party of $m$ heroes, and you have to clear a dungeon with $n$ monsters. Each monster is characterized by its power $a_i$. Each hero is characterized by his power $p_i$ and endurance $s_i$. The heroes clear the dungeon day by day. In the beginning of each day, you choose a hero (exactly one) who is going to enter the dungeon this day. When the hero enters the dungeon, he is challenged by the first monster which was not defeated during the previous days (so, if the heroes have already defeated $k$ monsters, the hero fights with the monster $k + 1$). When the hero fights the monster, there are two possible outcomes: if the monster's power is strictly greater than the hero's power, the hero retreats from the dungeon. The current day ends; otherwise, the monster is defeated. After defeating a monster, the hero either continues fighting with the next monster or leaves the dungeon. He leaves the dungeon either if he has already defeated the number of monsters equal to his endurance during this day (so, the $i$-th hero cannot defeat more than $s_i$ monsters during each day), or if all monsters are defeated โ€” otherwise, he fights with the next monster. When the hero leaves the dungeon, the current day ends. Your goal is to defeat the last monster. What is the minimum number of days that you need to achieve your goal? Each day you have to use exactly one hero; it is possible that some heroes don't fight the monsters at all. Each hero can be used arbitrary number of times. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^5$) โ€” the number of test cases. Then the test cases follow. The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) โ€” the number of monsters in the dungeon. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the power of the $i$-th monster. The third line contains one integer $m$ ($1 \le m \le 2 \cdot 10^5$) โ€” the number of heroes in your party. Then $m$ lines follow, each describing a hero. Each line contains two integers $p_i$ and $s_i$ ($1 \le p_i \le 10^9$, $1 \le s_i \le n$) โ€” the power and the endurance of the $i$-th hero. It is guaranteed that the sum of $n + m$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case print one integer โ€” the minimum number of days you have to spend to defeat all of the monsters (or $-1$ if it is impossible). -----Example----- Input 2 6 2 3 11 14 1 8 2 3 2 100 1 5 3 5 100 2 3 2 30 5 90 1 Output 5 -1
import sys input = lambda: sys.stdin.readline().strip() print = lambda s: sys.stdout.write(s) t = int(input()) for i in range(t): n = int(input()) ls1 = list(map(int, input().split())) m = int(input()) ls2 = [] for i in range(m): ls2.append(tuple(map(int, input().split()))) if max(ls1)>max(i[0] for i in ls2): print('-1\n') else: temp = {} for i in range(1, n+1): temp[i] = 0 for i in ls2: try: temp[i[1]] = max(temp[i[1]], i[0]) except: temp[i[1]] = i[0] d = {} d[n] = temp[n] for k in range(n-1, 0, -1): d[k] = max(d[k+1], temp[k]) i = 0 cnt = 1 ans = 1 M = ls1[0] while True: if d[cnt]>=M: cnt+=1 i+=1 if i==n: break M = max(M, ls1[i]) else: ans+=1 cnt = 1 M = ls1[i] print(str(ans)+'\n')
You play a computer game. In this game, you lead a party of $m$ heroes, and you have to clear a dungeon with $n$ monsters. Each monster is characterized by its power $a_i$. Each hero is characterized by his power $p_i$ and endurance $s_i$. The heroes clear the dungeon day by day. In the beginning of each day, you choose a hero (exactly one) who is going to enter the dungeon this day. When the hero enters the dungeon, he is challenged by the first monster which was not defeated during the previous days (so, if the heroes have already defeated $k$ monsters, the hero fights with the monster $k + 1$). When the hero fights the monster, there are two possible outcomes: if the monster's power is strictly greater than the hero's power, the hero retreats from the dungeon. The current day ends; otherwise, the monster is defeated. After defeating a monster, the hero either continues fighting with the next monster or leaves the dungeon. He leaves the dungeon either if he has already defeated the number of monsters equal to his endurance during this day (so, the $i$-th hero cannot defeat more than $s_i$ monsters during each day), or if all monsters are defeated โ€” otherwise, he fights with the next monster. When the hero leaves the dungeon, the current day ends. Your goal is to defeat the last monster. What is the minimum number of days that you need to achieve your goal? Each day you have to use exactly one hero; it is possible that some heroes don't fight the monsters at all. Each hero can be used arbitrary number of times. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^5$) โ€” the number of test cases. Then the test cases follow. The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) โ€” the number of monsters in the dungeon. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the power of the $i$-th monster. The third line contains one integer $m$ ($1 \le m \le 2 \cdot 10^5$) โ€” the number of heroes in your party. Then $m$ lines follow, each describing a hero. Each line contains two integers $p_i$ and $s_i$ ($1 \le p_i \le 10^9$, $1 \le s_i \le n$) โ€” the power and the endurance of the $i$-th hero. It is guaranteed that the sum of $n + m$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case print one integer โ€” the minimum number of days you have to spend to defeat all of the monsters (or $-1$ if it is impossible). -----Example----- Input 2 6 2 3 11 14 1 8 2 3 2 100 1 5 3 5 100 2 3 2 30 5 90 1 Output 5 -1
import math import sys from bisect import bisect_right as bs for _ in range(int(input())): n=int(sys.stdin.readline()) a=list(map(int,sys.stdin.readline().split())) m=int(input()) ma=-1 h=[0]*(n+1) for i in range(m): x,y=list(map(int,sys.stdin.readline().split())) ma=max(ma,x) h[y]=max(h[y],x) for i in range(n-1,0,-1): h[i]=max(h[i+1],h[i]) # print(h) if ma<max(a): print(-1) else: ma=-1 prev=0 ans=1 i=0 while i<n: ma=max(a[i],ma) # print(ma,i,ans) if h[i-prev+1]<ma: prev=i ans+=1 ma=-1 else: i+=1 print(ans)
You play a computer game. In this game, you lead a party of $m$ heroes, and you have to clear a dungeon with $n$ monsters. Each monster is characterized by its power $a_i$. Each hero is characterized by his power $p_i$ and endurance $s_i$. The heroes clear the dungeon day by day. In the beginning of each day, you choose a hero (exactly one) who is going to enter the dungeon this day. When the hero enters the dungeon, he is challenged by the first monster which was not defeated during the previous days (so, if the heroes have already defeated $k$ monsters, the hero fights with the monster $k + 1$). When the hero fights the monster, there are two possible outcomes: if the monster's power is strictly greater than the hero's power, the hero retreats from the dungeon. The current day ends; otherwise, the monster is defeated. After defeating a monster, the hero either continues fighting with the next monster or leaves the dungeon. He leaves the dungeon either if he has already defeated the number of monsters equal to his endurance during this day (so, the $i$-th hero cannot defeat more than $s_i$ monsters during each day), or if all monsters are defeated โ€” otherwise, he fights with the next monster. When the hero leaves the dungeon, the current day ends. Your goal is to defeat the last monster. What is the minimum number of days that you need to achieve your goal? Each day you have to use exactly one hero; it is possible that some heroes don't fight the monsters at all. Each hero can be used arbitrary number of times. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^5$) โ€” the number of test cases. Then the test cases follow. The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) โ€” the number of monsters in the dungeon. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the power of the $i$-th monster. The third line contains one integer $m$ ($1 \le m \le 2 \cdot 10^5$) โ€” the number of heroes in your party. Then $m$ lines follow, each describing a hero. Each line contains two integers $p_i$ and $s_i$ ($1 \le p_i \le 10^9$, $1 \le s_i \le n$) โ€” the power and the endurance of the $i$-th hero. It is guaranteed that the sum of $n + m$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case print one integer โ€” the minimum number of days you have to spend to defeat all of the monsters (or $-1$ if it is impossible). -----Example----- Input 2 6 2 3 11 14 1 8 2 3 2 100 1 5 3 5 100 2 3 2 30 5 90 1 Output 5 -1
from collections import * from bisect import bisect_left as bl import sys input = sys.stdin.readline def li():return [int(i) for i in input().rstrip('\n').split(' ')] def st():return input().rstrip('\n') def val():return int(input()) def stli():return [int(i) for i in input().rstrip('\n')] for _ in range(val()): n = val() a = li() # print(a) m = val() h = [] visited = defaultdict(int) for i in range(m): x,y = li() visited[x] = max(visited[x],y) h = [] for i in visited: h.append([i,visited[i]]) h.sort(reverse = 1) endurance = {} m = len(h) currmax = -float('inf') for i in range(m): if h[i][1]>currmax: currmax = max(currmax,h[i][1]) endurance[h[i][0]] = currmax power = sorted(list(endurance)) # print(power,endurance) totdays = 0 i = 0 while i<n: ind = bl(power,a[i]) if ind == len(power): totdays = -1 break cou = 0 while i<n: while ind<len(power) and a[i]>power[ind]: ind+=1 if ind == len(power): totdays = -1 break if endurance[power[ind]] <= cou: break i+=1 cou += 1 if totdays == -1:break totdays += 1 print(totdays)
You play a computer game. In this game, you lead a party of $m$ heroes, and you have to clear a dungeon with $n$ monsters. Each monster is characterized by its power $a_i$. Each hero is characterized by his power $p_i$ and endurance $s_i$. The heroes clear the dungeon day by day. In the beginning of each day, you choose a hero (exactly one) who is going to enter the dungeon this day. When the hero enters the dungeon, he is challenged by the first monster which was not defeated during the previous days (so, if the heroes have already defeated $k$ monsters, the hero fights with the monster $k + 1$). When the hero fights the monster, there are two possible outcomes: if the monster's power is strictly greater than the hero's power, the hero retreats from the dungeon. The current day ends; otherwise, the monster is defeated. After defeating a monster, the hero either continues fighting with the next monster or leaves the dungeon. He leaves the dungeon either if he has already defeated the number of monsters equal to his endurance during this day (so, the $i$-th hero cannot defeat more than $s_i$ monsters during each day), or if all monsters are defeated โ€” otherwise, he fights with the next monster. When the hero leaves the dungeon, the current day ends. Your goal is to defeat the last monster. What is the minimum number of days that you need to achieve your goal? Each day you have to use exactly one hero; it is possible that some heroes don't fight the monsters at all. Each hero can be used arbitrary number of times. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^5$) โ€” the number of test cases. Then the test cases follow. The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) โ€” the number of monsters in the dungeon. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the power of the $i$-th monster. The third line contains one integer $m$ ($1 \le m \le 2 \cdot 10^5$) โ€” the number of heroes in your party. Then $m$ lines follow, each describing a hero. Each line contains two integers $p_i$ and $s_i$ ($1 \le p_i \le 10^9$, $1 \le s_i \le n$) โ€” the power and the endurance of the $i$-th hero. It is guaranteed that the sum of $n + m$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case print one integer โ€” the minimum number of days you have to spend to defeat all of the monsters (or $-1$ if it is impossible). -----Example----- Input 2 6 2 3 11 14 1 8 2 3 2 100 1 5 3 5 100 2 3 2 30 5 90 1 Output 5 -1
from collections import * from bisect import bisect_left as bl import sys input = sys.stdin.readline def li():return [int(i) for i in input().rstrip('\n').split(' ')] def val():return int(input()) for _ in range(val()): n = val();a = li();m = val();h = [];visited = defaultdict(int) for i in range(m): x,y = li() visited[x] = max(visited[x],y) endurance, currmax, h = {}, -float('inf'), sorted([[i,visited[i]] for i in visited],key = lambda x:x[0],reverse = 1) for i in range(len(h)): if h[i][1]>currmax: currmax = max(currmax,h[i][1]) endurance[h[i][0]] = currmax power = sorted(list(endurance)) totdays = i = 0 while i<n: ind = bl(power,a[i]) if ind == len(power): totdays = -1 break cou = 0 while i<n: while ind<len(power) and a[i]>power[ind]:ind+=1 if ind == len(power): totdays = -1;break if endurance[power[ind]] <= cou:break i+=1 cou += 1 if totdays == -1:break totdays += 1 print(totdays)
You play a computer game. In this game, you lead a party of $m$ heroes, and you have to clear a dungeon with $n$ monsters. Each monster is characterized by its power $a_i$. Each hero is characterized by his power $p_i$ and endurance $s_i$. The heroes clear the dungeon day by day. In the beginning of each day, you choose a hero (exactly one) who is going to enter the dungeon this day. When the hero enters the dungeon, he is challenged by the first monster which was not defeated during the previous days (so, if the heroes have already defeated $k$ monsters, the hero fights with the monster $k + 1$). When the hero fights the monster, there are two possible outcomes: if the monster's power is strictly greater than the hero's power, the hero retreats from the dungeon. The current day ends; otherwise, the monster is defeated. After defeating a monster, the hero either continues fighting with the next monster or leaves the dungeon. He leaves the dungeon either if he has already defeated the number of monsters equal to his endurance during this day (so, the $i$-th hero cannot defeat more than $s_i$ monsters during each day), or if all monsters are defeated โ€” otherwise, he fights with the next monster. When the hero leaves the dungeon, the current day ends. Your goal is to defeat the last monster. What is the minimum number of days that you need to achieve your goal? Each day you have to use exactly one hero; it is possible that some heroes don't fight the monsters at all. Each hero can be used arbitrary number of times. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^5$) โ€” the number of test cases. Then the test cases follow. The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) โ€” the number of monsters in the dungeon. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the power of the $i$-th monster. The third line contains one integer $m$ ($1 \le m \le 2 \cdot 10^5$) โ€” the number of heroes in your party. Then $m$ lines follow, each describing a hero. Each line contains two integers $p_i$ and $s_i$ ($1 \le p_i \le 10^9$, $1 \le s_i \le n$) โ€” the power and the endurance of the $i$-th hero. It is guaranteed that the sum of $n + m$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case print one integer โ€” the minimum number of days you have to spend to defeat all of the monsters (or $-1$ if it is impossible). -----Example----- Input 2 6 2 3 11 14 1 8 2 3 2 100 1 5 3 5 100 2 3 2 30 5 90 1 Output 5 -1
import sys input = sys.stdin.readline T = int(input()) Ans = [] for _ in range(T): N = int(input()) A = list(map(int, input().split())) M = int(input()) PS = [list(map(int, input().split())) for _ in range(M)] L = [0] * (N+1) for p, s in PS: L[s] = max(L[s], p) for i in range(N-1, -1, -1): L[i] = max(L[i], L[i+1]) ans = 1 cnt = 1 ma = 0 if L[1] < max(A): Ans.append(-1) continue for a in A: ma = max(ma, a) if L[cnt] < ma: cnt = 1 ans += 1 ma = a cnt += 1 Ans.append(ans) print("\n".join(map(str, Ans)))
You play a computer game. In this game, you lead a party of $m$ heroes, and you have to clear a dungeon with $n$ monsters. Each monster is characterized by its power $a_i$. Each hero is characterized by his power $p_i$ and endurance $s_i$. The heroes clear the dungeon day by day. In the beginning of each day, you choose a hero (exactly one) who is going to enter the dungeon this day. When the hero enters the dungeon, he is challenged by the first monster which was not defeated during the previous days (so, if the heroes have already defeated $k$ monsters, the hero fights with the monster $k + 1$). When the hero fights the monster, there are two possible outcomes: if the monster's power is strictly greater than the hero's power, the hero retreats from the dungeon. The current day ends; otherwise, the monster is defeated. After defeating a monster, the hero either continues fighting with the next monster or leaves the dungeon. He leaves the dungeon either if he has already defeated the number of monsters equal to his endurance during this day (so, the $i$-th hero cannot defeat more than $s_i$ monsters during each day), or if all monsters are defeated โ€” otherwise, he fights with the next monster. When the hero leaves the dungeon, the current day ends. Your goal is to defeat the last monster. What is the minimum number of days that you need to achieve your goal? Each day you have to use exactly one hero; it is possible that some heroes don't fight the monsters at all. Each hero can be used arbitrary number of times. -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^5$) โ€” the number of test cases. Then the test cases follow. The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) โ€” the number of monsters in the dungeon. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the power of the $i$-th monster. The third line contains one integer $m$ ($1 \le m \le 2 \cdot 10^5$) โ€” the number of heroes in your party. Then $m$ lines follow, each describing a hero. Each line contains two integers $p_i$ and $s_i$ ($1 \le p_i \le 10^9$, $1 \le s_i \le n$) โ€” the power and the endurance of the $i$-th hero. It is guaranteed that the sum of $n + m$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case print one integer โ€” the minimum number of days you have to spend to defeat all of the monsters (or $-1$ if it is impossible). -----Example----- Input 2 6 2 3 11 14 1 8 2 3 2 100 1 5 3 5 100 2 3 2 30 5 90 1 Output 5 -1
import sys sin = sys.stdin t = int(sin.readline()) for _ in range(t): n = int(sin.readline()) monpows = [int(x) for x in sin.readline().split()] m = int(sin.readline()) endtopow = dict() maxhero = 0 for _ in range(m): h = [int(x) for x in sin.readline().split()] maxhero = max(maxhero, h[0]) if h[1] in endtopow: endtopow[h[1]] = max(h[0], endtopow[h[1]]) else: endtopow[h[1]] = h[0] endurances = [0 for x in range(n+2)] for i in range(len(endurances) - 2, -1, -1): if i in endtopow: endurances[i] = max(endurances[i+1], endtopow[i]) else: endurances[i] = endurances[i+1] days = 0 msofar = 0 maxpow = 0 i = 0 cant = False while i < n: maxpow = max(maxpow, monpows[i]) if maxpow > maxhero: cant = True break if maxpow <= endurances[msofar + 1]: i += 1 msofar += 1 else: msofar = 0 maxpow = 0 days += 1 days += 1 if not cant: print(days) else: print(-1)