index int64 0 5.16k | difficulty int64 7 12 | question stringlengths 126 7.12k | solution stringlengths 30 18.6k | test_cases dict |
|---|---|---|---|---|
800 | 7 | Today, Mezo is playing a game. Zoma, a character in that game, is initially at position x = 0. Mezo starts sending n commands to Zoma. There are two possible commands:
* 'L' (Left) sets the position x: =x - 1;
* 'R' (Right) sets the position x: =x + 1.
Unfortunately, Mezo's controller malfunctions sometimes. ... | def main():
print(int(input())+1)
main() | {
"input": [
"4\nLRLR\n"
],
"output": [
"5\n"
]
} |
801 | 9 | Gildong owns a bulgogi restaurant. The restaurant has a lot of customers, so many of them like to make a reservation before visiting it.
Gildong tries so hard to satisfy the customers that he even memorized all customers' preferred temperature ranges! Looking through the reservation list, he wants to satisfy all custo... | def f(s):
return s==s[::-1]
for _ in range(int(input())):
n,t0=map(int,input().split())
mn,mx = t0,t0
ans=True
prev=0
for _ in range(n):
t,tl,th=map(int,input().split())
mn=mn-(t-prev)
mx=mx+(t-prev)
if mx<tl or mn>th:
ans=False
if mn<tl:
mn=tl
if mx>th:
mx=th
prev=t
if ans:
print("YES")... | {
"input": [
"4\n3 0\n5 1 2\n7 3 5\n10 -1 0\n2 12\n5 7 10\n10 16 20\n3 -100\n100 0 0\n100 -50 50\n200 100 100\n1 100\n99 -100 0\n"
],
"output": [
"YES\nNO\nYES\nNO\n"
]
} |
802 | 10 | The round carousel consists of n figures of animals. Figures are numbered from 1 to n in order of the carousel moving. Thus, after the n-th figure the figure with the number 1 follows. Each figure has its own type — the type of the animal corresponding to this figure (the horse, the tiger and so on). The type of animal... | def solve(n,a):
if len(set(a))==1:return [1]*n
arr=[1 if x%2==0 else 2 for x in range(n)]
if n%2!=0 and a[0]!=a[-1]:
for j in range(1,n):
if a[j]==a[j-1]:
arr[j]=arr[j-1]
for k in range(j+1,n):
arr[k]=3-arr[k-1]
break
... | {
"input": [
"4\n5\n1 2 1 2 2\n6\n1 2 2 1 2 2\n5\n1 2 1 2 3\n3\n10 10 10\n"
],
"output": [
"2\n1 2 1 2 2 \n2\n1 2 1 2 1 2 \n3\n1 2 1 2 3\n1\n1 1 1 \n"
]
} |
803 | 10 | Phoenix has decided to become a scientist! He is currently investigating the growth of bacteria.
Initially, on day 1, there is one bacterium with mass 1.
Every day, some number of bacteria will split (possibly zero or all). When a bacterium of mass m splits, it becomes two bacteria of mass m/2 each. For example, a ba... | def solve(n):
i =1
l = []
while(i <= n):
l.append(i)
n -= i
i *=2
if n > 0:
l.append(n)
l.sort()
print(len(l)-1)
for i in range(1,len(l)):
print(l[i] - l[i-1],end = " ")
print()
t = int(input())
for _ in range(t):
n = int(input())
solve(n)
| {
"input": [
"3\n9\n11\n2\n"
],
"output": [
"3\n1 0 2\n3\n1 2 0\n1\n0\n"
]
} |
804 | 8 | Lee was cleaning his house for the party when he found a messy string under the carpets. Now he'd like to make it clean accurately and in a stylish way...
The string s he found is a binary string of length n (i. e. string consists only of 0-s and 1-s).
In one move he can choose two consecutive characters s_i and s_{i... | def main():
for _ in range(int(input())):
n=int(input())
s=list(input())
s1="".join(s)
print(s1[:s1.find('1')]+"".join(s[s1.rfind('0'):]) if(s1.rfind('0')+1!=s1.find('1')) else s1 )
main() | {
"input": [
"5\n10\n0001111111\n4\n0101\n8\n11001101\n10\n1110000000\n1\n1\n"
],
"output": [
"0001111111\n001\n01\n0\n1\n"
]
} |
805 | 7 | A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
For a positive integer n, we call a... | def main():
for _ in range(int(input())):
print(*list(range(1,int(input())+1)))
main()
# | {
"input": [
"3\n1\n3\n7\n"
],
"output": [
"1\n1 2 3\n1 2 3 4 5 6 7\n"
]
} |
806 | 10 | Tenten runs a weapon shop for ninjas. Today she is willing to sell n shurikens which cost 1, 2, ..., n ryo (local currency). During a day, Tenten will place the shurikens onto the showcase, which is empty at the beginning of the day. Her job is fairly simple: sometimes Tenten places another shuriken (from the available... | t = int(input())
c = [0]*t
def error() :
print('NO')
raise SystemExit
v = [[10e9, -1]]
index = 0
for _ in range(t+t) :
q = input()
if q[0] == '+' :
v.append([0, index])
index += 1
continue
n = int(q.split()[1])
#if len(v) == 0 : error()
if v[-1][0] > n : error()
... | {
"input": [
"1\n- 1\n+\n",
"3\n+\n+\n+\n- 2\n- 1\n- 3\n",
"4\n+\n+\n- 2\n+\n- 3\n+\n- 1\n- 4\n"
],
"output": [
"NO\n",
"NO\n",
"YES\n4 2 3 1"
]
} |
807 | 10 | You are given a sequence a consisting of n integers a_1, a_2, ..., a_n, and an integer x. Your task is to make the sequence a sorted (it is considered sorted if the condition a_1 ≤ a_2 ≤ a_3 ≤ ... ≤ a_n holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly ... | #!/usr/bin/env pypy3
def ans(A, x):
if A == sorted(A): return 0
i = 0
ret = 0
while i < len(A) and A != sorted(A):
if x < A[i]:
ret += 1
x, A[i] = A[i], x
i += 1
else:
i += 1
continue
if A == sorted(A):
return ret
return -1
T = int(input())
for _ in range(T):
_, x = input().split()
x = in... | {
"input": [
"6\n4 1\n2 3 5 4\n5 6\n1 1 3 4 4\n1 10\n2\n2 10\n11 9\n2 10\n12 11\n5 18\n81 324 218 413 324\n"
],
"output": [
"\n3\n0\n0\n-1\n1\n3\n"
]
} |
808 | 10 | You are given an array a of length n consisting of integers. You can apply the following operation, consisting of several steps, on the array a zero or more times:
* you select two different numbers in the array a_i and a_j;
* you remove i-th and j-th elements from the array.
For example, if n=6 and a=[1, 6,... | def solve():
n = int(input())
a = sorted(list(map(int, input().split())))
c = 0
i = 0
h = n // 2 + n % 2
while i < n // 2:
if a[i] != a[h + i]:
c += 2
i += 1
return n - c
t = int(input())
while t > 0:
print(solve())
t -= 1
| {
"input": [
"5\n6\n1 6 1 1 4 4\n2\n1 2\n2\n1 1\n5\n4 5 4 5 4\n6\n2 3 2 1 3 1\n"
],
"output": [
"\n0\n0\n2\n1\n0\n"
]
} |
809 | 9 | One day little Vasya found mom's pocket book. The book had n names of her friends and unusually enough, each name was exactly m letters long. Let's number the names from 1 to n in the order in which they are written.
As mom wasn't home, Vasya decided to play with names: he chose three integers i, j, k (1 ≤ i < j ≤ n, ... | def readln(): return tuple(map(int, input().split()))
n, m = readln()
lst = [input() for _ in range(n)]
lst = [len(set(_)) for _ in zip(*lst)]
ans = 1
mod = 10**9 + 7
for v in lst:
ans = (ans * v) % mod
print(ans)
| {
"input": [
"2 3\nAAB\nBAA\n",
"4 5\nABABA\nBCGDG\nAAAAA\nYABSA\n"
],
"output": [
"4\n",
"216\n"
]
} |
810 | 9 | Valera's lifelong ambition was to be a photographer, so he bought a new camera. Every day he got more and more clients asking for photos, and one day Valera needed a program that would determine the maximum number of people he can serve.
The camera's memory is d megabytes. Valera's camera can take photos of high and l... | R=lambda:map(int,input().split())
n,d=R()
a,b=R()
def get():
x,y=R()
return a*x+b*y
s=sorted([(get(),i) for i in range(1,n+1)])
#print(s)
ans=[]
for i in range(n):
if d>=s[i][0]:
ans+=[s[i][1]]
d-=s[i][0]
else:
break
print(len(ans))
print(*ans)
| {
"input": [
"3 10\n2 3\n1 4\n2 1\n1 0\n",
"3 6\n6 6\n1 1\n1 0\n1 0\n"
],
"output": [
"2\n3 2\n",
"1\n2\n"
]
} |
811 | 8 | You've got two rectangular tables with sizes na × ma and nb × mb cells. The tables consist of zeroes and ones. We will consider the rows and columns of both tables indexed starting from 1. Then we will define the element of the first table, located at the intersection of the i-th row and the j-th column, as ai, j; we w... | n,m=map(int,input().split())
ar=[]
for i in range(n):
ar.append(list(map(int,list(input()))))
a,b=map(int,input().split())
arr=[]
for j in range(a):
arr.append(list(map(int,list(input()))))
def check(x,y):
res=0
for i in range(n):
for j in range(m):
if i+x<a and j+y<b and i+x>=0 an... | {
"input": [
"3 2\n01\n10\n00\n2 3\n001\n111\n",
"3 3\n000\n010\n000\n1 1\n1\n"
],
"output": [
"0 1\n",
"-1 -1\n"
]
} |
812 | 7 | The "BerCorp" company has got n employees. These employees can use m approved official languages for the formal correspondence. The languages are numbered with integers from 1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official language... | rd = lambda: list(map(int, input().split()))
def root(x):
if f[x]!=x: f[x] = root(f[x])
return f[x]
n, m = rd()
N=range(n)
f=list(N)
lang = [0]*n
for i in N:
lang[i] = set(rd()[1:])
for i in N:
for j in N[:i]:
if j==root(j) and lang[j].intersection(lang[i]):
f[j] = i
lang[i] = lang[i].union(lang[j])
pri... | {
"input": [
"8 7\n0\n3 1 2 3\n1 1\n2 5 4\n2 6 7\n1 3\n2 7 4\n1 1\n",
"2 2\n1 2\n0\n",
"5 5\n1 2\n2 2 3\n2 3 4\n2 4 5\n1 5\n"
],
"output": [
"2",
"1",
"0"
]
} |
813 | 9 | One day Bob got a letter in an envelope. Bob knows that when Berland's post officers send a letter directly from city «A» to city «B», they stamp it with «A B», or «B A». Unfortunately, often it is impossible to send a letter directly from the city of the sender to the city of the receiver, that's why the letter is sen... | from collections import defaultdict, Counter
class Solution:
def __init__(self):
pass
def solve_and_print(self):
d, c = defaultdict(int), []
for _ in range(int(input())):
x, y = [int(x) for x in input().split()]
c += [x, y]
d[x] += y
d[y]... | {
"input": [
"2\n1 100\n100 2\n",
"3\n3 1\n100 2\n3 2\n"
],
"output": [
"1 100 2 \n",
"1 3 2 100 \n"
]
} |
814 | 7 | You are given a cube of size k × k × k, which consists of unit cubes. Two unit cubes are considered neighbouring, if they have common face.
Your task is to paint each of k3 unit cubes one of two colours (black or white), so that the following conditions must be satisfied:
* each white cube has exactly 2 neighbourin... | class CodeforcesTask323ASolution:
def __init__(self):
self.result = ''
self.k = 0
def read_input(self):
self.k = int(input())
def process_task(self):
if self.k % 2:
self.result = "-1"
elif self.k == 2:
self.result = "bb\nww\n\nbb\nww"
... | {
"input": [
"1\n",
"2\n"
],
"output": [
"-1\n",
"bb\nbb\n\nww\nww\n\n"
]
} |
815 | 10 | In mathematics, a subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. For example, the sequence BDF is a subsequence of ABCDEF. A substring of a string is a continuous subsequence of the string. For example, BCD is a substri... | def gen(i, j):
if a[i][j] == -1:
a[i][j] = max(gen(i - 1, j - 1) + s2[i - 1] * (s2[i - 1] == s1[j - 1]), gen(i - 1, j), gen(i, j - 1), key = lambda x: [len(x), -x.count(virus)])
return a[i][j]
s1, s2, virus = [input() for x in range(3)]
a = [[''] * (len(s1) + 1)] + [[''] + [-1] * (len(s1)) for x in ra... | {
"input": [
"AA\nA\nA\n",
"AJKEQSLOBSROFGZ\nOVGURWZLWVLUXTH\nOZ\n"
],
"output": [
"0\n",
"OGZ\n"
]
} |
816 | 8 | The Tower of Hanoi is a well-known mathematical puzzle. It consists of three rods, and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape.
The objective of the pu... | # !/bin/env python3
# coding: UTF-8
# ✪ H4WK3yE乡
# Mohd. Farhan Tahir
# Indian Institute Of Information Technology and Management,Gwalior
# Question Link
# https://codeforces.com/problemset/problem/392/B
#
# ///==========Libraries, Constants and Functions=============///
import sys
inf = float("inf")
mod = 10000... | {
"input": [
"0 2 1\n1 0 100\n1 2 0\n5\n",
"0 2 2\n1 0 100\n1 2 0\n3\n",
"0 1 1\n1 0 1\n1 1 0\n3\n"
],
"output": [
"87\n",
"19\n",
"7\n"
]
} |
817 | 9 | It's holiday. Mashmokh and his boss, Bimokh, are playing a game invented by Mashmokh.
In this game Mashmokh writes sequence of n distinct integers on the board. Then Bimokh makes several (possibly zero) moves. On the first move he removes the first and the second integer from from the board, on the second move he rem... | def numbers(n, k):
if k == 0 and n == 1:
return [1]
if n == 1 or k < n // 2:
return [-1]
result, t = list(), k - (n - 2) // 2
result.append(t)
result.append(2 * t)
for i in range(n - 2):
result.append(2 * t + i + 1)
return result
N, K = [int(j) for j in input().spli... | {
"input": [
"5 3",
"7 2\n",
"5 2\n"
],
"output": [
"2 4 5 6 7\n",
"-1\n",
"1 2 3 4 5 "
]
} |
818 | 7 | Have you ever played Hanabi? If not, then you've got to try it out! This problem deals with a simplified version of the game.
Overall, the game has 25 types of cards (5 distinct colors and 5 distinct values). Borya is holding n cards. The game is somewhat complicated by the fact that everybody sees Borya's cards excep... | input()
colour = {'R': 0, 'G': 1, 'B': 2, 'Y': 3, 'W': 4}
cards = {(colour[c], ord(v) - ord('1')) for c, v in input().split()}
def ok(cs, vs):
return len({
(c if (cs >> c) & 1 else -1, v if (vs >> v) & 1 else -1)
for c, v in cards
}) == len(cards)
print(min(bin(cs).count('1') + bin... | {
"input": [
"5\nB1 Y1 W1 G1 R1\n",
"4\nG4 R4 R3 B3\n",
"2\nG3 G3\n"
],
"output": [
"4\n",
"2\n",
"0\n"
]
} |
819 | 8 | Peter had a cube with non-zero length of a side. He put the cube into three-dimensional space in such a way that its vertices lay at integer points (it is possible that the cube's sides are not parallel to the coordinate axes). Then he took a piece of paper and wrote down eight lines, each containing three integers — c... | from itertools import permutations as p
d = lambda a, b: sum((i - j) ** 2 for i, j in zip(a, b))
f = lambda a, b: [i + j - k for i, j, k in zip(a, b, q)]
g = lambda t: sorted(sorted(q) for q in t)
v = [sorted(map(int, input().split())) for i in range(8)]
q = v.pop()
def yes(t, v):
print('YES')
d = [str(sorte... | {
"input": [
"0 0 0\n0 0 1\n0 0 1\n0 0 1\n0 1 1\n0 1 1\n0 1 1\n1 1 1\n",
"0 0 0\n0 0 0\n0 0 0\n0 0 0\n1 1 1\n1 1 1\n1 1 1\n1 1 1\n"
],
"output": [
"YES\n0 0 0 \n0 0 1 \n0 1 0 \n1 0 0 \n0 1 1 \n1 0 1 \n1 1 0 \n1 1 1 \n",
"NO\n"
]
} |
820 | 7 | Giga Tower is the tallest and deepest building in Cyberland. There are 17 777 777 777 floors, numbered from - 8 888 888 888 to 8 888 888 888. In particular, there is floor 0 between floor - 1 and floor 1. Every day, thousands of tourists come to this place to enjoy the wonderful view.
In Cyberland, it is believed t... | n=int(input())
def tower(n):
count=1
x=str(n)
while "8" not in str(n+1):
n+=1
count+=1
return count
print(tower(n)) | {
"input": [
"18\n",
"179\n",
"-1\n"
],
"output": [
"10\n",
"1\n",
"9\n"
]
} |
821 | 11 | Fox Ciel is participating in a party in Prime Kingdom. There are n foxes there (include Fox Ciel). The i-th fox is ai years old.
They will have dinner around some round tables. You want to distribute foxes such that:
1. Each fox is sitting at some table.
2. Each table has at least 3 foxes sitting around it.
3... | #E
def main():
sieve = [False, True] * 10001
for i in range(3, 140, 2):
if sieve[i]:
j, k = i * 2, i * i
le = (20001 - k) // j + 1
sieve[k::j] = [False] * le
n = int(input())
aa = list(map(int, input().split()))
pp = [-1] * n
def dget(v):
if ds... | {
"input": [
"24\n2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25\n",
"5\n2 2 2 2 2\n",
"4\n3 4 8 9\n",
"12\n2 3 4 5 6 7 8 9 10 11 12 13\n"
],
"output": [
"3\n8 1 2 3 24 5 6 23 4 \n10 7 8 9 12 15 14 13 16 11 10 \n6 17 18 21 20 19 22 \n",
"Impossible\n",
"1\n4 1 2 4 3 \n... |
822 | 7 | Karafs is some kind of vegetable in shape of an 1 × h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs.
<image>
Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is ... | A, B, n = map(int, input().split())
def s(i):
return A + i * B
def f(r):
return r * A + r * (r - 1) // 2 * B
for i in range(n):
l, t, m = map(int, input().split())
ans = -1
if s(l - 1) <= t:
L, R = l, 10 ** 6 + 1
while L + 1 < R:
M = (L + R) // 2
if f(M) - ... | {
"input": [
"1 5 2\n1 5 10\n2 7 4\n",
"2 1 4\n1 5 3\n3 3 10\n7 10 2\n6 4 8\n"
],
"output": [
"1\n2\n",
"4\n-1\n8\n-1\n"
]
} |
823 | 8 | Gerald bought two very rare paintings at the Sotheby's auction and he now wants to hang them on the wall. For that he bought a special board to attach it to the wall and place the paintings on the board. The board has shape of an a1 × b1 rectangle, the paintings have shape of a a2 × b2 and a3 × b3 rectangles.
Since th... | x, y = map(int, input().split())
a, b = map(int, input().split())
c, d = map(int, input().split())
def solve():
for X, Y in [(x, y), (y, x)]:
for A, B in [(a, b), (b, a)]:
for C, D in [(c, d), (d, c)]:
if A + C <= X and max(B, D) <= Y:
return 'YES'
return 'NO'
print(solve()) | {
"input": [
"3 2\n1 3\n2 1\n",
"5 5\n3 3\n3 3\n",
"4 2\n2 3\n1 2\n"
],
"output": [
"Yes\n",
"No\n",
"Yes\n"
]
} |
824 | 10 | The mobile application store has a new game called "Subway Roller".
The protagonist of the game Philip is located in one end of the tunnel and wants to get out of the other one. The tunnel is a rectangular field consisting of three rows and n columns. At the beginning of the game the hero is in some cell of the leftmo... | def R(i,j):
if i<n:
smas[j][i]=1
p=False
if mas[j][i+1]=='.':
if mas[j][i+3]=='.'and smas[j][i+3]==0:
p =p or R(i+3,j)
if j>0 and mas[j-1][i+1]=='.'and mas[j-1][i+3]=='.' and smas[j-1][i+3]==0:
p =p or R(i+3,j-1)
if j<2 and... | {
"input": [
"2\n16 4\n...AAAAA........\ns.BBB......CCCCC\n........DDDDD...\n16 4\n...AAAAA........\ns.BBB....CCCCC..\n.......DDDDD....\n",
"2\n10 4\ns.ZZ......\n.....AAABB\n.YYYYYY...\n10 4\ns.ZZ......\n....AAAABB\n.YYYYYY...\n"
],
"output": [
"YES\nNO\n",
"YES\nNO\n"
]
} |
825 | 11 | In the spirit of the holidays, Saitama has given Genos two grid paths of length n (a weird gift even by Saitama's standards). A grid path is an ordered sequence of neighbouring squares in an infinite grid. Two squares are neighbouring if they share a side.
One example of a grid path is (0, 0) → (0, 1) → (0, 2) → (1, 2... | def prefix(s):
v = [0]*len(s)
for i in range(1,len(s)):
k = v[i-1]
while k > 0 and s[k] != s[i]:
k = v[k-1]
if s[k] == s[i]:
k = k + 1
v[i] = k
return v
n = int(input())
n-=1
s1 = input()
s2 = input()
opos = {'W':'E', 'E':'W', 'N':'S', 'S':'N'}
s3 = '... | {
"input": [
"3\nNN\nSS\n",
"7\nNNESWW\nSWSWSW\n"
],
"output": [
"NO\n",
"YES\n"
]
} |
826 | 9 | <image>
You can preview the image in better quality by the link: [http://assets.codeforces.com/files/656/without-text.png](//assets.codeforces.com/files/656/without-text.png)
Input
The only line of the input is a string (between 1 and 50 characters long, inclusive). Each character will be an alphanumeric character o... | #!/usr/bin/env python
def main():
ords = list(map(ord, input()))
positive = [o - ord('A') + 1 for o in ords if ord('@') < o < ord('[')]
negative = [o - ord('a') + 1 for o in ords if ord('`') < o < ord('{')]
print(sum(positive) - sum(negative))
if __name__ == '__main__':
main() | {
"input": [
"APRIL.1st\n",
"Codeforces\n"
],
"output": [
"17\n",
"-87\n"
]
} |
827 | 9 | You are given n points on the straight line — the positions (x-coordinates) of the cities and m points on the same line — the positions (x-coordinates) of the cellular towers. All towers work in the same way — they provide cellular network for all cities, which are located at the distance which is no more than r from t... | def main():
from bisect import bisect_left
input()
aa = list(map(int, input().split()))
bb = -10 ** 10, *map(int, input().split()), 10 ** 10
for i, a in enumerate(aa):
j = bisect_left(bb, a)
u, v = bb[j] - a, a - bb[j - 1]
aa[i] = u if u < v else v
print(max(aa))
if __n... | {
"input": [
"3 2\n-2 2 4\n-3 0\n",
"5 3\n1 5 10 14 17\n4 11 15\n"
],
"output": [
"4\n",
"3\n"
]
} |
828 | 10 | You are given a string s, consisting of lowercase English letters, and the integer m.
One should choose some symbols from the given string so that any contiguous subsegment of length m has at least one selected symbol. Note that here we choose positions of symbols, not the symbols themselves.
Then one uses the chosen... | from collections import Counter
from string import ascii_lowercase as asc
m, s = int(input()), input()
g = Counter(s)
def solve(c):
p = 0
for q in ''.join(x if x >= c else ' ' for x in s).split():
i, j = 0, -1
while j + m < len(q):
j = q.rfind(c, j + 1, j + m + 1)
if j ... | {
"input": [
"3\ncbabc\n",
"3\nbcabcbaccba\n",
"2\nabcab\n"
],
"output": [
"a",
"aaabb",
"aab"
]
} |
829 | 10 | Innokentiy likes tea very much and today he wants to drink exactly n cups of tea. He would be happy to drink more but he had exactly n tea bags, a of them are green and b are black.
Innokentiy doesn't like to drink the same tea (green or black) more than k times in a row. Your task is to determine the order of brewing... | def swap(c):
if c == 'G':
return 'B'
else:
return 'G'
if __name__ == '__main__':
n, k, a, b = map(int, input().split())
bchar = 'G' if a > b else 'B'
schar = swap(bchar)
a, b = min(a, b), max(a, b)
res = []
while b > 0:
t1 = min(b, k)
t2 = (k * a) // b
res.append(bchar * t1)
b -= t1
if a > 0:
... | {
"input": [
"5 1 3 2\n",
"7 2 2 5\n",
"4 3 4 0\n"
],
"output": [
"GBGBG",
"BBGBBGB",
"NO\n"
]
} |
830 | 10 | Vasya has the sequence consisting of n integers. Vasya consider the pair of integers x and y k-interesting, if their binary representation differs from each other exactly in k bits. For example, if k = 2, the pair of integers x = 5 and y = 3 is k-interesting, because their binary representation x=101 and y=011 differs ... | def binary(n):
curr=0
while(n>0):
if(n%2):
curr+=1
n=n//2
return curr
l=input().split()
n=int(l[0])
k=int(l[1])
l=input().split()
li=[int(i) for i in l]
arr=[]
for i in range(2**15):
if(binary(i)==k):
arr.append(i)
hashi=dict()
for i in li:
if i in hashi:
... | {
"input": [
"6 0\n200 100 100 100 200 200\n",
"4 1\n0 3 2 1\n"
],
"output": [
"6\n",
"4\n"
]
} |
831 | 10 | Bankopolis is an incredible city in which all the n crossroads are located on a straight line and numbered from 1 to n along it. On each crossroad there is a bank office.
The crossroads are connected with m oriented bicycle lanes (the i-th lane goes from crossroad ui to crossroad vi), the difficulty of each of the lan... | import sys
from functools import lru_cache
input = sys.stdin.readline
# sys.setrecursionlimit(2 * 10**6)
def inpl():
return list(map(int, input().split()))
@lru_cache(maxsize=None)
def recur(v, s, e, k):
"""
vから初めて[s, e]の都市をk個まわる最小値は?
"""
if k == 0:
return 0
elif k > e - s + 1:
... | {
"input": [
"7 4\n4\n1 6 2\n6 2 2\n2 4 2\n2 7 1\n",
"4 3\n4\n2 1 2\n1 3 2\n3 4 2\n4 1 1\n"
],
"output": [
"6\n",
"3\n"
]
} |
832 | 7 | A few years ago, Hitagi encountered a giant crab, who stole the whole of her body weight. Ever since, she tried to avoid contact with others, for fear that this secret might be noticed.
To get rid of the oddity and recover her weight, a special integer sequence is needed. Hitagi's sequence has been broken for a long t... | def read(): return map(int, input().split())
n, k = read()
a = list(read())
b = list(sorted(read()))
for i in range(n):
if a[i] == 0:
a[i] = b.pop()
print("No" if sorted(a) == a else "Yes")
| {
"input": [
"4 2\n11 0 0 14\n5 4\n",
"6 1\n2 3 0 8 9 10\n5\n",
"7 7\n0 0 0 0 0 0 0\n1 2 3 4 5 6 7\n",
"4 1\n8 94 0 4\n89\n"
],
"output": [
"YES\n",
"NO\n",
"YES\n",
"YES\n"
]
} |
833 | 7 | Valery is very interested in magic. Magic attracts him so much that he sees it everywhere. He explains any strange and weird phenomenon through intervention of supernatural forces. But who would have thought that even in a regular array of numbers Valera manages to see something beautiful and magical.
Valera absolutel... | def sac(v,n):
p,d,t='',0,1
v.append('end')
"""subarray count"""
for c in v:
if c==p:
t+=1
continue
else:
if t>1:
d+=t*(t-1)//2
p=c
t=1
return d
n=int(input())
v=input().split()
print(n+sac(v,n)) | {
"input": [
"5\n-2 -2 -2 0 1\n",
"4\n2 1 1 4\n"
],
"output": [
"8\n",
"5\n"
]
} |
834 | 8 | Vlad likes to eat in cafes very much. During his life, he has visited cafes n times. Unfortunately, Vlad started to feel that his last visits are not any different from each other. To fix that Vlad had a small research.
First of all, Vlad assigned individual indices to all cafes. Then, he wrote down indices of cafes h... | def main():
input()
l = [999999] * 200001
for i, a in enumerate(map(int, input().split())):
l[a] = i
print(min(range(200001), key=l.__getitem__))
if __name__ == '__main__':
main()
| {
"input": [
"6\n2 1 2 2 4 1\n",
"5\n1 3 2 1 2\n"
],
"output": [
"2\n",
"3\n"
]
} |
835 | 8 | You are given an integer N. Consider all possible segments on the coordinate axis with endpoints at integer points with coordinates between 0 and N, inclusive; there will be <image> of them.
You want to draw these segments in several layers so that in each layer the segments don't overlap (they might touch at the endp... | def solve(n):
return int((n + n % 2) * (n + 2 - n % 2) / 4)
n = int(input())
print(solve(n)) | {
"input": [
"3\n",
"2\n",
"4\n"
],
"output": [
"4\n",
"2\n",
"6\n"
]
} |
836 | 10 | For an array b of length m we define the function f as
f(b) = \begin{cases} b[1] & if m = 1 \\\ f(b[1] ⊕ b[2],b[2] ⊕ b[3],...,b[m-1] ⊕ b[m]) & otherwise, \end{cases}
where ⊕ is [bitwise exclusive OR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
For example, f(1,2,4,8)=f(1⊕2,2⊕4,4⊕8)=f(3,6,12)=f(3⊕6... | def pyramid(arr, n):
mtx = [[0] * n for _ in range(n)]
for i in range(n):
mtx[0][i] = arr[i]
for i in range(1, n):
for j in range(n - i):
mtx[i][j] = mtx[i-1][j] ^ mtx[i-1][j+1]
for i in range(1, n):
for j in range(n - i):
mtx[i][j] = max(mtx[i][j], mtx[i-1][j], mtx[i-1][j+1])
... | {
"input": [
"6\n1 2 4 8 16 32\n4\n1 6\n2 5\n3 4\n1 2\n",
"3\n8 4 1\n2\n2 3\n1 2\n"
],
"output": [
"60\n30\n12\n3\n",
"5\n12\n"
]
} |
837 | 9 | Recently Monocarp got a job. His working day lasts exactly m minutes. During work, Monocarp wants to drink coffee at certain moments: there are n minutes a_1, a_2, ..., a_n, when he is able and willing to take a coffee break (for the sake of simplicity let's consider that each coffee break lasts exactly one minute).
... | from collections import deque
def main():
n, m, d = map(int, input().split())
aa = list(map(int, input().split()))
q, cnt = deque(((-d, 1),)), 1
for i in sorted(range(n), key=aa.__getitem__):
a = aa[i]
b, j = q.popleft()
if a <= b + d:
q.appendleft((b, j))
... | {
"input": [
"10 10 1\n10 5 7 4 6 3 2 1 9 8\n",
"4 5 3\n3 5 1 2\n"
],
"output": [
"2\n2 1 1 2 2 1 2 1 1 2\n",
"3\n3 1 1 2\n"
]
} |
838 | 8 | Colossal! — exclaimed Hawk-nose. — A programmer! That's exactly what we are looking for.
Arkadi and Boris Strugatsky. Monday starts on Saturday
Reading the book "Equations of Mathematical Magic" Roman Oira-Oira and Cristobal Junta found an interesting equation: a - (a ⊕ x) - x = 0 for some given a, where ⊕ stands for... | def solve():
a = int(input())
print(2**(bin(a)[2:].count('1')))
for _ in range(int(input())):
solve() | {
"input": [
"3\n0\n2\n1073741823\n"
],
"output": [
"1\n2\n1073741824\n"
]
} |
839 | 11 | Hiasat registered a new account in NeckoForces and when his friends found out about that, each one of them asked to use his name as Hiasat's handle.
Luckily for Hiasat, he can change his handle in some points in time. Also he knows the exact moments friends will visit his profile page. Formally, you are given a sequen... | import time
def find_max_clique(remain, size, max_, index, maxs):
# print(remain, size, max_)
result = max_
if size + len(remain) <= result:
# print('pruning (1)...')
return result
if not remain:
# print('trivial')
return size
while remain:
candidate = max(re... | {
"input": [
"5 3\n1\n2 motarack\n2 mike\n1\n2 light\n",
"4 3\n1\n2 alice\n2 bob\n2 tanyaromanova\n"
],
"output": [
"2",
"1"
]
} |
840 | 7 | Polycarp is going to participate in the contest. It starts at h_1:m_1 and ends at h_2:m_2. It is guaranteed that the contest lasts an even number of minutes (i.e. m_1 \% 2 = m_2 \% 2, where x \% y is x modulo y). It is also guaranteed that the entire contest is held during a single day. And finally it is guaranteed tha... | def q():
return list(map(int, input().split(':')))
h1, m1 = q()
h2, m2 = q()
a1 = h1 * 60 + m1
a2 = h2 * 60 + m2
res = (a1 + a2) // 2
print("{:02d}:{:02d}".format(res // 60, res % 60))
| {
"input": [
"01:02\n03:02\n",
"11:10\n11:12\n",
"10:00\n11:00\n"
],
"output": [
"02:02\n",
"11:11\n",
"10:30\n"
]
} |
841 | 10 | Now Serval is a junior high school student in Japari Middle School, and he is still thrilled on math as before.
As a talented boy in mathematics, he likes to play with numbers. This time, he wants to play with numbers on a rooted tree.
A tree is a connected graph without cycles. A rooted tree has a special vertex ca... | N = int(input())
T = [int(a) for a in input().split()]
P = [int(a)-1 for a in input().split()]
C = [[] for i in range(N)]
for i in range(N-1):
C[P[i]].append(i+1)
L = sum([1 for i in range(N) if len(C[i]) == 0])
DP = [-1] * N
def calc(i):
if len(C[i]) == 0:
DP[i] = 1
else:
X = [DP[j] for j i... | {
"input": [
"6\n1 0 1 1 0 1\n1 2 2 2 2\n",
"9\n1 1 0 0 1 0 1 0 1\n1 1 2 2 3 3 4 4\n",
"5\n1 0 1 0 1\n1 1 1 1\n",
"8\n1 0 0 1 0 1 1 0\n1 1 2 2 3 3 3\n"
],
"output": [
"1",
"5",
"4",
"4"
]
} |
842 | 9 | At first, there was a legend related to the name of the problem, but now it's just a formal statement.
You are given n points a_1, a_2, ..., a_n on the OX axis. Now you are asked to find such an integer point x on OX axis that f_k(x) is minimal possible.
The function f_k(x) can be described in the following way:
... | from collections import deque
def solve():
n, k = map(int, input().split())
l = list(map(int, input().split()))
ans = min((l[i] - l[i - k], l[i]) for i in range(k, n))
return (ans[1] - (ans[0] + 1) // 2)
if __name__ == '__main__':
out = deque()
for _ in range(int(input())):
out.append(... | {
"input": [
"3\n3 2\n1 2 5\n2 1\n1 1000000000\n1 0\n4\n"
],
"output": [
"3\n500000000\n4\n"
]
} |
843 | 8 | You are given a picture consisting of n rows and m columns. Rows are numbered from 1 to n from the top to the bottom, columns are numbered from 1 to m from the left to the right. Each cell is painted either black or white.
You think that this picture is not interesting enough. You consider a picture to be interesting... |
def main():
q = int(input())
for _ in range(q):
n,m = map(int,input().split())
tab = []
for x in range(n):
tab.append(input())
v = [0]*n
hor = [0]*m
for x in range(n):
for y in range(m):
if tab[x][y] == "*":
v[x] += 1
hor[y] += 1
mi = 1000*1000
for x in range(n):
for y in ... | {
"input": [
"9\n5 5\n..*..\n..*..\n*****\n..*..\n..*..\n3 4\n****\n.*..\n.*..\n4 3\n***\n*..\n*..\n*..\n5 5\n*****\n*.*.*\n*****\n..*.*\n..***\n1 4\n****\n5 5\n.....\n..*..\n.***.\n..*..\n.....\n5 3\n...\n.*.\n.*.\n***\n.*.\n3 3\n.*.\n*.*\n.*.\n4 4\n*.**\n....\n*.**\n*.**\n"
],
"output": [
"0\n0\n0\n0\n0... |
844 | 11 | The problem was inspired by Pied Piper story. After a challenge from Hooli's compression competitor Nucleus, Richard pulled an all-nighter to invent a new approach to compression: middle-out.
You are given two strings s and t of the same length n. Their characters are numbered from 1 to n from left to right (i.e. from... |
from collections import *
def go():
n,s,t=int(input()),input(),input()
if Counter(s)!=Counter(t): return -1
ans=0
for i in range(n):
k=0
for j in range(i,n):
while k<n and s[k] != t[j]: k += 1
if k == n: break
k += 1
ans = max(ans, j-i+1)
return n-ans
for _ in range(int(input())):
print(go()) | {
"input": [
"3\n9\niredppipe\npiedpiper\n4\nestt\ntest\n4\ntste\ntest\n",
"4\n1\na\nz\n5\nadhas\ndasha\n5\naashd\ndasha\n5\naahsd\ndasha\n"
],
"output": [
"2\n1\n2\n",
"-1\n2\n2\n3\n"
]
} |
845 | 7 | You are given a non-empty string s=s_1s_2... s_n, which consists only of lowercase Latin letters. Polycarp does not like a string if it contains at least one string "one" or at least one string "two" (or both at the same time) as a substring. In other words, Polycarp does not like the string s if there is an integer j ... | def main():
for _ in range(int(input())):
s = input().replace('twone', 'tw_ne').replace('one', 'o_e').replace('two', 't_o')
n = s.count('_')
print(n)
if n:
print(*[i for i, c in enumerate(s, 1) if c == '_'])
if __name__ == '__main__':
main()
| {
"input": [
"10\nonetwonetwooneooonetwooo\ntwo\none\ntwooooo\nttttwo\nttwwoo\nooone\nonnne\noneeeee\noneeeeeeetwooooo\n",
"4\nonetwone\ntestme\noneoneone\ntwotwo\n"
],
"output": [
"6\n2 6 10 13 18 21 \n1\n2 \n1\n2 \n1\n2 \n1\n5 \n0\n\n1\n4 \n0\n\n1\n2 \n2\n2 11 \n",
"2\n2 6 \n0\n\n3\n2 5 8 \n2\n2... |
846 | 8 | Mishka wants to buy some food in the nearby shop. Initially, he has s burles on his card.
Mishka can perform the following operation any number of times (possibly, zero): choose some positive integer number 1 ≤ x ≤ s, buy food that costs exactly x burles and obtain ⌊x/10⌋ burles as a cashback (in other words, Mishka ... | def f(n):
return n + (n-1)//9
t = int(input())
for _ in range(t):
n = int(input())
print(f(n))
| {
"input": [
"6\n1\n10\n19\n9876\n12345\n1000000000\n"
],
"output": [
"1\n11\n21\n10973\n13716\n1111111111\n"
]
} |
847 | 8 | Everybody knows that opposites attract. That is the key principle of the "Perfect Matching" dating agency. The "Perfect Matching" matchmakers have classified each registered customer by his interests and assigned to the i-th client number ti ( - 10 ≤ ti ≤ 10). Of course, one number can be assigned to any number of cust... | def main():
n = int(input())
l = [0] * 21
for t in map(int, input().split()):
l[t + 10] += 1
print(sum(a * b for a, b in zip(l[:10], l[::-1])) + l[10] * (l[10] - 1) // 2)
if __name__ == '__main__':
main() | {
"input": [
"5\n-3 3 0 0 3\n",
"3\n0 0 0\n"
],
"output": [
"3\n",
"3\n"
]
} |
848 | 10 | You have a tree of n vertices. You are going to convert this tree into n rubber bands on infinitely large plane. Conversion rule follows:
* For every pair of vertices a and b, rubber bands a and b should intersect if and only if there is an edge exists between a and b in the tree.
* Shape of rubber bands must be ... | import os
import sys
input = sys.stdin.buffer.readline
#sys.setrecursionlimit(int(3e5))
from collections import deque
from queue import PriorityQueue
import math
import copy
# list(map(int, input().split()))
#####################################################################################
class CF(object):
... | {
"input": [
"6\n1 3\n2 3\n3 4\n4 5\n4 6\n",
"4\n1 2\n2 3\n3 4\n"
],
"output": [
"4\n",
"2\n"
]
} |
849 | 9 | We call two numbers x and y similar if they have the same parity (the same remainder when divided by 2), or if |x-y|=1. For example, in each of the pairs (2, 6), (4, 3), (11, 7), the numbers are similar to each other, and in the pairs (1, 4), (3, 12), they are not.
You are given an array a of n (n is even) positive in... | def solve(a):
if len(list(filter(lambda x: x % 2, a))) % 2 == 0:
return "YES"
b = sorted(a)
return "YES" if 1 in [b[i+1] - b[i] for i in range(len(b)-1)] else "NO"
t = int(input())
for i_t in range(t):
input()
a = list(map(int, input().split(" ")))
print(solve(a)) | {
"input": [
"7\n4\n11 14 16 12\n2\n1 8\n4\n1 1 1 1\n4\n1 2 5 6\n2\n12 13\n6\n1 6 3 10 5 8\n6\n1 12 3 10 5 8\n"
],
"output": [
"YES\nNO\nYES\nYES\nYES\nYES\nNO\n"
]
} |
850 | 10 | There are n warriors in a row. The power of the i-th warrior is a_i. All powers are pairwise distinct.
You have two types of spells which you may cast:
1. Fireball: you spend x mana and destroy exactly k consecutive warriors;
2. Berserk: you spend y mana, choose two consecutive warriors, and the warrior with gr... | def update(a,b):
global res
if a > b:
return True
l = b-a+1
apply_x = False
mx = max(lst1[a:b+1])
if a-1 >= 0 and lst1[a-1] > mx:
apply_x = True
if b+1 <= n-1 and lst1[b+1] > mx:
apply_x = True
if not apply_x and l < k:
return False
rem = l%k
res += rem*y
l -= rem
if x <= y*k:
res += (l//k... | {
"input": [
"5 2\n5 2 3\n3 1 4 5 2\n3 5\n",
"4 4\n2 1 11\n1 3 2 4\n1 3 2 4\n",
"4 4\n5 1 4\n4 3 1 2\n2 4 3 1\n"
],
"output": [
"8\n",
"0\n",
"-1\n"
]
} |
851 | 10 | You are given an array a_1, a_2 ... a_n. Calculate the number of tuples (i, j, k, l) such that:
* 1 ≤ i < j < k < l ≤ n;
* a_i = a_k and a_j = a_l;
Input
The first line contains a single integer t (1 ≤ t ≤ 100) — the number of test cases.
The first line of each test case contains a single integer n (4 ≤ n ≤ ... | def solve():
n = int(input())
a = [int(x) for x in input().split()]
left = [0]*3001
cnt = 0
for j in range(n-1):
right = [0]*3001
for k in range(n-1,j,-1):
cnt = cnt + left[a[k]]*right[a[j]]
right[a[k]]+=1
left[a[j]]+=1
print(cnt)
def main():
t = int(input())
while t>0:
solve()
t-=1
if __name_... | {
"input": [
"2\n5\n2 2 2 2 2\n6\n1 3 3 1 2 3\n"
],
"output": [
"5\n2\n"
]
} |
852 | 12 | // We decided to drop the legend about the power sockets but feel free to come up with your own :^)
Define a chain:
* a chain of length 1 is a single vertex;
* a chain of length x is a chain of length x-1 with a new vertex connected to the end of it with a single edge.
You are given n chains of lengths l_1,... | def check(d, k):
pos, cur, P = 0, 1, 0
total = 1
for i, x in enumerate(L):
while cur == 0:
pos += 1
P += add[pos]
if pos + 1 >= d:
break
cur += P
cur -= 1
if pos + x//2 + 1 <= d:
... | {
"input": [
"3 5\n4 3 4\n",
"1 2\n3\n",
"2 10\n5 7\n",
"3 3\n4 3 3\n"
],
"output": [
"\n4\n",
"\n2\n",
"\n-1\n",
"\n3\n"
]
} |
853 | 8 | On a weekend, Qingshan suggests that she and her friend Daniel go hiking. Unfortunately, they are busy high school students, so they can only go hiking on scratch paper.
A permutation p is written from left to right on the paper. First Qingshan chooses an integer index x (1≤ x≤ n) and tells it to Daniel. After that, D... | n = int(input())
nums = list(map(int, input().split()))
left = [0]*n
right = [0]*n
for i in range(1, n):
if nums[i] > nums[i-1]:
left[i] = left[i-1]+1
for i in range(n-2, -1, -1):
if nums[i] > nums[i+1]:
right[i] = right[i+1]+1
def main():
ml = max(left)
mr = max(right)
if ml != mr... | {
"input": [
"5\n1 2 5 4 3\n",
"7\n1 2 4 6 5 3 7\n"
],
"output": [
"\n1\n",
"\n0\n"
]
} |
854 | 7 | You've gotten an n × m sheet of squared paper. Some of its squares are painted. Let's mark the set of all painted squares as A. Set A is connected. Your task is to find the minimum number of squares that we can delete from set A to make it not connected.
A set of painted squares is called connected, if for every two s... | n, m = map(int, input().split())
a = [list(input()) for i in range(n)]
dirs = [(0, 1), (1, 0), (0, -1), (-1, 0)]
def valid(x, y):
return 0 <= x < n and 0 <= y < m
def dfs():
cnt, p = 0, -1
for i in range(n):
for j in range(m):
if a[i][j] == '#':
cnt += 1
... | {
"input": [
"5 4\n####\n#..#\n#..#\n#..#\n####\n",
"5 5\n#####\n#...#\n#####\n#...#\n#####\n"
],
"output": [
"2\n",
"2\n"
]
} |
855 | 7 | Bajtek is learning to skate on ice. He's a beginner, so his only mode of transportation is pushing off from a snow drift to the north, east, south or west and sliding until he lands in another snow drift. He has noticed that in this way it's impossible to get from some snow drifts to some other by any sequence of moves... | n=int(input())
rank=[-1]*9999
def f(x):
if rank[x]<0:rank[x]=x
if x!=rank[x]:rank[x]=f(rank[x])
return rank[x]
for _ in[0]*n:
a,b=map(int,input().split())
b+=1000
n-=(rank[a]>=0)+(rank[b]>=0)-(f(a)==f(b))
rank[f(a)]=f(b)
print(n-1) | {
"input": [
"2\n2 1\n1 2\n",
"2\n2 1\n4 1\n"
],
"output": [
"1\n",
"0\n"
]
} |
856 | 7 | Overall there are m actors in Berland. Each actor has a personal identifier — an integer from 1 to m (distinct actors have distinct identifiers). Vasya likes to watch Berland movies with Berland actors, and he has k favorite actors. He watched the movie trailers for the next month and wrote the following information fo... | import sys
try:
sys.stdin = open('input.txt')
sys.stdout = open('output.txt', 'w')
except:
pass
def compl(n, s):
return set(filter(lambda x: x not in s, range(1, n + 1)))
m, k = list(map(int, input().split()))
id = list(map(int, input().split()))
n = int(input())
favorite = set(id)
... | {
"input": [
"5 3\n1 3 5\n4\njumanji\n3\n0 0 0\ntheeagle\n5\n1 2 3 4 0\nmatrix\n3\n2 4 0\nsourcecode\n2\n2 4\n",
"5 3\n1 2 3\n6\nfirstfilm\n3\n0 0 0\nsecondfilm\n4\n0 0 4 5\nthirdfilm\n1\n2\nfourthfilm\n1\n5\nfifthfilm\n1\n4\nsixthfilm\n2\n1 0\n"
],
"output": [
"2\n0\n1\n1\n",
"2\n2\n1\n1\n1\n2\n"... |
857 | 10 | Squirrel Liss is interested in sequences. She also has preferences of integers. She thinks n integers a1, a2, ..., an are good.
Now she is interested in good sequences. A sequence x1, x2, ..., xk is called good if it satisfies the following three conditions:
* The sequence is strictly increasing, i.e. xi < xi + 1 f... | # Legends Always Come Up with Solution
# Author: Manvir Singh
import os
import sys
from io import BytesIO, IOBase
from collections import Counter
def seieve_prime_factorisation(n):
p, i = [1] * (n + 1), 2
while i * i <= n:
if p[i] == 1:
for j in range(i * i, n + 1, i):
p[j]... | {
"input": [
"9\n1 2 3 5 6 7 8 9 10\n",
"5\n2 3 4 6 9\n"
],
"output": [
"4\n",
"4\n"
]
} |
858 | 10 | Little penguin Polo loves his home village. The village has n houses, indexed by integers from 1 to n. Each house has a plaque containing an integer, the i-th house has a plaque containing integer pi (1 ≤ pi ≤ n).
Little penguin Polo loves walking around this village. The walk looks like that. First he stands by a hou... | mod=10**9+7
def power(x, a):
if(a==0):
return(1)
z=power(x, a//2)
z=(z*z)%mod
if(a%2):
z=(z*x)%mod
return(z)
[n, k]=list(map(int, input().split()))
print((power(n-k, n-k)*power(k, k-1))%mod) | {
"input": [
"5 2\n",
"7 4\n"
],
"output": [
"54\n",
"1728\n"
]
} |
859 | 7 | The end of the school year is near and Ms. Manana, the teacher, will soon have to say goodbye to a yet another class. She decided to prepare a goodbye present for her n students and give each of them a jigsaw puzzle (which, as wikipedia states, is a tiling puzzle that requires the assembly of numerous small, often oddl... | def mi():
return map(int, input().split())
n,m = mi()
a = sorted(list(mi()))
poss = 1e10
for i in range(m-n+1):
poss = min(poss,a[i+n-1]-a[i])
print (poss) | {
"input": [
"4 6\n10 12 10 7 5 22\n"
],
"output": [
"5\n"
]
} |
860 | 9 | After a terrifying forest fire in Berland a forest rebirth program was carried out. Due to it N rows with M trees each were planted and the rows were so neat that one could map it on a system of coordinates so that the j-th tree in the i-th row would have the coordinates of (i, j). However a terrible thing happened and... | '''
___ ____
____ _____ _____/ (_)_ ______ ____ _____/ / /_ __ ______ ___ __
/ __ `/ __ `/ __ / / / / / __ \/ __ `/ __ / __ \/ / / / __ `/ / / /
/ /_/ / /_/ / /_/ / / /_/ / /_/ / /_/ / /_/ / / / / /_/ / /_/ / /_/ /
\__,_/\__,_/\__,_/_/\__,_/ .___/\__... | {
"input": [
"3 3\n1\n2 2\n",
"3 3\n1\n1 1\n",
"3 3\n2\n1 1 3 3\n"
],
"output": [
"1 1\n",
"3 3\n",
"2 2"
]
} |
861 | 7 | Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of mi... | def main():
input()
i = res = 0
for c in input()[::2]:
if c == "1":
i += 1
else:
res += i
print(res)
if __name__ == '__main__':
main()
| {
"input": [
"5\n1 0 1 0 1\n",
"4\n0 0 1 0\n"
],
"output": [
"3\n",
"1\n"
]
} |
862 | 7 | Quite recently, a very smart student named Jury decided that lectures are boring, so he downloaded a game called "Black Square" on his super cool touchscreen phone.
In this game, the phone's screen is divided into four vertical strips. Each second, a black square appears on some of the strips. According to the rules o... | def ans(a,n):
s = 0
for i in n:
s += a[int(i)-1]
return s
a = list(map(int, input().split()))
n = input()
print(ans(a,n)) | {
"input": [
"1 2 3 4\n123214\n",
"1 5 3 2\n11221\n"
],
"output": [
"13\n",
"13\n"
]
} |
863 | 9 | Twilight Sparkle learnt that the evil Nightmare Moon would return during the upcoming Summer Sun Celebration after one thousand years of imprisonment on the moon. She tried to warn her mentor Princess Celestia, but the princess ignored her and sent her to Ponyville to check on the preparations for the celebration.
<im... | import sys
input = sys.stdin.readline
def solve():
n, m = map(int, input().split())
g = [[] for i in range(n+1)]
for i in range(m):
u, v = map(int, input().split())
g[u].append(v)
g[v].append(u)
a = list(map(int, input().split()))
s = None
for i in range(1,n+1):
if a[i-1] == 1:
s = i
break
if s i... | {
"input": [
"2 0\n0 0\n",
"3 2\n1 2\n2 3\n1 1 1\n",
"5 7\n1 2\n1 3\n1 4\n1 5\n3 4\n3 5\n4 5\n0 1 0 1 0\n"
],
"output": [
"0\n",
"7\n1 2 3 2 1 2 1 ",
"10\n2 1 3 4 5 4 5 4 3 1 "
]
} |
864 | 7 | Dreamoon wants to climb up a stair of n steps. He can climb 1 or 2 steps at each move. Dreamoon wants the number of moves to be a multiple of an integer m.
What is the minimal number of moves making him climb to the top of the stairs that satisfies his condition?
Input
The single line contains two space separated i... | def f():
n,m=map(int,input().split())
for i in range(-(-n//2),n+1):
if i%m==0:
return i
return -1
print(f()) | {
"input": [
"10 2\n",
"3 5\n"
],
"output": [
"6\n",
"-1\n"
]
} |
865 | 8 | Vasya studies positional numeral systems. Unfortunately, he often forgets to write the base of notation in which the expression is written. Once he saw a note in his notebook saying a + b = ?, and that the base of the positional notation wasn’t written anywhere. Now Vasya has to choose a base p and regard the expressio... | def s():
[a,b] = input().split()
m = int(max(max(a),max(b)))+1
a = int(a)
b = int(b)
c = 0
r = 0
while a or b or c:
r += 1
c = (a%10+b%10+c)//m
a//=10
b//=10
print(r)
s() | {
"input": [
"78 87\n",
"1 1\n"
],
"output": [
"3\n",
"2\n"
]
} |
866 | 10 | A social network for dogs called DH (DogHouse) has k special servers to recompress uploaded videos of cute cats. After each video is uploaded, it should be recompressed on one (any) of the servers, and only after that it can be saved in the social network.
We know that each server takes one second to recompress a one ... | import heapq
def __main__(n, k):
servers = [0] * k
times = []
for i in range(n):
s, m = list(map(int, input().split()))
time = max(servers[0], s)
heapq.heapreplace(servers, time + m)
times.append(time + m)
print('\n'.join(str(time) for time in times))
if __name__ == ... | {
"input": [
"6 1\n1 1000000000\n2 1000000000\n3 1000000000\n4 1000000000\n5 1000000000\n6 3\n",
"3 2\n1 5\n2 5\n3 5\n"
],
"output": [
"1000000001\n2000000001\n3000000001\n4000000001\n5000000001\n5000000004\n",
"6\n7\n11\n"
]
} |
867 | 8 | The Hedgehog recently remembered one of his favorite childhood activities, — solving puzzles, and got into it with new vigor. He would sit day in, day out with his friend buried into thousands of tiny pieces of the picture, looking for the required items one by one.
Soon the Hedgehog came up with a brilliant idea: ins... | n, m = map(int, input().split())
grid = [ input().strip() for r in range(n) ]
def flatten(piece):
return '.'.join([ ''.join(row) for row in piece ])
def rotate(piece):
n, m = len(piece), len(piece[0])
rotated = [ [ None for c in range(n) ] for r in range(m) ]
for r in range(n):
for c in range(... | {
"input": [
"2 4\nABDC\nABDC\n",
"2 6\nABCCBA\nABCCBA\n"
],
"output": [
"3\n2 1\n",
"1\n2 6\n"
]
} |
868 | 7 | Today Patrick waits for a visit from his friend Spongebob. To prepare for the visit, Patrick needs to buy some goodies in two stores located near his house. There is a d1 meter long road between his house and the first shop and a d2 meter long road between his house and the second shop. Also, there is a road of length ... | def main():
a, b, c = map(int, input().split())
res = min(a+c+b, 2*(a+b), 2*(b+c), 2*(a+c))
print(res)
main() | {
"input": [
"10 20 30\n",
"1 1 5\n"
],
"output": [
"60\n",
"4\n"
]
} |
869 | 11 | The Romans have attacked again. This time they are much more than the Persians but Shapur is ready to defeat them. He says: "A lion is never afraid of a hundred sheep".
Nevertheless Shapur has to find weaknesses in the Roman army to defeat them. So he gives the army a weakness number.
In Shapur's opinion the weaknes... | def update(bit, i):
while (i < len(bit)):
bit[i] += 1
i += (i & (-i))
def getsum(bit, i):
res = 0
while (i > 0):
res += bit[i]
i -= (i & (-i))
return res
n = int(input())
arr = list(map(int, input().split()))
d = {}
l = list(sorted(set(arr)))
ind = 1
for i in range(le... | {
"input": [
"4\n1 5 4 3\n",
"3\n3 2 1\n",
"3\n2 3 1\n",
"4\n10 8 3 1\n"
],
"output": [
"1",
"1",
"0",
"4"
]
} |
870 | 10 | A revolution took place on the Buka Island. New government replaced the old one. The new government includes n parties and each of them is entitled to some part of the island according to their contribution to the revolution. However, they can't divide the island.
The island can be conventionally represented as two re... | import sys
from array import array # noqa: F401
def input():
return sys.stdin.buffer.readline().decode('utf-8')
a, b, c, d, n = map(int, input().split())
party = list(map(int, input().split()))
island = [['.'] * (a + c) for _ in range(max(b, d))]
i, j, ub, lb = (b - 1 if a % 2 else 0), 0, 0, b
delta = (-1 if ... | {
"input": [
"3 2 1 4 4\n1 2 3 4\n",
"3 4 2 2 3\n5 8 3\n"
],
"output": [
"YES\nbbcd\naccd\n...d\n...d\n",
"YES\naabbc\nabbcc\nabb..\nabb..\n"
]
} |
871 | 8 | Little Artem found a grasshopper. He brought it to his house and constructed a jumping area for him.
The area looks like a strip of cells 1 × n. Each cell contains the direction for the next jump and the length of that jump. Grasshopper starts in the first cell and follows the instructions written on the cells. Grassh... | def sg(c):
return -1 if c == '<' else 1
n = int(input())
drs = list(map(sg, input()))
lns = list(map(int, input().split(' ')))
msk = [0] * n
pos = 0
while 0 <= pos < n:
if msk[pos] == 1:
print('INFINITE')
exit()
msk[pos] = 1
pos += drs[pos]*lns[pos]
print('FINITE') | {
"input": [
"3\n>><\n2 1 1\n",
"2\n><\n1 2\n"
],
"output": [
"FINITE\n",
"FINITE\n"
]
} |
872 | 10 | Heidi the Cow is aghast: cracks in the northern Wall? Zombies gathering outside, forming groups, preparing their assault? This must not happen! Quickly, she fetches her HC2 (Handbook of Crazy Constructions) and looks for the right chapter:
How to build a wall:
1. Take a set of bricks.
2. Select one of the possibl... | result=0
mod=10**6 +3
n,C=map(int,input().split())
def fact(n):
fact=1
for i in range(1,n+1):
fact=(fact*i)%mod
return fact
def pow(a,b):
p=1
for i in range(b):
p=(a*p)% mod
return p
result=fact(n+C)*pow(fact(n),mod-2)*pow(fact(C),mod-2)-1
print(int(result%mod)) | {
"input": [
"37 63\n",
"11 5\n",
"3 2\n",
"2 2\n",
"5 1\n"
],
"output": [
"230574\n",
"4367\n",
"9\n",
"5\n",
"5\n"
]
} |
873 | 7 | On the way to Rio de Janeiro Ostap kills time playing with a grasshopper he took with him in a special box. Ostap builds a line of length n such that some cells of this line are empty and some contain obstacles. Then, he places his grasshopper to one of the empty cells and a small insect in another empty cell. The gras... | def solve(s, k):
i = min(s.index('G'), s.index('T')) + k
while i < len(s):
if s[i] == '#': return "NO"
if s[i] in ['G', 'T']: return "YES"
i += k
return "NO"
n, k = map(int, input().split())
print(solve(input(), k))
| {
"input": [
"6 1\nT....G\n",
"6 2\n..GT..\n",
"7 3\nT..#..G\n",
"5 2\n#G#T#\n"
],
"output": [
"YES\n",
"NO\n",
"NO\n",
"YES\n"
]
} |
874 | 7 | Can you imagine our life if we removed all zeros from it? For sure we will have many problems.
In this problem we will have a simple example if we removed all zeros from our life, it's the addition operation. Let's assume you are given this equation a + b = c, where a and b are positive integers, and c is the sum of a... | def fun(x):
x = str(x).replace("0", "")
return int(x)
a = int(input())
b = int(input())
print("YES" if fun(a) + fun(b) == fun(a + b) else "NO")
| {
"input": [
"105\n106\n",
"101\n102\n"
],
"output": [
"NO\n",
"YES\n"
]
} |
875 | 11 | Have you ever tasted Martian food? Well, you should.
Their signature dish is served on a completely black plate with the radius of R, flat as a pancake.
First, they put a perfectly circular portion of the Golden Honduras on the plate. It has the radius of r and is located as close to the edge of the plate as possible... | #!/usr/bin/env python3
def solve(R,r,k):
# Thanks to Numberphile's "Epic circles" video
# Use the formula for radii of circles in Pappus chain
r = r / R
n = k
answer = ((1-r)*r)/(2*((n**2)*((1-r)**2)+r))
# Note that in a Pappus chain the diameter of the circle is 1, so we need to scale up:
answer = 2*R *... | {
"input": [
"2\n4 3 1\n4 2 2\n"
],
"output": [
"0.9230769231\n0.6666666667\n"
]
} |
876 | 10 | The main city magazine offers its readers an opportunity to publish their ads. The format of the ad should be like this:
There are space-separated non-empty words of lowercase and uppercase Latin letters.
There are hyphen characters '-' in some words, their positions set word wrapping points. Word can include more th... | n=int(input())
s=input()
lo,hi=0,2000000
ans=1000000
c=0
l=[]
for i in s:
c+=1
if i=='-' or i==' ':
l.append(c)
c=0
l.append(c)
#print(l)
def possible(x):
rows=1
curr=0
for i in l:
if (curr+i)<=x:
curr+=i
elif i>x:
return False
else:
rows+=1
curr=i
# print(x,rows)
return rows<=n
while lo<=hi... | {
"input": [
"4\nEdu-ca-tion-al Ro-unds are so fun\n",
"4\ngarage for sa-le\n"
],
"output": [
"10\n",
"7\n"
]
} |
877 | 8 | Polycarp has a checkered sheet of paper of size n × m. Polycarp painted some of cells with black, the others remained white. Inspired by Malevich's "Black Square", Polycarp wants to paint minimum possible number of white cells with black so that all black cells form a square.
You are to determine the minimum possible ... | def aaa():
r,c=map(int,input().split())
rr,cc,stop=[],[],1
for ro in range(r):
for co,ch in enumerate(input()):
if ch=="B":
rr.append(ro)
cc.append(co)
stop=0
if stop: return 1
rh,cw=max(rr)-min(rr)+1,max(cc)-min(cc)+1
sl=max(rh,cw)
if sl>r or sl>c: return -1
return sl*sl-len(rr)
print(aaa())
... | {
"input": [
"1 2\nBB\n",
"5 4\nWWWW\nWWWB\nWWWB\nWWBB\nWWWW\n",
"3 3\nWWW\nWWW\nWWW\n"
],
"output": [
"-1",
"5",
"1"
]
} |
878 | 9 | Perhaps many have heard that the World Biathlon Championship has finished. Although our hero Valera was not present at this spectacular event himself and only watched it on TV, it excited him so much that he decided to enroll in a biathlon section.
Of course, biathlon as any sport, proved very difficult in practice. I... | def main():
from array import array
from bisect import bisect
from sys import stdin
input = stdin.readline
O = -1
n = int(input())
xr = []
for i in range(n):
xi, ri = map(int, input().split())
xr.append((xi, ri ** 2, i))
xr.sort()
cur = 1
res1 = 0
res2 = a... | {
"input": [
"3\n3 2\n7 1\n11 2\n4\n2 1\n6 0\n6 4\n11 2\n",
"3\n2 1\n5 2\n10 1\n5\n0 1\n1 3\n3 0\n4 0\n4 0\n"
],
"output": [
"3\n1 2 4 \n",
"2\n3 3 -1 \n"
]
} |
879 | 11 | Ann and Borya have n piles with candies and n is even number. There are ai candies in pile with number i.
Ann likes numbers which are square of some integer and Borya doesn't like numbers which are square of any integer. During one move guys can select some pile with candies and add one candy to it (this candy is new ... | import math as m
n = int(input())
a = [int(z) for z in input().split()]
ans = 0
def sq(a):
tmp = int(m.sqrt(a))
s = tmp * tmp
tmp += 1
b = tmp * tmp
if abs(a - s) < abs(a - b):
return abs(a - s)
else:
return abs(a - b)
d = []
for i in range(n):
d.append((sq(a[i]), a[i]))
d.so... | {
"input": [
"6\n0 0 0 0 0 0\n",
"6\n120 110 23 34 25 45\n",
"10\n121 56 78 81 45 100 1 0 54 78\n",
"4\n12 14 30 4\n"
],
"output": [
"6\n",
"3\n",
"0\n",
"2\n"
]
} |
880 | 11 | Yes, that's another problem with definition of "beautiful" numbers.
Let's call a positive integer x beautiful if its decimal representation without leading zeroes contains even number of digits, and there exists a permutation of this representation which is palindromic. For example, 4242 is a beautiful number, since i... | '''
Auther: ghoshashis545 Ashis Ghosh
College: Jalpaiguri Govt Enggineering College
'''
from os import path
from io import BytesIO, IOBase
import sys
from heapq import heappush,heappop
from functools import cmp_to_key as ctk
from collections import deque,Counter,defaultdict as dd
from bisect import bisect,bis... | {
"input": [
"4\n89\n88\n1000\n28923845\n"
],
"output": [
"88\n77\n99\n28923839\n"
]
} |
881 | 9 | Ivar the Boneless is a great leader. He is trying to capture Kattegat from Lagertha. The war has begun and wave after wave Ivar's warriors are falling in battle.
Ivar has n warriors, he places them on a straight line in front of the main gate, in a way that the i-th warrior stands right after (i-1)-th warrior. The fir... | from bisect import bisect_right
def bs(s,x):
return bisect_right(s,x)
n,q=map(int,input().split())
l=list(map(int,input().split()))
k=list(map(int,input().split()))
s=[0]
for i in range(n):
s.append(s[i]+l[i])
p=0
for i in range(q):
x=bs(s,(k[i]+p))
p=p+k[i]
if x==n+1:
print(n)
p=0... | {
"input": [
"4 4\n1 2 3 4\n9 1 10 6\n",
"5 5\n1 2 1 2 1\n3 10 1 1 1\n"
],
"output": [
"1\n4\n4\n1\n",
"3\n5\n4\n4\n3\n"
]
} |
882 | 8 | Allen is hosting a formal dinner party. 2n people come to the event in n pairs (couples). After a night of fun, Allen wants to line everyone up for a final picture. The 2n people line up, but Allen doesn't like the ordering. Allen prefers if each pair occupies adjacent positions in the line, as this makes the picture m... |
def inp():
return list(map(int,input().split()))
n,=inp()
l=inp()
ans=0
for i in range(n-1):
x=l.pop(0)
ans+=l.index(x)
l.remove(x)
print(ans)
| {
"input": [
"4\n1 1 2 3 3 2 4 4\n",
"3\n1 1 2 2 3 3\n",
"3\n3 1 2 3 1 2\n"
],
"output": [
"2\n",
"0\n",
"3\n"
]
} |
883 | 10 | Let's call an undirected graph G = (V, E) relatively prime if and only if for each edge (v, u) ∈ E GCD(v, u) = 1 (the greatest common divisor of v and u is 1). If there is no edge between some pair of vertices v and u then the value of GCD(v, u) doesn't matter. The vertices are numbered from 1 to |V|.
Construct a rela... | def gcd(a,b):return a if not b else gcd(b,a%b)
n,m=map(int,input().split())
ans,cnt='Possible',0
for i in range(1,n+1):
for j in range(i+1,n+1):
if gcd(i,j)==1:
cnt+=1; ans+='\n%d %d'%(i,j)
if cnt==m: break
if cnt==m: break
print(ans if cnt==m and m>n-2 else 'Impossible') | {
"input": [
"5 6\n",
"6 12\n"
],
"output": [
"Possible\n1 2\n1 3\n1 4\n1 5\n2 3\n2 5\n",
"Impossible\n"
]
} |
884 | 7 | The king's birthday dinner was attended by k guests. The dinner was quite a success: every person has eaten several dishes (though the number of dishes was the same for every person) and every dish was served alongside with a new set of kitchen utensils.
All types of utensils in the kingdom are numbered from 1 to 100.... | def okr(a):
if a == int(a):
return int(a)
else: return int(a) + 1
n, k = map(int, input().split())
li = map(int, input().split())
num = [0]*100
coun = [0]*100
for i in li:
i -= 1
coun[i] = 1
num[i] += 1
nume = sum(coun)
maxx = max(num)
prev = okr(maxx/k)*nume*k
print(prev - n) | {
"input": [
"10 3\n1 3 3 1 3 5 5 5 5 100\n",
"5 2\n1 2 2 1 3\n"
],
"output": [
"14\n",
"1\n"
]
} |
885 | 10 | From "ftying rats" to urban saniwation workers - can synthetic biology tronsform how we think of pigeons?
The upiquitous pigeon has long been viewed as vermin - spleading disease, scavenging through trush, and defecating in populous urban spases. Yet they are product of selextive breeding for purposes as diverse as r... | def li():
return list(map(int, input().split(" ")))
input()
ans = 2;
a = li()
ans += min(a)^a[2]
print(ans) | {
"input": [
"5\n1 2 3 4 5\n"
],
"output": [
"4\n"
]
} |
886 | 9 | The legend of the foundation of Vectorland talks of two integers x and y. Centuries ago, the array king placed two markers at points |x| and |y| on the number line and conquered all the land in between (including the endpoints), which he declared to be Arrayland. Many years later, the vector king placed markers at poin... | def main():
n = int(input())
a = sorted(map(abs, map(int, input().split())))
i = 0
res = 0
for j in range(n):
while a[i] * 2 < a[j]:
i += 1
res += j - i
print(res)
main()
| {
"input": [
"2\n3 6\n",
"3\n2 5 -3\n"
],
"output": [
"1\n",
"2\n"
]
} |
887 | 7 | Polycarp decided to relax on his weekend and visited to the performance of famous ropewalkers: Agafon, Boniface and Konrad.
The rope is straight and infinite in both directions. At the beginning of the performance, Agafon, Boniface and Konrad are located in positions a, b and c respectively. At the end of the performa... | def main():
a, b, c, d = (int(x) for x in input().split())
a, b, c = sorted([a, b, c])
return max(d - (c - b), 0) + max(d - (b - a), 0)
print(main()) | {
"input": [
"5 2 6 3\n",
"2 3 10 4\n",
"3 1 5 6\n",
"8 3 3 2\n"
],
"output": [
"2\n",
"3\n",
"8\n",
"2\n"
]
} |
888 | 11 | There are n boxers, the weight of the i-th boxer is a_i. Each of them can change the weight by no more than 1 before the competition (the weight cannot become equal to zero, that is, it must remain positive). Weight is always an integer number.
It is necessary to choose the largest boxing team in terms of the number o... |
def solve(w):
w.sort(reverse=True)
last = w[0] + 1
res = 1
for e in w[1:]:
for d in range(1, -2, -1):
if 0 < e + d < last:
last = e + d
res += 1
break
return res
input()
w = list(map(int, input().split()))
print(solve(w))
| {
"input": [
"6\n1 1 1 4 4 4\n",
"4\n3 2 4 1\n"
],
"output": [
"5\n",
"4\n"
]
} |
889 | 9 | Mike and Ann are sitting in the classroom. The lesson is boring, so they decided to play an interesting game. Fortunately, all they need to play this game is a string s and a number k (0 ≤ k < |s|).
At the beginning of the game, players are given a substring of s with left border l and right border r, both equal to k ... | def solve(s):
curr = chr(ord('z') + 1)
for i in range(len(s)):
c = s[i]
if c <= curr:
curr = c
print("Mike")
else:
print("Ann")
solve(input())
| {
"input": [
"abba\n",
"cba\n"
],
"output": [
"Mike\nAnn\nAnn\nMike\n",
"Mike\nMike\nMike\n"
]
} |
890 | 11 | Hyakugoku has just retired from being the resident deity of the South Black Snail Temple in order to pursue her dream of becoming a cartoonist. She spent six months in that temple just playing "Cat's Cradle" so now she wants to try a different game — "Snakes and Ladders". Unfortunately, she already killed all the snake... | h = [list(map(int, input().split())) for _ in range(10)]
tp = {}
def celli(x, y):
if x % 2:
return (9 - x) * 10 + y
return (9 - x) * 10 + 9 - y
for i in range(10):
for j in range(10):
if h[i][j]:
tp[celli(i, j)] = celli(i - h[i][j], j)
dp = [0] * 94 + [6] * 5 + [0]
for i in range(93, -1, -1):
tot = 0
f... | {
"input": [
"0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 6 6 6 6 6 6 0 0 0\n1 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n",
"0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0... |
891 | 9 | So the Beautiful Regional Contest (BeRC) has come to an end! n students took part in the contest. The final standings are already known: the participant in the i-th place solved p_i problems. Since the participants are primarily sorted by the number of solved problems, then p_1 ≥ p_2 ≥ ... ≥ p_n.
Help the jury distrib... | from sys import stdin
def rl():
return [int(w) for w in stdin.readline().split()]
k, = rl()
for _ in range(k):
n, = rl()
p = rl()
g = 1
while g < n and p[g-1] == p[g]:
g += 1
s = g + 1
while g + s < n and p[g+s-1] == p[g+s]:
s += 1
a = n//2
while a > 0 and p[a-1] ... | {
"input": [
"5\n12\n5 4 4 3 2 2 1 1 1 1 1 1\n4\n4 3 2 1\n1\n1000000\n20\n20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1\n32\n64 64 63 58 58 58 58 58 37 37 37 37 34 34 28 28 28 28 28 28 24 24 19 17 17 17 17 16 16 16 16 11\n"
],
"output": [
"1 2 3\n0 0 0\n0 0 0\n1 2 7\n2 6 6\n"
]
} |
892 | 8 | Let's assume that we have a pair of numbers (a, b). We can get a new pair (a + b, b) or (a, a + b) from the given pair in a single step.
Let the initial pair of numbers be (1,1). Your task is to find number k, that is, the least number of steps needed to transform (1,1) into the pair where at least one number equals n... | def calc(n,m):
ans=0
while(m>1):
ans+=n//m
n,m=m,n%m
if m==0:
return float("inf")
return ans+n-1
n=int(input())
ans=n-1
for i in range(1,n+1):
ans=min(ans,calc(n,i))
print(ans)
| {
"input": [
"5\n",
"1\n"
],
"output": [
"3\n",
"0\n"
]
} |
893 | 8 | Ashish has an array a of consisting of 2n positive integers. He wants to compress a into an array b of size n-1. To do this, he first discards exactly 2 (any two) elements from a. He then performs the following operation until there are no elements left in a:
* Remove any two elements from a and append their sum to... | def main():
n = int(input())
a = enumerate(list(map(lambda x: int(x)%2, input().split(' '))))
a = sorted(a, key = lambda pair: pair[1])
i=0
while i<n-1 and a[2*i+1][1] == 0:
print(a[2*i][0]+1, a[2*i+1][0]+1)
i+=1
j=0
a = list(reversed(a))
while i+j < n-1:
print(a[... | {
"input": [
"3\n3\n1 2 3 4 5 6\n2\n5 7 9 10\n5\n1 3 3 4 5 90 100 101 2 3\n"
],
"output": [
"2 4\n1 3\n1 2\n4 6\n7 9\n1 2\n3 5\n"
]
} |
894 | 9 | You are given an array a consisting of n integers numbered from 1 to n.
Let's define the k-amazing number of the array as the minimum number that occurs in all of the subsegments of the array having length k (recall that a subsegment of a of length k is a contiguous part of a containing exactly k elements). If there i... | def solve(n, a):
maxD = [1] * (n + 1)
lastP = [-1] * (n + 1)
for i in range(n):
d = i - lastP[a[i]]
maxD[a[i]] = max(maxD[a[i]], d)
lastP[a[i]] = i
res = [-1] * n
k = n
for v in range(1, n + 1):
if lastP[v] == -1:
continue
minK = max(maxD[v], n - lastP[v])
while k >= minK:
res[k - 1] = v
k -=... | {
"input": [
"3\n5\n1 2 3 4 5\n5\n4 4 4 4 2\n6\n1 3 1 5 3 1\n"
],
"output": [
"-1 -1 3 2 1 \n-1 4 4 4 2 \n-1 -1 1 1 1 1 \n"
]
} |
895 | 7 | After battling Shikamaru, Tayuya decided that her flute is too predictable, and replaced it with a guitar. The guitar has 6 strings and an infinite number of frets numbered from 1. Fretting the fret number j on the i-th string produces the note a_{i} + j.
Tayuya wants to play a melody of n notes. Each note can be play... | from collections import Counter
def main():
a = list(map(int, input().split()))
n = int(input())
b = list(map(int, input().split()))
x = []
for i in a:
for j in b:
x.append((j - i, j))
x.sort()
j = 0
r = float('inf')
c = len(set(b))
d = Counter()
for i i... | {
"input": [
"1 4 100 10 30 5\n6\n101 104 105 110 130 200\n",
"1 1 2 2 3 3\n7\n13 4 11 12 11 13 12\n"
],
"output": [
"0\n",
"7\n"
]
} |
896 | 7 | There are n cards numbered 1, …, n. The card i has a red digit r_i and a blue digit b_i written on it.
We arrange all n cards in random order from left to right, with all permutations of 1, …, n having the same probability. We then read all red digits on the cards from left to right, and obtain an integer R. In the sa... | def solve_case():
n = int(input())
a = input()
b = input()
x = sum([a[i] > b[i] for i in range(n)])
y = sum([a[i] < b[i] for i in range(n)])
if x > y:
print('RED')
elif x < y:
print('BLUE')
else:
print('EQUAL')
def main():
for _ in range(int(input())):
... | {
"input": [
"3\n3\n777\n111\n3\n314\n159\n5\n09281\n09281\n"
],
"output": [
"\nRED\nBLUE\nEQUAL\n"
]
} |
897 | 7 | You have two positive integers a and b.
You can perform two kinds of operations:
* a = ⌊ a/b ⌋ (replace a with the integer part of the division between a and b)
* b=b+1 (increase b by 1)
Find the minimum number of operations required to make a=0.
Input
The first line contains a single integer t (1 ≤ t ≤ 10... | def calc(a,b):
if(a==1): return 1e10
i=0
while(b>0): b=b//a; i+=1
return i
for _ in range(int(input())):
a,b=map(int,input().split());ans=0
while((calc(b,a)-calc(b+1,a))>=1): b+=1; ans+=1
print(ans+calc(b,a)) | {
"input": [
"6\n9 2\n1337 1\n1 1\n50000000 4\n991026972 997\n1234 5678\n"
],
"output": [
"\n4\n9\n2\n12\n3\n1\n"
]
} |
898 | 8 | The student council has a shared document file. Every day, some members of the student council write the sequence TMT (short for Towa Maji Tenshi) in it.
However, one day, the members somehow entered the sequence into the document at the same time, creating a jumbled mess. Therefore, it is Suguru Doujima's task to fig... | def solve(s: str) -> bool:
if s.count('T') != s.count('M')*2:
return False
N = len(s) // 3; C = 0
for c in s:
if c == 'T':
C += 1
else:
C -= 1
if C < 0 or C > N:
break
return C == N
for s in [*open(0)][2::2]:
print("NYOE S"[solve(s[:-1])::2]) | {
"input": [
"5\n3\nTMT\n3\nMTT\n6\nTMTMTT\n6\nTMTTTT\n6\nTTMMTT\n"
],
"output": [
"\nYES\nNO\nYES\nNO\nYES\n"
]
} |
899 | 9 | You are given a string s consisting of the characters 0, 1, and ?.
Let's call a string unstable if it consists of the characters 0 and 1 and any two adjacent characters are different (i. e. it has the form 010101... or 101010...).
Let's call a string beautiful if it consists of the characters 0, 1, and ?, and you can... | # Major "inspiration" from Demoralizer. Way too implementation-heavy for me at the moment.
def solve(s: str) -> int:
ans = 0
x = [-1, -1]
for i in range(len(s)):
y = ord(s[i]) - ord("0")
if (y == 0 or y == 1):
x[y ^ (i % 2)] = i
ans += i - min(x)
return ans
test_case... | {
"input": [
"3\n0?10\n???\n?10??1100\n"
],
"output": [
"\n8\n6\n25\n"
]
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.