message stringlengths 2 28.7k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 21 109k | cluster float64 7 7 | __index_level_0__ int64 42 217k |
|---|---|---|---|---|---|
Provide a correct Python 3 solution for this coding contest problem.
There are 2N squares arranged from left to right. You are given a string of length 2N representing the color of each of the squares.
The color of the i-th square from the left is black if the i-th character of S is `B`, and white if that character is `W`.
You will perform the following operation exactly N times: choose two distinct squares, then invert the colors of these squares and the squares between them. Here, to invert the color of a square is to make it white if it is black, and vice versa.
Throughout this process, you cannot choose the same square twice or more. That is, each square has to be chosen exactly once.
Find the number of ways to make all the squares white at the end of the process, modulo 10^9+7.
Two ways to make the squares white are considered different if and only if there exists i (1 \leq i \leq N) such that the pair of the squares chosen in the i-th operation is different.
Constraints
* 1 \leq N \leq 10^5
* |S| = 2N
* Each character of S is `B` or `W`.
Input
Input is given from Standard Input in the following format:
N
S
Output
Print the number of ways to make all the squares white at the end of the process, modulo 10^9+7. If there are no such ways, print 0.
Examples
Input
2
BWWB
Output
4
Input
4
BWBBWWWB
Output
288
Input
5
WWWWWWWWWW
Output
0 | instruction | 0 | 108,443 | 7 | 216,886 |
"Correct Solution:
```
# -*- coding: utf-8 -*-
MOD = 10**9+7
n = int(input())
s = input()
if s[0]=="W" or s[-1]=="W":
print(0)
exit()
lr = ["L"]
for i in range(1,2*n):
if s[i-1]==s[i]:
if lr[i-1]=="L":
lr.append("R")
else:
lr.append("L")
else:
lr.append(lr[i-1])
la = [0 for _ in range(2*n+1)]
ra = [0 for _ in range(2*n+1)]
for i in range(2*n):
if lr[i]=="L":
la[i+1] = la[i]+1
ra[i+1] = ra[i]
else:
la[i+1] = la[i]
ra[i+1] = ra[i]+1
if la[-1]!=ra[-1]:
print(0)
exit()
res = 1
for i in range(2*n):
if lr[i]=="R":
res *= la[i]-ra[i]
res %= MOD
for i in range(2,n+1):
res = (res*i)%MOD
print(res)
``` | output | 1 | 108,443 | 7 | 216,887 |
Provide a correct Python 3 solution for this coding contest problem.
There are 2N squares arranged from left to right. You are given a string of length 2N representing the color of each of the squares.
The color of the i-th square from the left is black if the i-th character of S is `B`, and white if that character is `W`.
You will perform the following operation exactly N times: choose two distinct squares, then invert the colors of these squares and the squares between them. Here, to invert the color of a square is to make it white if it is black, and vice versa.
Throughout this process, you cannot choose the same square twice or more. That is, each square has to be chosen exactly once.
Find the number of ways to make all the squares white at the end of the process, modulo 10^9+7.
Two ways to make the squares white are considered different if and only if there exists i (1 \leq i \leq N) such that the pair of the squares chosen in the i-th operation is different.
Constraints
* 1 \leq N \leq 10^5
* |S| = 2N
* Each character of S is `B` or `W`.
Input
Input is given from Standard Input in the following format:
N
S
Output
Print the number of ways to make all the squares white at the end of the process, modulo 10^9+7. If there are no such ways, print 0.
Examples
Input
2
BWWB
Output
4
Input
4
BWBBWWWB
Output
288
Input
5
WWWWWWWWWW
Output
0 | instruction | 0 | 108,444 | 7 | 216,888 |
"Correct Solution:
```
#各マスがちょうど 1 回ずつ選ばれる
n=int(input())
s=input()
d=[False]
for i in range(1,n*2):
d.append((s[i]==s[i-1])^d[-1])
from collections import Counter
cd =Counter(d)
if not cd[True] or cd[0]!=cd[1] or s[0]=="W":
print(0)
exit()
st=d.index(True)
l=st
ans=1;mod=10**9+7
for i in range(st,2*n):
if d[i]:
ans*=l
l-=1
ans%=mod
else:
l+=1
for i in range(1,n+1):
ans*=i
ans%=mod
print(ans)
``` | output | 1 | 108,444 | 7 | 216,889 |
Provide a correct Python 3 solution for this coding contest problem.
There are 2N squares arranged from left to right. You are given a string of length 2N representing the color of each of the squares.
The color of the i-th square from the left is black if the i-th character of S is `B`, and white if that character is `W`.
You will perform the following operation exactly N times: choose two distinct squares, then invert the colors of these squares and the squares between them. Here, to invert the color of a square is to make it white if it is black, and vice versa.
Throughout this process, you cannot choose the same square twice or more. That is, each square has to be chosen exactly once.
Find the number of ways to make all the squares white at the end of the process, modulo 10^9+7.
Two ways to make the squares white are considered different if and only if there exists i (1 \leq i \leq N) such that the pair of the squares chosen in the i-th operation is different.
Constraints
* 1 \leq N \leq 10^5
* |S| = 2N
* Each character of S is `B` or `W`.
Input
Input is given from Standard Input in the following format:
N
S
Output
Print the number of ways to make all the squares white at the end of the process, modulo 10^9+7. If there are no such ways, print 0.
Examples
Input
2
BWWB
Output
4
Input
4
BWBBWWWB
Output
288
Input
5
WWWWWWWWWW
Output
0 | instruction | 0 | 108,445 | 7 | 216,890 |
"Correct Solution:
```
from itertools import groupby
import heapq
N=int(input())
S=input()
mod=10**9+7
fact=[i for i in range(2*N+1)]
fact[0]=1
for i in range(1,2*N+1):
fact[i]*=fact[i-1]
fact[i]%=mod
inverse=[pow(fact[i],mod-2,mod) for i in range(2*N+1)]
data=["" for i in range(2*N)]
for i in range(2*N):
if S[i]=="B" and i%2==1:
data[i]="L"
elif S[i]=="B" and i%2==0:
data[i]="R"
elif S[i]=="W" and i%2==1:
data[i]="R"
elif S[i]=="W" and i%2==0:
data[i]="L"
if data[0]=="L" or data[-1]=="R":
print(0)
exit()
ans=fact[N]
data=groupby(data)
que=[]
heapq.heapify(que)
i=0
for key,group in data:
g=len(list(group))
heapq.heappush(que,(i,g))
i+=1
rest=0
while que:
i,R=heapq.heappop(que)
j,L=heapq.heappop(que)
if L>R+rest:
print(0)
break
else:
ans*=fact[R+rest]*inverse[R+rest-L]
ans%=mod
rest=R+rest-L
else:
if rest==0:
print(ans)
else:
print(0)
``` | output | 1 | 108,445 | 7 | 216,891 |
Provide a correct Python 3 solution for this coding contest problem.
There are 2N squares arranged from left to right. You are given a string of length 2N representing the color of each of the squares.
The color of the i-th square from the left is black if the i-th character of S is `B`, and white if that character is `W`.
You will perform the following operation exactly N times: choose two distinct squares, then invert the colors of these squares and the squares between them. Here, to invert the color of a square is to make it white if it is black, and vice versa.
Throughout this process, you cannot choose the same square twice or more. That is, each square has to be chosen exactly once.
Find the number of ways to make all the squares white at the end of the process, modulo 10^9+7.
Two ways to make the squares white are considered different if and only if there exists i (1 \leq i \leq N) such that the pair of the squares chosen in the i-th operation is different.
Constraints
* 1 \leq N \leq 10^5
* |S| = 2N
* Each character of S is `B` or `W`.
Input
Input is given from Standard Input in the following format:
N
S
Output
Print the number of ways to make all the squares white at the end of the process, modulo 10^9+7. If there are no such ways, print 0.
Examples
Input
2
BWWB
Output
4
Input
4
BWBBWWWB
Output
288
Input
5
WWWWWWWWWW
Output
0 | instruction | 0 | 108,446 | 7 | 216,892 |
"Correct Solution:
```
n = int(input())
S = [v for v in input()]
# determine L or R
lst = [0 for i in range(2 * n)]
for i in range(2 * n):
if S[i] == 'W' and (2 * n - i - 1) % 2 == 0:
lst[i] = 'L'
elif S[i] == 'W' and (2 * n - i - 1) % 2 == 1:
lst[i] = 'R'
elif S[i] == 'B' and (2 * n - i - 1) % 2 == 0:
lst[i] = 'R'
elif S[i] == 'B' and (2 * n - i - 1) % 2 == 1:
lst[i] = 'L'
deq = 0
val = 1
#tracing lst
for i in range(2 * n):
if lst[i] == 'L':
deq += 1
elif lst[i] == 'R':
val *= deq
val %= 10 ** 9 + 7
deq -= 1
if deq != 0:
print(0)
else:
for i in range(1, n + 1):
val = val * i % (10 ** 9 +7)
print(val)
``` | output | 1 | 108,446 | 7 | 216,893 |
Provide a correct Python 3 solution for this coding contest problem.
There are 2N squares arranged from left to right. You are given a string of length 2N representing the color of each of the squares.
The color of the i-th square from the left is black if the i-th character of S is `B`, and white if that character is `W`.
You will perform the following operation exactly N times: choose two distinct squares, then invert the colors of these squares and the squares between them. Here, to invert the color of a square is to make it white if it is black, and vice versa.
Throughout this process, you cannot choose the same square twice or more. That is, each square has to be chosen exactly once.
Find the number of ways to make all the squares white at the end of the process, modulo 10^9+7.
Two ways to make the squares white are considered different if and only if there exists i (1 \leq i \leq N) such that the pair of the squares chosen in the i-th operation is different.
Constraints
* 1 \leq N \leq 10^5
* |S| = 2N
* Each character of S is `B` or `W`.
Input
Input is given from Standard Input in the following format:
N
S
Output
Print the number of ways to make all the squares white at the end of the process, modulo 10^9+7. If there are no such ways, print 0.
Examples
Input
2
BWWB
Output
4
Input
4
BWBBWWWB
Output
288
Input
5
WWWWWWWWWW
Output
0 | instruction | 0 | 108,447 | 7 | 216,894 |
"Correct Solution:
```
# c
M = 10**9+7
n = int(input())
s = input()
if (s[0] != 'B') | (s[-1] != 'B'): print(0); exit()
a = [1]+[0]*(2*n-1)
b = [0]*2*n
for i in range(1, 2*n):
if s[i] == s[i-1]:
a[i] = 1^a[i-1]
b[i] = 1^b[i-1]
else:
a[i] = a[i-1]
b[i] = b[i-1]
if (sum(a) != sum(b)) | (b[-1] == 0):
print(0); exit()
for i in range(1, 2*n):
a[i] += a[i-1]-b[i]
c = 1
for i, j in zip(a, b[1:]):
if j: c *= i*j; c %= M
for i in range(1, n+1):
c = c*i%M
print(c)
``` | output | 1 | 108,447 | 7 | 216,895 |
Provide a correct Python 3 solution for this coding contest problem.
There are 2N squares arranged from left to right. You are given a string of length 2N representing the color of each of the squares.
The color of the i-th square from the left is black if the i-th character of S is `B`, and white if that character is `W`.
You will perform the following operation exactly N times: choose two distinct squares, then invert the colors of these squares and the squares between them. Here, to invert the color of a square is to make it white if it is black, and vice versa.
Throughout this process, you cannot choose the same square twice or more. That is, each square has to be chosen exactly once.
Find the number of ways to make all the squares white at the end of the process, modulo 10^9+7.
Two ways to make the squares white are considered different if and only if there exists i (1 \leq i \leq N) such that the pair of the squares chosen in the i-th operation is different.
Constraints
* 1 \leq N \leq 10^5
* |S| = 2N
* Each character of S is `B` or `W`.
Input
Input is given from Standard Input in the following format:
N
S
Output
Print the number of ways to make all the squares white at the end of the process, modulo 10^9+7. If there are no such ways, print 0.
Examples
Input
2
BWWB
Output
4
Input
4
BWBBWWWB
Output
288
Input
5
WWWWWWWWWW
Output
0 | instruction | 0 | 108,448 | 7 | 216,896 |
"Correct Solution:
```
import sys
import math
from pprint import pprint
def solve(n, s):
ans = 0
if s[0] == "W" or s[2*n-1] == "W":
return ans
l_c = 0
r_c = 0
o_c = 1
mod = 10**9 + 7
for s_i in s:
if s_i == "B" and (l_c - r_c) % 2 == 0:
l_c += 1
elif s_i == "W" and (l_c - r_c) % 2 == 0:
o_c *= l_c - r_c
o_c %= mod
r_c += 1
elif s_i == "B" and (l_c - r_c) % 2 == 1:
o_c *= l_c - r_c
o_c %= mod
r_c += 1
elif s_i == "W" and (l_c - r_c) % 2 == 1:
l_c += 1
# print(s_i, l_c, r_c, o_c)
if l_c - r_c < 0:
return 0
# print(l_c, r_c, o_c)
if l_c - r_c > 0:
return 0
return (o_c * math.factorial(n)) % mod
if __name__ == '__main__':
n = int(sys.stdin.readline().strip())
s = list(sys.stdin.readline().strip())
print(solve(n, s))
``` | output | 1 | 108,448 | 7 | 216,897 |
Provide a correct Python 3 solution for this coding contest problem.
There are 2N squares arranged from left to right. You are given a string of length 2N representing the color of each of the squares.
The color of the i-th square from the left is black if the i-th character of S is `B`, and white if that character is `W`.
You will perform the following operation exactly N times: choose two distinct squares, then invert the colors of these squares and the squares between them. Here, to invert the color of a square is to make it white if it is black, and vice versa.
Throughout this process, you cannot choose the same square twice or more. That is, each square has to be chosen exactly once.
Find the number of ways to make all the squares white at the end of the process, modulo 10^9+7.
Two ways to make the squares white are considered different if and only if there exists i (1 \leq i \leq N) such that the pair of the squares chosen in the i-th operation is different.
Constraints
* 1 \leq N \leq 10^5
* |S| = 2N
* Each character of S is `B` or `W`.
Input
Input is given from Standard Input in the following format:
N
S
Output
Print the number of ways to make all the squares white at the end of the process, modulo 10^9+7. If there are no such ways, print 0.
Examples
Input
2
BWWB
Output
4
Input
4
BWBBWWWB
Output
288
Input
5
WWWWWWWWWW
Output
0 | instruction | 0 | 108,449 | 7 | 216,898 |
"Correct Solution:
```
import bisect
N = int(input())
S = input()
mod = 10 ** 9 + 7
small = []
large = []
for i in range(N * 2):
if S[i] == 'B':
if i % 2 == 0:
small.append(i)
else:
large.append(i)
else:
if i % 2 == 0:
large.append(i)
else:
small.append(i)
if len(small) != len(large):
print(0)
else:
ans = 1
for i in range(N):
large_i = large[i]
smaller = bisect.bisect_left(small, large_i)
ans = (ans * (smaller - i)) % mod
for i in range(1, N + 1):
ans = (ans * i) % mod
print(ans)
``` | output | 1 | 108,449 | 7 | 216,899 |
Provide a correct Python 3 solution for this coding contest problem.
There are 2N squares arranged from left to right. You are given a string of length 2N representing the color of each of the squares.
The color of the i-th square from the left is black if the i-th character of S is `B`, and white if that character is `W`.
You will perform the following operation exactly N times: choose two distinct squares, then invert the colors of these squares and the squares between them. Here, to invert the color of a square is to make it white if it is black, and vice versa.
Throughout this process, you cannot choose the same square twice or more. That is, each square has to be chosen exactly once.
Find the number of ways to make all the squares white at the end of the process, modulo 10^9+7.
Two ways to make the squares white are considered different if and only if there exists i (1 \leq i \leq N) such that the pair of the squares chosen in the i-th operation is different.
Constraints
* 1 \leq N \leq 10^5
* |S| = 2N
* Each character of S is `B` or `W`.
Input
Input is given from Standard Input in the following format:
N
S
Output
Print the number of ways to make all the squares white at the end of the process, modulo 10^9+7. If there are no such ways, print 0.
Examples
Input
2
BWWB
Output
4
Input
4
BWBBWWWB
Output
288
Input
5
WWWWWWWWWW
Output
0 | instruction | 0 | 108,450 | 7 | 216,900 |
"Correct Solution:
```
import math
N = int(input())
S = input()
mod = 10**9 + 7
left = 0
ret = 1
for i in range(len(S)):
if S[i] == 'W':
if left % 2 == 1:
left += 1
else:
if left > 0:
ret *= left
ret %= mod
left -= 1
else:
print(0)
exit()
else:
if left % 2 == 1:
ret *= left
ret %= mod
left -= 1
else:
left += 1
if left > 0:
print(0)
exit()
for i in range(1, N+1):
ret *= i
ret %= mod
print(ret)
``` | output | 1 | 108,450 | 7 | 216,901 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are 2N squares arranged from left to right. You are given a string of length 2N representing the color of each of the squares.
The color of the i-th square from the left is black if the i-th character of S is `B`, and white if that character is `W`.
You will perform the following operation exactly N times: choose two distinct squares, then invert the colors of these squares and the squares between them. Here, to invert the color of a square is to make it white if it is black, and vice versa.
Throughout this process, you cannot choose the same square twice or more. That is, each square has to be chosen exactly once.
Find the number of ways to make all the squares white at the end of the process, modulo 10^9+7.
Two ways to make the squares white are considered different if and only if there exists i (1 \leq i \leq N) such that the pair of the squares chosen in the i-th operation is different.
Constraints
* 1 \leq N \leq 10^5
* |S| = 2N
* Each character of S is `B` or `W`.
Input
Input is given from Standard Input in the following format:
N
S
Output
Print the number of ways to make all the squares white at the end of the process, modulo 10^9+7. If there are no such ways, print 0.
Examples
Input
2
BWWB
Output
4
Input
4
BWBBWWWB
Output
288
Input
5
WWWWWWWWWW
Output
0
Submitted Solution:
```
import sys, math
n = int(input())
s = list(input())
m = 10**9+7
store = 0
ans = 1
for i in range(2*n):
if i % 2 == 0:
if s[i] == "B":
s[i] = "W"
else:
s[i] = "B"
for i in range(2*n):
if s[i] == "W":
store += 1
else:
if store <= 0:
print(0)
sys.exit()
ans = ans % m * store
store -= 1
if not store == 0:
print(0)
sys.exit()
print(ans*math.factorial(n) % m)
``` | instruction | 0 | 108,451 | 7 | 216,902 |
Yes | output | 1 | 108,451 | 7 | 216,903 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are 2N squares arranged from left to right. You are given a string of length 2N representing the color of each of the squares.
The color of the i-th square from the left is black if the i-th character of S is `B`, and white if that character is `W`.
You will perform the following operation exactly N times: choose two distinct squares, then invert the colors of these squares and the squares between them. Here, to invert the color of a square is to make it white if it is black, and vice versa.
Throughout this process, you cannot choose the same square twice or more. That is, each square has to be chosen exactly once.
Find the number of ways to make all the squares white at the end of the process, modulo 10^9+7.
Two ways to make the squares white are considered different if and only if there exists i (1 \leq i \leq N) such that the pair of the squares chosen in the i-th operation is different.
Constraints
* 1 \leq N \leq 10^5
* |S| = 2N
* Each character of S is `B` or `W`.
Input
Input is given from Standard Input in the following format:
N
S
Output
Print the number of ways to make all the squares white at the end of the process, modulo 10^9+7. If there are no such ways, print 0.
Examples
Input
2
BWWB
Output
4
Input
4
BWBBWWWB
Output
288
Input
5
WWWWWWWWWW
Output
0
Submitted Solution:
```
def f_c():
n = int(input())
s = input()
q = 10**9+7
stack, c = 1, 1
for si in s[1:]:
if si=="B":
if stack%2==0:
stack += 1
else:
c *= stack
stack -= 1
else:
if stack%2==0:
c *= stack
stack -= 1
else:
stack += 1
c %= q
if stack!=0 or c==0:
print(0)
else:
for i in range(1, n+1):
c *= i
c %= q
print(c)
if __name__ == "__main__":
f_c()
``` | instruction | 0 | 108,452 | 7 | 216,904 |
Yes | output | 1 | 108,452 | 7 | 216,905 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are 2N squares arranged from left to right. You are given a string of length 2N representing the color of each of the squares.
The color of the i-th square from the left is black if the i-th character of S is `B`, and white if that character is `W`.
You will perform the following operation exactly N times: choose two distinct squares, then invert the colors of these squares and the squares between them. Here, to invert the color of a square is to make it white if it is black, and vice versa.
Throughout this process, you cannot choose the same square twice or more. That is, each square has to be chosen exactly once.
Find the number of ways to make all the squares white at the end of the process, modulo 10^9+7.
Two ways to make the squares white are considered different if and only if there exists i (1 \leq i \leq N) such that the pair of the squares chosen in the i-th operation is different.
Constraints
* 1 \leq N \leq 10^5
* |S| = 2N
* Each character of S is `B` or `W`.
Input
Input is given from Standard Input in the following format:
N
S
Output
Print the number of ways to make all the squares white at the end of the process, modulo 10^9+7. If there are no such ways, print 0.
Examples
Input
2
BWWB
Output
4
Input
4
BWBBWWWB
Output
288
Input
5
WWWWWWWWWW
Output
0
Submitted Solution:
```
n=int(input())
n*=2
A=list(input())
mod=10**9+7
if A[0]=="W" or A[-1]=="W": print(0);exit()
A=[i=="B" for i in A]
LR=[""]*n
LR[0]="L"
for i in range(n-1):
LR[i+1]=LR[i] if A[i]!=A[i+1] else ["L","R"][LR[i]=="L"]
from collections import Counter
#print(LR)
C=Counter(LR)
if C["L"]!=C["R"]:print(0);exit()
if LR[-1]=="L": print(0);exit()
r=0
ans=1
n//=2
for s in range(2*n-1,-1,-1):
if LR[s]=="R":
r+=1
else:
ans*=r
ans%=mod
r-=1
for i in range(1,n+1):
ans*=i
ans%=mod
print(ans)
``` | instruction | 0 | 108,453 | 7 | 216,906 |
Yes | output | 1 | 108,453 | 7 | 216,907 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are 2N squares arranged from left to right. You are given a string of length 2N representing the color of each of the squares.
The color of the i-th square from the left is black if the i-th character of S is `B`, and white if that character is `W`.
You will perform the following operation exactly N times: choose two distinct squares, then invert the colors of these squares and the squares between them. Here, to invert the color of a square is to make it white if it is black, and vice versa.
Throughout this process, you cannot choose the same square twice or more. That is, each square has to be chosen exactly once.
Find the number of ways to make all the squares white at the end of the process, modulo 10^9+7.
Two ways to make the squares white are considered different if and only if there exists i (1 \leq i \leq N) such that the pair of the squares chosen in the i-th operation is different.
Constraints
* 1 \leq N \leq 10^5
* |S| = 2N
* Each character of S is `B` or `W`.
Input
Input is given from Standard Input in the following format:
N
S
Output
Print the number of ways to make all the squares white at the end of the process, modulo 10^9+7. If there are no such ways, print 0.
Examples
Input
2
BWWB
Output
4
Input
4
BWBBWWWB
Output
288
Input
5
WWWWWWWWWW
Output
0
Submitted Solution:
```
MOD = 10 ** 9 + 7
n = int(input())
s = list(str(input()))
if s[0] == "W" or s[-1] == "W":
print(0)
else:
dir = [0] * (2*n)
for i in range(1,2*n):
if s[i] != s[i-1]:
dir[i] = dir[i-1]
else:
dir[i] = (dir[i-1] + 1) % 2
c = 0
for x in dir:
if x == 0:
c += 1
#print(c)
#print(dir)
if c != n:
print(0)
else:
ans = 1
count = 0
for i in range(2*n):
if dir[i] == 0:
count += 1
else:
ans = (ans * count) % MOD
count -= 1
for k in range(1,n+1):
ans = (ans * k) % MOD
print(ans)
``` | instruction | 0 | 108,454 | 7 | 216,908 |
Yes | output | 1 | 108,454 | 7 | 216,909 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are 2N squares arranged from left to right. You are given a string of length 2N representing the color of each of the squares.
The color of the i-th square from the left is black if the i-th character of S is `B`, and white if that character is `W`.
You will perform the following operation exactly N times: choose two distinct squares, then invert the colors of these squares and the squares between them. Here, to invert the color of a square is to make it white if it is black, and vice versa.
Throughout this process, you cannot choose the same square twice or more. That is, each square has to be chosen exactly once.
Find the number of ways to make all the squares white at the end of the process, modulo 10^9+7.
Two ways to make the squares white are considered different if and only if there exists i (1 \leq i \leq N) such that the pair of the squares chosen in the i-th operation is different.
Constraints
* 1 \leq N \leq 10^5
* |S| = 2N
* Each character of S is `B` or `W`.
Input
Input is given from Standard Input in the following format:
N
S
Output
Print the number of ways to make all the squares white at the end of the process, modulo 10^9+7. If there are no such ways, print 0.
Examples
Input
2
BWWB
Output
4
Input
4
BWBBWWWB
Output
288
Input
5
WWWWWWWWWW
Output
0
Submitted Solution:
```
def main():
import sys
def input():
return sys.stdin.readline()[:-1]
N = int(input())
S = list(input())
if S[0]!='B' or S[-1]!='B':
print(0)
exit()
mod = 10**9 + 7
cnt_l = 1
cnt = 1
pre = 'B'
d = 1 #1がleft
ans = 1
for i in range(1,N*2):
if S[i-1]==S[i]:
if d:
d = 0
ans *= cnt%mod
cnt -= 1
else:
d = 1
cnt_l += 1
cnt += 1
else:
if d:
cnt_l += 1
cnt += 1
else:
ans *= cnt%mod
cnt -= 1
for i in range(1,N+1):
ans *= i
ans %= mod
if cnt_l!=N:
print(0)
exit()
print(ans)
if __name__=='__main__':
main()
``` | instruction | 0 | 108,455 | 7 | 216,910 |
No | output | 1 | 108,455 | 7 | 216,911 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are 2N squares arranged from left to right. You are given a string of length 2N representing the color of each of the squares.
The color of the i-th square from the left is black if the i-th character of S is `B`, and white if that character is `W`.
You will perform the following operation exactly N times: choose two distinct squares, then invert the colors of these squares and the squares between them. Here, to invert the color of a square is to make it white if it is black, and vice versa.
Throughout this process, you cannot choose the same square twice or more. That is, each square has to be chosen exactly once.
Find the number of ways to make all the squares white at the end of the process, modulo 10^9+7.
Two ways to make the squares white are considered different if and only if there exists i (1 \leq i \leq N) such that the pair of the squares chosen in the i-th operation is different.
Constraints
* 1 \leq N \leq 10^5
* |S| = 2N
* Each character of S is `B` or `W`.
Input
Input is given from Standard Input in the following format:
N
S
Output
Print the number of ways to make all the squares white at the end of the process, modulo 10^9+7. If there are no such ways, print 0.
Examples
Input
2
BWWB
Output
4
Input
4
BWBBWWWB
Output
288
Input
5
WWWWWWWWWW
Output
0
Submitted Solution:
```
import math
n = int(input())
s = input()
count = 0
ans = 1
for i, x in enumerate(s):
if (i%2 == 0 and x == "B") or (i%2 == 1 and x == "W"): # B:1(R), W:0(L)
count += 1
else:
ans *= count
count -= 1
ans *= math.factorial(n)
if count != 0:
print(0)
else:
print(ans%(10**9+7))
``` | instruction | 0 | 108,456 | 7 | 216,912 |
No | output | 1 | 108,456 | 7 | 216,913 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are 2N squares arranged from left to right. You are given a string of length 2N representing the color of each of the squares.
The color of the i-th square from the left is black if the i-th character of S is `B`, and white if that character is `W`.
You will perform the following operation exactly N times: choose two distinct squares, then invert the colors of these squares and the squares between them. Here, to invert the color of a square is to make it white if it is black, and vice versa.
Throughout this process, you cannot choose the same square twice or more. That is, each square has to be chosen exactly once.
Find the number of ways to make all the squares white at the end of the process, modulo 10^9+7.
Two ways to make the squares white are considered different if and only if there exists i (1 \leq i \leq N) such that the pair of the squares chosen in the i-th operation is different.
Constraints
* 1 \leq N \leq 10^5
* |S| = 2N
* Each character of S is `B` or `W`.
Input
Input is given from Standard Input in the following format:
N
S
Output
Print the number of ways to make all the squares white at the end of the process, modulo 10^9+7. If there are no such ways, print 0.
Examples
Input
2
BWWB
Output
4
Input
4
BWBBWWWB
Output
288
Input
5
WWWWWWWWWW
Output
0
Submitted Solution:
```
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
n = int(input())
s = input().rstrip()
MOD = 10**9+7
cnt = 0
point = 1
for i, x in enumerate(s):
if (x == 'B' and i % 2 == 0) or (x == 'W' and i % 2 != 0):
cnt += 1
else:
point *= cnt
cnt -= 1
# factorial
for x in range(1, n+1):
point *= x
point %= MOD
print(point)
``` | instruction | 0 | 108,457 | 7 | 216,914 |
No | output | 1 | 108,457 | 7 | 216,915 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are 2N squares arranged from left to right. You are given a string of length 2N representing the color of each of the squares.
The color of the i-th square from the left is black if the i-th character of S is `B`, and white if that character is `W`.
You will perform the following operation exactly N times: choose two distinct squares, then invert the colors of these squares and the squares between them. Here, to invert the color of a square is to make it white if it is black, and vice versa.
Throughout this process, you cannot choose the same square twice or more. That is, each square has to be chosen exactly once.
Find the number of ways to make all the squares white at the end of the process, modulo 10^9+7.
Two ways to make the squares white are considered different if and only if there exists i (1 \leq i \leq N) such that the pair of the squares chosen in the i-th operation is different.
Constraints
* 1 \leq N \leq 10^5
* |S| = 2N
* Each character of S is `B` or `W`.
Input
Input is given from Standard Input in the following format:
N
S
Output
Print the number of ways to make all the squares white at the end of the process, modulo 10^9+7. If there are no such ways, print 0.
Examples
Input
2
BWWB
Output
4
Input
4
BWBBWWWB
Output
288
Input
5
WWWWWWWWWW
Output
0
Submitted Solution:
```
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
MOD = 10**9 + 7
N = int(input())
S = input().rstrip()
is_left = [(i%2 == 0) ^ (x == 'W') for i,x in enumerate(S)]
answer = 1
left = 0
for bl in is_left:
if bl:
left += 1
else:
answer *= left
left -= 1
answer %= MOD
for i in range(1,N+1):
answer *= i
answer %= MOD
print(answer)
``` | instruction | 0 | 108,458 | 7 | 216,916 |
No | output | 1 | 108,458 | 7 | 216,917 |
Provide a correct Python 3 solution for this coding contest problem.
There are 2N balls, N white and N black, arranged in a row. The integers from 1 through N are written on the white balls, one on each ball, and they are also written on the black balls, one on each ball. The integer written on the i-th ball from the left (1 ≤ i ≤ 2N) is a_i, and the color of this ball is represented by a letter c_i. c_i = `W` represents the ball is white; c_i = `B` represents the ball is black.
Takahashi the human wants to achieve the following objective:
* For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the white ball with i written on it is to the left of the white ball with j written on it.
* For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the black ball with i written on it is to the left of the black ball with j written on it.
In order to achieve this, he can perform the following operation:
* Swap two adjacent balls.
Find the minimum number of operations required to achieve the objective.
Constraints
* 1 ≤ N ≤ 2000
* 1 ≤ a_i ≤ N
* c_i = `W` or c_i = `B`.
* If i ≠ j, (a_i,c_i) ≠ (a_j,c_j).
Input
Input is given from Standard Input in the following format:
N
c_1 a_1
c_2 a_2
:
c_{2N} a_{2N}
Output
Print the minimum number of operations required to achieve the objective.
Examples
Input
3
B 1
W 2
B 3
W 1
W 3
B 2
Output
4
Input
4
B 4
W 4
B 3
W 3
B 2
W 2
B 1
W 1
Output
18
Input
9
W 3
B 1
B 4
W 1
B 5
W 9
W 2
B 6
W 5
B 3
W 8
B 9
W 7
B 2
B 8
W 4
W 6
B 7
Output
41 | instruction | 0 | 108,491 | 7 | 216,982 |
"Correct Solution:
```
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 6)
def main():
inf = 5000
n = int(input()[:-1])
# iより前に色cのj以上が何個あるかがcnts[c][i][j]
cnts = [[[0] * (n + 1) for _ in range(2 * n + 1)] for __ in range(2)]
pos = [[0] * n for _ in range(2)]
for i in range(n * 2):
c, a = input().split()
c = (c == "W") * 1
a = int(a) - 1
pos[c][a] = i
for cc in range(2):
for j in range(n):
if c == cc and a >= j:
cnts[cc][i + 1][j] = cnts[cc][i][j] + 1
else:
cnts[cc][i + 1][j] = cnts[cc][i][j]
# for x in cnts:
# print(x)
# print(pos)
dp = [[inf] * (n + 1) for _ in range(n + 1)]
dp[0][0] = 0
for w in range(1, n + 1):
i = pos[1][w - 1]
dp[0][w] = dp[0][w - 1] + cnts[0][i][0] + cnts[1][i][w - 1]
for b in range(1, n + 1):
i = pos[0][b - 1]
dp[b][0] = dp[b - 1][0] + cnts[0][i][b - 1] + cnts[1][i][0]
for b in range(1, n + 1):
for w in range(1, n + 1):
bi = pos[0][b - 1]
wi = pos[1][w - 1]
dp[b][w] = min(dp[b - 1][w] + cnts[0][bi][b - 1] + cnts[1][bi][w],
dp[b][w - 1] + cnts[0][wi][b] + cnts[1][wi][w - 1])
print(dp[n][n])
main()
``` | output | 1 | 108,491 | 7 | 216,983 |
Provide a correct Python 3 solution for this coding contest problem.
There are 2N balls, N white and N black, arranged in a row. The integers from 1 through N are written on the white balls, one on each ball, and they are also written on the black balls, one on each ball. The integer written on the i-th ball from the left (1 ≤ i ≤ 2N) is a_i, and the color of this ball is represented by a letter c_i. c_i = `W` represents the ball is white; c_i = `B` represents the ball is black.
Takahashi the human wants to achieve the following objective:
* For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the white ball with i written on it is to the left of the white ball with j written on it.
* For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the black ball with i written on it is to the left of the black ball with j written on it.
In order to achieve this, he can perform the following operation:
* Swap two adjacent balls.
Find the minimum number of operations required to achieve the objective.
Constraints
* 1 ≤ N ≤ 2000
* 1 ≤ a_i ≤ N
* c_i = `W` or c_i = `B`.
* If i ≠ j, (a_i,c_i) ≠ (a_j,c_j).
Input
Input is given from Standard Input in the following format:
N
c_1 a_1
c_2 a_2
:
c_{2N} a_{2N}
Output
Print the minimum number of operations required to achieve the objective.
Examples
Input
3
B 1
W 2
B 3
W 1
W 3
B 2
Output
4
Input
4
B 4
W 4
B 3
W 3
B 2
W 2
B 1
W 1
Output
18
Input
9
W 3
B 1
B 4
W 1
B 5
W 9
W 2
B 6
W 5
B 3
W 8
B 9
W 7
B 2
B 8
W 4
W 6
B 7
Output
41 | instruction | 0 | 108,492 | 7 | 216,984 |
"Correct Solution:
```
class BIT():#1-indexed
def __init__(self, size):
self.table = [0 for _ in range(size+2)]
self.size = size
def Sum(self, i):#1からiまでの和
s = 0
while i > 0:
s += self.table[i]
i -= (i & -i)
return s
def Add(self, i, x):#
while i <= self.size:
self.table[i] += x
i += (i & -i)
return
n = int(input())
s = [input().split() for _ in range(2*n)]
costb = [[0 for _ in range(n+1)] for _ in range(n+1)]
costw = [[0 for _ in range(n+1)] for _ in range(n+1)]
bitb, bitw = BIT(n), BIT(n)
for i in range(2*n-1, -1, -1):
c, x = s[i][0], int(s[i][1])
if c == "B":
base = bitb.Sum(x-1)
for j in range(n+1):
costb[x-1][j] = base + bitw.Sum(j)
bitb.Add(x, 1)
else:
base = bitw.Sum(x-1)
for j in range(n+1):
costw[j][x-1] = base + bitb.Sum(j)
bitw.Add(x, 1)
dp = [[0 for _ in range(n+1)] for _ in range(n+1)]
for i in range(n+1):
for j in range(n+1):
if i+j == 0:
continue
if i == 0:
dp[i][j] = dp[i][j-1] + costw[i][j-1]
elif j == 0:
dp[i][j] = dp[i-1][j] + costb[i-1][j]
else:
dp[i][j] = min(dp[i][j-1] + costw[i][j-1], dp[i-1][j] + costb[i-1][j])
print(dp[n][n])
``` | output | 1 | 108,492 | 7 | 216,985 |
Provide a correct Python 3 solution for this coding contest problem.
There are 2N balls, N white and N black, arranged in a row. The integers from 1 through N are written on the white balls, one on each ball, and they are also written on the black balls, one on each ball. The integer written on the i-th ball from the left (1 ≤ i ≤ 2N) is a_i, and the color of this ball is represented by a letter c_i. c_i = `W` represents the ball is white; c_i = `B` represents the ball is black.
Takahashi the human wants to achieve the following objective:
* For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the white ball with i written on it is to the left of the white ball with j written on it.
* For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the black ball with i written on it is to the left of the black ball with j written on it.
In order to achieve this, he can perform the following operation:
* Swap two adjacent balls.
Find the minimum number of operations required to achieve the objective.
Constraints
* 1 ≤ N ≤ 2000
* 1 ≤ a_i ≤ N
* c_i = `W` or c_i = `B`.
* If i ≠ j, (a_i,c_i) ≠ (a_j,c_j).
Input
Input is given from Standard Input in the following format:
N
c_1 a_1
c_2 a_2
:
c_{2N} a_{2N}
Output
Print the minimum number of operations required to achieve the objective.
Examples
Input
3
B 1
W 2
B 3
W 1
W 3
B 2
Output
4
Input
4
B 4
W 4
B 3
W 3
B 2
W 2
B 1
W 1
Output
18
Input
9
W 3
B 1
B 4
W 1
B 5
W 9
W 2
B 6
W 5
B 3
W 8
B 9
W 7
B 2
B 8
W 4
W 6
B 7
Output
41 | instruction | 0 | 108,493 | 7 | 216,986 |
"Correct Solution:
```
N = int(input())
X = [i for i in range(N+1)]
Y = [[] for _ in range(N)]
B, W = [], []
ans = 0
for i in range(2 * N):
c, a = input().split()
a = int(a) - 1
if c == "B":
X = [X[i] + 1 if i <= a else X[i] - 1 for i in range(N+1)]
B.append(a)
ans += len([b for b in B if b > a])
else:
Y[a] = X[:]
W.append(a)
ans += len([b for b in W if b > a])
Z = [0] * (N+1)
for y in Y:
for i in range(N+1):
Z[i] += y[i]
for i in range(1, N+1):
Z[i] = min(Z[i], Z[i-1])
ans += Z[-1]
print(ans)
``` | output | 1 | 108,493 | 7 | 216,987 |
Provide a correct Python 3 solution for this coding contest problem.
There are 2N balls, N white and N black, arranged in a row. The integers from 1 through N are written on the white balls, one on each ball, and they are also written on the black balls, one on each ball. The integer written on the i-th ball from the left (1 ≤ i ≤ 2N) is a_i, and the color of this ball is represented by a letter c_i. c_i = `W` represents the ball is white; c_i = `B` represents the ball is black.
Takahashi the human wants to achieve the following objective:
* For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the white ball with i written on it is to the left of the white ball with j written on it.
* For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the black ball with i written on it is to the left of the black ball with j written on it.
In order to achieve this, he can perform the following operation:
* Swap two adjacent balls.
Find the minimum number of operations required to achieve the objective.
Constraints
* 1 ≤ N ≤ 2000
* 1 ≤ a_i ≤ N
* c_i = `W` or c_i = `B`.
* If i ≠ j, (a_i,c_i) ≠ (a_j,c_j).
Input
Input is given from Standard Input in the following format:
N
c_1 a_1
c_2 a_2
:
c_{2N} a_{2N}
Output
Print the minimum number of operations required to achieve the objective.
Examples
Input
3
B 1
W 2
B 3
W 1
W 3
B 2
Output
4
Input
4
B 4
W 4
B 3
W 3
B 2
W 2
B 1
W 1
Output
18
Input
9
W 3
B 1
B 4
W 1
B 5
W 9
W 2
B 6
W 5
B 3
W 8
B 9
W 7
B 2
B 8
W 4
W 6
B 7
Output
41 | instruction | 0 | 108,494 | 7 | 216,988 |
"Correct Solution:
```
from itertools import accumulate
def solve(n, rev):
def existence_right(rev_c):
n2 = n * 2
acc = [[0] * n2]
row = [0] * n2
for x in rev_c:
row[n2 - x - 1] += 1
acc.append(list(reversed(list(accumulate(row)))))
return acc
# How many white/black ball lower than 'k' righter than index x? (0<=x<=2N-1)
# cost[color][k][x]
cost = list(map(existence_right, rev))
dp = [0] + list(accumulate(c[y] for y, c in zip(rev[1], cost[1])))
for x, cw0, cw1 in zip(rev[0], cost[0], cost[0][1:]):
ndp = [0] * (n + 1)
cw0x = cw0[x]
ndp[0] = prev = dp[0] + cw0x
for b, (y, cb0, cb1) in enumerate(zip(rev[1], cost[1], cost[1][1:])):
ndp[b + 1] = prev = min(dp[b + 1] + cw0x + cb1[x], prev + cw1[y] + cb0[y])
dp = ndp
return dp[n]
n = int(input())
# White/Black 'k' ball is what-th in whole row?
rev = [[0] * n, [0] * n]
for i in range(n * 2):
c, a = input().split()
a = int(a) - 1
rev[int(c == 'B')][a] = i
print(solve(n, rev))
``` | output | 1 | 108,494 | 7 | 216,989 |
Provide a correct Python 3 solution for this coding contest problem.
There are 2N balls, N white and N black, arranged in a row. The integers from 1 through N are written on the white balls, one on each ball, and they are also written on the black balls, one on each ball. The integer written on the i-th ball from the left (1 ≤ i ≤ 2N) is a_i, and the color of this ball is represented by a letter c_i. c_i = `W` represents the ball is white; c_i = `B` represents the ball is black.
Takahashi the human wants to achieve the following objective:
* For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the white ball with i written on it is to the left of the white ball with j written on it.
* For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the black ball with i written on it is to the left of the black ball with j written on it.
In order to achieve this, he can perform the following operation:
* Swap two adjacent balls.
Find the minimum number of operations required to achieve the objective.
Constraints
* 1 ≤ N ≤ 2000
* 1 ≤ a_i ≤ N
* c_i = `W` or c_i = `B`.
* If i ≠ j, (a_i,c_i) ≠ (a_j,c_j).
Input
Input is given from Standard Input in the following format:
N
c_1 a_1
c_2 a_2
:
c_{2N} a_{2N}
Output
Print the minimum number of operations required to achieve the objective.
Examples
Input
3
B 1
W 2
B 3
W 1
W 3
B 2
Output
4
Input
4
B 4
W 4
B 3
W 3
B 2
W 2
B 1
W 1
Output
18
Input
9
W 3
B 1
B 4
W 1
B 5
W 9
W 2
B 6
W 5
B 3
W 8
B 9
W 7
B 2
B 8
W 4
W 6
B 7
Output
41 | instruction | 0 | 108,495 | 7 | 216,990 |
"Correct Solution:
```
# copy
# https://beta.atcoder.jp/contests/arc097/submissions/2509168
N = int(input())
balls = []
for _ in range(2 * N):
c, a = input().split()
balls += [(c, int(a))]
# numB[i][b]: 初期状態において、i番目の玉より右にある、1~bが書かれた黒玉の数
numB = [[0] * (N + 1) for _ in range(2 * N)]
numW = [[0] * (N + 1) for _ in range(2 * N)]
for i in reversed(range(1, 2 * N)):
c, a = balls[i]
numB[i - 1] = list(numB[i])
numW[i - 1] = list(numW[i])
if c == 'B':
for b in range(a, N + 1):
numB[i - 1][b] += 1
else:
for w in range(a, N + 1):
numW[i - 1][w] += 1
# costB[b][w]: 黒玉1~b、白玉1~wのうち、初期状態において、黒玉[b+1]より右にある玉の数
costB = [[0] * (N + 1) for _ in range(N + 1)]
costW = [[0] * (N + 1) for _ in range(N + 1)]
for i in reversed(range(2 * N)):
c, a = balls[i]
if c == 'B':
cost = numB[i][a - 1]
for w in range(N + 1):
costB[a - 1][w] = cost + numW[i][w]
else:
cost = numW[i][a - 1]
for b in range(N + 1):
costW[b][a - 1] = cost + numB[i][b]
INF = 10 ** 9 + 7
# dp[b][w]: 黒玉1-b, 白玉1-wまで置いたときの転倒数の最小値
dp = [[INF] * (N + 1) for _ in range(N + 1)]
dp[0][0] = 0
for b in range(N + 1):
for w in range(N + 1):
if b > 0:
dp[b][w] = min(dp[b][w], dp[b - 1][w] + costB[b - 1][w])
if w > 0:
dp[b][w] = min(dp[b][w], dp[b][w - 1] + costW[b][w - 1])
print(dp[N][N])
``` | output | 1 | 108,495 | 7 | 216,991 |
Provide a correct Python 3 solution for this coding contest problem.
There are 2N balls, N white and N black, arranged in a row. The integers from 1 through N are written on the white balls, one on each ball, and they are also written on the black balls, one on each ball. The integer written on the i-th ball from the left (1 ≤ i ≤ 2N) is a_i, and the color of this ball is represented by a letter c_i. c_i = `W` represents the ball is white; c_i = `B` represents the ball is black.
Takahashi the human wants to achieve the following objective:
* For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the white ball with i written on it is to the left of the white ball with j written on it.
* For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the black ball with i written on it is to the left of the black ball with j written on it.
In order to achieve this, he can perform the following operation:
* Swap two adjacent balls.
Find the minimum number of operations required to achieve the objective.
Constraints
* 1 ≤ N ≤ 2000
* 1 ≤ a_i ≤ N
* c_i = `W` or c_i = `B`.
* If i ≠ j, (a_i,c_i) ≠ (a_j,c_j).
Input
Input is given from Standard Input in the following format:
N
c_1 a_1
c_2 a_2
:
c_{2N} a_{2N}
Output
Print the minimum number of operations required to achieve the objective.
Examples
Input
3
B 1
W 2
B 3
W 1
W 3
B 2
Output
4
Input
4
B 4
W 4
B 3
W 3
B 2
W 2
B 1
W 1
Output
18
Input
9
W 3
B 1
B 4
W 1
B 5
W 9
W 2
B 6
W 5
B 3
W 8
B 9
W 7
B 2
B 8
W 4
W 6
B 7
Output
41 | instruction | 0 | 108,496 | 7 | 216,992 |
"Correct Solution:
```
def examC():
ans = 0
print(ans)
return
def examD():
ans = 0
print(ans)
return
# 解説AC
def examE():
N = I()
Bnum = [-1]*N
Wnum = [-1]*N
B = {}
W = {}
for i in range(2*N):
c, a = LSI()
a = int(a)-1
if c=="W":
W[i] = a+1
Wnum[a] = i
else:
B[i] = a+1
Bnum[a] = i
SB = [[0]*(N*2) for _ in range(N+1)]; SW = [[0]*(N*2) for _ in range(N+1)]
for i in range(2*N):
if not i in B:
continue
cur = B[i]
cnt = 0
SB[cur][i] = 0
for j in range(2*N):
if not j in B:
SB[cur][j] = cnt
continue
if B[j]<=cur:
cnt += 1
SB[cur][j] = cnt
for i in range(2*N):
if not i in W:
continue
cur = W[i]
cnt = 0
SW[cur][i] = 0
for j in range(2*N):
if not j in W:
SW[cur][j] = cnt
continue
if W[j]<=cur:
cnt += 1
SW[cur][j] = cnt
#for b in SB:
#print(b)
#print(SW)
#print(SW,len(SW))
dp = [[inf]*(N+1) for _ in range(N+1)]
dp[0][0] = 0
for i in range(N):
dp[0][i+1] = dp[0][i]+Bnum[i]-SB[i][Bnum[i]]
dp[i+1][0] = dp[i][0]+Wnum[i]-SW[i][Wnum[i]]
#print(dp)
for i in range(N):
w = Wnum[i]
for j in range(N):
b = Bnum[j]
costb = b - (SB[j][b]+SW[i+1][b])
costw = w - (SB[j+1][w]+SW[i][w])
#if i==2 and j==4:
# print(w,SB[i][w],SW[j+1][w])
# print(b,SB[i][b],SW[j+1][b])
#print(costb,costw,i,j)
#input()
if costb<0:
costb = 0
#print(i,j)
if costw < 0:
costw = 0
#print(i,j)
dp[i+1][j+1] = min(dp[i+1][j+1],dp[i+1][j]+costb,dp[i][j+1]+costw)
ans = dp[-1][-1]
#for v in dp:
# print(v)
print(ans)
return
def examF():
ans = 0
print(ans)
return
from decimal import getcontext,Decimal as dec
import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(input())
def LI(): return list(map(int,sys.stdin.readline().split()))
def DI(): return dec(input())
def LDI(): return list(map(dec,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = dec("0.000000000001")
alphabet = [chr(ord('a') + i) for i in range(26)]
alphabet_convert = {chr(ord('a') + i): i for i in range(26)}
getcontext().prec = 28
sys.setrecursionlimit(10**7)
if __name__ == '__main__':
examE()
"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""
``` | output | 1 | 108,496 | 7 | 216,993 |
Provide a correct Python 3 solution for this coding contest problem.
There are 2N balls, N white and N black, arranged in a row. The integers from 1 through N are written on the white balls, one on each ball, and they are also written on the black balls, one on each ball. The integer written on the i-th ball from the left (1 ≤ i ≤ 2N) is a_i, and the color of this ball is represented by a letter c_i. c_i = `W` represents the ball is white; c_i = `B` represents the ball is black.
Takahashi the human wants to achieve the following objective:
* For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the white ball with i written on it is to the left of the white ball with j written on it.
* For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the black ball with i written on it is to the left of the black ball with j written on it.
In order to achieve this, he can perform the following operation:
* Swap two adjacent balls.
Find the minimum number of operations required to achieve the objective.
Constraints
* 1 ≤ N ≤ 2000
* 1 ≤ a_i ≤ N
* c_i = `W` or c_i = `B`.
* If i ≠ j, (a_i,c_i) ≠ (a_j,c_j).
Input
Input is given from Standard Input in the following format:
N
c_1 a_1
c_2 a_2
:
c_{2N} a_{2N}
Output
Print the minimum number of operations required to achieve the objective.
Examples
Input
3
B 1
W 2
B 3
W 1
W 3
B 2
Output
4
Input
4
B 4
W 4
B 3
W 3
B 2
W 2
B 1
W 1
Output
18
Input
9
W 3
B 1
B 4
W 1
B 5
W 9
W 2
B 6
W 5
B 3
W 8
B 9
W 7
B 2
B 8
W 4
W 6
B 7
Output
41 | instruction | 0 | 108,497 | 7 | 216,994 |
"Correct Solution:
```
def main():
from sys import stdin
input = stdin.readline
n = int(input())
ab = [list(input().split()) for _ in [0]*(2*n)]
ab = [(a == "B", int(b)-1) for a, b in ab]
dct_b = [0]*n
dct_w = [0]*n
for i, (a, b) in enumerate(ab):
if a:
dct_b[b] = i
else:
dct_w[b] = i
dp = [[10**9]*(n+1) for _ in [0]*(n+1)] # dp[i][j] = Bをi個、Wをj個置くための最小手数
dp[0][0] = 0
# 左i個にあるj以上のボールの数
lb = [[0]*(n+1) for _ in [0]*(2*n)]
lw = [[0]*(n+1) for _ in [0]*(2*n)]
for j in range(n):
cnt1, cnt2 = 0, 0
for i, (a, b) in enumerate(ab):
lb[i][j] = cnt1
lw[i][j] = cnt2
if a:
if b >= j:
cnt1 += 1
else:
if b >= j:
cnt2 += 1
dp = [[10**9]*(n+1) for _ in [0]*(n+1)] # dp[i][j] = Bをi個、Wをj個置くための最小手数
dp[0][0] = 0
for i in range(n+1):
dp1 = dp[i]
if i < n:
dp2 = dp[i+1]
t = dct_b[i]
for j in range(n+1):
d = dp1[j]
if i < n:
dp2[j] = min(dp2[j], d+lb[t][i]+lw[t][j])
if j < n:
t2 = dct_w[j]
dp1[j+1] = min(dp1[j+1], d+lb[t2][i]+lw[t2][j])
print(dp[n][n])
main()
``` | output | 1 | 108,497 | 7 | 216,995 |
Provide a correct Python 3 solution for this coding contest problem.
There are 2N balls, N white and N black, arranged in a row. The integers from 1 through N are written on the white balls, one on each ball, and they are also written on the black balls, one on each ball. The integer written on the i-th ball from the left (1 ≤ i ≤ 2N) is a_i, and the color of this ball is represented by a letter c_i. c_i = `W` represents the ball is white; c_i = `B` represents the ball is black.
Takahashi the human wants to achieve the following objective:
* For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the white ball with i written on it is to the left of the white ball with j written on it.
* For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the black ball with i written on it is to the left of the black ball with j written on it.
In order to achieve this, he can perform the following operation:
* Swap two adjacent balls.
Find the minimum number of operations required to achieve the objective.
Constraints
* 1 ≤ N ≤ 2000
* 1 ≤ a_i ≤ N
* c_i = `W` or c_i = `B`.
* If i ≠ j, (a_i,c_i) ≠ (a_j,c_j).
Input
Input is given from Standard Input in the following format:
N
c_1 a_1
c_2 a_2
:
c_{2N} a_{2N}
Output
Print the minimum number of operations required to achieve the objective.
Examples
Input
3
B 1
W 2
B 3
W 1
W 3
B 2
Output
4
Input
4
B 4
W 4
B 3
W 3
B 2
W 2
B 1
W 1
Output
18
Input
9
W 3
B 1
B 4
W 1
B 5
W 9
W 2
B 6
W 5
B 3
W 8
B 9
W 7
B 2
B 8
W 4
W 6
B 7
Output
41 | instruction | 0 | 108,498 | 7 | 216,996 |
"Correct Solution:
```
N = int(input())
MB = {}; MW = {}
for i in range(2*N):
c, a = input().split()
c = "BW".index(c); a = int(a)
if c:
MW[a-1] = i
else:
MB[a-1] = i
P = [[0]*(N+1) for i in range(N+1)]
Q = [[0]*(N+1) for i in range(N+1)]
for p in range(N):
cnt = MW[p] - sum(MW[p0] < MW[p] for p0 in range(p))
P[p][0] = cnt
for q in range(N):
if MB[q] < MW[p]:
cnt -= 1
P[p][q+1] = cnt
for q in range(N):
cnt = MB[q] - sum(MB[q0] < MB[q] for q0 in range(q))
Q[q][0] = cnt
for p in range(N):
if MW[p] < MB[q]:
cnt -= 1
Q[q][p+1] = cnt
dp = [[10**9]*(N+1) for i in range(N+1)]
dp[0][0] = 0
for i in range(N):
dp[i+1][0] = dp[i][0] + P[i][0]
for i in range(N):
dp[0][i+1] = dp[0][i] + Q[i][0]
for i in range(N):
for j in range(N):
dp[i+1][j+1] = min(dp[i][j+1] + P[i][j+1], dp[i+1][j] + Q[j][i+1])
print(dp[N][N])
``` | output | 1 | 108,498 | 7 | 216,997 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are 2N balls, N white and N black, arranged in a row. The integers from 1 through N are written on the white balls, one on each ball, and they are also written on the black balls, one on each ball. The integer written on the i-th ball from the left (1 ≤ i ≤ 2N) is a_i, and the color of this ball is represented by a letter c_i. c_i = `W` represents the ball is white; c_i = `B` represents the ball is black.
Takahashi the human wants to achieve the following objective:
* For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the white ball with i written on it is to the left of the white ball with j written on it.
* For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the black ball with i written on it is to the left of the black ball with j written on it.
In order to achieve this, he can perform the following operation:
* Swap two adjacent balls.
Find the minimum number of operations required to achieve the objective.
Constraints
* 1 ≤ N ≤ 2000
* 1 ≤ a_i ≤ N
* c_i = `W` or c_i = `B`.
* If i ≠ j, (a_i,c_i) ≠ (a_j,c_j).
Input
Input is given from Standard Input in the following format:
N
c_1 a_1
c_2 a_2
:
c_{2N} a_{2N}
Output
Print the minimum number of operations required to achieve the objective.
Examples
Input
3
B 1
W 2
B 3
W 1
W 3
B 2
Output
4
Input
4
B 4
W 4
B 3
W 3
B 2
W 2
B 1
W 1
Output
18
Input
9
W 3
B 1
B 4
W 1
B 5
W 9
W 2
B 6
W 5
B 3
W 8
B 9
W 7
B 2
B 8
W 4
W 6
B 7
Output
41
Submitted Solution:
```
import sys
input = sys.stdin.readline
N = int(input())
c = [0] * (2 * N)
a = [0] * (2 * N)
for i in range(2 * N):
c[i], a[i] = input().split()
a[i] = int(a[i]) - 1
idx = [[0] * 2 for _ in range(N)]
for i in range(2 * N):
idx[a[i]][c[i] == 'W'] = i
costB = [[0] * (N + 1) for _ in range(N)]
for i in range(N):
j = idx[i][0]
cnt = 0
for k in range(i):
if j < idx[k][0]:
cnt += 1
costB[i][0] = j + cnt - i
cnt2 = 0
for k in range(1, N + 1):
if j < idx[k - 1][1]:
cnt2 += 1
costB[i][k] = j + cnt + cnt2 - (i + k)
costW = [[0] * (N + 1) for _ in range(N)]
for i in range(N):
j = idx[i][1]
cnt = 0
for k in range(i):
if j < idx[k][1]:
cnt += 1
costW[i][0] = j + cnt - i
cnt2 = 0
for k in range(1, N + 1):
if j < idx[k - 1][0]:
cnt2 += 1
costW[i][k] = j + cnt + cnt2 - (i + k)
dp = [[10**18] * (N + 1) for _ in range(2 * N + 1)]
dp[0][0] = 0
for i in range(1, 2 * N + 1):
for j in range(max(0, i - N), min(i, N) + 1):
dp[i][j] = dp[i-1][j] + costW[i-j-1][j]
if j > 0:
dp[i][j] = min(dp[i][j], dp[i-1][j-1] + costB[j-1][i-j])
print(dp[2*N][N])
``` | instruction | 0 | 108,499 | 7 | 216,998 |
Yes | output | 1 | 108,499 | 7 | 216,999 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are 2N balls, N white and N black, arranged in a row. The integers from 1 through N are written on the white balls, one on each ball, and they are also written on the black balls, one on each ball. The integer written on the i-th ball from the left (1 ≤ i ≤ 2N) is a_i, and the color of this ball is represented by a letter c_i. c_i = `W` represents the ball is white; c_i = `B` represents the ball is black.
Takahashi the human wants to achieve the following objective:
* For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the white ball with i written on it is to the left of the white ball with j written on it.
* For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the black ball with i written on it is to the left of the black ball with j written on it.
In order to achieve this, he can perform the following operation:
* Swap two adjacent balls.
Find the minimum number of operations required to achieve the objective.
Constraints
* 1 ≤ N ≤ 2000
* 1 ≤ a_i ≤ N
* c_i = `W` or c_i = `B`.
* If i ≠ j, (a_i,c_i) ≠ (a_j,c_j).
Input
Input is given from Standard Input in the following format:
N
c_1 a_1
c_2 a_2
:
c_{2N} a_{2N}
Output
Print the minimum number of operations required to achieve the objective.
Examples
Input
3
B 1
W 2
B 3
W 1
W 3
B 2
Output
4
Input
4
B 4
W 4
B 3
W 3
B 2
W 2
B 1
W 1
Output
18
Input
9
W 3
B 1
B 4
W 1
B 5
W 9
W 2
B 6
W 5
B 3
W 8
B 9
W 7
B 2
B 8
W 4
W 6
B 7
Output
41
Submitted Solution:
```
from itertools import accumulate
def solve(n, rev):
def existence_right(rev_c):
n2 = n * 2
acc = [[0] * n2]
row = [0] * n2
for x in rev_c:
row[n2 - x - 1] += 1
acc.append(list(reversed(list(accumulate(row)))))
return acc
# How many white/black ball lower than 'k' righter than index x? (0<=x<=2N-1)
# cost[color][k][x]
cost = list(map(existence_right, rev))
dp = [[0] * (n + 1) for _ in range(n + 1)]
for w, x in enumerate(rev[0]):
dp[w + 1][0] = dp[w][0] + cost[0][w][x]
for b, y in enumerate(rev[1]):
dp[0][b + 1] = dp[0][b] + cost[1][b][y]
for w, x in enumerate(rev[0]):
dpw0, dpw1 = dp[w], dp[w + 1]
cost_wx, cost_w1 = cost[0][w][x], cost[0][w + 1]
for b, y in enumerate(rev[1]):
dpw1[b + 1] = min(dpw0[b + 1] + cost_wx + cost[1][b + 1][x],
dpw1[b] + cost_w1[y] + cost[1][b][y])
return dp[n][n]
n = int(input())
# White/Black 'k' ball is what-th in whole row?
rev = [[0] * n, [0] * n]
for i in range(n * 2):
c, a = input().split()
a = int(a) - 1
rev[int(c == 'B')][a] = i
print(solve(n, rev))
``` | instruction | 0 | 108,500 | 7 | 217,000 |
Yes | output | 1 | 108,500 | 7 | 217,001 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are 2N balls, N white and N black, arranged in a row. The integers from 1 through N are written on the white balls, one on each ball, and they are also written on the black balls, one on each ball. The integer written on the i-th ball from the left (1 ≤ i ≤ 2N) is a_i, and the color of this ball is represented by a letter c_i. c_i = `W` represents the ball is white; c_i = `B` represents the ball is black.
Takahashi the human wants to achieve the following objective:
* For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the white ball with i written on it is to the left of the white ball with j written on it.
* For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the black ball with i written on it is to the left of the black ball with j written on it.
In order to achieve this, he can perform the following operation:
* Swap two adjacent balls.
Find the minimum number of operations required to achieve the objective.
Constraints
* 1 ≤ N ≤ 2000
* 1 ≤ a_i ≤ N
* c_i = `W` or c_i = `B`.
* If i ≠ j, (a_i,c_i) ≠ (a_j,c_j).
Input
Input is given from Standard Input in the following format:
N
c_1 a_1
c_2 a_2
:
c_{2N} a_{2N}
Output
Print the minimum number of operations required to achieve the objective.
Examples
Input
3
B 1
W 2
B 3
W 1
W 3
B 2
Output
4
Input
4
B 4
W 4
B 3
W 3
B 2
W 2
B 1
W 1
Output
18
Input
9
W 3
B 1
B 4
W 1
B 5
W 9
W 2
B 6
W 5
B 3
W 8
B 9
W 7
B 2
B 8
W 4
W 6
B 7
Output
41
Submitted Solution:
```
from itertools import accumulate
n = int(input())
b = []
# iがあるインデックス
bc = {}
wc = {}
for i in range(2*n):
c,a = input().split()
a = int(a)
b.append((c,a))
if c == "B":
bc[a]=i
else:
wc[a]=i
# black[i][j] : 黒のi(1-indexed)のある位置より左にある、i未満の個数 + 白のj以下の個数
black = [[0]*(n+1) for i in range(n+1)]
white = [[0]*(n+1) for i in range(n+1)]
for i in range(2*n):
c,a = b[i]
# 同色でa未満の個数 cnt
cnt = 0
num = [0]*(n+1)
for j in range(i):
if b[j][0] == c:
if b[j][1] < a:
cnt += 1
else:
num[b[j][1]] += 1
# 累積和
num = list(accumulate(num))
if c == "B":
black[a] = [j+cnt for j in num]
else:
white[a] = [j+cnt for j in num]
DP = [[float("inf")]*(n+1) for i in range(n+1)]
DP[0][0] = 0
for i in range(n+1):
for j in range(n+1):
# 黒がi個、白がj個→i+1, j or i, j+1
if i+1 <= n:
DP[i+1][j] = min(DP[i+1][j], DP[i][j]+bc[i+1]-black[i+1][j])
if j+1 <= n:
DP[i][j+1] = min(DP[i][j+1], DP[i][j]+wc[j+1]-white[j+1][i])
print(DP[n][n])
``` | instruction | 0 | 108,501 | 7 | 217,002 |
Yes | output | 1 | 108,501 | 7 | 217,003 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are 2N balls, N white and N black, arranged in a row. The integers from 1 through N are written on the white balls, one on each ball, and they are also written on the black balls, one on each ball. The integer written on the i-th ball from the left (1 ≤ i ≤ 2N) is a_i, and the color of this ball is represented by a letter c_i. c_i = `W` represents the ball is white; c_i = `B` represents the ball is black.
Takahashi the human wants to achieve the following objective:
* For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the white ball with i written on it is to the left of the white ball with j written on it.
* For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the black ball with i written on it is to the left of the black ball with j written on it.
In order to achieve this, he can perform the following operation:
* Swap two adjacent balls.
Find the minimum number of operations required to achieve the objective.
Constraints
* 1 ≤ N ≤ 2000
* 1 ≤ a_i ≤ N
* c_i = `W` or c_i = `B`.
* If i ≠ j, (a_i,c_i) ≠ (a_j,c_j).
Input
Input is given from Standard Input in the following format:
N
c_1 a_1
c_2 a_2
:
c_{2N} a_{2N}
Output
Print the minimum number of operations required to achieve the objective.
Examples
Input
3
B 1
W 2
B 3
W 1
W 3
B 2
Output
4
Input
4
B 4
W 4
B 3
W 3
B 2
W 2
B 1
W 1
Output
18
Input
9
W 3
B 1
B 4
W 1
B 5
W 9
W 2
B 6
W 5
B 3
W 8
B 9
W 7
B 2
B 8
W 4
W 6
B 7
Output
41
Submitted Solution:
```
N = int(input())
balls = []
for i in range(2 * N):
c, a = input().split()
balls += [(c, int(a))]
# numB[i][b]: 初期状態において、i番目の玉より右にある、1~bが書かれた黒玉の数
numB = [[0] * (N + 1) for i in range(2 * N)]
numW = [[0] * (N + 1) for i in range(2 * N)]
for i in reversed(range(1, 2 * N)):
c, a = balls[i]
numB[i - 1] = list(numB[i])
numW[i - 1] = list(numW[i])
if c == 'B':
for b in range(a, N + 1):
numB[i - 1][b] += 1
else:
for w in range(a, N + 1):
numW[i - 1][w] += 1
# costB[b][w]: 黒玉1~b、白玉1~wのうち、初期状態において、黒玉[b+1]より右にある玉の数
costB = [[0] * (N + 1) for i in range(N + 1)]
costW = [[0] * (N + 1) for i in range(N + 1)]
for i in reversed(range(2 * N)):
c, a = balls[i]
if c == 'B':
cost = numB[i][a - 1]
for w in range(N + 1):
costB[a - 1][w] = cost + numW[i][w]
else:
cost = numW[i][a - 1]
for b in range(N + 1):
costW[b][a - 1] = cost + numB[i][b]
dp = [[float('inf')] * (N + 1) for i in range(N + 1)]
dp[0][0] = 0
for b in range(N + 1):
for w in range(N + 1):
if b > 0:
dp[b][w] = min(dp[b][w], dp[b - 1][w] + costB[b - 1][w])
if w > 0:
dp[b][w] = min(dp[b][w], dp[b][w - 1] + costW[b][w - 1])
print(dp[N][N])
``` | instruction | 0 | 108,502 | 7 | 217,004 |
Yes | output | 1 | 108,502 | 7 | 217,005 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are 2N balls, N white and N black, arranged in a row. The integers from 1 through N are written on the white balls, one on each ball, and they are also written on the black balls, one on each ball. The integer written on the i-th ball from the left (1 ≤ i ≤ 2N) is a_i, and the color of this ball is represented by a letter c_i. c_i = `W` represents the ball is white; c_i = `B` represents the ball is black.
Takahashi the human wants to achieve the following objective:
* For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the white ball with i written on it is to the left of the white ball with j written on it.
* For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the black ball with i written on it is to the left of the black ball with j written on it.
In order to achieve this, he can perform the following operation:
* Swap two adjacent balls.
Find the minimum number of operations required to achieve the objective.
Constraints
* 1 ≤ N ≤ 2000
* 1 ≤ a_i ≤ N
* c_i = `W` or c_i = `B`.
* If i ≠ j, (a_i,c_i) ≠ (a_j,c_j).
Input
Input is given from Standard Input in the following format:
N
c_1 a_1
c_2 a_2
:
c_{2N} a_{2N}
Output
Print the minimum number of operations required to achieve the objective.
Examples
Input
3
B 1
W 2
B 3
W 1
W 3
B 2
Output
4
Input
4
B 4
W 4
B 3
W 3
B 2
W 2
B 1
W 1
Output
18
Input
9
W 3
B 1
B 4
W 1
B 5
W 9
W 2
B 6
W 5
B 3
W 8
B 9
W 7
B 2
B 8
W 4
W 6
B 7
Output
41
Submitted Solution:
```
import sys
input = sys.stdin.readline
N = int(input())
black = [None] * (N+1) # 数字 -> 場所
white = [None] * (N+1)
for i in range(1,2*N+1):
c,a = input().split()
a = int(a)
if c == 'W':
white[a] = i
else:
black[a] = i
after = []
b = 1
w = 1
while True:
if b <= N and w <= N:
ib = black[b]
iw = white[w]
if ib < iw:
after.append(ib)
b += 1
else:
after.append(iw)
w += 1
elif b <= N:
after.append(black[b])
b += 1
elif w <= N:
after.append(white[w])
w += 1
else:
break
# あとは転倒数を数えて終わり
def BIT_update(tree,x):
L = len(tree)
while x < L:
tree[x] += 1
x += x & (-x)
def BIT_sum(tree,x):
L = len(tree)
s = 0
while x:
s += tree[x]
x -= x & (-x)
return s
tree = [0] * (2*N+10)
answer = 0
for i,x in enumerate(after):
answer += i - BIT_sum(tree,x)
BIT_update(tree,x)
print(answer)
``` | instruction | 0 | 108,503 | 7 | 217,006 |
No | output | 1 | 108,503 | 7 | 217,007 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are 2N balls, N white and N black, arranged in a row. The integers from 1 through N are written on the white balls, one on each ball, and they are also written on the black balls, one on each ball. The integer written on the i-th ball from the left (1 ≤ i ≤ 2N) is a_i, and the color of this ball is represented by a letter c_i. c_i = `W` represents the ball is white; c_i = `B` represents the ball is black.
Takahashi the human wants to achieve the following objective:
* For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the white ball with i written on it is to the left of the white ball with j written on it.
* For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the black ball with i written on it is to the left of the black ball with j written on it.
In order to achieve this, he can perform the following operation:
* Swap two adjacent balls.
Find the minimum number of operations required to achieve the objective.
Constraints
* 1 ≤ N ≤ 2000
* 1 ≤ a_i ≤ N
* c_i = `W` or c_i = `B`.
* If i ≠ j, (a_i,c_i) ≠ (a_j,c_j).
Input
Input is given from Standard Input in the following format:
N
c_1 a_1
c_2 a_2
:
c_{2N} a_{2N}
Output
Print the minimum number of operations required to achieve the objective.
Examples
Input
3
B 1
W 2
B 3
W 1
W 3
B 2
Output
4
Input
4
B 4
W 4
B 3
W 3
B 2
W 2
B 1
W 1
Output
18
Input
9
W 3
B 1
B 4
W 1
B 5
W 9
W 2
B 6
W 5
B 3
W 8
B 9
W 7
B 2
B 8
W 4
W 6
B 7
Output
41
Submitted Solution:
```
def main():
from sys import stdin
input = stdin.readline
# 一点更新・一点取得の加法BIT
class BIT():
def __init__(self, n):
self.n = n
self.bit = [0]*(n+1)
# 位置iに値vを足す
def add(self, i):
x, n, bit = i+1, self.n, self.bit
while x < n+1:
bit[x] += 1
x += x & (-x)
# 位置0からiまでの和(sum(bit[:i]))を計算する
def sum(self, i):
ret, x, bit = 0, i, self.bit
while x > 0:
ret += bit[x]
x -= x & (-x)
return ret
n = int(input())
ab = [list(input().split()) for _ in [0]*(2*n)]
ab = [(a, int(b)-1) for a, b in ab]
f_B = [[0]*(n+1) for _ in [0]*n] # f_B[i][j] = Bがi個、Wがj個あるときのBを置く手数
f_W = [[0]*(n+1) for _ in [0]*n] # f_W[i][j] = Bがi個、Wがj個あるときのWを置く手数
dp = [10**9]*(n+1) # dp[i][j] = Bをi個、Wをj個置くための最小手数
dp[0] = 0
# まずf_Bから出す
for i in range(n+1):
bit = BIT(n)
cnt = 0
for a, b in ab:
if a == "B":
f_B[b][i] = bit.sum(n-b-1)+cnt
bit.add(n-b-1)
else:
if b >= i:
cnt += 1
# f_Wも同様
for i in range(n+1):
bit = BIT(n)
cnt = 0
for a, b in ab:
if a == "W":
f_W[b][i] = bit.sum(n-b-1)+cnt
bit.add(n-b-1)
else:
if b >= i:
cnt += 1
for i in range(n):
dp2 = [10**9]*(n+1)
for j in range(n+1):
d = dp[j]
try:
dp2[j] = min(dp2[j], d+f_B[i][j])
except:
pass
try:
dp[j+1] = min(dp[j+1], d+f_W[j][i])
except:
pass
dp = dp2
print(dp[n])
main()
``` | instruction | 0 | 108,504 | 7 | 217,008 |
No | output | 1 | 108,504 | 7 | 217,009 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are 2N balls, N white and N black, arranged in a row. The integers from 1 through N are written on the white balls, one on each ball, and they are also written on the black balls, one on each ball. The integer written on the i-th ball from the left (1 ≤ i ≤ 2N) is a_i, and the color of this ball is represented by a letter c_i. c_i = `W` represents the ball is white; c_i = `B` represents the ball is black.
Takahashi the human wants to achieve the following objective:
* For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the white ball with i written on it is to the left of the white ball with j written on it.
* For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the black ball with i written on it is to the left of the black ball with j written on it.
In order to achieve this, he can perform the following operation:
* Swap two adjacent balls.
Find the minimum number of operations required to achieve the objective.
Constraints
* 1 ≤ N ≤ 2000
* 1 ≤ a_i ≤ N
* c_i = `W` or c_i = `B`.
* If i ≠ j, (a_i,c_i) ≠ (a_j,c_j).
Input
Input is given from Standard Input in the following format:
N
c_1 a_1
c_2 a_2
:
c_{2N} a_{2N}
Output
Print the minimum number of operations required to achieve the objective.
Examples
Input
3
B 1
W 2
B 3
W 1
W 3
B 2
Output
4
Input
4
B 4
W 4
B 3
W 3
B 2
W 2
B 1
W 1
Output
18
Input
9
W 3
B 1
B 4
W 1
B 5
W 9
W 2
B 6
W 5
B 3
W 8
B 9
W 7
B 2
B 8
W 4
W 6
B 7
Output
41
Submitted Solution:
```
def main():
from sys import stdin
input = stdin.readline
n = int(input())
ab = [list(input().split()) for _ in [0]*(2*n)]
ab = [(a, int(b)-1) for a, b in ab]
dct = {(a, b): i for i, (a, b) in enumerate(ab)}
dp = [[10**9]*(n+1) for _ in [0]*(n+1)] # dp[i][j] = Bをi個、Wをj個置くための最小手数
dp[0][0] = 0
# 左i個にあるj以上のボールの数
lb = [[0]*(n+1) for _ in [0]*(2*n)]
lw = [[0]*(n+1) for _ in [0]*(2*n)]
for j in range(n):
cnt1, cnt2 = 0, 0
for i, (a, b) in enumerate(ab):
lb[i][j] = cnt1
lw[i][j] = cnt2
if a == "B":
if b >= j:
cnt1 += 1
else:
if b >= j:
cnt2 += 1
for i in range(n+1):
try:
t = dct[("B", i)]
except:
pass
for j in range(n+1):
d = dp[i][j]
try:
dp[i+1][j] = min(dp[i+1][j], d+lb[t][i]+lw[t][j])
except:
pass
try:
t2 = dct[("W", j)]
dp[i][j+1] = min(dp[i][j+1], d+lb[t2][i]+lw[t2][j])
except:
pass
print(dp[n][n])
main()
``` | instruction | 0 | 108,505 | 7 | 217,010 |
No | output | 1 | 108,505 | 7 | 217,011 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are 2N balls, N white and N black, arranged in a row. The integers from 1 through N are written on the white balls, one on each ball, and they are also written on the black balls, one on each ball. The integer written on the i-th ball from the left (1 ≤ i ≤ 2N) is a_i, and the color of this ball is represented by a letter c_i. c_i = `W` represents the ball is white; c_i = `B` represents the ball is black.
Takahashi the human wants to achieve the following objective:
* For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the white ball with i written on it is to the left of the white ball with j written on it.
* For every pair of integers (i,j) such that 1 ≤ i < j ≤ N, the black ball with i written on it is to the left of the black ball with j written on it.
In order to achieve this, he can perform the following operation:
* Swap two adjacent balls.
Find the minimum number of operations required to achieve the objective.
Constraints
* 1 ≤ N ≤ 2000
* 1 ≤ a_i ≤ N
* c_i = `W` or c_i = `B`.
* If i ≠ j, (a_i,c_i) ≠ (a_j,c_j).
Input
Input is given from Standard Input in the following format:
N
c_1 a_1
c_2 a_2
:
c_{2N} a_{2N}
Output
Print the minimum number of operations required to achieve the objective.
Examples
Input
3
B 1
W 2
B 3
W 1
W 3
B 2
Output
4
Input
4
B 4
W 4
B 3
W 3
B 2
W 2
B 1
W 1
Output
18
Input
9
W 3
B 1
B 4
W 1
B 5
W 9
W 2
B 6
W 5
B 3
W 8
B 9
W 7
B 2
B 8
W 4
W 6
B 7
Output
41
Submitted Solution:
```
class BinaryIndexedTree:
def __init__(self,n):
self.bit=[0]*n
def add(self,i,x):
i+=1
while i<=len(self.bit):
self.bit[i-1]+=x
i+=i&-i
def sum_sub(self,i):
a=0
i+=1
while i:
a+=self.bit[i-1]
i-=i&-i
return a
def sum(self,i,j):
a=self.sum_sub(j)
if i!=0:a-=self.sum_sub(i-1)
return a
n=int(input())
b=[0]*n
w=[0]*n
for i in range(2*n):
c,a=input().split()
if c=="B":b[int(a)-1]=i
else:w[int(a)-1]=i
bit=[BinaryIndexedTree(2*n)for _ in range(n+1)]
dp=[(n+1)*[10**10]for _ in range(n+1)]
dp[0][0]=0
for i in range(n+1):
dp[i][0]=0
for k in range(i):
dp[i][0]+=w[k]+bit[i].sum(w[k],2*n-1)-k
bit[i].add(w[k],1)
for j in range(n):
for i in range(n+1):
if i==0:
dp[i][j+1]=dp[i][j]+b[j]+bit[i].sum(b[j],2*n-1)-(i+j)
else:
dp[i][j+1]=dp[i][j]+(b[j]+bit[i].sum(b[j],2*n-1)-(i+j))
dp[i][j+1]=min(dp[i][j+1],dp[i-1][j+1]+(w[i-1]+bit[i-1].sum(w[i-1],2*n-1)-(i+j)))
bit[i].add(b[j],1)
print(dp[n][n])
``` | instruction | 0 | 108,506 | 7 | 217,012 |
No | output | 1 | 108,506 | 7 | 217,013 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.