description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
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
|
t = int(input())
for _ in range(t):
n = int(input())
s = [int(x) for x in input().split()]
m = int(input())
L = []
for i in range(m):
pi, si = map(int, input().split())
L.append((pi, si))
edu = [0] * (n + 1)
for i in range(0, len(L)):
edu[L[i][1]] = max(edu[L[i][1]], L[i][0])
mx = edu[n]
opt = [edu[n]]
for i in range(n - 1, -1, -1):
mx = max(mx, edu[i])
opt.append(mx)
opt = opt[::-1]
if max(opt) < max(s):
print(-1)
else:
temp = -1
ptr = 0
curr_mx = 0
ct = 0
ans = 1
while ptr < n:
curr_mx = max(curr_mx, s[ptr])
if opt[ct + 1] >= curr_mx:
ptr += 1
ct += 1
else:
ans += 1
curr_mx = 0
ct = 0
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
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)))
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
|
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
|
o = int(input())
for _ in range(0, o):
n = int(input())
lis = [0] + list(map(int, input().split()))
m = int(input())
l = [0] * (n + 1)
for i in range(0, m):
p, s = map(int, input().split())
l[s] = max(l[s], p)
for i in range(n - 1, -1, -1):
l[i] = max(l[i], l[i + 1])
k = 0
ans = 0
while k < n:
q = 0
t = 0
while k + t + 1 <= n and l[t + 1] >= max(q, lis[k + t + 1]):
q = max(q, lis[k + t + 1])
t += 1
if t == 0:
ans = -1
break
k += t
ans += 1
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
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
|
t = int(input())
for r in range(0, t):
n = int(input())
a = list(map(int, input().split()))
monster = max(a)
m = int(input())
h = list(tuple(map(int, input().split())) for i in range(m))
h.sort()
if monster > h[m - 1][0]:
print(-1)
continue
i = m - 2
last = h[m - 1][1]
while i >= 0:
if h[i][1] <= last:
del h[i]
else:
last = h[i][1]
i -= 1
index = 0
ans = 0
size = len(h) - 1
while index < n:
mx = a[index]
s = h[size][1]
i = size
while h[i][0] >= mx and index < n:
if s != 0:
index += 1
if index == n:
break
mx = max(a[index], mx)
s -= 1
else:
if i == 0:
break
s = h[i - 1][1] - h[i][1]
i -= 1
ans += 1
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
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 *
input = stdin.readline
for _ in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
d = [(0) for i in range(n + 1)]
for i in range(int(input())):
p, e = map(int, input().split())
d[e] = max(d[e], p)
for i in range(n - 1, -1, -1):
d[i] = max(d[i], d[i + 1])
if d[1] < max(l):
print(-1)
continue
days, curr, m = 1, 1, 0
for i in l:
m = max(i, m)
if d[curr] < m:
days += 1
curr = 1
m = i
curr += 1
print(days)
|
ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
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
|
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
m = int(input())
p = []
s = []
for i in range(m):
x, y = map(int, input().split())
p.append(x)
s.append(y)
bst = [(0) for i in range(n + 1)]
for i in range(m):
bst[s[i]] = max(bst[s[i]], p[i])
for i in range(n - 1, -1, -1):
bst[i] = max(bst[i + 1], bst[i])
x = 0
cnt = 0
mst = 0
while mst != n:
prev = mst
m_p = a[mst]
x = 1
while m_p <= bst[x]:
x += 1
mst += 1
if mst == n:
break
m_p = max(m_p, a[mst])
if mst == prev:
cnt = -1
break
cnt += 1
print(cnt)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
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)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
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
def init_max(init_max_val):
for i in range(n):
seg_max[i + num_max - 1] = init_max_val[i]
for i in range(num_max - 2, -1, -1):
seg_max[i] = max(seg_max[2 * i + 1], seg_max[2 * i + 2])
def update_max(k, x):
k += num_max - 1
seg_max[k] = x
while k:
k = (k - 1) // 2
seg_max[k] = max(seg_max[k * 2 + 1], seg_max[k * 2 + 2])
def query_max(p, q):
if q <= p:
return ide_ele_max
p += num_max - 1
q += num_max - 2
res = ide_ele_max
while q - p > 1:
if p & 1 == 0:
res = max(res, seg_max[p])
if q & 1 == 1:
res = max(res, seg_max[q])
q -= 1
p = p // 2
q = (q - 1) // 2
if p == q:
res = max(res, seg_max[p])
else:
res = max(max(res, seg_max[p]), seg_max[q])
return res
qq = int(input())
for testcases in [0] * qq:
n = int(input())
a = list(map(int, input().split()))
m = int(input())
p = [-1] * n
for _ in [0] * m:
x, y = map(int, input().split())
p[y - 1] = max(p[y - 1], x)
tmp_max = -1
for i in range(n - 1, -1, -1):
tmp_max = max(tmp_max, p[i])
p[i] = tmp_max
if p[0] < max(a):
print(-1)
continue
if n == 1:
print(1)
continue
if n == 2:
if p[1] >= a[0] and p[1] >= a[1]:
print(1)
else:
print(2)
continue
ide_ele_max = -1
num_max = 2 ** (n - 1).bit_length()
seg_max = [ide_ele_max] * 2 * num_max
init_max(a)
start = 0
res = 0
while start < n:
ok = 0
ng = n - start
while ng - ok > 1:
mid = (ok + ng) // 2
if query_max(start, start + mid + 1) <= p[mid]:
ok = mid
else:
ng = mid
res += 1
start += ok + 1
print(res)
|
IMPORT ASSIGN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
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
readline = sys.stdin.readline
mr = lambda: map(int, readline().split())
t = int(readline())
Ans = []
for _ in range(t):
n = int(readline())
monsters = list(mr())
m = int(readline())
heroes = [tuple(mr()) for _ in range(m)]
tmp = [0] * (n + 1)
for x in heroes:
tmp[x[1]] = max(tmp[x[1]], x[0])
for i in range(n - 1, -1, -1):
tmp[i] = max(tmp[i + 1], tmp[i])
qq = 0
ans = 0
while qq < n:
maxMonsterPower = 0
j = 0
for i in range(1, n - qq + 1):
if maxMonsterPower < monsters[qq + i - 1]:
maxMonsterPower = monsters[qq + i - 1]
if tmp[i] < maxMonsterPower:
break
j += 1
if j < 1 and qq < n:
ans = -1
break
qq += j
ans += 1
Ans.append(ans)
print("\n".join(map(str, Ans)))
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
|
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 t in range(int(sys.stdin.readline())):
mm = int(sys.stdin.readline())
m = [int(i) for i in sys.stdin.readline().split()]
hh = int(sys.stdin.readline())
h = []
for j in range(hh):
h.append([int(i) for i in sys.stdin.readline().split()])
dp = [-1] * (mm + 1)
for hhh in h:
dp[hhh[1]] = max(hhh[0], dp[hhh[1]])
for j in range(mm - 1, -1, -1):
dp[j] = max(dp[j], dp[j + 1])
ind = 0
ans = 0
while ind < mm:
cnt = 0
mx_p = 0
while ind + cnt < mm:
mx_p = max(mx_p, m[ind + cnt])
if dp[cnt + 1] >= mx_p:
cnt += 1
else:
break
if cnt == 0:
ans = -1
break
else:
ind += cnt
ans += 1
print(ans)
|
IMPORT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
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
class RangeMinimumQuery:
def __init__(self, n, func=min, inf=float("inf")):
self.n0 = 2 ** (n - 1).bit_length()
self.op = func
self.inf = inf
self.data = [self.inf] * (2 * self.n0 - 1)
def query(self, l, r):
l += self.n0
r += self.n0
res = self.inf
while l < r:
if r & 1:
r -= 1
res = self.op(res, self.data[r - 1])
if l & 1:
res = self.op(res, self.data[l - 1])
l += 1
l >>= 1
r >>= 1
return res
def update(self, i, x):
i += self.n0 - 1
self.data[i] = x
while i + 1:
i = ~-i // 2
self.data[i] = self.op(self.data[2 * i + 1], self.data[2 * i + 2])
def solve():
n = int(input())
a = list(map(int, input().split()))
RMQ = RangeMinimumQuery(n, func=max, inf=0)
for i, ai in enumerate(a):
RMQ.update(i, ai)
m = int(input())
ps = [list(map(int, input().split())) for i in range(m)]
bst = [0] * (n + 1)
for p, s in ps:
bst[s] = max(bst[s], p)
for i in reversed(range(1, n + 1)):
bst[i - 1] = max(bst[i - 1], bst[i])
cur = -1
ans = 0
while cur != n - 1:
left = cur
right = n
while right - left > 1:
mid = (right + left) // 2
x = mid - cur
if RMQ.query(cur + 1, mid + 1) > bst[x]:
right = mid
else:
left = mid
if left == cur:
print(-1)
return
cur = left
ans += 1
print(ans)
t = int(input())
for i in range(t):
solve()
|
IMPORT ASSIGN VAR VAR CLASS_DEF FUNC_DEF VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR WHILE BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
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)]
p = [0] * (n + 1)
for i in range(m):
p[ps[i][1]] = max(p[ps[i][1]], ps[i][0])
for i in range(n)[::-1]:
p[i] = max(p[i], p[i + 1])
if p[1] < max(a):
ANS.append(-1)
continue
ans = 0
mx = 0
cnt = 0
i = 0
for x in a:
cnt += 1
mx = max(mx, x)
if p[cnt] < mx:
ans += 1
mx = x
cnt = 1
if cnt:
ans += 1
ANS.append(ans)
print("\n".join(map(str, ANS)))
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
|
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())
while t:
t -= 1
n = int(input())
a = list(map(int, input().split()))
m = int(input())
heros = []
for _ in range(m):
p, e = map(int, input().split())
heros.append((p, e))
bst = [0] * n
for p, e in heros:
bst[e - 1] = max(p, bst[e - 1])
for i in range(n - 2, -1, -1):
bst[i] = max(bst[i + 1], bst[i])
td = 0
days = 0
while td < n:
streak = 0
max_def = a[td + streak]
while td + streak < n and max(max_def, a[td + streak]) <= bst[streak]:
max_def = max(max_def, a[td + streak])
streak += 1
if not streak:
print(-1)
break
days += 1
td += streak
streak = 0
else:
print(days)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR WHILE BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
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())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
h = int(input())
he = []
dp = [0] * (n + 2)
for _ in range(h):
p, s = list(map(int, input().split()))
he.append([p, s])
dp[s] = max(dp[s], p)
for i in range(n + 1 - 1, -1, -1):
dp[i] = max(dp[i], dp[i + 1])
i = 0
flag = 1
total = 0
while i <= n - 1:
m = 0
for j in range(i, n + 1):
if j >= n:
break
m = max(m, a[j])
if m > dp[j - i + 1]:
break
total += 1
if j == i:
flag = 0
break
i = j
if flag == 0:
print(-1)
else:
print(total)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
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")
|
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
|
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
for nt in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
m = int(input())
maxx = {}
for i in range(m):
a, b = map(int, input().split())
if b not in maxx:
maxx[b] = a
else:
maxx[b] = max(a, maxx[b])
arr = []
for i in maxx:
arr.append([maxx[i], i])
arr.sort(reverse=True)
pref = []
prev = 0
for i in range(len(arr)):
new = arr[i][1]
if new > prev:
for j in range(prev, new):
pref.append(arr[i][0])
prev = new
if pref[0] < max(l):
print(-1)
continue
m = 0
count = 0
day = 0
for i in range(n):
m = max(m, l[i])
count += 1
if count > len(pref) or pref[count - 1] < m:
day += 1
count = 1
m = l[i]
print(day + 1)
|
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
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
|
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
m = int(input())
b = [tuple(map(int, input().split())) for __ in range(m)]
ans = 1
mx = [0] * (n + 1)
for p, s in b:
mx[s] = max(mx[s], p)
for i in range(n - 1, -1, -1):
mx[i] = max(mx[i], mx[i + 1])
if mx[1] < max(a):
print(-1)
else:
index = 1
ma = 0
for mon in a:
ma = max(mon, ma)
if mx[index] < ma:
index = 1
ans += 1
ma = mon
index += 1
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
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
def I():
return sys.stdin.readline().rstrip()
for _ in range(int(I())):
n = int(I())
a = list(map(int, I().split()))
m = int(I())
pl = sorted([list(map(int, I().split())) for _ in range(m)])
pln, mxs = [], 0
for x in pl[::-1]:
if x[1] > mxs:
pln.append(x)
mxs = max(mxs, x[1])
pl = pln[::-1]
m = len(pl)
p, s = map(list, zip(*pl))
if max(a) > max(p):
print(-1)
else:
days = 0
c = 0
d2 = 1
while d2 <= m:
d2 *= 2
d2 //= 2
while c < n:
days += 1
mx = 0
inday = 0
while c < n:
mx = max(mx, a[c])
inday += 1
pi = -1
d = d2
while d:
np = pi + d
if np < m and p[np] < mx:
pi = np
d //= 2
pi += 1
if pi < m and s[pi] >= inday:
c += 1
else:
break
print(days)
|
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST NUMBER FOR VAR VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
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
|
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
m = int(input())
h = [tuple(map(int, input().split())) for i in range(m)]
h.sort(reverse=True)
new_h = []
prev = 0
for p, s in h:
if s > prev:
new_h.append((p, s))
prev = s
h = new_h
hum = 0
res = 1
cur = 0
maxp = 0
for mon in a:
maxp = max(mon, maxp)
cur += 1
if mon > h[0][0]:
res = -1
break
if hum < len(h) and cur > h[hum][1]:
hum += 1
if hum == len(h) or maxp > h[hum][0]:
res += 1
hum = 0
cur = 1
maxp = mon
print(res)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
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
|
t = int(input())
for i in range(t):
n = int(input())
monsters = list(map(int, input().split()))
m = int(input())
dct = {}
lst = []
now = 0
index = 0
for j in range(m):
a, b = list(map(int, input().split()))
if b > now or b == now and dct[index][0] < a:
index = j
now = b
dct[j] = [a, b]
lst.append([a, j])
lst.sort()
sfx = []
sfx.append([dct[lst[m - 1][1]][1], lst[m - 1][1]])
for j in range(m - 2, -1, -1):
result1, i1 = sfx[-1]
i2 = lst[j][1]
result2 = dct[i2][1]
if result2 > result1:
sfx.append([result2, i2])
else:
sfx.append(sfx[-1])
days = 1
count = 0
endurance = now
strength = dct[index][0]
for j in range(n):
if endurance == 0:
endurance = now
strength = dct[index][0]
days += 1
count = 0
if lst[-1][0] < monsters[j]:
days = -1
break
if monsters[j] > strength:
left = -1
right = m - 1
while left + 1 < right:
mid = (left + right) // 2
if lst[mid][0] >= monsters[j]:
right = mid
else:
left = mid
nxt = sfx[m - right - 1][1]
strength = dct[nxt][0]
endurance = dct[nxt][1] - count
if endurance <= 0:
endurance = dct[nxt][1]
days += 1
count = 0
count += 1
endurance -= 1
print(days)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR LIST VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
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
readline = sys.stdin.readline
readlines = sys.stdin.readlines
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prn = lambda x: print(*x, sep="\n")
def solve():
n = ni()
a = nl()
m = ni()
g = [0] * (n + 2)
for _ in range(m):
s, p = nm()
g[p] = max(g[p], s)
for i in range(n, -1, -1):
g[i] = max(g[i], g[i + 1])
if max(a) > max(g):
print(-1)
return
ans = idx = 0
while idx < n:
cm = a[idx]
ct = 1
while idx + ct < n and max(cm, a[idx + ct]) <= g[ct + 1]:
cm = max(cm, a[idx + ct])
ct += 1
ans += 1
idx += ct
print(ans)
return
T = ni()
for _ in range(T):
solve()
|
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
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
|
q = int(input())
for i in range(q):
n = int(input())
a = input().split(" ")
a = [int(a[k]) for k in range(n)]
m = int(input())
p = list()
s = list()
dp = [(0) for j in range(n + 1)]
for j in range(m):
l = input().split(" ")
l = [int(l[k]) for k in range(0, 2)]
p.append(l[0])
s.append(l[1])
dp[l[1]] = max(dp[l[1]], l[0])
for j in range(n - 1, 0, -1):
dp[j] = max(dp[j], dp[j + 1])
zi = 0
ok = True
ans = 0
while zi < n and ok == True:
ans += 1
curr = zi
maxp = a[curr]
while maxp <= dp[curr - zi + 1]:
curr += 1
if curr == n:
break
maxp = max(maxp, a[curr])
if curr == zi:
ok = False
zi = curr
if ok == True:
print(ans)
else:
print("-1")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
|
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
class SegmentTree:
def __init__(self, arr, func, initialRes=0):
self.f = func
self.N = len(arr)
self.tree = [(0) for _ in range(4 * self.N)]
self.initialRes = initialRes
for i in range(self.N):
self.tree[self.N + i] = arr[i]
for i in range(self.N - 1, 0, -1):
self.tree[i] = self.f(self.tree[i << 1], self.tree[i << 1 | 1])
def updateTreeNode(self, idx, value):
self.tree[idx + self.N] = value
idx += self.N
i = idx
while i > 1:
self.tree[i >> 1] = self.f(self.tree[i], self.tree[i ^ 1])
i >>= 1
def query(self, l, r):
r += 1
res = self.initialRes
l += self.N
r += self.N
while l < r:
if l & 1:
res = self.f(res, self.tree[l])
l += 1
if r & 1:
r -= 1
res = self.f(res, self.tree[r])
l >>= 1
r >>= 1
return res
def getMaxSegTree(arr):
return SegmentTree(arr, lambda a, b: max(a, b), initialRes=-float("inf"))
def getMinSegTree(arr):
return SegmentTree(arr, lambda a, b: min(a, b), initialRes=float("inf"))
def getSumSegTree(arr):
return SegmentTree(arr, lambda a, b: a + b, initialRes=0)
def main():
def checkPossible(currMonster, furthestMonster):
maxMonsterPower = maxAST.query(currMonster, furthestMonster)
requiredendurance = furthestMonster - currMonster + 1
i = -1
b = m
while b > 0:
while i + b < m and ps[i + b][0] < maxMonsterPower:
i += b
b //= 2
i += 1
maxHeroEndurance = suffixMaxEndurance[i]
return maxHeroEndurance >= requiredendurance
t = int(input())
allans = []
for _ in range(t):
n = int(input())
a = readIntArr()
m = int(input())
ps = []
for __ in range(m):
ps.append(readIntArr())
ps.sort(key=lambda x: x[0])
if ps[-1][0] < max(a):
allans.append(-1)
continue
maxAST = getMaxSegTree(a)
suffixMaxEndurance = [(-inf) for _ in range(m)]
for i in range(m - 1, -1, -1):
suffixMaxEndurance[i] = ps[i][1]
if i + 1 < m:
suffixMaxEndurance[i] = max(
suffixMaxEndurance[i], suffixMaxEndurance[i + 1]
)
nDays = 0
currMonster = 0
while currMonster < n:
nDays += 1
furthestMonster = currMonster
b = n
while b > 0:
while furthestMonster + b < n and checkPossible(
currMonster, furthestMonster + b
):
furthestMonster += b
b //= 2
currMonster = furthestMonster + 1
allans.append(nDays)
multiLineArrayPrint(allans)
return
input = sys.stdin.buffer.readline
def oneLineArrayPrint(arr):
print(" ".join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
print("\n".join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
print("\n".join([" ".join([str(x) for x in y]) for y in arr]))
def readIntArr():
return [int(x) for x in input().split()]
def makeArr(defaultVal, dimensionArr):
dv = defaultVal
da = dimensionArr
if len(da) == 1:
return [dv for _ in range(da[0])]
else:
return [makeArr(dv, da[1:]) for _ in range(da[0])]
def queryInteractive(x, y):
print("? {} {}".format(x, y))
sys.stdout.flush()
return int(input())
def answerInteractive(ans):
print("! {}".format(ans))
sys.stdout.flush()
inf = float("inf")
MOD = 10**9 + 7
for _abc in range(1):
main()
|
IMPORT CLASS_DEF FUNC_DEF NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER WHILE BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
|
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
t = int(stdin.readline())
for _ in range(t):
n = int(stdin.readline())
x = list(map(int, stdin.readline().split()))
m = int(stdin.readline())
v, v2 = [], []
for i in range(m):
p, s = map(int, stdin.readline().split())
v2.append([p, s])
v2.sort()
for i in range(m - 1, -1, -1):
if i == m - 1 or v2[i][0] != v2[i + 1][0] and v2[i][1] != v2[i + 1][1]:
v.append(v2[i])
m = len(v)
ans, count, pointer, ma = 1, 0, 0, 0
for i in range(n):
count += 1
ma = max(ma, x[i])
if x[i] > v[0][0]:
ans = -1
break
if count <= v[pointer][1]:
pass
else:
while pointer < m and v[pointer][1] < count:
pointer += 1
if pointer < m and v[pointer][0] < ma:
break
if pointer >= m or v[pointer][1] < count or v[pointer][0] < ma:
pointer, count = 0, 1
ans += 1
ma = x[i]
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
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
def iin():
return int(stdin.readline())
def lin():
return list(map(int, stdin.readline().split()))
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))
main()
|
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
|
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
def main():
t = int(input())
allans = []
for _ in range(t):
n = int(input())
a = readIntArr()
m = int(input())
ps = []
for __ in range(m):
ps.append(readIntArr())
maxMonsterPower = max(a)
maxHeroPower = 0
for p, s in ps:
maxHeroPower = max(maxHeroPower, p)
if maxMonsterPower > maxHeroPower:
allans.append(-1)
continue
maxPower = [(-1) for _ in range(n + 1)]
for p, s in ps:
maxPower[s] = max(maxPower[s], p)
for i in range(n - 1, -1, -1):
maxPower[i] = max(maxPower[i], maxPower[i + 1])
currMonst = 0
nDays = 0
while currMonst < n:
nDays += 1
nextMonst = currMonst
monstMax = a[nextMonst]
for nMonst in range(1, n + 1):
if maxPower[nMonst] < monstMax:
assert nMonst != 1
break
nextMonst += 1
if nextMonst == n:
break
monstMax = max(monstMax, a[nextMonst])
assert nextMonst > currMonst
currMonst = nextMonst
allans.append(nDays)
multiLineArrayPrint(allans)
return
input = sys.stdin.buffer.readline
def oneLineArrayPrint(arr):
print(" ".join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
print("\n".join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
print("\n".join([" ".join([str(x) for x in y]) for y in arr]))
def readIntArr():
return [int(x) for x in input().split()]
def makeArr(defaultVal, dimensionArr):
dv = defaultVal
da = dimensionArr
if len(da) == 1:
return [dv for _ in range(da[0])]
else:
return [makeArr(dv, da[1:]) for _ in range(da[0])]
def queryInteractive(x, y):
print("? {} {}".format(x, y))
sys.stdout.flush()
return int(input())
def answerInteractive(ans):
print("! {}".format(ans))
sys.stdout.flush()
inf = float("inf")
MOD = 10**9 + 7
for _abc in range(1):
main()
|
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
|
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
|
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
m = int(input())
heroes = []
bst = [(0) for i in range(n + 1)]
for _ in range(m):
x, y = map(int, input().split())
bst[y] = max(bst[y], x)
for i in range(n - 1, -1, -1):
bst[i] = max(bst[i], bst[i + 1])
pos = 0
res = 0
yes = True
while pos < n:
res += 1
npos = pos
mx = 0
while npos < n:
mx = max(mx, a[npos])
if mx > bst[npos - pos + 1]:
break
npos += 1
if pos == npos:
yes = False
break
pos = npos
if not yes:
res = -1
print(res)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
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)
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
|
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER
|
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
|
tests = int(input())
for i in range(tests):
rez = 0
k = 0
n = int(input())
powers = list(map(int, input().split()))
m = int(input())
vect = []
powers = [0] + powers
vect = [0] * (n + 1)
for j in range(0, m):
prop = list(map(int, input().split()))
p = prop[0]
s = prop[1]
vect[s] = max(vect[s], p)
for j in range(n - 1, -1, -1):
vect[j] = max(vect[j], vect[j + 1])
while k < n:
a = 0
b = 0
while k + b <= n - 1 and vect[b + 1] >= max(a, powers[k + b + 1]):
a = max(a, powers[k + b + 1])
b = b + 1
if b == 0:
rez = -1
break
k = k + b
rez = rez + 1
print(rez)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
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
def input():
return next(stdin)
def main():
def solve():
n = int(input())
aa = [int(a) for a in input().split()]
m = int(input())
pso = []
for _ in range(m):
p, s = map(int, input().split())
pso.append((p, s))
pso.sort(reverse=True)
smax = 0
ps = []
for p, s in pso:
if s > smax:
smax = s
if ps != [] and p == ps[-1][0]:
ps[-1] = p, s
else:
ps.append((p, s))
pos = 0
count = 0
while pos < n:
d = 0
ma = 0
for p, s in ps:
if d + pos >= n or p < ma or p < aa[d + pos]:
break
while s > d and d + pos < n and p >= aa[d + pos]:
if aa[d + pos] > ma:
ma = aa[d + pos]
d += 1
if d == 0:
print(-1)
return
count += 1
pos += d
print(count)
q = int(input())
for _ in range(q):
solve()
main()
|
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR LIST VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR WHILE VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
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
|
IMPORT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
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
for _ in range(int(input())):
n = int(input())
mons = tuple(map(int, input().split()))
m = int(input())
_heros = sorted(
(tuple(map(int, input().split())) for _ in range(m)), reverse=True
) + [(0, 10**9)]
heros = []
max_s = 0
for i in range(m + 1):
if _heros[i][1] <= max_s:
continue
heros.append(_heros[i])
max_s = max(max_s, _heros[i][1])
if heros[0][0] < max(mons):
print(-1)
continue
mons_i = 0
for ans in range(1, 10**9):
hero_i = 0
max_mons_power = 0
for i in range(n - mons_i):
while heros[hero_i][1] <= i:
hero_i += 1
max_mons_power = max(max_mons_power, mons[mons_i])
if heros[hero_i][0] < max_mons_power:
break
mons_i += 1
else:
print(ans)
break
|
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER LIST NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR WHILE VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
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
|
T = int(input())
for t in range(T):
N = int(input())
A = list(map(int, input().split()))
M = int(input())
heroes = []
for m in range(M):
hero = list(map(int, input().split()))
heroes.append(hero)
best = {}
for p, s in heroes:
best[s] = max(best.get(s, 0), p)
for i in range(N - 1, -1, -1):
best[i] = max(best.get(i, 0), best.get(i + 1, 0))
cnt = 0
res = 0
while cnt < N:
if A[cnt] > best[1]:
res = -1
break
else:
x = 0
i = cnt + x
maxa = A[i]
while i < N:
maxa = max(maxa, A[i])
if maxa > best.get(x + 1, 0):
x -= 1
i -= 1
break
x += 1
i = cnt + x
res += 1
cnt += x + 1
print(res)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
This time Baby Ehab will only cut and not stick. He starts with a piece of paper with an array a of length n written on it, and then he does the following:
* he picks a range (l, r) and cuts the subsegment a_l, a_{l + 1}, β¦, a_r out, removing the rest of the array.
* he then cuts this range into multiple subranges.
* to add a number theory spice to it, he requires that the elements of every subrange must have their product equal to their [least common multiple (LCM)](https://en.wikipedia.org/wiki/Least_common_multiple).
Formally, he partitions the elements of a_l, a_{l + 1}, β¦, a_r into contiguous subarrays such that the product of every subarray is equal to its LCM. Now, for q independent ranges (l, r), tell Baby Ehab the minimum number of subarrays he needs.
Input
The first line contains 2 integers n and q (1 β€ n,q β€ 10^5) β the length of the array a and the number of queries.
The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β the elements of the array a.
Each of the next q lines contains 2 integers l and r (1 β€ l β€ r β€ n) β the endpoints of this query's interval.
Output
For each query, print its answer on a new line.
Example
Input
6 3
2 3 10 7 5 14
1 6
2 4
3 5
Output
3
1
2
Note
The first query asks about the whole array. You can partition it into [2], [3,10,7], and [5,14]. The first subrange has product and LCM equal to 2. The second has product and LCM equal to 210. And the third has product and LCM equal to 70. Another possible partitioning is [2,3], [10,7], and [5,14].
The second query asks about the range (2,4). Its product is equal to its LCM, so you don't need to partition it further.
The last query asks about the range (3,5). You can partition it into [10,7] and [5].
|
import sys
input = sys.stdin.buffer.readline
def LI():
return [int(i) for i in input().split()]
def LI_():
return [(int(i) - 1) for i in input().split()]
n, q = LI()
arr = LI()
prime_last_appear_at = dict()
dp = [([n] * (n + 1)) for i in range(18)]
have_primes = [[] for _ in range(10**5 + 3)]
for p in range(2, 10**5 + 2):
if not have_primes[p]:
prime_last_appear_at[p] = n
i = p
while i < len(have_primes):
have_primes[i].append(p)
i += p
for i in range(n - 1, -1, -1):
dp[0][i] = dp[0][i + 1]
for p in have_primes[arr[i]]:
dp[0][i] = min(dp[0][i], prime_last_appear_at[p])
prime_last_appear_at[p] = i
for i in range(1, len(dp)):
for j in range(n):
dp[i][j] = dp[i - 1][dp[i - 1][j]]
for _ in range(q):
l, r = LI_()
cur = l
res = 1
for jump in range(len(dp) - 1, -1, -1):
if dp[jump][cur] <= r:
res += 1 << jump
cur = dp[jump][cur]
print(res)
|
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
This time Baby Ehab will only cut and not stick. He starts with a piece of paper with an array a of length n written on it, and then he does the following:
* he picks a range (l, r) and cuts the subsegment a_l, a_{l + 1}, β¦, a_r out, removing the rest of the array.
* he then cuts this range into multiple subranges.
* to add a number theory spice to it, he requires that the elements of every subrange must have their product equal to their [least common multiple (LCM)](https://en.wikipedia.org/wiki/Least_common_multiple).
Formally, he partitions the elements of a_l, a_{l + 1}, β¦, a_r into contiguous subarrays such that the product of every subarray is equal to its LCM. Now, for q independent ranges (l, r), tell Baby Ehab the minimum number of subarrays he needs.
Input
The first line contains 2 integers n and q (1 β€ n,q β€ 10^5) β the length of the array a and the number of queries.
The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β the elements of the array a.
Each of the next q lines contains 2 integers l and r (1 β€ l β€ r β€ n) β the endpoints of this query's interval.
Output
For each query, print its answer on a new line.
Example
Input
6 3
2 3 10 7 5 14
1 6
2 4
3 5
Output
3
1
2
Note
The first query asks about the whole array. You can partition it into [2], [3,10,7], and [5,14]. The first subrange has product and LCM equal to 2. The second has product and LCM equal to 210. And the third has product and LCM equal to 70. Another possible partitioning is [2,3], [10,7], and [5,14].
The second query asks about the range (2,4). Its product is equal to its LCM, so you don't need to partition it further.
The last query asks about the range (3,5). You can partition it into [10,7] and [5].
|
import sys
input = sys.stdin.readline
MAX = 10**5
pf = list(range(MAX + 1))
p = 2
while p * p <= MAX:
if pf[p] == p:
for m in range(p * p, MAX + 1, p):
pf[m] = p
p += 1
def prime_factors(n):
while n > 1:
p = pf[n]
while n % p == 0:
n //= p
yield p
n, q = map(int, input().split())
a = list(map(int, input().split()))
LOG = 17
nxt = [([-1] * (n + 1)) for _ in range(LOG)]
last = {}
for i in range(n - 1, -1, -1):
nxt[0][i] = nxt[0][i + 1] if i + 1 < n else i + 1
for p in prime_factors(a[i]):
if p in last:
nxt[0][i] = min(nxt[0][i], last[p])
last[p] = i
for l in range(1, LOG):
j = nxt[l - 1][i]
nxt[l][i] = nxt[l - 1][j] if j != -1 else -1
for _ in range(q):
l, r = map(int, input().split())
l -= 1
res = 0
for e in range(LOG - 1, -1, -1):
if l <= nxt[e][l] <= r:
l = nxt[e][l]
res += 2**e
if l < r:
res += 1
print(res)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER FUNC_DEF WHILE VAR NUMBER ASSIGN VAR VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR EXPR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP NUMBER VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
This time Baby Ehab will only cut and not stick. He starts with a piece of paper with an array a of length n written on it, and then he does the following:
* he picks a range (l, r) and cuts the subsegment a_l, a_{l + 1}, β¦, a_r out, removing the rest of the array.
* he then cuts this range into multiple subranges.
* to add a number theory spice to it, he requires that the elements of every subrange must have their product equal to their [least common multiple (LCM)](https://en.wikipedia.org/wiki/Least_common_multiple).
Formally, he partitions the elements of a_l, a_{l + 1}, β¦, a_r into contiguous subarrays such that the product of every subarray is equal to its LCM. Now, for q independent ranges (l, r), tell Baby Ehab the minimum number of subarrays he needs.
Input
The first line contains 2 integers n and q (1 β€ n,q β€ 10^5) β the length of the array a and the number of queries.
The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β the elements of the array a.
Each of the next q lines contains 2 integers l and r (1 β€ l β€ r β€ n) β the endpoints of this query's interval.
Output
For each query, print its answer on a new line.
Example
Input
6 3
2 3 10 7 5 14
1 6
2 4
3 5
Output
3
1
2
Note
The first query asks about the whole array. You can partition it into [2], [3,10,7], and [5,14]. The first subrange has product and LCM equal to 2. The second has product and LCM equal to 210. And the third has product and LCM equal to 70. Another possible partitioning is [2,3], [10,7], and [5,14].
The second query asks about the range (2,4). Its product is equal to its LCM, so you don't need to partition it further.
The last query asks about the range (3,5). You can partition it into [10,7] and [5].
|
from sys import stdin, stdout
def cut(n, a_a):
N = max(a_a)
p_a = seive_a(N)
dic = {}
b_a = [[(-1) for j in range(20)] for i in range(n)]
b_a[n - 1][0] = n
for i in range(n - 1, -1, -1):
if i == n - 1:
r = n
else:
r = b_a[i + 1][0]
for p in p_a[a_a[i]]:
if p in dic:
r = min(r, dic[p])
dic[p] = i
b_a[i][0] = r
for i in range(1, 20):
for j in range(n):
if (
b_a[j][i - 1] == -1
or b_a[j][i - 1] == n
or b_a[b_a[j][i - 1]][i - 1] == -1
):
continue
b_a[j][i] = b_a[b_a[j][i - 1]][i - 1]
return b_a
def solve(b_a, l, r):
res = 0
for step in range(19, -1, -1):
if b_a[l][step] != -1 and b_a[l][step] <= r:
res += 1 << step
l = b_a[l][step]
return res + 1
def seive_a(n):
p_a = [[] for i in range(n + 1)]
for i in range(2, n + 1):
if len(p_a[i]) > 0:
continue
j = i
while j <= n:
p_a[j].append(i)
j += i
return p_a
n, q = map(int, stdin.readline().split())
a_a = list(map(int, stdin.readline().split()))
b_a = cut(n, a_a)
for _ in range(q):
l, r = map(int, stdin.readline().split())
res = solve(b_a, l - 1, r - 1)
stdout.write(str(res) + "\n")
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
|
This time Baby Ehab will only cut and not stick. He starts with a piece of paper with an array a of length n written on it, and then he does the following:
* he picks a range (l, r) and cuts the subsegment a_l, a_{l + 1}, β¦, a_r out, removing the rest of the array.
* he then cuts this range into multiple subranges.
* to add a number theory spice to it, he requires that the elements of every subrange must have their product equal to their [least common multiple (LCM)](https://en.wikipedia.org/wiki/Least_common_multiple).
Formally, he partitions the elements of a_l, a_{l + 1}, β¦, a_r into contiguous subarrays such that the product of every subarray is equal to its LCM. Now, for q independent ranges (l, r), tell Baby Ehab the minimum number of subarrays he needs.
Input
The first line contains 2 integers n and q (1 β€ n,q β€ 10^5) β the length of the array a and the number of queries.
The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β the elements of the array a.
Each of the next q lines contains 2 integers l and r (1 β€ l β€ r β€ n) β the endpoints of this query's interval.
Output
For each query, print its answer on a new line.
Example
Input
6 3
2 3 10 7 5 14
1 6
2 4
3 5
Output
3
1
2
Note
The first query asks about the whole array. You can partition it into [2], [3,10,7], and [5,14]. The first subrange has product and LCM equal to 2. The second has product and LCM equal to 210. And the third has product and LCM equal to 70. Another possible partitioning is [2,3], [10,7], and [5,14].
The second query asks about the range (2,4). Its product is equal to its LCM, so you don't need to partition it further.
The last query asks about the range (3,5). You can partition it into [10,7] and [5].
|
import sys
MAXPRIME = 100000
primeFactors = [[] for _ in range(MAXPRIME + 1)]
for i in range(2, MAXPRIME + 1):
if len(primeFactors[i]) == 0:
for j in range(i, MAXPRIME + 1, i):
primeFactors[j].append(i)
def main():
n, q = readIntArr()
arr = readIntArr()
elemPrimes = [inf for _ in range(100001)]
furthestCoprimeIndex = [inf for _ in range(n)]
for i in range(n - 1, -1, -1):
for p in primeFactors[arr[i]]:
furthestCoprimeIndex[i] = min(furthestCoprimeIndex[i], elemPrimes[p] - 1)
elemPrimes[p] = i
furthest2 = furthestCoprimeIndex
currmin = inf
for i in range(n - 1, -1, -1):
furthest2[i] = min(furthest2[i], currmin)
currmin = min(currmin, furthest2[i])
maxPower = 18
dp = [[inf for _ in range(maxPower)] for __ in range(n)]
for i in range(n):
dp[i][0] = furthest2[i]
for j in range(1, maxPower):
for i in range(n):
if dp[i][j - 1] + 1 >= n:
break
else:
dp[i][j] = dp[dp[i][j - 1] + 1][j - 1]
allans = []
for _ in range(q):
l, r = readIntArr()
l -= 1
r -= 1
r2 = l - 1
cnts = 0
while True:
l2 = r2 + 1
breakOuter = False
for j in range(maxPower):
if dp[l2][j] >= r:
if j == 0:
cnts += 1
breakOuter = True
else:
cnts += 1 << j - 1
r2 = dp[l2][j - 1]
break
if breakOuter:
break
allans.append(cnts)
multiLineArrayPrint(allans)
return
input = sys.stdin.buffer.readline
def oneLineArrayPrint(arr):
print(" ".join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
print("\n".join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
print("\n".join([" ".join([str(x) for x in y]) for y in arr]))
def readIntArr():
return [int(x) for x in input().split()]
def makeArr(defaultVal, dimensionArr):
dv = defaultVal
da = dimensionArr
if len(da) == 1:
return [dv for _ in range(da[0])]
else:
return [makeArr(dv, da[1:]) for _ in range(da[0])]
def queryInteractive(x, y):
print("? {} {}".format(x, y))
sys.stdout.flush()
return int(input())
def answerInteractive(ans):
print("! {}".format(ans))
sys.stdout.flush()
inf = float("inf")
MOD = 10**9 + 7
for _abc in range(1):
main()
|
IMPORT ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
|
This time Baby Ehab will only cut and not stick. He starts with a piece of paper with an array a of length n written on it, and then he does the following:
* he picks a range (l, r) and cuts the subsegment a_l, a_{l + 1}, β¦, a_r out, removing the rest of the array.
* he then cuts this range into multiple subranges.
* to add a number theory spice to it, he requires that the elements of every subrange must have their product equal to their [least common multiple (LCM)](https://en.wikipedia.org/wiki/Least_common_multiple).
Formally, he partitions the elements of a_l, a_{l + 1}, β¦, a_r into contiguous subarrays such that the product of every subarray is equal to its LCM. Now, for q independent ranges (l, r), tell Baby Ehab the minimum number of subarrays he needs.
Input
The first line contains 2 integers n and q (1 β€ n,q β€ 10^5) β the length of the array a and the number of queries.
The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β the elements of the array a.
Each of the next q lines contains 2 integers l and r (1 β€ l β€ r β€ n) β the endpoints of this query's interval.
Output
For each query, print its answer on a new line.
Example
Input
6 3
2 3 10 7 5 14
1 6
2 4
3 5
Output
3
1
2
Note
The first query asks about the whole array. You can partition it into [2], [3,10,7], and [5,14]. The first subrange has product and LCM equal to 2. The second has product and LCM equal to 210. And the third has product and LCM equal to 70. Another possible partitioning is [2,3], [10,7], and [5,14].
The second query asks about the range (2,4). Its product is equal to its LCM, so you don't need to partition it further.
The last query asks about the range (3,5). You can partition it into [10,7] and [5].
|
import sys
input = sys.stdin.buffer.readline
spf = [0] * (10**5 + 1)
primes = []
for i in range(2, 10**5 + 1):
if spf[i] == 0:
spf[i] = i
primes.append(i)
j = 0
while j < len(primes) and primes[j] <= spf[i] and primes[j] * i <= 10**5:
spf[primes[j] * i] = primes[j]
j += 1
n, q = map(int, input().split())
a = list(map(int, input().split()))
ancestor = [([n] * 20) for i in range(10**5 + 1)]
next_start = n
last_occurence = [n] * (10**5 + 1)
for i in range(n - 1, -1, -1):
lst = -1
while a[i] != 1:
if lst != spf[a[i]]:
next_start = min(next_start, last_occurence[spf[a[i]]])
last_occurence[spf[a[i]]] = i
lst = spf[a[i]]
a[i] //= spf[a[i]]
ancestor[i][0] = next_start
for j in range(1, 20):
ancestor[i][j] = ancestor[ancestor[i][j - 1]][j - 1]
for i in range(q):
l, r = map(int, input().split())
l -= 1
r -= 1
subarrays = 1
for j in range(19, -1, -1):
if ancestor[l][j] <= r:
l = ancestor[l][j]
subarrays += 1 << j
print(subarrays)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
|
This time Baby Ehab will only cut and not stick. He starts with a piece of paper with an array a of length n written on it, and then he does the following:
* he picks a range (l, r) and cuts the subsegment a_l, a_{l + 1}, β¦, a_r out, removing the rest of the array.
* he then cuts this range into multiple subranges.
* to add a number theory spice to it, he requires that the elements of every subrange must have their product equal to their [least common multiple (LCM)](https://en.wikipedia.org/wiki/Least_common_multiple).
Formally, he partitions the elements of a_l, a_{l + 1}, β¦, a_r into contiguous subarrays such that the product of every subarray is equal to its LCM. Now, for q independent ranges (l, r), tell Baby Ehab the minimum number of subarrays he needs.
Input
The first line contains 2 integers n and q (1 β€ n,q β€ 10^5) β the length of the array a and the number of queries.
The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β the elements of the array a.
Each of the next q lines contains 2 integers l and r (1 β€ l β€ r β€ n) β the endpoints of this query's interval.
Output
For each query, print its answer on a new line.
Example
Input
6 3
2 3 10 7 5 14
1 6
2 4
3 5
Output
3
1
2
Note
The first query asks about the whole array. You can partition it into [2], [3,10,7], and [5,14]. The first subrange has product and LCM equal to 2. The second has product and LCM equal to 210. And the third has product and LCM equal to 70. Another possible partitioning is [2,3], [10,7], and [5,14].
The second query asks about the range (2,4). Its product is equal to its LCM, so you don't need to partition it further.
The last query asks about the range (3,5). You can partition it into [10,7] and [5].
|
import sys
input_ = lambda: sys.stdin.readline().strip("\r\n")
ii = lambda: int(input_())
il = lambda: list(map(int, input_().split()))
ilf = lambda: list(map(float, input_().split()))
ip = lambda: input_()
fi = lambda: float(input_())
ap = lambda ab, bc, cd: ab[bc].append(cd)
li = lambda: list(input_())
pr = lambda x: print(x)
prinT = lambda x: print(x)
f = lambda: sys.stdout.flush()
mod = 10**9 + 7
MX = 10**5 + 1
prime = [[] for i in range(MX)]
for i in range(2, MX):
if prime[i] == []:
for j in range(i, MX, i):
prime[j].append(i)
n, q = il()
a = [0] + il()
nxt = [(n + 1) for _ in range(MX)]
dp = [[(0) for i in range(18)] for j in range(n + 2)]
dp[n + 1][0] = n + 1
for i in range(n, 0, -1):
dp[i][0] = dp[i + 1][0]
for j in prime[a[i]]:
dp[i][0] = min(dp[i][0], nxt[j])
nxt[j] = i
for i in range(n + 1, 0, -1):
for j in range(1, 18):
dp[i][j] = dp[dp[i][j - 1]][j - 1]
for _ in range(q):
l, r = il()
ans = 1
for i in range(17, -1, -1):
if dp[l][i] <= r:
ans += 1 << i
l = dp[l][i]
print(ans)
|
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR LIST FOR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
This time Baby Ehab will only cut and not stick. He starts with a piece of paper with an array a of length n written on it, and then he does the following:
* he picks a range (l, r) and cuts the subsegment a_l, a_{l + 1}, β¦, a_r out, removing the rest of the array.
* he then cuts this range into multiple subranges.
* to add a number theory spice to it, he requires that the elements of every subrange must have their product equal to their [least common multiple (LCM)](https://en.wikipedia.org/wiki/Least_common_multiple).
Formally, he partitions the elements of a_l, a_{l + 1}, β¦, a_r into contiguous subarrays such that the product of every subarray is equal to its LCM. Now, for q independent ranges (l, r), tell Baby Ehab the minimum number of subarrays he needs.
Input
The first line contains 2 integers n and q (1 β€ n,q β€ 10^5) β the length of the array a and the number of queries.
The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β the elements of the array a.
Each of the next q lines contains 2 integers l and r (1 β€ l β€ r β€ n) β the endpoints of this query's interval.
Output
For each query, print its answer on a new line.
Example
Input
6 3
2 3 10 7 5 14
1 6
2 4
3 5
Output
3
1
2
Note
The first query asks about the whole array. You can partition it into [2], [3,10,7], and [5,14]. The first subrange has product and LCM equal to 2. The second has product and LCM equal to 210. And the third has product and LCM equal to 70. Another possible partitioning is [2,3], [10,7], and [5,14].
The second query asks about the range (2,4). Its product is equal to its LCM, so you don't need to partition it further.
The last query asks about the range (3,5). You can partition it into [10,7] and [5].
|
import sys
input = sys.stdin.buffer.readline
def I():
return int(input())
def LI():
return [int(i) for i in input().split()]
def LI_():
return [(int(i) - 1) for i in input().split()]
def decompos(num):
res = []
if num % 2 == 0:
res.append(2)
while num % 2 == 0:
num //= 2
if num == 0:
return []
for i in range(3, int(num**0.5) + 1, 2):
if i > num:
break
if num % i == 0:
res.append(i)
while num % i == 0:
num //= i
if num > 1:
res.append(num)
return res
n, q = LI()
prime_last_appear_at = dict()
dp = [([n] * (n + 1)) for i in range(18)]
arr = LI()
for i in range(n - 1, -1, -1):
dp[0][i] = dp[0][i + 1]
P = decompos(arr[i])
for p in P:
if p in prime_last_appear_at:
dp[0][i] = min(dp[0][i], prime_last_appear_at[p])
prime_last_appear_at[p] = i
for i in range(1, len(dp)):
for j in range(n):
dp[i][j] = dp[i - 1][dp[i - 1][j]]
for _ in range(q):
l, r = LI_()
cur = l
res = 1
for jump in range(len(dp) - 1, -1, -1):
if dp[jump][cur] <= r:
res += 1 << jump
cur = dp[jump][cur]
print(res)
|
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER RETURN LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
This time Baby Ehab will only cut and not stick. He starts with a piece of paper with an array a of length n written on it, and then he does the following:
* he picks a range (l, r) and cuts the subsegment a_l, a_{l + 1}, β¦, a_r out, removing the rest of the array.
* he then cuts this range into multiple subranges.
* to add a number theory spice to it, he requires that the elements of every subrange must have their product equal to their [least common multiple (LCM)](https://en.wikipedia.org/wiki/Least_common_multiple).
Formally, he partitions the elements of a_l, a_{l + 1}, β¦, a_r into contiguous subarrays such that the product of every subarray is equal to its LCM. Now, for q independent ranges (l, r), tell Baby Ehab the minimum number of subarrays he needs.
Input
The first line contains 2 integers n and q (1 β€ n,q β€ 10^5) β the length of the array a and the number of queries.
The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β the elements of the array a.
Each of the next q lines contains 2 integers l and r (1 β€ l β€ r β€ n) β the endpoints of this query's interval.
Output
For each query, print its answer on a new line.
Example
Input
6 3
2 3 10 7 5 14
1 6
2 4
3 5
Output
3
1
2
Note
The first query asks about the whole array. You can partition it into [2], [3,10,7], and [5,14]. The first subrange has product and LCM equal to 2. The second has product and LCM equal to 210. And the third has product and LCM equal to 70. Another possible partitioning is [2,3], [10,7], and [5,14].
The second query asks about the range (2,4). Its product is equal to its LCM, so you don't need to partition it further.
The last query asks about the range (3,5). You can partition it into [10,7] and [5].
|
import sys
input = sys.stdin.readline
def main():
N, Q = map(int, input().split())
A = list(map(int, input().split()))
MX = int(100000.0)
primeFactors = [[] for _ in range(MX + 1)]
for i in range(2, MX + 1):
if len(primeFactors[i]) > 0:
continue
for j in range(i, MX + 1, i):
primeFactors[j].append(i)
MXH = 18
nxt = [N for _ in range(MX + 1)]
dp = [[N for _ in range(N + 1)] for __ in range(MXH + 1)]
for i in range(N - 1, -1, -1):
dp[0][i] = dp[0][i + 1]
for factor in primeFactors[A[i]]:
dp[0][i] = min(dp[0][i], nxt[factor])
nxt[factor] = i
for i in range(1, MXH + 1):
for j in range(N):
dp[i][j] = dp[i - 1][dp[i - 1][j]]
for _ in range(Q):
l, r = map(int, input().split())
l -= 1
r -= 1
cnt = 1
for i in range(MXH, -1, -1):
if dp[i][l] <= r:
cnt += 1 << i
l = dp[i][l]
sys.stdout.write(f"{cnt}\n")
main()
|
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
|
This time Baby Ehab will only cut and not stick. He starts with a piece of paper with an array a of length n written on it, and then he does the following:
* he picks a range (l, r) and cuts the subsegment a_l, a_{l + 1}, β¦, a_r out, removing the rest of the array.
* he then cuts this range into multiple subranges.
* to add a number theory spice to it, he requires that the elements of every subrange must have their product equal to their [least common multiple (LCM)](https://en.wikipedia.org/wiki/Least_common_multiple).
Formally, he partitions the elements of a_l, a_{l + 1}, β¦, a_r into contiguous subarrays such that the product of every subarray is equal to its LCM. Now, for q independent ranges (l, r), tell Baby Ehab the minimum number of subarrays he needs.
Input
The first line contains 2 integers n and q (1 β€ n,q β€ 10^5) β the length of the array a and the number of queries.
The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 10^5) β the elements of the array a.
Each of the next q lines contains 2 integers l and r (1 β€ l β€ r β€ n) β the endpoints of this query's interval.
Output
For each query, print its answer on a new line.
Example
Input
6 3
2 3 10 7 5 14
1 6
2 4
3 5
Output
3
1
2
Note
The first query asks about the whole array. You can partition it into [2], [3,10,7], and [5,14]. The first subrange has product and LCM equal to 2. The second has product and LCM equal to 210. And the third has product and LCM equal to 70. Another possible partitioning is [2,3], [10,7], and [5,14].
The second query asks about the range (2,4). Its product is equal to its LCM, so you don't need to partition it further.
The last query asks about the range (3,5). You can partition it into [10,7] and [5].
|
import sys
def I():
return int(sys.stdin.readline().rstrip())
def MI():
return map(int, sys.stdin.readline().rstrip().split())
def LI():
return list(map(int, sys.stdin.readline().rstrip().split()))
def LI2():
return list(map(int, sys.stdin.readline().rstrip()))
def S():
return sys.stdin.readline().rstrip()
def LS():
return list(sys.stdin.readline().rstrip().split())
def LS2():
return list(sys.stdin.readline().rstrip())
n, q = MI()
A = LI()
prime_fac = [[] for _ in range(10**5 + 1)]
nex = [0] * (10**5 + 1)
for i in range(2, 10**5 + 1):
if not prime_fac[i]:
nex[i] = n
for j in range(i, 10**5 + 1, i):
prime_fac[j].append(i)
dp = [([0] * 18) for _ in range(n + 1)]
dp[n][0] = n
for i in range(n - 1, -1, -1):
a = A[i]
dp[i][0] = dp[i + 1][0]
for p in prime_fac[a]:
dp[i][0] = min(dp[i][0], nex[p])
nex[p] = i
for j in range(1, 18):
for i in range(n + 1):
dp[i][j] = dp[dp[i][j - 1]][j - 1]
for _ in range(q):
l, r = MI()
l -= 1
r -= 1
ans = 1
for j in range(17, -1, -1):
if dp[l][j] <= r:
ans += 1 << j
l = dp[l][j]
print(ans)
|
IMPORT FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
from itertools import groupby, repeat
def line():
return map(int, input().split())
def num():
return int(input())
def nfunc(f, n, *args, **kwargs):
return (f(*args, **kwargs) for _ in repeat(None, n))
def byw(q):
return q[1][0]
def byh(q):
return q[1][1]
t = num()
for _ in repeat(None, t):
n = num()
frs = [(min(q, w), max(q, w)) for q, w in nfunc(line, n)]
wss = [list(q) for _, q in groupby(sorted(enumerate(frs), key=byw), key=byw)]
hmins = [wss[0][0][0]]
wmap = [0] * n
for j, q in enumerate(wss):
for i, (w, h) in q:
wmap[i] = j
i, (w, h) = min(q, key=byh)
if h < frs[hmins[-1]][1]:
hmins.append(i)
else:
hmins.append(hmins[-1])
hmins = hmins[1:]
ans = []
for i in range(n):
j = wmap[i]
w, h = frs[i]
if j and h > frs[hmins[j - 1]][1]:
ans.append(hmins[j - 1] + 1)
else:
ans.append(-1)
print(" ".join(map(str, ans)))
|
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NONE VAR FUNC_DEF RETURN VAR NUMBER NUMBER FUNC_DEF RETURN VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NONE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
import itertools
import sys
class RMQ:
def __init__(self, n):
self.sz = 1
self.inf = (1 << 40) - 1
while self.sz <= n:
self.sz = self.sz << 1
self.dat = [self.inf] * (2 * self.sz - 1)
def update(self, idx, x):
idx += self.sz - 1
self.dat[idx] = x
while idx > 0:
idx = idx - 1 >> 1
self.dat[idx] = min(self.dat[idx * 2 + 1], self.dat[idx * 2 + 2])
def query(self, a, b):
return self.query_help(a, b, 0, 0, self.sz)
def query_help(self, a, b, k, l, r):
if r <= a or b <= l:
return self.inf
elif a <= l and r <= b:
return self.dat[k]
else:
return min(
self.query_help(a, b, 2 * k + 1, l, l + r >> 1),
self.query_help(a, b, 2 * k + 2, l + r >> 1, r),
)
msk = (1 << 20) - 1
def solve(a, b, n):
rmq = RMQ(2 * n)
for i in range(n):
aa = a[i]
aaa = (a[i] << 20) + i
bb = b[i]
bbb = (b[i] << 20) + i
if bbb < rmq.query(aa, aa + 1):
rmq.update(aa, bbb)
if aaa < rmq.query(bb, bb + 1):
rmq.update(bb, aaa)
ans = []
for i in range(n):
rmm = rmq.query(0, b[i])
if rmm >> 20 < a[i]:
ans.append((rmm & msk) + 1)
else:
ans.append(-1)
sys.stdout.write(" ".join(map(str, ans)) + "\n")
for t in range(int(sys.stdin.readline())):
n = int(sys.stdin.readline())
d = [[int(i) for i in sys.stdin.readline().split()] for j in range(n)]
d_ = [d[i][0] for i in range(n)] + [d[i][1] for i in range(n)]
d_.sort()
d__ = {}
for i in range(2 * n):
d__[d_[i]] = i
dda = [d__[d[i][0]] for i in range(n)]
ddb = [d__[d[i][1]] for i in range(n)]
solve(dda, ddb, n)
|
IMPORT IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR FUNC_DEF IF VAR VAR VAR VAR RETURN VAR IF VAR VAR VAR VAR RETURN VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
t = int(input())
for i in range(t):
n = int(input())
a = []
ans = [(-1) for i in range(n)]
for i in range(n):
h, w = map(int, input().split())
if w > h:
h, w = w, h
a.append((h, w, i))
a.sort(key=lambda x: x[1], reverse=True)
a.sort(key=lambda x: x[0])
prei = -1
prew = 10**10
for h, w, i in a:
if prew < w:
ans[i] = prei + 1
else:
prei = i
prew = w
print(*ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
import sys
input = sys.stdin.readline
def find_num(t, arr, num, find_index):
low = 0
high = len(arr) - 1
while low < high:
mid = (low + high) // 2
if arr[mid][0] >= t[0]:
high = mid - 1
else:
low = mid + 1
if arr[low][0] >= t[0]:
if low > 0 and num[low - 1][0] < t[1]:
return find_index[tuple(arr[num[low - 1][1]])]
return -1
else:
if num[low][0] < t[1]:
return find_index[tuple(arr[num[low][1]])]
return -1
for nt in range(int(input())):
n = int(input())
a = []
d1, d2 = {}, {}
c1, c2 = [], []
for i in range(n):
a.append(list(map(int, input().split())))
d1[tuple(a[-1])] = i + 1
b = sorted(a)
c1.append([b[0][1], 0])
for i in range(1, n):
if b[i][1] < c1[-1][0]:
c1.append([b[i][1], i])
else:
c1.append([c1[-1][0], c1[-1][1]])
c = []
for i in range(n):
c.append([a[i][1], a[i][0]])
d2[tuple(c[-1])] = i + 1
c.sort()
c2.append([c[0][1], 0])
for i in range(1, n):
if c[i][1] < c2[-1][0]:
c2.append([c[i][1], i])
else:
c2.append([c2[-1][0], c2[-1][1]])
ans = []
for i in a:
x = find_num(i, b, c1, d1)
if x == -1:
y = find_num(i, c, c2, d2)
ans.append(y)
else:
ans.append(x)
print(*ans)
|
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER IF VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR DICT DICT ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR EXPR FUNC_CALL VAR LIST VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR EXPR FUNC_CALL VAR LIST VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
def solve():
n = int(input())
data = []
kek = dict()
for i in range(n):
a, b = map(int, input().split())
data.append((max(a, b), min(a, b), i))
data_1 = sorted(data, key=lambda x: x[0])
key = 0
kek[0] = [1000000001, -1]
for person in data_1:
a, b, i = person
value = kek.get(a)
if not value:
kek[a] = kek[key][:]
kek[a - 1] = kek[key][:]
key = a
value = kek.get(a)
if value[0] > b:
kek[a] = [b, i]
for lol in data:
a, b, i = lol
value, index = kek.get(max(a, b) - 1)
if value < min(a, b):
print(index + 1, end=" ")
else:
print(-1, end=" ")
print()
for _ in range(int(input())):
solve()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR LIST VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
t = int(input())
while t:
n = int(input())
arr = []
for i in range(n):
a, b = list(map(int, input().split()))
if a > b:
a, b = b, a
arr.append((a, -b, i))
arr.append((b, -1, i))
ans = [(-1) for i in range(n)]
a = -1
min_height = float("inf")
element = -1
arr.sort()
for w, h, i in arr:
h = -h
if min_height < h:
ans[i] = element + 1
elif h < min_height:
min_height = h
element = i
print(*ans)
t -= 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
t = int(input())
for _ in range(t):
n = int(input())
hw = [0] * n
for i in range(n):
h, w = map(int, input().split())
if h > w:
h, w = w, h
hw[i] = [i, h, w]
hw.sort(key=lambda x: x[2], reverse=True)
hw.sort(key=lambda x: x[1])
prev = -1, 10**10
ans = [-1] * n
for i, h, w in hw:
if prev[1] < w:
ans[i] = prev[0] + 1
else:
prev = i, w
print(*ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
MOD = 1000000007
def si():
return input()
def ii():
return int(input())
def li():
return list(map(int, input().split()))
def mi():
return map(int, input().split())
def sout(v):
print(v, end=" ")
def d2b(n):
return bin(n).replace("0b", "")
def twod(n, m, num):
return [[num for x in range(m)] for y in range(n)]
def vow():
return ["a", "e", "i", "o", "u"]
def let():
return [chr(i) for i in range(97, 123)]
def gcd(x, y):
while y:
x, y = y, x % y
return x
def ispow2(x):
return x and not x & x - 1
def prime_factors(n):
i = 2
factors = []
while i * i <= n:
if n % i:
i += 1
else:
n //= i
factors.append(i)
if n > 1:
factors.append(n)
return list(factors)
t = ii()
while t:
t -= 1
n = ii()
a = []
for i in range(n):
h, w = mi()
a.append([-h, w, i])
a.append([-w, h, i])
w = sorted(a, key=lambda x: (x[1], x[0]))
n *= 2
for i in range(n):
w[i][0] = abs(w[i][0])
i = 0
hmin = 999999999999
ans = [(-1) for _ in range(n // 2)]
while i < n:
j = w[i][2]
if w[i][0] < hmin:
hmin = w[i][0]
id = w[i][2]
elif w[i][0] > hmin:
ans[w[i][2]] = id + 1
i += 1
print(" ".join(str(i) for i in ans))
|
ASSIGN VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR VAR STRING STRING FUNC_DEF RETURN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN LIST STRING STRING STRING STRING STRING FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER FUNC_DEF WHILE VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF RETURN VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
tc = int(input())
for _ in range(tc):
n = int(input())
fr = []
for i in range(n):
h, w = list(map(int, input().split()))
fr.append([min(h, w), max(h, w), i + 1, -1])
fr.sort(key=lambda x: -x[1])
fr.sort(key=lambda x: -x[0])
smallW = {}
prev = float("inf"), -1
for i in reversed(range(n)):
if smallW.get(fr[i][0]) is None:
smallW[fr[i][0]] = prev
if prev[0] > fr[i][1]:
prev = fr[i][1], fr[i][2]
for i in range(n):
if smallW[fr[i][0]][0] < fr[i][1]:
fr[i][3] = smallW[fr[i][0]][1]
ret = []
fr.sort(key=lambda x: x[2])
for i in range(n):
ret.append(str(fr[i][3]))
print(" ".join(ret))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR STRING NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER NONE ASSIGN VAR VAR VAR NUMBER VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
t = int(input())
for i in range(t):
n = int(input())
arr = []
lst = [-1] * (n + 1)
for j in range(n):
h, w = map(int, input().split(" "))
arr.append([min(h, w), max(h, w), j + 1])
arr.sort()
index = 0
mnwt = arr[0][1]
index2 = 0
mnwt2 = arr[0][1]
for i in range(1, n):
if arr[i][0] > arr[index2][0]:
index = index2
mnwt = mnwt2
if arr[index][0] < arr[i][0] and mnwt < arr[i][1]:
lst[arr[i][2]] = arr[index][2]
elif arr[index2][0] < arr[i][0] and mnwt2 < arr[i][1]:
lst[arr[i][2]] = arr[index2][2]
if arr[i][1] < mnwt2:
index2 = i
mnwt2 = arr[i][1]
for i in range(1, n + 1):
if i == n:
print(lst[i])
else:
print(lst[i], end=" ")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
for T in range(int(input())):
n = int(input())
res = ["-1" for i in range(n)]
images = []
for idx in range(n):
w, h = map(int, input().split())
if w < h:
w, h = h, w
images.append((w, h, idx))
images.sort(key=lambda x: (x[0], x[1]), reverse=True)
hmin = []
hmin.append((images[-1][1], images[-1][2], images[-1][0]))
for i in range(n - 2, -1, -1):
if images[i + 1][1] < hmin[-1][0]:
hmin.append((images[i + 1][1], images[i + 1][2], images[i + 1][0]))
else:
continue
for i in range(n):
while hmin and images[i][0] <= hmin[-1][2]:
hmin.pop()
if not hmin:
break
if images[i][1] > hmin[-1][0]:
res[images[i][2]] = str(hmin[-1][1] + 1)
print(" ".join(res))
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR IF VAR IF VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
def read_int():
return int(input())
def read_ints():
return map(int, input().split(" "))
t = read_int()
for case_num in range(t):
n = read_int()
people = []
for i in range(n):
h, w = read_ints()
people.append((h, w, i))
people.append((w, h, i))
ans = [-1] * n
people.sort()
lo = int(10000000000.0)
idx = -1
l = -1
for h, w, i in people:
while l + 1 < n and people[l + 1][0] < h:
l += 1
if people[l][1] < lo:
lo = people[l][1]
idx = people[l][2]
if lo < w:
ans[i] = idx + 1
print(" ".join(map(str, ans)))
|
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR WHILE BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
import sys
zz = 1
sys.setrecursionlimit(10**5)
if zz:
input = sys.stdin.readline
else:
sys.stdin = open("input.txt", "r")
sys.stdout = open("all.txt", "w")
di = [[-1, 0], [1, 0], [0, 1], [0, -1]]
def fori(n):
return [fi() for i in range(n)]
def inc(d, c, x=1):
d[c] = d[c] + x if c in d else x
def ii():
return input().rstrip()
def li():
return [int(xx) for xx in input().split()]
def fli():
return [float(x) for x in input().split()]
def comp(a, b):
if a > b:
return 2
return 2 if a == b else 0
def gi():
return [xx for xx in input().split()]
def gtc(tc, ans):
print("Case #" + str(tc) + ":", ans)
def cil(n, m):
return n // m + int(n % m > 0)
def fi():
return int(input())
def pro(a):
return reduce(lambda a, b: a * b, a)
def swap(a, i, j):
a[i], a[j] = a[j], a[i]
def si():
return list(input().rstrip())
def mi():
return map(int, input().split())
def gh():
sys.stdout.flush()
def isvalid(i, j, n, m):
return 0 <= i < n and 0 <= j < m
def bo(i):
return ord(i) - ord("a")
def graph(n, m):
for i in range(m):
x, y = mi()
a[x].append(y)
a[y].append(x)
t = fi()
uu = t
while t > 0:
t -= 1
n = fi()
d = {}
a = []
for i in range(n):
h, w = mi()
a.append([min(h, w), max(h, w)])
if min(h, w) not in d:
d[min(h, w)] = []
d[min(h, w)].append([max(h, w), i])
r = []
for i in d:
d[i].sort()
r.append(i)
ans = []
r.sort()
p = []
for i in range(len(r)):
if len(p):
if p[-1][0] > d[r[i]][0][0]:
p.append(d[r[i]][0])
else:
p.append(p[-1])
else:
p.append(d[r[i]][0])
ans = []
for i in range(n):
l = anss = 0
if a[i][0] <= r[0]:
ans.append(-1)
continue
rr = len(r) - 1
while l <= rr:
mid = (l + rr) // 2
if r[mid] >= a[i][0]:
rr = mid - 1
else:
anss = mid
l = mid + 1
if p[anss][0] < a[i][1]:
ans.append(p[anss][1] + 1)
else:
ans.append(-1)
print(*ans)
|
IMPORT ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN NUMBER RETURN VAR VAR NUMBER NUMBER FUNC_DEF RETURN VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR VAR STRING VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_DEF RETURN NUMBER VAR VAR NUMBER VAR VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR LIST FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR IF VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
test = int(input())
for faoseufn in range(test):
n = int(input())
l = []
for i in range(n):
a, b = list(map(int, input().split()))
if a < b:
a, b = b, a
l.append((a, b, i + 1))
l.sort()
ans = [-1] * n
w = {}
ind = {}
w[l[0][0]] = l[0][1]
ind[l[0][0]] = l[0][2]
low = [l[0]]
for i in range(1, n):
if l[i - 1][0] < l[i][0]:
if l[i][1] < low[-1][1]:
low.append(l[i])
ptr = 0
mptr = len(low) - 1
for i in range(1, n):
if low[0][0] == l[i][0]:
continue
elif low[-1][1] >= l[i][1]:
continue
while ptr < mptr and l[i][0] > low[ptr + 1][0]:
ptr += 1
if low[ptr][0] < l[i][0] and low[ptr][1] < l[i][1]:
ans[l[i][2] - 1] = low[ptr][2]
print(*ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER NUMBER VAR VAR NUMBER IF VAR NUMBER NUMBER VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
for i in range(int(input())):
n = int(input())
a = []
arr = [-1]
for j in range(n):
h, w = map(int, input().split())
if h < w:
h, w = w, h
a.append([h, w, j + 1])
arr.append(-1)
a.sort()
width = 1000000000
a.append(a[-1])
j = 0
pos = -1
position = -1
while j < n:
w = 1000000000
while j < n and a[j][0] == a[j + 1][0]:
if a[j][1] > width:
arr[a[j][2]] = position
if a[j][1] < w:
w = a[j][1]
pos = a[j][2]
j += 1
else:
if a[j][1] > width:
arr[a[j][2]] = position
if w > a[j][1]:
w = a[j][1]
pos = a[j][2]
j += 1
if width > w:
width = w
position = pos
print(*arr[1:])
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
for z in range(int(input())):
n = int(input())
l = []
ans = [(-1) for i in range(n)]
for i in range(n):
a, b = map(int, input().split())
l.append((a, 1, b, i))
l.append((a, 0, b, i))
l.append((b, 0, a, i))
l.sort()
stack = []
for i in l:
if i[1] == 1:
if len(stack) == 0 or stack[-1][0] > i[2]:
stack.append((i[2], i[3]))
elif stack and stack[-1][0] < i[2]:
ans[i[3]] = stack[-1][1] + 1
print(*ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
def corrPlace(allPoint):
n = len(allPoint)
ans = [-1] * n
allPoint.sort(reverse=True)
message = [allPoint[-1]]
for i in range(n - 2, -1, -1):
if message[-1][0] != allPoint[i][0] and message[-1][1] > allPoint[i][1]:
message.append(allPoint[i])
cachePlace = len(message) - 1
for imess in allPoint:
ai = imess[0]
bi = imess[1]
while cachePlace != 0 and message[cachePlace][0] >= ai:
cachePlace -= 1
if message[cachePlace][0] < ai and message[cachePlace][1] < bi:
ans[imess[2] - 1] = message[cachePlace][2]
return ans
t = int(input())
for qwq in range(t):
n = int(input())
allPoint = []
for i in range(n):
l = list(map(int, input().split()))
allPoint.append((max(l), min(l), i + 1))
ans = corrPlace(allPoint)
for i in ans:
print(i, end=" ")
print()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
for _ in range(int(input())):
n = int(input())
ans = [(-1) for i in range(n)]
p, q = [], []
for i in range(n):
h, w = map(int, input().split())
p.append((h, w, i))
q.append((w, h, i))
p.sort(key=lambda x: x[0])
q.sort(key=lambda x: x[0])
bstp, bstq = (0, -1), (0, -1)
j, k = 0, 0
for i in range(n):
while j < n and p[j][0] < p[i][0]:
if bstp[1] == -1 or bstp[0] > p[j][1]:
bstp = p[j][1], p[j][2]
j += 1
while k < n and q[k][0] < p[i][0]:
if bstq[1] == -1 or bstq[0] > q[k][1]:
bstq = q[k][1], q[k][2]
k += 1
if bstp[1] != -1 and bstp[0] < p[i][1]:
ans[p[i][2]] = bstp[1] + 1
if bstq[1] != -1 and bstq[0] < p[i][1]:
ans[p[i][2]] = bstq[1] + 1
for i in range(n):
print(ans[i], end=" ")
print()
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
t = int(input())
for _ in range(t):
n = int(input())
lst = []
for times in range(n):
w, h = map(int, input().split())
if w > h:
lst.append([h, w, times])
else:
lst.append([w, h, times])
lst = sorted(lst, key=lambda x: (x[0], -x[1]))
minn = lst[0][1]
so = lst[0][2]
t = 0
ans = [-1] * n
for i in lst:
if i[1] <= minn:
minn = min(minn, i[1])
so = i[2] + 1
else:
ans[i[2]] = so
for i in ans:
print(i)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR IF VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
mxn = 10**10
for i in range(int(input())):
n = int(input())
ls = []
lsr = []
res = [-1] * (n + 1)
for i in range(n):
a, b = map(int, input().split())
ls.append([a, b, i + 1])
lsr.append([b, a, i + 1])
ls.sort()
lsr.sort()
mini = [0] * n
mini[0] = mxn
minir = [0] * n
minir[0] = mxn
mn = mxn
idx = -1
mnr = mxn
idxr = -1
for i in range(n):
if mn > ls[i][1]:
mn = ls[i][1]
idx = ls[i][2]
mini[i] = [mn, idx]
if mnr > lsr[i][1]:
mnr = lsr[i][1]
idxr = lsr[i][2]
minir[i] = [mnr, idxr]
for i in range(n):
lo = 0
hi = n - 1
idx = -1
while lo <= hi:
mid = lo + (hi - lo) // 2
if ls[mid][0] < ls[i][0]:
idx = mid
lo = mid + 1
else:
hi = mid - 1
if idx != -1 and ls[i][1] > mini[idx][0]:
res[ls[i][2]] = mini[idx][1]
else:
lo = 0
hi = n - 1
idx = -1
while lo <= hi:
mid = lo + (hi - lo) // 2
if lsr[mid][0] < ls[i][0]:
idx = mid
lo = mid + 1
else:
hi = mid - 1
if idx != -1:
if ls[i][1] > minir[idx][0]:
res[ls[i][2]] = minir[idx][1]
print(*res[1:])
|
ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR LIST VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR LIST VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
import sys
def correctPlacement(n, fren):
fren.sort()
ans = ["-1"] * n
j = 0
k = -1
for i in range(n):
while fren[j][0] != fren[i][0]:
if k == -1 or fren[j][1] < fren[k][1]:
k = j
j += 1
if k != -1 and fren[k][1] < fren[i][1]:
ans[fren[i][2] - 1] = str(fren[k][2])
print(" ".join(ans))
def main():
for t in range(int(input())):
n = int(input())
fren = []
for i in range(1, n + 1):
h, w = map(int, input().split())
if h > w:
fren.append([h, w, i])
else:
fren.append([w, h, i])
correctPlacement(n, fren)
main()
|
IMPORT FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
t = int(input())
for q in range(t):
n = int(input())
I = []
for i in range(n):
ch = input()
L = [int(i) for i in ch.split()]
I.append((min(L[0], L[1]), max(L[0], L[1]), i + 1))
I.sort()
curr = [I[0]]
T = [-1] * n
for i in range(1, n):
if I[i][0] > I[i - 1][0] and I[i][1] < curr[-1][1]:
curr.append(I[i])
if I[i][0] > curr[-1][0] and I[i][1] > curr[-1][1]:
T[I[i][2] - 1] = curr[-1][2]
if len(curr) >= 2 and I[i][0] > curr[-2][0] and I[i][1] > curr[-2][1]:
T[I[i][2] - 1] = curr[-2][2]
ch = ""
for i in T:
ch += str(i) + " "
print(ch)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
t = int(input())
for _ in range(t):
n = int(input())
dimensions = []
for i in range(n):
h, w = map(int, input().split())
dimensions.append((i, min(h, w), max(h, w)))
dimensions = sorted(dimensions, key=lambda item: item[2], reverse=True)
dimensions = sorted(dimensions, key=lambda item: item[1], reverse=False)
ans = [-1] * n
min_b = dimensions[0][2]
index = 0
for i, l, b in dimensions:
if min_b < b:
ans[i] = index + 1
else:
index = i
min_b = b
print(*ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
for _ in range(int(input())):
n = int(input())
a = [[0, 0, 0] for _ in range(n)]
for i, x in enumerate(a):
x[0], x[1] = map(int, input().split())
if x[0] > x[1]:
x[0], x[1] = x[1], x[0]
x[2] = i
a.sort()
ans = [-1] * n
min_w = -1
i, j = 0, 0
while i < n:
while j < n and a[i][0] == a[j][0]:
j += 1
if min_w >= 0:
for k in range(i, j):
if a[min_w][1] < a[k][1]:
ans[a[k][2]] = a[min_w][2] + 1
for k in range(i, j):
if min_w < 0 or a[min_w][1] > a[k][1]:
min_w = k
i = j
print(*ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
for _ in range(int(input())):
n = int(input())
arr = []
for i in range(n):
h, w = map(int, input().split())
arr.append((h, -w, i))
arr.append((w, -h, i))
arr.sort()
min_height = float("inf")
element = -1
ans = [-1] * n
for w, h, i in arr:
h = -h
if min_height < h:
ans[i] = element
elif min_height > h:
min_height = h
element = i + 1
print(*ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
rn = lambda: int(input())
rl = lambda: list(map(int, input().split()))
rns = lambda: map(int, input().split())
rs = lambda: input()
yn = lambda x: print("Yes") if x else print("No")
YN = lambda x: print("YES") if x else print("NO")
for _ in range(rn()):
n = rn()
p = []
for i in range(n):
p.append(sorted(rl()) + [i])
p.sort(key=lambda x: x[1], reverse=True)
p.sort(key=lambda x: x[0])
ans = [-1] * n
index = -1
m = float("inf")
for i in range(n):
if m < p[i][1]:
ans[p[i][2]] = index + 1
else:
index = p[i][2]
m = p[i][1]
print(*ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR LIST VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
for _ in range(int(input())):
n = int(input())
frds = [(sorted(map(int, input().split(" "))) + [i]) for i in range(n)]
overlap = [-1] * n
frds = sorted(frds, key=lambda x: x[1], reverse=True)
frds = sorted(frds, key=lambda x: x[0])
minh, minw, minfr = frds[0]
notsame_frds = []
for hi, wi, fri in frds:
if wi > minw:
notsame_frds.append((hi, wi, fri))
overlap[fri] = minfr + 1
else:
notsame_frds.append((wi, hi, fri))
minw = wi
minfr = fri
notsame_frds = sorted(notsame_frds, key=lambda x: x[1], reverse=True)
notsame_frds = sorted(notsame_frds, key=lambda x: x[0])
minh, minw, minfr = notsame_frds[0]
for hi, wi, fri in notsame_frds:
if overlap[fri] != -1:
continue
if wi > minw:
overlap[fri] = minfr + 1
else:
minw = wi
minfr = fri
print(*overlap)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING LIST VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
t = int(input())
big = 2 * 10**9
for i in range(0, t):
n = int(input())
ans = [-1] * n
pairs = []
for j in range(0, n):
nextStr = list(map(int, input().split(" ")))
nextStr.append(j)
pairs.append(nextStr)
pairs.sort()
k = 0
mn = [0] * n
ps = [0] * n
for j in range(0, n):
if j == 0 or pairs[j][1] < mn[j - 1]:
mn[j] = pairs[j][1]
ps[j] = pairs[j][2]
else:
mn[j] = mn[j - 1]
ps[j] = ps[j - 1]
while k < n and pairs[k][0] < pairs[j][0]:
k += 1
if not k == 0 and pairs[j][1] > mn[k - 1]:
ans[pairs[j][2]] = ps[k - 1] + 1
for j in range(0, n):
if not ans[pairs[j][2]] == -1:
continue
l = 0
r = n - 1
while l < r:
d = l + r + 1 >> 1
if pairs[d][0] >= pairs[j][1]:
r = d - 1
else:
l = d
if pairs[j][1] <= pairs[l][0]:
continue
if pairs[j][0] > mn[l]:
ans[pairs[j][2]] = ps[l] + 1
print(*ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
import sys
for _ in range(int(input())):
n = int(input())
a = []
for i in range(n):
h, w = map(int, input().split())
if w > h:
h, w = w, h
a.append((i, h, w))
a.sort(key=lambda x: (x[1], -x[2]))
ans = [-1] * n
maxi = sys.maxsize
index = -1
for i, h, w in a:
if w > maxi:
ans[i] = index
if maxi > w:
maxi, index = w, i + 1
print(*ans)
|
IMPORT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
def binary_search(arr, item):
l = 0
r = len(arr) - 1
while r - l > -1:
middle = (l + r) // 2
key = arr[middle]
if key == item:
return middle
elif (key[0], -key[1], key[2]) < (item[0], -item[1], item[2]):
l = middle + 1
else:
r = middle - 1
print(arr, item, middle)
return None
t = int(input())
for it in range(t):
n = int(input())
friends = []
for i in range(n):
a, b = tuple(map(int, input().split()))
if b > a:
a, b = b, a
friend = a, b, i + 1
friends.append(friend)
sorted_friends = sorted(friends, key=lambda x: (x[0], -x[1], x[2]))
min_friend_suffix = [sorted_friends[0]]
for i in range(len(sorted_friends) - 1):
smallest = min_friend_suffix[-1]
friend = sorted_friends[i]
next_friend = sorted_friends[i + 1]
if friend[0] < next_friend[0] and friend[1] < smallest[1]:
min_friend_suffix.append(friend)
else:
min_friend_suffix.append(smallest)
for friend in friends:
index = binary_search(sorted_friends, friend)
candidate = min_friend_suffix[index]
if candidate[0] < friend[0] and candidate[1] < friend[1]:
print(candidate[2], end=" ")
else:
print(-1, end=" ")
print()
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR RETURN VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
t = int(input())
for __ in range(t):
n = int(input())
arr = []
for i in range(n):
h, w = map(int, input().split(" "))
if h > w:
h, w = w, h
arr.append((h, w, i))
arr.sort()
new_list = []
new_sub_list = []
for i in range(n):
if not new_sub_list or arr[i][0] == new_sub_list[-1][0]:
new_sub_list.append(arr[i])
else:
new_list.append(new_sub_list)
new_sub_list = [arr[i]]
if new_sub_list:
new_list.append(new_sub_list)
res = [-1] * n
wmin = new_list[0][0][1]
widx = new_list[0][0][2]
for i in range(1, len(new_list)):
for h, w, idx in new_list[i]:
if w > wmin:
res[idx] = widx + 1
if wmin > new_list[i][0][1]:
wmin = new_list[i][0][1]
widx = new_list[i][0][2]
print(" ".join(str(val) for val in res))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
import sys
sys.setrecursionlimit(10**5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II():
return int(sys.stdin.buffer.readline())
def MI():
return map(int, sys.stdin.buffer.readline().split())
def LI():
return list(map(int, sys.stdin.buffer.readline().split()))
def LLI(rows_number):
return [LI() for _ in range(rows_number)]
def BI():
return sys.stdin.buffer.readline().rstrip()
def SI():
return sys.stdin.buffer.readline().rstrip().decode()
inf = 10**16
md = 10**9 + 7
for _ in range(II()):
hw = []
n = II()
for i in range(n):
h, w = MI()
if h > w:
h, w = w, h
hw.append((h, w, i))
hw.sort()
ci = pi = pmn = mn = inf
ph = -1
ans = [-1] * n
for h, w, i in hw:
if h != ph:
pmn = mn
pi = ci
ph = h
if w > pmn:
ans[i] = pi + 1
if w < mn:
mn = w
ci = i
print(*ans)
|
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
import sys
data = sys.stdin.read().split("\n")[::-1]
def input():
return data.pop()
res = []
def solve(hs, ws, n):
friends = sorted(range(n), key=lambda i: (hs[i], ws[i]))
ans = [-1] * n
thinnest_shorter_than_current = ws.index(max(ws))
min_height = min(hs)
thinnest = thinnest_shorter_than_current
for i, f in enumerate(friends):
f_prev = friends[i - 1]
if i == 0 or hs[f_prev] != hs[f]:
if ws[thinnest_shorter_than_current] > ws[thinnest]:
thinnest_shorter_than_current = thinnest
thinnest = f
if ws[thinnest] > ws[f]:
f = thinnest
if hs[f] == min_height:
continue
if ws[f] > ws[thinnest_shorter_than_current]:
ans[f] = thinnest_shorter_than_current + 1
return ans
for _ in range(int(input())):
n = int(input())
hs = [-1] * n
ws = [-1] * n
for i in range(n):
hs[i], ws[i] = map(int, input().split())
if hs[i] > ws[i]:
hs[i], ws[i] = ws[i], hs[i]
res.append(" ".join(map(str, solve(hs, ws, n))))
print("\n".join(res))
|
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING NUMBER FUNC_DEF RETURN FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
cases = int(input())
for _ in range(cases):
n = int(input())
arr = []
for i in range(n):
a, b = [int(i) for i in input().strip().split()]
arr.append((a, b, i))
arr.append((b, a, i))
arr.sort()
res = {}
def solve(arr):
n = len(arr)
lo = 0
idx = 0
while idx < n:
first = idx
aa, bb, ii = arr[lo]
while idx < n and arr[idx][0] == arr[first][0]:
a, b, i = arr[idx]
if a > aa and b > bb or a > bb and b > aa:
res[i] = str(ii + 1)
idx += 1
if arr[first][1] < arr[lo][1]:
lo = first
solve(arr)
print(" ".join([(res[i] if i in res else "-1") for i in range(n)]))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR VAR VAR STRING VAR FUNC_CALL VAR VAR
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
for _ in range(int(input())):
n = int(input())
a = list()
for i in range(n):
w, h = map(int, input().split())
a.append((w, h, i))
a.append((h, w, i))
a.sort()
mn, mk, mw = [a[0][1]], [0], [-1]
for i in range(1, n * 2):
if mn[-1] > a[i][1]:
mn.append(a[i][1])
mk.append(i)
else:
mn.append(mn[-1])
mk.append(mk[-1])
if a[i][0] > a[i - 1][0]:
mw.append(i - 1)
else:
mw.append(mw[-1])
ans = [-1] * n
for i in range(n * 2):
if mw[i] != -1:
if mn[mw[i]] < a[i][1]:
ans[a[i][2]] = a[mk[mw[i]]][2] + 1
print(*ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR LIST VAR NUMBER NUMBER LIST NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
t = int(input())
for _ in range(t):
n = int(input())
a = []
for i in range(1, n + 1):
h, w = map(int, input().split())
if h > w:
a.append((h, w, i))
else:
a.append((w, h, i))
a.sort()
ans = ["-1"] * n
j = 0
k = -1
for i in range(n):
while a[j][0] != a[i][0]:
if k == -1 or a[j][1] < a[k][1]:
k = j
j += 1
if k != -1 and a[k][1] < a[i][1]:
ans[a[i][2] - 1] = str(a[k][2])
print(" ".join(ans))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
from sys import stdin
input = stdin.readline
def photo(arr):
temp = []
ans = [-1] * len(arr)
for i in range(len(arr)):
temp.append([arr[i][0], arr[i][1], i])
temp = sorted(temp, key=lambda s: s[1], reverse=True)
temp = sorted(temp, key=lambda s: s[0])
mw = [10**10, -1]
for i in range(len(temp)):
if mw[0] < temp[i][1]:
ans[temp[i][2]] = mw[1] + 1
else:
mw = [temp[i][1], temp[i][2]]
print(*ans)
return ""
for i in range(int(input())):
a = int(input())
blanck = []
for j in range(a):
x, y = map(int, input().strip().split())
if x > y:
x, y = y, x
blanck.append([x, y])
print(photo(blanck))
|
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
import sys
def main():
res = ""
input = sys.stdin.readline
print = sys.stdout.write
t = int(input())
for _ in range(t):
n = int(input())
a = []
origin = []
answer = {}
sub_res = ""
for i in range(n):
w, h = map(int, input().split())
if w > h:
origin.append((w, h))
a.append((w, h, i))
else:
origin.append((h, w))
a.append((h, w, i))
a.sort()
last_ind = 0
answer[last_ind] = 1000000001, -1
for w, h, ind in a:
if not answer.get(w):
answer[w - 1] = answer[last_ind]
answer[w] = answer[last_ind]
last_ind = w
if h < answer[w][0]:
answer[w] = [h, ind]
for w, h in origin:
if answer[w - 1][0] < h:
sub_res += str(answer[w - 1][1] + 1) + " "
else:
sub_res += "-1 "
res += str(sub_res) + "\n"
print(res)
main()
|
IMPORT FUNC_DEF ASSIGN VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR LIST VAR VAR FOR VAR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER STRING VAR STRING VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
import sys
t = int(input())
for i in range(0, t):
n = int(input())
people = []
for j in range(0, n):
h, w = map(int, input().split())
people.append((w, h, j))
people.append((h, w, j))
people.sort(key=lambda x: x[1], reverse=True)
people.sort(key=lambda x: x[0])
minHeight = sys.maxsize
ans = [-1] * n
element = -1
for j in people:
w = j[0]
h = j[1]
ind = j[2]
if h < minHeight:
minHeight = h
element = ind
elif h > minHeight:
ans[ind] = element + 1
print(*ans)
|
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
import sys
input = sys.stdin.buffer.readline
T = int(input())
def solve():
n = int(input())
res = [-1] * n
sizes = []
for i in range(n):
w, h = map(int, input().split())
if w > h:
sizes += ((h, w, i),)
else:
sizes += ((w, h, i),)
sizes.sort(key=lambda x: x[0])
l = 0
lw, lh, lj = sizes[l]
min_h, min_h_j = lh, lj
while l < n and lw == sizes[l][0]:
lw, lh, lj = sizes[l]
if lh < min_h:
min_h, min_h_j = lh, lj
l += 1
r = l
while r < n:
lw, lh, lj = sizes[l]
rh, rj = lh, lj
while r < n and sizes[r][0] == lw:
_, rh, rj = sizes[r]
if rh > min_h:
res[rj] = min_h_j + 1
r += 1
if r == n:
break
while l < r:
lw, lh, lj = sizes[l]
if lh < min_h:
min_h, min_h_j = lh, lj
l += 1
print(*res)
while T:
solve()
T -= 1
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR NUMBER
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n = int(input())
a = []
for i in range(n):
w, h = map(int, input().split())
a.append([w, h, i])
a.append([h, w, i])
a.sort(key=lambda x: (x[0], -x[1]))
ans = [-1] * n
minh = 1000000000.0
mini = 1
for i in range(2 * n):
w, h, j = a[i][0], a[i][1], a[i][2]
if h > minh:
ans[j] = mini
else:
minh = h
mini = j + 1
print(*ans)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
cases = int(input())
for _ in range(cases):
n = int(input())
arr = []
oarr = []
seen = {}
for i in range(n):
a, b = [int(i) for i in input().strip().split()]
oarr.append((a, b))
if (a, b) not in seen:
arr.append((a, b, i))
seen[a, b] = 1
if (b, a) not in seen:
arr.append((b, a, i))
seen[b, a] = 1
arr.sort()
res = {}
def solve(arr):
n = len(arr)
lo = 0
idx = 0
while idx < n:
first = idx
aa, bb, ii = arr[lo]
while idx < n and arr[idx][0] == arr[first][0]:
a, b, i = arr[idx]
if a > aa and b > bb or a > bb and b > aa:
res[a, b] = str(ii + 1)
idx += 1
if arr[first][1] < arr[lo][1]:
lo = first
solve(arr)
rs = []
for i, (a, b) in enumerate(oarr):
ans = "-1"
for x, y in [(a, b), (b, a)]:
if (x, y) in res:
ans = res[x, y]
rs.append(ans)
print(" ".join(rs))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR DICT FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR VAR LIST VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.
Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).
The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:
$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);
$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).
For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:
the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);
the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).
In other cases, the person in the foreground will overlap the person in the background.
Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).
Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases. Then $t$ test cases follow.
The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the number of friends.
This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) β height and width of the $i$-th friend, respectively.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.
If there are several answers, output any.
-----Examples-----
Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1
-1 -1 -1
-1 -1 2 2
3 3 -1 3
-----Note-----
The first test case is described in the statement.
In the third test case, the following answers are also correct:
$[-1, -1, 1, 2]$;
$[-1, -1, 1, 1]$;
$[-1, -1, 2, 1]$.
|
def set_p(p, d, i, v):
p[d[i][2]] = v if v == -1 else d[v][2] + 1
def solve(n, d):
d.sort(key=lambda x: x[0])
p = [0] * n
m_c = m_nc = -1
for i in range(n):
if i:
if d[i][0] > d[i - 1][0]:
m_nc = min(m_c, m_nc, key=lambda x: d[x][1]) if m_nc != -1 else m_c
m_c = -1
if m_c == -1:
if d[i][1] <= d[m_nc][1]:
set_p(p, d, i, -1)
else:
set_p(p, d, i, m_nc)
m_c = i
else:
m_c = min(m_c, i, key=lambda x: d[x][1])
if d[i][1] <= d[m_nc][1]:
set_p(p, d, i, -1)
else:
set_p(p, d, i, m_nc)
else:
m_c = i
set_p(p, d, i, -1)
print(*p)
def main():
t = int(input())
for _ in range(t):
n = int(input())
d = []
for i in range(n):
hi, wi = map(int, input().split())
if hi < wi:
d.append((wi, hi, i))
else:
d.append((hi, wi, i))
solve(n, d)
main()
|
FUNC_DEF ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) β the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) β the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer β the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
t = int(input())
for i in range(t):
n, k = map(int, input().split())
nums = [int(i) for i in input().split()]
leftMult, rightMult = 1, 2
left = 0
count = 1
ans = 0
for right in range(1, n):
cond = nums[right - 1] * leftMult < nums[right] * rightMult
if cond:
count += 1
else:
count = 1
if count > k:
ans += 1
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) β the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) β the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer β the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
for _ in range(int(input())):
z, y = map(int, input().split())
x = list(map(int, input().split()))
r = 0
prev = x[0]
first = 0
last = 0
a = 1
while a < z:
if x[a] * 2 > x[a - 1]:
last += 1
else:
r += max(0, last - first + 1 - y)
first, last = a, a
a += 1
r += max(0, last - first + 1 - y)
print(r)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) β the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) β the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer β the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
def solve():
n, k = map(int, input().split())
n += 1
a = list(map(int, input().split())) + [-1]
ans = 0
cnt = 1
for i in range(1, n):
if a[i] * 2 > a[i - 1]:
cnt += 1
else:
ans += max(0, cnt - k)
cnt = 1
print(ans)
def main():
t = int(input())
for _ in range(t):
solve()
main()
|
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) β the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) β the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer β the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
t = int(input())
for _ in range(t):
n, k = map(int, input().split())
a = [int(i) for i in input().split()]
sum = 0
ans = 0
for i in range(1, n):
if a[i] > a[i - 1] / 2:
sum += 1
else:
sum = 0
if sum >= k:
ans += 1
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) β the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) β the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer β the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
for _ in range(int(input())):
n, k = map(int, input().split())
l = list(map(int, input().split()))
r = []
cnt = 0
for i in range(n - 1):
r.append(l[i] < 2 * l[i + 1])
ones = sum(r[:k])
if ones == k:
cnt += 1
for i in range(k, n - 1):
ones += r[i]
ones -= r[i - k]
if ones == k:
cnt += 1
print(cnt)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) β the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) β the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer β the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
T = int(input())
for _ in range(T):
n, k = map(int, input().split())
s = list(map(int, input().split()))
ans = 0
start = 0
end = 1
count = 0
while end < n:
if s[end] * 2 > s[end - 1]:
end += 1
count += 1
if count == k:
ans += 1
start += 1
count -= 1
else:
start = end
end = start + 1
count = 0
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) β the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) β the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer β the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
for _ in range(int(input())):
n, k = map(int, input().split())
nums = list(map(int, input().split()))
ans = [1] * n
for i in range(1, n):
if nums[i - 1] < 2 * nums[i]:
ans[i] = 1 + ans[i - 1]
count = 0
for i in range(k, n):
if ans[i] - ans[i - k] == k:
count += 1
print(count)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) β the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) β the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer β the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
for _ in [0] * int(input()):
n, k = map(int, input().split())
(*a,) = map(int, input().split())
t = 0
b = [i for i in range(n - 1) if a[i] >= 2 * a[i + 1]]
m = len(b)
if m == 0:
print(n - k)
continue
for i in range(m - 1):
t += max(0, b[i + 1] - b[i] - k)
print(t + max(0, b[0] - k + 1) + max(0, n - b[-1] - k - 1))
|
FOR VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) β the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) β the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer β the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
for _ in range(int(input())):
n, k = map(int, input().split())
arr = list(map(int, input().split()))
streak = 1
ans = 0
for i in range(1, n):
if arr[i - 1] < arr[i] * 2:
streak += 1
else:
ans += max(streak - k, 0)
streak = 1
ans += max(streak - k, 0)
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) β the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) β the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer β the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
for _ in range(int(input())):
n, k = map(int, input().split())
l = list(map(int, input().split()))
j = 0
ans = 0
for i in range(1, n):
if 2 * l[i] > l[i - 1]:
v = 10
else:
j = i
if i - j == k:
ans += 1
j += 1
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) β the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) β the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer β the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
def solve():
n, k = map(int, input().split())
arr = [int(i) for i in input().split()]
t = []
for i, v in enumerate(arr[:-1]):
if arr[i + 1] * 2 > v:
t.append(True)
else:
t.append(False)
res = 0
f = sum(1 if not i else 0 for i in t[:k])
if f == 0:
res += 1
for i, v in enumerate(t[k:]):
if not v:
f += 1
if not t[i]:
f -= 1
if not f:
res += 1
return res
def main():
for _ in range(int(input())):
print(solve())
return
main()
|
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR IF VAR NUMBER VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR NUMBER RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN EXPR FUNC_CALL VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) β the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) β the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer β the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
for _ in range(int(input())):
n, k = list(map(int, input().split()))
arr = list(map(int, input().split()))
prev = arr[0]
cnt = 0
ans = 0
for i in range(1, n):
if arr[i] > prev // 2:
if cnt == k - 1:
ans += 1
else:
cnt += 1
else:
cnt = 0
prev = arr[i]
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) β the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) β the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer β the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
for _ in range(int(input())):
n, k = map(int, input().split())
a = [int(i) for i in input().split()]
dp = [(1) for i in range(n)]
count = 0
for i in range(1, n):
if 2 * a[i] > a[i - 1]:
dp[i] = dp[i - 1] + 1
else:
dp[i] = 1
if dp[i] > k:
count += 1
print(count)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) β the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) β the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer β the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
def solve():
n, k = map(int, input().split())
arr = list(map(int, input().split()))
i = 1
x = 1
ans = 0
ct = 1
while i < n:
if arr[i - 1] * x < arr[i] * x * 2:
ct += 1
else:
if ct >= k:
ans += ct - k
ct = 1
x = 1
i += 1
if ct >= k:
ans += ct - k
print(ans)
t = int(input())
for i in range(t):
solve()
|
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Given an array $a$ of length $n$ and an integer $k$, find the number of indices $1 \leq i \leq n - k$ such that the subarray $[a_i, \dots, a_{i+k}]$ with length $k+1$ (not with length $k$) has the following property:
If you multiply the first element by $2^0$, the second element by $2^1$, ..., and the ($k+1$)-st element by $2^k$, then this subarray is sorted in strictly increasing order.
More formally, count the number of indices $1 \leq i \leq n - k$ such that $$2^0 \cdot a_i < 2^1 \cdot a_{i+1} < 2^2 \cdot a_{i+2} < \dots < 2^k \cdot a_{i+k}.$$
-----Input-----
The first line contains an integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains two integers $n$, $k$ ($3 \leq n \leq 2 \cdot 10^5$, $1 \leq k < n$) β the length of the array and the number of inequalities.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) β the elements of the array.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer β the number of indices satisfying the condition in the statement.
-----Examples-----
Input
6
4 2
20 22 19 84
5 1
9 5 3 2 1
5 2
9 5 3 2 1
7 2
22 12 16 4 3 22 12
7 3
22 12 16 4 3 22 12
9 3
3 9 12 3 9 12 3 9 12
Output
2
3
2
3
1
0
-----Note-----
In the first test case, both subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2,a_3] = [20,22,19]$, and $1 \cdot 20 < 2 \cdot 22 < 4 \cdot 19$.
$i=2$: the subarray $[a_2,a_3,a_4] = [22,19,84]$, and $1 \cdot 22 < 2 \cdot 19 < 4 \cdot 84$.
In the second test case, three subarrays satisfy the condition:
$i=1$: the subarray $[a_1,a_2] = [9,5]$, and $1 \cdot 9 < 2 \cdot 5$.
$i=2$: the subarray $[a_2,a_3] = [5,3]$, and $1 \cdot 5 < 2 \cdot 3$.
$i=3$: the subarray $[a_3,a_4] = [3,2]$, and $1 \cdot 3 < 2 \cdot 2$.
$i=4$: the subarray $[a_4,a_5] = [2,1]$, but $1 \cdot 2 = 2 \cdot 1$, so this subarray doesn't satisfy the condition.
|
def p2Arr():
x = int(input())
for __ in range(x):
[n, k] = list(map(int, input().split()))
arr = list(map(int, input().split()))
c = 1
fn = 0
for i in range(1, len(arr)):
c += 1 if 2 * arr[i] > arr[i - 1] else -1 * (c - 1)
if c >= k + 1:
fn += 1
print(fn)
p2Arr()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN LIST VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.