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 |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR
Submitted Solution:
```
n, m, s, f = map(int, input().split())
l = []
for i in range(m):
t, c,r = map(int, input().split())
l.append([t, c, r])
if s > f:
st = ""
t = 1
i = 0
while(i<m):
if t == l[i][0]:
if (s<l[i][1] or s>l[i][2]) and (s-1<l[i][1] or s-1>l[i][2]):
s-=1
st+="L"
else:
st+="X"
i+=1
if s == f:
break
else:
st+='L'
s-=1
if s == f:
break
t+=1
while(s>f):
s-=1
st+='L'
print(st)
if s<f:
st = ""
t = 1
i = 0
while(i<m):
if t == l[i][0]:
if (s<l[i][1] or s>l[i][2]) and (s+1<l[i][1] or s+1>l[i][2]):
s+=1
st+="R"
else:
st+="X"
i+=1
if s == f:
break
else:
st+='R'
s+=1
if s == f:
break
t+=1
while(s<f):
s+=1
st+='R'
print(st)
``` | instruction | 0 | 69,291 | 14 | 138,582 |
Yes | output | 1 | 69,291 | 14 | 138,583 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR
Submitted Solution:
```
def geto(a, b):
return max(0, min(a[1], b[1]) - max(a[0], b[0])+1)
n,m,s,f = map(int, input().split())
di = {}
for _ in range(m):
t, l, r = map(int, input().split())
di[t] = [l, r]
t = 1
ans = []
while s != f:
if f > s:
inte = [s, s+1]
if t in di and geto(inte, di[t]): ans += ['X']
else:
ans += ['R']
s += 1
else:
inte = [s-1, s]
if t in di and geto(inte, di[t]): ans += ['X']
else:
ans += ['L']
s -= 1
t += 1
print("".join(ans))
``` | instruction | 0 | 69,292 | 14 | 138,584 |
Yes | output | 1 | 69,292 | 14 | 138,585 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR
Submitted Solution:
```
n,m,s,f=list(map(int,input().split()))
if s>f:
a='L'
c=-1
elif s==f:
a='X'
c=0
else:
a='R'
c=1
l=[]
for i in range(m):
l.append(list(map(int,input().split())))
ans=''
if l[0][0]!=1:
d=l[0][0]-1
while s!=f and d>0:
s=s+c
ans=ans+a
d=d-1
for i in range(m):
if s==f:
break
if i==0:
if (s>=l[i][1] and s<=l[i][2]) or (s+c>=l[i][1] and s+c<=l[i][2]):
ans=ans+'X'
else:
ans=ans+a
s=s+c
else:
if l[i][0]-l[i-1][0]==1:
if (s>=l[i][1] and s<=l[i][2]) or (s+c>=l[i][1] and s+c<=l[i][2]):
ans=ans+'X'
else:
ans=ans+a
s=s+c
else:
d=l[i][0]-l[i-1][0]-1
while s!=f and d>0:
s=s+c
ans=ans+a
d=d-1
if s==f:
break
if (s>=l[i][1] and s<=l[i][2]) or (s+c>=l[i][1] and s+c<=l[i][2]):
ans=ans+'X'
else:
ans=ans+a
s=s+c
if s==f:
break
if s!=f:
ans=ans+a
s=s+c
print(ans)
``` | instruction | 0 | 69,293 | 14 | 138,586 |
No | output | 1 | 69,293 | 14 | 138,587 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR
Submitted Solution:
```
n, m, s, f = map(int, input().split())
l = []
for i in range(m):
t, c,r = map(int, input().split())
l.append([t, c, r])
if s > f:
st = ""
t = 1
i = 0
while(i<m):
print(t, i, s, f, l[i])
if t == l[i][0]:
if (s<l[i][1] or s>l[i][2]) and (s-1<l[i][1] or s-1>l[i][2]):
s-=1
st+="L"
else:
st+="X"
i+=1
if s == f:
break
t+=1
while(s<f):
s-=1
st+='L'
print(st)
if s<f:
st = ""
t = 1
i = 0
while(i<m):
print(t, i, s, f, l[i])
if t == l[i][0]:
if (s<l[i][1] or s>l[i][2]) and (s+1<l[i][1] or s+1>l[i][2]):
s+=1
st+="R"
else:
st+="X"
i+=1
if s == f:
break
t+=1
while(s<f):
s+=1
st+='R'
print(st)
``` | instruction | 0 | 69,294 | 14 | 138,588 |
No | output | 1 | 69,294 | 14 | 138,589 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR
Submitted Solution:
```
import sys
input = sys.stdin.readline
def print(val):
sys.stdout.write(str(val) + '\n')
def prog():
n,m,s,f = map(int,input().split())
curr = s
step = 1
steps = {}
output = []
for i in range(m):
t,l,r = map(int,input().split())
steps[t] = [l,r]
if f > s:
while curr != f:
if step not in steps:
output.append('R')
step += 1
curr += 1
elif curr + 1 < steps[step][0] or curr > steps[step][1]:
output.append('R')
step += 1
curr += 1
else:
output.append('X')
step += 1
else:
while curr != f:
if step not in steps:
output.append('L')
step += 1
curr -= 1
elif curr + 1 < steps[step][0] or curr > steps[step][1]:
output.append('L')
step += 1
curr -= 1
else:
output.append('X')
step += 1
print(''.join(output))
prog()
``` | instruction | 0 | 69,295 | 14 | 138,590 |
No | output | 1 | 69,295 | 14 | 138,591 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Xenia the vigorous detective faced n (n β₯ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right.
Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone.
But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step ti (steps are numbered from 1) Xenia watches spies numbers li, li + 1, li + 2, ..., ri (1 β€ li β€ ri β€ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him.
You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps).
Input
The first line contains four integers n, m, s and f (1 β€ n, m β€ 105; 1 β€ s, f β€ n; s β f; n β₯ 2). Each of the following m lines contains three integers ti, li, ri (1 β€ ti β€ 109, 1 β€ li β€ ri β€ n). It is guaranteed that t1 < t2 < t3 < ... < tm.
Output
Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X".
As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note.
If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
Examples
Input
3 5 1 3
1 1 2
2 2 3
3 3 3
4 1 1
10 1 3
Output
XXRR
Submitted Solution:
```
n,m,s,f=map(int,input().split())
p=s
d=-1
c='L'
if s<f:
d=1
c='R'
t=1
ts={}
ans=""
for _ in range(m):
x,y,z=map(int,input().split())
ts[x]=(y,z)
while(p!=f):
if t in ts:
(l,r)=ts[t]
if l<=p<=r:
ans+='X'
else:
p+=d
ans+=c
t+=1
print(ans)
``` | instruction | 0 | 69,296 | 14 | 138,592 |
No | output | 1 | 69,296 | 14 | 138,593 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n people taking dancing lessons. Every person is characterized by his/her dancing skill ai. At the beginning of the lesson they line up from left to right. While there is at least one couple of a boy and a girl in the line, the following process is repeated: the boy and girl who stand next to each other, having the minimal difference in dancing skills start to dance. If there are several such couples, the one first from the left starts to dance. After a couple leaves to dance, the line closes again, i.e. as a result the line is always continuous. The difference in dancing skills is understood as the absolute value of difference of ai variable. Your task is to find out what pairs and in what order will start dancing.
Input
The first line contains an integer n (1 β€ n β€ 2Β·105) β the number of people. The next line contains n symbols B or G without spaces. B stands for a boy, G stands for a girl. The third line contains n space-separated integers ai (1 β€ ai β€ 107) β the dancing skill. People are specified from left to right in the order in which they lined up.
Output
Print the resulting number of couples k. Then print k lines containing two numerals each β the numbers of people forming the couple. The people are numbered with integers from 1 to n from left to right. When a couple leaves to dance you shouldn't renumber the people. The numbers in one couple should be sorted in the increasing order. Print the couples in the order in which they leave to dance.
Examples
Input
4
BGBG
4 2 4 3
Output
2
3 4
1 2
Input
4
BBGG
4 6 1 5
Output
2
2 3
1 4
Input
4
BGBB
1 1 2 3
Output
1
1 2 | instruction | 0 | 69,345 | 14 | 138,690 |
Tags: data structures
Correct Solution:
```
import heapq, sys
q = []
cnt = 0
n = int(input())
sex = input()
a = list(map(int, input().split()))
l = [i-1 for i in range(n)]
r = [i+1 for i in range(n)]
for i in range(n-1):
if sex[i] != sex[i+1]:
heapq.heappush(q, (abs(a[i] - a[i+1]), i, i+1))
cnt += 1
ans = []
while cnt > 0:
t, i, j = heapq.heappop(q)
cnt -= 1
if r[i] == -1 or r[j] == -1:
continue
ans.append('%d %d' % (i+1, j+1))
u, v = l[i], r[j]
r[i] = r[j] = -1
if u >= 0:
r[u] = v
if v < n:
l[v] = u
if u >= 0 and v < n and sex[u] != sex[v]:
heapq.heappush(q, (abs(a[u] - a[v]), u, v))
cnt += 1
sys.stdout.write(str(len(ans)) + '\n' + '\n'.join(ans) + '\n')
``` | output | 1 | 69,345 | 14 | 138,691 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n people taking dancing lessons. Every person is characterized by his/her dancing skill ai. At the beginning of the lesson they line up from left to right. While there is at least one couple of a boy and a girl in the line, the following process is repeated: the boy and girl who stand next to each other, having the minimal difference in dancing skills start to dance. If there are several such couples, the one first from the left starts to dance. After a couple leaves to dance, the line closes again, i.e. as a result the line is always continuous. The difference in dancing skills is understood as the absolute value of difference of ai variable. Your task is to find out what pairs and in what order will start dancing.
Input
The first line contains an integer n (1 β€ n β€ 2Β·105) β the number of people. The next line contains n symbols B or G without spaces. B stands for a boy, G stands for a girl. The third line contains n space-separated integers ai (1 β€ ai β€ 107) β the dancing skill. People are specified from left to right in the order in which they lined up.
Output
Print the resulting number of couples k. Then print k lines containing two numerals each β the numbers of people forming the couple. The people are numbered with integers from 1 to n from left to right. When a couple leaves to dance you shouldn't renumber the people. The numbers in one couple should be sorted in the increasing order. Print the couples in the order in which they leave to dance.
Examples
Input
4
BGBG
4 2 4 3
Output
2
3 4
1 2
Input
4
BBGG
4 6 1 5
Output
2
2 3
1 4
Input
4
BGBB
1 1 2 3
Output
1
1 2 | instruction | 0 | 69,346 | 14 | 138,692 |
Tags: data structures
Correct Solution:
```
__author__ = 'Utena'
from heapq import heappushpop, heapify,heappop,heappush
n=int(input())
danced=[0]*(n+1)
gender=[0]+list(input())
skill=[0]+list(map(int,input().split()))
next=[0]+[i+1 for i in range(1,n)]+[0]
prev=[0]+[i for i in range(n)]
ans=[]
line=[]
total=0
for i in range(1,n):
x,y=i,i+1
if gender[x]==gender[y]:continue
else:
line.append((abs(skill[x]-skill[y]),(x,y)))
heapify(line)
while len(line)>0:
t0,temp=heappop(line)
x,y=temp[0],temp[1]
if danced[x]+danced[y]==0:
ans.append(str(x)+' '+str(y))
total+=1
danced[x]=1
danced[y]=1
if x>1 and y<n:
a=prev[x]
b=next[y]
prev[b]=a
next[a]=b
if gender[a]!=gender[b]:
heappush(line,(abs(skill[a]-skill[b]),(a,b)))
print(total)
print('\n'.join(ans))
``` | output | 1 | 69,346 | 14 | 138,693 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n people taking dancing lessons. Every person is characterized by his/her dancing skill ai. At the beginning of the lesson they line up from left to right. While there is at least one couple of a boy and a girl in the line, the following process is repeated: the boy and girl who stand next to each other, having the minimal difference in dancing skills start to dance. If there are several such couples, the one first from the left starts to dance. After a couple leaves to dance, the line closes again, i.e. as a result the line is always continuous. The difference in dancing skills is understood as the absolute value of difference of ai variable. Your task is to find out what pairs and in what order will start dancing.
Input
The first line contains an integer n (1 β€ n β€ 2Β·105) β the number of people. The next line contains n symbols B or G without spaces. B stands for a boy, G stands for a girl. The third line contains n space-separated integers ai (1 β€ ai β€ 107) β the dancing skill. People are specified from left to right in the order in which they lined up.
Output
Print the resulting number of couples k. Then print k lines containing two numerals each β the numbers of people forming the couple. The people are numbered with integers from 1 to n from left to right. When a couple leaves to dance you shouldn't renumber the people. The numbers in one couple should be sorted in the increasing order. Print the couples in the order in which they leave to dance.
Examples
Input
4
BGBG
4 2 4 3
Output
2
3 4
1 2
Input
4
BBGG
4 6 1 5
Output
2
2 3
1 4
Input
4
BGBB
1 1 2 3
Output
1
1 2 | instruction | 0 | 69,347 | 14 | 138,694 |
Tags: data structures
Correct Solution:
```
from heapq import heappushpop, heapify,heappop,heappush
n=int(input())
li = input()
a = list(map(int, input().split()))
h,seq = [],[]
dan = [0]*(n+1)
for i in range(n+1)[2:n]:
#dan[i]=(Node(li[i-1],i-1,i+1))
dan[i]=[i-1,i+1]
#dan[1]=(Node(li[0],None,2))
dan[1] = [None,2]
#dan[n]=(Node(li[n-1],n-1,None))
dan[n] = [n-1,None]
for i in range(n)[:-1]:
if li[i]!=li[i+1]:
h.append((abs(a[i+1]-a[i]),(i+1,i+2)))
#print(h)
#print(dan)
notthere=set()
#for i in range(n+1)[1:]:
# print(dan[i].data)
heapify(h)
while h:
# pop the first in h
d = heappop(h)
#print(1)
# check if it is actually a pair
if not(d[1] in notthere):
l,r = d[1][0],d[1][1]
le = dan[l][0];ri=dan[r][1]
L = R = False
if le != None:
if li[le-1] != li[l-1]:
notthere.add((le,l))
#print('aaa')
dan[le][1] = ri
L =True
if ri !=None:
if li[ri-1] != li[r-1]:
notthere.add((r,ri))
#print('bbb')
dan[ri][0] = le
R = True
if L and R:
if li[le-1] != li[ri-1]:
heappush(h,(abs(a[le-1]-a[ri-1]),(le,ri)))
#print(notthere)
seq.append(' '.join([str(l),str(r)]))
print(len(seq))
print('\n'.join(seq))
``` | output | 1 | 69,347 | 14 | 138,695 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n people taking dancing lessons. Every person is characterized by his/her dancing skill ai. At the beginning of the lesson they line up from left to right. While there is at least one couple of a boy and a girl in the line, the following process is repeated: the boy and girl who stand next to each other, having the minimal difference in dancing skills start to dance. If there are several such couples, the one first from the left starts to dance. After a couple leaves to dance, the line closes again, i.e. as a result the line is always continuous. The difference in dancing skills is understood as the absolute value of difference of ai variable. Your task is to find out what pairs and in what order will start dancing.
Input
The first line contains an integer n (1 β€ n β€ 2Β·105) β the number of people. The next line contains n symbols B or G without spaces. B stands for a boy, G stands for a girl. The third line contains n space-separated integers ai (1 β€ ai β€ 107) β the dancing skill. People are specified from left to right in the order in which they lined up.
Output
Print the resulting number of couples k. Then print k lines containing two numerals each β the numbers of people forming the couple. The people are numbered with integers from 1 to n from left to right. When a couple leaves to dance you shouldn't renumber the people. The numbers in one couple should be sorted in the increasing order. Print the couples in the order in which they leave to dance.
Examples
Input
4
BGBG
4 2 4 3
Output
2
3 4
1 2
Input
4
BBGG
4 6 1 5
Output
2
2 3
1 4
Input
4
BGBB
1 1 2 3
Output
1
1 2 | instruction | 0 | 69,348 | 14 | 138,696 |
Tags: data structures
Correct Solution:
```
#Again testing the condition of the server, code by Yijie Xia
import heapq as hq
n=int(input())
s=input()
a=list(map(int,input().split()))
pre=[i-1 for i in range(n)]
nxt=[i+1 for i in range(n)]
free=[1 for i in range(n)]
line=[(abs(a[i]-a[i+1]),i,i+1) for i in range(n-1) if s[i]!=s[i+1]]
hq.heapify(line)
ans=[]
while line:
t,ppre,pnxt=hq.heappop(line)
if free[ppre] and free[pnxt]:
ans.append(str(ppre+1)+' '+str(pnxt+1))
free[ppre],free[pnxt]=0,0
if ppre==0 or pnxt==n-1:
continue
preppre,nxtpnxt=pre[ppre],nxt[pnxt]
nxt[preppre],pre[nxtpnxt]=nxtpnxt,preppre
if s[preppre]!=s[nxtpnxt]:
hq.heappush(line,(abs(a[preppre]-a[nxtpnxt]),preppre,nxtpnxt))
print(len(ans))
print('\n'.join(ans))
``` | output | 1 | 69,348 | 14 | 138,697 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n people taking dancing lessons. Every person is characterized by his/her dancing skill ai. At the beginning of the lesson they line up from left to right. While there is at least one couple of a boy and a girl in the line, the following process is repeated: the boy and girl who stand next to each other, having the minimal difference in dancing skills start to dance. If there are several such couples, the one first from the left starts to dance. After a couple leaves to dance, the line closes again, i.e. as a result the line is always continuous. The difference in dancing skills is understood as the absolute value of difference of ai variable. Your task is to find out what pairs and in what order will start dancing.
Input
The first line contains an integer n (1 β€ n β€ 2Β·105) β the number of people. The next line contains n symbols B or G without spaces. B stands for a boy, G stands for a girl. The third line contains n space-separated integers ai (1 β€ ai β€ 107) β the dancing skill. People are specified from left to right in the order in which they lined up.
Output
Print the resulting number of couples k. Then print k lines containing two numerals each β the numbers of people forming the couple. The people are numbered with integers from 1 to n from left to right. When a couple leaves to dance you shouldn't renumber the people. The numbers in one couple should be sorted in the increasing order. Print the couples in the order in which they leave to dance.
Examples
Input
4
BGBG
4 2 4 3
Output
2
3 4
1 2
Input
4
BBGG
4 6 1 5
Output
2
2 3
1 4
Input
4
BGBB
1 1 2 3
Output
1
1 2 | instruction | 0 | 69,349 | 14 | 138,698 |
Tags: data structures
Correct Solution:
```
from heapq import heapify,heappush,heappop
n = int(input())
sex = input()
a = [int(i) for i in input().split()]
cp = []
r = []
b = sex.count('B')
N = min(n-b,b)
for i in range(1,len(a)):
if sex[i] != sex[i-1]:
cp.append((abs(a[i]-a[i-1]),i-1,i))
heapify(cp)
prev = [i for i in range(-1,len(sex))]
nxt = [i for i in range(1,len(sex)+2)]
trace = [0 for i in range(len(sex))]
cnt = 0
while cnt < N:
temp = heappop(cp)
x,y = temp[1],temp[2]
if trace[x] or trace[y]:
continue
r.append(str(x+1)+' '+str(y+1))
cnt += 1
trace[x],trace[y] = 1,1
if prev[x] != -1 and nxt[y] != len(a):
nxt[prev[x]] = nxt[y]
prev[nxt[y]] = prev[x]
if sex[prev[x]] != sex[nxt[y]]:
heappush(cp,(abs(a[prev[x]]-a[nxt[y]]),prev[x],nxt[y]))
print(len(r))
print('\n'.join(r))
``` | output | 1 | 69,349 | 14 | 138,699 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n people taking dancing lessons. Every person is characterized by his/her dancing skill ai. At the beginning of the lesson they line up from left to right. While there is at least one couple of a boy and a girl in the line, the following process is repeated: the boy and girl who stand next to each other, having the minimal difference in dancing skills start to dance. If there are several such couples, the one first from the left starts to dance. After a couple leaves to dance, the line closes again, i.e. as a result the line is always continuous. The difference in dancing skills is understood as the absolute value of difference of ai variable. Your task is to find out what pairs and in what order will start dancing.
Input
The first line contains an integer n (1 β€ n β€ 2Β·105) β the number of people. The next line contains n symbols B or G without spaces. B stands for a boy, G stands for a girl. The third line contains n space-separated integers ai (1 β€ ai β€ 107) β the dancing skill. People are specified from left to right in the order in which they lined up.
Output
Print the resulting number of couples k. Then print k lines containing two numerals each β the numbers of people forming the couple. The people are numbered with integers from 1 to n from left to right. When a couple leaves to dance you shouldn't renumber the people. The numbers in one couple should be sorted in the increasing order. Print the couples in the order in which they leave to dance.
Examples
Input
4
BGBG
4 2 4 3
Output
2
3 4
1 2
Input
4
BBGG
4 6 1 5
Output
2
2 3
1 4
Input
4
BGBB
1 1 2 3
Output
1
1 2 | instruction | 0 | 69,350 | 14 | 138,700 |
Tags: data structures
Correct Solution:
```
import heapq as hq
n=int(input())
s=input()
a=list(map(int,input().split()))
pre=[i-1 for i in range(n)]
nxt=[i+1 for i in range(n)]
free=[1 for i in range(n)]
line=[(abs(a[i]-a[i+1]),i,i+1) for i in range(n-1) if s[i]!=s[i+1]]
#print(line)
hq.heapify(line)
ans=[]
while line:
t,ppre,pnxt=hq.heappop(line)
if free[ppre] and free[pnxt]:
ans.append(str(ppre+1)+' '+str(pnxt+1))
free[ppre],free[pnxt]=0,0
if ppre==0 or pnxt==n-1:
continue
preppre,nxtpnxt=pre[ppre],nxt[pnxt]
nxt[preppre],pre[nxtpnxt]=nxtpnxt,preppre
if s[preppre]!=s[nxtpnxt]:
hq.heappush(line,(abs(a[preppre]-a[nxtpnxt]),preppre,nxtpnxt))
print(len(ans))
print('\n'.join(ans))
``` | output | 1 | 69,350 | 14 | 138,701 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n people taking dancing lessons. Every person is characterized by his/her dancing skill ai. At the beginning of the lesson they line up from left to right. While there is at least one couple of a boy and a girl in the line, the following process is repeated: the boy and girl who stand next to each other, having the minimal difference in dancing skills start to dance. If there are several such couples, the one first from the left starts to dance. After a couple leaves to dance, the line closes again, i.e. as a result the line is always continuous. The difference in dancing skills is understood as the absolute value of difference of ai variable. Your task is to find out what pairs and in what order will start dancing.
Input
The first line contains an integer n (1 β€ n β€ 2Β·105) β the number of people. The next line contains n symbols B or G without spaces. B stands for a boy, G stands for a girl. The third line contains n space-separated integers ai (1 β€ ai β€ 107) β the dancing skill. People are specified from left to right in the order in which they lined up.
Output
Print the resulting number of couples k. Then print k lines containing two numerals each β the numbers of people forming the couple. The people are numbered with integers from 1 to n from left to right. When a couple leaves to dance you shouldn't renumber the people. The numbers in one couple should be sorted in the increasing order. Print the couples in the order in which they leave to dance.
Examples
Input
4
BGBG
4 2 4 3
Output
2
3 4
1 2
Input
4
BBGG
4 6 1 5
Output
2
2 3
1 4
Input
4
BGBB
1 1 2 3
Output
1
1 2 | instruction | 0 | 69,351 | 14 | 138,702 |
Tags: data structures
Correct Solution:
```
import heapq,sys
q=[]
c=0
n=int(input())
s=input()
a=list(map(int,input().split()))
l=[i-1 for i in range(n)]
r=[i+1 for i in range(n)]
for i in range(n-1):
if s[i]!=s[i+1]:
heapq.heappush(q,(abs(a[i]-a[i+1]),i,i+1))
c+=1
ans=[]
while c>0:
t,i,j=heapq.heappop(q)
c-=1
if r[i]==-1 or r[j]==-1:
continue
ans.append('%d %d'%(i+1,j+1))
u,v=l[i],r[j]
r[i]=r[j]=-1
if u>=0: r[u]=v
if v<n: l[v]=u
if u>=0 and v<n and s[u]!=s[v]:
heapq.heappush(q,(abs(a[u]-a[v]),u,v))
c+=1
sys.stdout.write(str(len(ans))+'\n'+'\n'.join(ans)+'\n')
# Made By Mostafa_Khaled
``` | output | 1 | 69,351 | 14 | 138,703 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n people taking dancing lessons. Every person is characterized by his/her dancing skill ai. At the beginning of the lesson they line up from left to right. While there is at least one couple of a boy and a girl in the line, the following process is repeated: the boy and girl who stand next to each other, having the minimal difference in dancing skills start to dance. If there are several such couples, the one first from the left starts to dance. After a couple leaves to dance, the line closes again, i.e. as a result the line is always continuous. The difference in dancing skills is understood as the absolute value of difference of ai variable. Your task is to find out what pairs and in what order will start dancing.
Input
The first line contains an integer n (1 β€ n β€ 2Β·105) β the number of people. The next line contains n symbols B or G without spaces. B stands for a boy, G stands for a girl. The third line contains n space-separated integers ai (1 β€ ai β€ 107) β the dancing skill. People are specified from left to right in the order in which they lined up.
Output
Print the resulting number of couples k. Then print k lines containing two numerals each β the numbers of people forming the couple. The people are numbered with integers from 1 to n from left to right. When a couple leaves to dance you shouldn't renumber the people. The numbers in one couple should be sorted in the increasing order. Print the couples in the order in which they leave to dance.
Examples
Input
4
BGBG
4 2 4 3
Output
2
3 4
1 2
Input
4
BBGG
4 6 1 5
Output
2
2 3
1 4
Input
4
BGBB
1 1 2 3
Output
1
1 2 | instruction | 0 | 69,352 | 14 | 138,704 |
Tags: data structures
Correct Solution:
```
from heapq import heappush, heappop,heapify
n = int(input())
b = list(input())
a = [int(i)for i in input().split()]
c = []
d = [0]*n
e = []
ahead = [0]+[i for i in range(n)]
after = [i+1 for i in range(n)]+[0]
num = 0
for i in range(n-1):
x = i
y = i+1
if b[x]!=b[y]:
#print('b[i]!=b[i+1]')#
c.append((abs(a[x]-a[y]),x,y))
heapify(c)
while c:
#print('c is not NUll')#
skill, cp1 ,cp2 = heappop(c)
if d[cp1]+d[cp2] == 0:
d[cp1],d[cp2] = 1,1
#print(cp1,cp2)#
e.append(str(cp1+1)+" "+str(cp2+1))
num += 1
if cp1 > 0 and cp2 < n-1:#
x = ahead[cp1]
#print(x)
y = after[cp2]
#print(y)
ahead[y] = x
after[x] = y
if b[x] != b[y]:
heappush(c,(abs(a[x]-a[y]),x,y))
print(num)
print('\n'.join(e))
``` | output | 1 | 69,352 | 14 | 138,705 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n people taking dancing lessons. Every person is characterized by his/her dancing skill ai. At the beginning of the lesson they line up from left to right. While there is at least one couple of a boy and a girl in the line, the following process is repeated: the boy and girl who stand next to each other, having the minimal difference in dancing skills start to dance. If there are several such couples, the one first from the left starts to dance. After a couple leaves to dance, the line closes again, i.e. as a result the line is always continuous. The difference in dancing skills is understood as the absolute value of difference of ai variable. Your task is to find out what pairs and in what order will start dancing.
Input
The first line contains an integer n (1 β€ n β€ 2Β·105) β the number of people. The next line contains n symbols B or G without spaces. B stands for a boy, G stands for a girl. The third line contains n space-separated integers ai (1 β€ ai β€ 107) β the dancing skill. People are specified from left to right in the order in which they lined up.
Output
Print the resulting number of couples k. Then print k lines containing two numerals each β the numbers of people forming the couple. The people are numbered with integers from 1 to n from left to right. When a couple leaves to dance you shouldn't renumber the people. The numbers in one couple should be sorted in the increasing order. Print the couples in the order in which they leave to dance.
Examples
Input
4
BGBG
4 2 4 3
Output
2
3 4
1 2
Input
4
BBGG
4 6 1 5
Output
2
2 3
1 4
Input
4
BGBB
1 1 2 3
Output
1
1 2
Submitted Solution:
```
from heapq import heappush,heappop,heapify
n=int(input())
symbols=input()
skl=list(map(int,input().split()))
LMap=[i-1 for i in range(n+1)]
RMap=[i+1 for i in range(n+1)]
LMap[1],RMap[n]=1,n
h=[]
res=[]
cnt=0
B=symbols.count("B")
N=min(n-B,B)
ind=[True]*(n+1)
for i in range(n-1) :
if symbols[i]!=symbols[i+1] :
h.append((abs(skl[i]-skl[i+1]),i+1,i+2))
heapify(h)
i=0
while cnt<N :
d,L,R=heappop(h)
if ind[L] and ind[R] :
cnt+=1
ind[L],ind[R]=False,False
res.append(str(L)+" "+str(R))
L,R=LMap[L],RMap[R]
if L>=1 and R<=n :
RMap[L],LMap[R]=R,L
if symbols[L-1]!=symbols[R-1] :
heappush(h,(abs(skl[L-1]-skl[R-1]),L,R))
assert cnt==N
print(cnt)
for i in res :
print(i)
``` | instruction | 0 | 69,353 | 14 | 138,706 |
Yes | output | 1 | 69,353 | 14 | 138,707 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n people taking dancing lessons. Every person is characterized by his/her dancing skill ai. At the beginning of the lesson they line up from left to right. While there is at least one couple of a boy and a girl in the line, the following process is repeated: the boy and girl who stand next to each other, having the minimal difference in dancing skills start to dance. If there are several such couples, the one first from the left starts to dance. After a couple leaves to dance, the line closes again, i.e. as a result the line is always continuous. The difference in dancing skills is understood as the absolute value of difference of ai variable. Your task is to find out what pairs and in what order will start dancing.
Input
The first line contains an integer n (1 β€ n β€ 2Β·105) β the number of people. The next line contains n symbols B or G without spaces. B stands for a boy, G stands for a girl. The third line contains n space-separated integers ai (1 β€ ai β€ 107) β the dancing skill. People are specified from left to right in the order in which they lined up.
Output
Print the resulting number of couples k. Then print k lines containing two numerals each β the numbers of people forming the couple. The people are numbered with integers from 1 to n from left to right. When a couple leaves to dance you shouldn't renumber the people. The numbers in one couple should be sorted in the increasing order. Print the couples in the order in which they leave to dance.
Examples
Input
4
BGBG
4 2 4 3
Output
2
3 4
1 2
Input
4
BBGG
4 6 1 5
Output
2
2 3
1 4
Input
4
BGBB
1 1 2 3
Output
1
1 2
Submitted Solution:
```
from heapq import heappush,heappop,heapify
n=int(input())
symbols=input()
skl=list(map(int,input().split()))
LMap=[i-1 for i in range(n+1)]
RMap=[i+1 for i in range(n+1)]
LMap[1],RMap[n]=1,n
h=[]
res=[]
cnt=0
B=symbols.count("B")
N=min(n-B,B)
ind=[True]*(n+1)
h=[]
for i in range(n-1) :
if symbols[i]!=symbols[i+1] :
h.append((abs(skl[i]-skl[i+1]),i+1,i+2))
heapify(h)
i=0
while cnt<N :
d,L,R=heappop(h)
if ind[L] and ind[R] :
cnt+=1
ind[L],ind[R]=False,False
res.append(str(L)+" "+str(R))
if L==1 or R==n :
continue
L,R=LMap[L],RMap[R]
RMap[L],LMap[R]=R,L
if symbols[L-1]!=symbols[R-1] :
heappush(h,(abs(skl[L-1]-skl[R-1]),L,R))
assert cnt==N
print(cnt)
for i in res :
print(i)
``` | instruction | 0 | 69,354 | 14 | 138,708 |
Yes | output | 1 | 69,354 | 14 | 138,709 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n people taking dancing lessons. Every person is characterized by his/her dancing skill ai. At the beginning of the lesson they line up from left to right. While there is at least one couple of a boy and a girl in the line, the following process is repeated: the boy and girl who stand next to each other, having the minimal difference in dancing skills start to dance. If there are several such couples, the one first from the left starts to dance. After a couple leaves to dance, the line closes again, i.e. as a result the line is always continuous. The difference in dancing skills is understood as the absolute value of difference of ai variable. Your task is to find out what pairs and in what order will start dancing.
Input
The first line contains an integer n (1 β€ n β€ 2Β·105) β the number of people. The next line contains n symbols B or G without spaces. B stands for a boy, G stands for a girl. The third line contains n space-separated integers ai (1 β€ ai β€ 107) β the dancing skill. People are specified from left to right in the order in which they lined up.
Output
Print the resulting number of couples k. Then print k lines containing two numerals each β the numbers of people forming the couple. The people are numbered with integers from 1 to n from left to right. When a couple leaves to dance you shouldn't renumber the people. The numbers in one couple should be sorted in the increasing order. Print the couples in the order in which they leave to dance.
Examples
Input
4
BGBG
4 2 4 3
Output
2
3 4
1 2
Input
4
BBGG
4 6 1 5
Output
2
2 3
1 4
Input
4
BGBB
1 1 2 3
Output
1
1 2
Submitted Solution:
```
import heapq
n = int(input())
line = str(input())
a = [int(i) for i in input().split()]
diff = []
couples = []
left = [i-1 for i in range(n)]
right = [i+1 for i in range(n)]
for i in range(n-1):
if line[i] != line[i+1]:
heapq.heappush(diff,(abs(a[i]-a[i+1]),i,i+1))
while len(diff)>0:
d,l,r = heapq.heappop(diff)
if left[l] != None and right[r] != None:
couples.append(str(l+1)+' '+str(r+1))
left[r] = None
right[l] = None
new_l = left[l]
new_r = right[r]
if new_l != -1 and new_r != n:
left[new_r] = new_l
right[new_l] = new_r
if line[new_l] != line[new_r]:
heapq.heappush(diff,(abs(a[new_l]-a[new_r]),new_l,new_r))
print(len(couples))
print('\n' .join(couples))
``` | instruction | 0 | 69,355 | 14 | 138,710 |
Yes | output | 1 | 69,355 | 14 | 138,711 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n people taking dancing lessons. Every person is characterized by his/her dancing skill ai. At the beginning of the lesson they line up from left to right. While there is at least one couple of a boy and a girl in the line, the following process is repeated: the boy and girl who stand next to each other, having the minimal difference in dancing skills start to dance. If there are several such couples, the one first from the left starts to dance. After a couple leaves to dance, the line closes again, i.e. as a result the line is always continuous. The difference in dancing skills is understood as the absolute value of difference of ai variable. Your task is to find out what pairs and in what order will start dancing.
Input
The first line contains an integer n (1 β€ n β€ 2Β·105) β the number of people. The next line contains n symbols B or G without spaces. B stands for a boy, G stands for a girl. The third line contains n space-separated integers ai (1 β€ ai β€ 107) β the dancing skill. People are specified from left to right in the order in which they lined up.
Output
Print the resulting number of couples k. Then print k lines containing two numerals each β the numbers of people forming the couple. The people are numbered with integers from 1 to n from left to right. When a couple leaves to dance you shouldn't renumber the people. The numbers in one couple should be sorted in the increasing order. Print the couples in the order in which they leave to dance.
Examples
Input
4
BGBG
4 2 4 3
Output
2
3 4
1 2
Input
4
BBGG
4 6 1 5
Output
2
2 3
1 4
Input
4
BGBB
1 1 2 3
Output
1
1 2
Submitted Solution:
```
from heapq import heappush,heappop,heapify
n=int(input())
symbols=input()
skl=list(map(int,input().split()))
LMap=[i-1 for i in range(n+1)]
RMap=[i+1 for i in range(n+1)]
LMap[1],RMap[n]=1,n
h=[]
res=[]
cnt=0
B=symbols.count("B")
N=min(n-B,B)
ind=[True]*(n+1)
for i in range(n-1) :
if symbols[i]!=symbols[i+1] :
h.append((abs(skl[i]-skl[i+1]),i+1,i+2))
heapify(h)
i=0
while cnt<N :
d,L,R=heappop(h)
if ind[L] and ind[R] :
cnt+=1
ind[L],ind[R]=False,False
res.append(str(L)+" "+str(R))
if L==1 or R==n :
continue
L,R=LMap[L],RMap[R]
RMap[L],LMap[R]=R,L
if symbols[L-1]!=symbols[R-1] :
heappush(h,(abs(skl[L-1]-skl[R-1]),L,R))
assert cnt==N
print(cnt)
for i in res :
print(i)
``` | instruction | 0 | 69,356 | 14 | 138,712 |
Yes | output | 1 | 69,356 | 14 | 138,713 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n people taking dancing lessons. Every person is characterized by his/her dancing skill ai. At the beginning of the lesson they line up from left to right. While there is at least one couple of a boy and a girl in the line, the following process is repeated: the boy and girl who stand next to each other, having the minimal difference in dancing skills start to dance. If there are several such couples, the one first from the left starts to dance. After a couple leaves to dance, the line closes again, i.e. as a result the line is always continuous. The difference in dancing skills is understood as the absolute value of difference of ai variable. Your task is to find out what pairs and in what order will start dancing.
Input
The first line contains an integer n (1 β€ n β€ 2Β·105) β the number of people. The next line contains n symbols B or G without spaces. B stands for a boy, G stands for a girl. The third line contains n space-separated integers ai (1 β€ ai β€ 107) β the dancing skill. People are specified from left to right in the order in which they lined up.
Output
Print the resulting number of couples k. Then print k lines containing two numerals each β the numbers of people forming the couple. The people are numbered with integers from 1 to n from left to right. When a couple leaves to dance you shouldn't renumber the people. The numbers in one couple should be sorted in the increasing order. Print the couples in the order in which they leave to dance.
Examples
Input
4
BGBG
4 2 4 3
Output
2
3 4
1 2
Input
4
BBGG
4 6 1 5
Output
2
2 3
1 4
Input
4
BGBB
1 1 2 3
Output
1
1 2
Submitted Solution:
```
n = int(input())
gender = [char for char in input()]
a = [int(i) for i in input().split()]
line = [gender[0]]
difference = []
couple = []
for i in range(1,n):
line.append(gender[i])
if line[i] != line[i - 1]:
for j in range(len(difference)):
if abs(a[i] - a[i - 1]) >= difference[j][0] and i - 1 == difference[j][2]: continue
elif abs(a[i] - a[i - 1]) >= difference[j][0]:
difference.insert(j,(abs(a[i] - a[i - 1]),i - 1,i))
break
if j == len(difference) - 1:
difference.append((abs(a[i] - a[i - 1]),i - 1,i))
if not len(difference):
difference.append((abs(a[i] - a[i - 1]),i - 1,i))
print(difference)
while len(difference):
dif,fir,sec = difference.pop()
couple.append((fir + 1,sec + 1))
if not (fir == 0 or sec == n - 1):
if line[fir - 1] != line[sec + 1]:
for j in range(len(difference)):
if abs(a[fir - 1] - a[sec + 1]) >= difference[j][0] and fir - 1 == difference[j][2]: continue
elif abs(a[fir - 1] - a[sec + 1]) <= difference[j][0] and sec + 1 == difference[j][2]:
difference.insert(j,(abs(a[fir - 1] - a[sec + 1]),fir - 1,sec + 1))
del(difference[j + 1])
break
elif abs(a[i] - a[i - 1]) >= difference[j][0]:
difference.insert(j,(abs(a[fir - 1] - a[sec + 1]),fir - 1,sec + 1))
break
if j == len(difference) - 1:
difference.append((abs(a[fir - 1] - a[sec + 1]),fir - 1,sec + 1))
if not len(difference):
difference.append((abs(a[fir - 1] - a[sec + 1]),fir - 1,sec + 1))
print(difference)
print(len(couple))
for i in range(len(couple)):
print(*couple[i])
``` | instruction | 0 | 69,357 | 14 | 138,714 |
No | output | 1 | 69,357 | 14 | 138,715 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n people taking dancing lessons. Every person is characterized by his/her dancing skill ai. At the beginning of the lesson they line up from left to right. While there is at least one couple of a boy and a girl in the line, the following process is repeated: the boy and girl who stand next to each other, having the minimal difference in dancing skills start to dance. If there are several such couples, the one first from the left starts to dance. After a couple leaves to dance, the line closes again, i.e. as a result the line is always continuous. The difference in dancing skills is understood as the absolute value of difference of ai variable. Your task is to find out what pairs and in what order will start dancing.
Input
The first line contains an integer n (1 β€ n β€ 2Β·105) β the number of people. The next line contains n symbols B or G without spaces. B stands for a boy, G stands for a girl. The third line contains n space-separated integers ai (1 β€ ai β€ 107) β the dancing skill. People are specified from left to right in the order in which they lined up.
Output
Print the resulting number of couples k. Then print k lines containing two numerals each β the numbers of people forming the couple. The people are numbered with integers from 1 to n from left to right. When a couple leaves to dance you shouldn't renumber the people. The numbers in one couple should be sorted in the increasing order. Print the couples in the order in which they leave to dance.
Examples
Input
4
BGBG
4 2 4 3
Output
2
3 4
1 2
Input
4
BBGG
4 6 1 5
Output
2
2 3
1 4
Input
4
BGBB
1 1 2 3
Output
1
1 2
Submitted Solution:
```
import heapq
n=int(input())
g=input()
a=[int(i) for i in input().split()]
h=[]
heapq.heapify(h)
prev=[i-1 for i in range(0,n)]
next=[i+1 for i in range(0,n)]
d=[0]*n;
r1=[]
r2=[]
r=[]
for i in range(0,n-1):
if(g[i]!=g[i+1]):
heapq.heappush(h,(abs(a[i]-a[i+1]),i,i+1))
while h :
aa,ii,jj=heapq.heappop(h)
if(d[ii]==0) and (d[jj]==0):
r1.append(ii+1)
r2.append(jj+1)
d[ii]=1;
d[jj]=1;
pp=prev[ii];
nn=next[jj];
if(pp>=0) and (nn<n) and (g[nn]!=g[pp]):
next[pp]=nn;
prev[nn]=pp;
heapq.heappush(h,(abs(a[nn]-a[pp]),pp,nn))
print(len(r1));
for i in range(len(r1)):
r.append(str(r1[i])+' '+str(r2[i]));
print(*r,sep='\n');
``` | instruction | 0 | 69,358 | 14 | 138,716 |
No | output | 1 | 69,358 | 14 | 138,717 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n people taking dancing lessons. Every person is characterized by his/her dancing skill ai. At the beginning of the lesson they line up from left to right. While there is at least one couple of a boy and a girl in the line, the following process is repeated: the boy and girl who stand next to each other, having the minimal difference in dancing skills start to dance. If there are several such couples, the one first from the left starts to dance. After a couple leaves to dance, the line closes again, i.e. as a result the line is always continuous. The difference in dancing skills is understood as the absolute value of difference of ai variable. Your task is to find out what pairs and in what order will start dancing.
Input
The first line contains an integer n (1 β€ n β€ 2Β·105) β the number of people. The next line contains n symbols B or G without spaces. B stands for a boy, G stands for a girl. The third line contains n space-separated integers ai (1 β€ ai β€ 107) β the dancing skill. People are specified from left to right in the order in which they lined up.
Output
Print the resulting number of couples k. Then print k lines containing two numerals each β the numbers of people forming the couple. The people are numbered with integers from 1 to n from left to right. When a couple leaves to dance you shouldn't renumber the people. The numbers in one couple should be sorted in the increasing order. Print the couples in the order in which they leave to dance.
Examples
Input
4
BGBG
4 2 4 3
Output
2
3 4
1 2
Input
4
BBGG
4 6 1 5
Output
2
2 3
1 4
Input
4
BGBB
1 1 2 3
Output
1
1 2
Submitted Solution:
```
n = int(input())
sex = input()
a = [int(i) for i in input().split()]
cp = []
r = []
temp = []
for i in range(1,len(a)):
if sex[i] != sex[i-1]:
cp.append((i-1,i,abs(a[i]-a[i-1])))
cp.sort(key = lambda x:(-x[2],-x[0]))
prev = [i for i in range(-1,len(sex)-1)]+[None]
nxt = [i for i in range(1,len(sex)+1)]+[None]
while len(cp) != 0:
r.append(cp.pop())
x,y = r[-1][0],r[-1][1]
if x in temp or y in temp:
r.pop()
continue
temp.append(x)
temp.append(y)
nxt[prev[x]] = nxt[y]
prev[nxt[y]] = prev[x]
if prev[x] != -1 and nxt[y] != len(a):
if sex[prev[x]] != sex[nxt[y]]:
cp.append((prev[x],nxt[y],abs(a[prev[x]]-a[nxt[y]])))
print(cp)
for i in range(len(cp)-1,0,-1):
if cp[i][2] > cp[i-1][2] or (cp[i][2] == cp[i-1][2] and cp[i][0] > cp[i-1][0]):
cp[i],cp[i-1] = cp[i-1],cp[i]
else:
break
print(len(r))
for i in r:
print(i[0]+1,i[1]+1)
``` | instruction | 0 | 69,359 | 14 | 138,718 |
No | output | 1 | 69,359 | 14 | 138,719 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n people taking dancing lessons. Every person is characterized by his/her dancing skill ai. At the beginning of the lesson they line up from left to right. While there is at least one couple of a boy and a girl in the line, the following process is repeated: the boy and girl who stand next to each other, having the minimal difference in dancing skills start to dance. If there are several such couples, the one first from the left starts to dance. After a couple leaves to dance, the line closes again, i.e. as a result the line is always continuous. The difference in dancing skills is understood as the absolute value of difference of ai variable. Your task is to find out what pairs and in what order will start dancing.
Input
The first line contains an integer n (1 β€ n β€ 2Β·105) β the number of people. The next line contains n symbols B or G without spaces. B stands for a boy, G stands for a girl. The third line contains n space-separated integers ai (1 β€ ai β€ 107) β the dancing skill. People are specified from left to right in the order in which they lined up.
Output
Print the resulting number of couples k. Then print k lines containing two numerals each β the numbers of people forming the couple. The people are numbered with integers from 1 to n from left to right. When a couple leaves to dance you shouldn't renumber the people. The numbers in one couple should be sorted in the increasing order. Print the couples in the order in which they leave to dance.
Examples
Input
4
BGBG
4 2 4 3
Output
2
3 4
1 2
Input
4
BBGG
4 6 1 5
Output
2
2 3
1 4
Input
4
BGBB
1 1 2 3
Output
1
1 2
Submitted Solution:
```
n = int(input())
s = input()
a = list(map(int,input().split()))
tog = []
sep = []
i=0
b = []
g = []
itr = ''
while(i<n):
if(i+1<n and s[i]=='B' and s[i+1] =='G'):
tog.append([abs(a[i]-a[i+1]), [i+1, i+2]])
i+=1
itr+='t'
elif(i+1<n and s[i] == 'G' and s[i+1] == 'B'):
tog.append([abs(a[i]-a[i+1]), [i+1,i+2]])
i+=1
itr+='t'
elif(i+1<n and s[i]=='B' and s[i+1] == 'B'):
if(len(g)):
sep.append([abs(a[i]-g[-1][0]), [min(i,g[-1][1])+1, max(i, g[-1][1])+1]])
itr+='s'
else:
b.append([a[i], i])
elif(i+1<n and s[i] == 'G' and s[i+1] =='G'):
if(len(b)):
sep.append([abs(a[i]-b[-1][0]), [min(i,b[-1][1])+1, max(i, b[-1][1])+1]])
itr+='s'
else:
g.append([a[i], i])
if(i==n-1):
if(i>0 and s[i] == 'G' and s[i-1] != 'B'):
if(len(b)):
sep.append([abs(a[i]-b[-1][0]), [min(i,b[-1][1])+1, max(i, b[-1][1])+1]])
itr+='s'
elif(i>0 and s[i]=='B' and s[i-1]!='G'):
if(len(g)):
sep.append([abs(a[i]-g[-1][0]), [min(i,g[-1][1])+1, max(i, g[-1][1])+1]])
itr+='s'
i+=1
tog.sort()
sep.sort()
print(len(tog)+len(sep))
#for i in range(len(itr)):
# if(itr[i]=='t'):
# print(*tog[0][1])
# del tog[0]
# elif(itr[i] == 's'):
# print(*sep[0][1])
# del sep[0]
for i in range(len(tog)):
print(*tog[i][1])
for i in range(len(sep)):
print(*sep[i][1])
``` | instruction | 0 | 69,360 | 14 | 138,720 |
No | output | 1 | 69,360 | 14 | 138,721 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:
<image>
In the above formula, 1 β€ l < r β€ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a.
Input
The first line contains single integer n (2 β€ n β€ 105) β the size of the array a.
The second line contains n integers a1, a2, ..., an (-109 β€ ai β€ 109) β the array elements.
Output
Print the only integer β the maximum value of f.
Examples
Input
5
1 4 2 3 1
Output
3
Input
4
1 5 4 7
Output
6
Note
In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5].
In the second case maximal value of f is reachable only on the whole array. | instruction | 0 | 69,491 | 14 | 138,982 |
Tags: dp, two pointers
Correct Solution:
```
def main():
n = int(input())
a = list(map(int, input().split()))
b = [abs(a[i] - a[i + 1]) for i in range(len(a) - 1)]
ansmx = [0] * len(b)
ansmn = [0] * len(b)
ansmx[-1] = b[-1]
ansmn[-1] = 0
for i in range(1, len(b)):
j = i - 1
ansmx[-1 - i] = b[-1 - i] - ansmn[-i]
ansmn[-1 - i] = min(b[-1 - i] - ansmx[-i], 0)
print(max(ansmx))
if __name__ == "__main__":
main()
``` | output | 1 | 69,491 | 14 | 138,983 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:
<image>
In the above formula, 1 β€ l < r β€ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a.
Input
The first line contains single integer n (2 β€ n β€ 105) β the size of the array a.
The second line contains n integers a1, a2, ..., an (-109 β€ ai β€ 109) β the array elements.
Output
Print the only integer β the maximum value of f.
Examples
Input
5
1 4 2 3 1
Output
3
Input
4
1 5 4 7
Output
6
Note
In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5].
In the second case maximal value of f is reachable only on the whole array. | instruction | 0 | 69,492 | 14 | 138,984 |
Tags: dp, two pointers
Correct Solution:
```
n=int(input())
a=list(map(int,input().split()))
ans=-10**9-1
mini=0
temp=0
for i in range(n-1):
if(i%2==0):temp+=abs(a[i]-a[i+1])
else:temp-=abs(a[i]-a[i+1])
mini=min(mini,temp)
ans=max(ans,temp-mini)
mini=0
temp=0
for i in range(1,n-1):
if(i%2==0):temp-=abs(a[i]-a[i+1])
else:temp+=abs(a[i]-a[i+1])
mini=min(mini,temp)
ans=max(ans,temp-mini)
print(ans)
``` | output | 1 | 69,492 | 14 | 138,985 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:
<image>
In the above formula, 1 β€ l < r β€ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a.
Input
The first line contains single integer n (2 β€ n β€ 105) β the size of the array a.
The second line contains n integers a1, a2, ..., an (-109 β€ ai β€ 109) β the array elements.
Output
Print the only integer β the maximum value of f.
Examples
Input
5
1 4 2 3 1
Output
3
Input
4
1 5 4 7
Output
6
Note
In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5].
In the second case maximal value of f is reachable only on the whole array. | instruction | 0 | 69,493 | 14 | 138,986 |
Tags: dp, two pointers
Correct Solution:
```
from sys import stdin
input=lambda : stdin.readline().strip()
from math import ceil,sqrt,factorial,gcd
from collections import deque
from bisect import bisect_left,bisect_right
n=int(input())
l=list(map(int,input().split()))
z=[]
for i in range(n-1):
z.append(abs(l[i]-l[i+1]))
ans=0
a,b=0,0
for i in z:
a,b=max(b+i,0),max(a-i,0)
ans=max(ans,a+b)
print(ans)
``` | output | 1 | 69,493 | 14 | 138,987 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:
<image>
In the above formula, 1 β€ l < r β€ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a.
Input
The first line contains single integer n (2 β€ n β€ 105) β the size of the array a.
The second line contains n integers a1, a2, ..., an (-109 β€ ai β€ 109) β the array elements.
Output
Print the only integer β the maximum value of f.
Examples
Input
5
1 4 2 3 1
Output
3
Input
4
1 5 4 7
Output
6
Note
In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5].
In the second case maximal value of f is reachable only on the whole array. | instruction | 0 | 69,495 | 14 | 138,990 |
Tags: dp, two pointers
Correct Solution:
```
def mp(): return map(int,input().split())
def lt(): return list(map(int,input().split()))
def pt(x): print(x)
def ip(): return input()
def it(): return int(input())
def sl(x): return [t for t in x]
def spl(x): return x.split()
def aj(liste, item): liste.append(item)
def bin(x): return "{0:b}".format(x)
def listring(l): return ' '.join([str(x) for x in l])
def ptlist(l): print(' '.join([str(x) for x in l]))
n = it()
a = lt()
L = [abs(a[i]-a[i+1]) for i in range(n-1)]
ans = [0 for _ in range(n)]
ans[0] = L[0]
for i in range(1,n-1):
ans[i] = max(L[i],L[i]-ans[i-1])
if i-2 >= 0:
ans[i] = max(ans[i],L[i]-L[i-1]+ans[i-2])
print(max(ans))
``` | output | 1 | 69,495 | 14 | 138,991 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:
<image>
In the above formula, 1 β€ l < r β€ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a.
Input
The first line contains single integer n (2 β€ n β€ 105) β the size of the array a.
The second line contains n integers a1, a2, ..., an (-109 β€ ai β€ 109) β the array elements.
Output
Print the only integer β the maximum value of f.
Examples
Input
5
1 4 2 3 1
Output
3
Input
4
1 5 4 7
Output
6
Note
In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5].
In the second case maximal value of f is reachable only on the whole array. | instruction | 0 | 69,496 | 14 | 138,992 |
Tags: dp, two pointers
Correct Solution:
```
from collections import defaultdict
from sys import stdin
def fun(ar):
if(len(ar)) < 2:
return 0
ans, cur = 0, 0
for i in range(len(ar)-1):
if (i & 1) == 0 and cur < 0:
cur = 0
cur += abs(ar[i+1]-ar[i])*(-1 if i & 1 else 1)
ans = max(ans, cur)
return ans
def main():
n = int(stdin.readline())
ar = list(map(int, stdin.readline().split()))
print(max(fun(ar), fun(ar[1:])))
if __name__ == "__main__":
main()
``` | output | 1 | 69,496 | 14 | 138,993 |
Provide a correct Python 3 solution for this coding contest problem.
There are N people (with different names) and K clubs. For each club you know the list of members (so you have K unordered lists). Each person can be a member of many clubs (and also 0 clubs) and two different clubs might have exactly the same members. The value of the number K is the minimum possible such that the following property can be true for at least one configuration of clubs. If every person changes her name (maintaining the property that all names are different) and you know the new K lists of members of the clubs (but you don't know which list corresponds to which club), you are sure that you would be able to match the old names with the new ones.
Count the number of such configurations of clubs (or say that there are more than 1000). Two configurations of clubs that can be obtained one from the other if the N people change their names are considered equal.
Formal statement: A club is a (possibly empty) subset of \\{1,\dots, N\\} (the subset identifies the members, assuming that the people are indexed by the numbers 1, 2,\dots, N). A configuration of clubs is an unordered collection of K clubs (not necessarily distinct). Given a permutation \sigma(1), \dots, \sigma(N) of \\{1,\dots, N\\} and a configuration of clubs L=\\{C_1, C_2, \dots, C_K\\}, we denote with \sigma(L) the configuration of clubs \\{\sigma(C_1), \sigma(C_2), \dots, \sigma(C_K)\\} (if C is a club, \sigma(C)=\\{\sigma(x):\, x\in C\\}). A configuration of clubs L is name-preserving if for any pair of distinct permutations \sigma\not=\tau, it holds \sigma(L)\not=\tau(L).
You have to count the number of name-preserving configurations of clubs with the minimum possible number of clubs (so K is minimum). Two configurations L_1, L_2 such that L_2=\sigma(L_1) (for some permutation \sigma) are considered equal. If there are more than 1000 such configurations, print -1.
Constraints
* 2 \le N \le 2\cdot10^{18}
Input
The input is given from Standard Input in the format
N
Output
If ans is the number of configurations with the properties described in the statement, you should print on Standard Output
ans
If there are more than 1000 such configurations, print -1.
Examples
Input
2
Output
1
Input
3
Output
1
Input
4
Output
7
Input
13
Output
6
Input
26
Output
-1
Input
123456789123456789
Output
-1 | instruction | 0 | 69,592 | 14 | 139,184 |
"Correct Solution:
```
#
# File : F.py
# Author : Kazune Takahashi
# Created : 5/24/2020, 3:07:25 AM
# Powered by Visual Studio Code
#
dic = {
1: 1,
2: 1,
3: 1,
4: 7,
5: 4,
6: 1,
7: 336,
8: 384,
9: 334,
10: 220,
11: 108,
12: 36,
13: 6,
27: 976,
28: 108,
29: 4,
60: 220,
61: 1,
124: 334,
252: 384,
508: 334,
1020: 220,
2044: 108,
4092: 36,
8188: 6,
134217723: 976,
268435451: 108,
536870907: 4,
1152921504606846970: 220,
2305843009213693946: 1,
}
n = int(input())
if n in dic:
print(dic[n])
else:
print(-1)
``` | output | 1 | 69,592 | 14 | 139,185 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N people (with different names) and K clubs. For each club you know the list of members (so you have K unordered lists). Each person can be a member of many clubs (and also 0 clubs) and two different clubs might have exactly the same members. The value of the number K is the minimum possible such that the following property can be true for at least one configuration of clubs. If every person changes her name (maintaining the property that all names are different) and you know the new K lists of members of the clubs (but you don't know which list corresponds to which club), you are sure that you would be able to match the old names with the new ones.
Count the number of such configurations of clubs (or say that there are more than 1000). Two configurations of clubs that can be obtained one from the other if the N people change their names are considered equal.
Formal statement: A club is a (possibly empty) subset of \\{1,\dots, N\\} (the subset identifies the members, assuming that the people are indexed by the numbers 1, 2,\dots, N). A configuration of clubs is an unordered collection of K clubs (not necessarily distinct). Given a permutation \sigma(1), \dots, \sigma(N) of \\{1,\dots, N\\} and a configuration of clubs L=\\{C_1, C_2, \dots, C_K\\}, we denote with \sigma(L) the configuration of clubs \\{\sigma(C_1), \sigma(C_2), \dots, \sigma(C_K)\\} (if C is a club, \sigma(C)=\\{\sigma(x):\, x\in C\\}). A configuration of clubs L is name-preserving if for any pair of distinct permutations \sigma\not=\tau, it holds \sigma(L)\not=\tau(L).
You have to count the number of name-preserving configurations of clubs with the minimum possible number of clubs (so K is minimum). Two configurations L_1, L_2 such that L_2=\sigma(L_1) (for some permutation \sigma) are considered equal. If there are more than 1000 such configurations, print -1.
Constraints
* 2 \le N \le 2\cdot10^{18}
Input
The input is given from Standard Input in the format
N
Output
If ans is the number of configurations with the properties described in the statement, you should print on Standard Output
ans
If there are more than 1000 such configurations, print -1.
Examples
Input
2
Output
1
Input
3
Output
1
Input
4
Output
7
Input
13
Output
6
Input
26
Output
-1
Input
123456789123456789
Output
-1
Submitted Solution:
```
n = int(input())
if(n==2):
print(1)
elif(n==3):
print(1)
elif(n==4):
print(7)
elif(n==13):
print(6)
elif(n==26):
print(-1)
else:
print(1)
``` | instruction | 0 | 69,593 | 14 | 139,186 |
No | output | 1 | 69,593 | 14 | 139,187 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N people (with different names) and K clubs. For each club you know the list of members (so you have K unordered lists). Each person can be a member of many clubs (and also 0 clubs) and two different clubs might have exactly the same members. The value of the number K is the minimum possible such that the following property can be true for at least one configuration of clubs. If every person changes her name (maintaining the property that all names are different) and you know the new K lists of members of the clubs (but you don't know which list corresponds to which club), you are sure that you would be able to match the old names with the new ones.
Count the number of such configurations of clubs (or say that there are more than 1000). Two configurations of clubs that can be obtained one from the other if the N people change their names are considered equal.
Formal statement: A club is a (possibly empty) subset of \\{1,\dots, N\\} (the subset identifies the members, assuming that the people are indexed by the numbers 1, 2,\dots, N). A configuration of clubs is an unordered collection of K clubs (not necessarily distinct). Given a permutation \sigma(1), \dots, \sigma(N) of \\{1,\dots, N\\} and a configuration of clubs L=\\{C_1, C_2, \dots, C_K\\}, we denote with \sigma(L) the configuration of clubs \\{\sigma(C_1), \sigma(C_2), \dots, \sigma(C_K)\\} (if C is a club, \sigma(C)=\\{\sigma(x):\, x\in C\\}). A configuration of clubs L is name-preserving if for any pair of distinct permutations \sigma\not=\tau, it holds \sigma(L)\not=\tau(L).
You have to count the number of name-preserving configurations of clubs with the minimum possible number of clubs (so K is minimum). Two configurations L_1, L_2 such that L_2=\sigma(L_1) (for some permutation \sigma) are considered equal. If there are more than 1000 such configurations, print -1.
Constraints
* 2 \le N \le 2\cdot10^{18}
Input
The input is given from Standard Input in the format
N
Output
If ans is the number of configurations with the properties described in the statement, you should print on Standard Output
ans
If there are more than 1000 such configurations, print -1.
Examples
Input
2
Output
1
Input
3
Output
1
Input
4
Output
7
Input
13
Output
6
Input
26
Output
-1
Input
123456789123456789
Output
-1
Submitted Solution:
```
print('Amethyst')
``` | instruction | 0 | 69,594 | 14 | 139,188 |
No | output | 1 | 69,594 | 14 | 139,189 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N people (with different names) and K clubs. For each club you know the list of members (so you have K unordered lists). Each person can be a member of many clubs (and also 0 clubs) and two different clubs might have exactly the same members. The value of the number K is the minimum possible such that the following property can be true for at least one configuration of clubs. If every person changes her name (maintaining the property that all names are different) and you know the new K lists of members of the clubs (but you don't know which list corresponds to which club), you are sure that you would be able to match the old names with the new ones.
Count the number of such configurations of clubs (or say that there are more than 1000). Two configurations of clubs that can be obtained one from the other if the N people change their names are considered equal.
Formal statement: A club is a (possibly empty) subset of \\{1,\dots, N\\} (the subset identifies the members, assuming that the people are indexed by the numbers 1, 2,\dots, N). A configuration of clubs is an unordered collection of K clubs (not necessarily distinct). Given a permutation \sigma(1), \dots, \sigma(N) of \\{1,\dots, N\\} and a configuration of clubs L=\\{C_1, C_2, \dots, C_K\\}, we denote with \sigma(L) the configuration of clubs \\{\sigma(C_1), \sigma(C_2), \dots, \sigma(C_K)\\} (if C is a club, \sigma(C)=\\{\sigma(x):\, x\in C\\}). A configuration of clubs L is name-preserving if for any pair of distinct permutations \sigma\not=\tau, it holds \sigma(L)\not=\tau(L).
You have to count the number of name-preserving configurations of clubs with the minimum possible number of clubs (so K is minimum). Two configurations L_1, L_2 such that L_2=\sigma(L_1) (for some permutation \sigma) are considered equal. If there are more than 1000 such configurations, print -1.
Constraints
* 2 \le N \le 2\cdot10^{18}
Input
The input is given from Standard Input in the format
N
Output
If ans is the number of configurations with the properties described in the statement, you should print on Standard Output
ans
If there are more than 1000 such configurations, print -1.
Examples
Input
2
Output
1
Input
3
Output
1
Input
4
Output
7
Input
13
Output
6
Input
26
Output
-1
Input
123456789123456789
Output
-1
Submitted Solution:
```
if __name__=="__main__":
print(-1)
``` | instruction | 0 | 69,595 | 14 | 139,190 |
No | output | 1 | 69,595 | 14 | 139,191 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N people (with different names) and K clubs. For each club you know the list of members (so you have K unordered lists). Each person can be a member of many clubs (and also 0 clubs) and two different clubs might have exactly the same members. The value of the number K is the minimum possible such that the following property can be true for at least one configuration of clubs. If every person changes her name (maintaining the property that all names are different) and you know the new K lists of members of the clubs (but you don't know which list corresponds to which club), you are sure that you would be able to match the old names with the new ones.
Count the number of such configurations of clubs (or say that there are more than 1000). Two configurations of clubs that can be obtained one from the other if the N people change their names are considered equal.
Formal statement: A club is a (possibly empty) subset of \\{1,\dots, N\\} (the subset identifies the members, assuming that the people are indexed by the numbers 1, 2,\dots, N). A configuration of clubs is an unordered collection of K clubs (not necessarily distinct). Given a permutation \sigma(1), \dots, \sigma(N) of \\{1,\dots, N\\} and a configuration of clubs L=\\{C_1, C_2, \dots, C_K\\}, we denote with \sigma(L) the configuration of clubs \\{\sigma(C_1), \sigma(C_2), \dots, \sigma(C_K)\\} (if C is a club, \sigma(C)=\\{\sigma(x):\, x\in C\\}). A configuration of clubs L is name-preserving if for any pair of distinct permutations \sigma\not=\tau, it holds \sigma(L)\not=\tau(L).
You have to count the number of name-preserving configurations of clubs with the minimum possible number of clubs (so K is minimum). Two configurations L_1, L_2 such that L_2=\sigma(L_1) (for some permutation \sigma) are considered equal. If there are more than 1000 such configurations, print -1.
Constraints
* 2 \le N \le 2\cdot10^{18}
Input
The input is given from Standard Input in the format
N
Output
If ans is the number of configurations with the properties described in the statement, you should print on Standard Output
ans
If there are more than 1000 such configurations, print -1.
Examples
Input
2
Output
1
Input
3
Output
1
Input
4
Output
7
Input
13
Output
6
Input
26
Output
-1
Input
123456789123456789
Output
-1
Submitted Solution:
```
print(2)
``` | instruction | 0 | 69,596 | 14 | 139,192 |
No | output | 1 | 69,596 | 14 | 139,193 |
Provide tags and a correct Python 3 solution for this coding contest problem.
As meticulous Gerald sets the table, Alexander finished another post on Codeforces and begins to respond to New Year greetings from friends. Alexander has n friends, and each of them sends to Alexander exactly one e-card. Let us number his friends by numbers from 1 to n in the order in which they send the cards. Let's introduce the same numbering for the cards, that is, according to the numbering the i-th friend sent to Alexander a card number i.
Alexander also sends cards to friends, but he doesn't look for the new cards on the Net. He simply uses the cards previously sent to him (sometimes, however, he does need to add some crucial details). Initially Alexander doesn't have any cards. Alexander always follows the two rules:
1. He will never send to a firend a card that this friend has sent to him.
2. Among the other cards available to him at the moment, Alexander always chooses one that Alexander himself likes most.
Alexander plans to send to each friend exactly one card. Of course, Alexander can send the same card multiple times.
Alexander and each his friend has the list of preferences, which is a permutation of integers from 1 to n. The first number in the list is the number of the favorite card, the second number shows the second favorite, and so on, the last number shows the least favorite card.
Your task is to find a schedule of sending cards for Alexander. Determine at which moments of time Alexander must send cards to his friends, to please each of them as much as possible. In other words, so that as a result of applying two Alexander's rules, each friend receives the card that is preferred for him as much as possible.
Note that Alexander doesn't choose freely what card to send, but he always strictly follows the two rules.
Input
The first line contains an integer n (2 β€ n β€ 300) β the number of Alexander's friends, equal to the number of cards. Next n lines contain his friends' preference lists. Each list consists of n different integers from 1 to n. The last line contains Alexander's preference list in the same format.
Output
Print n space-separated numbers: the i-th number should be the number of the friend, whose card Alexander receives right before he should send a card to the i-th friend. If there are several solutions, print any of them.
Examples
Input
4
1 2 3 4
4 1 3 2
4 3 1 2
3 4 2 1
3 1 2 4
Output
2 1 1 4
Note
In the sample, the algorithm of actions Alexander and his friends perform is as follows:
1. Alexander receives card 1 from the first friend.
2. Alexander sends the card he has received (at the moment he only has one card, and therefore it is the most preferable for him) to friends with the numbers 2 and 3.
3. Alexander receives card 2 from the second friend, now he has two cards β 1 and 2.
4. Alexander sends a card to the first friend. Despite the fact that Alexander likes card 1 more, he sends card 2 as he cannot send a friend the card sent by that very friend.
5. Alexander receives card 3 from the third friend.
6. Alexander receives card 4 from the fourth friend.
7. Among the cards Alexander has number 3 is his favorite and he sends it to the fourth friend.
Note that Alexander can send cards to multiple friends at a time (in this case the second and the third one). Alexander can send card 3 to the fourth friend after he receives the third card or after he receives the fourth card (both variants are correct). | instruction | 0 | 70,037 | 14 | 140,074 |
Tags: brute force, greedy, implementation
Correct Solution:
```
n = int(input())
arr = [list(map(int, input().split())) for _ in range(n+1)]
res = [0] * n
for i in range(n):
p = [0] * (n+1)
for j in range(n):
p[arr[i][j]] = j
u, t, b = 0, int(1e5), int(1e5)
for x in arr[n]:
if x != i+1 and x < b:
if p[x] < t:
u, t = x, p[x]
b = x
res[i] = u
print(*res)
``` | output | 1 | 70,037 | 14 | 140,075 |
Provide tags and a correct Python 3 solution for this coding contest problem.
As meticulous Gerald sets the table, Alexander finished another post on Codeforces and begins to respond to New Year greetings from friends. Alexander has n friends, and each of them sends to Alexander exactly one e-card. Let us number his friends by numbers from 1 to n in the order in which they send the cards. Let's introduce the same numbering for the cards, that is, according to the numbering the i-th friend sent to Alexander a card number i.
Alexander also sends cards to friends, but he doesn't look for the new cards on the Net. He simply uses the cards previously sent to him (sometimes, however, he does need to add some crucial details). Initially Alexander doesn't have any cards. Alexander always follows the two rules:
1. He will never send to a firend a card that this friend has sent to him.
2. Among the other cards available to him at the moment, Alexander always chooses one that Alexander himself likes most.
Alexander plans to send to each friend exactly one card. Of course, Alexander can send the same card multiple times.
Alexander and each his friend has the list of preferences, which is a permutation of integers from 1 to n. The first number in the list is the number of the favorite card, the second number shows the second favorite, and so on, the last number shows the least favorite card.
Your task is to find a schedule of sending cards for Alexander. Determine at which moments of time Alexander must send cards to his friends, to please each of them as much as possible. In other words, so that as a result of applying two Alexander's rules, each friend receives the card that is preferred for him as much as possible.
Note that Alexander doesn't choose freely what card to send, but he always strictly follows the two rules.
Input
The first line contains an integer n (2 β€ n β€ 300) β the number of Alexander's friends, equal to the number of cards. Next n lines contain his friends' preference lists. Each list consists of n different integers from 1 to n. The last line contains Alexander's preference list in the same format.
Output
Print n space-separated numbers: the i-th number should be the number of the friend, whose card Alexander receives right before he should send a card to the i-th friend. If there are several solutions, print any of them.
Examples
Input
4
1 2 3 4
4 1 3 2
4 3 1 2
3 4 2 1
3 1 2 4
Output
2 1 1 4
Note
In the sample, the algorithm of actions Alexander and his friends perform is as follows:
1. Alexander receives card 1 from the first friend.
2. Alexander sends the card he has received (at the moment he only has one card, and therefore it is the most preferable for him) to friends with the numbers 2 and 3.
3. Alexander receives card 2 from the second friend, now he has two cards β 1 and 2.
4. Alexander sends a card to the first friend. Despite the fact that Alexander likes card 1 more, he sends card 2 as he cannot send a friend the card sent by that very friend.
5. Alexander receives card 3 from the third friend.
6. Alexander receives card 4 from the fourth friend.
7. Among the cards Alexander has number 3 is his favorite and he sends it to the fourth friend.
Note that Alexander can send cards to multiple friends at a time (in this case the second and the third one). Alexander can send card 3 to the fourth friend after he receives the third card or after he receives the fourth card (both variants are correct). | instruction | 0 | 70,038 | 14 | 140,076 |
Tags: brute force, greedy, implementation
Correct Solution:
```
def f():
t = [0] * n
for i, j in enumerate(input().split()): t[int(j) - 1] = i
return t
n = int(input())
p = [f() for i in range(n)]
s = f()
for x, t in enumerate(p):
i = j = x < 1
for y in range(n):
if x != y and s[y] < s[i]: i = y
if t[i] < t[j]: j = i
print(j + 1)
``` | output | 1 | 70,038 | 14 | 140,077 |
Provide tags and a correct Python 3 solution for this coding contest problem.
As meticulous Gerald sets the table, Alexander finished another post on Codeforces and begins to respond to New Year greetings from friends. Alexander has n friends, and each of them sends to Alexander exactly one e-card. Let us number his friends by numbers from 1 to n in the order in which they send the cards. Let's introduce the same numbering for the cards, that is, according to the numbering the i-th friend sent to Alexander a card number i.
Alexander also sends cards to friends, but he doesn't look for the new cards on the Net. He simply uses the cards previously sent to him (sometimes, however, he does need to add some crucial details). Initially Alexander doesn't have any cards. Alexander always follows the two rules:
1. He will never send to a firend a card that this friend has sent to him.
2. Among the other cards available to him at the moment, Alexander always chooses one that Alexander himself likes most.
Alexander plans to send to each friend exactly one card. Of course, Alexander can send the same card multiple times.
Alexander and each his friend has the list of preferences, which is a permutation of integers from 1 to n. The first number in the list is the number of the favorite card, the second number shows the second favorite, and so on, the last number shows the least favorite card.
Your task is to find a schedule of sending cards for Alexander. Determine at which moments of time Alexander must send cards to his friends, to please each of them as much as possible. In other words, so that as a result of applying two Alexander's rules, each friend receives the card that is preferred for him as much as possible.
Note that Alexander doesn't choose freely what card to send, but he always strictly follows the two rules.
Input
The first line contains an integer n (2 β€ n β€ 300) β the number of Alexander's friends, equal to the number of cards. Next n lines contain his friends' preference lists. Each list consists of n different integers from 1 to n. The last line contains Alexander's preference list in the same format.
Output
Print n space-separated numbers: the i-th number should be the number of the friend, whose card Alexander receives right before he should send a card to the i-th friend. If there are several solutions, print any of them.
Examples
Input
4
1 2 3 4
4 1 3 2
4 3 1 2
3 4 2 1
3 1 2 4
Output
2 1 1 4
Note
In the sample, the algorithm of actions Alexander and his friends perform is as follows:
1. Alexander receives card 1 from the first friend.
2. Alexander sends the card he has received (at the moment he only has one card, and therefore it is the most preferable for him) to friends with the numbers 2 and 3.
3. Alexander receives card 2 from the second friend, now he has two cards β 1 and 2.
4. Alexander sends a card to the first friend. Despite the fact that Alexander likes card 1 more, he sends card 2 as he cannot send a friend the card sent by that very friend.
5. Alexander receives card 3 from the third friend.
6. Alexander receives card 4 from the fourth friend.
7. Among the cards Alexander has number 3 is his favorite and he sends it to the fourth friend.
Note that Alexander can send cards to multiple friends at a time (in this case the second and the third one). Alexander can send card 3 to the fourth friend after he receives the third card or after he receives the fourth card (both variants are correct). | instruction | 0 | 70,039 | 14 | 140,078 |
Tags: brute force, greedy, implementation
Correct Solution:
```
import sys
from itertools import *
from math import *
def solve():
n = int(input())
tab = [list(map(int, input().split())) for _ in range(n)]
alex = list(map(int, input().split()))
willget = [[0]*n for _ in range(n)]
has = list()
for row in range(n):
hasrow = list()
for val in alex:
if val <= row + 1:
hasrow.append(val)
has.append(hasrow)
# print(has)
# for col in range(n):
# willget[row][col] = has[0] if has[0] != col + 1 else (-1 if len(has) == 1 else has[1])
# for row in willget:
# print(row)
# for row in has: print(row)
for friend in range(1, n+1): #n + 1
bestindex = -1
bestpriority = n + 1
for time in range(n):
willsend = has[time][0] if has[time][0] != friend else (-1 if len(has[time]) == 1 else has[time][1])
# print(willsend)
rating = n
for i in range(n - 1, -1, -1):
if tab[friend - 1][i] == willsend:
rating = i
if rating < bestpriority:
bestpriority = rating
bestindex = time
print(bestindex + 1, end = ' ')
if sys.hexversion == 50594544 : sys.stdin = open("test.txt")
solve()
``` | output | 1 | 70,039 | 14 | 140,079 |
Provide tags and a correct Python 3 solution for this coding contest problem.
As meticulous Gerald sets the table, Alexander finished another post on Codeforces and begins to respond to New Year greetings from friends. Alexander has n friends, and each of them sends to Alexander exactly one e-card. Let us number his friends by numbers from 1 to n in the order in which they send the cards. Let's introduce the same numbering for the cards, that is, according to the numbering the i-th friend sent to Alexander a card number i.
Alexander also sends cards to friends, but he doesn't look for the new cards on the Net. He simply uses the cards previously sent to him (sometimes, however, he does need to add some crucial details). Initially Alexander doesn't have any cards. Alexander always follows the two rules:
1. He will never send to a firend a card that this friend has sent to him.
2. Among the other cards available to him at the moment, Alexander always chooses one that Alexander himself likes most.
Alexander plans to send to each friend exactly one card. Of course, Alexander can send the same card multiple times.
Alexander and each his friend has the list of preferences, which is a permutation of integers from 1 to n. The first number in the list is the number of the favorite card, the second number shows the second favorite, and so on, the last number shows the least favorite card.
Your task is to find a schedule of sending cards for Alexander. Determine at which moments of time Alexander must send cards to his friends, to please each of them as much as possible. In other words, so that as a result of applying two Alexander's rules, each friend receives the card that is preferred for him as much as possible.
Note that Alexander doesn't choose freely what card to send, but he always strictly follows the two rules.
Input
The first line contains an integer n (2 β€ n β€ 300) β the number of Alexander's friends, equal to the number of cards. Next n lines contain his friends' preference lists. Each list consists of n different integers from 1 to n. The last line contains Alexander's preference list in the same format.
Output
Print n space-separated numbers: the i-th number should be the number of the friend, whose card Alexander receives right before he should send a card to the i-th friend. If there are several solutions, print any of them.
Examples
Input
4
1 2 3 4
4 1 3 2
4 3 1 2
3 4 2 1
3 1 2 4
Output
2 1 1 4
Note
In the sample, the algorithm of actions Alexander and his friends perform is as follows:
1. Alexander receives card 1 from the first friend.
2. Alexander sends the card he has received (at the moment he only has one card, and therefore it is the most preferable for him) to friends with the numbers 2 and 3.
3. Alexander receives card 2 from the second friend, now he has two cards β 1 and 2.
4. Alexander sends a card to the first friend. Despite the fact that Alexander likes card 1 more, he sends card 2 as he cannot send a friend the card sent by that very friend.
5. Alexander receives card 3 from the third friend.
6. Alexander receives card 4 from the fourth friend.
7. Among the cards Alexander has number 3 is his favorite and he sends it to the fourth friend.
Note that Alexander can send cards to multiple friends at a time (in this case the second and the third one). Alexander can send card 3 to the fourth friend after he receives the third card or after he receives the fourth card (both variants are correct). | instruction | 0 | 70,040 | 14 | 140,080 |
Tags: brute force, greedy, implementation
Correct Solution:
```
n = int(input())
frnd = [list(map(int, input().split() )) for _ in range(n)]
ara = list(map(int, input().split() ))
pref = [0]*(n+1)
ans = [0] * n
for i in range(n):
pref[ara[i]] = i
def ok(val, row):
for x in ara:
if x-1 == row:
continue
if x < val:
return 0
if x == val:
return 1
for ro in range(n):
for x in frnd[ro]:
if x - 1 == ro:
continue
if ok(x, ro):
ans[ro] = x
break
for i in range(n):
print(ans[i], end=' ')
print()
``` | output | 1 | 70,040 | 14 | 140,081 |
Provide tags and a correct Python 3 solution for this coding contest problem.
As meticulous Gerald sets the table, Alexander finished another post on Codeforces and begins to respond to New Year greetings from friends. Alexander has n friends, and each of them sends to Alexander exactly one e-card. Let us number his friends by numbers from 1 to n in the order in which they send the cards. Let's introduce the same numbering for the cards, that is, according to the numbering the i-th friend sent to Alexander a card number i.
Alexander also sends cards to friends, but he doesn't look for the new cards on the Net. He simply uses the cards previously sent to him (sometimes, however, he does need to add some crucial details). Initially Alexander doesn't have any cards. Alexander always follows the two rules:
1. He will never send to a firend a card that this friend has sent to him.
2. Among the other cards available to him at the moment, Alexander always chooses one that Alexander himself likes most.
Alexander plans to send to each friend exactly one card. Of course, Alexander can send the same card multiple times.
Alexander and each his friend has the list of preferences, which is a permutation of integers from 1 to n. The first number in the list is the number of the favorite card, the second number shows the second favorite, and so on, the last number shows the least favorite card.
Your task is to find a schedule of sending cards for Alexander. Determine at which moments of time Alexander must send cards to his friends, to please each of them as much as possible. In other words, so that as a result of applying two Alexander's rules, each friend receives the card that is preferred for him as much as possible.
Note that Alexander doesn't choose freely what card to send, but he always strictly follows the two rules.
Input
The first line contains an integer n (2 β€ n β€ 300) β the number of Alexander's friends, equal to the number of cards. Next n lines contain his friends' preference lists. Each list consists of n different integers from 1 to n. The last line contains Alexander's preference list in the same format.
Output
Print n space-separated numbers: the i-th number should be the number of the friend, whose card Alexander receives right before he should send a card to the i-th friend. If there are several solutions, print any of them.
Examples
Input
4
1 2 3 4
4 1 3 2
4 3 1 2
3 4 2 1
3 1 2 4
Output
2 1 1 4
Note
In the sample, the algorithm of actions Alexander and his friends perform is as follows:
1. Alexander receives card 1 from the first friend.
2. Alexander sends the card he has received (at the moment he only has one card, and therefore it is the most preferable for him) to friends with the numbers 2 and 3.
3. Alexander receives card 2 from the second friend, now he has two cards β 1 and 2.
4. Alexander sends a card to the first friend. Despite the fact that Alexander likes card 1 more, he sends card 2 as he cannot send a friend the card sent by that very friend.
5. Alexander receives card 3 from the third friend.
6. Alexander receives card 4 from the fourth friend.
7. Among the cards Alexander has number 3 is his favorite and he sends it to the fourth friend.
Note that Alexander can send cards to multiple friends at a time (in this case the second and the third one). Alexander can send card 3 to the fourth friend after he receives the third card or after he receives the fourth card (both variants are correct). | instruction | 0 | 70,041 | 14 | 140,082 |
Tags: brute force, greedy, implementation
Correct Solution:
```
import sys
from itertools import *
from math import *
def solve():
n = int(input())
tab = [list(map(int, input().split())) for _ in range(n)]
alex = list(map(int, input().split()))
has = list()
for row in range(n):
hasrow = list()
for val in alex:
if val <= row + 1:
hasrow.append(val)
has.append(hasrow)
for friend in range(1, n+1): #n + 1
bestindex = -1
bestpriority = n + 1
for time in range(n):
willsend = has[time][0] if has[time][0] != friend else (-1 if len(has[time]) == 1 else has[time][1])
rating = n
for i in range(n - 1, -1, -1):
if tab[friend - 1][i] == willsend:
rating = i
if rating < bestpriority:
bestpriority = rating
bestindex = time
print(bestindex + 1, end = ' ')
if sys.hexversion == 50594544 : sys.stdin = open("test.txt")
solve()
``` | output | 1 | 70,041 | 14 | 140,083 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
As meticulous Gerald sets the table, Alexander finished another post on Codeforces and begins to respond to New Year greetings from friends. Alexander has n friends, and each of them sends to Alexander exactly one e-card. Let us number his friends by numbers from 1 to n in the order in which they send the cards. Let's introduce the same numbering for the cards, that is, according to the numbering the i-th friend sent to Alexander a card number i.
Alexander also sends cards to friends, but he doesn't look for the new cards on the Net. He simply uses the cards previously sent to him (sometimes, however, he does need to add some crucial details). Initially Alexander doesn't have any cards. Alexander always follows the two rules:
1. He will never send to a firend a card that this friend has sent to him.
2. Among the other cards available to him at the moment, Alexander always chooses one that Alexander himself likes most.
Alexander plans to send to each friend exactly one card. Of course, Alexander can send the same card multiple times.
Alexander and each his friend has the list of preferences, which is a permutation of integers from 1 to n. The first number in the list is the number of the favorite card, the second number shows the second favorite, and so on, the last number shows the least favorite card.
Your task is to find a schedule of sending cards for Alexander. Determine at which moments of time Alexander must send cards to his friends, to please each of them as much as possible. In other words, so that as a result of applying two Alexander's rules, each friend receives the card that is preferred for him as much as possible.
Note that Alexander doesn't choose freely what card to send, but he always strictly follows the two rules.
Input
The first line contains an integer n (2 β€ n β€ 300) β the number of Alexander's friends, equal to the number of cards. Next n lines contain his friends' preference lists. Each list consists of n different integers from 1 to n. The last line contains Alexander's preference list in the same format.
Output
Print n space-separated numbers: the i-th number should be the number of the friend, whose card Alexander receives right before he should send a card to the i-th friend. If there are several solutions, print any of them.
Examples
Input
4
1 2 3 4
4 1 3 2
4 3 1 2
3 4 2 1
3 1 2 4
Output
2 1 1 4
Note
In the sample, the algorithm of actions Alexander and his friends perform is as follows:
1. Alexander receives card 1 from the first friend.
2. Alexander sends the card he has received (at the moment he only has one card, and therefore it is the most preferable for him) to friends with the numbers 2 and 3.
3. Alexander receives card 2 from the second friend, now he has two cards β 1 and 2.
4. Alexander sends a card to the first friend. Despite the fact that Alexander likes card 1 more, he sends card 2 as he cannot send a friend the card sent by that very friend.
5. Alexander receives card 3 from the third friend.
6. Alexander receives card 4 from the fourth friend.
7. Among the cards Alexander has number 3 is his favorite and he sends it to the fourth friend.
Note that Alexander can send cards to multiple friends at a time (in this case the second and the third one). Alexander can send card 3 to the fourth friend after he receives the third card or after he receives the fourth card (both variants are correct).
Submitted Solution:
```
def f():
t = [0] * n
for i, j in enumerate(input().split()): t[int(j) - 1] = i
return t
n = int(input())
p = [f() for i in range(n)]
s = f()
for k, t in enumerate(p):
t[k] = 1e9
i = j = k < 1
for k in range(n):
if s[k] < s[i]: i = k
if t[i] < t[j]: j = i
print(j + 1)
``` | instruction | 0 | 70,042 | 14 | 140,084 |
No | output | 1 | 70,042 | 14 | 140,085 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
As meticulous Gerald sets the table, Alexander finished another post on Codeforces and begins to respond to New Year greetings from friends. Alexander has n friends, and each of them sends to Alexander exactly one e-card. Let us number his friends by numbers from 1 to n in the order in which they send the cards. Let's introduce the same numbering for the cards, that is, according to the numbering the i-th friend sent to Alexander a card number i.
Alexander also sends cards to friends, but he doesn't look for the new cards on the Net. He simply uses the cards previously sent to him (sometimes, however, he does need to add some crucial details). Initially Alexander doesn't have any cards. Alexander always follows the two rules:
1. He will never send to a firend a card that this friend has sent to him.
2. Among the other cards available to him at the moment, Alexander always chooses one that Alexander himself likes most.
Alexander plans to send to each friend exactly one card. Of course, Alexander can send the same card multiple times.
Alexander and each his friend has the list of preferences, which is a permutation of integers from 1 to n. The first number in the list is the number of the favorite card, the second number shows the second favorite, and so on, the last number shows the least favorite card.
Your task is to find a schedule of sending cards for Alexander. Determine at which moments of time Alexander must send cards to his friends, to please each of them as much as possible. In other words, so that as a result of applying two Alexander's rules, each friend receives the card that is preferred for him as much as possible.
Note that Alexander doesn't choose freely what card to send, but he always strictly follows the two rules.
Input
The first line contains an integer n (2 β€ n β€ 300) β the number of Alexander's friends, equal to the number of cards. Next n lines contain his friends' preference lists. Each list consists of n different integers from 1 to n. The last line contains Alexander's preference list in the same format.
Output
Print n space-separated numbers: the i-th number should be the number of the friend, whose card Alexander receives right before he should send a card to the i-th friend. If there are several solutions, print any of them.
Examples
Input
4
1 2 3 4
4 1 3 2
4 3 1 2
3 4 2 1
3 1 2 4
Output
2 1 1 4
Note
In the sample, the algorithm of actions Alexander and his friends perform is as follows:
1. Alexander receives card 1 from the first friend.
2. Alexander sends the card he has received (at the moment he only has one card, and therefore it is the most preferable for him) to friends with the numbers 2 and 3.
3. Alexander receives card 2 from the second friend, now he has two cards β 1 and 2.
4. Alexander sends a card to the first friend. Despite the fact that Alexander likes card 1 more, he sends card 2 as he cannot send a friend the card sent by that very friend.
5. Alexander receives card 3 from the third friend.
6. Alexander receives card 4 from the fourth friend.
7. Among the cards Alexander has number 3 is his favorite and he sends it to the fourth friend.
Note that Alexander can send cards to multiple friends at a time (in this case the second and the third one). Alexander can send card 3 to the fourth friend after he receives the third card or after he receives the fourth card (both variants are correct).
Submitted Solution:
```
print("2 1 1 1")
``` | instruction | 0 | 70,043 | 14 | 140,086 |
No | output | 1 | 70,043 | 14 | 140,087 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
As meticulous Gerald sets the table, Alexander finished another post on Codeforces and begins to respond to New Year greetings from friends. Alexander has n friends, and each of them sends to Alexander exactly one e-card. Let us number his friends by numbers from 1 to n in the order in which they send the cards. Let's introduce the same numbering for the cards, that is, according to the numbering the i-th friend sent to Alexander a card number i.
Alexander also sends cards to friends, but he doesn't look for the new cards on the Net. He simply uses the cards previously sent to him (sometimes, however, he does need to add some crucial details). Initially Alexander doesn't have any cards. Alexander always follows the two rules:
1. He will never send to a firend a card that this friend has sent to him.
2. Among the other cards available to him at the moment, Alexander always chooses one that Alexander himself likes most.
Alexander plans to send to each friend exactly one card. Of course, Alexander can send the same card multiple times.
Alexander and each his friend has the list of preferences, which is a permutation of integers from 1 to n. The first number in the list is the number of the favorite card, the second number shows the second favorite, and so on, the last number shows the least favorite card.
Your task is to find a schedule of sending cards for Alexander. Determine at which moments of time Alexander must send cards to his friends, to please each of them as much as possible. In other words, so that as a result of applying two Alexander's rules, each friend receives the card that is preferred for him as much as possible.
Note that Alexander doesn't choose freely what card to send, but he always strictly follows the two rules.
Input
The first line contains an integer n (2 β€ n β€ 300) β the number of Alexander's friends, equal to the number of cards. Next n lines contain his friends' preference lists. Each list consists of n different integers from 1 to n. The last line contains Alexander's preference list in the same format.
Output
Print n space-separated numbers: the i-th number should be the number of the friend, whose card Alexander receives right before he should send a card to the i-th friend. If there are several solutions, print any of them.
Examples
Input
4
1 2 3 4
4 1 3 2
4 3 1 2
3 4 2 1
3 1 2 4
Output
2 1 1 4
Note
In the sample, the algorithm of actions Alexander and his friends perform is as follows:
1. Alexander receives card 1 from the first friend.
2. Alexander sends the card he has received (at the moment he only has one card, and therefore it is the most preferable for him) to friends with the numbers 2 and 3.
3. Alexander receives card 2 from the second friend, now he has two cards β 1 and 2.
4. Alexander sends a card to the first friend. Despite the fact that Alexander likes card 1 more, he sends card 2 as he cannot send a friend the card sent by that very friend.
5. Alexander receives card 3 from the third friend.
6. Alexander receives card 4 from the fourth friend.
7. Among the cards Alexander has number 3 is his favorite and he sends it to the fourth friend.
Note that Alexander can send cards to multiple friends at a time (in this case the second and the third one). Alexander can send card 3 to the fourth friend after he receives the third card or after he receives the fourth card (both variants are correct).
Submitted Solution:
```
print("3 3 1 3")
``` | instruction | 0 | 70,044 | 14 | 140,088 |
No | output | 1 | 70,044 | 14 | 140,089 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
As meticulous Gerald sets the table, Alexander finished another post on Codeforces and begins to respond to New Year greetings from friends. Alexander has n friends, and each of them sends to Alexander exactly one e-card. Let us number his friends by numbers from 1 to n in the order in which they send the cards. Let's introduce the same numbering for the cards, that is, according to the numbering the i-th friend sent to Alexander a card number i.
Alexander also sends cards to friends, but he doesn't look for the new cards on the Net. He simply uses the cards previously sent to him (sometimes, however, he does need to add some crucial details). Initially Alexander doesn't have any cards. Alexander always follows the two rules:
1. He will never send to a firend a card that this friend has sent to him.
2. Among the other cards available to him at the moment, Alexander always chooses one that Alexander himself likes most.
Alexander plans to send to each friend exactly one card. Of course, Alexander can send the same card multiple times.
Alexander and each his friend has the list of preferences, which is a permutation of integers from 1 to n. The first number in the list is the number of the favorite card, the second number shows the second favorite, and so on, the last number shows the least favorite card.
Your task is to find a schedule of sending cards for Alexander. Determine at which moments of time Alexander must send cards to his friends, to please each of them as much as possible. In other words, so that as a result of applying two Alexander's rules, each friend receives the card that is preferred for him as much as possible.
Note that Alexander doesn't choose freely what card to send, but he always strictly follows the two rules.
Input
The first line contains an integer n (2 β€ n β€ 300) β the number of Alexander's friends, equal to the number of cards. Next n lines contain his friends' preference lists. Each list consists of n different integers from 1 to n. The last line contains Alexander's preference list in the same format.
Output
Print n space-separated numbers: the i-th number should be the number of the friend, whose card Alexander receives right before he should send a card to the i-th friend. If there are several solutions, print any of them.
Examples
Input
4
1 2 3 4
4 1 3 2
4 3 1 2
3 4 2 1
3 1 2 4
Output
2 1 1 4
Note
In the sample, the algorithm of actions Alexander and his friends perform is as follows:
1. Alexander receives card 1 from the first friend.
2. Alexander sends the card he has received (at the moment he only has one card, and therefore it is the most preferable for him) to friends with the numbers 2 and 3.
3. Alexander receives card 2 from the second friend, now he has two cards β 1 and 2.
4. Alexander sends a card to the first friend. Despite the fact that Alexander likes card 1 more, he sends card 2 as he cannot send a friend the card sent by that very friend.
5. Alexander receives card 3 from the third friend.
6. Alexander receives card 4 from the fourth friend.
7. Among the cards Alexander has number 3 is his favorite and he sends it to the fourth friend.
Note that Alexander can send cards to multiple friends at a time (in this case the second and the third one). Alexander can send card 3 to the fourth friend after he receives the third card or after he receives the fourth card (both variants are correct).
Submitted Solution:
```
print("2 1 1 4")
``` | instruction | 0 | 70,045 | 14 | 140,090 |
No | output | 1 | 70,045 | 14 | 140,091 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Once n people simultaneously signed in to the reception at the recently opened, but already thoroughly bureaucratic organization (abbreviated TBO). As the organization is thoroughly bureaucratic, it can accept and cater for exactly one person per day. As a consequence, each of n people made an appointment on one of the next n days, and no two persons have an appointment on the same day.
However, the organization workers are very irresponsible about their job, so none of the signed in people was told the exact date of the appointment. The only way to know when people should come is to write some requests to TBO.
The request form consists of m empty lines. Into each of these lines the name of a signed in person can be written (it can be left blank as well). Writing a person's name in the same form twice is forbidden, such requests are ignored. TBO responds very quickly to written requests, but the reply format is of very poor quality β that is, the response contains the correct appointment dates for all people from the request form, but the dates are in completely random order. Responds to all requests arrive simultaneously at the end of the day (each response specifies the request that it answers).
Fortunately, you aren't among these n lucky guys. As an observer, you have the following task β given n and m, determine the minimum number of requests to submit to TBO to clearly determine the appointment date for each person.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases. Each of the following t lines contains two integers n and m (1 β€ n, m β€ 109) β the number of people who have got an appointment at TBO and the number of empty lines in the request form, correspondingly.
Output
Print t lines, each containing an answer for the corresponding test case (in the order they are given in the input) β the minimum number of requests to submit to TBO.
Examples
Input
5
4 1
4 2
7 3
1 1
42 7
Output
3
2
3
0
11
Note
In the first sample, you need to submit three requests to TBO with three different names. When you learn the appointment dates of three people out of four, you can find out the fourth person's date by elimination, so you do not need a fourth request.
In the second sample you need only two requests. Let's number the persons from 1 to 4 and mention persons 1 and 2 in the first request and persons 1 and 3 in the second request. It is easy to see that after that we can clearly determine each person's appointment date regardless of the answers obtained from TBO.
In the fourth sample only one person signed up for an appointment. He doesn't need to submit any requests β his appointment date is tomorrow. | instruction | 0 | 70,112 | 14 | 140,224 |
Tags: binary search, combinatorics
Correct Solution:
```
def go(m, k):
rem, ans, binom = m*k, 0, 1
ones = 0
while ones <= k:
take = min(1 if ones == 0 else rem//ones, binom)
if take == 0:
break
ans += take
rem -= ones * take
binom = binom*(k-ones)//(ones+1)
ones += 1
return ans
def solve():
n, m = map(int, input().split())
ans = 1;
while go(m, ans) < n:
ans *= 2
jmp = ans
while jmp:
if go(m, ans-jmp) >= n:
ans -= jmp
jmp //= 2
print(ans)
t = int(input())
for _ in range(t):
solve()
``` | output | 1 | 70,112 | 14 | 140,225 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya has k matches, placed in n matchboxes lying in a line from left to right. We know that k is divisible by n. Petya wants all boxes to have the same number of matches inside. For that, he can move a match from its box to the adjacent one in one move. How many such moves does he need to achieve the desired configuration?
Input
The first line contains integer n (1 β€ n β€ 50000). The second line contains n non-negative numbers that do not exceed 109, the i-th written number is the number of matches in the i-th matchbox. It is guaranteed that the total number of matches is divisible by n.
Output
Print the total minimum number of moves.
Examples
Input
6
1 6 2 5 3 7
Output
12 | instruction | 0 | 70,175 | 14 | 140,350 |
Tags: greedy, implementation
Correct Solution:
```
n=int(input())
a=list(map(int,input().split()))
t=0
for i in range(n):
t+=a[i]
t=t//n
cnt=0
for i in range(n):
if a[i]>t:
a[i+1]+=abs(a[i]-t)
cnt+=(abs(a[i]-t))
elif a[i]<t:
a[i+1]-=abs(a[i]-t)
cnt+=(abs(a[i]-t))
print(cnt)
``` | output | 1 | 70,175 | 14 | 140,351 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya has k matches, placed in n matchboxes lying in a line from left to right. We know that k is divisible by n. Petya wants all boxes to have the same number of matches inside. For that, he can move a match from its box to the adjacent one in one move. How many such moves does he need to achieve the desired configuration?
Input
The first line contains integer n (1 β€ n β€ 50000). The second line contains n non-negative numbers that do not exceed 109, the i-th written number is the number of matches in the i-th matchbox. It is guaranteed that the total number of matches is divisible by n.
Output
Print the total minimum number of moves.
Examples
Input
6
1 6 2 5 3 7
Output
12 | instruction | 0 | 70,179 | 14 | 140,358 |
Tags: greedy, implementation
Correct Solution:
```
n=int(input())
lst=[*map(int,input().split())]
item=sum(lst)//n
from sys import exit
if n==1:print(0);exit()
less,more,leng1,leng2=[],[],0,0
for i,x in enumerate(lst):
if x<item:less.append(i);leng1+=1
if x>item:more.append(i);leng2+=1
if leng1==0 and leng2==0:print(0);exit()
i,j=0,0
x,y=less[i],more[j]
res=0
while i<leng1 and j<leng2:
x,y=less[i],more[j]
elem1=lst[x]
elem2=lst[y]
diff1=item-elem1
diff2=elem2-item
if diff2>diff1:
res+=(abs(x-y)*diff1)
lst[y]-=diff1
i+=1
elif diff2<diff1:
res+=(abs(x-y)*diff2)
lst[x]+=diff2
j+=1
else:
res+=(abs(x-y)*diff1)
i,j=i+1,j+1
print(res)
``` | output | 1 | 70,179 | 14 | 140,359 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya has k matches, placed in n matchboxes lying in a line from left to right. We know that k is divisible by n. Petya wants all boxes to have the same number of matches inside. For that, he can move a match from its box to the adjacent one in one move. How many such moves does he need to achieve the desired configuration?
Input
The first line contains integer n (1 β€ n β€ 50000). The second line contains n non-negative numbers that do not exceed 109, the i-th written number is the number of matches in the i-th matchbox. It is guaranteed that the total number of matches is divisible by n.
Output
Print the total minimum number of moves.
Examples
Input
6
1 6 2 5 3 7
Output
12 | instruction | 0 | 70,180 | 14 | 140,360 |
Tags: greedy, implementation
Correct Solution:
```
n = int(input())
a = [int(i) for i in input().split()]
av = sum(a) // len(a)
ans = 0
s = 0
for i in a:
s += i - av
ans += abs(s)
print(ans)
# Made By Mostafa_Khaled
``` | output | 1 | 70,180 | 14 | 140,361 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya has k matches, placed in n matchboxes lying in a line from left to right. We know that k is divisible by n. Petya wants all boxes to have the same number of matches inside. For that, he can move a match from its box to the adjacent one in one move. How many such moves does he need to achieve the desired configuration?
Input
The first line contains integer n (1 β€ n β€ 50000). The second line contains n non-negative numbers that do not exceed 109, the i-th written number is the number of matches in the i-th matchbox. It is guaranteed that the total number of matches is divisible by n.
Output
Print the total minimum number of moves.
Examples
Input
6
1 6 2 5 3 7
Output
12 | instruction | 0 | 70,181 | 14 | 140,362 |
Tags: greedy, implementation
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
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")
##########################################################
from collections import Counter
# c=sorted((i,int(val))for i,val in enumerate(input().split()))
import heapq
# c=sorted((i,int(val))for i,val in enumerate(input().split()))
# n = int(input())
# ls = list(map(int, input().split()))
# n, k = map(int, input().split())
# n =int(input())
#arr=[(i,x) for i,x in enum]
#arr.sort(key=lambda x:x[0])
#print(arr)
# e=list(map(int, input().split()))
from collections import Counter
#print("\n".join(ls))
#print(os.path.commonprefix(ls[0:2]))
#n=int(input())
from bisect import bisect_right
#for _ in range(int(input())):
#n=int(input())
#arr = list(map(int, input().split()))
#for _ in range(int(input())):
#n, k = map(int, input().split())
import bisect
n=int(input())
#n, p,q,r = map(int, input().split())
arr = list(map(int, input().split()))
final=sum(arr)//n
s=0
ans=0
for i in range(n):
s+=arr[i]-final
ans+=abs(s)
print(ans)
``` | output | 1 | 70,181 | 14 | 140,363 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Petya has k matches, placed in n matchboxes lying in a line from left to right. We know that k is divisible by n. Petya wants all boxes to have the same number of matches inside. For that, he can move a match from its box to the adjacent one in one move. How many such moves does he need to achieve the desired configuration?
Input
The first line contains integer n (1 β€ n β€ 50000). The second line contains n non-negative numbers that do not exceed 109, the i-th written number is the number of matches in the i-th matchbox. It is guaranteed that the total number of matches is divisible by n.
Output
Print the total minimum number of moves.
Examples
Input
6
1 6 2 5 3 7
Output
12 | instruction | 0 | 70,182 | 14 | 140,364 |
Tags: greedy, implementation
Correct Solution:
```
n = int(input())
a = [int(i) for i in input().split()]
av = sum(a) // len(a)
ans = 0
s = 0
for i in a:
s += i - av
ans += abs(s)
print(ans)
``` | output | 1 | 70,182 | 14 | 140,365 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys:
* Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing if and only if he is granted all the toys in his lovely set.
* If a child starts playing, then sooner or later he gives the toys back. No child keeps the toys forever.
* Children request toys at distinct moments of time. No two children request a toy at the same time.
* If a child is granted a toy, he never gives it back until he finishes playing with his lovely set.
* If a child is not granted a toy, he waits until he is granted this toy. He can't request another toy while waiting.
* If two children are waiting for the same toy, then the child who requested it first will take the toy first.
Children don't like to play with each other. That's why they never share toys. When a child requests a toy, then granting the toy to this child depends on whether the toy is free or not. If the toy is free, Sagheer will give it to the child. Otherwise, the child has to wait for it and can't request another toy.
Children are smart and can detect if they have to wait forever before they get the toys they want. In such case they start crying. In other words, a crying set is a set of children in which each child is waiting for a toy that is kept by another child in the set.
Now, we have reached a scenario where all the children made all the requests for their lovely sets, except for one child x that still has one last request for his lovely set. Some children are playing while others are waiting for a toy, but no child is crying, and no one has yet finished playing. If the child x is currently waiting for some toy, he makes his last request just after getting that toy. Otherwise, he makes the request right away. When child x will make his last request, how many children will start crying?
You will be given the scenario and q independent queries. Each query will be of the form x y meaning that the last request of the child x is for the toy y. Your task is to help Sagheer find the size of the maximal crying set when child x makes his last request.
Input
The first line contains four integers n, m, k, q (1 β€ n, m, k, q β€ 105) β the number of children, toys, scenario requests and queries.
Each of the next k lines contains two integers a, b (1 β€ a β€ n and 1 β€ b β€ m) β a scenario request meaning child a requests toy b. The requests are given in the order they are made by children.
Each of the next q lines contains two integers x, y (1 β€ x β€ n and 1 β€ y β€ m) β the request to be added to the scenario meaning child x will request toy y just after getting the toy he is waiting for (if any).
It is guaranteed that the scenario requests are consistent and no child is initially crying. All the scenario requests are distinct and no query coincides with a scenario request.
Output
For each query, print on a single line the number of children who will start crying when child x makes his last request for toy y. Please answer all queries independent of each other.
Examples
Input
3 3 5 1
1 1
2 2
3 3
1 2
2 3
3 1
Output
3
Input
5 4 7 2
1 1
2 2
2 1
5 1
3 3
4 4
4 1
5 3
5 4
Output
0
2
Note
In the first example, child 1 is waiting for toy 2, which child 2 has, while child 2 is waiting for top 3, which child 3 has. When child 3 makes his last request, the toy he requests is held by child 1. Each of the three children is waiting for a toy held by another child and no one is playing, so all the three will start crying.
In the second example, at the beginning, child i is holding toy i for 1 β€ i β€ 4. Children 1 and 3 have completed their lovely sets. After they finish playing, toy 3 will be free while toy 1 will be taken by child 2 who has just completed his lovely set. After he finishes, toys 1 and 2 will be free and child 5 will take toy 1. Now:
* In the first query, child 5 will take toy 3 and after he finishes playing, child 4 can play.
* In the second query, child 5 will request toy 4 which is held by child 4. At the same time, child 4 is waiting for toy 1 which is now held by child 5. None of them can play and they will start crying. | instruction | 0 | 70,393 | 14 | 140,786 |
Tags: dfs and similar, graphs, implementation, trees
Correct Solution:
```
from sys import stdin
from sys import stdout
n, m, k, q = map(int, stdin.readline().split())
d = [None for i in range(m)]
roots = set(range(n))
matrix = [[] for i in range(n)]
for i in range(k):
x, y = map(int, stdin.readline().split())
if d[y - 1] is None:
d[y - 1] = x - 1
else:
matrix[d[y - 1]].append(x - 1)
roots.discard(x - 1)
d[y - 1] = x - 1
location = [None for i in range(n)]
comp_of_conn = []
graph = [matrix[i][:] for i in range(n)]
for i in roots:
stack = []
time = 1
queue = [[i, time]]
while queue:
j = queue[-1]
time += 1
if len(graph[j[0]]) == 0:
stack.append(queue.pop() + [time])
else:
queue.append([graph[j[0]].pop(), time])
stack.reverse()
if len(stack) > 1:
for j in range(len(stack)):
location[stack[j][0]] = [len(comp_of_conn), j]
for j in range(len(stack) - 1, -1, -1):
app = 0
for u in matrix[stack[j][0]]:
app += stack[location[u][1]][3]
stack[j].append(app + 1)
comp_of_conn.append(stack)
for i in range(q):
x, y = map(int, stdin.readline().split())
x -= 1
y = d[y - 1]
if y is None:
stdout.write('0\n')
elif location[x] is not None and location[y] is not None and location[x][0] == location[y][0]:
c = location[x][0]
ind_x = location[x][1]
ind_y = location[y][1]
if comp_of_conn[c][ind_x][1] < comp_of_conn[c][ind_y][1] and comp_of_conn[c][ind_x][2] > comp_of_conn[c][ind_y][
2]:
stdout.write(str(comp_of_conn[c][ind_x][3]) + '\n')
else:
stdout.write('0\n')
else:
stdout.write('0\n')
``` | output | 1 | 70,393 | 14 | 140,787 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys:
* Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing if and only if he is granted all the toys in his lovely set.
* If a child starts playing, then sooner or later he gives the toys back. No child keeps the toys forever.
* Children request toys at distinct moments of time. No two children request a toy at the same time.
* If a child is granted a toy, he never gives it back until he finishes playing with his lovely set.
* If a child is not granted a toy, he waits until he is granted this toy. He can't request another toy while waiting.
* If two children are waiting for the same toy, then the child who requested it first will take the toy first.
Children don't like to play with each other. That's why they never share toys. When a child requests a toy, then granting the toy to this child depends on whether the toy is free or not. If the toy is free, Sagheer will give it to the child. Otherwise, the child has to wait for it and can't request another toy.
Children are smart and can detect if they have to wait forever before they get the toys they want. In such case they start crying. In other words, a crying set is a set of children in which each child is waiting for a toy that is kept by another child in the set.
Now, we have reached a scenario where all the children made all the requests for their lovely sets, except for one child x that still has one last request for his lovely set. Some children are playing while others are waiting for a toy, but no child is crying, and no one has yet finished playing. If the child x is currently waiting for some toy, he makes his last request just after getting that toy. Otherwise, he makes the request right away. When child x will make his last request, how many children will start crying?
You will be given the scenario and q independent queries. Each query will be of the form x y meaning that the last request of the child x is for the toy y. Your task is to help Sagheer find the size of the maximal crying set when child x makes his last request.
Input
The first line contains four integers n, m, k, q (1 β€ n, m, k, q β€ 105) β the number of children, toys, scenario requests and queries.
Each of the next k lines contains two integers a, b (1 β€ a β€ n and 1 β€ b β€ m) β a scenario request meaning child a requests toy b. The requests are given in the order they are made by children.
Each of the next q lines contains two integers x, y (1 β€ x β€ n and 1 β€ y β€ m) β the request to be added to the scenario meaning child x will request toy y just after getting the toy he is waiting for (if any).
It is guaranteed that the scenario requests are consistent and no child is initially crying. All the scenario requests are distinct and no query coincides with a scenario request.
Output
For each query, print on a single line the number of children who will start crying when child x makes his last request for toy y. Please answer all queries independent of each other.
Examples
Input
3 3 5 1
1 1
2 2
3 3
1 2
2 3
3 1
Output
3
Input
5 4 7 2
1 1
2 2
2 1
5 1
3 3
4 4
4 1
5 3
5 4
Output
0
2
Note
In the first example, child 1 is waiting for toy 2, which child 2 has, while child 2 is waiting for top 3, which child 3 has. When child 3 makes his last request, the toy he requests is held by child 1. Each of the three children is waiting for a toy held by another child and no one is playing, so all the three will start crying.
In the second example, at the beginning, child i is holding toy i for 1 β€ i β€ 4. Children 1 and 3 have completed their lovely sets. After they finish playing, toy 3 will be free while toy 1 will be taken by child 2 who has just completed his lovely set. After he finishes, toys 1 and 2 will be free and child 5 will take toy 1. Now:
* In the first query, child 5 will take toy 3 and after he finishes playing, child 4 can play.
* In the second query, child 5 will request toy 4 which is held by child 4. At the same time, child 4 is waiting for toy 1 which is now held by child 5. None of them can play and they will start crying.
Submitted Solution:
```
# 2000
import copy
y=[int(x)for x in input().split()]
aal=[]
bbl=[]
def search (data,tosearch):
for sublist in data:
if sublist[0] == tosearch:
return True
return False
for i in range(y[2]):
z=[int(x)for x in input().split()]
aal.append(z[:])
for i in range(y[3]):
z=[int(x)for x in input().split()]
bbl.append(z[:])
for m in range (y[3]):
fav=[]
H = [-1] * y[0]
w = []
for i in range(y[0]):
fav.append([])
aal.append(bbl[m])
for z in aal:
fav[z[0] - 1].append(z[1])
for z in aal:
if search(w,z[0]):
w.append(z)
elif not z[1] in H :
H[z[0]-1]=z[1]
fav[z[0]-1].remove(z[1])
elif len(fav[H.index(z[1])])==0:
H[H.index(z[1])]=-1
H[z[0]-1]=z[1]
fav[z[0]-1].remove(z[1])
else :
w.append(z)
for x in w:
if not x[1] in H:
H[x[0] - 1] = x[1]
fav[x[0]-1].remove(x[1])
w.remove(x)
elif len(fav[H.index(x[1])]) == 0:
H[x[0] - 1] = x[1]
fav[x[0]-1].remove(x[1])
w.remove(x)
aal.pop()
print(len(w))
``` | instruction | 0 | 70,394 | 14 | 140,788 |
No | output | 1 | 70,394 | 14 | 140,789 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Sagheer is working at a kindergarten. There are n children and m different toys. These children use well-defined protocols for playing with the toys:
* Each child has a lovely set of toys that he loves to play with. He requests the toys one after another at distinct moments of time. A child starts playing if and only if he is granted all the toys in his lovely set.
* If a child starts playing, then sooner or later he gives the toys back. No child keeps the toys forever.
* Children request toys at distinct moments of time. No two children request a toy at the same time.
* If a child is granted a toy, he never gives it back until he finishes playing with his lovely set.
* If a child is not granted a toy, he waits until he is granted this toy. He can't request another toy while waiting.
* If two children are waiting for the same toy, then the child who requested it first will take the toy first.
Children don't like to play with each other. That's why they never share toys. When a child requests a toy, then granting the toy to this child depends on whether the toy is free or not. If the toy is free, Sagheer will give it to the child. Otherwise, the child has to wait for it and can't request another toy.
Children are smart and can detect if they have to wait forever before they get the toys they want. In such case they start crying. In other words, a crying set is a set of children in which each child is waiting for a toy that is kept by another child in the set.
Now, we have reached a scenario where all the children made all the requests for their lovely sets, except for one child x that still has one last request for his lovely set. Some children are playing while others are waiting for a toy, but no child is crying, and no one has yet finished playing. If the child x is currently waiting for some toy, he makes his last request just after getting that toy. Otherwise, he makes the request right away. When child x will make his last request, how many children will start crying?
You will be given the scenario and q independent queries. Each query will be of the form x y meaning that the last request of the child x is for the toy y. Your task is to help Sagheer find the size of the maximal crying set when child x makes his last request.
Input
The first line contains four integers n, m, k, q (1 β€ n, m, k, q β€ 105) β the number of children, toys, scenario requests and queries.
Each of the next k lines contains two integers a, b (1 β€ a β€ n and 1 β€ b β€ m) β a scenario request meaning child a requests toy b. The requests are given in the order they are made by children.
Each of the next q lines contains two integers x, y (1 β€ x β€ n and 1 β€ y β€ m) β the request to be added to the scenario meaning child x will request toy y just after getting the toy he is waiting for (if any).
It is guaranteed that the scenario requests are consistent and no child is initially crying. All the scenario requests are distinct and no query coincides with a scenario request.
Output
For each query, print on a single line the number of children who will start crying when child x makes his last request for toy y. Please answer all queries independent of each other.
Examples
Input
3 3 5 1
1 1
2 2
3 3
1 2
2 3
3 1
Output
3
Input
5 4 7 2
1 1
2 2
2 1
5 1
3 3
4 4
4 1
5 3
5 4
Output
0
2
Note
In the first example, child 1 is waiting for toy 2, which child 2 has, while child 2 is waiting for top 3, which child 3 has. When child 3 makes his last request, the toy he requests is held by child 1. Each of the three children is waiting for a toy held by another child and no one is playing, so all the three will start crying.
In the second example, at the beginning, child i is holding toy i for 1 β€ i β€ 4. Children 1 and 3 have completed their lovely sets. After they finish playing, toy 3 will be free while toy 1 will be taken by child 2 who has just completed his lovely set. After he finishes, toys 1 and 2 will be free and child 5 will take toy 1. Now:
* In the first query, child 5 will take toy 3 and after he finishes playing, child 4 can play.
* In the second query, child 5 will request toy 4 which is held by child 4. At the same time, child 4 is waiting for toy 1 which is now held by child 5. None of them can play and they will start crying.
Submitted Solution:
```
n, m, k, q = map(int, input().split())
last_requested_toy = [-1 for i in range(m + 1)]
availability = [True for i in range(m + 1)]
dependency = [-1 for i in range(n + 1)]
children = [[] for i in range(n + 1)]
def is_ancestor(x, y):
tmp_node = y
while tmp_node != -1:
if tmp_node == x:
return True
else:
tmp_node = dependency[tmp_node]
def solve(x, y):
if not is_ancestor(x, y):
return 0
else:
ans = 1
sub = [x]
for node in sub:
for child in children[node]:
ans += len(children[node])
sub.append(child)
return ans
for i in range(k):
a, b = map(int, input().split())
if availability[b]:
availability[b] = False
else:
dependency[a] = last_requested_toy[b]
children[last_requested_toy[b]].append(a)
last_requested_toy[b] = a
for i in range(q):
x, y = map(int, input().split())
print(solve(x, last_requested_toy[y]))
``` | instruction | 0 | 70,395 | 14 | 140,790 |
No | output | 1 | 70,395 | 14 | 140,791 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.