inputs
stringlengths 50
14k
| targets
stringlengths 4
655k
|
|---|---|
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):
# print()
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[i] stores the max power of the hero that can go i steps
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])
# print(a)
# print(b)
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
# ind = 0
# while ind < n:
# temp = 0
# for i in range(m):
# l = 0
# if b[i][0] <= temp:
# break
# if ind + temp == n:
# break
# # print(a[ind:len(a)])
# end = min(ind+b[i][0], len(a))
# for j in range(ind, end):
# if a[j] > b[i][1]:
# break
# l += 1
# if l > temp:
# temp = l
# # print(temp, ind)
# ind += temp
# ans += 1
# # print(a, temp)
print(ans)
|
You play a computer game. In this game, you lead a party of $m$ heroes, and you have to clear a dungeon with $n$ monsters. Each monster is characterized by its power $a_i$. Each hero is characterized by his power $p_i$ and endurance $s_i$.
The heroes clear the dungeon day by day. In the beginning of each day, you choose a hero (exactly one) who is going to enter the dungeon this day.
When the hero enters the dungeon, he is challenged by the first monster which was not defeated during the previous days (so, if the heroes have already defeated $k$ monsters, the hero fights with the monster $k + 1$). When the hero fights the monster, there are two possible outcomes:
if the monster's power is strictly greater than the hero's power, the hero retreats from the dungeon. The current day ends; otherwise, the monster is defeated.
After defeating a monster, the hero either continues fighting with the next monster or leaves the dungeon. He leaves the dungeon either if he has already defeated the number of monsters equal to his endurance during this day (so, the $i$-th hero cannot defeat more than $s_i$ monsters during each day), or if all monsters are defeated — otherwise, he fights with the next monster. When the hero leaves the dungeon, the current day ends.
Your goal is to defeat the last monster. What is the minimum number of days that you need to achieve your goal? Each day you have to use exactly one hero; it is possible that some heroes don't fight the monsters at all. Each hero can be used arbitrary number of times.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^5$) — the number of test cases. Then the test cases follow.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of monsters in the dungeon.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the power of the $i$-th monster.
The third line contains one integer $m$ ($1 \le m \le 2 \cdot 10^5$) — the number of heroes in your party.
Then $m$ lines follow, each describing a hero. Each line contains two integers $p_i$ and $s_i$ ($1 \le p_i \le 10^9$, $1 \le s_i \le n$) — the power and the endurance of the $i$-th hero.
It is guaranteed that the sum of $n + m$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case print one integer — the minimum number of days you have to spend to defeat all of the monsters (or $-1$ if it is impossible).
-----Example-----
Input
2
6
2 3 11 14 1 8
2
3 2
100 1
5
3 5 100 2 3
2
30 5
90 1
Output
5
-1
|
import sys
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 = list(map( list, 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 )
|
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)
|
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
|
# -*- coding: utf-8 -*-
import sys
from bisect import bisect_left
def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return list(map(int, input().split()))
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7
class SegTree:
def __init__(self, n, func, intv, A=[]):
self.n = n
self.func = func
self.intv = intv
n2 = 1
while n2 < n:
n2 <<= 1
self.n2 = n2
self.tree = [self.intv] * (n2 << 1)
if A:
for i in range(n):
self.tree[n2+i] = A[i]
for i in range(n2-1, -1, -1):
self.tree[i] = self.func(self.tree[i*2], self.tree[i*2+1])
def update(self, i, x):
i += self.n2
self.tree[i] = x
while i > 0:
i >>= 1
self.tree[i] = self.func(self.tree[i*2], self.tree[i*2+1])
def query(self, a, b):
l = a + self.n2
r = b + self.n2
s = self.intv
while l < r:
if r & 1:
r -= 1
s = self.func(s, self.tree[r])
if l & 1:
s = self.func(s, self.tree[l])
l += 1
l >>= 1
r >>= 1
return s
def get(self, i):
return self.tree[i+self.n2]
def all(self):
return self.tree[1]
def bisearch_max(mn, mx, func):
ok = mn
ng = mx
while ok+1 < ng:
mid = (ok+ng) // 2
if func(mid):
ok = mid
else:
ng = mid
return ok
def check(m):
mx = st.query(cur, m)
idx = bisect_left(P, mx)
if idx == M:
return False
_, s = PS[idx]
scnt = m - cur
return s >= scnt
ans = []
for _ in range(INT()):
N = INT()
A = LIST()
M = INT()
PS = []
for i in range(M):
p, s = MAP()
PS.append((p, s))
PS.sort()
for i in range(M-1, 0, -1):
if PS[i][1] > PS[i-1][1]:
PS[i-1] = (PS[i-1][0], PS[i][1])
P, _ = list(zip(*PS))
st = SegTree(N, max, 0, A)
cur = day = 0
while cur < N:
res = bisearch_max(cur, N+1, check)
if res == cur:
ans.append(str(-1))
break
cur = res
day += 1
else:
ans.append(str(day))
print('\n'.join(ans))
|
You play a computer game. In this game, you lead a party of $m$ heroes, and you have to clear a dungeon with $n$ monsters. Each monster is characterized by its power $a_i$. Each hero is characterized by his power $p_i$ and endurance $s_i$.
The heroes clear the dungeon day by day. In the beginning of each day, you choose a hero (exactly one) who is going to enter the dungeon this day.
When the hero enters the dungeon, he is challenged by the first monster which was not defeated during the previous days (so, if the heroes have already defeated $k$ monsters, the hero fights with the monster $k + 1$). When the hero fights the monster, there are two possible outcomes:
if the monster's power is strictly greater than the hero's power, the hero retreats from the dungeon. The current day ends; otherwise, the monster is defeated.
After defeating a monster, the hero either continues fighting with the next monster or leaves the dungeon. He leaves the dungeon either if he has already defeated the number of monsters equal to his endurance during this day (so, the $i$-th hero cannot defeat more than $s_i$ monsters during each day), or if all monsters are defeated — otherwise, he fights with the next monster. When the hero leaves the dungeon, the current day ends.
Your goal is to defeat the last monster. What is the minimum number of days that you need to achieve your goal? Each day you have to use exactly one hero; it is possible that some heroes don't fight the monsters at all. Each hero can be used arbitrary number of times.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 10^5$) — the number of test cases. Then the test cases follow.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of monsters in the dungeon.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the power of the $i$-th monster.
The third line contains one integer $m$ ($1 \le m \le 2 \cdot 10^5$) — the number of heroes in your party.
Then $m$ lines follow, each describing a hero. Each line contains two integers $p_i$ and $s_i$ ($1 \le p_i \le 10^9$, $1 \le s_i \le n$) — the power and the endurance of the $i$-th hero.
It is guaranteed that the sum of $n + m$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case print one integer — the minimum number of days you have to spend to defeat all of the monsters (or $-1$ if it is impossible).
-----Example-----
Input
2
6
2 3 11 14 1 8
2
3 2
100 1
5
3 5 100 2 3
2
30 5
90 1
Output
5
-1
|
import sys
input = sys.stdin.readline
t = int(input())
ANS = []
for _ in range(t):
n = int(input())
a = list(map(int,input().split()))
m = int(input())
ps = [list(map(int, input().split())) for _ in range(m)]
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)))
|
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):
print(-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
print(ans)
|
Polycarp plays a computer game (yet again). In this game, he fights monsters using magic spells.
There are two types of spells: fire spell of power $x$ deals $x$ damage to the monster, and lightning spell of power $y$ deals $y$ damage to the monster and doubles the damage of the next spell Polycarp casts. Each spell can be cast only once per battle, but Polycarp can cast them in any order.
For example, suppose that Polycarp knows three spells: a fire spell of power $5$, a lightning spell of power $1$, and a lightning spell of power $8$. There are $6$ ways to choose the order in which he casts the spells:
first, second, third. This order deals $5 + 1 + 2 \cdot 8 = 22$ damage; first, third, second. This order deals $5 + 8 + 2 \cdot 1 = 15$ damage; second, first, third. This order deals $1 + 2 \cdot 5 + 8 = 19$ damage; second, third, first. This order deals $1 + 2 \cdot 8 + 2 \cdot 5 = 27$ damage; third, first, second. This order deals $8 + 2 \cdot 5 + 1 = 19$ damage; third, second, first. This order deals $8 + 2 \cdot 1 + 2 \cdot 5 = 20$ damage.
Initially, Polycarp knows $0$ spells. His spell set changes $n$ times, each time he either learns a new spell or forgets an already known one. After each change, calculate the maximum possible damage Polycarp may deal using the spells he knows.
-----Input-----
The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of changes to the spell set.
Each of the next $n$ lines contains two integers $tp$ and $d$ ($0 \le tp_i \le 1$; $-10^9 \le d \le 10^9$; $d_i \neq 0$) — the description of the change. If $tp_i$ if equal to $0$, then Polycarp learns (or forgets) a fire spell, otherwise he learns (or forgets) a lightning spell.
If $d_i > 0$, then Polycarp learns a spell of power $d_i$. Otherwise, Polycarp forgets a spell with power $-d_i$, and it is guaranteed that he knew that spell before the change.
It is guaranteed that the powers of all spells Polycarp knows after each change are different (Polycarp never knows two spells with the same power).
-----Output-----
After each change, print the maximum damage Polycarp can deal with his current set of spells.
-----Example-----
Input
6
1 5
0 10
1 -5
0 5
1 11
0 -10
Output
5
25
10
15
36
21
|
class BIT():
def __init__(self,n):
self.BIT=[0]*(n+1)
self.num=n
def query(self,idx):
res_sum = 0
while idx > 0:
res_sum += self.BIT[idx]
idx -= idx&(-idx)
return res_sum
#Ai += x O(logN)
def update(self,idx,x):
while idx <= self.num:
self.BIT[idx] += x
idx += idx&(-idx)
return
import sys,heapq,random
input=sys.stdin.readline
n=int(input())
spell=[tuple(map(int,input().split())) for i in range(n)]
S=set([])
for i in range(n):
S.add(abs(spell[i][1]))
S=list(S)
S.sort(reverse=True)
comp={i:e+1 for e,i in enumerate(S)}
N=len(S)
x_exist=BIT(N)
y_exist=BIT(N)
power=BIT(N)
X,Y,S=0,0,0
Xmax=[]
Ymin=[]
x_data=[0]*(N+1)
y_data=[0]*(N+1)
for i in range(n):
t,d=spell[i]
S+=d
if d<0:
id=comp[-d]
if t==0:
X-=1
x_exist.update(id,-1)
power.update(id,d)
x_data[id]-=1
else:
Y-=1
y_exist.update(id,-1)
power.update(id,d)
y_data[id]-=1
else:
id=comp[d]
if t==0:
X+=1
x_exist.update(id,1)
power.update(id,d)
heapq.heappush(Xmax,-d)
x_data[id]+=1
else:
Y+=1
y_exist.update(id,1)
power.update(id,d)
heapq.heappush(Ymin,d)
y_data[id]+=1
if X==0:
if Y==0:
print(0)
else:
while not y_data[comp[Ymin[0]]]:
heapq.heappop(Ymin)
print(2*S-Ymin[0])
else:
if Y==0:
print(S)
else:
start=0
end=N
while end-start>1:
test=(end+start)//2
if x_exist.query(test)+y_exist.query(test)<=Y:
start=test
else:
end=test
if y_exist.query(start)!=Y:
print(S+power.query(start))
else:
while not y_data[comp[Ymin[0]]]:
heapq.heappop(Ymin)
while not x_data[comp[-Xmax[0]]]:
heapq.heappop(Xmax)
print(S+power.query(start)-Ymin[0]-Xmax[0])
|
Polycarp plays a computer game (yet again). In this game, he fights monsters using magic spells.
There are two types of spells: fire spell of power $x$ deals $x$ damage to the monster, and lightning spell of power $y$ deals $y$ damage to the monster and doubles the damage of the next spell Polycarp casts. Each spell can be cast only once per battle, but Polycarp can cast them in any order.
For example, suppose that Polycarp knows three spells: a fire spell of power $5$, a lightning spell of power $1$, and a lightning spell of power $8$. There are $6$ ways to choose the order in which he casts the spells:
first, second, third. This order deals $5 + 1 + 2 \cdot 8 = 22$ damage; first, third, second. This order deals $5 + 8 + 2 \cdot 1 = 15$ damage; second, first, third. This order deals $1 + 2 \cdot 5 + 8 = 19$ damage; second, third, first. This order deals $1 + 2 \cdot 8 + 2 \cdot 5 = 27$ damage; third, first, second. This order deals $8 + 2 \cdot 5 + 1 = 19$ damage; third, second, first. This order deals $8 + 2 \cdot 1 + 2 \cdot 5 = 20$ damage.
Initially, Polycarp knows $0$ spells. His spell set changes $n$ times, each time he either learns a new spell or forgets an already known one. After each change, calculate the maximum possible damage Polycarp may deal using the spells he knows.
-----Input-----
The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of changes to the spell set.
Each of the next $n$ lines contains two integers $tp$ and $d$ ($0 \le tp_i \le 1$; $-10^9 \le d \le 10^9$; $d_i \neq 0$) — the description of the change. If $tp_i$ if equal to $0$, then Polycarp learns (or forgets) a fire spell, otherwise he learns (or forgets) a lightning spell.
If $d_i > 0$, then Polycarp learns a spell of power $d_i$. Otherwise, Polycarp forgets a spell with power $-d_i$, and it is guaranteed that he knew that spell before the change.
It is guaranteed that the powers of all spells Polycarp knows after each change are different (Polycarp never knows two spells with the same power).
-----Output-----
After each change, print the maximum damage Polycarp can deal with his current set of spells.
-----Example-----
Input
6
1 5
0 10
1 -5
0 5
1 11
0 -10
Output
5
25
10
15
36
21
|
#!/usr/bin/env python3
import sys
input = sys.stdin.readline
class SegmentTree:
def __init__(self, a):
# Operator
self.op = lambda a, b : a + b
# Identity element
self.e = 0
self.n = len(a)
self.lv = (self.n - 1).bit_length()
self.size = 2**self.lv
self.data = [self.e] * (2*self.size - 1)
# Bisect checking function
self._check = lambda x, acc : acc >= x
self._acc = self.e
self.initialize(a)
# Initialize data
def initialize(self, a):
for i in range(self.n):
self.data[self.size + i - 1] = a[i]
for i in range(self.size-2, -1, -1):
self.data[i] = self.op(self.data[i*2 + 1], self.data[i*2 + 2])
# Update ak as x (0-indexed)
def update(self, k, x):
k += self.size - 1
self.data[k] = x
while k > 0:
k = (k - 1) // 2
self.data[k] = self.op(self.data[2*k+1], self.data[2*k+2])
# Min value in [l, r) (0-indexed)
def fold(self, l, r):
L = l + self.size; R = r + self.size
s = self.e
while L < R:
if R & 1:
R -= 1
s = self.op(s, self.data[R-1])
if L & 1:
s = self.op(s, self.data[L-1])
L += 1
L >>= 1; R >>= 1
return s
def _bisect_forward(self, x, start, k):
# When segment-k is at the bottom, accumulate and return.
if k >= self.size - 1:
self._acc = self.op(self._acc, self.data[k])
if self._check(x, self._acc):
return k - (self.size - 1)
else:
return -1
width = 2**(self.lv - (k+1).bit_length() + 1)
mid = (k+1) * width + width // 2 - self.size
# When left-child isn't in range, just look at right-child.
if mid <= start:
return self._bisect_forward(x, start, 2*k + 2)
# When segment-k is in range and has no answer in it, accumulate and return -1
tmp_acc = self.op(self._acc, self.data[k])
if start <= mid - width // 2 and not self._check(x, tmp_acc):
self._acc = tmp_acc
return -1
# Check left-child then right-child
vl = self._bisect_forward(x, start, 2*k + 1)
if vl != -1:
return vl
return self._bisect_forward(x, start, 2*k + 2)
# Returns min index s.t. start <= index and satisfy check(data[start:idx)) = True
def bisect_forward(self, x, start=None):
if start:
ret = self._bisect_forward(x, start, 0)
else:
ret = self._bisect_forward(x, 0, 0)
self._acc = self.e
return ret
def _bisect_backward(self, x, start, k):
# When segment-k is at the bottom, accumulate and return.
if k >= self.size - 1:
self._acc = self.op(self._acc, self.data[k])
if self._check(x, self._acc):
return k - (self.size - 1)
else:
return -1
width = 2**(self.lv - (k+1).bit_length() + 1)
mid = (k+1) * width + width // 2 - self.size
# When right-child isn't in range, just look at right-child.
if mid >= start:
return self._bisect_backward(x, start, 2*k + 1)
# When segment-k is in range and has no answer in it, accumulate and return -1
tmp_acc = self.op(self._acc, self.data[k])
if start > mid + width // 2 and not self._check(x, tmp_acc):
self._acc = tmp_acc
return -1
# Check right-child then left-child
vl = self._bisect_backward(x, start, 2*k + 2)
if vl != -1:
return vl
return self._bisect_backward(x, start, 2*k + 1)
# Returns max index s.t. index < start and satisfy check(data[idx:start)) = True
def bisect_backward(self, x, start=None):
if start:
ret = self._bisect_backward(x, start, 0)
else:
ret = self._bisect_backward(x, self.n, 0)
self._acc = self.e
return ret
n = int(input())
query = []
seen = set([0])
for _ in range(n):
kind, val = map(int, input().split())
query.append((kind, val))
if val > 0:
seen.add(val)
unique = list(seen)
unique.sort()
comp = {val: i for i, val in enumerate(unique)}
decomp = {i: val for i, val in enumerate(unique)}
decopm = {}
nn = len(comp)
base = [0] * nn
STfire = SegmentTree(base)
STnum = SegmentTree(base)
STval = SegmentTree(base)
tnum = 0
fnum = 0
spell = 0
total = 0
for kind, val in query:
cd = comp[abs(val)]
if val > 0:
STval.update(cd, val)
STnum.update(cd, 1)
total += val
if kind == 1:
tnum += 1
else:
STfire.update(cd, 1)
fnum += 1
else:
total += val
STval.update(cd, 0)
STnum.update(cd, 0)
if kind == 1:
tnum -= 1
else:
STfire.update(cd, 0)
fnum -= 1
spell = tnum + fnum
if fnum == 0:
fid = -1
else:
fid = STfire.bisect_forward(fnum)
l = STnum.bisect_forward(spell - tnum)
if tnum == 0:
print(total)
continue
if fid >= l + 1:
double_total = STval.fold(l + 1, nn)
print(total + double_total)
else:
l = STnum.bisect_forward(spell - tnum + 1)
double_total = STval.fold(l + 1, nn)
if fnum > 0:
print(total + double_total + decomp[fid])
else:
print(total + double_total)
|
Let's call an array $a_1, a_2, \dots, a_m$ of nonnegative integer numbers good if $a_1 + a_2 + \dots + a_m = 2\cdot(a_1 \oplus a_2 \oplus \dots \oplus a_m)$, where $\oplus$ denotes the bitwise XOR operation.
For example, array $[1, 2, 3, 6]$ is good, as $1 + 2 + 3 + 6 = 12 = 2\cdot 6 = 2\cdot (1\oplus 2 \oplus 3 \oplus 6)$. At the same time, array $[1, 2, 1, 3]$ isn't good, as $1 + 2 + 1 + 3 = 7 \neq 2\cdot 1 = 2\cdot(1\oplus 2 \oplus 1 \oplus 3)$.
You are given an array of length $n$: $a_1, a_2, \dots, a_n$. Append at most $3$ elements to it to make it good. Appended elements don't have to be different. It can be shown that the solution always exists under the given constraints. If there are different solutions, you are allowed to output any of them. Note that you don't have to minimize the number of added elements!. So, if an array is good already you are allowed to not append elements.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10\,000$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ $(1\le n \le 10^5)$ — the size of the array.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0\le a_i \le 10^9$) — the elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, output two lines.
In the first line, output a single integer $s$ ($0\le s\le 3$) — the number of elements you want to append.
In the second line, output $s$ integers $b_1, \dots, b_s$ ($0\le b_i \le 10^{18}$) — the elements you want to append to the array.
If there are different solutions, you are allowed to output any of them.
-----Example-----
Input
3
4
1 2 3 6
1
8
2
1 1
Output
0
2
4 4
3
2 6 2
-----Note-----
In the first test case of the example, the sum of all numbers is $12$, and their $\oplus$ is $6$, so the condition is already satisfied.
In the second test case of the example, after adding $4, 4$, the array becomes $[8, 4, 4]$. The sum of numbers in it is $16$, $\oplus$ of numbers in it is $8$.
|
for nt in range(int(input())):
n=int(input())
l=list(map(int,input().split()))
s=sum(l)
e=l[0]
for i in range(1,n):
e=e^l[i]
if s==2*e:
print(0)
print ()
else:
print(2)
print(e,s+e)
|
Let's call an array $a_1, a_2, \dots, a_m$ of nonnegative integer numbers good if $a_1 + a_2 + \dots + a_m = 2\cdot(a_1 \oplus a_2 \oplus \dots \oplus a_m)$, where $\oplus$ denotes the bitwise XOR operation.
For example, array $[1, 2, 3, 6]$ is good, as $1 + 2 + 3 + 6 = 12 = 2\cdot 6 = 2\cdot (1\oplus 2 \oplus 3 \oplus 6)$. At the same time, array $[1, 2, 1, 3]$ isn't good, as $1 + 2 + 1 + 3 = 7 \neq 2\cdot 1 = 2\cdot(1\oplus 2 \oplus 1 \oplus 3)$.
You are given an array of length $n$: $a_1, a_2, \dots, a_n$. Append at most $3$ elements to it to make it good. Appended elements don't have to be different. It can be shown that the solution always exists under the given constraints. If there are different solutions, you are allowed to output any of them. Note that you don't have to minimize the number of added elements!. So, if an array is good already you are allowed to not append elements.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10\,000$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ $(1\le n \le 10^5)$ — the size of the array.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0\le a_i \le 10^9$) — the elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, output two lines.
In the first line, output a single integer $s$ ($0\le s\le 3$) — the number of elements you want to append.
In the second line, output $s$ integers $b_1, \dots, b_s$ ($0\le b_i \le 10^{18}$) — the elements you want to append to the array.
If there are different solutions, you are allowed to output any of them.
-----Example-----
Input
3
4
1 2 3 6
1
8
2
1 1
Output
0
2
4 4
3
2 6 2
-----Note-----
In the first test case of the example, the sum of all numbers is $12$, and their $\oplus$ is $6$, so the condition is already satisfied.
In the second test case of the example, after adding $4, 4$, the array becomes $[8, 4, 4]$. The sum of numbers in it is $16$, $\oplus$ of numbers in it is $8$.
|
for _ in range(int(input())):
n=int(input())
l=list(map(int,input().split()))
x=0
for i in l:
x^=i
s=sum(l)
if s==(2*x):
print("0\n")
else:
if x==0:
print("1")
print(s)
else:
print("2")
print(x,x+s)
|
All techniques in the ninja world consist of hand seals. At the moment Naruto is learning a new technique, which consists of $n\cdot m$ different seals, denoted by distinct numbers. All of them were written in an $n\times m$ table.
The table is lost now. Naruto managed to remember elements of each row from left to right, and elements of each column from top to bottom, but he doesn't remember the order of rows and columns. Please restore the table consistent with this data so that Naruto will be able to learn the new technique.
-----Input-----
The first line of the input contains the only integer $t$ ($1\leq t\leq 100\,000$) denoting the number of test cases. Their descriptions follow.
The first line of each test case description consists of two space-separated integers $n$ and $m$ ($1 \leq n, m \leq 500$) standing for the number of rows and columns in the table, respectively. All hand seals are encoded by the positive integers from $1$ to $n\cdot m$.
The following $n$ lines contain $m$ space separated integers each, denoting elements of an arbitrary row in the table left to right.
The following $m$ lines contain $n$ space separated integers each, denoting elements of an arbitrary column in the table top to bottom.
Sum of $nm$ over all test cases does not exceed $250\,000$. It is guaranteed that each row occurs in the input exactly once, as well as each column. It is also guaranteed that each number from $1$ to $nm$ occurs exactly once in all rows, as well as in all columns. Finally, it is guaranteed that a table consistent with the input exists.
-----Output-----
For each test case, output $n$ lines with $m$ space-separated integers each, denoting the restored table. One can show that the answer is always unique.
-----Example-----
Input
2
2 3
6 5 4
1 2 3
1 6
2 5
3 4
3 1
2
3
1
3 1 2
Output
1 2 3
6 5 4
3
1
2
-----Note-----
Consider the first test case. The matrix is $2 \times 3$. You are given the rows and columns in arbitrary order.
One of the rows is $[6, 5, 4]$. One of the rows is $[1, 2, 3]$.
One of the columns is $[1, 6]$. One of the columns is $[2, 5]$. One of the columns is $[3, 4]$.
You are to reconstruct the matrix. The answer is given in the output.
|
from sys import stdin
input = stdin.readline
tests = int(input())
for test in range(tests):
n, m = list(map(int, input().split()))
a = [[0] * m for _ in range(n)]
r = [[int(i) for i in input().split()] for _ in range(n)]
c = [[int(i) for i in input().split()] for _ in range(m)]
z = [[-1, -1] for _ in range(n * m + 1)]
for i in range(n):
for j in range(m):
z[r[i][j]][0] = j
for i in range(m):
for j in range(n):
z[c[i][j]][1] = j
for i in range(1, n * m + 1):
a[z[i][1]][z[i][0]] = i
for i in a:
print(' '.join([str(j) for j in i]))
|
All techniques in the ninja world consist of hand seals. At the moment Naruto is learning a new technique, which consists of $n\cdot m$ different seals, denoted by distinct numbers. All of them were written in an $n\times m$ table.
The table is lost now. Naruto managed to remember elements of each row from left to right, and elements of each column from top to bottom, but he doesn't remember the order of rows and columns. Please restore the table consistent with this data so that Naruto will be able to learn the new technique.
-----Input-----
The first line of the input contains the only integer $t$ ($1\leq t\leq 100\,000$) denoting the number of test cases. Their descriptions follow.
The first line of each test case description consists of two space-separated integers $n$ and $m$ ($1 \leq n, m \leq 500$) standing for the number of rows and columns in the table, respectively. All hand seals are encoded by the positive integers from $1$ to $n\cdot m$.
The following $n$ lines contain $m$ space separated integers each, denoting elements of an arbitrary row in the table left to right.
The following $m$ lines contain $n$ space separated integers each, denoting elements of an arbitrary column in the table top to bottom.
Sum of $nm$ over all test cases does not exceed $250\,000$. It is guaranteed that each row occurs in the input exactly once, as well as each column. It is also guaranteed that each number from $1$ to $nm$ occurs exactly once in all rows, as well as in all columns. Finally, it is guaranteed that a table consistent with the input exists.
-----Output-----
For each test case, output $n$ lines with $m$ space-separated integers each, denoting the restored table. One can show that the answer is always unique.
-----Example-----
Input
2
2 3
6 5 4
1 2 3
1 6
2 5
3 4
3 1
2
3
1
3 1 2
Output
1 2 3
6 5 4
3
1
2
-----Note-----
Consider the first test case. The matrix is $2 \times 3$. You are given the rows and columns in arbitrary order.
One of the rows is $[6, 5, 4]$. One of the rows is $[1, 2, 3]$.
One of the columns is $[1, 6]$. One of the columns is $[2, 5]$. One of the columns is $[3, 4]$.
You are to reconstruct the matrix. The answer is given in the output.
|
import sys as _sys
def main():
t = int(input())
for i_t in range(t):
rows_n, columns_n = _read_ints()
rows = [tuple(_read_ints()) for i_row in range(rows_n)]
columns = [tuple(_read_ints()) for i_column in range(columns_n)]
any_first_column_element = rows[0][0]
i_first_column = 0
while any_first_column_element not in columns[i_first_column]:
i_first_column += 1
first_column = columns[i_first_column]
# Can be written in O(N*log(N)) but it is not necessary for N <= 500
rows = sorted(rows, key=lambda row: first_column.index(row[0]))
for row in rows:
print(*row)
def _read_line():
result = _sys.stdin.readline()
assert result[-1] == "\n"
return result[:-1]
def _read_ints():
return list(map(int, _read_line().split()))
def __starting_point():
main()
__starting_point()
|
All techniques in the ninja world consist of hand seals. At the moment Naruto is learning a new technique, which consists of $n\cdot m$ different seals, denoted by distinct numbers. All of them were written in an $n\times m$ table.
The table is lost now. Naruto managed to remember elements of each row from left to right, and elements of each column from top to bottom, but he doesn't remember the order of rows and columns. Please restore the table consistent with this data so that Naruto will be able to learn the new technique.
-----Input-----
The first line of the input contains the only integer $t$ ($1\leq t\leq 100\,000$) denoting the number of test cases. Their descriptions follow.
The first line of each test case description consists of two space-separated integers $n$ and $m$ ($1 \leq n, m \leq 500$) standing for the number of rows and columns in the table, respectively. All hand seals are encoded by the positive integers from $1$ to $n\cdot m$.
The following $n$ lines contain $m$ space separated integers each, denoting elements of an arbitrary row in the table left to right.
The following $m$ lines contain $n$ space separated integers each, denoting elements of an arbitrary column in the table top to bottom.
Sum of $nm$ over all test cases does not exceed $250\,000$. It is guaranteed that each row occurs in the input exactly once, as well as each column. It is also guaranteed that each number from $1$ to $nm$ occurs exactly once in all rows, as well as in all columns. Finally, it is guaranteed that a table consistent with the input exists.
-----Output-----
For each test case, output $n$ lines with $m$ space-separated integers each, denoting the restored table. One can show that the answer is always unique.
-----Example-----
Input
2
2 3
6 5 4
1 2 3
1 6
2 5
3 4
3 1
2
3
1
3 1 2
Output
1 2 3
6 5 4
3
1
2
-----Note-----
Consider the first test case. The matrix is $2 \times 3$. You are given the rows and columns in arbitrary order.
One of the rows is $[6, 5, 4]$. One of the rows is $[1, 2, 3]$.
One of the columns is $[1, 6]$. One of the columns is $[2, 5]$. One of the columns is $[3, 4]$.
You are to reconstruct the matrix. The answer is given in the output.
|
import math
from collections import deque
from sys import stdin, stdout, setrecursionlimit
from string import ascii_letters
letters = ascii_letters[:26]
from collections import defaultdict
#from functools import reduce
input = stdin.readline
#print = stdout.write
for _ in range(int(input())):
n, m = list(map(int, input().split()))
horizontal = [list(map(int, input().split())) for i in range(n)]
vertical = [list(map(int, input().split())) for i in range(m)]
vals = vertical[0]
first = vals[0]
pos = 0
for i in horizontal:
for g in range(m):
if i[g] == first:
pos = g
path = {}
order = {}
for ind, i in enumerate(vals):
path[i] = ind
for ind, i in enumerate(horizontal):
order[path[i[pos]]] = ind
for i in range(len(order)):
print(*horizontal[order[i]])
|
There are $n$ programmers that you want to split into several non-empty teams. The skill of the $i$-th programmer is $a_i$. You want to assemble the maximum number of teams from them. There is a restriction for each team: the number of programmers in the team multiplied by the minimum skill among all programmers in the team must be at least $x$.
Each programmer should belong to at most one team. Some programmers may be left without a team.
Calculate the maximum number of teams that you can assemble.
-----Input-----
The first line contains the integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains two integers $n$ and $x$ ($1 \le n \le 10^5; 1 \le x \le 10^9$) — the number of programmers and the restriction of team skill respectively.
The second line of each test case contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the skill of the $i$-th programmer.
The sum of $n$ over all inputs does not exceed $10^5$.
-----Output-----
For each test case print one integer — the maximum number of teams that you can assemble.
-----Example-----
Input
3
5 10
7 11 2 9 5
4 8
2 4 2 3
4 11
1 3 3 7
Output
2
1
0
|
__MULTITEST = True
## solve
def solve():
n, x = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
group = 0
ptr = n-1
members = 0
currentMin = int(1e10)
while ptr > -1:
currentMin = min(currentMin, a[ptr])
members += 1
if currentMin * members >= x:
group += 1
members = 0
currentMin = int(1e10)
ptr -= 1
print(group)
## main
def __starting_point():
t = (int(input()) if __MULTITEST else 1)
for tt in range(t):
solve();
__starting_point()
|
There are $n$ programmers that you want to split into several non-empty teams. The skill of the $i$-th programmer is $a_i$. You want to assemble the maximum number of teams from them. There is a restriction for each team: the number of programmers in the team multiplied by the minimum skill among all programmers in the team must be at least $x$.
Each programmer should belong to at most one team. Some programmers may be left without a team.
Calculate the maximum number of teams that you can assemble.
-----Input-----
The first line contains the integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains two integers $n$ and $x$ ($1 \le n \le 10^5; 1 \le x \le 10^9$) — the number of programmers and the restriction of team skill respectively.
The second line of each test case contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the skill of the $i$-th programmer.
The sum of $n$ over all inputs does not exceed $10^5$.
-----Output-----
For each test case print one integer — the maximum number of teams that you can assemble.
-----Example-----
Input
3
5 10
7 11 2 9 5
4 8
2 4 2 3
4 11
1 3 3 7
Output
2
1
0
|
import sys
import math
def II():
return int(sys.stdin.readline())
def LI():
return list(map(int, sys.stdin.readline().split()))
def MI():
return list(map(int, sys.stdin.readline().split()))
def SI():
return sys.stdin.readline().strip()
t = II()
for q in range(t):
n,x = MI()
a = sorted(LI())
a = a[::-1]
l = 0
count = 1
ans = 0
while l<n:
if count*a[l]>=x:
ans+=1
count = 1
else:
count+=1
l+=1
print(ans)
|
There are $n$ programmers that you want to split into several non-empty teams. The skill of the $i$-th programmer is $a_i$. You want to assemble the maximum number of teams from them. There is a restriction for each team: the number of programmers in the team multiplied by the minimum skill among all programmers in the team must be at least $x$.
Each programmer should belong to at most one team. Some programmers may be left without a team.
Calculate the maximum number of teams that you can assemble.
-----Input-----
The first line contains the integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains two integers $n$ and $x$ ($1 \le n \le 10^5; 1 \le x \le 10^9$) — the number of programmers and the restriction of team skill respectively.
The second line of each test case contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the skill of the $i$-th programmer.
The sum of $n$ over all inputs does not exceed $10^5$.
-----Output-----
For each test case print one integer — the maximum number of teams that you can assemble.
-----Example-----
Input
3
5 10
7 11 2 9 5
4 8
2 4 2 3
4 11
1 3 3 7
Output
2
1
0
|
for _ in range(int(input())):
a,x = [int(a) for a in input().split(' ')]
arr = [int(a) for a in input().split(' ')]
arr = sorted(arr,reverse=True)
cur_skill = x
teams = 0
cur_len = 0
for i in arr:
if i >= x:
teams+=1
continue
else:
cur_skill = i
cur_len+=1
if cur_len*cur_skill >= x:
teams+=1
cur_len = 0
cur_skill = x
print(teams)
|
There are $n$ programmers that you want to split into several non-empty teams. The skill of the $i$-th programmer is $a_i$. You want to assemble the maximum number of teams from them. There is a restriction for each team: the number of programmers in the team multiplied by the minimum skill among all programmers in the team must be at least $x$.
Each programmer should belong to at most one team. Some programmers may be left without a team.
Calculate the maximum number of teams that you can assemble.
-----Input-----
The first line contains the integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains two integers $n$ and $x$ ($1 \le n \le 10^5; 1 \le x \le 10^9$) — the number of programmers and the restriction of team skill respectively.
The second line of each test case contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the skill of the $i$-th programmer.
The sum of $n$ over all inputs does not exceed $10^5$.
-----Output-----
For each test case print one integer — the maximum number of teams that you can assemble.
-----Example-----
Input
3
5 10
7 11 2 9 5
4 8
2 4 2 3
4 11
1 3 3 7
Output
2
1
0
|
import bisect
import sys
import math
input = sys.stdin.readline
import functools
from collections import defaultdict
############ ---- Input Functions ---- ############
def inp():
return(int(input()))
def inlt():
return(list(map(int,input().split())))
def insr():
s = input()
return(list(s[:len(s) - 1]))
def invr():
return(list(map(int,input().split())))
############ ---- Solution ---- ############
def solve(case):
[n, x] = inlt()
aa = inlt()
aa.sort()
new_team = n-1
res = 0
for i in range(n-1, -1, -1):
if aa[i] * (new_team - i + 1) >= x:
res += 1
new_team = i-1
return res
if len(sys.argv) > 1 and sys.argv[1].startswith("input"):
f = open("./" + sys.argv[1], 'r')
input = f.readline
T = inp()
for i in range(T):
res = solve(i+1)
print(str(res))
|
There are $n$ programmers that you want to split into several non-empty teams. The skill of the $i$-th programmer is $a_i$. You want to assemble the maximum number of teams from them. There is a restriction for each team: the number of programmers in the team multiplied by the minimum skill among all programmers in the team must be at least $x$.
Each programmer should belong to at most one team. Some programmers may be left without a team.
Calculate the maximum number of teams that you can assemble.
-----Input-----
The first line contains the integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains two integers $n$ and $x$ ($1 \le n \le 10^5; 1 \le x \le 10^9$) — the number of programmers and the restriction of team skill respectively.
The second line of each test case contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the skill of the $i$-th programmer.
The sum of $n$ over all inputs does not exceed $10^5$.
-----Output-----
For each test case print one integer — the maximum number of teams that you can assemble.
-----Example-----
Input
3
5 10
7 11 2 9 5
4 8
2 4 2 3
4 11
1 3 3 7
Output
2
1
0
|
from sys import stdin, stdout
import math,sys
from itertools import permutations, combinations
from collections import defaultdict,deque,OrderedDict
from os import path
import bisect as bi
import heapq
def yes():print('YES')
def no():print('NO')
if (path.exists('input.txt')):
#------------------Sublime--------------------------------------#
sys.stdin=open('input.txt','r');sys.stdout=open('output.txt','w');
def I():return (int(input()))
def In():return(list(map(int,input().split())))
else:
#------------------PYPY FAst I/o--------------------------------#
def I():return (int(stdin.readline()))
def In():return(list(map(int,stdin.readline().split())))
def dict(a):
d={}
for x in a:
if d.get(x,-1)!=-1:
d[x]+=1
else:
d[x]=1
return d
def main():
try:
n,X=In()
l=list(In())
l.sort(reverse=True)
mi=-1
j,ans=0,0
for x in range(n):
if mi==-1:
mi=l[x]
j=1
else:
mi=min(mi,l[x])
j+=1
if mi*j>=X:
ans+=1
mi=-1
j=0
print(ans)
except:
pass
M = 998244353
P = 1000000007
def __starting_point():
for _ in range(I()):main()
#for _ in range(1):main()
__starting_point()
|
There are $n$ programmers that you want to split into several non-empty teams. The skill of the $i$-th programmer is $a_i$. You want to assemble the maximum number of teams from them. There is a restriction for each team: the number of programmers in the team multiplied by the minimum skill among all programmers in the team must be at least $x$.
Each programmer should belong to at most one team. Some programmers may be left without a team.
Calculate the maximum number of teams that you can assemble.
-----Input-----
The first line contains the integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains two integers $n$ and $x$ ($1 \le n \le 10^5; 1 \le x \le 10^9$) — the number of programmers and the restriction of team skill respectively.
The second line of each test case contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the skill of the $i$-th programmer.
The sum of $n$ over all inputs does not exceed $10^5$.
-----Output-----
For each test case print one integer — the maximum number of teams that you can assemble.
-----Example-----
Input
3
5 10
7 11 2 9 5
4 8
2 4 2 3
4 11
1 3 3 7
Output
2
1
0
|
from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush,heappop,heapify
import math
from collections import *
from functools import reduce,cmp_to_key
import sys
input = sys.stdin.readline
M = mod = 998244353
def factors(n):return sorted(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))
def inv_mod(n):return pow(n, mod - 2, mod)
def li():return [int(i) for i in input().rstrip('\n').split()]
def st():return input().rstrip('\n')
def val():return int(input().rstrip('\n'))
def li2():return [i for i in input().rstrip('\n')]
def li3():return [int(i) for i in input().rstrip('\n')]
for _ in range(val()):
n, x = li()
l = sorted(li())[::-1]
ans = curr = 0
mi = float('inf')
for i in range(n):
curr += 1
mi = min(mi,l[i])
if curr * mi >= x:
ans += 1
mi = float('inf')
curr = 0
print(ans)
|
There are $n$ programmers that you want to split into several non-empty teams. The skill of the $i$-th programmer is $a_i$. You want to assemble the maximum number of teams from them. There is a restriction for each team: the number of programmers in the team multiplied by the minimum skill among all programmers in the team must be at least $x$.
Each programmer should belong to at most one team. Some programmers may be left without a team.
Calculate the maximum number of teams that you can assemble.
-----Input-----
The first line contains the integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains two integers $n$ and $x$ ($1 \le n \le 10^5; 1 \le x \le 10^9$) — the number of programmers and the restriction of team skill respectively.
The second line of each test case contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the skill of the $i$-th programmer.
The sum of $n$ over all inputs does not exceed $10^5$.
-----Output-----
For each test case print one integer — the maximum number of teams that you can assemble.
-----Example-----
Input
3
5 10
7 11 2 9 5
4 8
2 4 2 3
4 11
1 3 3 7
Output
2
1
0
|
def solve(arr,n,x,ans):
arr.sort()
teams = 0
size = 0
while arr:
min_val = arr.pop()
size += 1
if min_val*size >= x:
teams += 1
size = 0
ans.append(teams)
def main():
t = int(input())
ans = []
for i in range(t):
n,x = list(map(int,input().split()))
arr = list(map(int,input().split()))
solve(arr,n,x,ans)
for i in ans:
print(i)
main()
|
There are $n$ programmers that you want to split into several non-empty teams. The skill of the $i$-th programmer is $a_i$. You want to assemble the maximum number of teams from them. There is a restriction for each team: the number of programmers in the team multiplied by the minimum skill among all programmers in the team must be at least $x$.
Each programmer should belong to at most one team. Some programmers may be left without a team.
Calculate the maximum number of teams that you can assemble.
-----Input-----
The first line contains the integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains two integers $n$ and $x$ ($1 \le n \le 10^5; 1 \le x \le 10^9$) — the number of programmers and the restriction of team skill respectively.
The second line of each test case contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the skill of the $i$-th programmer.
The sum of $n$ over all inputs does not exceed $10^5$.
-----Output-----
For each test case print one integer — the maximum number of teams that you can assemble.
-----Example-----
Input
3
5 10
7 11 2 9 5
4 8
2 4 2 3
4 11
1 3 3 7
Output
2
1
0
|
def solve(n, x, arr):
arr = sorted(arr)
res = 0
temp_length_so_far = 0
for i in range(n - 1, -1, -1):
temp_length_so_far += 1
if arr[i] * temp_length_so_far >= x:
res += 1
temp_length_so_far = 0
return res
T = int(input())
for _ in range(T):
n, x = list(map(int, input().split()))
arr = list(map(int, input().split()))
print(solve(n, x, arr))
|
There are $n$ programmers that you want to split into several non-empty teams. The skill of the $i$-th programmer is $a_i$. You want to assemble the maximum number of teams from them. There is a restriction for each team: the number of programmers in the team multiplied by the minimum skill among all programmers in the team must be at least $x$.
Each programmer should belong to at most one team. Some programmers may be left without a team.
Calculate the maximum number of teams that you can assemble.
-----Input-----
The first line contains the integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains two integers $n$ and $x$ ($1 \le n \le 10^5; 1 \le x \le 10^9$) — the number of programmers and the restriction of team skill respectively.
The second line of each test case contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the skill of the $i$-th programmer.
The sum of $n$ over all inputs does not exceed $10^5$.
-----Output-----
For each test case print one integer — the maximum number of teams that you can assemble.
-----Example-----
Input
3
5 10
7 11 2 9 5
4 8
2 4 2 3
4 11
1 3 3 7
Output
2
1
0
|
t = int(input())
for _ in range(t):
n, x = list(map(int, input().split()))
l = list(map(int, input().split()))
l.sort(reverse=True)
out = 0
count = 0
for v in l:
if v * (count + 1) >= x:
out += 1
count = 0
else:
count += 1
print(out)
|
There are $n$ programmers that you want to split into several non-empty teams. The skill of the $i$-th programmer is $a_i$. You want to assemble the maximum number of teams from them. There is a restriction for each team: the number of programmers in the team multiplied by the minimum skill among all programmers in the team must be at least $x$.
Each programmer should belong to at most one team. Some programmers may be left without a team.
Calculate the maximum number of teams that you can assemble.
-----Input-----
The first line contains the integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains two integers $n$ and $x$ ($1 \le n \le 10^5; 1 \le x \le 10^9$) — the number of programmers and the restriction of team skill respectively.
The second line of each test case contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the skill of the $i$-th programmer.
The sum of $n$ over all inputs does not exceed $10^5$.
-----Output-----
For each test case print one integer — the maximum number of teams that you can assemble.
-----Example-----
Input
3
5 10
7 11 2 9 5
4 8
2 4 2 3
4 11
1 3 3 7
Output
2
1
0
|
import math
from collections import deque
import sys
sys.setrecursionlimit(10**4)
def Divisors(n) :
l=[]
i = 2
while i <= math.sqrt(n):
if (n % i == 0) :
if (n // i == i) :
l.append(i)
else :
l.append(i)
l.append(n//i)
i = i + 1
return l
def SieveOfEratosthenes(n):
l=[]
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
for p in range(2, n+1):
if prime[p]:
l.append(p)
return l
def primeFactors(n):
l=[]
while n % 2 == 0:
l.append(2)
n = n / 2
for i in range(3,int(math.sqrt(n))+1,2):
while n % i== 0:
l.append(i)
n = n / i
if n > 2:
l.append(n)
return(l)
def Factors(n) :
result = []
for i in range(2,(int)(math.sqrt(n))+1) :
if (n % i == 0) :
if (i == (n/i)) :
result.append(i)
else :
result.append(i)
result.append(n//i)
result.append(1)
return result
def maxSubArraySum(a):
max_so_far = 0
max_ending_here = 0
size=len(a)
for i in range(0, size):
max_ending_here = max_ending_here + a[i]
if (max_so_far < abs(max_ending_here)):
max_so_far = max_ending_here
return max_so_far
def longestsubarray(arr, n, k):
current_count = 0
# this will contain length of
# longest subarray found
max_count = 0
for i in range(0, n, 1):
if (arr[i] % k != 0):
current_count += 1
else:
current_count = 0
max_count = max(current_count,
max_count)
return max_count
#print(SieveOfEratosthenes(100))
#print(Divisors(100))
#print(primeFactors(100))
#print(Factors(100))
#print(maxSubArraySum(a))
def main():
n,x=list(map(int,input().split()))
l=list(map(int,input().split()))
l.sort()
c=1
ans=0
for j in range(len(l)-1,-1,-1):
if l[j]*c >=x:
ans+=1
c=1
else:
c+=1
print(ans)
t=int(input())
for i in range(0,t):
main()
|
There are $n$ programmers that you want to split into several non-empty teams. The skill of the $i$-th programmer is $a_i$. You want to assemble the maximum number of teams from them. There is a restriction for each team: the number of programmers in the team multiplied by the minimum skill among all programmers in the team must be at least $x$.
Each programmer should belong to at most one team. Some programmers may be left without a team.
Calculate the maximum number of teams that you can assemble.
-----Input-----
The first line contains the integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains two integers $n$ and $x$ ($1 \le n \le 10^5; 1 \le x \le 10^9$) — the number of programmers and the restriction of team skill respectively.
The second line of each test case contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the skill of the $i$-th programmer.
The sum of $n$ over all inputs does not exceed $10^5$.
-----Output-----
For each test case print one integer — the maximum number of teams that you can assemble.
-----Example-----
Input
3
5 10
7 11 2 9 5
4 8
2 4 2 3
4 11
1 3 3 7
Output
2
1
0
|
input=__import__('sys').stdin.readline
for _ in range(int(input())):
n,x=map(int,input().split())
s=sorted(map(int,input().split()),reverse=True)
i=ans=0
c=1
while i<n:
if c*s[i]>=x:ans+=1;c=1
else:c+=1
i+=1
print(ans)
|
There are $n$ programmers that you want to split into several non-empty teams. The skill of the $i$-th programmer is $a_i$. You want to assemble the maximum number of teams from them. There is a restriction for each team: the number of programmers in the team multiplied by the minimum skill among all programmers in the team must be at least $x$.
Each programmer should belong to at most one team. Some programmers may be left without a team.
Calculate the maximum number of teams that you can assemble.
-----Input-----
The first line contains the integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains two integers $n$ and $x$ ($1 \le n \le 10^5; 1 \le x \le 10^9$) — the number of programmers and the restriction of team skill respectively.
The second line of each test case contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the skill of the $i$-th programmer.
The sum of $n$ over all inputs does not exceed $10^5$.
-----Output-----
For each test case print one integer — the maximum number of teams that you can assemble.
-----Example-----
Input
3
5 10
7 11 2 9 5
4 8
2 4 2 3
4 11
1 3 3 7
Output
2
1
0
|
import sys
import heapq, functools, collections
import math, random
from collections import Counter, defaultdict
# available on Google, not available on Codeforces
# import numpy as np
# import scipy
def solve(lst,x): # fix inputs here
console("----- solving ------")
lst = sorted(lst)[::-1]
cnt = 0
pdt = lst[0]
res = 0
for i in lst:
cnt += 1
pdt = min(i, pdt)
if cnt*pdt >= x:
res += 1
cnt = 0
pdt = i
# return a string (i.e. not a list or matrix)
return res
def console(*args): # the judge will not read these print statement
print('\033[36m', *args, '\033[0m', file=sys.stderr)
return
# fast read all
# sys.stdin.readlines()
for case_num in range(int(input())):
# read line as a string
# strr = input()
# read line as an integer
# k = int(input())
# read one line and parse each word as a string
# lst = input().split()
# read one line and parse each word as an integer
_,x = list(map(int,input().split()))
lst = list(map(int,input().split()))
# read matrix and parse as integers (after reading read nrows)
# lst = list(map(int,input().split()))
# nrows = lst[0] # index containing information, please change
# grid = []
# for _ in range(nrows):
# grid.append(list(map(int,input().split())))
res = solve(lst, x) # please change
# Google - case number required
# print("Case #{}: {}".format(case_num+1, res))
# Codeforces - no case number required
print(res)
|
There are $n$ programmers that you want to split into several non-empty teams. The skill of the $i$-th programmer is $a_i$. You want to assemble the maximum number of teams from them. There is a restriction for each team: the number of programmers in the team multiplied by the minimum skill among all programmers in the team must be at least $x$.
Each programmer should belong to at most one team. Some programmers may be left without a team.
Calculate the maximum number of teams that you can assemble.
-----Input-----
The first line contains the integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains two integers $n$ and $x$ ($1 \le n \le 10^5; 1 \le x \le 10^9$) — the number of programmers and the restriction of team skill respectively.
The second line of each test case contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the skill of the $i$-th programmer.
The sum of $n$ over all inputs does not exceed $10^5$.
-----Output-----
For each test case print one integer — the maximum number of teams that you can assemble.
-----Example-----
Input
3
5 10
7 11 2 9 5
4 8
2 4 2 3
4 11
1 3 3 7
Output
2
1
0
|
t=int(input())
for _ in range(t):
n,x=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
a.reverse()
count=0
ans=0
for i in range(n):
count+=1
if count*a[i]>=x:
ans+=1
count=0
print(ans)
|
There are $n$ programmers that you want to split into several non-empty teams. The skill of the $i$-th programmer is $a_i$. You want to assemble the maximum number of teams from them. There is a restriction for each team: the number of programmers in the team multiplied by the minimum skill among all programmers in the team must be at least $x$.
Each programmer should belong to at most one team. Some programmers may be left without a team.
Calculate the maximum number of teams that you can assemble.
-----Input-----
The first line contains the integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains two integers $n$ and $x$ ($1 \le n \le 10^5; 1 \le x \le 10^9$) — the number of programmers and the restriction of team skill respectively.
The second line of each test case contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the skill of the $i$-th programmer.
The sum of $n$ over all inputs does not exceed $10^5$.
-----Output-----
For each test case print one integer — the maximum number of teams that you can assemble.
-----Example-----
Input
3
5 10
7 11 2 9 5
4 8
2 4 2 3
4 11
1 3 3 7
Output
2
1
0
|
for kek in range(int(input())):
(n, x) = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
for i in range(n):
f = 0
if x % a[i] != 0:
f += 1
a[i] = x // a[i] + f
a.sort()
ans = 0
com = 1
for i in a:
if i == com:
ans += 1
com = 1
else:
com += 1
print(ans)
|
There are $n$ programmers that you want to split into several non-empty teams. The skill of the $i$-th programmer is $a_i$. You want to assemble the maximum number of teams from them. There is a restriction for each team: the number of programmers in the team multiplied by the minimum skill among all programmers in the team must be at least $x$.
Each programmer should belong to at most one team. Some programmers may be left without a team.
Calculate the maximum number of teams that you can assemble.
-----Input-----
The first line contains the integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains two integers $n$ and $x$ ($1 \le n \le 10^5; 1 \le x \le 10^9$) — the number of programmers and the restriction of team skill respectively.
The second line of each test case contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the skill of the $i$-th programmer.
The sum of $n$ over all inputs does not exceed $10^5$.
-----Output-----
For each test case print one integer — the maximum number of teams that you can assemble.
-----Example-----
Input
3
5 10
7 11 2 9 5
4 8
2 4 2 3
4 11
1 3 3 7
Output
2
1
0
|
for _ in range(int(input())):
n, k = map(int, input().split())
arr = list(map(int, input().split()))
arr.sort(reverse=True)
i = 0
teams = 0
teamMem=0
while (i < len(arr)):
if ((teamMem+1) * arr[i]>=k):
teams += 1
teamMem = 0
else:
teamMem += 1
i += 1
# print(teams,teamMem)
print(teams)
|
There are $n$ programmers that you want to split into several non-empty teams. The skill of the $i$-th programmer is $a_i$. You want to assemble the maximum number of teams from them. There is a restriction for each team: the number of programmers in the team multiplied by the minimum skill among all programmers in the team must be at least $x$.
Each programmer should belong to at most one team. Some programmers may be left without a team.
Calculate the maximum number of teams that you can assemble.
-----Input-----
The first line contains the integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains two integers $n$ and $x$ ($1 \le n \le 10^5; 1 \le x \le 10^9$) — the number of programmers and the restriction of team skill respectively.
The second line of each test case contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the skill of the $i$-th programmer.
The sum of $n$ over all inputs does not exceed $10^5$.
-----Output-----
For each test case print one integer — the maximum number of teams that you can assemble.
-----Example-----
Input
3
5 10
7 11 2 9 5
4 8
2 4 2 3
4 11
1 3 3 7
Output
2
1
0
|
import sys
input = lambda : sys.stdin.readline().rstrip()
for _ in range(int(input())):
n,x=map(int,input().split())
a = sorted([int(x) for x in input().split()])
ans = 0
while a and a[-1]>=x:
ans +=1
a.pop()
i=len(a)-1
l=1
while i>=0:
if a[i]*l>=x:
ans += 1
l=0
i-=1
l+=1
print(ans)
|
Assume that you have $k$ one-dimensional segments $s_1, s_2, \dots s_k$ (each segment is denoted by two integers — its endpoints). Then you can build the following graph on these segments. The graph consists of $k$ vertexes, and there is an edge between the $i$-th and the $j$-th vertexes ($i \neq j$) if and only if the segments $s_i$ and $s_j$ intersect (there exists at least one point that belongs to both of them).
For example, if $s_1 = [1, 6], s_2 = [8, 20], s_3 = [4, 10], s_4 = [2, 13], s_5 = [17, 18]$, then the resulting graph is the following: [Image]
A tree of size $m$ is good if it is possible to choose $m$ one-dimensional segments so that the graph built on these segments coincides with this tree.
You are given a tree, you have to find its good subtree with maximum possible size. Recall that a subtree is a connected subgraph of a tree.
Note that you have to answer $q$ independent queries.
-----Input-----
The first line contains one integer $q$ ($1 \le q \le 15 \cdot 10^4$) — the number of the queries.
The first line of each query contains one integer $n$ ($2 \le n \le 3 \cdot 10^5$) — the number of vertices in the tree.
Each of the next $n - 1$ lines contains two integers $x$ and $y$ ($1 \le x, y \le n$) denoting an edge between vertices $x$ and $y$. It is guaranteed that the given graph is a tree.
It is guaranteed that the sum of all $n$ does not exceed $3 \cdot 10^5$.
-----Output-----
For each query print one integer — the maximum size of a good subtree of the given tree.
-----Example-----
Input
1
10
1 2
1 3
1 4
2 5
2 6
3 7
3 8
4 9
4 10
Output
8
-----Note-----
In the first query there is a good subtree of size $8$. The vertices belonging to this subtree are ${9, 4, 10, 2, 5, 1, 6, 3}$.
|
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n = int(input())
ab = [list(map(int,input().split())) for i in range(n-1)]
graph = [[] for i in range(n+1)]
deg = [0]*(n+1)
for a,b in ab:
graph[a].append(b)
graph[b].append(a)
deg[a] += 1
deg[b] += 1
pnt = [max(deg[i]-1,1) for i in range(n+1)]
root = 1
stack = [root]
dist = [0]*(n+1)
dist[root] = pnt[root]
while stack:
x = stack.pop()
for y in graph[x]:
if dist[y] == 0:
dist[y] = dist[x]+pnt[y]
stack.append(y)
far = dist.index(max(dist))
root = far
stack = [root]
dist = [0]*(n+1)
dist[root] = pnt[root]
while stack:
x = stack.pop()
for y in graph[x]:
if dist[y] == 0:
dist[y] = dist[x]+pnt[y]
stack.append(y)
print(max(dist))
|
Ayoub thinks that he is a very smart person, so he created a function $f(s)$, where $s$ is a binary string (a string which contains only symbols "0" and "1"). The function $f(s)$ is equal to the number of substrings in the string $s$ that contains at least one symbol, that is equal to "1".
More formally, $f(s)$ is equal to the number of pairs of integers $(l, r)$, such that $1 \leq l \leq r \leq |s|$ (where $|s|$ is equal to the length of string $s$), such that at least one of the symbols $s_l, s_{l+1}, \ldots, s_r$ is equal to "1".
For example, if $s = $"01010" then $f(s) = 12$, because there are $12$ such pairs $(l, r)$: $(1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)$.
Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers $n$ and $m$ and asked him this problem. For all binary strings $s$ of length $n$ which contains exactly $m$ symbols equal to "1", find the maximum value of $f(s)$.
Mahmoud couldn't solve the problem so he asked you for help. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) — the number of test cases. The description of the test cases follows.
The only line for each test case contains two integers $n$, $m$ ($1 \leq n \leq 10^{9}$, $0 \leq m \leq n$) — the length of the string and the number of symbols equal to "1" in it.
-----Output-----
For every test case print one integer number — the maximum value of $f(s)$ over all strings $s$ of length $n$, which has exactly $m$ symbols, equal to "1".
-----Example-----
Input
5
3 1
3 2
3 3
4 0
5 2
Output
4
5
6
0
12
-----Note-----
In the first test case, there exists only $3$ strings of length $3$, which has exactly $1$ symbol, equal to "1". These strings are: $s_1 = $"100", $s_2 = $"010", $s_3 = $"001". The values of $f$ for them are: $f(s_1) = 3, f(s_2) = 4, f(s_3) = 3$, so the maximum value is $4$ and the answer is $4$.
In the second test case, the string $s$ with the maximum value is "101".
In the third test case, the string $s$ with the maximum value is "111".
In the fourth test case, the only string $s$ of length $4$, which has exactly $0$ symbols, equal to "1" is "0000" and the value of $f$ for that string is $0$, so the answer is $0$.
In the fifth test case, the string $s$ with the maximum value is "01010" and it is described as an example in the problem statement.
|
import sys
input = sys.stdin.readline
t=int(input())
def calc(x):
return x*(x+1)//2
for test in range(t):
n,m=list(map(int,input().split()))
ANS=calc(n)
k=n-m
q,mod=divmod(k,m+1)
ANS-=calc(q+1)*mod+calc(q)*(m+1-mod)
print(ANS)
|
Ayoub thinks that he is a very smart person, so he created a function $f(s)$, where $s$ is a binary string (a string which contains only symbols "0" and "1"). The function $f(s)$ is equal to the number of substrings in the string $s$ that contains at least one symbol, that is equal to "1".
More formally, $f(s)$ is equal to the number of pairs of integers $(l, r)$, such that $1 \leq l \leq r \leq |s|$ (where $|s|$ is equal to the length of string $s$), such that at least one of the symbols $s_l, s_{l+1}, \ldots, s_r$ is equal to "1".
For example, if $s = $"01010" then $f(s) = 12$, because there are $12$ such pairs $(l, r)$: $(1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)$.
Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers $n$ and $m$ and asked him this problem. For all binary strings $s$ of length $n$ which contains exactly $m$ symbols equal to "1", find the maximum value of $f(s)$.
Mahmoud couldn't solve the problem so he asked you for help. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) — the number of test cases. The description of the test cases follows.
The only line for each test case contains two integers $n$, $m$ ($1 \leq n \leq 10^{9}$, $0 \leq m \leq n$) — the length of the string and the number of symbols equal to "1" in it.
-----Output-----
For every test case print one integer number — the maximum value of $f(s)$ over all strings $s$ of length $n$, which has exactly $m$ symbols, equal to "1".
-----Example-----
Input
5
3 1
3 2
3 3
4 0
5 2
Output
4
5
6
0
12
-----Note-----
In the first test case, there exists only $3$ strings of length $3$, which has exactly $1$ symbol, equal to "1". These strings are: $s_1 = $"100", $s_2 = $"010", $s_3 = $"001". The values of $f$ for them are: $f(s_1) = 3, f(s_2) = 4, f(s_3) = 3$, so the maximum value is $4$ and the answer is $4$.
In the second test case, the string $s$ with the maximum value is "101".
In the third test case, the string $s$ with the maximum value is "111".
In the fourth test case, the only string $s$ of length $4$, which has exactly $0$ symbols, equal to "1" is "0000" and the value of $f$ for that string is $0$, so the answer is $0$.
In the fifth test case, the string $s$ with the maximum value is "01010" and it is described as an example in the problem statement.
|
import sys
for _ in range(int(sys.stdin.readline())):
n, m = map(int, sys.stdin.readline().split())
s = (n-m)//(m+1)
none = (m+1-(n-m)%(m+1))*s*(s+1)//2 + ((n-m)%(m+1))*(s+1)*(s+2)//2
print((n+1)*n//2 - none)
|
Ayoub thinks that he is a very smart person, so he created a function $f(s)$, where $s$ is a binary string (a string which contains only symbols "0" and "1"). The function $f(s)$ is equal to the number of substrings in the string $s$ that contains at least one symbol, that is equal to "1".
More formally, $f(s)$ is equal to the number of pairs of integers $(l, r)$, such that $1 \leq l \leq r \leq |s|$ (where $|s|$ is equal to the length of string $s$), such that at least one of the symbols $s_l, s_{l+1}, \ldots, s_r$ is equal to "1".
For example, if $s = $"01010" then $f(s) = 12$, because there are $12$ such pairs $(l, r)$: $(1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)$.
Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers $n$ and $m$ and asked him this problem. For all binary strings $s$ of length $n$ which contains exactly $m$ symbols equal to "1", find the maximum value of $f(s)$.
Mahmoud couldn't solve the problem so he asked you for help. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) — the number of test cases. The description of the test cases follows.
The only line for each test case contains two integers $n$, $m$ ($1 \leq n \leq 10^{9}$, $0 \leq m \leq n$) — the length of the string and the number of symbols equal to "1" in it.
-----Output-----
For every test case print one integer number — the maximum value of $f(s)$ over all strings $s$ of length $n$, which has exactly $m$ symbols, equal to "1".
-----Example-----
Input
5
3 1
3 2
3 3
4 0
5 2
Output
4
5
6
0
12
-----Note-----
In the first test case, there exists only $3$ strings of length $3$, which has exactly $1$ symbol, equal to "1". These strings are: $s_1 = $"100", $s_2 = $"010", $s_3 = $"001". The values of $f$ for them are: $f(s_1) = 3, f(s_2) = 4, f(s_3) = 3$, so the maximum value is $4$ and the answer is $4$.
In the second test case, the string $s$ with the maximum value is "101".
In the third test case, the string $s$ with the maximum value is "111".
In the fourth test case, the only string $s$ of length $4$, which has exactly $0$ symbols, equal to "1" is "0000" and the value of $f$ for that string is $0$, so the answer is $0$.
In the fifth test case, the string $s$ with the maximum value is "01010" and it is described as an example in the problem statement.
|
def main():
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
N, M = list(map(int, input().split()))
S = ((N+1) * N)//2
zero = N - M
num = zero // (M+1)
major = zero % (M+1)
minor = M+1 - major
S -= major * (((num+2)*(num+1)) // 2)
S -= minor * (((num+1)*num) // 2)
print(S)
def __starting_point():
main()
__starting_point()
|
Ayoub thinks that he is a very smart person, so he created a function $f(s)$, where $s$ is a binary string (a string which contains only symbols "0" and "1"). The function $f(s)$ is equal to the number of substrings in the string $s$ that contains at least one symbol, that is equal to "1".
More formally, $f(s)$ is equal to the number of pairs of integers $(l, r)$, such that $1 \leq l \leq r \leq |s|$ (where $|s|$ is equal to the length of string $s$), such that at least one of the symbols $s_l, s_{l+1}, \ldots, s_r$ is equal to "1".
For example, if $s = $"01010" then $f(s) = 12$, because there are $12$ such pairs $(l, r)$: $(1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)$.
Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers $n$ and $m$ and asked him this problem. For all binary strings $s$ of length $n$ which contains exactly $m$ symbols equal to "1", find the maximum value of $f(s)$.
Mahmoud couldn't solve the problem so he asked you for help. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) — the number of test cases. The description of the test cases follows.
The only line for each test case contains two integers $n$, $m$ ($1 \leq n \leq 10^{9}$, $0 \leq m \leq n$) — the length of the string and the number of symbols equal to "1" in it.
-----Output-----
For every test case print one integer number — the maximum value of $f(s)$ over all strings $s$ of length $n$, which has exactly $m$ symbols, equal to "1".
-----Example-----
Input
5
3 1
3 2
3 3
4 0
5 2
Output
4
5
6
0
12
-----Note-----
In the first test case, there exists only $3$ strings of length $3$, which has exactly $1$ symbol, equal to "1". These strings are: $s_1 = $"100", $s_2 = $"010", $s_3 = $"001". The values of $f$ for them are: $f(s_1) = 3, f(s_2) = 4, f(s_3) = 3$, so the maximum value is $4$ and the answer is $4$.
In the second test case, the string $s$ with the maximum value is "101".
In the third test case, the string $s$ with the maximum value is "111".
In the fourth test case, the only string $s$ of length $4$, which has exactly $0$ symbols, equal to "1" is "0000" and the value of $f$ for that string is $0$, so the answer is $0$.
In the fifth test case, the string $s$ with the maximum value is "01010" and it is described as an example in the problem statement.
|
import sys
readline = sys.stdin.readline
T = int(readline())
Ans = [None]*T
def calc(d):
return d*(d+1)//2
for qu in range(T):
N, M = list(map(int, readline().split()))
seg = M+1
leng = N-M
Ans[qu] = calc(N) - calc(leng//seg) * (seg-leng%seg) - calc(leng//seg+1) *(leng%seg)
print('\n'.join(map(str, Ans)))
|
Ayoub thinks that he is a very smart person, so he created a function $f(s)$, where $s$ is a binary string (a string which contains only symbols "0" and "1"). The function $f(s)$ is equal to the number of substrings in the string $s$ that contains at least one symbol, that is equal to "1".
More formally, $f(s)$ is equal to the number of pairs of integers $(l, r)$, such that $1 \leq l \leq r \leq |s|$ (where $|s|$ is equal to the length of string $s$), such that at least one of the symbols $s_l, s_{l+1}, \ldots, s_r$ is equal to "1".
For example, if $s = $"01010" then $f(s) = 12$, because there are $12$ such pairs $(l, r)$: $(1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)$.
Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers $n$ and $m$ and asked him this problem. For all binary strings $s$ of length $n$ which contains exactly $m$ symbols equal to "1", find the maximum value of $f(s)$.
Mahmoud couldn't solve the problem so he asked you for help. Can you help him?
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) — the number of test cases. The description of the test cases follows.
The only line for each test case contains two integers $n$, $m$ ($1 \leq n \leq 10^{9}$, $0 \leq m \leq n$) — the length of the string and the number of symbols equal to "1" in it.
-----Output-----
For every test case print one integer number — the maximum value of $f(s)$ over all strings $s$ of length $n$, which has exactly $m$ symbols, equal to "1".
-----Example-----
Input
5
3 1
3 2
3 3
4 0
5 2
Output
4
5
6
0
12
-----Note-----
In the first test case, there exists only $3$ strings of length $3$, which has exactly $1$ symbol, equal to "1". These strings are: $s_1 = $"100", $s_2 = $"010", $s_3 = $"001". The values of $f$ for them are: $f(s_1) = 3, f(s_2) = 4, f(s_3) = 3$, so the maximum value is $4$ and the answer is $4$.
In the second test case, the string $s$ with the maximum value is "101".
In the third test case, the string $s$ with the maximum value is "111".
In the fourth test case, the only string $s$ of length $4$, which has exactly $0$ symbols, equal to "1" is "0000" and the value of $f$ for that string is $0$, so the answer is $0$.
In the fifth test case, the string $s$ with the maximum value is "01010" and it is described as an example in the problem statement.
|
def main():
from sys import stdin, stdout
for _ in range(int(stdin.readline())):
n, m = list(map(int, stdin.readline().split()))
n += 1
m += 1
div, mod = divmod(n, m)
stdout.write(f'{(n ** 2 - div ** 2 * (m - mod) - (div + 1) ** 2 * mod) // 2}\n')
main()
|
Reminder: the median of the array $[a_1, a_2, \dots, a_{2k+1}]$ of odd number of elements is defined as follows: let $[b_1, b_2, \dots, b_{2k+1}]$ be the elements of the array in the sorted order. Then median of this array is equal to $b_{k+1}$.
There are $2n$ students, the $i$-th student has skill level $a_i$. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the $2$ classes such that each class has odd number of students (not divisible by $2$). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students halved.
The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2 n}$ ($1 \le a_i \le 10^9$) — skill levels of students.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
-----Example-----
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
-----Note-----
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be $|1 - 1| = 0$.
In the second test, one of the possible partitions is to make the first class of students with skill levels $[6, 4, 2]$, so that the skill level of the first class will be $4$, and second with $[5, 1, 3]$, so that the skill level of the second class will be $3$. Absolute difference will be $|4 - 3| = 1$.
Note that you can't assign like $[2, 3]$, $[6, 5, 4, 1]$ or $[]$, $[6, 5, 4, 1, 2, 3]$ because classes have even number of students.
$[2]$, $[1, 3, 4]$ is also not possible because students with skills $5$ and $6$ aren't assigned to a class.
In the third test you can assign the students in the following way: $[3, 4, 13, 13, 20], [2, 5, 8, 16, 17]$ or $[3, 8, 17], [2, 4, 5, 13, 13, 16, 20]$. Both divisions give minimal possible absolute difference.
|
for _ in range(int(input())):
n = int(input())
ar = list(map(int, input().split()))
ar.sort()
print(abs(ar[n] - ar[n - 1]))
|
Reminder: the median of the array $[a_1, a_2, \dots, a_{2k+1}]$ of odd number of elements is defined as follows: let $[b_1, b_2, \dots, b_{2k+1}]$ be the elements of the array in the sorted order. Then median of this array is equal to $b_{k+1}$.
There are $2n$ students, the $i$-th student has skill level $a_i$. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the $2$ classes such that each class has odd number of students (not divisible by $2$). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students halved.
The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2 n}$ ($1 \le a_i \le 10^9$) — skill levels of students.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
-----Example-----
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
-----Note-----
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be $|1 - 1| = 0$.
In the second test, one of the possible partitions is to make the first class of students with skill levels $[6, 4, 2]$, so that the skill level of the first class will be $4$, and second with $[5, 1, 3]$, so that the skill level of the second class will be $3$. Absolute difference will be $|4 - 3| = 1$.
Note that you can't assign like $[2, 3]$, $[6, 5, 4, 1]$ or $[]$, $[6, 5, 4, 1, 2, 3]$ because classes have even number of students.
$[2]$, $[1, 3, 4]$ is also not possible because students with skills $5$ and $6$ aren't assigned to a class.
In the third test you can assign the students in the following way: $[3, 4, 13, 13, 20], [2, 5, 8, 16, 17]$ or $[3, 8, 17], [2, 4, 5, 13, 13, 16, 20]$. Both divisions give minimal possible absolute difference.
|
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
a = sorted(a)
print(a[n]- a[n - 1])
|
Reminder: the median of the array $[a_1, a_2, \dots, a_{2k+1}]$ of odd number of elements is defined as follows: let $[b_1, b_2, \dots, b_{2k+1}]$ be the elements of the array in the sorted order. Then median of this array is equal to $b_{k+1}$.
There are $2n$ students, the $i$-th student has skill level $a_i$. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the $2$ classes such that each class has odd number of students (not divisible by $2$). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students halved.
The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2 n}$ ($1 \le a_i \le 10^9$) — skill levels of students.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
-----Example-----
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
-----Note-----
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be $|1 - 1| = 0$.
In the second test, one of the possible partitions is to make the first class of students with skill levels $[6, 4, 2]$, so that the skill level of the first class will be $4$, and second with $[5, 1, 3]$, so that the skill level of the second class will be $3$. Absolute difference will be $|4 - 3| = 1$.
Note that you can't assign like $[2, 3]$, $[6, 5, 4, 1]$ or $[]$, $[6, 5, 4, 1, 2, 3]$ because classes have even number of students.
$[2]$, $[1, 3, 4]$ is also not possible because students with skills $5$ and $6$ aren't assigned to a class.
In the third test you can assign the students in the following way: $[3, 4, 13, 13, 20], [2, 5, 8, 16, 17]$ or $[3, 8, 17], [2, 4, 5, 13, 13, 16, 20]$. Both divisions give minimal possible absolute difference.
|
def iinput():
return [int(x) for x in input().split()]
def main():
n = int(input())
data = iinput()
data.sort()
return abs(data[n] - data[n - 1])
for t in range(int(input())):
print(main())
|
Reminder: the median of the array $[a_1, a_2, \dots, a_{2k+1}]$ of odd number of elements is defined as follows: let $[b_1, b_2, \dots, b_{2k+1}]$ be the elements of the array in the sorted order. Then median of this array is equal to $b_{k+1}$.
There are $2n$ students, the $i$-th student has skill level $a_i$. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the $2$ classes such that each class has odd number of students (not divisible by $2$). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students halved.
The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2 n}$ ($1 \le a_i \le 10^9$) — skill levels of students.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
-----Example-----
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
-----Note-----
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be $|1 - 1| = 0$.
In the second test, one of the possible partitions is to make the first class of students with skill levels $[6, 4, 2]$, so that the skill level of the first class will be $4$, and second with $[5, 1, 3]$, so that the skill level of the second class will be $3$. Absolute difference will be $|4 - 3| = 1$.
Note that you can't assign like $[2, 3]$, $[6, 5, 4, 1]$ or $[]$, $[6, 5, 4, 1, 2, 3]$ because classes have even number of students.
$[2]$, $[1, 3, 4]$ is also not possible because students with skills $5$ and $6$ aren't assigned to a class.
In the third test you can assign the students in the following way: $[3, 4, 13, 13, 20], [2, 5, 8, 16, 17]$ or $[3, 8, 17], [2, 4, 5, 13, 13, 16, 20]$. Both divisions give minimal possible absolute difference.
|
import sys
T = int(sys.stdin.readline().strip())
for t in range (0, T):
n = int(sys.stdin.readline().strip())
a = list(map(int, sys.stdin.readline().strip().split()))
a.sort()
print(a[n]-a[n-1])
|
Reminder: the median of the array $[a_1, a_2, \dots, a_{2k+1}]$ of odd number of elements is defined as follows: let $[b_1, b_2, \dots, b_{2k+1}]$ be the elements of the array in the sorted order. Then median of this array is equal to $b_{k+1}$.
There are $2n$ students, the $i$-th student has skill level $a_i$. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the $2$ classes such that each class has odd number of students (not divisible by $2$). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students halved.
The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2 n}$ ($1 \le a_i \le 10^9$) — skill levels of students.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
-----Example-----
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
-----Note-----
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be $|1 - 1| = 0$.
In the second test, one of the possible partitions is to make the first class of students with skill levels $[6, 4, 2]$, so that the skill level of the first class will be $4$, and second with $[5, 1, 3]$, so that the skill level of the second class will be $3$. Absolute difference will be $|4 - 3| = 1$.
Note that you can't assign like $[2, 3]$, $[6, 5, 4, 1]$ or $[]$, $[6, 5, 4, 1, 2, 3]$ because classes have even number of students.
$[2]$, $[1, 3, 4]$ is also not possible because students with skills $5$ and $6$ aren't assigned to a class.
In the third test you can assign the students in the following way: $[3, 4, 13, 13, 20], [2, 5, 8, 16, 17]$ or $[3, 8, 17], [2, 4, 5, 13, 13, 16, 20]$. Both divisions give minimal possible absolute difference.
|
import math, collections, sys
input = sys.stdin.readline
def case():
n = int(input())
a = list(map(int, input().split()))
a.sort()
print(a[n]-a[n-1])
for _ in range(int(input())):
case()
|
Reminder: the median of the array $[a_1, a_2, \dots, a_{2k+1}]$ of odd number of elements is defined as follows: let $[b_1, b_2, \dots, b_{2k+1}]$ be the elements of the array in the sorted order. Then median of this array is equal to $b_{k+1}$.
There are $2n$ students, the $i$-th student has skill level $a_i$. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the $2$ classes such that each class has odd number of students (not divisible by $2$). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students halved.
The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2 n}$ ($1 \le a_i \le 10^9$) — skill levels of students.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
-----Example-----
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
-----Note-----
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be $|1 - 1| = 0$.
In the second test, one of the possible partitions is to make the first class of students with skill levels $[6, 4, 2]$, so that the skill level of the first class will be $4$, and second with $[5, 1, 3]$, so that the skill level of the second class will be $3$. Absolute difference will be $|4 - 3| = 1$.
Note that you can't assign like $[2, 3]$, $[6, 5, 4, 1]$ or $[]$, $[6, 5, 4, 1, 2, 3]$ because classes have even number of students.
$[2]$, $[1, 3, 4]$ is also not possible because students with skills $5$ and $6$ aren't assigned to a class.
In the third test you can assign the students in the following way: $[3, 4, 13, 13, 20], [2, 5, 8, 16, 17]$ or $[3, 8, 17], [2, 4, 5, 13, 13, 16, 20]$. Both divisions give minimal possible absolute difference.
|
for nt in range(int(input())):
n=int(input())
l=list(map(int,input().split()))
if n==1:
print (abs(l[0]-l[1]))
continue
l.sort()
print (abs(l[n]-l[n-1]))
|
Reminder: the median of the array $[a_1, a_2, \dots, a_{2k+1}]$ of odd number of elements is defined as follows: let $[b_1, b_2, \dots, b_{2k+1}]$ be the elements of the array in the sorted order. Then median of this array is equal to $b_{k+1}$.
There are $2n$ students, the $i$-th student has skill level $a_i$. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the $2$ classes such that each class has odd number of students (not divisible by $2$). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students halved.
The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2 n}$ ($1 \le a_i \le 10^9$) — skill levels of students.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
-----Example-----
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
-----Note-----
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be $|1 - 1| = 0$.
In the second test, one of the possible partitions is to make the first class of students with skill levels $[6, 4, 2]$, so that the skill level of the first class will be $4$, and second with $[5, 1, 3]$, so that the skill level of the second class will be $3$. Absolute difference will be $|4 - 3| = 1$.
Note that you can't assign like $[2, 3]$, $[6, 5, 4, 1]$ or $[]$, $[6, 5, 4, 1, 2, 3]$ because classes have even number of students.
$[2]$, $[1, 3, 4]$ is also not possible because students with skills $5$ and $6$ aren't assigned to a class.
In the third test you can assign the students in the following way: $[3, 4, 13, 13, 20], [2, 5, 8, 16, 17]$ or $[3, 8, 17], [2, 4, 5, 13, 13, 16, 20]$. Both divisions give minimal possible absolute difference.
|
import math
for _ in range(int(input())):
n=int(input())
li=list(map(int,input().split()))
li.sort()
print(li[n]-li[n-1])
|
Reminder: the median of the array $[a_1, a_2, \dots, a_{2k+1}]$ of odd number of elements is defined as follows: let $[b_1, b_2, \dots, b_{2k+1}]$ be the elements of the array in the sorted order. Then median of this array is equal to $b_{k+1}$.
There are $2n$ students, the $i$-th student has skill level $a_i$. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the $2$ classes such that each class has odd number of students (not divisible by $2$). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students halved.
The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2 n}$ ($1 \le a_i \le 10^9$) — skill levels of students.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
-----Example-----
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
-----Note-----
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be $|1 - 1| = 0$.
In the second test, one of the possible partitions is to make the first class of students with skill levels $[6, 4, 2]$, so that the skill level of the first class will be $4$, and second with $[5, 1, 3]$, so that the skill level of the second class will be $3$. Absolute difference will be $|4 - 3| = 1$.
Note that you can't assign like $[2, 3]$, $[6, 5, 4, 1]$ or $[]$, $[6, 5, 4, 1, 2, 3]$ because classes have even number of students.
$[2]$, $[1, 3, 4]$ is also not possible because students with skills $5$ and $6$ aren't assigned to a class.
In the third test you can assign the students in the following way: $[3, 4, 13, 13, 20], [2, 5, 8, 16, 17]$ or $[3, 8, 17], [2, 4, 5, 13, 13, 16, 20]$. Both divisions give minimal possible absolute difference.
|
def main():
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
N = int(input())
A = list(map(int, input().split()))
A.sort()
print(A[N] - A[N-1])
def __starting_point():
main()
__starting_point()
|
Reminder: the median of the array $[a_1, a_2, \dots, a_{2k+1}]$ of odd number of elements is defined as follows: let $[b_1, b_2, \dots, b_{2k+1}]$ be the elements of the array in the sorted order. Then median of this array is equal to $b_{k+1}$.
There are $2n$ students, the $i$-th student has skill level $a_i$. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the $2$ classes such that each class has odd number of students (not divisible by $2$). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students halved.
The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2 n}$ ($1 \le a_i \le 10^9$) — skill levels of students.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
-----Example-----
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
-----Note-----
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be $|1 - 1| = 0$.
In the second test, one of the possible partitions is to make the first class of students with skill levels $[6, 4, 2]$, so that the skill level of the first class will be $4$, and second with $[5, 1, 3]$, so that the skill level of the second class will be $3$. Absolute difference will be $|4 - 3| = 1$.
Note that you can't assign like $[2, 3]$, $[6, 5, 4, 1]$ or $[]$, $[6, 5, 4, 1, 2, 3]$ because classes have even number of students.
$[2]$, $[1, 3, 4]$ is also not possible because students with skills $5$ and $6$ aren't assigned to a class.
In the third test you can assign the students in the following way: $[3, 4, 13, 13, 20], [2, 5, 8, 16, 17]$ or $[3, 8, 17], [2, 4, 5, 13, 13, 16, 20]$. Both divisions give minimal possible absolute difference.
|
t=int(input())
for i in range(t):
n=int(input())
a=[int (i) for i in input().split()]
a=sorted(a)
print(a[n]-a[n-1])
|
Reminder: the median of the array $[a_1, a_2, \dots, a_{2k+1}]$ of odd number of elements is defined as follows: let $[b_1, b_2, \dots, b_{2k+1}]$ be the elements of the array in the sorted order. Then median of this array is equal to $b_{k+1}$.
There are $2n$ students, the $i$-th student has skill level $a_i$. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the $2$ classes such that each class has odd number of students (not divisible by $2$). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students halved.
The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2 n}$ ($1 \le a_i \le 10^9$) — skill levels of students.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
-----Example-----
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
-----Note-----
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be $|1 - 1| = 0$.
In the second test, one of the possible partitions is to make the first class of students with skill levels $[6, 4, 2]$, so that the skill level of the first class will be $4$, and second with $[5, 1, 3]$, so that the skill level of the second class will be $3$. Absolute difference will be $|4 - 3| = 1$.
Note that you can't assign like $[2, 3]$, $[6, 5, 4, 1]$ or $[]$, $[6, 5, 4, 1, 2, 3]$ because classes have even number of students.
$[2]$, $[1, 3, 4]$ is also not possible because students with skills $5$ and $6$ aren't assigned to a class.
In the third test you can assign the students in the following way: $[3, 4, 13, 13, 20], [2, 5, 8, 16, 17]$ or $[3, 8, 17], [2, 4, 5, 13, 13, 16, 20]$. Both divisions give minimal possible absolute difference.
|
import sys
# inf = open('input.txt', 'r')
# reader = (line.rstrip() for line in inf)
reader = (line.rstrip() for line in sys.stdin)
input = reader.__next__
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
a.sort()
print(a[n] - a[n - 1])
# inf.close()
|
Reminder: the median of the array $[a_1, a_2, \dots, a_{2k+1}]$ of odd number of elements is defined as follows: let $[b_1, b_2, \dots, b_{2k+1}]$ be the elements of the array in the sorted order. Then median of this array is equal to $b_{k+1}$.
There are $2n$ students, the $i$-th student has skill level $a_i$. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the $2$ classes such that each class has odd number of students (not divisible by $2$). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students halved.
The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2 n}$ ($1 \le a_i \le 10^9$) — skill levels of students.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
-----Example-----
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
-----Note-----
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be $|1 - 1| = 0$.
In the second test, one of the possible partitions is to make the first class of students with skill levels $[6, 4, 2]$, so that the skill level of the first class will be $4$, and second with $[5, 1, 3]$, so that the skill level of the second class will be $3$. Absolute difference will be $|4 - 3| = 1$.
Note that you can't assign like $[2, 3]$, $[6, 5, 4, 1]$ or $[]$, $[6, 5, 4, 1, 2, 3]$ because classes have even number of students.
$[2]$, $[1, 3, 4]$ is also not possible because students with skills $5$ and $6$ aren't assigned to a class.
In the third test you can assign the students in the following way: $[3, 4, 13, 13, 20], [2, 5, 8, 16, 17]$ or $[3, 8, 17], [2, 4, 5, 13, 13, 16, 20]$. Both divisions give minimal possible absolute difference.
|
t = int(input())
for i in range(t):
n = int(input())
a = list(map(int,input().split()))
a.sort()
print(a[n]-a[n-1])
|
Reminder: the median of the array $[a_1, a_2, \dots, a_{2k+1}]$ of odd number of elements is defined as follows: let $[b_1, b_2, \dots, b_{2k+1}]$ be the elements of the array in the sorted order. Then median of this array is equal to $b_{k+1}$.
There are $2n$ students, the $i$-th student has skill level $a_i$. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the $2$ classes such that each class has odd number of students (not divisible by $2$). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students halved.
The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2 n}$ ($1 \le a_i \le 10^9$) — skill levels of students.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
-----Example-----
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
-----Note-----
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be $|1 - 1| = 0$.
In the second test, one of the possible partitions is to make the first class of students with skill levels $[6, 4, 2]$, so that the skill level of the first class will be $4$, and second with $[5, 1, 3]$, so that the skill level of the second class will be $3$. Absolute difference will be $|4 - 3| = 1$.
Note that you can't assign like $[2, 3]$, $[6, 5, 4, 1]$ or $[]$, $[6, 5, 4, 1, 2, 3]$ because classes have even number of students.
$[2]$, $[1, 3, 4]$ is also not possible because students with skills $5$ and $6$ aren't assigned to a class.
In the third test you can assign the students in the following way: $[3, 4, 13, 13, 20], [2, 5, 8, 16, 17]$ or $[3, 8, 17], [2, 4, 5, 13, 13, 16, 20]$. Both divisions give minimal possible absolute difference.
|
def solve():
n = int(input())
arr = sorted(map(int, input().split()))
print(arr[n] - arr[n-1])
for _ in range(int(input())):
solve()
|
Reminder: the median of the array $[a_1, a_2, \dots, a_{2k+1}]$ of odd number of elements is defined as follows: let $[b_1, b_2, \dots, b_{2k+1}]$ be the elements of the array in the sorted order. Then median of this array is equal to $b_{k+1}$.
There are $2n$ students, the $i$-th student has skill level $a_i$. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the $2$ classes such that each class has odd number of students (not divisible by $2$). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students halved.
The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2 n}$ ($1 \le a_i \le 10^9$) — skill levels of students.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
-----Example-----
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
-----Note-----
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be $|1 - 1| = 0$.
In the second test, one of the possible partitions is to make the first class of students with skill levels $[6, 4, 2]$, so that the skill level of the first class will be $4$, and second with $[5, 1, 3]$, so that the skill level of the second class will be $3$. Absolute difference will be $|4 - 3| = 1$.
Note that you can't assign like $[2, 3]$, $[6, 5, 4, 1]$ or $[]$, $[6, 5, 4, 1, 2, 3]$ because classes have even number of students.
$[2]$, $[1, 3, 4]$ is also not possible because students with skills $5$ and $6$ aren't assigned to a class.
In the third test you can assign the students in the following way: $[3, 4, 13, 13, 20], [2, 5, 8, 16, 17]$ or $[3, 8, 17], [2, 4, 5, 13, 13, 16, 20]$. Both divisions give minimal possible absolute difference.
|
for _ in range(int(input())):
n = int(input())
l1 = list(map(int, input().split()))
l1.sort()
print(l1[n] - l1[n-1])
|
Reminder: the median of the array $[a_1, a_2, \dots, a_{2k+1}]$ of odd number of elements is defined as follows: let $[b_1, b_2, \dots, b_{2k+1}]$ be the elements of the array in the sorted order. Then median of this array is equal to $b_{k+1}$.
There are $2n$ students, the $i$-th student has skill level $a_i$. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the $2$ classes such that each class has odd number of students (not divisible by $2$). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students halved.
The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2 n}$ ($1 \le a_i \le 10^9$) — skill levels of students.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
-----Example-----
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
-----Note-----
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be $|1 - 1| = 0$.
In the second test, one of the possible partitions is to make the first class of students with skill levels $[6, 4, 2]$, so that the skill level of the first class will be $4$, and second with $[5, 1, 3]$, so that the skill level of the second class will be $3$. Absolute difference will be $|4 - 3| = 1$.
Note that you can't assign like $[2, 3]$, $[6, 5, 4, 1]$ or $[]$, $[6, 5, 4, 1, 2, 3]$ because classes have even number of students.
$[2]$, $[1, 3, 4]$ is also not possible because students with skills $5$ and $6$ aren't assigned to a class.
In the third test you can assign the students in the following way: $[3, 4, 13, 13, 20], [2, 5, 8, 16, 17]$ or $[3, 8, 17], [2, 4, 5, 13, 13, 16, 20]$. Both divisions give minimal possible absolute difference.
|
def main():
t = int(input())
for _ in range(t):
n = int(input())
a = [int(i) for i in input().split()]
a.sort()
print(a[n] - a[n-1])
main()
|
Reminder: the median of the array $[a_1, a_2, \dots, a_{2k+1}]$ of odd number of elements is defined as follows: let $[b_1, b_2, \dots, b_{2k+1}]$ be the elements of the array in the sorted order. Then median of this array is equal to $b_{k+1}$.
There are $2n$ students, the $i$-th student has skill level $a_i$. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the $2$ classes such that each class has odd number of students (not divisible by $2$). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students halved.
The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2 n}$ ($1 \le a_i \le 10^9$) — skill levels of students.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
-----Example-----
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
-----Note-----
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be $|1 - 1| = 0$.
In the second test, one of the possible partitions is to make the first class of students with skill levels $[6, 4, 2]$, so that the skill level of the first class will be $4$, and second with $[5, 1, 3]$, so that the skill level of the second class will be $3$. Absolute difference will be $|4 - 3| = 1$.
Note that you can't assign like $[2, 3]$, $[6, 5, 4, 1]$ or $[]$, $[6, 5, 4, 1, 2, 3]$ because classes have even number of students.
$[2]$, $[1, 3, 4]$ is also not possible because students with skills $5$ and $6$ aren't assigned to a class.
In the third test you can assign the students in the following way: $[3, 4, 13, 13, 20], [2, 5, 8, 16, 17]$ or $[3, 8, 17], [2, 4, 5, 13, 13, 16, 20]$. Both divisions give minimal possible absolute difference.
|
def main():
n = int(input())
a = list(map(int, input().split()))
a = sorted(a)
print(a[len(a)//2] - a[len(a)//2 - 1])
def __starting_point():
t = int(input())
for i in range(t):
main()
__starting_point()
|
Reminder: the median of the array $[a_1, a_2, \dots, a_{2k+1}]$ of odd number of elements is defined as follows: let $[b_1, b_2, \dots, b_{2k+1}]$ be the elements of the array in the sorted order. Then median of this array is equal to $b_{k+1}$.
There are $2n$ students, the $i$-th student has skill level $a_i$. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the $2$ classes such that each class has odd number of students (not divisible by $2$). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students halved.
The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2 n}$ ($1 \le a_i \le 10^9$) — skill levels of students.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
-----Example-----
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
-----Note-----
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be $|1 - 1| = 0$.
In the second test, one of the possible partitions is to make the first class of students with skill levels $[6, 4, 2]$, so that the skill level of the first class will be $4$, and second with $[5, 1, 3]$, so that the skill level of the second class will be $3$. Absolute difference will be $|4 - 3| = 1$.
Note that you can't assign like $[2, 3]$, $[6, 5, 4, 1]$ or $[]$, $[6, 5, 4, 1, 2, 3]$ because classes have even number of students.
$[2]$, $[1, 3, 4]$ is also not possible because students with skills $5$ and $6$ aren't assigned to a class.
In the third test you can assign the students in the following way: $[3, 4, 13, 13, 20], [2, 5, 8, 16, 17]$ or $[3, 8, 17], [2, 4, 5, 13, 13, 16, 20]$. Both divisions give minimal possible absolute difference.
|
for t in range(int(input())):
n = int(input())
a = sorted([int(i) for i in input().split()])
print(a[n] - a[n-1])
|
Reminder: the median of the array $[a_1, a_2, \dots, a_{2k+1}]$ of odd number of elements is defined as follows: let $[b_1, b_2, \dots, b_{2k+1}]$ be the elements of the array in the sorted order. Then median of this array is equal to $b_{k+1}$.
There are $2n$ students, the $i$-th student has skill level $a_i$. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the $2$ classes such that each class has odd number of students (not divisible by $2$). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students halved.
The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2 n}$ ($1 \le a_i \le 10^9$) — skill levels of students.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
-----Example-----
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
-----Note-----
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be $|1 - 1| = 0$.
In the second test, one of the possible partitions is to make the first class of students with skill levels $[6, 4, 2]$, so that the skill level of the first class will be $4$, and second with $[5, 1, 3]$, so that the skill level of the second class will be $3$. Absolute difference will be $|4 - 3| = 1$.
Note that you can't assign like $[2, 3]$, $[6, 5, 4, 1]$ or $[]$, $[6, 5, 4, 1, 2, 3]$ because classes have even number of students.
$[2]$, $[1, 3, 4]$ is also not possible because students with skills $5$ and $6$ aren't assigned to a class.
In the third test you can assign the students in the following way: $[3, 4, 13, 13, 20], [2, 5, 8, 16, 17]$ or $[3, 8, 17], [2, 4, 5, 13, 13, 16, 20]$. Both divisions give minimal possible absolute difference.
|
for t in range(int(input())):
n = int(input())
l = [int(i) for i in input().split()]
l.sort()
print(abs(l[n] - l[n - 1]))
|
Reminder: the median of the array $[a_1, a_2, \dots, a_{2k+1}]$ of odd number of elements is defined as follows: let $[b_1, b_2, \dots, b_{2k+1}]$ be the elements of the array in the sorted order. Then median of this array is equal to $b_{k+1}$.
There are $2n$ students, the $i$-th student has skill level $a_i$. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the $2$ classes such that each class has odd number of students (not divisible by $2$). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students halved.
The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2 n}$ ($1 \le a_i \le 10^9$) — skill levels of students.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
-----Example-----
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
-----Note-----
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be $|1 - 1| = 0$.
In the second test, one of the possible partitions is to make the first class of students with skill levels $[6, 4, 2]$, so that the skill level of the first class will be $4$, and second with $[5, 1, 3]$, so that the skill level of the second class will be $3$. Absolute difference will be $|4 - 3| = 1$.
Note that you can't assign like $[2, 3]$, $[6, 5, 4, 1]$ or $[]$, $[6, 5, 4, 1, 2, 3]$ because classes have even number of students.
$[2]$, $[1, 3, 4]$ is also not possible because students with skills $5$ and $6$ aren't assigned to a class.
In the third test you can assign the students in the following way: $[3, 4, 13, 13, 20], [2, 5, 8, 16, 17]$ or $[3, 8, 17], [2, 4, 5, 13, 13, 16, 20]$. Both divisions give minimal possible absolute difference.
|
t=int(input())
for i in range(t):
n=int(input())
a=list(map(int,input().strip().split()))
a.sort()
print(a[n]-a[n-1])
|
Reminder: the median of the array $[a_1, a_2, \dots, a_{2k+1}]$ of odd number of elements is defined as follows: let $[b_1, b_2, \dots, b_{2k+1}]$ be the elements of the array in the sorted order. Then median of this array is equal to $b_{k+1}$.
There are $2n$ students, the $i$-th student has skill level $a_i$. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the $2$ classes such that each class has odd number of students (not divisible by $2$). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students halved.
The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2 n}$ ($1 \le a_i \le 10^9$) — skill levels of students.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
-----Example-----
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
-----Note-----
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be $|1 - 1| = 0$.
In the second test, one of the possible partitions is to make the first class of students with skill levels $[6, 4, 2]$, so that the skill level of the first class will be $4$, and second with $[5, 1, 3]$, so that the skill level of the second class will be $3$. Absolute difference will be $|4 - 3| = 1$.
Note that you can't assign like $[2, 3]$, $[6, 5, 4, 1]$ or $[]$, $[6, 5, 4, 1, 2, 3]$ because classes have even number of students.
$[2]$, $[1, 3, 4]$ is also not possible because students with skills $5$ and $6$ aren't assigned to a class.
In the third test you can assign the students in the following way: $[3, 4, 13, 13, 20], [2, 5, 8, 16, 17]$ or $[3, 8, 17], [2, 4, 5, 13, 13, 16, 20]$. Both divisions give minimal possible absolute difference.
|
def main():
def solve():
n = int(input())
aa = [int(a) for a in input().split()]
aa.sort()
print(aa[n] - aa[n-1])
q = int(input())
for _ in range(q):
solve()
def __starting_point():
main()
__starting_point()
|
Reminder: the median of the array $[a_1, a_2, \dots, a_{2k+1}]$ of odd number of elements is defined as follows: let $[b_1, b_2, \dots, b_{2k+1}]$ be the elements of the array in the sorted order. Then median of this array is equal to $b_{k+1}$.
There are $2n$ students, the $i$-th student has skill level $a_i$. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the $2$ classes such that each class has odd number of students (not divisible by $2$). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students halved.
The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2 n}$ ($1 \le a_i \le 10^9$) — skill levels of students.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
-----Example-----
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
-----Note-----
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be $|1 - 1| = 0$.
In the second test, one of the possible partitions is to make the first class of students with skill levels $[6, 4, 2]$, so that the skill level of the first class will be $4$, and second with $[5, 1, 3]$, so that the skill level of the second class will be $3$. Absolute difference will be $|4 - 3| = 1$.
Note that you can't assign like $[2, 3]$, $[6, 5, 4, 1]$ or $[]$, $[6, 5, 4, 1, 2, 3]$ because classes have even number of students.
$[2]$, $[1, 3, 4]$ is also not possible because students with skills $5$ and $6$ aren't assigned to a class.
In the third test you can assign the students in the following way: $[3, 4, 13, 13, 20], [2, 5, 8, 16, 17]$ or $[3, 8, 17], [2, 4, 5, 13, 13, 16, 20]$. Both divisions give minimal possible absolute difference.
|
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split(' ')))
a = sorted(a)
print(a[len(a)//2] - a[len(a)//2-1])
|
Reminder: the median of the array $[a_1, a_2, \dots, a_{2k+1}]$ of odd number of elements is defined as follows: let $[b_1, b_2, \dots, b_{2k+1}]$ be the elements of the array in the sorted order. Then median of this array is equal to $b_{k+1}$.
There are $2n$ students, the $i$-th student has skill level $a_i$. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the $2$ classes such that each class has odd number of students (not divisible by $2$). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students halved.
The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2 n}$ ($1 \le a_i \le 10^9$) — skill levels of students.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
-----Example-----
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
-----Note-----
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be $|1 - 1| = 0$.
In the second test, one of the possible partitions is to make the first class of students with skill levels $[6, 4, 2]$, so that the skill level of the first class will be $4$, and second with $[5, 1, 3]$, so that the skill level of the second class will be $3$. Absolute difference will be $|4 - 3| = 1$.
Note that you can't assign like $[2, 3]$, $[6, 5, 4, 1]$ or $[]$, $[6, 5, 4, 1, 2, 3]$ because classes have even number of students.
$[2]$, $[1, 3, 4]$ is also not possible because students with skills $5$ and $6$ aren't assigned to a class.
In the third test you can assign the students in the following way: $[3, 4, 13, 13, 20], [2, 5, 8, 16, 17]$ or $[3, 8, 17], [2, 4, 5, 13, 13, 16, 20]$. Both divisions give minimal possible absolute difference.
|
t = int(input())
for _ in range(t):
n = int(input())
s = [int(x) for x in input().split()]
s = sorted(s)
print (abs(s[n]-s[n-1]))
|
Reminder: the median of the array $[a_1, a_2, \dots, a_{2k+1}]$ of odd number of elements is defined as follows: let $[b_1, b_2, \dots, b_{2k+1}]$ be the elements of the array in the sorted order. Then median of this array is equal to $b_{k+1}$.
There are $2n$ students, the $i$-th student has skill level $a_i$. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the $2$ classes such that each class has odd number of students (not divisible by $2$). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students halved.
The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2 n}$ ($1 \le a_i \le 10^9$) — skill levels of students.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
-----Example-----
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
-----Note-----
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be $|1 - 1| = 0$.
In the second test, one of the possible partitions is to make the first class of students with skill levels $[6, 4, 2]$, so that the skill level of the first class will be $4$, and second with $[5, 1, 3]$, so that the skill level of the second class will be $3$. Absolute difference will be $|4 - 3| = 1$.
Note that you can't assign like $[2, 3]$, $[6, 5, 4, 1]$ or $[]$, $[6, 5, 4, 1, 2, 3]$ because classes have even number of students.
$[2]$, $[1, 3, 4]$ is also not possible because students with skills $5$ and $6$ aren't assigned to a class.
In the third test you can assign the students in the following way: $[3, 4, 13, 13, 20], [2, 5, 8, 16, 17]$ or $[3, 8, 17], [2, 4, 5, 13, 13, 16, 20]$. Both divisions give minimal possible absolute difference.
|
t=int(input())
while t:
n=int(input())
a=input().split()
for i in range(2*n):
a[i]=int(a[i])
a.sort()
print(a[n]-a[n-1])
t-=1
|
Reminder: the median of the array $[a_1, a_2, \dots, a_{2k+1}]$ of odd number of elements is defined as follows: let $[b_1, b_2, \dots, b_{2k+1}]$ be the elements of the array in the sorted order. Then median of this array is equal to $b_{k+1}$.
There are $2n$ students, the $i$-th student has skill level $a_i$. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the $2$ classes such that each class has odd number of students (not divisible by $2$). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students halved.
The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2 n}$ ($1 \le a_i \le 10^9$) — skill levels of students.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
-----Example-----
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
-----Note-----
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be $|1 - 1| = 0$.
In the second test, one of the possible partitions is to make the first class of students with skill levels $[6, 4, 2]$, so that the skill level of the first class will be $4$, and second with $[5, 1, 3]$, so that the skill level of the second class will be $3$. Absolute difference will be $|4 - 3| = 1$.
Note that you can't assign like $[2, 3]$, $[6, 5, 4, 1]$ or $[]$, $[6, 5, 4, 1, 2, 3]$ because classes have even number of students.
$[2]$, $[1, 3, 4]$ is also not possible because students with skills $5$ and $6$ aren't assigned to a class.
In the third test you can assign the students in the following way: $[3, 4, 13, 13, 20], [2, 5, 8, 16, 17]$ or $[3, 8, 17], [2, 4, 5, 13, 13, 16, 20]$. Both divisions give minimal possible absolute difference.
|
def solve(n, a_s):
a_s.sort()
return a_s[n] - a_s[n - 1]
def __starting_point():
t = int(input())
for _ in range(t):
n = int(input())
a_s = [int(ch) for ch in input().split(' ')]
print(solve(n, a_s))
__starting_point()
|
Reminder: the median of the array $[a_1, a_2, \dots, a_{2k+1}]$ of odd number of elements is defined as follows: let $[b_1, b_2, \dots, b_{2k+1}]$ be the elements of the array in the sorted order. Then median of this array is equal to $b_{k+1}$.
There are $2n$ students, the $i$-th student has skill level $a_i$. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the $2$ classes such that each class has odd number of students (not divisible by $2$). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students halved.
The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2 n}$ ($1 \le a_i \le 10^9$) — skill levels of students.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
-----Example-----
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
-----Note-----
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be $|1 - 1| = 0$.
In the second test, one of the possible partitions is to make the first class of students with skill levels $[6, 4, 2]$, so that the skill level of the first class will be $4$, and second with $[5, 1, 3]$, so that the skill level of the second class will be $3$. Absolute difference will be $|4 - 3| = 1$.
Note that you can't assign like $[2, 3]$, $[6, 5, 4, 1]$ or $[]$, $[6, 5, 4, 1, 2, 3]$ because classes have even number of students.
$[2]$, $[1, 3, 4]$ is also not possible because students with skills $5$ and $6$ aren't assigned to a class.
In the third test you can assign the students in the following way: $[3, 4, 13, 13, 20], [2, 5, 8, 16, 17]$ or $[3, 8, 17], [2, 4, 5, 13, 13, 16, 20]$. Both divisions give minimal possible absolute difference.
|
for _ in range(int(input())):
n = int(input())
l = [int(i) for i in input().split()]
l.sort(reverse=True)
ind = 0
P = []
S = []
for i in l:
if (ind % 2) == 0:
P.append(i)
else:
S.append(i)
ind += 1
if n % 2 == 0:
a = abs(P[(n//2)-1] - S[n//2])
b = abs(S[(n//2)-1] - P[n//2])
print(min(a,b))
else:
print(abs(P[n//2] - S[n//2]))
|
Reminder: the median of the array $[a_1, a_2, \dots, a_{2k+1}]$ of odd number of elements is defined as follows: let $[b_1, b_2, \dots, b_{2k+1}]$ be the elements of the array in the sorted order. Then median of this array is equal to $b_{k+1}$.
There are $2n$ students, the $i$-th student has skill level $a_i$. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the $2$ classes such that each class has odd number of students (not divisible by $2$). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students halved.
The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2 n}$ ($1 \le a_i \le 10^9$) — skill levels of students.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
-----Example-----
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
-----Note-----
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be $|1 - 1| = 0$.
In the second test, one of the possible partitions is to make the first class of students with skill levels $[6, 4, 2]$, so that the skill level of the first class will be $4$, and second with $[5, 1, 3]$, so that the skill level of the second class will be $3$. Absolute difference will be $|4 - 3| = 1$.
Note that you can't assign like $[2, 3]$, $[6, 5, 4, 1]$ or $[]$, $[6, 5, 4, 1, 2, 3]$ because classes have even number of students.
$[2]$, $[1, 3, 4]$ is also not possible because students with skills $5$ and $6$ aren't assigned to a class.
In the third test you can assign the students in the following way: $[3, 4, 13, 13, 20], [2, 5, 8, 16, 17]$ or $[3, 8, 17], [2, 4, 5, 13, 13, 16, 20]$. Both divisions give minimal possible absolute difference.
|
for _ in range(int(input())):
n=int(input())
l=list(map(int,input().split()))
l.sort()
print(l[n]-l[n-1])
|
Reminder: the median of the array $[a_1, a_2, \dots, a_{2k+1}]$ of odd number of elements is defined as follows: let $[b_1, b_2, \dots, b_{2k+1}]$ be the elements of the array in the sorted order. Then median of this array is equal to $b_{k+1}$.
There are $2n$ students, the $i$-th student has skill level $a_i$. It's not guaranteed that all skill levels are distinct.
Let's define skill level of a class as the median of skill levels of students of the class.
As a principal of the school, you would like to assign each student to one of the $2$ classes such that each class has odd number of students (not divisible by $2$). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.
What is the minimum possible absolute difference you can achieve?
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students halved.
The second line of each test case contains $2n$ integers $a_1, a_2, \dots, a_{2 n}$ ($1 \le a_i \le 10^9$) — skill levels of students.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
-----Output-----
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.
-----Example-----
Input
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Output
0
1
5
-----Note-----
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be $|1 - 1| = 0$.
In the second test, one of the possible partitions is to make the first class of students with skill levels $[6, 4, 2]$, so that the skill level of the first class will be $4$, and second with $[5, 1, 3]$, so that the skill level of the second class will be $3$. Absolute difference will be $|4 - 3| = 1$.
Note that you can't assign like $[2, 3]$, $[6, 5, 4, 1]$ or $[]$, $[6, 5, 4, 1, 2, 3]$ because classes have even number of students.
$[2]$, $[1, 3, 4]$ is also not possible because students with skills $5$ and $6$ aren't assigned to a class.
In the third test you can assign the students in the following way: $[3, 4, 13, 13, 20], [2, 5, 8, 16, 17]$ or $[3, 8, 17], [2, 4, 5, 13, 13, 16, 20]$. Both divisions give minimal possible absolute difference.
|
import sys
t = int(input())
for _ in range(t):
n=int(input())
l = list(map(int,sys.stdin.readline().split()))
l.sort()
print(l[n]-l[n-1])
|
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
max_score = 0
curr_score= 0
init_hand = cardPoints[len(cardPoints)-k:]
max_score = sum(init_hand)
curr_score = max_score
for i in range(k):
curr_score -= init_hand[i]
curr_score += cardPoints[i]
if curr_score > max_score:
max_score = curr_score
return max_score
|
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
n = len(cardPoints) - k
min = 0
window = 0
all = 0
for i in range(n):
window += cardPoints[i]
all += cardPoints[i]
min = window
# print(all)
for x in range(k):
# print(x)
all += cardPoints[x+n]
window -= cardPoints[x]
window += cardPoints[x+n]
if window < min:
min = window
return all - min
# print(all)
# print(all-min)
|
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
result = curr = 0
for i in range(-k, k):
curr += cardPoints[i]
if i >= 0:
curr -= cardPoints[i - k]
result = max(result, curr)
return result
|
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
# dfs TLE
# use sliding window instead
# keep moving a window of size n - k along the way
maxSum = sum(cardPoints)
if len(cardPoints) <= k:
return maxSum
subSum = 0
ans = 0
for i in range(len(cardPoints)):
subSum += cardPoints[i]
if i + 1 >= (len(cardPoints) - k):
ans = max(ans, maxSum - subSum)
subSum -= cardPoints[i - (len(cardPoints) - k - 1)]
return ans
|
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
answer = 0
left = [0]*len(cardPoints)
right = [0]*len(cardPoints)
for i in range(len(cardPoints)) :
if i == 0 : left[0] = cardPoints[0]
else :
left[i] = left[i-1] + cardPoints[i]
for i in range(len(cardPoints)-1,-1,-1) :
if i == len(cardPoints)-1 : right[-1] = cardPoints[-1]
else :
right[i] = right[i+1] + cardPoints[i]
for i in range(k+1) :
if i == 0 : Sum = right[-k]
elif i == k : Sum = left[k-1]
else :
Sum = left[k-i-1] + right[-i]
answer = max(answer,Sum)
return answer
|
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
size = len(cardPoints) - k
minSum = float('inf')
cur = 0
left = 0
for i, v in enumerate(cardPoints):
cur += v
if i - left + 1 > size:
cur -= cardPoints[left]
left += 1
if i - left + 1 == size:
minSum = min(minSum, cur)
return sum(cardPoints) - minSum
|
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, points: List[int], num_cards: int) -> int:
size = len(points) - num_cards
min_subarray_sum = math.inf
left = curr = 0
for right, val in enumerate(points):
curr += val
if right - left + 1 > size:
curr -= points[left]
left += 1
if right - left + 1 == size:
min_subarray_sum = min(min_subarray_sum, curr)
return sum(points) - min_subarray_sum
|
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
n = len(cardPoints) - k
min = 0
window = 0
all = 0
for i in range(n):
window += cardPoints[i]
all += cardPoints[i]
min = window
# print(all)
for x in range(k):
# print(x)
y = x+n
all += cardPoints[y]
window -= cardPoints[x]
window += cardPoints[y]
if window < min:
min = window
return all - min
# print(all)
# print(all-min)
|
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
# The Edge Case not needed here: if k > len(cardPoints) or k<=0: raise ValueErro('')
left, right = [0], [0]
for i in range(k):
left.append(left[-1]+ cardPoints[i])
right.append(right[-1] + cardPoints[len(cardPoints) -1 - i])
res = 0
for i in range(k+1):
x = left[i] + right[k-i]
res = max(res,x)
return res
|
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
# The Edge Case not needed here: if k > len(cardPoints) or k<=0: raise ValueErro('')
# Your solution is O(N) and this solution is O(k)
# Eventhough the real time is similar, O(k) is better
left = [0] * (k+1)
right = [0] * (k+1)
for i in range(k):
left[i+1] = left[i] + cardPoints[i]
right[i+1] = right[i] + cardPoints[-i-1]
return max(left[j]+right[k-j] for j in range(k+1))
|
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
left_cumsum = [0]
right_cumsum = [0]
for p in cardPoints[:k+1]:
left_cumsum.append(left_cumsum[-1] + p)
for p in reversed(cardPoints[-(k+1):]):
right_cumsum.append(right_cumsum[-1] + p)
result = 0
for i in range(k+1):
result = max(result, left_cumsum[i] + right_cumsum[k-i])
return result
|
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
left, right = [0], [0]
for i in range(len(cardPoints)):
left.append(left[-1]+cardPoints[i])
right.append(right[-1]+cardPoints[-i-1])
return max(left[i]+right[k-i] for i in range(k+1))
|
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
n = len(cardPoints)
# prefix sum solution
pre = [0]*(n+1)
for i in range(n):
pre[i+1] = pre[i] + cardPoints[i]
max_val = -1
for i in range(k+1):
max_val = max(max_val, pre[i] + pre[n] - pre[n-k+i])
return max_val
# if k >= n:
# return sum(cardPoints)
# def dfs(i,j):
# if i + (n-j-1) >= k:
# return 0
# else:
# return max(dfs(i+1,j)+cardPoints[i], dfs(i,j-1)+cardPoints[j])
# return dfs(0,n-1)
|
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
n = len(cardPoints)
total = sum(cardPoints)
if n == k or n < k : return total
remove = n - k
ans = 0
'''memo = [0]*(n+1)
memo[0] = 0
start = 0
for i in range(0, n):
memo[i+1] = memo[i] + cardPoints[i]
if i-start + 1 == remove:
ans = max(ans, total-(memo[i+1]-memo[start]))
start = start+1'''
curr = 0
start = 0
for right in range(n):
curr += cardPoints[right]
if right-start+1 == remove:
ans = max(ans, total-curr)
curr -= cardPoints[start]
start +=1
return ans
|
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
left, right = [0], [0]
for i in range(k):
left.append(left[-1]+ cardPoints[i])
right.append(right[-1] + cardPoints[len(cardPoints) -1 - i])
res = 0
for i in range(k+1):
x = left[i] + right[k-i]
res = max(res,x)
return res
# front_sum=back_sum=[0]
# print 'cardPoints:', cardPoints
# print 'k:', k
# frontSum, backSum = [0], [0]
# for n in cardPoints:
# frontSum.append(frontSum[-1]+n)
# print 'frontSum:', frontSum
# for n in cardPoints[::-1]:
# backSum.append(backSum[-1]+n)
# print 'backSum:', backSum
# allCombinations = [frontSum[i]+backSum[k-i] for i in range(k+1)]
# print 'allCombinations:', allCombinations
# return max(allCombinations)
|
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
min_len = len(cardPoints) - k
curr_sum = 0
min_val = 0
for start in range(len(cardPoints) - min_len + 1):
if start == 0:
curr_sum = sum(cardPoints[start:start+min_len])
min_val = curr_sum
else:
curr_sum = curr_sum - cardPoints[start - 1] + cardPoints[start+min_len-1]
if min_val > curr_sum:
min_val = curr_sum
return sum(cardPoints) - min_val
# front_sum=back_sum=[0]
# print 'cardPoints:', cardPoints
# print 'k:', k
# frontSum, backSum = [0], [0]
# for n in cardPoints:
# frontSum.append(frontSum[-1]+n)
# print 'frontSum:', frontSum
# for n in cardPoints[::-1]:
# backSum.append(backSum[-1]+n)
# print 'backSum:', backSum
# allCombinations = [frontSum[i]+backSum[k-i] for i in range(k+1)]
# print 'allCombinations:', allCombinations
# return max(allCombinations)
|
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
n = len(cardPoints) - k
min = 0
window = 0
all = 0
for i in range(n):
window += cardPoints[i]
all += cardPoints[i]
min = window
print(all)
for x in range(k):
print(x)
all += cardPoints[x+n]
window -= cardPoints[x]
window += cardPoints[x+n]
if window < min:
min = window
return all - min
# print(all)
# print(all-min)
|
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
n = len(cardPoints)
sums = [0] * (n+1)
for i in range(1, n+1):
sums[i] = sums[i-1] + cardPoints[i-1]
ans = float('inf')
for i in range(k+1):
ans = min(ans, sums[i+n-k] - sums[i])
print((sums, ans))
return sums[-1] - ans
|
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
s = sum(cardPoints)
if k >= len(cardPoints):
return s
maxPoint = 0
cur = 0
j = 0
# i-j+k == n
n = len(cardPoints)
for i, point in enumerate(cardPoints):
if i-j+k > n-1:
cur -= cardPoints[j]
j += 1
cur += point
if i-j+k == n-1:
maxPoint = max(maxPoint, s-cur)
return maxPoint
|
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
# find a subarray in the middle that its sum is min, maintain n-k length
n = len(cardPoints)
runningsum = 0
start = end = 0
total = sum(cardPoints)
minsum = float('inf')
while end < len(cardPoints):
runningsum += cardPoints[end]
if end - start + 1 > n-k:
runningsum -= cardPoints[start]
start += 1
if end - start + 1 == n-k:
minsum = min(minsum, runningsum)
end += 1
return total-minsum
|
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
# cardLen = len(cardPoints)
# if cardLen == k:
# return sum(cardPoints)
# dp = {}
# def takeCard(l0, r0, k0):
# if k0 == 1:
# return max(cardPoints[l0], cardPoints[r0])
# if (l0, r0, k0) in dp:
# return dp[(l0, r0, k0)]
# ans = max(cardPoints[l0] + takeCard(l0+1, r0, k0-1), cardPoints[r0] + takeCard(l0, r0-1, k0-1))
# dp[(l0, r0, k0)] = ans
# return ans
# return takeCard(0, cardLen-1, k)
cardLen = len(cardPoints)
frontSum = [0]
for num in cardPoints:
frontSum.append(frontSum[-1]+ num)
backSum = [0 for _ in range(cardLen + 1)]
for i in range(cardLen - 1, -1, -1):
backSum[i] = cardPoints[i] + backSum[i+1]
ans = frontSum[k]
for i in range(k):
ans = max(ans, frontSum[i] + backSum[-(k-i)-1])
return ans
|
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
left, right = [0], [0]
for i in range(k):
left.append(left[-1]+ cardPoints[i])
right.append(right[-1] + cardPoints[len(cardPoints) -1 - i])
print(left)
print(right)
res = 0
for i in range(k+1):
x = left[i] + right[k-i]
res = max(res,x)
return res
|
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
forwardSum = [m for m in cardPoints]
backwardSum = cardPoints.copy()
backwardSum.append(0)
for c in range(1, len(cardPoints)):
forwardSum[c] = forwardSum[c-1] + forwardSum[c]
for l in range(len(cardPoints)-2, 0, -1):
backwardSum[l] = backwardSum[l+1] + backwardSum[l]
maximum = 0
for i in range(k-1, -2, -1):
if i != -1:
maximum = max(maximum, forwardSum[i] + backwardSum[len(backwardSum)-1-(k-1-i)])
else:
maximum = max(maximum, backwardSum[len(backwardSum)-1-k])
return maximum
|
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
n = len(cardPoints) - k
min = 0
window = 0
all = 0
for i in range(n):
window += cardPoints[i]
min = window
all = window
# print(all)
for x in range(k):
# print(x)
all += cardPoints[x+n]
window -= cardPoints[x]
window += cardPoints[x+n]
if window < min:
min = window
return all - min
# print(all)
# print(all-min)
|
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
score = sum(cardPoints[:k])
best = score
for i in range(k):
score += cardPoints[-(i+1)] - cardPoints[k-i-1]
if score > best:
best = score
return best
|
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
leftsum = [0] * len(cardPoints)
rightsum = [0] * len(cardPoints)
n = len(cardPoints)
leftsum[0] = cardPoints[0]
rightsum[n-1] = cardPoints[n-1]
for i in range(1,n):
leftsum[i] = leftsum[i-1] + cardPoints[i]
rightsum[n-1-i] = rightsum[n-1-i+1] + cardPoints[n-1-i]
res = max(leftsum[k-1],rightsum[-(k-1+1)])
for i in range(k-1):
res = max((leftsum[i] + rightsum[-(k-i-1)]), res)
return res
|
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
'''
convert this into a sliding window problem
rephrase the problem:
find the max window of length k between cardPoints[n-k:n+k]
[1,2,3,4,5,6]
k = 2
4,5,0,1
'''
ans = 0
curSum = 0
n = len(cardPoints)
for i in range(n-k, n+k):
curSum += cardPoints[i%n]
if i >= n:
curSum -= cardPoints[(i-k)%n]
ans = max(ans, curSum)
return ans
|
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
if not cardPoints or len(cardPoints) == 0:
return 0
window = len(cardPoints) - k
res = float('inf')
s = 0
for i in range(window):
s += cardPoints[i]
res = min(s, res)
for i in range(window, len(cardPoints)):
print(cardPoints[i],s,i)
s -= cardPoints[i-window]
s += cardPoints[i]
res = min(s, res)
return sum(cardPoints) - res
|
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
n = len(cardPoints)
if n == k:
return sum(cardPoints)
pre, post = [0] * (n + 1), [0] * (n + 1)
for i in range(1, n + 1):
pre[i] = pre[i - 1] + cardPoints[i - 1]
for i in range(1, n + 1):
post[i] = post[i - 1] + cardPoints[n - i]
best = 0
# n = 7
# k = 3
# i = 0
# x = 5
# print (pre, post)
for i in range(k + 1):
best = max(best, pre[i] + post[k - i])
return best
|
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
if k == len(cardPoints):
return sum(cardPoints)
front = [0] * k
back = [0] * k
front[0] = cardPoints[0]
for i in range(1,k):
front[i] = front[i-1] + cardPoints[i]
back[0] = cardPoints[-1]
for i in range(1,k):
back[i] = back[i-1] + cardPoints[-1-i]
max_score = 0
print(front,back)
for i in range(k+1):
if i == 0:
max_score = max(max_score,back[-1])
# print(i,back[-1])
elif i == k:
max_score = max(max_score,front[k-1])
# print(i,front[k-1])
else:
max_score = max(max_score,front[i-1]+back[k-i-1])
# print(i,k-i,max_score,front[i-1]+back[k-i-1])
# print(i,k-i,max_score)
return max_score
|
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
length = len(cardPoints)
total = sum(cardPoints)
if k == length:
return total
curr = 0
temp = 2 ** 31 - 1
left = 0
for right in range(length):
curr += cardPoints[right]
if right - left + 1 < length - k:
continue
print(right, curr)
temp = min(temp, curr)
curr -= cardPoints[left]
left += 1
return total - temp
|
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
n = len(cardPoints)
cum_sum = [0 for i in range(n)]
cum_sum[0] = cardPoints[0]
rev_sum = [0 for i in range(n)]
rev_sum[0] = cardPoints[-1]
for i in range(1,n):
cum_sum[i] = cum_sum[i-1]+cardPoints[i]
rev_sum[i] = rev_sum[i-1]+cardPoints[n-i-1]
max_sum = max(cum_sum[k-1],rev_sum[k-1])
for i in range(1,k):
max_sum = max(max_sum,(cum_sum[i-1]+rev_sum[k-i-1]))
return max_sum
|
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
remainCnt = len(cardPoints) - k
if remainCnt == 0: return sum(cardPoints)
minRemainSum = float('inf')
curr = 0
cnt = 0
for i in range(len(cardPoints)):
cnt += 1
curr += cardPoints[i]
if cnt == remainCnt:
minRemainSum = min(minRemainSum, curr)
curr -= cardPoints[i + 1 - cnt]
cnt -= 1
return sum(cardPoints) - minRemainSum
|
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
# Sliding window of length k
ans = total = sum(cardPoints[:k])
for i in range(1, k+1):
total -= cardPoints[k-i]
total += cardPoints[-1-i+1]
ans = max(ans, total)
return ans
|
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
from collections import deque
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
first = deque(cardPoints[0:k])
second = deque(cardPoints[len(cardPoints)-k:])
final = 0
firstSum = sum(first)
secondSum = sum(second)
for i in range(k):
if firstSum > secondSum:
final += first[0]
firstSum -= first.popleft()
secondSum -= second.popleft()
else:
final += second[len(second)-1]
firstSum -= first.pop()
secondSum -= second.pop()
return final
|
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints, k: int) -> int:
N = len(cardPoints)
preS, afterS = [0]*(N+1), [0]*(N+1)
ans = 0
for i in range(1,N+1):
preS[i]=preS[i-1]+cardPoints[i-1]
for j in range(1,N+1):
afterS[j] = afterS[j-1]+cardPoints[N-j]
for l in range(k+1):
ans = max(ans,preS[l]+afterS[k-l])
return ans
|
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
right_index = len(cardPoints)-k
curr_max = sum(cardPoints[right_index:])
curr_sum = curr_max
for left_index in range(0, k):
curr_sum -= cardPoints[right_index]
right_index += 1
curr_sum += cardPoints[left_index]
if curr_sum > curr_max:
curr_max = curr_sum
return curr_max
|
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, cardPoints: List[int], k: int) -> int:
n = len(cardPoints)
cum_sum = [0 for i in range(n)]
cum_sum[0] = cardPoints[0]
rev_sum = [0 for i in range(n)]
rev_sum[0] = cardPoints[-1]
for i in range(1,n):
cum_sum[i] = cum_sum[i-1]+cardPoints[i]
rev_sum[i] = rev_sum[i-1]+cardPoints[n-i-1]
max_sum = max(cum_sum[k-1],rev_sum[k-1])
for i in range(1,k):
max_sum = max(max_sum,(cum_sum[i-1]+rev_sum[k-i-1]))
return max_sum
|
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints.
In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards.
Your score is the sum of the points of the cards you have taken.
Given the integer array cardPoints and the integer k, return the maximum score you can obtain.
Example 1:
Input: cardPoints = [1,2,3,4,5,6,1], k = 3
Output: 12
Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
Example 2:
Input: cardPoints = [2,2,2], k = 2
Output: 4
Explanation: Regardless of which two cards you take, your score will always be 4.
Example 3:
Input: cardPoints = [9,7,7,9,7,7,9], k = 7
Output: 55
Explanation: You have to take all the cards. Your score is the sum of points of all cards.
Example 4:
Input: cardPoints = [1,1000,1], k = 1
Output: 1
Explanation: You cannot take the card in the middle. Your best score is 1.
Example 5:
Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3
Output: 202
Constraints:
1 <= cardPoints.length <= 10^5
1 <= cardPoints[i] <= 10^4
1 <= k <= cardPoints.length
|
class Solution:
def maxScore(self, A: List[int], k: int) -> int:
su = sum(A[:k])
n = len(A)
res = su
for i in range(k):
su -= A[k-i-1]
su += A[n-i-1]
res = max(res, su)
return res
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.