message stringlengths 2 65.1k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 0 108k | cluster float64 14 14 | __index_level_0__ int64 0 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
In the Main Berland Bank n people stand in a queue at the cashier, everyone knows his/her height hi, and the heights of the other people in the queue. Each of them keeps in mind number ai β how many people who are taller than him/her and stand in queue in front of him.
After a while the cashier has a lunch break and the people in the queue seat on the chairs in the waiting room in a random order.
When the lunch break was over, it turned out that nobody can remember the exact order of the people in the queue, but everyone remembers his number ai.
Your task is to restore the order in which the people stood in the queue if it is possible. There may be several acceptable orders, but you need to find any of them. Also, you need to print a possible set of numbers hi β the heights of people in the queue, so that the numbers ai are correct.
Input
The first input line contains integer n β the number of people in the queue (1 β€ n β€ 3000). Then n lines contain descriptions of the people as "namei ai" (one description on one line), where namei is a non-empty string consisting of lowercase Latin letters whose length does not exceed 10 characters (the i-th person's name), ai is an integer (0 β€ ai β€ n - 1), that represents the number of people who are higher and stand in the queue in front of person i. It is guaranteed that all names are different.
Output
If there's no acceptable order of the people in the queue, print the single line containing "-1" without the quotes. Otherwise, print in n lines the people as "namei hi", where hi is the integer from 1 to 109 (inclusive), the possible height of a man whose name is namei. Print the people in the order in which they stand in the queue, starting from the head of the queue and moving to its tail. Numbers hi are not necessarily unique.
Examples
Input
4
a 0
b 2
c 0
d 0
Output
a 150
c 170
d 180
b 160
Input
4
vasya 0
petya 1
manya 3
dunay 3
Output
-1 | instruction | 0 | 27,624 | 14 | 55,248 |
Tags: constructive algorithms, greedy, sortings
Correct Solution:
```
n = int(input())
def func(s):
name, a = s.split()
return [int(a), name]
arr = [func(input()) for i in range(n)]
arr.sort()
flag = True
for i in range(n):
if i < arr[i][0]:
flag = False
break
if flag:
h = [0] * n
for i in range(n // 2):
hi = h[i] = 20000 * (i + 1 - arr[i][0])
for x in range(i):
if h[x] >= hi:
h[x] += 20000
for i in range(n // 2, n):
hi = h[i] = i + 1 - arr[i][0]
for x in range(n // 2, i):
if h[x] >= hi:
h[x] += 1
hh = sorted([[h[i], i] for i in range(n // 2, n)])
hh = sorted([[hh[i][1], i + 1] for i in range(n - n // 2)])
for i in range(n // 2, n):
h[i] += 20000 * (h[i] - hh[i - n // 2][1])
for i in range(n):
print(arr[i][1], h[i])
else:
print(-1)
``` | output | 1 | 27,624 | 14 | 55,249 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In the Main Berland Bank n people stand in a queue at the cashier, everyone knows his/her height hi, and the heights of the other people in the queue. Each of them keeps in mind number ai β how many people who are taller than him/her and stand in queue in front of him.
After a while the cashier has a lunch break and the people in the queue seat on the chairs in the waiting room in a random order.
When the lunch break was over, it turned out that nobody can remember the exact order of the people in the queue, but everyone remembers his number ai.
Your task is to restore the order in which the people stood in the queue if it is possible. There may be several acceptable orders, but you need to find any of them. Also, you need to print a possible set of numbers hi β the heights of people in the queue, so that the numbers ai are correct.
Input
The first input line contains integer n β the number of people in the queue (1 β€ n β€ 3000). Then n lines contain descriptions of the people as "namei ai" (one description on one line), where namei is a non-empty string consisting of lowercase Latin letters whose length does not exceed 10 characters (the i-th person's name), ai is an integer (0 β€ ai β€ n - 1), that represents the number of people who are higher and stand in the queue in front of person i. It is guaranteed that all names are different.
Output
If there's no acceptable order of the people in the queue, print the single line containing "-1" without the quotes. Otherwise, print in n lines the people as "namei hi", where hi is the integer from 1 to 109 (inclusive), the possible height of a man whose name is namei. Print the people in the order in which they stand in the queue, starting from the head of the queue and moving to its tail. Numbers hi are not necessarily unique.
Examples
Input
4
a 0
b 2
c 0
d 0
Output
a 150
c 170
d 180
b 160
Input
4
vasya 0
petya 1
manya 3
dunay 3
Output
-1
Submitted Solution:
```
n = int(input())
a = []
for _ in range(n):
name, num = input().split()
a.append([name, int(num)])
a = sorted(a, key = lambda x: x[1])
h = []
def solve(a):
h = []
for name, num in a:
if num > len(h):
return False, -1
if num == 0:
if len(h) == 0:
h.append([name, 1])
else:
h.append([name, h[-1][1]+1])
else:
h = h[:-num] + [[name, h[-num][1]]] + h[-num:]
for h_ in h[-num:]:
h_[1] += 1
h = {name: h_ for name, h_ in h}
for x in a:
x[1] = h[x[0]]
return True, a
flg, ans = solve(a)
if flg == False:
print(ans)
else:
for name, h in ans:
print(name+' '+str(h))
#4
#a 0
#b 2
#c 0
#d 0
``` | instruction | 0 | 27,625 | 14 | 55,250 |
Yes | output | 1 | 27,625 | 14 | 55,251 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In the Main Berland Bank n people stand in a queue at the cashier, everyone knows his/her height hi, and the heights of the other people in the queue. Each of them keeps in mind number ai β how many people who are taller than him/her and stand in queue in front of him.
After a while the cashier has a lunch break and the people in the queue seat on the chairs in the waiting room in a random order.
When the lunch break was over, it turned out that nobody can remember the exact order of the people in the queue, but everyone remembers his number ai.
Your task is to restore the order in which the people stood in the queue if it is possible. There may be several acceptable orders, but you need to find any of them. Also, you need to print a possible set of numbers hi β the heights of people in the queue, so that the numbers ai are correct.
Input
The first input line contains integer n β the number of people in the queue (1 β€ n β€ 3000). Then n lines contain descriptions of the people as "namei ai" (one description on one line), where namei is a non-empty string consisting of lowercase Latin letters whose length does not exceed 10 characters (the i-th person's name), ai is an integer (0 β€ ai β€ n - 1), that represents the number of people who are higher and stand in the queue in front of person i. It is guaranteed that all names are different.
Output
If there's no acceptable order of the people in the queue, print the single line containing "-1" without the quotes. Otherwise, print in n lines the people as "namei hi", where hi is the integer from 1 to 109 (inclusive), the possible height of a man whose name is namei. Print the people in the order in which they stand in the queue, starting from the head of the queue and moving to its tail. Numbers hi are not necessarily unique.
Examples
Input
4
a 0
b 2
c 0
d 0
Output
a 150
c 170
d 180
b 160
Input
4
vasya 0
petya 1
manya 3
dunay 3
Output
-1
Submitted Solution:
```
n = int(input())
a = []
for _ in range(n):
x = input().split()
a.append((x[0], int(x[1])))
a.sort(key=lambda x: x[1])
ans = []
for x in a:
if x[1] > len(ans):
print(-1)
exit()
ans.insert(x[1], (x[0], n))
n -= 1
for x in ans:
print(x[0], x[1])
# Made By Mostafa_Khaled
``` | instruction | 0 | 27,626 | 14 | 55,252 |
Yes | output | 1 | 27,626 | 14 | 55,253 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In the Main Berland Bank n people stand in a queue at the cashier, everyone knows his/her height hi, and the heights of the other people in the queue. Each of them keeps in mind number ai β how many people who are taller than him/her and stand in queue in front of him.
After a while the cashier has a lunch break and the people in the queue seat on the chairs in the waiting room in a random order.
When the lunch break was over, it turned out that nobody can remember the exact order of the people in the queue, but everyone remembers his number ai.
Your task is to restore the order in which the people stood in the queue if it is possible. There may be several acceptable orders, but you need to find any of them. Also, you need to print a possible set of numbers hi β the heights of people in the queue, so that the numbers ai are correct.
Input
The first input line contains integer n β the number of people in the queue (1 β€ n β€ 3000). Then n lines contain descriptions of the people as "namei ai" (one description on one line), where namei is a non-empty string consisting of lowercase Latin letters whose length does not exceed 10 characters (the i-th person's name), ai is an integer (0 β€ ai β€ n - 1), that represents the number of people who are higher and stand in the queue in front of person i. It is guaranteed that all names are different.
Output
If there's no acceptable order of the people in the queue, print the single line containing "-1" without the quotes. Otherwise, print in n lines the people as "namei hi", where hi is the integer from 1 to 109 (inclusive), the possible height of a man whose name is namei. Print the people in the order in which they stand in the queue, starting from the head of the queue and moving to its tail. Numbers hi are not necessarily unique.
Examples
Input
4
a 0
b 2
c 0
d 0
Output
a 150
c 170
d 180
b 160
Input
4
vasya 0
petya 1
manya 3
dunay 3
Output
-1
Submitted Solution:
```
#------------------------template--------------------------#
import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import*
from io import BytesIO, IOBase
def vsInput():
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
ALPHA='abcdefghijklmnopqrstuvwxyz'
M=1000000007
def value():return tuple(map(int,input().split()))
def array():return [int(i) for i in input().split()]
def Int():return int(input())
def Str():return input()
def arrayS():return [i for i in input().split()]
#-------------------------code---------------------------#
# vsInput()
n=Int()
status=[]
have={}
for i in range(n):
name,s=input().split()
have[name]=int(s)
status.append((int(s),name))
status.sort()
ans=[i[1] for i in status]
# print(ans)
rem=0
height=[0,10**9]
queue=[]
ok=True
for i in range(n):
if(have[ans[i]]>len(queue)):
ok=False
break
k=have[ans[i]]
key=n-len(queue)
# print(k,ans[i])
queue.insert(k,(ans[i],key))
if(not ok):
print(-1)
exit()
for i in queue:
print(*i)
# print(queue)
``` | instruction | 0 | 27,627 | 14 | 55,254 |
Yes | output | 1 | 27,627 | 14 | 55,255 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In the Main Berland Bank n people stand in a queue at the cashier, everyone knows his/her height hi, and the heights of the other people in the queue. Each of them keeps in mind number ai β how many people who are taller than him/her and stand in queue in front of him.
After a while the cashier has a lunch break and the people in the queue seat on the chairs in the waiting room in a random order.
When the lunch break was over, it turned out that nobody can remember the exact order of the people in the queue, but everyone remembers his number ai.
Your task is to restore the order in which the people stood in the queue if it is possible. There may be several acceptable orders, but you need to find any of them. Also, you need to print a possible set of numbers hi β the heights of people in the queue, so that the numbers ai are correct.
Input
The first input line contains integer n β the number of people in the queue (1 β€ n β€ 3000). Then n lines contain descriptions of the people as "namei ai" (one description on one line), where namei is a non-empty string consisting of lowercase Latin letters whose length does not exceed 10 characters (the i-th person's name), ai is an integer (0 β€ ai β€ n - 1), that represents the number of people who are higher and stand in the queue in front of person i. It is guaranteed that all names are different.
Output
If there's no acceptable order of the people in the queue, print the single line containing "-1" without the quotes. Otherwise, print in n lines the people as "namei hi", where hi is the integer from 1 to 109 (inclusive), the possible height of a man whose name is namei. Print the people in the order in which they stand in the queue, starting from the head of the queue and moving to its tail. Numbers hi are not necessarily unique.
Examples
Input
4
a 0
b 2
c 0
d 0
Output
a 150
c 170
d 180
b 160
Input
4
vasya 0
petya 1
manya 3
dunay 3
Output
-1
Submitted Solution:
```
n=int(input())
arr=[]
for i in range(n):
a,b=input().split()
arr.append([a,int(b)])
arr.sort(key=lambda x:x[1])
ans=[]
#print(arr)
for i in range(n):
if arr[i][1]>i:
print(-1)
exit()
i=0
for x in arr:
ans.insert(x[1],(x[0],n-i))
i+=1
for x in ans:print(x[0],x[1])
``` | instruction | 0 | 27,628 | 14 | 55,256 |
Yes | output | 1 | 27,628 | 14 | 55,257 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In the Main Berland Bank n people stand in a queue at the cashier, everyone knows his/her height hi, and the heights of the other people in the queue. Each of them keeps in mind number ai β how many people who are taller than him/her and stand in queue in front of him.
After a while the cashier has a lunch break and the people in the queue seat on the chairs in the waiting room in a random order.
When the lunch break was over, it turned out that nobody can remember the exact order of the people in the queue, but everyone remembers his number ai.
Your task is to restore the order in which the people stood in the queue if it is possible. There may be several acceptable orders, but you need to find any of them. Also, you need to print a possible set of numbers hi β the heights of people in the queue, so that the numbers ai are correct.
Input
The first input line contains integer n β the number of people in the queue (1 β€ n β€ 3000). Then n lines contain descriptions of the people as "namei ai" (one description on one line), where namei is a non-empty string consisting of lowercase Latin letters whose length does not exceed 10 characters (the i-th person's name), ai is an integer (0 β€ ai β€ n - 1), that represents the number of people who are higher and stand in the queue in front of person i. It is guaranteed that all names are different.
Output
If there's no acceptable order of the people in the queue, print the single line containing "-1" without the quotes. Otherwise, print in n lines the people as "namei hi", where hi is the integer from 1 to 109 (inclusive), the possible height of a man whose name is namei. Print the people in the order in which they stand in the queue, starting from the head of the queue and moving to its tail. Numbers hi are not necessarily unique.
Examples
Input
4
a 0
b 2
c 0
d 0
Output
a 150
c 170
d 180
b 160
Input
4
vasya 0
petya 1
manya 3
dunay 3
Output
-1
Submitted Solution:
```
from collections import Counter
import sys
from operator import itemgetter
n = int(input().strip())
names = Counter()
for i in range(n):
name, ai = input().strip().split()
names[name] = int(ai)
names = sorted(names.items(), key = lambda x: x[1])
for i, (k, v) in enumerate(names):
if v > i:
print(-1)
sys.exit()
for (k, v) in names:
print(k, v)
``` | instruction | 0 | 27,629 | 14 | 55,258 |
No | output | 1 | 27,629 | 14 | 55,259 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In the Main Berland Bank n people stand in a queue at the cashier, everyone knows his/her height hi, and the heights of the other people in the queue. Each of them keeps in mind number ai β how many people who are taller than him/her and stand in queue in front of him.
After a while the cashier has a lunch break and the people in the queue seat on the chairs in the waiting room in a random order.
When the lunch break was over, it turned out that nobody can remember the exact order of the people in the queue, but everyone remembers his number ai.
Your task is to restore the order in which the people stood in the queue if it is possible. There may be several acceptable orders, but you need to find any of them. Also, you need to print a possible set of numbers hi β the heights of people in the queue, so that the numbers ai are correct.
Input
The first input line contains integer n β the number of people in the queue (1 β€ n β€ 3000). Then n lines contain descriptions of the people as "namei ai" (one description on one line), where namei is a non-empty string consisting of lowercase Latin letters whose length does not exceed 10 characters (the i-th person's name), ai is an integer (0 β€ ai β€ n - 1), that represents the number of people who are higher and stand in the queue in front of person i. It is guaranteed that all names are different.
Output
If there's no acceptable order of the people in the queue, print the single line containing "-1" without the quotes. Otherwise, print in n lines the people as "namei hi", where hi is the integer from 1 to 109 (inclusive), the possible height of a man whose name is namei. Print the people in the order in which they stand in the queue, starting from the head of the queue and moving to its tail. Numbers hi are not necessarily unique.
Examples
Input
4
a 0
b 2
c 0
d 0
Output
a 150
c 170
d 180
b 160
Input
4
vasya 0
petya 1
manya 3
dunay 3
Output
-1
Submitted Solution:
```
#------------------------template--------------------------#
import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import*
from io import BytesIO, IOBase
def vsInput():
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
ALPHA='abcdefghijklmnopqrstuvwxyz'
M=1000000007
def value():return tuple(map(int,input().split()))
def array():return [int(i) for i in input().split()]
def Int():return int(input())
def Str():return input()
def arrayS():return [i for i in input().split()]
#-------------------------code---------------------------#
# vsInput()
n=Int()
status=[]
have={}
for i in range(n):
name,s=input().split()
have[name]=int(s)
status.append((int(s),name))
status.sort()
ans=[i[1] for i in status]
# print(ans)
rem=0
height=[0,10**9]
queue={}
ok=True
for i in range(n):
if(have[ans[i]]>len(queue)):
ok=False
break
k=len(queue)-have[ans[i]]
# print(k,ans[i])
key=(height[k]+height[k+1])//2
height.insert(k+1,key)
queue[ans[i]]=key
for i in range(len(height)):
if(height[i]>key):
height[i]+=2
if(not ok):
print(-1)
exit()
for i in queue:
print(i,queue[i])
# print(queue)
``` | instruction | 0 | 27,630 | 14 | 55,260 |
No | output | 1 | 27,630 | 14 | 55,261 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In the Main Berland Bank n people stand in a queue at the cashier, everyone knows his/her height hi, and the heights of the other people in the queue. Each of them keeps in mind number ai β how many people who are taller than him/her and stand in queue in front of him.
After a while the cashier has a lunch break and the people in the queue seat on the chairs in the waiting room in a random order.
When the lunch break was over, it turned out that nobody can remember the exact order of the people in the queue, but everyone remembers his number ai.
Your task is to restore the order in which the people stood in the queue if it is possible. There may be several acceptable orders, but you need to find any of them. Also, you need to print a possible set of numbers hi β the heights of people in the queue, so that the numbers ai are correct.
Input
The first input line contains integer n β the number of people in the queue (1 β€ n β€ 3000). Then n lines contain descriptions of the people as "namei ai" (one description on one line), where namei is a non-empty string consisting of lowercase Latin letters whose length does not exceed 10 characters (the i-th person's name), ai is an integer (0 β€ ai β€ n - 1), that represents the number of people who are higher and stand in the queue in front of person i. It is guaranteed that all names are different.
Output
If there's no acceptable order of the people in the queue, print the single line containing "-1" without the quotes. Otherwise, print in n lines the people as "namei hi", where hi is the integer from 1 to 109 (inclusive), the possible height of a man whose name is namei. Print the people in the order in which they stand in the queue, starting from the head of the queue and moving to its tail. Numbers hi are not necessarily unique.
Examples
Input
4
a 0
b 2
c 0
d 0
Output
a 150
c 170
d 180
b 160
Input
4
vasya 0
petya 1
manya 3
dunay 3
Output
-1
Submitted Solution:
```
'''input
8
a 0
b 0
c 0
d 2
e 2
f 3
g 3
h 5
'''
from sys import stdin
from collections import defaultdict
# main starts
n = int(stdin.readline().strip())
name_dict = dict()
name_list = []
for _ in range(n):
name, num = list(stdin.readline().split())
num = int(num)
name_dict[name] = num
name_list.append([name, num])
name_list.sort(key = lambda x:x[1])
ans = dict()
ans[name_list[0][0]] = 3001
cur = [3001]
for i in range(1,n):
# print(cur)
name, rank = name_list[i]
if len(cur) - 1 < rank:
print(-1)
exit()
r = cur[rank]
mn = float('inf')
# print(ans)
for j in ans:
if ans[j] <= r:
ans[j] -= 1
mn = min(ans[j], mn)
ans[name] = r
cur.append(mn)
for i in ans:
print(i, ans[i])
``` | instruction | 0 | 27,631 | 14 | 55,262 |
No | output | 1 | 27,631 | 14 | 55,263 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In the Main Berland Bank n people stand in a queue at the cashier, everyone knows his/her height hi, and the heights of the other people in the queue. Each of them keeps in mind number ai β how many people who are taller than him/her and stand in queue in front of him.
After a while the cashier has a lunch break and the people in the queue seat on the chairs in the waiting room in a random order.
When the lunch break was over, it turned out that nobody can remember the exact order of the people in the queue, but everyone remembers his number ai.
Your task is to restore the order in which the people stood in the queue if it is possible. There may be several acceptable orders, but you need to find any of them. Also, you need to print a possible set of numbers hi β the heights of people in the queue, so that the numbers ai are correct.
Input
The first input line contains integer n β the number of people in the queue (1 β€ n β€ 3000). Then n lines contain descriptions of the people as "namei ai" (one description on one line), where namei is a non-empty string consisting of lowercase Latin letters whose length does not exceed 10 characters (the i-th person's name), ai is an integer (0 β€ ai β€ n - 1), that represents the number of people who are higher and stand in the queue in front of person i. It is guaranteed that all names are different.
Output
If there's no acceptable order of the people in the queue, print the single line containing "-1" without the quotes. Otherwise, print in n lines the people as "namei hi", where hi is the integer from 1 to 109 (inclusive), the possible height of a man whose name is namei. Print the people in the order in which they stand in the queue, starting from the head of the queue and moving to its tail. Numbers hi are not necessarily unique.
Examples
Input
4
a 0
b 2
c 0
d 0
Output
a 150
c 170
d 180
b 160
Input
4
vasya 0
petya 1
manya 3
dunay 3
Output
-1
Submitted Solution:
```
n = int(input())
arr = sorted([input().split() for _ in range(n)], key=lambda x:x[1])
arr = [[int(arr[i][1]), arr[i][0]] for i in range(n)]
res = []
for i in range(n):
res.append(i - arr[i][0])
if res[i] < 0:
print(-1)
exit()
for j in range(i):
if res[j] >= res[i]:
res[j] += 1
for i in range(n):
print(arr[i][1], res[i]+1)
``` | instruction | 0 | 27,632 | 14 | 55,264 |
No | output | 1 | 27,632 | 14 | 55,265 |
Provide tags and a correct Python 3 solution for this coding contest problem.
β Oh my sweet Beaverette, would you fancy a walk along a wonderful woodland belt with me?
β Of course, my Smart Beaver! Let us enjoy the splendid view together. How about Friday night?
At this point the Smart Beaver got rushing. Everything should be perfect by Friday, so he needed to prepare the belt to the upcoming walk. He needed to cut down several trees.
Let's consider the woodland belt as a sequence of trees. Each tree i is described by the esthetic appeal ai β some trees are very esthetically pleasing, others are 'so-so', and some trees are positively ugly!
The Smart Beaver calculated that he needed the following effects to win the Beaverette's heart:
* The first objective is to please the Beaverette: the sum of esthetic appeal of the remaining trees must be maximum possible;
* the second objective is to surprise the Beaverette: the esthetic appeal of the first and the last trees in the resulting belt must be the same;
* and of course, the walk should be successful: there must be at least two trees in the woodland belt left.
Now help the Smart Beaver! Which trees does he need to cut down to win the Beaverette's heart?
Input
The first line contains a single integer n β the initial number of trees in the woodland belt, 2 β€ n. The second line contains space-separated integers ai β the esthetic appeals of each tree. All esthetic appeals do not exceed 109 in their absolute value.
* to get 30 points, you need to solve the problem with constraints: n β€ 100 (subproblem A1);
* to get 100 points, you need to solve the problem with constraints: n β€ 3Β·105 (subproblems A1+A2).
Output
In the first line print two integers β the total esthetic appeal of the woodland belt after the Smart Beaver's intervention and the number of the cut down trees k.
In the next line print k integers β the numbers of the trees the Beaver needs to cut down. Assume that the trees are numbered from 1 to n from left to right.
If there are multiple solutions, print any of them. It is guaranteed that at least two trees have equal esthetic appeal.
Examples
Input
5
1 2 3 1 2
Output
8 1
1
Input
5
1 -2 3 1 -2
Output
5 2
2 5 | instruction | 0 | 27,777 | 14 | 55,554 |
Tags: data structures, sortings
Correct Solution:
```
n, t = int(input()), list(map(int, input().split()))
a, b = {}, {}
for i, j in enumerate(t):
if not j in a: a[j] = i
else: b[j] = i
p = [(a[j], b[j] - 1) for j in b]
s = [j if j > 0 else 0 for j in t]
u = v = 2 * t[p[0][0]] - 1
for i in range(n - 1): s[i + 1] += s[i]
for i, j in p:
u = 2 * t[i] + s[j] - s[i]
if u > v: a, b, v = i, j, u
s = list(range(1, a + 1)) + [i for i, j in enumerate(t[a + 1: b + 1], a + 2) if j < 0] + list(range(b + 3, n + 1))
print(v, len(s))
print(' '.join(map(str, s)))
``` | output | 1 | 27,777 | 14 | 55,555 |
Provide tags and a correct Python 3 solution for this coding contest problem.
β Oh my sweet Beaverette, would you fancy a walk along a wonderful woodland belt with me?
β Of course, my Smart Beaver! Let us enjoy the splendid view together. How about Friday night?
At this point the Smart Beaver got rushing. Everything should be perfect by Friday, so he needed to prepare the belt to the upcoming walk. He needed to cut down several trees.
Let's consider the woodland belt as a sequence of trees. Each tree i is described by the esthetic appeal ai β some trees are very esthetically pleasing, others are 'so-so', and some trees are positively ugly!
The Smart Beaver calculated that he needed the following effects to win the Beaverette's heart:
* The first objective is to please the Beaverette: the sum of esthetic appeal of the remaining trees must be maximum possible;
* the second objective is to surprise the Beaverette: the esthetic appeal of the first and the last trees in the resulting belt must be the same;
* and of course, the walk should be successful: there must be at least two trees in the woodland belt left.
Now help the Smart Beaver! Which trees does he need to cut down to win the Beaverette's heart?
Input
The first line contains a single integer n β the initial number of trees in the woodland belt, 2 β€ n. The second line contains space-separated integers ai β the esthetic appeals of each tree. All esthetic appeals do not exceed 109 in their absolute value.
* to get 30 points, you need to solve the problem with constraints: n β€ 100 (subproblem A1);
* to get 100 points, you need to solve the problem with constraints: n β€ 3Β·105 (subproblems A1+A2).
Output
In the first line print two integers β the total esthetic appeal of the woodland belt after the Smart Beaver's intervention and the number of the cut down trees k.
In the next line print k integers β the numbers of the trees the Beaver needs to cut down. Assume that the trees are numbered from 1 to n from left to right.
If there are multiple solutions, print any of them. It is guaranteed that at least two trees have equal esthetic appeal.
Examples
Input
5
1 2 3 1 2
Output
8 1
1
Input
5
1 -2 3 1 -2
Output
5 2
2 5 | instruction | 0 | 27,778 | 14 | 55,556 |
Tags: data structures, sortings
Correct Solution:
```
import math
import sys
from collections import deque, Counter, OrderedDict, defaultdict
#import heapq
#ceil,floor,log,sqrt,factorial,pow,pi,gcd
#import bisect
#from bisect import bisect_left,bisect_right
input = sys.stdin.readline
def inp():
return(int(input()))
def inlt():
return(list(map(int,input().split())))
def insr():
s = input().strip()
return(list(s[:len(s)]))
def invr():
return(map(int,input().split()))
n=inp()
l=inlt()
g=Counter(l)
fst=defaultdict(lambda:-1)
lst=defaultdict(lambda:-1)
sm=[max(0,l[0])]
for i in range(1,n):
sm.append(sm[-1]+max(0,l[i]))
for i in range(n):
if fst[l[i]]==-1:
fst[l[i]]=i
for i in range(n-1,-1,-1):
if lst[l[i]]==-1:
lst[l[i]]=i
mx=-sys.maxsize
v=-1
for each in g:
if g[each]>=2:
if each<0:
val=2*each-sm[fst[each]]+sm[lst[each]]
else:
val=sm[lst[each]]-sm[fst[each]]+each
if val>mx:
mx=val
v=each
cnt=0
rem=[]
for i in range(n):
if i<fst[v]:
rem.append(i+1)
elif i>lst[v]:
rem.append(i+1)
elif l[i]<0 and i!=fst[v] and i!=lst[v]:
rem.append(i+1)
print(mx,len(rem))
print(*rem)
``` | output | 1 | 27,778 | 14 | 55,557 |
Provide tags and a correct Python 3 solution for this coding contest problem.
β Oh my sweet Beaverette, would you fancy a walk along a wonderful woodland belt with me?
β Of course, my Smart Beaver! Let us enjoy the splendid view together. How about Friday night?
At this point the Smart Beaver got rushing. Everything should be perfect by Friday, so he needed to prepare the belt to the upcoming walk. He needed to cut down several trees.
Let's consider the woodland belt as a sequence of trees. Each tree i is described by the esthetic appeal ai β some trees are very esthetically pleasing, others are 'so-so', and some trees are positively ugly!
The Smart Beaver calculated that he needed the following effects to win the Beaverette's heart:
* The first objective is to please the Beaverette: the sum of esthetic appeal of the remaining trees must be maximum possible;
* the second objective is to surprise the Beaverette: the esthetic appeal of the first and the last trees in the resulting belt must be the same;
* and of course, the walk should be successful: there must be at least two trees in the woodland belt left.
Now help the Smart Beaver! Which trees does he need to cut down to win the Beaverette's heart?
Input
The first line contains a single integer n β the initial number of trees in the woodland belt, 2 β€ n. The second line contains space-separated integers ai β the esthetic appeals of each tree. All esthetic appeals do not exceed 109 in their absolute value.
* to get 30 points, you need to solve the problem with constraints: n β€ 100 (subproblem A1);
* to get 100 points, you need to solve the problem with constraints: n β€ 3Β·105 (subproblems A1+A2).
Output
In the first line print two integers β the total esthetic appeal of the woodland belt after the Smart Beaver's intervention and the number of the cut down trees k.
In the next line print k integers β the numbers of the trees the Beaver needs to cut down. Assume that the trees are numbered from 1 to n from left to right.
If there are multiple solutions, print any of them. It is guaranteed that at least two trees have equal esthetic appeal.
Examples
Input
5
1 2 3 1 2
Output
8 1
1
Input
5
1 -2 3 1 -2
Output
5 2
2 5 | instruction | 0 | 27,779 | 14 | 55,558 |
Tags: data structures, sortings
Correct Solution:
```
from collections import defaultdict
n = int(input())
a = list(map(int,input().split()))
same = defaultdict(list)
only_positive = [max(0,x) for x in a]
partial_sum = [0 for i in range(n+1)]
for i in range(1,n+1):
partial_sum[i] = partial_sum[i-1]+only_positive[i-1]
for i in range(n):
same[a[i]].append(i)
best = -10**18
bl, br = -1, -1
for key in same:
if len(same[key]) >= 2:
l, r = same[key][0], same[key][-1]
cur = key*2 + partial_sum[r]-partial_sum[l+1]
if cur > best:
best = cur
bl, br = l, r
cut = []
for i in range(n):
if not (a[i] >= 0 and bl <= i and i <= br):
if i != bl and i != br:
cut.append(i+1)
print(best,len(cut))
print(*cut)
``` | output | 1 | 27,779 | 14 | 55,559 |
Provide tags and a correct Python 3 solution for this coding contest problem.
β Oh my sweet Beaverette, would you fancy a walk along a wonderful woodland belt with me?
β Of course, my Smart Beaver! Let us enjoy the splendid view together. How about Friday night?
At this point the Smart Beaver got rushing. Everything should be perfect by Friday, so he needed to prepare the belt to the upcoming walk. He needed to cut down several trees.
Let's consider the woodland belt as a sequence of trees. Each tree i is described by the esthetic appeal ai β some trees are very esthetically pleasing, others are 'so-so', and some trees are positively ugly!
The Smart Beaver calculated that he needed the following effects to win the Beaverette's heart:
* The first objective is to please the Beaverette: the sum of esthetic appeal of the remaining trees must be maximum possible;
* the second objective is to surprise the Beaverette: the esthetic appeal of the first and the last trees in the resulting belt must be the same;
* and of course, the walk should be successful: there must be at least two trees in the woodland belt left.
Now help the Smart Beaver! Which trees does he need to cut down to win the Beaverette's heart?
Input
The first line contains a single integer n β the initial number of trees in the woodland belt, 2 β€ n. The second line contains space-separated integers ai β the esthetic appeals of each tree. All esthetic appeals do not exceed 109 in their absolute value.
* to get 30 points, you need to solve the problem with constraints: n β€ 100 (subproblem A1);
* to get 100 points, you need to solve the problem with constraints: n β€ 3Β·105 (subproblems A1+A2).
Output
In the first line print two integers β the total esthetic appeal of the woodland belt after the Smart Beaver's intervention and the number of the cut down trees k.
In the next line print k integers β the numbers of the trees the Beaver needs to cut down. Assume that the trees are numbered from 1 to n from left to right.
If there are multiple solutions, print any of them. It is guaranteed that at least two trees have equal esthetic appeal.
Examples
Input
5
1 2 3 1 2
Output
8 1
1
Input
5
1 -2 3 1 -2
Output
5 2
2 5 | instruction | 0 | 27,780 | 14 | 55,560 |
Tags: data structures, sortings
Correct Solution:
```
def main():
n, aa = int(input()), list(map(int, input().split()))
partialsum, s, d, ranges = [0] * n, 0, {}, []
for i, a in enumerate(aa):
if a > 0:
s += a
partialsum[i] = s
if a in d:
d[a].append(i)
else:
d[a] = [i]
ranges = []
for a, l in d.items():
lo, hi = l[0], l[-1]
if lo < hi:
ranges.append((partialsum[hi - 1] - partialsum[lo] + a * 2, lo, hi))
s, lo, hi = max(ranges)
res = list(range(1, lo + 1))
for i in range(lo + 1, hi):
if aa[i] < 0:
res.append(i + 1)
res.extend(range(hi + 2, n + 1))
print(s, len(res))
print(" ".join(map(str, res)))
if __name__ == '__main__':
main()
``` | output | 1 | 27,780 | 14 | 55,561 |
Provide tags and a correct Python 3 solution for this coding contest problem.
β Oh my sweet Beaverette, would you fancy a walk along a wonderful woodland belt with me?
β Of course, my Smart Beaver! Let us enjoy the splendid view together. How about Friday night?
At this point the Smart Beaver got rushing. Everything should be perfect by Friday, so he needed to prepare the belt to the upcoming walk. He needed to cut down several trees.
Let's consider the woodland belt as a sequence of trees. Each tree i is described by the esthetic appeal ai β some trees are very esthetically pleasing, others are 'so-so', and some trees are positively ugly!
The Smart Beaver calculated that he needed the following effects to win the Beaverette's heart:
* The first objective is to please the Beaverette: the sum of esthetic appeal of the remaining trees must be maximum possible;
* the second objective is to surprise the Beaverette: the esthetic appeal of the first and the last trees in the resulting belt must be the same;
* and of course, the walk should be successful: there must be at least two trees in the woodland belt left.
Now help the Smart Beaver! Which trees does he need to cut down to win the Beaverette's heart?
Input
The first line contains a single integer n β the initial number of trees in the woodland belt, 2 β€ n. The second line contains space-separated integers ai β the esthetic appeals of each tree. All esthetic appeals do not exceed 109 in their absolute value.
* to get 30 points, you need to solve the problem with constraints: n β€ 100 (subproblem A1);
* to get 100 points, you need to solve the problem with constraints: n β€ 3Β·105 (subproblems A1+A2).
Output
In the first line print two integers β the total esthetic appeal of the woodland belt after the Smart Beaver's intervention and the number of the cut down trees k.
In the next line print k integers β the numbers of the trees the Beaver needs to cut down. Assume that the trees are numbered from 1 to n from left to right.
If there are multiple solutions, print any of them. It is guaranteed that at least two trees have equal esthetic appeal.
Examples
Input
5
1 2 3 1 2
Output
8 1
1
Input
5
1 -2 3 1 -2
Output
5 2
2 5 | instruction | 0 | 27,781 | 14 | 55,562 |
Tags: data structures, sortings
Correct Solution:
```
n=int(input())
a=tuple(map(int,input().split()))
c={}
p={}
s=x=y=0
m=-1e18
for i in range(0,len(a)):
d=c.get(a[i])
if d!=None and s-d+a[i]*2>m:
m=s-d+a[i]*2
x,y=p.get(a[i]),i
if(a[i]>0):s+=a[i]
if p.get(a[i])==None:
p[a[i]]=i
c[a[i]]=s
a=[str(i+1) for i in range(0,len(a)) if i!=x and i!=y and (a[i]<0 or i<x or i>y)]
print(m,len(a))
print(" ".join(a))
``` | output | 1 | 27,781 | 14 | 55,563 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Recently a serious bug has been found in the FOS code. The head of the F company wants to find the culprit and punish him. For that, he set up an organizational meeting, the issue is: who's bugged the code? Each of the n coders on the meeting said: 'I know for sure that either x or y did it!'
The head of the company decided to choose two suspects and invite them to his office. Naturally, he should consider the coders' opinions. That's why the head wants to make such a choice that at least p of n coders agreed with it. A coder agrees with the choice of two suspects if at least one of the two people that he named at the meeting was chosen as a suspect. In how many ways can the head of F choose two suspects?
Note that even if some coder was chosen as a suspect, he can agree with the head's choice if he named the other chosen coder at the meeting.
Input
The first line contains integers n and p (3 β€ n β€ 3Β·105; 0 β€ p β€ n) β the number of coders in the F company and the minimum number of agreed people.
Each of the next n lines contains two integers xi, yi (1 β€ xi, yi β€ n) β the numbers of coders named by the i-th coder. It is guaranteed that xi β i, yi β i, xi β yi.
Output
Print a single integer β the number of possible two-suspect sets. Note that the order of the suspects doesn't matter, that is, sets (1, 2) and (2, 1) are considered identical.
Examples
Input
4 2
2 3
1 4
1 4
2 1
Output
6
Input
8 6
5 6
5 7
5 8
6 2
2 1
7 3
1 3
1 4
Output
1 | instruction | 0 | 27,814 | 14 | 55,628 |
Tags: binary search, data structures, sortings
Correct Solution:
```
from collections import defaultdict
from bisect import bisect_left as lower
import sys
input = sys.stdin.readline
def put():
return map(int, input().split())
try:
n,m = put()
cnt, mp, ans = [0]*n, defaultdict(), [0]*n
for _ in range(n):
x,y = put()
x,y = x-1,y-1
key = (min(x,y), max(x,y))
if key in mp:
mp[key]+=1
else:
mp[key]=1
cnt[x]+=1
cnt[y]+=1
except:
print('lol')
for (x,y),val in mp.items():
if cnt[x]+cnt[y]>= m and cnt[x]+cnt[y]-val<m:
ans[x]-=1
ans[y]-=1
scnt = cnt.copy()
scnt.sort()
for i in range(n):
ans[i]+= n-lower(scnt, m-cnt[i])
if 2*cnt[i]>=m:
ans[i]-=1
print(sum(ans)//2)
``` | output | 1 | 27,814 | 14 | 55,629 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Today there is going to be an unusual performance at the circus β hamsters and tigers will perform together! All of them stand in circle along the arena edge and now the trainer faces a difficult task: he wants to swap the animals' positions so that all the hamsters stood together and all the tigers also stood together. The trainer swaps the animals in pairs not to create a mess. He orders two animals to step out of the circle and swap places. As hamsters feel highly uncomfortable when tigers are nearby as well as tigers get nervous when there's so much potential prey around (consisting not only of hamsters but also of yummier spectators), the trainer wants to spend as little time as possible moving the animals, i.e. he wants to achieve it with the minimal number of swaps. Your task is to help him.
Input
The first line contains number n (2 β€ n β€ 1000) which indicates the total number of animals in the arena. The second line contains the description of the animals' positions. The line consists of n symbols "H" and "T". The "H"s correspond to hamsters and the "T"s correspond to tigers. It is guaranteed that at least one hamster and one tiger are present on the arena. The animals are given in the order in which they are located circle-wise, in addition, the last animal stands near the first one.
Output
Print the single number which is the minimal number of swaps that let the trainer to achieve his goal.
Examples
Input
3
HTH
Output
0
Input
9
HTHTHTHHT
Output
2
Note
In the first example we shouldn't move anybody because the animals of each species already stand apart from the other species. In the second example you may swap, for example, the tiger in position 2 with the hamster in position 5 and then β the tiger in position 9 with the hamster in position 7. | instruction | 0 | 27,835 | 14 | 55,670 |
Tags: two pointers
Correct Solution:
```
n=int(input())
s=input()
h=0
for i in s:
if i=='H':h+=1
r=[]
t=0
for i in range(0,n):
if s[i]=='H':
for b in range((i+1)%n,min(((i+1)%n+h-1),n)):
if s[b]=='T':
t+=1
if ((i+1)%n+(h-1))>n:
for q in range(0,(((i+1)%n+(h-1))%n)):
if s[q]=='T':
t+=1
r+=[t]
t=0
i+=1
print(min(r))
``` | output | 1 | 27,835 | 14 | 55,671 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Today there is going to be an unusual performance at the circus β hamsters and tigers will perform together! All of them stand in circle along the arena edge and now the trainer faces a difficult task: he wants to swap the animals' positions so that all the hamsters stood together and all the tigers also stood together. The trainer swaps the animals in pairs not to create a mess. He orders two animals to step out of the circle and swap places. As hamsters feel highly uncomfortable when tigers are nearby as well as tigers get nervous when there's so much potential prey around (consisting not only of hamsters but also of yummier spectators), the trainer wants to spend as little time as possible moving the animals, i.e. he wants to achieve it with the minimal number of swaps. Your task is to help him.
Input
The first line contains number n (2 β€ n β€ 1000) which indicates the total number of animals in the arena. The second line contains the description of the animals' positions. The line consists of n symbols "H" and "T". The "H"s correspond to hamsters and the "T"s correspond to tigers. It is guaranteed that at least one hamster and one tiger are present on the arena. The animals are given in the order in which they are located circle-wise, in addition, the last animal stands near the first one.
Output
Print the single number which is the minimal number of swaps that let the trainer to achieve his goal.
Examples
Input
3
HTH
Output
0
Input
9
HTHTHTHHT
Output
2
Note
In the first example we shouldn't move anybody because the animals of each species already stand apart from the other species. In the second example you may swap, for example, the tiger in position 2 with the hamster in position 5 and then β the tiger in position 9 with the hamster in position 7. | instruction | 0 | 27,836 | 14 | 55,672 |
Tags: two pointers
Correct Solution:
```
n=int(input())
s=input()
h=s.count('H')
s=s+s
print(min(s[i:i+h].count('T') for i in range(n)))
``` | output | 1 | 27,836 | 14 | 55,673 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Today there is going to be an unusual performance at the circus β hamsters and tigers will perform together! All of them stand in circle along the arena edge and now the trainer faces a difficult task: he wants to swap the animals' positions so that all the hamsters stood together and all the tigers also stood together. The trainer swaps the animals in pairs not to create a mess. He orders two animals to step out of the circle and swap places. As hamsters feel highly uncomfortable when tigers are nearby as well as tigers get nervous when there's so much potential prey around (consisting not only of hamsters but also of yummier spectators), the trainer wants to spend as little time as possible moving the animals, i.e. he wants to achieve it with the minimal number of swaps. Your task is to help him.
Input
The first line contains number n (2 β€ n β€ 1000) which indicates the total number of animals in the arena. The second line contains the description of the animals' positions. The line consists of n symbols "H" and "T". The "H"s correspond to hamsters and the "T"s correspond to tigers. It is guaranteed that at least one hamster and one tiger are present on the arena. The animals are given in the order in which they are located circle-wise, in addition, the last animal stands near the first one.
Output
Print the single number which is the minimal number of swaps that let the trainer to achieve his goal.
Examples
Input
3
HTH
Output
0
Input
9
HTHTHTHHT
Output
2
Note
In the first example we shouldn't move anybody because the animals of each species already stand apart from the other species. In the second example you may swap, for example, the tiger in position 2 with the hamster in position 5 and then β the tiger in position 9 with the hamster in position 7. | instruction | 0 | 27,837 | 14 | 55,674 |
Tags: two pointers
Correct Solution:
```
n, s = int(input()), input() * 2
h = s.count('H') // 2
print(h - max(s[i:i + h].count('H') for i in range(n)))
``` | output | 1 | 27,837 | 14 | 55,675 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Today there is going to be an unusual performance at the circus β hamsters and tigers will perform together! All of them stand in circle along the arena edge and now the trainer faces a difficult task: he wants to swap the animals' positions so that all the hamsters stood together and all the tigers also stood together. The trainer swaps the animals in pairs not to create a mess. He orders two animals to step out of the circle and swap places. As hamsters feel highly uncomfortable when tigers are nearby as well as tigers get nervous when there's so much potential prey around (consisting not only of hamsters but also of yummier spectators), the trainer wants to spend as little time as possible moving the animals, i.e. he wants to achieve it with the minimal number of swaps. Your task is to help him.
Input
The first line contains number n (2 β€ n β€ 1000) which indicates the total number of animals in the arena. The second line contains the description of the animals' positions. The line consists of n symbols "H" and "T". The "H"s correspond to hamsters and the "T"s correspond to tigers. It is guaranteed that at least one hamster and one tiger are present on the arena. The animals are given in the order in which they are located circle-wise, in addition, the last animal stands near the first one.
Output
Print the single number which is the minimal number of swaps that let the trainer to achieve his goal.
Examples
Input
3
HTH
Output
0
Input
9
HTHTHTHHT
Output
2
Note
In the first example we shouldn't move anybody because the animals of each species already stand apart from the other species. In the second example you may swap, for example, the tiger in position 2 with the hamster in position 5 and then β the tiger in position 9 with the hamster in position 7. | instruction | 0 | 27,838 | 14 | 55,676 |
Tags: two pointers
Correct Solution:
```
I = int
K = input
W = print
q = min
x = sum
u = range
n = I(K())
s = K()
kh = s.count('H')
a = 'H'*kh+'T'*(n-kh)
W(q(x(a[j-i] != s[j] for j in u(n)) for i in u(n)) // 2)
``` | output | 1 | 27,838 | 14 | 55,677 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Today there is going to be an unusual performance at the circus β hamsters and tigers will perform together! All of them stand in circle along the arena edge and now the trainer faces a difficult task: he wants to swap the animals' positions so that all the hamsters stood together and all the tigers also stood together. The trainer swaps the animals in pairs not to create a mess. He orders two animals to step out of the circle and swap places. As hamsters feel highly uncomfortable when tigers are nearby as well as tigers get nervous when there's so much potential prey around (consisting not only of hamsters but also of yummier spectators), the trainer wants to spend as little time as possible moving the animals, i.e. he wants to achieve it with the minimal number of swaps. Your task is to help him.
Input
The first line contains number n (2 β€ n β€ 1000) which indicates the total number of animals in the arena. The second line contains the description of the animals' positions. The line consists of n symbols "H" and "T". The "H"s correspond to hamsters and the "T"s correspond to tigers. It is guaranteed that at least one hamster and one tiger are present on the arena. The animals are given in the order in which they are located circle-wise, in addition, the last animal stands near the first one.
Output
Print the single number which is the minimal number of swaps that let the trainer to achieve his goal.
Examples
Input
3
HTH
Output
0
Input
9
HTHTHTHHT
Output
2
Note
In the first example we shouldn't move anybody because the animals of each species already stand apart from the other species. In the second example you may swap, for example, the tiger in position 2 with the hamster in position 5 and then β the tiger in position 9 with the hamster in position 7. | instruction | 0 | 27,839 | 14 | 55,678 |
Tags: two pointers
Correct Solution:
```
n = int(input())
c, v = [0] * (n + 1), 0
for i, ch in enumerate(input()):
c[i + 1] = c[i] + (ch == 'H')
for i in range(n):
if i + c[n] <= n:
v = max(v, c[i + c[n]] - c[i])
else:
v = max(v, c[n] - c[i] + c[c[n] - (n - i)])
print(c[n] - v)
``` | output | 1 | 27,839 | 14 | 55,679 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Today there is going to be an unusual performance at the circus β hamsters and tigers will perform together! All of them stand in circle along the arena edge and now the trainer faces a difficult task: he wants to swap the animals' positions so that all the hamsters stood together and all the tigers also stood together. The trainer swaps the animals in pairs not to create a mess. He orders two animals to step out of the circle and swap places. As hamsters feel highly uncomfortable when tigers are nearby as well as tigers get nervous when there's so much potential prey around (consisting not only of hamsters but also of yummier spectators), the trainer wants to spend as little time as possible moving the animals, i.e. he wants to achieve it with the minimal number of swaps. Your task is to help him.
Input
The first line contains number n (2 β€ n β€ 1000) which indicates the total number of animals in the arena. The second line contains the description of the animals' positions. The line consists of n symbols "H" and "T". The "H"s correspond to hamsters and the "T"s correspond to tigers. It is guaranteed that at least one hamster and one tiger are present on the arena. The animals are given in the order in which they are located circle-wise, in addition, the last animal stands near the first one.
Output
Print the single number which is the minimal number of swaps that let the trainer to achieve his goal.
Examples
Input
3
HTH
Output
0
Input
9
HTHTHTHHT
Output
2
Note
In the first example we shouldn't move anybody because the animals of each species already stand apart from the other species. In the second example you may swap, for example, the tiger in position 2 with the hamster in position 5 and then β the tiger in position 9 with the hamster in position 7. | instruction | 0 | 27,840 | 14 | 55,680 |
Tags: two pointers
Correct Solution:
```
n=int(input())
a=input()
b=a.count('T')
c=-1
for i in range(n):
d=0
for j in range(b):
d+=int(a[(i+j)%n]=='H')
if c==-1 or d<c:
c=d
print(c)
``` | output | 1 | 27,840 | 14 | 55,681 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Today there is going to be an unusual performance at the circus β hamsters and tigers will perform together! All of them stand in circle along the arena edge and now the trainer faces a difficult task: he wants to swap the animals' positions so that all the hamsters stood together and all the tigers also stood together. The trainer swaps the animals in pairs not to create a mess. He orders two animals to step out of the circle and swap places. As hamsters feel highly uncomfortable when tigers are nearby as well as tigers get nervous when there's so much potential prey around (consisting not only of hamsters but also of yummier spectators), the trainer wants to spend as little time as possible moving the animals, i.e. he wants to achieve it with the minimal number of swaps. Your task is to help him.
Input
The first line contains number n (2 β€ n β€ 1000) which indicates the total number of animals in the arena. The second line contains the description of the animals' positions. The line consists of n symbols "H" and "T". The "H"s correspond to hamsters and the "T"s correspond to tigers. It is guaranteed that at least one hamster and one tiger are present on the arena. The animals are given in the order in which they are located circle-wise, in addition, the last animal stands near the first one.
Output
Print the single number which is the minimal number of swaps that let the trainer to achieve his goal.
Examples
Input
3
HTH
Output
0
Input
9
HTHTHTHHT
Output
2
Note
In the first example we shouldn't move anybody because the animals of each species already stand apart from the other species. In the second example you may swap, for example, the tiger in position 2 with the hamster in position 5 and then β the tiger in position 9 with the hamster in position 7. | instruction | 0 | 27,841 | 14 | 55,682 |
Tags: two pointers
Correct Solution:
```
n = int(input())
line = input().strip()
span = line.count('T')
count = line[:span].count('H')
best = count
for i in range(n):
if line[i] == 'H':
count -= 1
if line[(i + span) % n] == 'H':
count += 1
best = min(best, count)
print(best)
``` | output | 1 | 27,841 | 14 | 55,683 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Today there is going to be an unusual performance at the circus β hamsters and tigers will perform together! All of them stand in circle along the arena edge and now the trainer faces a difficult task: he wants to swap the animals' positions so that all the hamsters stood together and all the tigers also stood together. The trainer swaps the animals in pairs not to create a mess. He orders two animals to step out of the circle and swap places. As hamsters feel highly uncomfortable when tigers are nearby as well as tigers get nervous when there's so much potential prey around (consisting not only of hamsters but also of yummier spectators), the trainer wants to spend as little time as possible moving the animals, i.e. he wants to achieve it with the minimal number of swaps. Your task is to help him.
Input
The first line contains number n (2 β€ n β€ 1000) which indicates the total number of animals in the arena. The second line contains the description of the animals' positions. The line consists of n symbols "H" and "T". The "H"s correspond to hamsters and the "T"s correspond to tigers. It is guaranteed that at least one hamster and one tiger are present on the arena. The animals are given in the order in which they are located circle-wise, in addition, the last animal stands near the first one.
Output
Print the single number which is the minimal number of swaps that let the trainer to achieve his goal.
Examples
Input
3
HTH
Output
0
Input
9
HTHTHTHHT
Output
2
Note
In the first example we shouldn't move anybody because the animals of each species already stand apart from the other species. In the second example you may swap, for example, the tiger in position 2 with the hamster in position 5 and then β the tiger in position 9 with the hamster in position 7. | instruction | 0 | 27,842 | 14 | 55,684 |
Tags: two pointers
Correct Solution:
```
n = int(input())
s = input()
kh = s.count('H')
lst = ['H'] * kh + ['T'] * (n - kh)
best = kh
for i in range(n):
best = min(sum(lst[j] != s[j] for j in range(n)) // 2, best)
lst.append(lst.pop(0))
print(best)
``` | output | 1 | 27,842 | 14 | 55,685 |
Provide a correct Python 3 solution for this coding contest problem.
The secret organization AiZu AnalyticS has launched a top-secret investigation. There are N people targeted, with identification numbers from 1 to N. As an AZAS Information Strategy Investigator, you have decided to determine the number of people in your target who meet at least one of the following conditions:
* Those who do not belong to the organization $ A $ and who own the product $ C $.
* A person who belongs to the organization $ B $ and owns the product $ C $.
A program that calculates the number of people who meet the conditions when the identification number of the person who belongs to the organization $ A $, the person who belongs to the organization $ B $, and the person who owns the product $ C $ is given as input. Create. However, be careful not to count duplicate people who meet both conditions.
(Supplement: Regarding the above conditions)
Let $ A $, $ B $, and $ C $ be the sets of some elements selected from the set of natural numbers from 1 to $ N $. The number of people who satisfy the condition is the number of elements that satisfy $ (\ bar {A} \ cap C) \ cup (B \ cap C) $ (painted part in the figure). However, $ \ bar {A} $ is a complement of the set $ A $.
<image>
Input
The input is given in the following format.
N
X a1 a2 ... aX
Y b1 b2 ... bY
Z c1 c2 ... cZ
The input is 4 lines, and the number of people to be surveyed N (1 β€ N β€ 100) is given in the first line. On the second line, the number X (0 β€ X β€ N) of those who belong to the organization $ A $, followed by the identification number ai (1 β€ ai β€ N) of those who belong to the organization $ A $. Given. On the third line, the number Y (0 β€ Y β€ N) of those who belong to the organization $ B $, followed by the identification number bi (1 β€ bi β€ N) of those who belong to the organization $ B $. Given. On the fourth line, the number Z (0 β€ Z β€ N) of the person who owns the product $ C $, followed by the identification number ci (1 β€ ci β€ N) of the person who owns the product $ C $. ) Is given.
Output
Output the number of people who meet the conditions on one line.
Examples
Input
5
3 1 2 3
2 4 5
2 3 4
Output
1
Input
100
3 1 100 4
0
2 2 3
Output
2 | instruction | 0 | 28,177 | 14 | 56,354 |
"Correct Solution:
```
n = int(input())
x,*a = list(map(int, input().split()))
y,*b = list(map(int, input().split()))
z,*c = list(map(int, input().split()))
u = set(range(1,n+1))
a = set(a)
b = set(b)
c = set(c)
s1 = (u - a) & c
s2 = b & c
s3 = s1 & s2
print(len(s1) + len(s2) -len(s3))
``` | output | 1 | 28,177 | 14 | 56,355 |
Provide a correct Python 3 solution for this coding contest problem.
The secret organization AiZu AnalyticS has launched a top-secret investigation. There are N people targeted, with identification numbers from 1 to N. As an AZAS Information Strategy Investigator, you have decided to determine the number of people in your target who meet at least one of the following conditions:
* Those who do not belong to the organization $ A $ and who own the product $ C $.
* A person who belongs to the organization $ B $ and owns the product $ C $.
A program that calculates the number of people who meet the conditions when the identification number of the person who belongs to the organization $ A $, the person who belongs to the organization $ B $, and the person who owns the product $ C $ is given as input. Create. However, be careful not to count duplicate people who meet both conditions.
(Supplement: Regarding the above conditions)
Let $ A $, $ B $, and $ C $ be the sets of some elements selected from the set of natural numbers from 1 to $ N $. The number of people who satisfy the condition is the number of elements that satisfy $ (\ bar {A} \ cap C) \ cup (B \ cap C) $ (painted part in the figure). However, $ \ bar {A} $ is a complement of the set $ A $.
<image>
Input
The input is given in the following format.
N
X a1 a2 ... aX
Y b1 b2 ... bY
Z c1 c2 ... cZ
The input is 4 lines, and the number of people to be surveyed N (1 β€ N β€ 100) is given in the first line. On the second line, the number X (0 β€ X β€ N) of those who belong to the organization $ A $, followed by the identification number ai (1 β€ ai β€ N) of those who belong to the organization $ A $. Given. On the third line, the number Y (0 β€ Y β€ N) of those who belong to the organization $ B $, followed by the identification number bi (1 β€ bi β€ N) of those who belong to the organization $ B $. Given. On the fourth line, the number Z (0 β€ Z β€ N) of the person who owns the product $ C $, followed by the identification number ci (1 β€ ci β€ N) of the person who owns the product $ C $. ) Is given.
Output
Output the number of people who meet the conditions on one line.
Examples
Input
5
3 1 2 3
2 4 5
2 3 4
Output
1
Input
100
3 1 100 4
0
2 2 3
Output
2 | instruction | 0 | 28,178 | 14 | 56,356 |
"Correct Solution:
```
N = int(input())
U = [str(i+1) for i in range(N)]
X = input().split()[1:]
Y = input().split()[1:]
Z = input().split()[1:]
print(len(((set(U) - set(X)) & set(Z)) | (set(Y) & set(Z))))
``` | output | 1 | 28,178 | 14 | 56,357 |
Provide a correct Python 3 solution for this coding contest problem.
The secret organization AiZu AnalyticS has launched a top-secret investigation. There are N people targeted, with identification numbers from 1 to N. As an AZAS Information Strategy Investigator, you have decided to determine the number of people in your target who meet at least one of the following conditions:
* Those who do not belong to the organization $ A $ and who own the product $ C $.
* A person who belongs to the organization $ B $ and owns the product $ C $.
A program that calculates the number of people who meet the conditions when the identification number of the person who belongs to the organization $ A $, the person who belongs to the organization $ B $, and the person who owns the product $ C $ is given as input. Create. However, be careful not to count duplicate people who meet both conditions.
(Supplement: Regarding the above conditions)
Let $ A $, $ B $, and $ C $ be the sets of some elements selected from the set of natural numbers from 1 to $ N $. The number of people who satisfy the condition is the number of elements that satisfy $ (\ bar {A} \ cap C) \ cup (B \ cap C) $ (painted part in the figure). However, $ \ bar {A} $ is a complement of the set $ A $.
<image>
Input
The input is given in the following format.
N
X a1 a2 ... aX
Y b1 b2 ... bY
Z c1 c2 ... cZ
The input is 4 lines, and the number of people to be surveyed N (1 β€ N β€ 100) is given in the first line. On the second line, the number X (0 β€ X β€ N) of those who belong to the organization $ A $, followed by the identification number ai (1 β€ ai β€ N) of those who belong to the organization $ A $. Given. On the third line, the number Y (0 β€ Y β€ N) of those who belong to the organization $ B $, followed by the identification number bi (1 β€ bi β€ N) of those who belong to the organization $ B $. Given. On the fourth line, the number Z (0 β€ Z β€ N) of the person who owns the product $ C $, followed by the identification number ci (1 β€ ci β€ N) of the person who owns the product $ C $. ) Is given.
Output
Output the number of people who meet the conditions on one line.
Examples
Input
5
3 1 2 3
2 4 5
2 3 4
Output
1
Input
100
3 1 100 4
0
2 2 3
Output
2 | instruction | 0 | 28,179 | 14 | 56,358 |
"Correct Solution:
```
N=int(input())
X=list(map(int,input().split()))
Y=list(map(int,input().split()))
Z=list(map(int,input().split()))
X.pop(0)
Y.pop(0)
Z.pop(0)
result=[i for i in Z if i not in X or i in Y]
print(len(result))
``` | output | 1 | 28,179 | 14 | 56,359 |
Provide a correct Python 3 solution for this coding contest problem.
The secret organization AiZu AnalyticS has launched a top-secret investigation. There are N people targeted, with identification numbers from 1 to N. As an AZAS Information Strategy Investigator, you have decided to determine the number of people in your target who meet at least one of the following conditions:
* Those who do not belong to the organization $ A $ and who own the product $ C $.
* A person who belongs to the organization $ B $ and owns the product $ C $.
A program that calculates the number of people who meet the conditions when the identification number of the person who belongs to the organization $ A $, the person who belongs to the organization $ B $, and the person who owns the product $ C $ is given as input. Create. However, be careful not to count duplicate people who meet both conditions.
(Supplement: Regarding the above conditions)
Let $ A $, $ B $, and $ C $ be the sets of some elements selected from the set of natural numbers from 1 to $ N $. The number of people who satisfy the condition is the number of elements that satisfy $ (\ bar {A} \ cap C) \ cup (B \ cap C) $ (painted part in the figure). However, $ \ bar {A} $ is a complement of the set $ A $.
<image>
Input
The input is given in the following format.
N
X a1 a2 ... aX
Y b1 b2 ... bY
Z c1 c2 ... cZ
The input is 4 lines, and the number of people to be surveyed N (1 β€ N β€ 100) is given in the first line. On the second line, the number X (0 β€ X β€ N) of those who belong to the organization $ A $, followed by the identification number ai (1 β€ ai β€ N) of those who belong to the organization $ A $. Given. On the third line, the number Y (0 β€ Y β€ N) of those who belong to the organization $ B $, followed by the identification number bi (1 β€ bi β€ N) of those who belong to the organization $ B $. Given. On the fourth line, the number Z (0 β€ Z β€ N) of the person who owns the product $ C $, followed by the identification number ci (1 β€ ci β€ N) of the person who owns the product $ C $. ) Is given.
Output
Output the number of people who meet the conditions on one line.
Examples
Input
5
3 1 2 3
2 4 5
2 3 4
Output
1
Input
100
3 1 100 4
0
2 2 3
Output
2 | instruction | 0 | 28,180 | 14 | 56,360 |
"Correct Solution:
```
# coding=utf-8
if __name__ == '__main__':
N = int(input())
all_list = {i for i in range(1, N+1)}
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))
C_list = list(map(int, input().split()))
A_number = A_list[0]
A_identity = set(A_list[1:])
B_number = B_list[0]
B_identity = set(B_list[1:])
C_number = C_list[0]
C_identity = set(C_list[1:])
not_A_identity = all_list - A_identity
cond1 = not_A_identity & C_identity
cond2 = B_identity & C_identity
suspected = cond1 | cond2
print(len(suspected))
``` | output | 1 | 28,180 | 14 | 56,361 |
Provide a correct Python 3 solution for this coding contest problem.
The secret organization AiZu AnalyticS has launched a top-secret investigation. There are N people targeted, with identification numbers from 1 to N. As an AZAS Information Strategy Investigator, you have decided to determine the number of people in your target who meet at least one of the following conditions:
* Those who do not belong to the organization $ A $ and who own the product $ C $.
* A person who belongs to the organization $ B $ and owns the product $ C $.
A program that calculates the number of people who meet the conditions when the identification number of the person who belongs to the organization $ A $, the person who belongs to the organization $ B $, and the person who owns the product $ C $ is given as input. Create. However, be careful not to count duplicate people who meet both conditions.
(Supplement: Regarding the above conditions)
Let $ A $, $ B $, and $ C $ be the sets of some elements selected from the set of natural numbers from 1 to $ N $. The number of people who satisfy the condition is the number of elements that satisfy $ (\ bar {A} \ cap C) \ cup (B \ cap C) $ (painted part in the figure). However, $ \ bar {A} $ is a complement of the set $ A $.
<image>
Input
The input is given in the following format.
N
X a1 a2 ... aX
Y b1 b2 ... bY
Z c1 c2 ... cZ
The input is 4 lines, and the number of people to be surveyed N (1 β€ N β€ 100) is given in the first line. On the second line, the number X (0 β€ X β€ N) of those who belong to the organization $ A $, followed by the identification number ai (1 β€ ai β€ N) of those who belong to the organization $ A $. Given. On the third line, the number Y (0 β€ Y β€ N) of those who belong to the organization $ B $, followed by the identification number bi (1 β€ bi β€ N) of those who belong to the organization $ B $. Given. On the fourth line, the number Z (0 β€ Z β€ N) of the person who owns the product $ C $, followed by the identification number ci (1 β€ ci β€ N) of the person who owns the product $ C $. ) Is given.
Output
Output the number of people who meet the conditions on one line.
Examples
Input
5
3 1 2 3
2 4 5
2 3 4
Output
1
Input
100
3 1 100 4
0
2 2 3
Output
2 | instruction | 0 | 28,181 | 14 | 56,362 |
"Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
X = a.pop(0)
a = set(a)
b = list(map(int, input().split()))
Y = b.pop(0)
b = set(b)
c = list(map(int, input().split()))
Z = c.pop(0)
c = set(c)
a_bar = {x for x in range(1, n+1) if x not in a}
ans = (a_bar & c) | (b & c)
print(len(ans))
``` | output | 1 | 28,181 | 14 | 56,363 |
Provide a correct Python 3 solution for this coding contest problem.
The secret organization AiZu AnalyticS has launched a top-secret investigation. There are N people targeted, with identification numbers from 1 to N. As an AZAS Information Strategy Investigator, you have decided to determine the number of people in your target who meet at least one of the following conditions:
* Those who do not belong to the organization $ A $ and who own the product $ C $.
* A person who belongs to the organization $ B $ and owns the product $ C $.
A program that calculates the number of people who meet the conditions when the identification number of the person who belongs to the organization $ A $, the person who belongs to the organization $ B $, and the person who owns the product $ C $ is given as input. Create. However, be careful not to count duplicate people who meet both conditions.
(Supplement: Regarding the above conditions)
Let $ A $, $ B $, and $ C $ be the sets of some elements selected from the set of natural numbers from 1 to $ N $. The number of people who satisfy the condition is the number of elements that satisfy $ (\ bar {A} \ cap C) \ cup (B \ cap C) $ (painted part in the figure). However, $ \ bar {A} $ is a complement of the set $ A $.
<image>
Input
The input is given in the following format.
N
X a1 a2 ... aX
Y b1 b2 ... bY
Z c1 c2 ... cZ
The input is 4 lines, and the number of people to be surveyed N (1 β€ N β€ 100) is given in the first line. On the second line, the number X (0 β€ X β€ N) of those who belong to the organization $ A $, followed by the identification number ai (1 β€ ai β€ N) of those who belong to the organization $ A $. Given. On the third line, the number Y (0 β€ Y β€ N) of those who belong to the organization $ B $, followed by the identification number bi (1 β€ bi β€ N) of those who belong to the organization $ B $. Given. On the fourth line, the number Z (0 β€ Z β€ N) of the person who owns the product $ C $, followed by the identification number ci (1 β€ ci β€ N) of the person who owns the product $ C $. ) Is given.
Output
Output the number of people who meet the conditions on one line.
Examples
Input
5
3 1 2 3
2 4 5
2 3 4
Output
1
Input
100
3 1 100 4
0
2 2 3
Output
2 | instruction | 0 | 28,182 | 14 | 56,364 |
"Correct Solution:
```
N = int(input())
X = [int(i) for i in input().split()]
Y = [int(i) for i in input().split()]
Z = [int(i) for i in input().split()]
table = [[0 for i in range(3)] for j in range(N)]
for i in range(1, len(X)):
table[X[i] - 1][0] = 1
for i in range(1, len(Y)):
table[Y[i] - 1][1] = 1
for i in range(1, len(Z)):
table[Z[i] - 1][2] = 1
ans = 0
for i in range(len(table)):
if (table[i][0] == 0 and table[i][2] == 1) or (table[i][1] == 1 and table[i][2] == 1):
ans = ans + 1
print(ans)
``` | output | 1 | 28,182 | 14 | 56,365 |
Provide a correct Python 3 solution for this coding contest problem.
The secret organization AiZu AnalyticS has launched a top-secret investigation. There are N people targeted, with identification numbers from 1 to N. As an AZAS Information Strategy Investigator, you have decided to determine the number of people in your target who meet at least one of the following conditions:
* Those who do not belong to the organization $ A $ and who own the product $ C $.
* A person who belongs to the organization $ B $ and owns the product $ C $.
A program that calculates the number of people who meet the conditions when the identification number of the person who belongs to the organization $ A $, the person who belongs to the organization $ B $, and the person who owns the product $ C $ is given as input. Create. However, be careful not to count duplicate people who meet both conditions.
(Supplement: Regarding the above conditions)
Let $ A $, $ B $, and $ C $ be the sets of some elements selected from the set of natural numbers from 1 to $ N $. The number of people who satisfy the condition is the number of elements that satisfy $ (\ bar {A} \ cap C) \ cup (B \ cap C) $ (painted part in the figure). However, $ \ bar {A} $ is a complement of the set $ A $.
<image>
Input
The input is given in the following format.
N
X a1 a2 ... aX
Y b1 b2 ... bY
Z c1 c2 ... cZ
The input is 4 lines, and the number of people to be surveyed N (1 β€ N β€ 100) is given in the first line. On the second line, the number X (0 β€ X β€ N) of those who belong to the organization $ A $, followed by the identification number ai (1 β€ ai β€ N) of those who belong to the organization $ A $. Given. On the third line, the number Y (0 β€ Y β€ N) of those who belong to the organization $ B $, followed by the identification number bi (1 β€ bi β€ N) of those who belong to the organization $ B $. Given. On the fourth line, the number Z (0 β€ Z β€ N) of the person who owns the product $ C $, followed by the identification number ci (1 β€ ci β€ N) of the person who owns the product $ C $. ) Is given.
Output
Output the number of people who meet the conditions on one line.
Examples
Input
5
3 1 2 3
2 4 5
2 3 4
Output
1
Input
100
3 1 100 4
0
2 2 3
Output
2 | instruction | 0 | 28,183 | 14 | 56,366 |
"Correct Solution:
```
n = int(input())
x, *a = map(int, input().split())
y, *b = map(int, input().split())
z, *c = map(int, input().split())
_a = set(range(1, n+1)) ^ set(a)
match1 = set(_a) & set(c)
match2 = set(b) & set(c)
print(len(match1 | match2))
``` | output | 1 | 28,183 | 14 | 56,367 |
Provide a correct Python 3 solution for this coding contest problem.
The secret organization AiZu AnalyticS has launched a top-secret investigation. There are N people targeted, with identification numbers from 1 to N. As an AZAS Information Strategy Investigator, you have decided to determine the number of people in your target who meet at least one of the following conditions:
* Those who do not belong to the organization $ A $ and who own the product $ C $.
* A person who belongs to the organization $ B $ and owns the product $ C $.
A program that calculates the number of people who meet the conditions when the identification number of the person who belongs to the organization $ A $, the person who belongs to the organization $ B $, and the person who owns the product $ C $ is given as input. Create. However, be careful not to count duplicate people who meet both conditions.
(Supplement: Regarding the above conditions)
Let $ A $, $ B $, and $ C $ be the sets of some elements selected from the set of natural numbers from 1 to $ N $. The number of people who satisfy the condition is the number of elements that satisfy $ (\ bar {A} \ cap C) \ cup (B \ cap C) $ (painted part in the figure). However, $ \ bar {A} $ is a complement of the set $ A $.
<image>
Input
The input is given in the following format.
N
X a1 a2 ... aX
Y b1 b2 ... bY
Z c1 c2 ... cZ
The input is 4 lines, and the number of people to be surveyed N (1 β€ N β€ 100) is given in the first line. On the second line, the number X (0 β€ X β€ N) of those who belong to the organization $ A $, followed by the identification number ai (1 β€ ai β€ N) of those who belong to the organization $ A $. Given. On the third line, the number Y (0 β€ Y β€ N) of those who belong to the organization $ B $, followed by the identification number bi (1 β€ bi β€ N) of those who belong to the organization $ B $. Given. On the fourth line, the number Z (0 β€ Z β€ N) of the person who owns the product $ C $, followed by the identification number ci (1 β€ ci β€ N) of the person who owns the product $ C $. ) Is given.
Output
Output the number of people who meet the conditions on one line.
Examples
Input
5
3 1 2 3
2 4 5
2 3 4
Output
1
Input
100
3 1 100 4
0
2 2 3
Output
2 | instruction | 0 | 28,184 | 14 | 56,368 |
"Correct Solution:
```
N = int(input())
Target = [0] * N
A = list(map(int, input().split()))
if A[0] != 0 :
for i in range(A[0]) :
Target[A[i+1]-1] += 1
B = list(map(int, input().split()))
if B[0] != 0 :
for i in range(B[0]) :
Target[B[i+1]-1] += 2
C = list(map(int, input().split()))
if C[0] != 0 :
for i in range(C[0]) :
Target[C[i+1]-1] +=4
S = 0
for i in range(N) :
if Target[i] == 4 or Target[i] == 6 or Target[i] == 7 :
S += 1
print(S)
``` | output | 1 | 28,184 | 14 | 56,369 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The secret organization AiZu AnalyticS has launched a top-secret investigation. There are N people targeted, with identification numbers from 1 to N. As an AZAS Information Strategy Investigator, you have decided to determine the number of people in your target who meet at least one of the following conditions:
* Those who do not belong to the organization $ A $ and who own the product $ C $.
* A person who belongs to the organization $ B $ and owns the product $ C $.
A program that calculates the number of people who meet the conditions when the identification number of the person who belongs to the organization $ A $, the person who belongs to the organization $ B $, and the person who owns the product $ C $ is given as input. Create. However, be careful not to count duplicate people who meet both conditions.
(Supplement: Regarding the above conditions)
Let $ A $, $ B $, and $ C $ be the sets of some elements selected from the set of natural numbers from 1 to $ N $. The number of people who satisfy the condition is the number of elements that satisfy $ (\ bar {A} \ cap C) \ cup (B \ cap C) $ (painted part in the figure). However, $ \ bar {A} $ is a complement of the set $ A $.
<image>
Input
The input is given in the following format.
N
X a1 a2 ... aX
Y b1 b2 ... bY
Z c1 c2 ... cZ
The input is 4 lines, and the number of people to be surveyed N (1 β€ N β€ 100) is given in the first line. On the second line, the number X (0 β€ X β€ N) of those who belong to the organization $ A $, followed by the identification number ai (1 β€ ai β€ N) of those who belong to the organization $ A $. Given. On the third line, the number Y (0 β€ Y β€ N) of those who belong to the organization $ B $, followed by the identification number bi (1 β€ bi β€ N) of those who belong to the organization $ B $. Given. On the fourth line, the number Z (0 β€ Z β€ N) of the person who owns the product $ C $, followed by the identification number ci (1 β€ ci β€ N) of the person who owns the product $ C $. ) Is given.
Output
Output the number of people who meet the conditions on one line.
Examples
Input
5
3 1 2 3
2 4 5
2 3 4
Output
1
Input
100
3 1 100 4
0
2 2 3
Output
2
Submitted Solution:
```
N = int(input())
A, B, C =list(map(int, input().split())), list(map(int, input().split())), list(map(int, input().split()))
D = set(A[1:] + B[1:] + C[1:])
ans = 0
for e in D:
if(e not in A[1:] and e in C[1:]) or (e in B[1:] and e in C[1:]):
ans += 1
print(ans)
``` | instruction | 0 | 28,185 | 14 | 56,370 |
Yes | output | 1 | 28,185 | 14 | 56,371 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The secret organization AiZu AnalyticS has launched a top-secret investigation. There are N people targeted, with identification numbers from 1 to N. As an AZAS Information Strategy Investigator, you have decided to determine the number of people in your target who meet at least one of the following conditions:
* Those who do not belong to the organization $ A $ and who own the product $ C $.
* A person who belongs to the organization $ B $ and owns the product $ C $.
A program that calculates the number of people who meet the conditions when the identification number of the person who belongs to the organization $ A $, the person who belongs to the organization $ B $, and the person who owns the product $ C $ is given as input. Create. However, be careful not to count duplicate people who meet both conditions.
(Supplement: Regarding the above conditions)
Let $ A $, $ B $, and $ C $ be the sets of some elements selected from the set of natural numbers from 1 to $ N $. The number of people who satisfy the condition is the number of elements that satisfy $ (\ bar {A} \ cap C) \ cup (B \ cap C) $ (painted part in the figure). However, $ \ bar {A} $ is a complement of the set $ A $.
<image>
Input
The input is given in the following format.
N
X a1 a2 ... aX
Y b1 b2 ... bY
Z c1 c2 ... cZ
The input is 4 lines, and the number of people to be surveyed N (1 β€ N β€ 100) is given in the first line. On the second line, the number X (0 β€ X β€ N) of those who belong to the organization $ A $, followed by the identification number ai (1 β€ ai β€ N) of those who belong to the organization $ A $. Given. On the third line, the number Y (0 β€ Y β€ N) of those who belong to the organization $ B $, followed by the identification number bi (1 β€ bi β€ N) of those who belong to the organization $ B $. Given. On the fourth line, the number Z (0 β€ Z β€ N) of the person who owns the product $ C $, followed by the identification number ci (1 β€ ci β€ N) of the person who owns the product $ C $. ) Is given.
Output
Output the number of people who meet the conditions on one line.
Examples
Input
5
3 1 2 3
2 4 5
2 3 4
Output
1
Input
100
3 1 100 4
0
2 2 3
Output
2
Submitted Solution:
```
n=int(input())
member=[0]*(n+1)
count=0
a=list(map(int,input().split()))
for i in range(1,a[0]+1):
member[a[i]]+=1
b=list(map(int,input().split()))
for i in range(1,b[0]+1):
member[b[i]]+=2
c=list(map(int,input().split()))
for i in range(1,c[0]+1):
member[c[i]]+=4
for i in member:
if i==4 or i>5:count+=1
print(count)
``` | instruction | 0 | 28,186 | 14 | 56,372 |
Yes | output | 1 | 28,186 | 14 | 56,373 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The secret organization AiZu AnalyticS has launched a top-secret investigation. There are N people targeted, with identification numbers from 1 to N. As an AZAS Information Strategy Investigator, you have decided to determine the number of people in your target who meet at least one of the following conditions:
* Those who do not belong to the organization $ A $ and who own the product $ C $.
* A person who belongs to the organization $ B $ and owns the product $ C $.
A program that calculates the number of people who meet the conditions when the identification number of the person who belongs to the organization $ A $, the person who belongs to the organization $ B $, and the person who owns the product $ C $ is given as input. Create. However, be careful not to count duplicate people who meet both conditions.
(Supplement: Regarding the above conditions)
Let $ A $, $ B $, and $ C $ be the sets of some elements selected from the set of natural numbers from 1 to $ N $. The number of people who satisfy the condition is the number of elements that satisfy $ (\ bar {A} \ cap C) \ cup (B \ cap C) $ (painted part in the figure). However, $ \ bar {A} $ is a complement of the set $ A $.
<image>
Input
The input is given in the following format.
N
X a1 a2 ... aX
Y b1 b2 ... bY
Z c1 c2 ... cZ
The input is 4 lines, and the number of people to be surveyed N (1 β€ N β€ 100) is given in the first line. On the second line, the number X (0 β€ X β€ N) of those who belong to the organization $ A $, followed by the identification number ai (1 β€ ai β€ N) of those who belong to the organization $ A $. Given. On the third line, the number Y (0 β€ Y β€ N) of those who belong to the organization $ B $, followed by the identification number bi (1 β€ bi β€ N) of those who belong to the organization $ B $. Given. On the fourth line, the number Z (0 β€ Z β€ N) of the person who owns the product $ C $, followed by the identification number ci (1 β€ ci β€ N) of the person who owns the product $ C $. ) Is given.
Output
Output the number of people who meet the conditions on one line.
Examples
Input
5
3 1 2 3
2 4 5
2 3 4
Output
1
Input
100
3 1 100 4
0
2 2 3
Output
2
Submitted Solution:
```
n = int(input())
mp = [[0, 0, 0] for _ in range(n)]
xs = list(map(int, input().split()))
ys = list(map(int, input().split()))
zs = list(map(int, input().split()))
for a in xs[1:]:
mp[a - 1][0] = 1
for b in ys[1:]:
mp[b - 1][1] = 1
for c in zs[1:]:
mp[c - 1][2] = 1
ans = 0
for t in mp:
if t[2] and ((not t[0]) or t[1]):
ans += 1
print(ans)
``` | instruction | 0 | 28,187 | 14 | 56,374 |
Yes | output | 1 | 28,187 | 14 | 56,375 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The secret organization AiZu AnalyticS has launched a top-secret investigation. There are N people targeted, with identification numbers from 1 to N. As an AZAS Information Strategy Investigator, you have decided to determine the number of people in your target who meet at least one of the following conditions:
* Those who do not belong to the organization $ A $ and who own the product $ C $.
* A person who belongs to the organization $ B $ and owns the product $ C $.
A program that calculates the number of people who meet the conditions when the identification number of the person who belongs to the organization $ A $, the person who belongs to the organization $ B $, and the person who owns the product $ C $ is given as input. Create. However, be careful not to count duplicate people who meet both conditions.
(Supplement: Regarding the above conditions)
Let $ A $, $ B $, and $ C $ be the sets of some elements selected from the set of natural numbers from 1 to $ N $. The number of people who satisfy the condition is the number of elements that satisfy $ (\ bar {A} \ cap C) \ cup (B \ cap C) $ (painted part in the figure). However, $ \ bar {A} $ is a complement of the set $ A $.
<image>
Input
The input is given in the following format.
N
X a1 a2 ... aX
Y b1 b2 ... bY
Z c1 c2 ... cZ
The input is 4 lines, and the number of people to be surveyed N (1 β€ N β€ 100) is given in the first line. On the second line, the number X (0 β€ X β€ N) of those who belong to the organization $ A $, followed by the identification number ai (1 β€ ai β€ N) of those who belong to the organization $ A $. Given. On the third line, the number Y (0 β€ Y β€ N) of those who belong to the organization $ B $, followed by the identification number bi (1 β€ bi β€ N) of those who belong to the organization $ B $. Given. On the fourth line, the number Z (0 β€ Z β€ N) of the person who owns the product $ C $, followed by the identification number ci (1 β€ ci β€ N) of the person who owns the product $ C $. ) Is given.
Output
Output the number of people who meet the conditions on one line.
Examples
Input
5
3 1 2 3
2 4 5
2 3 4
Output
1
Input
100
3 1 100 4
0
2 2 3
Output
2
Submitted Solution:
```
n = int(input())
s = input()
a = [int(num) for num in s.split()]
s = input()
b = [int(num) for num in s.split()]
s = input()
c = [int(num) for num in s.split()]
isa = []
isb = []
isc = []
for i in range(n + 1):
isa.append(False)
isb.append(False)
isc.append(False)
for i in range(1, a[0] + 1):
isa[a[i]] = True
for i in range(1, b[0] + 1):
isb[b[i]] = True
for i in range(1, c[0] + 1):
isc[c[i]] = True
C = c[0]
CA = 0
ABC = 0
for i in range(1, n + 1):
if isc[i] & isa[i]:
CA += 1
if isa[i] & isb[i] & isc[i]:
ABC += 1
print(C - CA + ABC)
``` | instruction | 0 | 28,188 | 14 | 56,376 |
Yes | output | 1 | 28,188 | 14 | 56,377 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.
It takes the first swimmer exactly a minutes to swim across the entire pool and come back, exactly b minutes for the second swimmer and c minutes for the third. Hence, the first swimmer will be on the left side of the pool after 0, a, 2a, 3a, ... minutes after the start time, the second one will be at 0, b, 2b, 3b, ... minutes, and the third one will be on the left side of the pool after 0, c, 2c, 3c, ... minutes.
You came to the left side of the pool exactly p minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
Input
The first line of the input contains a single integer t (1 β€ t β€ 1000) β the number of test cases. Next t lines contains test case descriptions, one per line.
Each line contains four integers p, a, b and c (1 β€ p, a, b, c β€ 10^{18}), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
Output
For each test case, output one integer β how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
Example
Input
4
9 5 4 8
2 6 10 9
10 2 5 10
10 9 9 9
Output
1
4
0
8
Note
In the first test case, the first swimmer is on the left side in 0, 5, 10, 15, β¦ minutes after the start time, the second swimmer is on the left side in 0, 4, 8, 12, β¦ minutes after the start time, and the third swimmer is on the left side in 0, 8, 16, 24, β¦ minutes after the start time. You arrived at the pool in 9 minutes after the start time and in a minute you will meet the first swimmer on the left side.
In the second test case, the first swimmer is on the left side in 0, 6, 12, 18, β¦ minutes after the start time, the second swimmer is on the left side in 0, 10, 20, 30, β¦ minutes after the start time, and the third swimmer is on the left side in 0, 9, 18, 27, β¦ minutes after the start time. You arrived at the pool 2 minutes after the start time and after 4 minutes meet the first swimmer on the left side.
In the third test case, you came to the pool 10 minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!
In the fourth test case, all swimmers are located on the left side in 0, 9, 18, 27, β¦ minutes after the start time. You arrived at the pool 10 minutes after the start time and after 8 minutes meet all three swimmers on the left side. | instruction | 0 | 28,474 | 14 | 56,948 |
Tags: math
Correct Solution:
```
tc = int(input())
def waittime(p, a, b, c):
if p % a == 0 or p % b == 0 or p % c == 0:
return 0
return min(a - p % a, b - p % b, c - p % c)
for i in range(tc):
arr = input()
arr = arr.split()
p, a, b, c = list(map(int, arr))
print(waittime(p, a, b, c))
``` | output | 1 | 28,474 | 14 | 56,949 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.
It takes the first swimmer exactly a minutes to swim across the entire pool and come back, exactly b minutes for the second swimmer and c minutes for the third. Hence, the first swimmer will be on the left side of the pool after 0, a, 2a, 3a, ... minutes after the start time, the second one will be at 0, b, 2b, 3b, ... minutes, and the third one will be on the left side of the pool after 0, c, 2c, 3c, ... minutes.
You came to the left side of the pool exactly p minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
Input
The first line of the input contains a single integer t (1 β€ t β€ 1000) β the number of test cases. Next t lines contains test case descriptions, one per line.
Each line contains four integers p, a, b and c (1 β€ p, a, b, c β€ 10^{18}), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
Output
For each test case, output one integer β how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
Example
Input
4
9 5 4 8
2 6 10 9
10 2 5 10
10 9 9 9
Output
1
4
0
8
Note
In the first test case, the first swimmer is on the left side in 0, 5, 10, 15, β¦ minutes after the start time, the second swimmer is on the left side in 0, 4, 8, 12, β¦ minutes after the start time, and the third swimmer is on the left side in 0, 8, 16, 24, β¦ minutes after the start time. You arrived at the pool in 9 minutes after the start time and in a minute you will meet the first swimmer on the left side.
In the second test case, the first swimmer is on the left side in 0, 6, 12, 18, β¦ minutes after the start time, the second swimmer is on the left side in 0, 10, 20, 30, β¦ minutes after the start time, and the third swimmer is on the left side in 0, 9, 18, 27, β¦ minutes after the start time. You arrived at the pool 2 minutes after the start time and after 4 minutes meet the first swimmer on the left side.
In the third test case, you came to the pool 10 minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!
In the fourth test case, all swimmers are located on the left side in 0, 9, 18, 27, β¦ minutes after the start time. You arrived at the pool 10 minutes after the start time and after 8 minutes meet all three swimmers on the left side. | instruction | 0 | 28,475 | 14 | 56,950 |
Tags: math
Correct Solution:
```
for i in range(int(input())):
p, a, b, c = map(int, input().split())
if not p % a or not p % b or not p % c:
print(0)
else:
print(min(a - p % a, b - p % b, c - p % c))
``` | output | 1 | 28,475 | 14 | 56,951 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.
It takes the first swimmer exactly a minutes to swim across the entire pool and come back, exactly b minutes for the second swimmer and c minutes for the third. Hence, the first swimmer will be on the left side of the pool after 0, a, 2a, 3a, ... minutes after the start time, the second one will be at 0, b, 2b, 3b, ... minutes, and the third one will be on the left side of the pool after 0, c, 2c, 3c, ... minutes.
You came to the left side of the pool exactly p minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
Input
The first line of the input contains a single integer t (1 β€ t β€ 1000) β the number of test cases. Next t lines contains test case descriptions, one per line.
Each line contains four integers p, a, b and c (1 β€ p, a, b, c β€ 10^{18}), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
Output
For each test case, output one integer β how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
Example
Input
4
9 5 4 8
2 6 10 9
10 2 5 10
10 9 9 9
Output
1
4
0
8
Note
In the first test case, the first swimmer is on the left side in 0, 5, 10, 15, β¦ minutes after the start time, the second swimmer is on the left side in 0, 4, 8, 12, β¦ minutes after the start time, and the third swimmer is on the left side in 0, 8, 16, 24, β¦ minutes after the start time. You arrived at the pool in 9 minutes after the start time and in a minute you will meet the first swimmer on the left side.
In the second test case, the first swimmer is on the left side in 0, 6, 12, 18, β¦ minutes after the start time, the second swimmer is on the left side in 0, 10, 20, 30, β¦ minutes after the start time, and the third swimmer is on the left side in 0, 9, 18, 27, β¦ minutes after the start time. You arrived at the pool 2 minutes after the start time and after 4 minutes meet the first swimmer on the left side.
In the third test case, you came to the pool 10 minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!
In the fourth test case, all swimmers are located on the left side in 0, 9, 18, 27, β¦ minutes after the start time. You arrived at the pool 10 minutes after the start time and after 8 minutes meet all three swimmers on the left side. | instruction | 0 | 28,476 | 14 | 56,952 |
Tags: math
Correct Solution:
```
for _ in range(int(input())):
p,a,b,c=map(int,input().split())
ans=None
x=p//a
y=a-p+(x*a)
if x*a==p:
ans=0
else:
ans=y
x=p//b
y=b-p+(x*b)
if x*b==p:
ans=0
elif y<ans:
ans=y
x=p//c
y=c-p+(x*c)
if x*c==p:
ans=0
elif y<ans:
ans=y
print(ans)
``` | output | 1 | 28,476 | 14 | 56,953 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.
It takes the first swimmer exactly a minutes to swim across the entire pool and come back, exactly b minutes for the second swimmer and c minutes for the third. Hence, the first swimmer will be on the left side of the pool after 0, a, 2a, 3a, ... minutes after the start time, the second one will be at 0, b, 2b, 3b, ... minutes, and the third one will be on the left side of the pool after 0, c, 2c, 3c, ... minutes.
You came to the left side of the pool exactly p minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
Input
The first line of the input contains a single integer t (1 β€ t β€ 1000) β the number of test cases. Next t lines contains test case descriptions, one per line.
Each line contains four integers p, a, b and c (1 β€ p, a, b, c β€ 10^{18}), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
Output
For each test case, output one integer β how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
Example
Input
4
9 5 4 8
2 6 10 9
10 2 5 10
10 9 9 9
Output
1
4
0
8
Note
In the first test case, the first swimmer is on the left side in 0, 5, 10, 15, β¦ minutes after the start time, the second swimmer is on the left side in 0, 4, 8, 12, β¦ minutes after the start time, and the third swimmer is on the left side in 0, 8, 16, 24, β¦ minutes after the start time. You arrived at the pool in 9 minutes after the start time and in a minute you will meet the first swimmer on the left side.
In the second test case, the first swimmer is on the left side in 0, 6, 12, 18, β¦ minutes after the start time, the second swimmer is on the left side in 0, 10, 20, 30, β¦ minutes after the start time, and the third swimmer is on the left side in 0, 9, 18, 27, β¦ minutes after the start time. You arrived at the pool 2 minutes after the start time and after 4 minutes meet the first swimmer on the left side.
In the third test case, you came to the pool 10 minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!
In the fourth test case, all swimmers are located on the left side in 0, 9, 18, 27, β¦ minutes after the start time. You arrived at the pool 10 minutes after the start time and after 8 minutes meet all three swimmers on the left side. | instruction | 0 | 28,477 | 14 | 56,954 |
Tags: math
Correct Solution:
```
# Question A: Three Swimmers
def answer(p, a, b, c):
pma = p % a
pmb = p % b
pmc = p % c
if pma == 0 or pmb == 0 or pmc == 0:
return 0
else:
return min(a - pma, b - pmb, c - pmc)
def main():
t = int(input())
p = [0] * t
a = [0] * t
b = [0] * t
c = [0] * t
for j in range(t):
p[j], a[j], b[j], c[j] = [int(i) for i in input().split()]
for j in range(t):
print(answer(p[j], a[j], b[j], c[j]))
main()
``` | output | 1 | 28,477 | 14 | 56,955 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.
It takes the first swimmer exactly a minutes to swim across the entire pool and come back, exactly b minutes for the second swimmer and c minutes for the third. Hence, the first swimmer will be on the left side of the pool after 0, a, 2a, 3a, ... minutes after the start time, the second one will be at 0, b, 2b, 3b, ... minutes, and the third one will be on the left side of the pool after 0, c, 2c, 3c, ... minutes.
You came to the left side of the pool exactly p minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
Input
The first line of the input contains a single integer t (1 β€ t β€ 1000) β the number of test cases. Next t lines contains test case descriptions, one per line.
Each line contains four integers p, a, b and c (1 β€ p, a, b, c β€ 10^{18}), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
Output
For each test case, output one integer β how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
Example
Input
4
9 5 4 8
2 6 10 9
10 2 5 10
10 9 9 9
Output
1
4
0
8
Note
In the first test case, the first swimmer is on the left side in 0, 5, 10, 15, β¦ minutes after the start time, the second swimmer is on the left side in 0, 4, 8, 12, β¦ minutes after the start time, and the third swimmer is on the left side in 0, 8, 16, 24, β¦ minutes after the start time. You arrived at the pool in 9 minutes after the start time and in a minute you will meet the first swimmer on the left side.
In the second test case, the first swimmer is on the left side in 0, 6, 12, 18, β¦ minutes after the start time, the second swimmer is on the left side in 0, 10, 20, 30, β¦ minutes after the start time, and the third swimmer is on the left side in 0, 9, 18, 27, β¦ minutes after the start time. You arrived at the pool 2 minutes after the start time and after 4 minutes meet the first swimmer on the left side.
In the third test case, you came to the pool 10 minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!
In the fourth test case, all swimmers are located on the left side in 0, 9, 18, 27, β¦ minutes after the start time. You arrived at the pool 10 minutes after the start time and after 8 minutes meet all three swimmers on the left side. | instruction | 0 | 28,478 | 14 | 56,956 |
Tags: math
Correct Solution:
```
import math
T = int(input())
def solve():
p, a, b, c = map(int, input().split())
a *= p // a + (0 if p % a == 0 else 1)
b *= p // b + (0 if p % b == 0 else 1)
c *= p // c + (0 if p % c == 0 else 1)
print(min(a - p, b - p, c - p))
for i in range(T):
solve()
``` | output | 1 | 28,478 | 14 | 56,957 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.
It takes the first swimmer exactly a minutes to swim across the entire pool and come back, exactly b minutes for the second swimmer and c minutes for the third. Hence, the first swimmer will be on the left side of the pool after 0, a, 2a, 3a, ... minutes after the start time, the second one will be at 0, b, 2b, 3b, ... minutes, and the third one will be on the left side of the pool after 0, c, 2c, 3c, ... minutes.
You came to the left side of the pool exactly p minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
Input
The first line of the input contains a single integer t (1 β€ t β€ 1000) β the number of test cases. Next t lines contains test case descriptions, one per line.
Each line contains four integers p, a, b and c (1 β€ p, a, b, c β€ 10^{18}), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
Output
For each test case, output one integer β how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
Example
Input
4
9 5 4 8
2 6 10 9
10 2 5 10
10 9 9 9
Output
1
4
0
8
Note
In the first test case, the first swimmer is on the left side in 0, 5, 10, 15, β¦ minutes after the start time, the second swimmer is on the left side in 0, 4, 8, 12, β¦ minutes after the start time, and the third swimmer is on the left side in 0, 8, 16, 24, β¦ minutes after the start time. You arrived at the pool in 9 minutes after the start time and in a minute you will meet the first swimmer on the left side.
In the second test case, the first swimmer is on the left side in 0, 6, 12, 18, β¦ minutes after the start time, the second swimmer is on the left side in 0, 10, 20, 30, β¦ minutes after the start time, and the third swimmer is on the left side in 0, 9, 18, 27, β¦ minutes after the start time. You arrived at the pool 2 minutes after the start time and after 4 minutes meet the first swimmer on the left side.
In the third test case, you came to the pool 10 minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!
In the fourth test case, all swimmers are located on the left side in 0, 9, 18, 27, β¦ minutes after the start time. You arrived at the pool 10 minutes after the start time and after 8 minutes meet all three swimmers on the left side. | instruction | 0 | 28,479 | 14 | 56,958 |
Tags: math
Correct Solution:
```
# cook your dish here
for j in range(int(input())):
p,a,b,c=map(int, input().split())
x=p//a
y=p//b
z=p//c
if (p%a==0 or p%b==0 or p%c==0):
print("0")
else:
print(min((x+1)*a-p,(y+1)*b-p,(z+1)*c-p))
``` | output | 1 | 28,479 | 14 | 56,959 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.
It takes the first swimmer exactly a minutes to swim across the entire pool and come back, exactly b minutes for the second swimmer and c minutes for the third. Hence, the first swimmer will be on the left side of the pool after 0, a, 2a, 3a, ... minutes after the start time, the second one will be at 0, b, 2b, 3b, ... minutes, and the third one will be on the left side of the pool after 0, c, 2c, 3c, ... minutes.
You came to the left side of the pool exactly p minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
Input
The first line of the input contains a single integer t (1 β€ t β€ 1000) β the number of test cases. Next t lines contains test case descriptions, one per line.
Each line contains four integers p, a, b and c (1 β€ p, a, b, c β€ 10^{18}), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
Output
For each test case, output one integer β how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
Example
Input
4
9 5 4 8
2 6 10 9
10 2 5 10
10 9 9 9
Output
1
4
0
8
Note
In the first test case, the first swimmer is on the left side in 0, 5, 10, 15, β¦ minutes after the start time, the second swimmer is on the left side in 0, 4, 8, 12, β¦ minutes after the start time, and the third swimmer is on the left side in 0, 8, 16, 24, β¦ minutes after the start time. You arrived at the pool in 9 minutes after the start time and in a minute you will meet the first swimmer on the left side.
In the second test case, the first swimmer is on the left side in 0, 6, 12, 18, β¦ minutes after the start time, the second swimmer is on the left side in 0, 10, 20, 30, β¦ minutes after the start time, and the third swimmer is on the left side in 0, 9, 18, 27, β¦ minutes after the start time. You arrived at the pool 2 minutes after the start time and after 4 minutes meet the first swimmer on the left side.
In the third test case, you came to the pool 10 minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!
In the fourth test case, all swimmers are located on the left side in 0, 9, 18, 27, β¦ minutes after the start time. You arrived at the pool 10 minutes after the start time and after 8 minutes meet all three swimmers on the left side. | instruction | 0 | 28,480 | 14 | 56,960 |
Tags: math
Correct Solution:
```
t = (int)(input())
for _ in range(t):
p, a, b, c = map(int, input().split())
x = (p + a - 1) // a
y = (p + b - 1) // b
z = (p + c - 1) // c
ans = min(x * a - p, y * b - p, z * c - p)
print(ans, end="\n")
``` | output | 1 | 28,480 | 14 | 56,961 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.
It takes the first swimmer exactly a minutes to swim across the entire pool and come back, exactly b minutes for the second swimmer and c minutes for the third. Hence, the first swimmer will be on the left side of the pool after 0, a, 2a, 3a, ... minutes after the start time, the second one will be at 0, b, 2b, 3b, ... minutes, and the third one will be on the left side of the pool after 0, c, 2c, 3c, ... minutes.
You came to the left side of the pool exactly p minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
Input
The first line of the input contains a single integer t (1 β€ t β€ 1000) β the number of test cases. Next t lines contains test case descriptions, one per line.
Each line contains four integers p, a, b and c (1 β€ p, a, b, c β€ 10^{18}), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
Output
For each test case, output one integer β how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
Example
Input
4
9 5 4 8
2 6 10 9
10 2 5 10
10 9 9 9
Output
1
4
0
8
Note
In the first test case, the first swimmer is on the left side in 0, 5, 10, 15, β¦ minutes after the start time, the second swimmer is on the left side in 0, 4, 8, 12, β¦ minutes after the start time, and the third swimmer is on the left side in 0, 8, 16, 24, β¦ minutes after the start time. You arrived at the pool in 9 minutes after the start time and in a minute you will meet the first swimmer on the left side.
In the second test case, the first swimmer is on the left side in 0, 6, 12, 18, β¦ minutes after the start time, the second swimmer is on the left side in 0, 10, 20, 30, β¦ minutes after the start time, and the third swimmer is on the left side in 0, 9, 18, 27, β¦ minutes after the start time. You arrived at the pool 2 minutes after the start time and after 4 minutes meet the first swimmer on the left side.
In the third test case, you came to the pool 10 minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!
In the fourth test case, all swimmers are located on the left side in 0, 9, 18, 27, β¦ minutes after the start time. You arrived at the pool 10 minutes after the start time and after 8 minutes meet all three swimmers on the left side. | instruction | 0 | 28,481 | 14 | 56,962 |
Tags: math
Correct Solution:
```
import math
res = []
for _ in range(int(input())):
p,a,b,c = map(int,input().split())
a1 = p%a
b1 = p%b
c1 = p%c
if a1==0: res.append(0)
elif b1==0: res.append(0)
elif c1==0: res.append(0)
else: res.append(min(a-a1,b-b1,c-c1))
for i in res: print(i)
``` | output | 1 | 28,481 | 14 | 56,963 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A way to make a new task is to make it nondeterministic or probabilistic. For example, the hard task of Topcoder SRM 595, Constellation, is the probabilistic version of a convex hull.
Let's try to make a new task. Firstly we will use the following task. There are n people, sort them by their name. It is just an ordinary sorting problem, but we can make it more interesting by adding nondeterministic element. There are n people, each person will use either his/her first name or last name as a handle. Can the lexicographical order of the handles be exactly equal to the given permutation p?
More formally, if we denote the handle of the i-th person as hi, then the following condition must hold: <image>.
Input
The first line contains an integer n (1 β€ n β€ 105) β the number of people.
The next n lines each contains two strings. The i-th line contains strings fi and si (1 β€ |fi|, |si| β€ 50) β the first name and last name of the i-th person. Each string consists only of lowercase English letters. All of the given 2n strings will be distinct.
The next line contains n distinct integers: p1, p2, ..., pn (1 β€ pi β€ n).
Output
If it is possible, output "YES", otherwise output "NO".
Examples
Input
3
gennady korotkevich
petr mitrichev
gaoyuan chen
1 2 3
Output
NO
Input
3
gennady korotkevich
petr mitrichev
gaoyuan chen
3 1 2
Output
YES
Input
2
galileo galilei
nicolaus copernicus
2 1
Output
YES
Input
10
rean schwarzer
fei claussell
alisa reinford
eliot craig
laura arseid
jusis albarea
machias regnitz
sara valestin
emma millstein
gaius worzel
1 2 3 4 5 6 7 8 9 10
Output
NO
Input
10
rean schwarzer
fei claussell
alisa reinford
eliot craig
laura arseid
jusis albarea
machias regnitz
sara valestin
emma millstein
gaius worzel
2 4 9 6 5 7 1 3 8 10
Output
YES
Note
In example 1 and 2, we have 3 people: tourist, Petr and me (cgy4ever). You can see that whatever handle is chosen, I must be the first, then tourist and Petr must be the last.
In example 3, if Copernicus uses "copernicus" as his handle, everything will be alright. | instruction | 0 | 28,612 | 14 | 57,224 |
Tags: greedy
Correct Solution:
```
n = int(input())
names = []
for i in range(n):
names.append(input().split())
arr = list(map(int, input().split()))
prev = min(names[arr[0]-1])
for i in range(1, n):
if min(names[arr[i]-1]) >= prev:
prev = min(names[arr[i]-1])
elif max(names[arr[i]-1]) >= prev:
prev = max(names[arr[i]-1])
else:
print('NO')
break
else:
print('YES')
``` | output | 1 | 28,612 | 14 | 57,225 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A way to make a new task is to make it nondeterministic or probabilistic. For example, the hard task of Topcoder SRM 595, Constellation, is the probabilistic version of a convex hull.
Let's try to make a new task. Firstly we will use the following task. There are n people, sort them by their name. It is just an ordinary sorting problem, but we can make it more interesting by adding nondeterministic element. There are n people, each person will use either his/her first name or last name as a handle. Can the lexicographical order of the handles be exactly equal to the given permutation p?
More formally, if we denote the handle of the i-th person as hi, then the following condition must hold: <image>.
Input
The first line contains an integer n (1 β€ n β€ 105) β the number of people.
The next n lines each contains two strings. The i-th line contains strings fi and si (1 β€ |fi|, |si| β€ 50) β the first name and last name of the i-th person. Each string consists only of lowercase English letters. All of the given 2n strings will be distinct.
The next line contains n distinct integers: p1, p2, ..., pn (1 β€ pi β€ n).
Output
If it is possible, output "YES", otherwise output "NO".
Examples
Input
3
gennady korotkevich
petr mitrichev
gaoyuan chen
1 2 3
Output
NO
Input
3
gennady korotkevich
petr mitrichev
gaoyuan chen
3 1 2
Output
YES
Input
2
galileo galilei
nicolaus copernicus
2 1
Output
YES
Input
10
rean schwarzer
fei claussell
alisa reinford
eliot craig
laura arseid
jusis albarea
machias regnitz
sara valestin
emma millstein
gaius worzel
1 2 3 4 5 6 7 8 9 10
Output
NO
Input
10
rean schwarzer
fei claussell
alisa reinford
eliot craig
laura arseid
jusis albarea
machias regnitz
sara valestin
emma millstein
gaius worzel
2 4 9 6 5 7 1 3 8 10
Output
YES
Note
In example 1 and 2, we have 3 people: tourist, Petr and me (cgy4ever). You can see that whatever handle is chosen, I must be the first, then tourist and Petr must be the last.
In example 3, if Copernicus uses "copernicus" as his handle, everything will be alright. | instruction | 0 | 28,613 | 14 | 57,226 |
Tags: greedy
Correct Solution:
```
def solve(arr, p):
cur = "A" * 51
for rank in p:
if min(arr[rank - 1]) > cur:
cur = min(arr[rank - 1])
elif max(arr[rank - 1]) > cur:
cur = max(arr[rank - 1])
else:
return False
return True
n = int(input())
arr = list()
for i in range(n):
arr.append(input().split())
p = list(map(int, input().split()))
print("YES") if solve(arr, p) else print("NO")
``` | output | 1 | 28,613 | 14 | 57,227 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A way to make a new task is to make it nondeterministic or probabilistic. For example, the hard task of Topcoder SRM 595, Constellation, is the probabilistic version of a convex hull.
Let's try to make a new task. Firstly we will use the following task. There are n people, sort them by their name. It is just an ordinary sorting problem, but we can make it more interesting by adding nondeterministic element. There are n people, each person will use either his/her first name or last name as a handle. Can the lexicographical order of the handles be exactly equal to the given permutation p?
More formally, if we denote the handle of the i-th person as hi, then the following condition must hold: <image>.
Input
The first line contains an integer n (1 β€ n β€ 105) β the number of people.
The next n lines each contains two strings. The i-th line contains strings fi and si (1 β€ |fi|, |si| β€ 50) β the first name and last name of the i-th person. Each string consists only of lowercase English letters. All of the given 2n strings will be distinct.
The next line contains n distinct integers: p1, p2, ..., pn (1 β€ pi β€ n).
Output
If it is possible, output "YES", otherwise output "NO".
Examples
Input
3
gennady korotkevich
petr mitrichev
gaoyuan chen
1 2 3
Output
NO
Input
3
gennady korotkevich
petr mitrichev
gaoyuan chen
3 1 2
Output
YES
Input
2
galileo galilei
nicolaus copernicus
2 1
Output
YES
Input
10
rean schwarzer
fei claussell
alisa reinford
eliot craig
laura arseid
jusis albarea
machias regnitz
sara valestin
emma millstein
gaius worzel
1 2 3 4 5 6 7 8 9 10
Output
NO
Input
10
rean schwarzer
fei claussell
alisa reinford
eliot craig
laura arseid
jusis albarea
machias regnitz
sara valestin
emma millstein
gaius worzel
2 4 9 6 5 7 1 3 8 10
Output
YES
Note
In example 1 and 2, we have 3 people: tourist, Petr and me (cgy4ever). You can see that whatever handle is chosen, I must be the first, then tourist and Petr must be the last.
In example 3, if Copernicus uses "copernicus" as his handle, everything will be alright. | instruction | 0 | 28,614 | 14 | 57,228 |
Tags: greedy
Correct Solution:
```
n = int(input())
name = ['' for i in range(n)]
sname = ['' for i in range(n)]
for i in range(n):
name[i], sname[i] = input().split()
p = list(map(int, input().split()))
for i in range(n):
p[i] -= 1
nm = [False for i in range(n)]
snm = [False for i in range(n)]
nm[0] = snm[0] = True
for i in range(1, n):
if nm[i-1]:
if name[p[i]] > name[p[i-1]]:
nm[i] = True
if sname[p[i]] > name[p[i-1]]:
snm[i] = True
if snm[i-1]:
if name[p[i]] > sname[p[i-1]]:
nm[i] = True
if sname[p[i]] > sname[p[i-1]]:
snm[i] = True
print("YES" if snm[-1] or nm[-1] else "NO")
``` | output | 1 | 28,614 | 14 | 57,229 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A way to make a new task is to make it nondeterministic or probabilistic. For example, the hard task of Topcoder SRM 595, Constellation, is the probabilistic version of a convex hull.
Let's try to make a new task. Firstly we will use the following task. There are n people, sort them by their name. It is just an ordinary sorting problem, but we can make it more interesting by adding nondeterministic element. There are n people, each person will use either his/her first name or last name as a handle. Can the lexicographical order of the handles be exactly equal to the given permutation p?
More formally, if we denote the handle of the i-th person as hi, then the following condition must hold: <image>.
Input
The first line contains an integer n (1 β€ n β€ 105) β the number of people.
The next n lines each contains two strings. The i-th line contains strings fi and si (1 β€ |fi|, |si| β€ 50) β the first name and last name of the i-th person. Each string consists only of lowercase English letters. All of the given 2n strings will be distinct.
The next line contains n distinct integers: p1, p2, ..., pn (1 β€ pi β€ n).
Output
If it is possible, output "YES", otherwise output "NO".
Examples
Input
3
gennady korotkevich
petr mitrichev
gaoyuan chen
1 2 3
Output
NO
Input
3
gennady korotkevich
petr mitrichev
gaoyuan chen
3 1 2
Output
YES
Input
2
galileo galilei
nicolaus copernicus
2 1
Output
YES
Input
10
rean schwarzer
fei claussell
alisa reinford
eliot craig
laura arseid
jusis albarea
machias regnitz
sara valestin
emma millstein
gaius worzel
1 2 3 4 5 6 7 8 9 10
Output
NO
Input
10
rean schwarzer
fei claussell
alisa reinford
eliot craig
laura arseid
jusis albarea
machias regnitz
sara valestin
emma millstein
gaius worzel
2 4 9 6 5 7 1 3 8 10
Output
YES
Note
In example 1 and 2, we have 3 people: tourist, Petr and me (cgy4ever). You can see that whatever handle is chosen, I must be the first, then tourist and Petr must be the last.
In example 3, if Copernicus uses "copernicus" as his handle, everything will be alright. | instruction | 0 | 28,615 | 14 | 57,230 |
Tags: greedy
Correct Solution:
```
"""
Codeforces Contest 270 Problem C
Author : chaotic_iak
Language: Python 3.3.4
"""
def main():
n, = read()
names = []
for i in range(n): names.extend([(x,i+1) for x in read(1)])
names.sort()
p = read()
i = 0
j = 0
while i < n and j < 2*n:
if names[j][1] == p[i]:
i += 1
j += 1
if i == n:
print("YES")
else:
print("NO")
################################### NON-SOLUTION STUFF BELOW
def read(mode=2):
# 0: String
# 1: List of strings
# 2: List of integers
inputs = input().strip()
if mode == 0: return inputs
if mode == 1: return inputs.split()
if mode == 2: return list(map(int, inputs.split()))
def write(s="\n"):
if s is None: s = ""
if isinstance(s, list): s = " ".join(map(str, s))
s = str(s)
print(s, end="")
write(main())
``` | output | 1 | 28,615 | 14 | 57,231 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A way to make a new task is to make it nondeterministic or probabilistic. For example, the hard task of Topcoder SRM 595, Constellation, is the probabilistic version of a convex hull.
Let's try to make a new task. Firstly we will use the following task. There are n people, sort them by their name. It is just an ordinary sorting problem, but we can make it more interesting by adding nondeterministic element. There are n people, each person will use either his/her first name or last name as a handle. Can the lexicographical order of the handles be exactly equal to the given permutation p?
More formally, if we denote the handle of the i-th person as hi, then the following condition must hold: <image>.
Input
The first line contains an integer n (1 β€ n β€ 105) β the number of people.
The next n lines each contains two strings. The i-th line contains strings fi and si (1 β€ |fi|, |si| β€ 50) β the first name and last name of the i-th person. Each string consists only of lowercase English letters. All of the given 2n strings will be distinct.
The next line contains n distinct integers: p1, p2, ..., pn (1 β€ pi β€ n).
Output
If it is possible, output "YES", otherwise output "NO".
Examples
Input
3
gennady korotkevich
petr mitrichev
gaoyuan chen
1 2 3
Output
NO
Input
3
gennady korotkevich
petr mitrichev
gaoyuan chen
3 1 2
Output
YES
Input
2
galileo galilei
nicolaus copernicus
2 1
Output
YES
Input
10
rean schwarzer
fei claussell
alisa reinford
eliot craig
laura arseid
jusis albarea
machias regnitz
sara valestin
emma millstein
gaius worzel
1 2 3 4 5 6 7 8 9 10
Output
NO
Input
10
rean schwarzer
fei claussell
alisa reinford
eliot craig
laura arseid
jusis albarea
machias regnitz
sara valestin
emma millstein
gaius worzel
2 4 9 6 5 7 1 3 8 10
Output
YES
Note
In example 1 and 2, we have 3 people: tourist, Petr and me (cgy4ever). You can see that whatever handle is chosen, I must be the first, then tourist and Petr must be the last.
In example 3, if Copernicus uses "copernicus" as his handle, everything will be alright. | instruction | 0 | 28,616 | 14 | 57,232 |
Tags: greedy
Correct Solution:
```
# Description of the problem can be found at http://codeforces.com/problemset/problem/472/C
n = int(input())
l_n = list([input().split() for _ in range(n)])
o = list(map(int, input().split()))
l = "A"
for n in range(n):
l_n[o[n] - 1].sort()
if l_n[o[n] - 1][0] > l:
l = l_n[o[n] - 1][0]
elif l_n[o[n] - 1][1] > l:
l = l_n[o[n] - 1][1]
else:
print("NO")
exit()
print("YES")
``` | output | 1 | 28,616 | 14 | 57,233 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A way to make a new task is to make it nondeterministic or probabilistic. For example, the hard task of Topcoder SRM 595, Constellation, is the probabilistic version of a convex hull.
Let's try to make a new task. Firstly we will use the following task. There are n people, sort them by their name. It is just an ordinary sorting problem, but we can make it more interesting by adding nondeterministic element. There are n people, each person will use either his/her first name or last name as a handle. Can the lexicographical order of the handles be exactly equal to the given permutation p?
More formally, if we denote the handle of the i-th person as hi, then the following condition must hold: <image>.
Input
The first line contains an integer n (1 β€ n β€ 105) β the number of people.
The next n lines each contains two strings. The i-th line contains strings fi and si (1 β€ |fi|, |si| β€ 50) β the first name and last name of the i-th person. Each string consists only of lowercase English letters. All of the given 2n strings will be distinct.
The next line contains n distinct integers: p1, p2, ..., pn (1 β€ pi β€ n).
Output
If it is possible, output "YES", otherwise output "NO".
Examples
Input
3
gennady korotkevich
petr mitrichev
gaoyuan chen
1 2 3
Output
NO
Input
3
gennady korotkevich
petr mitrichev
gaoyuan chen
3 1 2
Output
YES
Input
2
galileo galilei
nicolaus copernicus
2 1
Output
YES
Input
10
rean schwarzer
fei claussell
alisa reinford
eliot craig
laura arseid
jusis albarea
machias regnitz
sara valestin
emma millstein
gaius worzel
1 2 3 4 5 6 7 8 9 10
Output
NO
Input
10
rean schwarzer
fei claussell
alisa reinford
eliot craig
laura arseid
jusis albarea
machias regnitz
sara valestin
emma millstein
gaius worzel
2 4 9 6 5 7 1 3 8 10
Output
YES
Note
In example 1 and 2, we have 3 people: tourist, Petr and me (cgy4ever). You can see that whatever handle is chosen, I must be the first, then tourist and Petr must be the last.
In example 3, if Copernicus uses "copernicus" as his handle, everything will be alright. | instruction | 0 | 28,617 | 14 | 57,234 |
Tags: greedy
Correct Solution:
```
n = int(input())
a = [input().split() for i in range(n)]
b = list(map(int, input().split()))
b = [e-1 for e in b]
a = [sorted(e) for e in a]
s = a[b[0]][0]
v=True
for i in range(n):
if (s<=a[b[i]][0]):
s=a[b[i]][0]
else:
if (s<=a[b[i]][1]):
s=a[b[i]][1]
else:
v=False
if v:
print("YES")
else:
print("NO")
``` | output | 1 | 28,617 | 14 | 57,235 |
Provide tags and a correct Python 3 solution for this coding contest problem.
A way to make a new task is to make it nondeterministic or probabilistic. For example, the hard task of Topcoder SRM 595, Constellation, is the probabilistic version of a convex hull.
Let's try to make a new task. Firstly we will use the following task. There are n people, sort them by their name. It is just an ordinary sorting problem, but we can make it more interesting by adding nondeterministic element. There are n people, each person will use either his/her first name or last name as a handle. Can the lexicographical order of the handles be exactly equal to the given permutation p?
More formally, if we denote the handle of the i-th person as hi, then the following condition must hold: <image>.
Input
The first line contains an integer n (1 β€ n β€ 105) β the number of people.
The next n lines each contains two strings. The i-th line contains strings fi and si (1 β€ |fi|, |si| β€ 50) β the first name and last name of the i-th person. Each string consists only of lowercase English letters. All of the given 2n strings will be distinct.
The next line contains n distinct integers: p1, p2, ..., pn (1 β€ pi β€ n).
Output
If it is possible, output "YES", otherwise output "NO".
Examples
Input
3
gennady korotkevich
petr mitrichev
gaoyuan chen
1 2 3
Output
NO
Input
3
gennady korotkevich
petr mitrichev
gaoyuan chen
3 1 2
Output
YES
Input
2
galileo galilei
nicolaus copernicus
2 1
Output
YES
Input
10
rean schwarzer
fei claussell
alisa reinford
eliot craig
laura arseid
jusis albarea
machias regnitz
sara valestin
emma millstein
gaius worzel
1 2 3 4 5 6 7 8 9 10
Output
NO
Input
10
rean schwarzer
fei claussell
alisa reinford
eliot craig
laura arseid
jusis albarea
machias regnitz
sara valestin
emma millstein
gaius worzel
2 4 9 6 5 7 1 3 8 10
Output
YES
Note
In example 1 and 2, we have 3 people: tourist, Petr and me (cgy4ever). You can see that whatever handle is chosen, I must be the first, then tourist and Petr must be the last.
In example 3, if Copernicus uses "copernicus" as his handle, everything will be alright. | instruction | 0 | 28,618 | 14 | 57,236 |
Tags: greedy
Correct Solution:
```
n=int(input())
l=[]
f=0
s3=""
for i in range(n):
s1,s2=map(str,input().strip().split(' '))
l.append([s1,s2])
#print(l)
lst = list(map(int,input().strip().split(' ')))
for j in range(n):
if j==0:
s3=min(l[lst[j]-1][0],l[lst[j]-1][1])
else:
s4=l[lst[j]-1][0]
s5=l[lst[j]-1][1]
if s4>s5:
s4,s5=s5,s4
if s3>s4 and s3>s5:
f=1
print('NO')
break
else:
if s3<s4:
s3=s4
else:
s3=s5
#print(s3)
if f==0:
print('YES')
``` | output | 1 | 28,618 | 14 | 57,237 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.