text stringlengths 198 433k | conversation_id int64 0 109k |
|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n walruses standing in a queue in an airport. They are numbered starting from the queue's tail: the 1-st walrus stands at the end of the queue and the n-th walrus stands at the beginning of the queue. The i-th walrus has the age equal to ai.
The i-th walrus becomes displeased if there's a younger walrus standing in front of him, that is, if exists such j (i < j), that ai > aj. The displeasure of the i-th walrus is equal to the number of walruses between him and the furthest walrus ahead of him, which is younger than the i-th one. That is, the further that young walrus stands from him, the stronger the displeasure is.
The airport manager asked you to count for each of n walruses in the queue his displeasure.
Input
The first line contains an integer n (2 ≤ n ≤ 105) — the number of walruses in the queue. The second line contains integers ai (1 ≤ ai ≤ 109).
Note that some walruses can have the same age but for the displeasure to emerge the walrus that is closer to the head of the queue needs to be strictly younger than the other one.
Output
Print n numbers: if the i-th walrus is pleased with everything, print "-1" (without the quotes). Otherwise, print the i-th walrus's displeasure: the number of other walruses that stand between him and the furthest from him younger walrus.
Examples
Input
6
10 8 5 3 50 45
Output
2 1 0 -1 0 -1
Input
7
10 4 6 3 2 8 15
Output
4 2 1 0 -1 -1 -1
Input
5
10 3 1 10 11
Output
1 0 -1 -1 -1
Submitted Solution:
```
from collections import defaultdict
mod=10**9+7
for _ in range(1):
n=int(input())
l=list(map(int,input().split()))
l=l[::-1]
a=[l[0]]
pos=[0]
ans=[]
for i in range(1,n):
if a[-1]>l[i]:
pos.append(i)
else:
pos.append(pos[-1])
a.append(min(a[-1],l[i]))
#print(a)
#print(pos)
for i in range(n):
j=pos[i]
x=i
while(j>=0 and a[j]<l[i]):
x=j
if j==0:
break
j=pos[j-1]
if l[i]==a[x]:
ans.append(-1)
else:
ans.append(i-pos[x]-1)
print(*ans[::-1])
```
Yes
| 95,000 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n walruses standing in a queue in an airport. They are numbered starting from the queue's tail: the 1-st walrus stands at the end of the queue and the n-th walrus stands at the beginning of the queue. The i-th walrus has the age equal to ai.
The i-th walrus becomes displeased if there's a younger walrus standing in front of him, that is, if exists such j (i < j), that ai > aj. The displeasure of the i-th walrus is equal to the number of walruses between him and the furthest walrus ahead of him, which is younger than the i-th one. That is, the further that young walrus stands from him, the stronger the displeasure is.
The airport manager asked you to count for each of n walruses in the queue his displeasure.
Input
The first line contains an integer n (2 ≤ n ≤ 105) — the number of walruses in the queue. The second line contains integers ai (1 ≤ ai ≤ 109).
Note that some walruses can have the same age but for the displeasure to emerge the walrus that is closer to the head of the queue needs to be strictly younger than the other one.
Output
Print n numbers: if the i-th walrus is pleased with everything, print "-1" (without the quotes). Otherwise, print the i-th walrus's displeasure: the number of other walruses that stand between him and the furthest from him younger walrus.
Examples
Input
6
10 8 5 3 50 45
Output
2 1 0 -1 0 -1
Input
7
10 4 6 3 2 8 15
Output
4 2 1 0 -1 -1 -1
Input
5
10 3 1 10 11
Output
1 0 -1 -1 -1
Submitted Solution:
```
# It's never too late to start!
from bisect import bisect_left, bisect_right
import os
import sys
from io import BytesIO, IOBase
from collections import Counter, defaultdict
from collections import deque
from functools import cmp_to_key
import math
import heapq
import re
def sin():
return input()
def ain():
return list(map(int, sin().split()))
def sain():
return input().split()
def iin():
return int(sin())
MAX = float('inf')
MIN = float('-inf')
MOD = 1000000007
def sieve(n):
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
s = set()
for p in range(2, n+1):
if prime[p]:
s.add(p)
return s
def readTree(n, m):
adj = [deque([]) for _ in range(n+1)]
for _ in range(m):
u,v = ain()
adj[u].append(v)
adj[v].append(u)
return adj
# Stay hungry, stay foolish!
def main():
n = iin()
d = deque([])
l = ain()
for i in range(n):
d.append([l[i], i])
k = sorted(d)
maxi = MIN
ans = [0]*n
for i in range(n):
maxi = max(maxi, k[i][1])
ans[k[i][1]] = maxi - k[i][1] - 1
print(*ans)
# Fast IO Template starts
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")
if os.getcwd() == 'D:\\code':
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
else:
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# Fast IO Template ends
if __name__ == "__main__":
main()
# Never Give Up - John Cena
```
Yes
| 95,001 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n walruses standing in a queue in an airport. They are numbered starting from the queue's tail: the 1-st walrus stands at the end of the queue and the n-th walrus stands at the beginning of the queue. The i-th walrus has the age equal to ai.
The i-th walrus becomes displeased if there's a younger walrus standing in front of him, that is, if exists such j (i < j), that ai > aj. The displeasure of the i-th walrus is equal to the number of walruses between him and the furthest walrus ahead of him, which is younger than the i-th one. That is, the further that young walrus stands from him, the stronger the displeasure is.
The airport manager asked you to count for each of n walruses in the queue his displeasure.
Input
The first line contains an integer n (2 ≤ n ≤ 105) — the number of walruses in the queue. The second line contains integers ai (1 ≤ ai ≤ 109).
Note that some walruses can have the same age but for the displeasure to emerge the walrus that is closer to the head of the queue needs to be strictly younger than the other one.
Output
Print n numbers: if the i-th walrus is pleased with everything, print "-1" (without the quotes). Otherwise, print the i-th walrus's displeasure: the number of other walruses that stand between him and the furthest from him younger walrus.
Examples
Input
6
10 8 5 3 50 45
Output
2 1 0 -1 0 -1
Input
7
10 4 6 3 2 8 15
Output
4 2 1 0 -1 -1 -1
Input
5
10 3 1 10 11
Output
1 0 -1 -1 -1
Submitted Solution:
```
from collections import deque
def bs(l,k):
lo=0
ans=-1
hi=len(l)-1
while lo<=hi:
mi=(lo+hi)>>1
if l[mi]<k:
ans=l[mi]
lo=mi+1
else:
hi=mi-1
return ans
for _ in range(1):
n=int(input())
ind={}
suff=[int(i) for i in input().split()]
search=deque()
ans=[0]*n
for i in range(n-1,-1,-1):
if not search:
search.append(suff[i])
ind[suff[i]]=i
else:
if suff[i]<search[0]:
search.appendleft(suff[i])
ind[suff[i]]=i
z=bs(search,suff[i])
if z==-1:
ans[i]=-1
else:
ans[i]=(ind[z]-i-1)
print(' '.join(str(i) for i in ans))
```
Yes
| 95,002 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n walruses standing in a queue in an airport. They are numbered starting from the queue's tail: the 1-st walrus stands at the end of the queue and the n-th walrus stands at the beginning of the queue. The i-th walrus has the age equal to ai.
The i-th walrus becomes displeased if there's a younger walrus standing in front of him, that is, if exists such j (i < j), that ai > aj. The displeasure of the i-th walrus is equal to the number of walruses between him and the furthest walrus ahead of him, which is younger than the i-th one. That is, the further that young walrus stands from him, the stronger the displeasure is.
The airport manager asked you to count for each of n walruses in the queue his displeasure.
Input
The first line contains an integer n (2 ≤ n ≤ 105) — the number of walruses in the queue. The second line contains integers ai (1 ≤ ai ≤ 109).
Note that some walruses can have the same age but for the displeasure to emerge the walrus that is closer to the head of the queue needs to be strictly younger than the other one.
Output
Print n numbers: if the i-th walrus is pleased with everything, print "-1" (without the quotes). Otherwise, print the i-th walrus's displeasure: the number of other walruses that stand between him and the furthest from him younger walrus.
Examples
Input
6
10 8 5 3 50 45
Output
2 1 0 -1 0 -1
Input
7
10 4 6 3 2 8 15
Output
4 2 1 0 -1 -1 -1
Input
5
10 3 1 10 11
Output
1 0 -1 -1 -1
Submitted Solution:
```
#!/usr/bin/env python3
num_lines = input()
raw = input()
items = raw.split(" ")
temp = []
for i in range(len(items)):
count = len(items) - i - 2
j = len(items) - 1
while (j > i):
if(int(items[j]) < int(items[i])):
break
count = count - 1
j = j - 1
temp.append(count)
print(temp)
```
No
| 95,003 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n walruses standing in a queue in an airport. They are numbered starting from the queue's tail: the 1-st walrus stands at the end of the queue and the n-th walrus stands at the beginning of the queue. The i-th walrus has the age equal to ai.
The i-th walrus becomes displeased if there's a younger walrus standing in front of him, that is, if exists such j (i < j), that ai > aj. The displeasure of the i-th walrus is equal to the number of walruses between him and the furthest walrus ahead of him, which is younger than the i-th one. That is, the further that young walrus stands from him, the stronger the displeasure is.
The airport manager asked you to count for each of n walruses in the queue his displeasure.
Input
The first line contains an integer n (2 ≤ n ≤ 105) — the number of walruses in the queue. The second line contains integers ai (1 ≤ ai ≤ 109).
Note that some walruses can have the same age but for the displeasure to emerge the walrus that is closer to the head of the queue needs to be strictly younger than the other one.
Output
Print n numbers: if the i-th walrus is pleased with everything, print "-1" (without the quotes). Otherwise, print the i-th walrus's displeasure: the number of other walruses that stand between him and the furthest from him younger walrus.
Examples
Input
6
10 8 5 3 50 45
Output
2 1 0 -1 0 -1
Input
7
10 4 6 3 2 8 15
Output
4 2 1 0 -1 -1 -1
Input
5
10 3 1 10 11
Output
1 0 -1 -1 -1
Submitted Solution:
```
from math import gcd
input=__import__('sys').stdin.readline
n = int(input())
lis = list(map(int,input().split()))
ans=[0]*n
for i in range(n):
l=i+1
r=n-1
while l<=r:
mid= l + (r-l)//2
a=lis[mid]
if a<lis[i]:
l=mid+1
else:
r=mid-1
if r<i:
ans[i]=-1
else:
ans[i]+=(r-i-1)
print(*ans)
```
No
| 95,004 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n walruses standing in a queue in an airport. They are numbered starting from the queue's tail: the 1-st walrus stands at the end of the queue and the n-th walrus stands at the beginning of the queue. The i-th walrus has the age equal to ai.
The i-th walrus becomes displeased if there's a younger walrus standing in front of him, that is, if exists such j (i < j), that ai > aj. The displeasure of the i-th walrus is equal to the number of walruses between him and the furthest walrus ahead of him, which is younger than the i-th one. That is, the further that young walrus stands from him, the stronger the displeasure is.
The airport manager asked you to count for each of n walruses in the queue his displeasure.
Input
The first line contains an integer n (2 ≤ n ≤ 105) — the number of walruses in the queue. The second line contains integers ai (1 ≤ ai ≤ 109).
Note that some walruses can have the same age but for the displeasure to emerge the walrus that is closer to the head of the queue needs to be strictly younger than the other one.
Output
Print n numbers: if the i-th walrus is pleased with everything, print "-1" (without the quotes). Otherwise, print the i-th walrus's displeasure: the number of other walruses that stand between him and the furthest from him younger walrus.
Examples
Input
6
10 8 5 3 50 45
Output
2 1 0 -1 0 -1
Input
7
10 4 6 3 2 8 15
Output
4 2 1 0 -1 -1 -1
Input
5
10 3 1 10 11
Output
1 0 -1 -1 -1
Submitted Solution:
```
n = int(input())
l = [int(x) for x in input().split()]
stack = [-1]
l = l[-1::-1]
mini = l[0]
maxi= l[0]
minin = 0
maxin = 0
for i in range(1,n):
if l[i]<mini:
mini = l[i]
minin = i
stack.append(-1)
elif l[i]>maxi:
maxi = l[i]
maxin = i
stack.append(i-1)
else:
for j in range(0, len(stack)):
if stack[j]==-1 and l[j]<l[i]:
stack.append(i-j-1)
break
stack = stack[-1::-1]
for i in stack:
print(i, end=' ')
```
No
| 95,005 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are n walruses standing in a queue in an airport. They are numbered starting from the queue's tail: the 1-st walrus stands at the end of the queue and the n-th walrus stands at the beginning of the queue. The i-th walrus has the age equal to ai.
The i-th walrus becomes displeased if there's a younger walrus standing in front of him, that is, if exists such j (i < j), that ai > aj. The displeasure of the i-th walrus is equal to the number of walruses between him and the furthest walrus ahead of him, which is younger than the i-th one. That is, the further that young walrus stands from him, the stronger the displeasure is.
The airport manager asked you to count for each of n walruses in the queue his displeasure.
Input
The first line contains an integer n (2 ≤ n ≤ 105) — the number of walruses in the queue. The second line contains integers ai (1 ≤ ai ≤ 109).
Note that some walruses can have the same age but for the displeasure to emerge the walrus that is closer to the head of the queue needs to be strictly younger than the other one.
Output
Print n numbers: if the i-th walrus is pleased with everything, print "-1" (without the quotes). Otherwise, print the i-th walrus's displeasure: the number of other walruses that stand between him and the furthest from him younger walrus.
Examples
Input
6
10 8 5 3 50 45
Output
2 1 0 -1 0 -1
Input
7
10 4 6 3 2 8 15
Output
4 2 1 0 -1 -1 -1
Input
5
10 3 1 10 11
Output
1 0 -1 -1 -1
Submitted Solution:
```
n=int(input())
a=list(map(int,input().split()))
b=[0]*n
c=[]
c1=0
for i in range(n-1,-1,-1):
if c==[] or a[i]<c[c1-1][0]:
c.append([a[i],i])
c1+=1
if i==n-1 or a[i]<a[i+1] and b[i+1]==-1:
b[i]=-1
elif a[i]==a[i+1]:
b[i]=b[i+1]
elif a[i]==c[c1-1][0]:
b[i]=-1
else:
m=round(c1/2)
k=m
while round(k)>0:
if c[m][0]>a[i]:
m+=round(k/2)
else:
m-=round(k/2)
k/=2
while m<c1-1 and c[m][0]>=a[i]:
m+=1
while m>0 and c[m-1][0]<a[i]:
m-=1
b[i]=c[m][1]-i-1
print(*b)
```
No
| 95,006 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We call an array almost increasing if we can erase not more than one element from it so that the array becomes strictly increasing (that is, every element is striclty greater than every element before it).
You are given an array a consisting of n elements. You are allowed to replace any element with any integer number (and you may do so any number of times you need). What is the minimum number of replacements you have to perform in order to make the array almost increasing?
Input
The first line contains one integer n (2 ≤ n ≤ 200000) — the number of elements in a.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the array a.
Output
Print the minimum number of replaces you have to perform so that a is almost increasing.
Examples
Input
5
5 4 3 2 1
Output
3
Input
5
1 2 8 9 5
Output
0
Submitted Solution:
```
def inCreasingSeq(n,seq,startIndex):
for i in range(startIndex,n-1):
if(seq[i] > seq[i+1]):
return i+1
return -1
def replace(index,seq):
seq[index] = seq[index - 1]
return
def solve(n,seq):
sortedList = list(sorted(seq))
if(sortedList == seq):
print("0")
return
else:
nbReplace = 0
ind = inCreasingSeq(n,seq,0)
while ind != -1:
replace(ind,seq)
nbReplace+=1
ind = inCreasingSeq(n,seq,ind)
print(nbReplace-1)
nbT = int (input())
lis = list(map(int,input().split()))
solve(nbT,lis)
```
No
| 95,007 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We call an array almost increasing if we can erase not more than one element from it so that the array becomes strictly increasing (that is, every element is striclty greater than every element before it).
You are given an array a consisting of n elements. You are allowed to replace any element with any integer number (and you may do so any number of times you need). What is the minimum number of replacements you have to perform in order to make the array almost increasing?
Input
The first line contains one integer n (2 ≤ n ≤ 200000) — the number of elements in a.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the array a.
Output
Print the minimum number of replaces you have to perform so that a is almost increasing.
Examples
Input
5
5 4 3 2 1
Output
3
Input
5
1 2 8 9 5
Output
0
Submitted Solution:
```
def min_changes(li):
change, check = 0, 0
li_len = len(li)
if li_len in [0,1]:
return change
else:
check = li[0]
for i in range(1, len(li)):
if check >= li[i]:
li[i] = check +1
change += 1
check += 1
else:
check = li[i]
return change
n = input()
a = list(map(int,input().split()))
print (min_changes(a)-1)
```
No
| 95,008 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We call an array almost increasing if we can erase not more than one element from it so that the array becomes strictly increasing (that is, every element is striclty greater than every element before it).
You are given an array a consisting of n elements. You are allowed to replace any element with any integer number (and you may do so any number of times you need). What is the minimum number of replacements you have to perform in order to make the array almost increasing?
Input
The first line contains one integer n (2 ≤ n ≤ 200000) — the number of elements in a.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the array a.
Output
Print the minimum number of replaces you have to perform so that a is almost increasing.
Examples
Input
5
5 4 3 2 1
Output
3
Input
5
1 2 8 9 5
Output
0
Submitted Solution:
```
Number = int(input())
Numbers = input().split()
count = 0 ;
for i in range (0,Number-1) :
if (int(Numbers[i]) > int(Numbers[i+1])) :
count = count +1
print (count - 1)
```
No
| 95,009 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We call an array almost increasing if we can erase not more than one element from it so that the array becomes strictly increasing (that is, every element is striclty greater than every element before it).
You are given an array a consisting of n elements. You are allowed to replace any element with any integer number (and you may do so any number of times you need). What is the minimum number of replacements you have to perform in order to make the array almost increasing?
Input
The first line contains one integer n (2 ≤ n ≤ 200000) — the number of elements in a.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the array a.
Output
Print the minimum number of replaces you have to perform so that a is almost increasing.
Examples
Input
5
5 4 3 2 1
Output
3
Input
5
1 2 8 9 5
Output
0
Submitted Solution:
```
def inCreasingSeq(n,seq,startIndex):
for i in range(startIndex,n-1):
if(seq[i] > seq[i+1]):
return i+1
return -1
def replace(index,seq):
seq[index] = seq[index - 1]
return
def solve(n,seq):
nbReplace = 0
ind = inCreasingSeq(n,seq,0)
while ind != -1:
replace(ind,seq)
nbReplace+=1
ind = inCreasingSeq(n,seq,ind)
print(max(0,nbReplace-1))
nbT = int (input())
lis = list(map(int,input().split()))
solve(nbT,lis)
```
No
| 95,010 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Hag is a very talented person. He has always had an artist inside him but his father forced him to study mechanical engineering.
Yesterday he spent all of his time cutting a giant piece of wood trying to make it look like a goose. Anyway, his dad found out that he was doing arts rather than studying mechanics and other boring subjects. He confronted Hag with the fact that he is a spoiled son that does not care about his future, and if he continues to do arts he will cut his 25 Lira monthly allowance.
Hag is trying to prove to his dad that the wooden piece is a project for mechanics subject. He also told his dad that the wooden piece is a strictly convex polygon with n vertices.
Hag brought two pins and pinned the polygon with them in the 1-st and 2-nd vertices to the wall. His dad has q queries to Hag of two types.
* 1 f t: pull a pin from the vertex f, wait for the wooden polygon to rotate under the gravity force (if it will rotate) and stabilize. And then put the pin in vertex t.
* 2 v: answer what are the coordinates of the vertex v.
Please help Hag to answer his father's queries.
You can assume that the wood that forms the polygon has uniform density and the polygon has a positive thickness, same in all points. After every query of the 1-st type Hag's dad tries to move the polygon a bit and watches it stabilize again.
Input
The first line contains two integers n and q (3≤ n ≤ 10 000, 1 ≤ q ≤ 200000) — the number of vertices in the polygon and the number of queries.
The next n lines describe the wooden polygon, the i-th line contains two integers x_i and y_i (|x_i|, |y_i|≤ 10^8) — the coordinates of the i-th vertex of the polygon. It is guaranteed that polygon is strictly convex and the vertices are given in the counter-clockwise order and all vertices are distinct.
The next q lines describe the queries, one per line. Each query starts with its type 1 or 2. Each query of the first type continues with two integers f and t (1 ≤ f, t ≤ n) — the vertex the pin is taken from, and the vertex the pin is put to and the polygon finishes rotating. It is guaranteed that the vertex f contains a pin. Each query of the second type continues with a single integer v (1 ≤ v ≤ n) — the vertex the coordinates of which Hag should tell his father.
It is guaranteed that there is at least one query of the second type.
Output
The output should contain the answer to each query of second type — two numbers in a separate line. Your answer is considered correct, if its absolute or relative error does not exceed 10^{-4}.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-4}
Examples
Input
3 4
0 0
2 0
2 2
1 1 2
2 1
2 2
2 3
Output
3.4142135624 -1.4142135624
2.0000000000 0.0000000000
0.5857864376 -1.4142135624
Input
3 2
-1 1
0 0
1 1
1 1 2
2 1
Output
1.0000000000 -1.0000000000
Note
In the first test note the initial and the final state of the wooden polygon.
<image>
Red Triangle is the initial state and the green one is the triangle after rotation around (2,0).
In the second sample note that the polygon rotates 180 degrees counter-clockwise or clockwise direction (it does not matter), because Hag's father makes sure that the polygon is stable and his son does not trick him.
Tags: geometry
Correct Solution:
```
from math import hypot, atan2, cos, sin, sqrt, pi
import sys
mapInt = lambda:map(int,sys.stdin.buffer.readline().split())
N, Q = mapInt()
# Get points from input
points = {}
for n in range(N) :
x, y = mapInt()
points[n+1] = [x,y]
# Calculate COM (centroid)
centroid = [0,0]
A = 0
for n in range(1,N+1) :
w = points[n][0]*points[n%N+1][1] - points[n%N+1][0]*points[n][1]
centroid[0] += (points[n][0] + points[n%N+1][0])*w
centroid[1] += (points[n][1] + points[n%N+1][1])*w
A += w
centroid[0] /= 3*A
centroid[1] /= 3*A
# Move all points such that COM is at origin
for i in points.keys() :
points[i][0] -= centroid[0]
points[i][1] -= centroid[1]
# atan2(x, -y) to use negative y as reference
totAng = 0
pins = [1,2]
for q in range(Q) :
inp = list(mapInt())
if inp[0] == 1 :
f, t = inp[1],inp[2]
if pins[0] == pins[1] == f :
pins[0] = t
continue
elif pins[0] == f :
pivot = pins[1]
pins[0] = t
else :
pivot = pins[0]
pins[1] = t
x, y = points[pivot]
centroid[0] += sin(totAng + atan2(x,-y))*hypot(x,y)
centroid[1] += (-cos(totAng + atan2(x,-y)) - 1)*hypot(x,y)
totAng = pi-atan2(x,-y)
elif inp[0] == 2 :
x, y = points[inp[1]]
pointX = sin(totAng + atan2(x,-y))*hypot(x,y) + centroid[0]
pointY = -cos(totAng + atan2(x,-y))*hypot(x,y) + centroid[1]
print ("%0.20f %0.20f" % (pointX, pointY))
```
| 95,011 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Hag is a very talented person. He has always had an artist inside him but his father forced him to study mechanical engineering.
Yesterday he spent all of his time cutting a giant piece of wood trying to make it look like a goose. Anyway, his dad found out that he was doing arts rather than studying mechanics and other boring subjects. He confronted Hag with the fact that he is a spoiled son that does not care about his future, and if he continues to do arts he will cut his 25 Lira monthly allowance.
Hag is trying to prove to his dad that the wooden piece is a project for mechanics subject. He also told his dad that the wooden piece is a strictly convex polygon with n vertices.
Hag brought two pins and pinned the polygon with them in the 1-st and 2-nd vertices to the wall. His dad has q queries to Hag of two types.
* 1 f t: pull a pin from the vertex f, wait for the wooden polygon to rotate under the gravity force (if it will rotate) and stabilize. And then put the pin in vertex t.
* 2 v: answer what are the coordinates of the vertex v.
Please help Hag to answer his father's queries.
You can assume that the wood that forms the polygon has uniform density and the polygon has a positive thickness, same in all points. After every query of the 1-st type Hag's dad tries to move the polygon a bit and watches it stabilize again.
Input
The first line contains two integers n and q (3≤ n ≤ 10 000, 1 ≤ q ≤ 200000) — the number of vertices in the polygon and the number of queries.
The next n lines describe the wooden polygon, the i-th line contains two integers x_i and y_i (|x_i|, |y_i|≤ 10^8) — the coordinates of the i-th vertex of the polygon. It is guaranteed that polygon is strictly convex and the vertices are given in the counter-clockwise order and all vertices are distinct.
The next q lines describe the queries, one per line. Each query starts with its type 1 or 2. Each query of the first type continues with two integers f and t (1 ≤ f, t ≤ n) — the vertex the pin is taken from, and the vertex the pin is put to and the polygon finishes rotating. It is guaranteed that the vertex f contains a pin. Each query of the second type continues with a single integer v (1 ≤ v ≤ n) — the vertex the coordinates of which Hag should tell his father.
It is guaranteed that there is at least one query of the second type.
Output
The output should contain the answer to each query of second type — two numbers in a separate line. Your answer is considered correct, if its absolute or relative error does not exceed 10^{-4}.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-4}
Examples
Input
3 4
0 0
2 0
2 2
1 1 2
2 1
2 2
2 3
Output
3.4142135624 -1.4142135624
2.0000000000 0.0000000000
0.5857864376 -1.4142135624
Input
3 2
-1 1
0 0
1 1
1 1 2
2 1
Output
1.0000000000 -1.0000000000
Note
In the first test note the initial and the final state of the wooden polygon.
<image>
Red Triangle is the initial state and the green one is the triangle after rotation around (2,0).
In the second sample note that the polygon rotates 180 degrees counter-clockwise or clockwise direction (it does not matter), because Hag's father makes sure that the polygon is stable and his son does not trick him.
Tags: geometry
Correct Solution:
```
#!/usr/bin/env python3
from math import hypot
[n, q] = map(int, input().strip().split())
xys = [tuple(map(int, input().strip().split())) for _ in range(n)]
qis = [tuple(map(int, input().strip().split())) for _ in range(q)]
dxys = [(xys[(i + 1) % n][0] - xys[i][0], xys[(i + 1) % n][1] - xys[i][1]) for i in range(n)]
S = 3 * sum((x*dy - y*dx) for (x, y), (dx, dy) in zip(xys, dxys))
Sx = sum((dx + 2*x) * (x*dy - y*dx) for (x, y), (dx, dy) in zip(xys, dxys))
Sy = sum((dy + 2*y) * (x*dy - y*dx) for (x, y), (dx, dy) in zip(xys, dxys))
#Sy = sum((2*dx*dy + 3*x*dx + 3*x*dy + 6*x*y)*dy for (x, y), (dx, dy) in zip(xys, dxys))
for p in [2, 3]:
while S % p == Sx % p == Sy % p == 0:
S //= p
Sx //= p
Sy //= p
xyms = [(S*x - Sx, S*y - Sy) for x, y in xys]
hs = [hypot(x, y) for x, y in xyms]
def to_coord(x, y):
return (x + Sx) / S, (y + Sy) / S
hangs = (0, 1)
hang_on = None
cx, cy = 0.0, 0.0
# hang on u
def get_v(v):
if hang_on is None:
return xyms[v]
else:
ux, uy = xyms[hang_on]
vx, vy = xyms[v]
h = hs[hang_on]
return ((uy * vx - ux * vy) / h, (ux * vx + uy * vy) / h)
#def ss(v1, v2):
# return tuple(vi + vj for vi, vj in zip(v1, v2))
#def disp():
# print ('hangs on', hang_on, 'of', hangs)
# print ('center', to_coord(cx, cy))
# print ({i: to_coord(*ss(get_v(i), (cx, cy))) for i in range(n)})
#disp()
for qi in qis:
if qi[0] == 1:
_, f, t = qi # 1-indexation
s = hangs[1 - hangs.index(f - 1)]
dx, dy = get_v(s)
cx += dx
cy += dy - hs[s]
hang_on = s
hangs = (s, t - 1)
# print ('{} --> {}'.format(f - 1, t - 1))
# disp()
else:
_, v = qi # 1-indexation
dx, dy = get_v(v - 1)
print (*to_coord(cx + dx, cy + dy))
```
| 95,012 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Hag is a very talented person. He has always had an artist inside him but his father forced him to study mechanical engineering.
Yesterday he spent all of his time cutting a giant piece of wood trying to make it look like a goose. Anyway, his dad found out that he was doing arts rather than studying mechanics and other boring subjects. He confronted Hag with the fact that he is a spoiled son that does not care about his future, and if he continues to do arts he will cut his 25 Lira monthly allowance.
Hag is trying to prove to his dad that the wooden piece is a project for mechanics subject. He also told his dad that the wooden piece is a strictly convex polygon with n vertices.
Hag brought two pins and pinned the polygon with them in the 1-st and 2-nd vertices to the wall. His dad has q queries to Hag of two types.
* 1 f t: pull a pin from the vertex f, wait for the wooden polygon to rotate under the gravity force (if it will rotate) and stabilize. And then put the pin in vertex t.
* 2 v: answer what are the coordinates of the vertex v.
Please help Hag to answer his father's queries.
You can assume that the wood that forms the polygon has uniform density and the polygon has a positive thickness, same in all points. After every query of the 1-st type Hag's dad tries to move the polygon a bit and watches it stabilize again.
Input
The first line contains two integers n and q (3≤ n ≤ 10 000, 1 ≤ q ≤ 200000) — the number of vertices in the polygon and the number of queries.
The next n lines describe the wooden polygon, the i-th line contains two integers x_i and y_i (|x_i|, |y_i|≤ 10^8) — the coordinates of the i-th vertex of the polygon. It is guaranteed that polygon is strictly convex and the vertices are given in the counter-clockwise order and all vertices are distinct.
The next q lines describe the queries, one per line. Each query starts with its type 1 or 2. Each query of the first type continues with two integers f and t (1 ≤ f, t ≤ n) — the vertex the pin is taken from, and the vertex the pin is put to and the polygon finishes rotating. It is guaranteed that the vertex f contains a pin. Each query of the second type continues with a single integer v (1 ≤ v ≤ n) — the vertex the coordinates of which Hag should tell his father.
It is guaranteed that there is at least one query of the second type.
Output
The output should contain the answer to each query of second type — two numbers in a separate line. Your answer is considered correct, if its absolute or relative error does not exceed 10^{-4}.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-4}
Examples
Input
3 4
0 0
2 0
2 2
1 1 2
2 1
2 2
2 3
Output
3.4142135624 -1.4142135624
2.0000000000 0.0000000000
0.5857864376 -1.4142135624
Input
3 2
-1 1
0 0
1 1
1 1 2
2 1
Output
1.0000000000 -1.0000000000
Note
In the first test note the initial and the final state of the wooden polygon.
<image>
Red Triangle is the initial state and the green one is the triangle after rotation around (2,0).
In the second sample note that the polygon rotates 180 degrees counter-clockwise or clockwise direction (it does not matter), because Hag's father makes sure that the polygon is stable and his son does not trick him.
Tags: geometry
Correct Solution:
```
import sys
read=lambda:map(int,sys.stdin.buffer.readline().split())
from math import *
n,q=read()
x,y=[],[]
cx,cy=0,0
deg=0
p1,p2=0,1
def pos(i):
return (cx+x[i]*cos(deg)-y[i]*sin(deg),cy+y[i]*cos(deg)+x[i]*sin(deg))
for i in range(n):
_x,_y=read()
x.append(_x);y.append(_y)
s=0
for i in range(2,n):
_s=(x[i]-x[0])*(y[i-1]-y[0])-(y[i]-y[0])*(x[i-1]-x[0])
cx+=(x[0]+x[i]+x[i-1])/3*_s
cy+=(y[0]+y[i]+y[i-1])/3*_s
s+=_s
cx/=s;cy/=s
for i in range(n): x[i]-=cx;y[i]-=cy
for i in range(q):
v=list(read())
if v[0]==1:
if p1==v[1]-1: p1,p2=p2,None
else: p1,p2=p1,None
cx,cy=pos(p1);cy-=hypot(x[p1],y[p1])
deg=pi/2-atan2(y[p1],x[p1]);p2=v[2]-1
else:
print("%.15f %.15f"%pos(v[1]-1))
```
| 95,013 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Hag is a very talented person. He has always had an artist inside him but his father forced him to study mechanical engineering.
Yesterday he spent all of his time cutting a giant piece of wood trying to make it look like a goose. Anyway, his dad found out that he was doing arts rather than studying mechanics and other boring subjects. He confronted Hag with the fact that he is a spoiled son that does not care about his future, and if he continues to do arts he will cut his 25 Lira monthly allowance.
Hag is trying to prove to his dad that the wooden piece is a project for mechanics subject. He also told his dad that the wooden piece is a strictly convex polygon with n vertices.
Hag brought two pins and pinned the polygon with them in the 1-st and 2-nd vertices to the wall. His dad has q queries to Hag of two types.
* 1 f t: pull a pin from the vertex f, wait for the wooden polygon to rotate under the gravity force (if it will rotate) and stabilize. And then put the pin in vertex t.
* 2 v: answer what are the coordinates of the vertex v.
Please help Hag to answer his father's queries.
You can assume that the wood that forms the polygon has uniform density and the polygon has a positive thickness, same in all points. After every query of the 1-st type Hag's dad tries to move the polygon a bit and watches it stabilize again.
Input
The first line contains two integers n and q (3≤ n ≤ 10 000, 1 ≤ q ≤ 200000) — the number of vertices in the polygon and the number of queries.
The next n lines describe the wooden polygon, the i-th line contains two integers x_i and y_i (|x_i|, |y_i|≤ 10^8) — the coordinates of the i-th vertex of the polygon. It is guaranteed that polygon is strictly convex and the vertices are given in the counter-clockwise order and all vertices are distinct.
The next q lines describe the queries, one per line. Each query starts with its type 1 or 2. Each query of the first type continues with two integers f and t (1 ≤ f, t ≤ n) — the vertex the pin is taken from, and the vertex the pin is put to and the polygon finishes rotating. It is guaranteed that the vertex f contains a pin. Each query of the second type continues with a single integer v (1 ≤ v ≤ n) — the vertex the coordinates of which Hag should tell his father.
It is guaranteed that there is at least one query of the second type.
Output
The output should contain the answer to each query of second type — two numbers in a separate line. Your answer is considered correct, if its absolute or relative error does not exceed 10^{-4}.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-4}
Examples
Input
3 4
0 0
2 0
2 2
1 1 2
2 1
2 2
2 3
Output
3.4142135624 -1.4142135624
2.0000000000 0.0000000000
0.5857864376 -1.4142135624
Input
3 2
-1 1
0 0
1 1
1 1 2
2 1
Output
1.0000000000 -1.0000000000
Note
In the first test note the initial and the final state of the wooden polygon.
<image>
Red Triangle is the initial state and the green one is the triangle after rotation around (2,0).
In the second sample note that the polygon rotates 180 degrees counter-clockwise or clockwise direction (it does not matter), because Hag's father makes sure that the polygon is stable and his son does not trick him.
Tags: geometry
Correct Solution:
```
from sys import stdin, stdout
from math import atan2, sqrt, sin, cos, pi
def main():
n, q = stdin.readline().split()
n = int(n)
q = int(q)
lines = stdin.readlines()
x = [0] * (n + 1)
y = [0] * (n + 1)
for i in range(n):
x[i], y[i] = lines[i].split()
x[i] = int(x[i])
y[i] = int(y[i])
x[n] = x[0]
y[n] = y[0]
a = 0
cx = 0
cy = 0
for i in range(n):
ii = i + 1
a += x[i] * y[ii] - x[ii] * y[i]
cx += (x[i] + x[ii]) * (x[i] * y[ii] - x[ii] * y[i])
cy += (y[i] + y[ii]) * (x[i] * y[ii] - x[ii] * y[i])
a *= 3
r = [0.0] * n
z = [0.0] * n
for i in range(n):
x[i] = x[i] * a - cx
y[i] = y[i] * a - cy
r[i] = sqrt(x[i] * x[i] + y[i] * y[i]) / a
z[i] = atan2(y[i], x[i])
cx /= a
cy /= a
p = [0, 1]
mt = 0.0
for i in range(q):
tmp = lines[n + i].split()
ta = int(tmp[0])
tb = int(tmp[1])
if ta == 1:
ta = int(tmp[2])
ta -= 1
tb -= 1
if p[0] == tb:
p[0], p[1] = p[1], p[0]
p[1] = ta
tc = z[p[0]] + mt
cx += r[p[0]] * cos(tc)
cy += r[p[0]] * (sin(tc) - 1)
tc = pi / 2 - tc;
mt = pi / 2 - z[p[0]];
else:
tb -= 1
tc = z[tb] + mt
stdout.write('%.10lf %.10lf\n' % (cx + r[tb] * cos(tc), cy + r[tb] * sin(tc)))
if __name__ == '__main__':
main()
```
| 95,014 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Hag is a very talented person. He has always had an artist inside him but his father forced him to study mechanical engineering.
Yesterday he spent all of his time cutting a giant piece of wood trying to make it look like a goose. Anyway, his dad found out that he was doing arts rather than studying mechanics and other boring subjects. He confronted Hag with the fact that he is a spoiled son that does not care about his future, and if he continues to do arts he will cut his 25 Lira monthly allowance.
Hag is trying to prove to his dad that the wooden piece is a project for mechanics subject. He also told his dad that the wooden piece is a strictly convex polygon with n vertices.
Hag brought two pins and pinned the polygon with them in the 1-st and 2-nd vertices to the wall. His dad has q queries to Hag of two types.
* 1 f t: pull a pin from the vertex f, wait for the wooden polygon to rotate under the gravity force (if it will rotate) and stabilize. And then put the pin in vertex t.
* 2 v: answer what are the coordinates of the vertex v.
Please help Hag to answer his father's queries.
You can assume that the wood that forms the polygon has uniform density and the polygon has a positive thickness, same in all points. After every query of the 1-st type Hag's dad tries to move the polygon a bit and watches it stabilize again.
Input
The first line contains two integers n and q (3≤ n ≤ 10 000, 1 ≤ q ≤ 200000) — the number of vertices in the polygon and the number of queries.
The next n lines describe the wooden polygon, the i-th line contains two integers x_i and y_i (|x_i|, |y_i|≤ 10^8) — the coordinates of the i-th vertex of the polygon. It is guaranteed that polygon is strictly convex and the vertices are given in the counter-clockwise order and all vertices are distinct.
The next q lines describe the queries, one per line. Each query starts with its type 1 or 2. Each query of the first type continues with two integers f and t (1 ≤ f, t ≤ n) — the vertex the pin is taken from, and the vertex the pin is put to and the polygon finishes rotating. It is guaranteed that the vertex f contains a pin. Each query of the second type continues with a single integer v (1 ≤ v ≤ n) — the vertex the coordinates of which Hag should tell his father.
It is guaranteed that there is at least one query of the second type.
Output
The output should contain the answer to each query of second type — two numbers in a separate line. Your answer is considered correct, if its absolute or relative error does not exceed 10^{-4}.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-4}
Examples
Input
3 4
0 0
2 0
2 2
1 1 2
2 1
2 2
2 3
Output
3.4142135624 -1.4142135624
2.0000000000 0.0000000000
0.5857864376 -1.4142135624
Input
3 2
-1 1
0 0
1 1
1 1 2
2 1
Output
1.0000000000 -1.0000000000
Note
In the first test note the initial and the final state of the wooden polygon.
<image>
Red Triangle is the initial state and the green one is the triangle after rotation around (2,0).
In the second sample note that the polygon rotates 180 degrees counter-clockwise or clockwise direction (it does not matter), because Hag's father makes sure that the polygon is stable and his son does not trick him.
Tags: geometry
Correct Solution:
```
from sys import stdin
from math import *
rl = lambda l: tuple(map(int, l.split()))
rd = lambda: rl(input())
class Point():
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, pt):
return Point(self.x + pt.x, self.y + pt.y)
def __iadd__(self, pt):
return self + pt
def __mul__(self, n):
return Point(self.x * n, self.y * n)
def __imul__(self, n):
return self * n
def __truediv__(self, n):
return Point(self.x / n, self.y / n)
def __itruediv__(self, n):
return self / n
def __str__(self):
return '%f %f' % (self.x, self.y)
def distance(self, pt):
return sqrt((self.x - pt.x) ** 2 + (self.y - pt.y) ** 2)
def angle(self, pt):
det_x = pt.x - self.x
det_y = pt.y - self.y
if det_x == 0:
if det_y > 0:
return pi / 2
if det_y < 0:
return pi / 2 * 3
if det_y == 0:
raise Exception('Fail to find angle between two identical points.')
if det_x > 0:
return atan(det_y / det_x)
if det_x < 0:
return atan(det_y / det_x) + pi
def get_pt_by_dis_ang(self, dis, ang):
return Point(self.x + cos(ang) * dis, self.y + sin(ang) * dis)
class Convex_polygon():
def __init__(self):
self.points = []
self.core = None
self.dis = None
self.base_ang = 0
self.ang = None
self.pin = [0, 1]
def add(self, pt):
self.points.append(pt)
def update(self):
self.update_core()
self.update_dis()
self.update_ang()
def cal_core_of_triangle(self, i, j, k):
s = (self.points[j].x - self.points[i].x) * (self.points[k].y - self.points[i].y)
s -= (self.points[j].y - self.points[i].y) * (self.points[k].x - self.points[i].x)
return s, (self.points[i] + self.points[j] + self.points[k]) / 3 * s
def update_core(self):
self.core = Point(0, 0)
s = 0
for i in range(2, len(P.points)):
det_s, det_core = self.cal_core_of_triangle(0, i, i - 1)
self.core += det_core
s += det_s
self.core /= s
def update_dis(self):
self.dis = []
for pt in self.points:
self.dis.append(self.core.distance(pt))
def update_ang(self):
self.ang = []
for pt in self.points:
self.ang.append(self.core.angle(pt))
def get_pt(self, i):
return self.core.get_pt_by_dis_ang(self.dis[i], self.base_ang + self.ang[i])
def __str__(self):
return '\n'.join(('points: ' + '[' + ', '.join(str(pt) for pt in self.points) + ']',
'core: ' + str(self.core),
'dis: ' + str(self.dis),
'base_ang ' + str(self.base_ang),
'ang ' + str(self.ang),
'pin ' + str(self.pin)))
n, q = rd()
P = Convex_polygon()
for _ in range(n):
P.add(Point(*rd()))
P.update()
for _ in stdin.readlines():
s = rl(_)
if s[0] == 1:
__, f, t = s
f -= 1
t -= 1
P.pin.remove(f)
i = P.pin[0]
top_pt = P.get_pt(i)
P.core = Point(top_pt.x, top_pt.y - P.dis[i])
P.base_ang = pi / 2 - P.ang[i]
P.pin.append(t)
else:
__, v = s
v -= 1
print(P.get_pt(v))
```
| 95,015 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Hag is a very talented person. He has always had an artist inside him but his father forced him to study mechanical engineering.
Yesterday he spent all of his time cutting a giant piece of wood trying to make it look like a goose. Anyway, his dad found out that he was doing arts rather than studying mechanics and other boring subjects. He confronted Hag with the fact that he is a spoiled son that does not care about his future, and if he continues to do arts he will cut his 25 Lira monthly allowance.
Hag is trying to prove to his dad that the wooden piece is a project for mechanics subject. He also told his dad that the wooden piece is a strictly convex polygon with n vertices.
Hag brought two pins and pinned the polygon with them in the 1-st and 2-nd vertices to the wall. His dad has q queries to Hag of two types.
* 1 f t: pull a pin from the vertex f, wait for the wooden polygon to rotate under the gravity force (if it will rotate) and stabilize. And then put the pin in vertex t.
* 2 v: answer what are the coordinates of the vertex v.
Please help Hag to answer his father's queries.
You can assume that the wood that forms the polygon has uniform density and the polygon has a positive thickness, same in all points. After every query of the 1-st type Hag's dad tries to move the polygon a bit and watches it stabilize again.
Input
The first line contains two integers n and q (3≤ n ≤ 10 000, 1 ≤ q ≤ 200000) — the number of vertices in the polygon and the number of queries.
The next n lines describe the wooden polygon, the i-th line contains two integers x_i and y_i (|x_i|, |y_i|≤ 10^8) — the coordinates of the i-th vertex of the polygon. It is guaranteed that polygon is strictly convex and the vertices are given in the counter-clockwise order and all vertices are distinct.
The next q lines describe the queries, one per line. Each query starts with its type 1 or 2. Each query of the first type continues with two integers f and t (1 ≤ f, t ≤ n) — the vertex the pin is taken from, and the vertex the pin is put to and the polygon finishes rotating. It is guaranteed that the vertex f contains a pin. Each query of the second type continues with a single integer v (1 ≤ v ≤ n) — the vertex the coordinates of which Hag should tell his father.
It is guaranteed that there is at least one query of the second type.
Output
The output should contain the answer to each query of second type — two numbers in a separate line. Your answer is considered correct, if its absolute or relative error does not exceed 10^{-4}.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-4}
Examples
Input
3 4
0 0
2 0
2 2
1 1 2
2 1
2 2
2 3
Output
3.4142135624 -1.4142135624
2.0000000000 0.0000000000
0.5857864376 -1.4142135624
Input
3 2
-1 1
0 0
1 1
1 1 2
2 1
Output
1.0000000000 -1.0000000000
Note
In the first test note the initial and the final state of the wooden polygon.
<image>
Red Triangle is the initial state and the green one is the triangle after rotation around (2,0).
In the second sample note that the polygon rotates 180 degrees counter-clockwise or clockwise direction (it does not matter), because Hag's father makes sure that the polygon is stable and his son does not trick him.
Tags: geometry
Correct Solution:
```
from sys import*
from math import*
def main():
n, q = stdin.readline().split()
n = int(n)
q = int(q)
lines = stdin.readlines()
x = [0] * (n + 1)
y = [0] * (n + 1)
for i in range(n):
x[i], y[i] = lines[i].split()
x[i] = int(x[i])
y[i] = int(y[i])
x[n] = x[0]
y[n] = y[0]
a = 0
cx = 0
cy = 0
for i in range(n):
ii = i + 1
a += x[i] * y[ii] - x[ii] * y[i]
cx += (x[i] + x[ii]) * (x[i] * y[ii] - x[ii] * y[i])
cy += (y[i] + y[ii]) * (x[i] * y[ii] - x[ii] * y[i])
a *= 3
r = [0.0] * n
z = [0.0] * n
for i in range(n):
x[i] = x[i] * a - cx
y[i] = y[i] * a - cy
r[i] = sqrt(x[i] * x[i] + y[i] * y[i]) / a
z[i] = atan2(y[i], x[i])
cx /= a
cy /= a
p = [0, 1]
mt = 0.0
for i in range(q):
tmp = lines[n + i].split()
ta = int(tmp[0])
tb = int(tmp[1])
if ta == 1:
ta = int(tmp[2])
ta -= 1
tb -= 1
if p[0] == tb:
p[0], p[1] = p[1], p[0]
p[1] = ta
tc = z[p[0]] + mt
cx += r[p[0]] * cos(tc)
cy += r[p[0]] * (sin(tc) - 1)
tc = pi / 2 - tc;
mt = pi / 2 - z[p[0]];
else:
tb -= 1
tc = z[tb] + mt
stdout.write('%.10lf %.10lf\n' % (cx + r[tb] * cos(tc), cy + r[tb] * sin(tc)))
if __name__ == '__main__':
main()
```
| 95,016 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Hag is a very talented person. He has always had an artist inside him but his father forced him to study mechanical engineering.
Yesterday he spent all of his time cutting a giant piece of wood trying to make it look like a goose. Anyway, his dad found out that he was doing arts rather than studying mechanics and other boring subjects. He confronted Hag with the fact that he is a spoiled son that does not care about his future, and if he continues to do arts he will cut his 25 Lira monthly allowance.
Hag is trying to prove to his dad that the wooden piece is a project for mechanics subject. He also told his dad that the wooden piece is a strictly convex polygon with n vertices.
Hag brought two pins and pinned the polygon with them in the 1-st and 2-nd vertices to the wall. His dad has q queries to Hag of two types.
* 1 f t: pull a pin from the vertex f, wait for the wooden polygon to rotate under the gravity force (if it will rotate) and stabilize. And then put the pin in vertex t.
* 2 v: answer what are the coordinates of the vertex v.
Please help Hag to answer his father's queries.
You can assume that the wood that forms the polygon has uniform density and the polygon has a positive thickness, same in all points. After every query of the 1-st type Hag's dad tries to move the polygon a bit and watches it stabilize again.
Input
The first line contains two integers n and q (3≤ n ≤ 10 000, 1 ≤ q ≤ 200000) — the number of vertices in the polygon and the number of queries.
The next n lines describe the wooden polygon, the i-th line contains two integers x_i and y_i (|x_i|, |y_i|≤ 10^8) — the coordinates of the i-th vertex of the polygon. It is guaranteed that polygon is strictly convex and the vertices are given in the counter-clockwise order and all vertices are distinct.
The next q lines describe the queries, one per line. Each query starts with its type 1 or 2. Each query of the first type continues with two integers f and t (1 ≤ f, t ≤ n) — the vertex the pin is taken from, and the vertex the pin is put to and the polygon finishes rotating. It is guaranteed that the vertex f contains a pin. Each query of the second type continues with a single integer v (1 ≤ v ≤ n) — the vertex the coordinates of which Hag should tell his father.
It is guaranteed that there is at least one query of the second type.
Output
The output should contain the answer to each query of second type — two numbers in a separate line. Your answer is considered correct, if its absolute or relative error does not exceed 10^{-4}.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-4}
Examples
Input
3 4
0 0
2 0
2 2
1 1 2
2 1
2 2
2 3
Output
3.4142135624 -1.4142135624
2.0000000000 0.0000000000
0.5857864376 -1.4142135624
Input
3 2
-1 1
0 0
1 1
1 1 2
2 1
Output
1.0000000000 -1.0000000000
Note
In the first test note the initial and the final state of the wooden polygon.
<image>
Red Triangle is the initial state and the green one is the triangle after rotation around (2,0).
In the second sample note that the polygon rotates 180 degrees counter-clockwise or clockwise direction (it does not matter), because Hag's father makes sure that the polygon is stable and his son does not trick him.
Tags: geometry
Correct Solution:
```
import math;
#Вычисление координаты точки по координатам центра, углу, и начальным относительно центра
def getCoordinate(gx, gy, alpha, x, y):
x1=gx+x*math.cos(alpha)-y*math.sin(alpha);
y1=gy+x*math.sin(alpha)+y*math.cos(alpha);
return x1, y1
#Вычисление угла, на который надо повернуть точку с координатами x, y,
#чтобы она оказалась прямо над gx, gy
def getAngle(gx, gy, x, y):
x=x-gx;
y=y-gy;
cos=x/math.sqrt(x**2+y**2);
alpha=math.acos(cos);
if y<0:
alpha=-alpha;
return math.pi/2-alpha;
n, q = map(int, input().split(' '));
x=[0]*n;
y=[0]*n;
for i in range(n):
x[i], y[i]=map(int, input().split(' '));
r=[0]*q;
f=[0]*q;
t=[0]*q;
v=[0]*q;
for i in range(q):
l=list(map(int, input().split(' ')));
r[i]=l[0];
if r[i]==1:
f[i]=l[1]-1;
t[i]=l[2]-1;
else:
v[i]=l[1]-1;
gx=0;
gy=0;
s=0;
for i in range(n):
ip=i+1;
if ip==n:
ip=0;
ds=x[i]*y[ip]-x[ip]*y[i];
s+=ds;
gx+=(x[i]+x[ip])*ds;
gy+=(y[i]+y[ip])*ds;
s/=2;
gx/=6*s;
gy/=6*s;
angles=[0]*n;
for i in range(n):
angles[i]=getAngle(gx, gy, x[i], y[i]);
for i in range(n):
x[i]-=gx;
y[i]-=gy;
alpha=0;
#print('pos',gx, gy, alpha);
#Восстанавливать положение точек будем по центру масс и углу
#Угол - поворот против часовой вокруг центра масс
fix={0, 1}
for i in range(q):
if r[i]==2:
currX, currY = getCoordinate(gx, gy, alpha, x[v[i]], y[v[i]]);
print("%.6f %.6f"%(currX, currY))
else:
if len(fix)==2:
fix.remove(f[i]);
#print('remove',f[i])
#j - единственный элемент в множестве
for j in fix:
#print(j);
currX, currY = getCoordinate(gx, gy, alpha, x[j], y[j]);
#print('fix:', currX, currY)
#dalpha=getAngle(gx, gy, currX, currY);
#alpha+=dalpha;
alpha=angles[j];
#Чтобы вычислить новые координаты g, нуно повернуть ее на угол
#dalpha относительно currX, currY
gx, gy=currX, currY-math.sqrt(x[j]**2+y[j]**2);
#print('pos',gx, gy, alpha/math.pi)
fix.add(t[i]);
```
| 95,017 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Hag is a very talented person. He has always had an artist inside him but his father forced him to study mechanical engineering.
Yesterday he spent all of his time cutting a giant piece of wood trying to make it look like a goose. Anyway, his dad found out that he was doing arts rather than studying mechanics and other boring subjects. He confronted Hag with the fact that he is a spoiled son that does not care about his future, and if he continues to do arts he will cut his 25 Lira monthly allowance.
Hag is trying to prove to his dad that the wooden piece is a project for mechanics subject. He also told his dad that the wooden piece is a strictly convex polygon with n vertices.
Hag brought two pins and pinned the polygon with them in the 1-st and 2-nd vertices to the wall. His dad has q queries to Hag of two types.
* 1 f t: pull a pin from the vertex f, wait for the wooden polygon to rotate under the gravity force (if it will rotate) and stabilize. And then put the pin in vertex t.
* 2 v: answer what are the coordinates of the vertex v.
Please help Hag to answer his father's queries.
You can assume that the wood that forms the polygon has uniform density and the polygon has a positive thickness, same in all points. After every query of the 1-st type Hag's dad tries to move the polygon a bit and watches it stabilize again.
Input
The first line contains two integers n and q (3≤ n ≤ 10 000, 1 ≤ q ≤ 200000) — the number of vertices in the polygon and the number of queries.
The next n lines describe the wooden polygon, the i-th line contains two integers x_i and y_i (|x_i|, |y_i|≤ 10^8) — the coordinates of the i-th vertex of the polygon. It is guaranteed that polygon is strictly convex and the vertices are given in the counter-clockwise order and all vertices are distinct.
The next q lines describe the queries, one per line. Each query starts with its type 1 or 2. Each query of the first type continues with two integers f and t (1 ≤ f, t ≤ n) — the vertex the pin is taken from, and the vertex the pin is put to and the polygon finishes rotating. It is guaranteed that the vertex f contains a pin. Each query of the second type continues with a single integer v (1 ≤ v ≤ n) — the vertex the coordinates of which Hag should tell his father.
It is guaranteed that there is at least one query of the second type.
Output
The output should contain the answer to each query of second type — two numbers in a separate line. Your answer is considered correct, if its absolute or relative error does not exceed 10^{-4}.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-4}
Examples
Input
3 4
0 0
2 0
2 2
1 1 2
2 1
2 2
2 3
Output
3.4142135624 -1.4142135624
2.0000000000 0.0000000000
0.5857864376 -1.4142135624
Input
3 2
-1 1
0 0
1 1
1 1 2
2 1
Output
1.0000000000 -1.0000000000
Note
In the first test note the initial and the final state of the wooden polygon.
<image>
Red Triangle is the initial state and the green one is the triangle after rotation around (2,0).
In the second sample note that the polygon rotates 180 degrees counter-clockwise or clockwise direction (it does not matter), because Hag's father makes sure that the polygon is stable and his son does not trick him.
Submitted Solution:
```
# -*- coding: utf-8 -*-
"""
Created on Tue Dec 3 11:47:37 2019
@author: CronuS
"""
def mult (m1, m2):
return [[sum([m1[i][k] * m2[k][l] for k in range(3)]) for l in range(3)] for i in range(len(m1))]
def getmatr (xc, yc, xi, yi, matr):
xc_ = matr[0][0] * xc + matr[1][0] * yc + matr[2][0]
yc_ = matr[0][1] * xc + matr[1][1] * yc + matr[2][1]
xi_ = matr[0][0] * xi + matr[1][0] * yi + matr[2][0]
yi_ = matr[0][1] * xi + matr[1][1] * yi + matr[2][1]
s_ = ((xc_ - xi_) ** 2 + (yc_ - yi_) ** 2) ** 0.5
cosa_ = (yi_ - yc_) / s_
sina_ = -(xi_ - xc_) / s_
matrnew = [[cosa_, -sina_, 0], [sina_, cosa_, 0], [-xi_ * cosa_ - yi_ * sina_ + xi_, xi_ * sina_ + yi_ * cosa_ + yi_, 1]]
matrnew = mult(matr, matrnew)
return matrnew
#print(mult(matr0, matr0))
s = list(map(int, input().split()))
n = s[0]; q = s[1]
p = []
mx = 0; my = 0; m = 0
k1 = 1; k2 = 2
for i in range(n):
p = p + [list(map(int, input().split()))]
if (i > 2):
mxi = (p[0][0] + p[i - 1][0] + p[i][0]) / 3
myi = (p[0][1] + p[i - 1][1] + p[i][1]) / 3
mi = abs((p[i - 1][0] - p[0][0]) * (p[i][1] - p[0][1]) - (p[i][0] - p[0][0]) * (p[i - 1][1] - p[0][1]))
mx = (mx * m + mxi * mi) / (m + mi)
my = (my * m + myi * mi) / (m + mi)
m = m + mi
elif (i == 2):
mx = (p[0][0] + p[1][0] + p[2][0]) / 3
my = (p[0][1] + p[1][1] + p[2][1]) / 3
m = abs((p[1][0] - p[0][0]) * (p[2][1] - p[0][1]) - (p[2][0] - p[0][0]) * (p[1][1] - p[0][1]))
p1, p2 = 1, 2
matr = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
ii = 0
for i in range(q):
z = list(map(int, input().split()))
if (z[0] == 1):
f = z[1]; t = z[2]
if (f == p1):
p1 = t
ii = p2 - 1
pass
else:
p2 = t
ii = p1 - 1
pass
matr = getmatr(mx, my, p[ii][0], p[ii][1], matr)
else:
ii = z[1] - 1
m = mult([[p[ii][0], p[ii][1], 1]], matr)
print(m[0][0], m[0][1])
#print(matr)
```
No
| 95,018 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Hag is a very talented person. He has always had an artist inside him but his father forced him to study mechanical engineering.
Yesterday he spent all of his time cutting a giant piece of wood trying to make it look like a goose. Anyway, his dad found out that he was doing arts rather than studying mechanics and other boring subjects. He confronted Hag with the fact that he is a spoiled son that does not care about his future, and if he continues to do arts he will cut his 25 Lira monthly allowance.
Hag is trying to prove to his dad that the wooden piece is a project for mechanics subject. He also told his dad that the wooden piece is a strictly convex polygon with n vertices.
Hag brought two pins and pinned the polygon with them in the 1-st and 2-nd vertices to the wall. His dad has q queries to Hag of two types.
* 1 f t: pull a pin from the vertex f, wait for the wooden polygon to rotate under the gravity force (if it will rotate) and stabilize. And then put the pin in vertex t.
* 2 v: answer what are the coordinates of the vertex v.
Please help Hag to answer his father's queries.
You can assume that the wood that forms the polygon has uniform density and the polygon has a positive thickness, same in all points. After every query of the 1-st type Hag's dad tries to move the polygon a bit and watches it stabilize again.
Input
The first line contains two integers n and q (3≤ n ≤ 10 000, 1 ≤ q ≤ 200000) — the number of vertices in the polygon and the number of queries.
The next n lines describe the wooden polygon, the i-th line contains two integers x_i and y_i (|x_i|, |y_i|≤ 10^8) — the coordinates of the i-th vertex of the polygon. It is guaranteed that polygon is strictly convex and the vertices are given in the counter-clockwise order and all vertices are distinct.
The next q lines describe the queries, one per line. Each query starts with its type 1 or 2. Each query of the first type continues with two integers f and t (1 ≤ f, t ≤ n) — the vertex the pin is taken from, and the vertex the pin is put to and the polygon finishes rotating. It is guaranteed that the vertex f contains a pin. Each query of the second type continues with a single integer v (1 ≤ v ≤ n) — the vertex the coordinates of which Hag should tell his father.
It is guaranteed that there is at least one query of the second type.
Output
The output should contain the answer to each query of second type — two numbers in a separate line. Your answer is considered correct, if its absolute or relative error does not exceed 10^{-4}.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-4}
Examples
Input
3 4
0 0
2 0
2 2
1 1 2
2 1
2 2
2 3
Output
3.4142135624 -1.4142135624
2.0000000000 0.0000000000
0.5857864376 -1.4142135624
Input
3 2
-1 1
0 0
1 1
1 1 2
2 1
Output
1.0000000000 -1.0000000000
Note
In the first test note the initial and the final state of the wooden polygon.
<image>
Red Triangle is the initial state and the green one is the triangle after rotation around (2,0).
In the second sample note that the polygon rotates 180 degrees counter-clockwise or clockwise direction (it does not matter), because Hag's father makes sure that the polygon is stable and his son does not trick him.
Submitted Solution:
```
from sys import stdin, stdout
from math import atan2, sqrt, sin, cos, pi
def main():
n, q = stdin.readline().split()
n = int(n)
q = int(q)
lines = stdin.readlines()
x = [0] * (n + 1)
y = [0] * (n + 1)
for i in range(n):
x[i], y[i] = lines[i].split()
x[i] = int(x[i])
y[i] = int(y[i])
x[n] = x[0]
y[n] = y[0]
a = 0
cx = 0
cy = 0
for i in range(n):
ii = i + 1
a += x[i] * y[ii] - x[ii] * y[i]
cx += (x[i] + x[ii]) * (x[i] * y[ii] - x[ii] * y[i])
cy += (y[i] + y[ii]) * (x[i] * y[ii] - x[ii] * y[i])
a *= 3
r = [0.0] * n
z = [0.0] * n
for i in range(n):
x[i] = x[i] * a - cx
y[i] = y[i] * a - cy
r[i] = sqrt(x[i] * x[i] + y[i] * y[i]) / a
z[i] = atan2(y[i], x[i])
cx /= a
cy /= a
p = [0, 1]
mt = 0.0
for i in range(q):
tmp = lines[n + i].split()
ta = int(tmp[0])
tb = int(tmp[1])
if ta == 1:
ta = int(ta)
ta -= 1
tb -= 1
if p[0] == tb:
p[0], p[1] = p[1], p[0]
p[1] = ta
tc = z[p[0]] + mt
cx += r[p[0]] * cos(tc)
cy += r[p[0]] * (sin(tc) - 1)
tc = pi / 2 - tc
mt += tc
else:
tb -= 1
tc = z[tb] + mt
stdout.write('%.10lf %.10lf\n' % (cx + r[tb] * cos(tc), cy + r[tb] * sin(tc)))
if __name__ == '__main__':
main()
```
No
| 95,019 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Hag is a very talented person. He has always had an artist inside him but his father forced him to study mechanical engineering.
Yesterday he spent all of his time cutting a giant piece of wood trying to make it look like a goose. Anyway, his dad found out that he was doing arts rather than studying mechanics and other boring subjects. He confronted Hag with the fact that he is a spoiled son that does not care about his future, and if he continues to do arts he will cut his 25 Lira monthly allowance.
Hag is trying to prove to his dad that the wooden piece is a project for mechanics subject. He also told his dad that the wooden piece is a strictly convex polygon with n vertices.
Hag brought two pins and pinned the polygon with them in the 1-st and 2-nd vertices to the wall. His dad has q queries to Hag of two types.
* 1 f t: pull a pin from the vertex f, wait for the wooden polygon to rotate under the gravity force (if it will rotate) and stabilize. And then put the pin in vertex t.
* 2 v: answer what are the coordinates of the vertex v.
Please help Hag to answer his father's queries.
You can assume that the wood that forms the polygon has uniform density and the polygon has a positive thickness, same in all points. After every query of the 1-st type Hag's dad tries to move the polygon a bit and watches it stabilize again.
Input
The first line contains two integers n and q (3≤ n ≤ 10 000, 1 ≤ q ≤ 200000) — the number of vertices in the polygon and the number of queries.
The next n lines describe the wooden polygon, the i-th line contains two integers x_i and y_i (|x_i|, |y_i|≤ 10^8) — the coordinates of the i-th vertex of the polygon. It is guaranteed that polygon is strictly convex and the vertices are given in the counter-clockwise order and all vertices are distinct.
The next q lines describe the queries, one per line. Each query starts with its type 1 or 2. Each query of the first type continues with two integers f and t (1 ≤ f, t ≤ n) — the vertex the pin is taken from, and the vertex the pin is put to and the polygon finishes rotating. It is guaranteed that the vertex f contains a pin. Each query of the second type continues with a single integer v (1 ≤ v ≤ n) — the vertex the coordinates of which Hag should tell his father.
It is guaranteed that there is at least one query of the second type.
Output
The output should contain the answer to each query of second type — two numbers in a separate line. Your answer is considered correct, if its absolute or relative error does not exceed 10^{-4}.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-4}
Examples
Input
3 4
0 0
2 0
2 2
1 1 2
2 1
2 2
2 3
Output
3.4142135624 -1.4142135624
2.0000000000 0.0000000000
0.5857864376 -1.4142135624
Input
3 2
-1 1
0 0
1 1
1 1 2
2 1
Output
1.0000000000 -1.0000000000
Note
In the first test note the initial and the final state of the wooden polygon.
<image>
Red Triangle is the initial state and the green one is the triangle after rotation around (2,0).
In the second sample note that the polygon rotates 180 degrees counter-clockwise or clockwise direction (it does not matter), because Hag's father makes sure that the polygon is stable and his son does not trick him.
Submitted Solution:
```
import math;
#Вычисление координаты точки по координатам центра, углу, и начальным относительно центра
def getCoordinate(gx, gy, alpha, x, y):
x1=gx+x*math.cos(alpha)-y*math.sin(alpha);
y1=gy+x*math.sin(alpha)+y*math.cos(alpha);
return x1, y1
#Вычисление угла, на который надо повернуть точку с координатами x, y,
#чтобы она оказалась прямо над gx, gy
def getAngle(gx, gy, x, y):
x=x-gx;
y=y-gy;
cos=x/math.sqrt(x**2+y**2);
alpha=math.acos(cos);
if y<0:
alpha=-alpha;
return math.pi/2-alpha;
n, q = map(int, input().split(' '));
x=[0]*n;
y=[0]*n;
for i in range(n):
x[i], y[i]=map(int, input().split(' '));
r=[0]*q;
f=[0]*q;
t=[0]*q;
v=[0]*q;
for i in range(q):
l=list(map(int, input().split(' ')));
r[i]=l[0];
if r[i]==1:
f[i]=l[1]-1;
t[i]=l[2]-1;
else:
v[i]=l[1]-1;
gx=0;
gy=0;
s=0;
for i in range(n):
ip=i+1;
if ip==n:
ip=0;
ds=x[i]*y[ip]-x[ip]*y[i];
s+=ds;
gx+=(x[i]+x[ip])*ds;
gy+=(y[i]+y[ip])*ds;
s/=2;
gx/=6*s;
gy/=6*s;
for i in range(n):
x[i]-=gx;
y[i]-=gy;
alpha=0;
#print('pos',gx, gy, alpha);
#Восстанавливать положение точек будем по центру масс и углу
#Угол - поворот против часовой вокруг центра масс
fix={0, 1}
for i in range(q):
if r[i]==2:
currX, currY = getCoordinate(gx, gy, alpha, x[v[i]], y[v[i]]);
print("%.6f %.6f"%(currX, currY))
else:
if len(fix)==2:
fix.remove(f[i]);
#print('remove',f[i])
#j - единственный элемент в множестве
for j in fix:
#print(j);
currX, currY = getCoordinate(gx, gy, alpha, x[j], y[j]);
#print('fix:', currX, currY)
dalpha=getAngle(gx, gy, currX, currY);
#Чтобы вычислить новые координаты g, нуно повернуть ее на угол
#dalpha относительно currX, currY
gx, gy=currX, currY-math.sqrt(x[j]**2+y[j]**2);
alpha+=dalpha;
#print('pos',gx, gy, alpha/math.pi)
fix.add(t[i]);
```
No
| 95,020 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Hag is a very talented person. He has always had an artist inside him but his father forced him to study mechanical engineering.
Yesterday he spent all of his time cutting a giant piece of wood trying to make it look like a goose. Anyway, his dad found out that he was doing arts rather than studying mechanics and other boring subjects. He confronted Hag with the fact that he is a spoiled son that does not care about his future, and if he continues to do arts he will cut his 25 Lira monthly allowance.
Hag is trying to prove to his dad that the wooden piece is a project for mechanics subject. He also told his dad that the wooden piece is a strictly convex polygon with n vertices.
Hag brought two pins and pinned the polygon with them in the 1-st and 2-nd vertices to the wall. His dad has q queries to Hag of two types.
* 1 f t: pull a pin from the vertex f, wait for the wooden polygon to rotate under the gravity force (if it will rotate) and stabilize. And then put the pin in vertex t.
* 2 v: answer what are the coordinates of the vertex v.
Please help Hag to answer his father's queries.
You can assume that the wood that forms the polygon has uniform density and the polygon has a positive thickness, same in all points. After every query of the 1-st type Hag's dad tries to move the polygon a bit and watches it stabilize again.
Input
The first line contains two integers n and q (3≤ n ≤ 10 000, 1 ≤ q ≤ 200000) — the number of vertices in the polygon and the number of queries.
The next n lines describe the wooden polygon, the i-th line contains two integers x_i and y_i (|x_i|, |y_i|≤ 10^8) — the coordinates of the i-th vertex of the polygon. It is guaranteed that polygon is strictly convex and the vertices are given in the counter-clockwise order and all vertices are distinct.
The next q lines describe the queries, one per line. Each query starts with its type 1 or 2. Each query of the first type continues with two integers f and t (1 ≤ f, t ≤ n) — the vertex the pin is taken from, and the vertex the pin is put to and the polygon finishes rotating. It is guaranteed that the vertex f contains a pin. Each query of the second type continues with a single integer v (1 ≤ v ≤ n) — the vertex the coordinates of which Hag should tell his father.
It is guaranteed that there is at least one query of the second type.
Output
The output should contain the answer to each query of second type — two numbers in a separate line. Your answer is considered correct, if its absolute or relative error does not exceed 10^{-4}.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-4}
Examples
Input
3 4
0 0
2 0
2 2
1 1 2
2 1
2 2
2 3
Output
3.4142135624 -1.4142135624
2.0000000000 0.0000000000
0.5857864376 -1.4142135624
Input
3 2
-1 1
0 0
1 1
1 1 2
2 1
Output
1.0000000000 -1.0000000000
Note
In the first test note the initial and the final state of the wooden polygon.
<image>
Red Triangle is the initial state and the green one is the triangle after rotation around (2,0).
In the second sample note that the polygon rotates 180 degrees counter-clockwise or clockwise direction (it does not matter), because Hag's father makes sure that the polygon is stable and his son does not trick him.
Submitted Solution:
```
import sys
read=lambda:map(int,sys.stdin.buffer.readline().split())
from math import *
n,q=read()
x,y=[],[]
cx,cy=0,0
deg=0
p1,p2=0,1
def pos(i):
return (cx+x[i]*cos(deg)-y[i]*sin(deg),cy+y[i]*cos(deg)+x[i]*sin(deg))
for i in range(n):
_x,_y=read()
cx+=_x;cy+=_y
x.append(_x);y.append(_y)
cx/=n;cy/=n
for i in range(n): x[i]-=cx;y[i]-=cy
for i in range(q):
v=list(read())
if v[0]==1:
if p1==v[1]-1: p1,p2=p2,None
else: p1,p2=p1,None
cx,cy=pos(p1);cy-=hypot(x[p1],y[p1])
deg=pi/2-atan2(y[p1],x[p1]);p2=v[2]-1
else:
print("%.15f %.15f"%pos(v[1]-1))
```
No
| 95,021 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Allen and Bessie are playing a simple number game. They both know a function f: \{0, 1\}^n → R, i. e. the function takes n binary arguments and returns a real value. At the start of the game, the variables x_1, x_2, ..., x_n are all set to -1. Each round, with equal probability, one of Allen or Bessie gets to make a move. A move consists of picking an i such that x_i = -1 and either setting x_i → 0 or x_i → 1.
After n rounds all variables are set, and the game value resolves to f(x_1, x_2, ..., x_n). Allen wants to maximize the game value, and Bessie wants to minimize it.
Your goal is to help Allen and Bessie find the expected game value! They will play r+1 times though, so between each game, exactly one value of f changes. In other words, between rounds i and i+1 for 1 ≤ i ≤ r, f(z_1, ..., z_n) → g_i for some (z_1, ..., z_n) ∈ \{0, 1\}^n. You are to find the expected game value in the beginning and after each change.
Input
The first line contains two integers n and r (1 ≤ n ≤ 18, 0 ≤ r ≤ 2^{18}).
The next line contains 2^n integers c_0, c_1, ..., c_{2^n-1} (0 ≤ c_i ≤ 10^9), denoting the initial values of f. More specifically, f(x_0, x_1, ..., x_{n-1}) = c_x, if x = \overline{x_{n-1} … x_0} in binary.
Each of the next r lines contains two integers z and g (0 ≤ z ≤ 2^n - 1, 0 ≤ g ≤ 10^9). If z = \overline{z_{n-1} ... z_0} in binary, then this means to set f(z_0, ..., z_{n-1}) → g.
Output
Print r+1 lines, the i-th of which denotes the value of the game f during the i-th round. Your answer must have absolute or relative error within 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
2 2
0 1 2 3
2 5
0 4
Output
1.500000
2.250000
3.250000
Input
1 0
2 3
Output
2.500000
Input
2 0
1 1 1 1
Output
1.000000
Note
Consider the second test case. If Allen goes first, he will set x_1 → 1, so the final value will be 3. If Bessie goes first, then she will set x_1 → 0 so the final value will be 2. Thus the answer is 2.5.
In the third test case, the game value will always be 1 regardless of Allen and Bessie's play.
Tags: math
Correct Solution:
```
n, r = [int(x) for x in input().split()]
n = 2 ** n
xs = [int(x) for x in input().split()]
s = sum(xs)
res = [0 for _ in range(r+1)]
for i in range(r):
res[i] = s / n
i, val = [int(x) for x in input().split()]
s += val - xs[i]
xs[i] = val
res[r] = s / n
print("\n".join(map(str, res)))
```
| 95,022 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Allen and Bessie are playing a simple number game. They both know a function f: \{0, 1\}^n → R, i. e. the function takes n binary arguments and returns a real value. At the start of the game, the variables x_1, x_2, ..., x_n are all set to -1. Each round, with equal probability, one of Allen or Bessie gets to make a move. A move consists of picking an i such that x_i = -1 and either setting x_i → 0 or x_i → 1.
After n rounds all variables are set, and the game value resolves to f(x_1, x_2, ..., x_n). Allen wants to maximize the game value, and Bessie wants to minimize it.
Your goal is to help Allen and Bessie find the expected game value! They will play r+1 times though, so between each game, exactly one value of f changes. In other words, between rounds i and i+1 for 1 ≤ i ≤ r, f(z_1, ..., z_n) → g_i for some (z_1, ..., z_n) ∈ \{0, 1\}^n. You are to find the expected game value in the beginning and after each change.
Input
The first line contains two integers n and r (1 ≤ n ≤ 18, 0 ≤ r ≤ 2^{18}).
The next line contains 2^n integers c_0, c_1, ..., c_{2^n-1} (0 ≤ c_i ≤ 10^9), denoting the initial values of f. More specifically, f(x_0, x_1, ..., x_{n-1}) = c_x, if x = \overline{x_{n-1} … x_0} in binary.
Each of the next r lines contains two integers z and g (0 ≤ z ≤ 2^n - 1, 0 ≤ g ≤ 10^9). If z = \overline{z_{n-1} ... z_0} in binary, then this means to set f(z_0, ..., z_{n-1}) → g.
Output
Print r+1 lines, the i-th of which denotes the value of the game f during the i-th round. Your answer must have absolute or relative error within 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
2 2
0 1 2 3
2 5
0 4
Output
1.500000
2.250000
3.250000
Input
1 0
2 3
Output
2.500000
Input
2 0
1 1 1 1
Output
1.000000
Note
Consider the second test case. If Allen goes first, he will set x_1 → 1, so the final value will be 3. If Bessie goes first, then she will set x_1 → 0 so the final value will be 2. Thus the answer is 2.5.
In the third test case, the game value will always be 1 regardless of Allen and Bessie's play.
Tags: math
Correct Solution:
```
R = lambda:list(map(int,input().split()))
sum1 = 0
p1 = 1
a = [0]*263000
b = a
n,r = R()
p1 = 2**n
a = R()
sum1 = sum(a)
p2 = sum1 / p1
for i in range(r):
z,g=R()
sum1 = sum1 - a[z] + g
a[z] = g
b[i] = sum1/p1
print(p2)
for i in range(r):
print(b[i])
```
| 95,023 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Allen and Bessie are playing a simple number game. They both know a function f: \{0, 1\}^n → R, i. e. the function takes n binary arguments and returns a real value. At the start of the game, the variables x_1, x_2, ..., x_n are all set to -1. Each round, with equal probability, one of Allen or Bessie gets to make a move. A move consists of picking an i such that x_i = -1 and either setting x_i → 0 or x_i → 1.
After n rounds all variables are set, and the game value resolves to f(x_1, x_2, ..., x_n). Allen wants to maximize the game value, and Bessie wants to minimize it.
Your goal is to help Allen and Bessie find the expected game value! They will play r+1 times though, so between each game, exactly one value of f changes. In other words, between rounds i and i+1 for 1 ≤ i ≤ r, f(z_1, ..., z_n) → g_i for some (z_1, ..., z_n) ∈ \{0, 1\}^n. You are to find the expected game value in the beginning and after each change.
Input
The first line contains two integers n and r (1 ≤ n ≤ 18, 0 ≤ r ≤ 2^{18}).
The next line contains 2^n integers c_0, c_1, ..., c_{2^n-1} (0 ≤ c_i ≤ 10^9), denoting the initial values of f. More specifically, f(x_0, x_1, ..., x_{n-1}) = c_x, if x = \overline{x_{n-1} … x_0} in binary.
Each of the next r lines contains two integers z and g (0 ≤ z ≤ 2^n - 1, 0 ≤ g ≤ 10^9). If z = \overline{z_{n-1} ... z_0} in binary, then this means to set f(z_0, ..., z_{n-1}) → g.
Output
Print r+1 lines, the i-th of which denotes the value of the game f during the i-th round. Your answer must have absolute or relative error within 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
2 2
0 1 2 3
2 5
0 4
Output
1.500000
2.250000
3.250000
Input
1 0
2 3
Output
2.500000
Input
2 0
1 1 1 1
Output
1.000000
Note
Consider the second test case. If Allen goes first, he will set x_1 → 1, so the final value will be 3. If Bessie goes first, then she will set x_1 → 0 so the final value will be 2. Thus the answer is 2.5.
In the third test case, the game value will always be 1 regardless of Allen and Bessie's play.
Tags: math
Correct Solution:
```
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time
(n, r) = (int(i) for i in input().split())
c = [int(i) for i in input().split()]
start = time.time()
s = sum(c)
n2 = 2**n
ans = [s/n2]
for i in range(r):
(k, new) = (int(i) for i in input().split())
s += new - c[k]
c[k] = new
ans.append(s/n2)
for i in range(len(ans)):
print(ans[i])
finish = time.time()
#print(finish - start)
```
| 95,024 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Allen and Bessie are playing a simple number game. They both know a function f: \{0, 1\}^n → R, i. e. the function takes n binary arguments and returns a real value. At the start of the game, the variables x_1, x_2, ..., x_n are all set to -1. Each round, with equal probability, one of Allen or Bessie gets to make a move. A move consists of picking an i such that x_i = -1 and either setting x_i → 0 or x_i → 1.
After n rounds all variables are set, and the game value resolves to f(x_1, x_2, ..., x_n). Allen wants to maximize the game value, and Bessie wants to minimize it.
Your goal is to help Allen and Bessie find the expected game value! They will play r+1 times though, so between each game, exactly one value of f changes. In other words, between rounds i and i+1 for 1 ≤ i ≤ r, f(z_1, ..., z_n) → g_i for some (z_1, ..., z_n) ∈ \{0, 1\}^n. You are to find the expected game value in the beginning and after each change.
Input
The first line contains two integers n and r (1 ≤ n ≤ 18, 0 ≤ r ≤ 2^{18}).
The next line contains 2^n integers c_0, c_1, ..., c_{2^n-1} (0 ≤ c_i ≤ 10^9), denoting the initial values of f. More specifically, f(x_0, x_1, ..., x_{n-1}) = c_x, if x = \overline{x_{n-1} … x_0} in binary.
Each of the next r lines contains two integers z and g (0 ≤ z ≤ 2^n - 1, 0 ≤ g ≤ 10^9). If z = \overline{z_{n-1} ... z_0} in binary, then this means to set f(z_0, ..., z_{n-1}) → g.
Output
Print r+1 lines, the i-th of which denotes the value of the game f during the i-th round. Your answer must have absolute or relative error within 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
2 2
0 1 2 3
2 5
0 4
Output
1.500000
2.250000
3.250000
Input
1 0
2 3
Output
2.500000
Input
2 0
1 1 1 1
Output
1.000000
Note
Consider the second test case. If Allen goes first, he will set x_1 → 1, so the final value will be 3. If Bessie goes first, then she will set x_1 → 0 so the final value will be 2. Thus the answer is 2.5.
In the third test case, the game value will always be 1 regardless of Allen and Bessie's play.
Tags: math
Correct Solution:
```
import sys
n, r = [int(x) for x in sys.stdin.readline().split()]
a = [int(x) for x in sys.stdin.readline().split()]
s = sum(a)
n = 2**n
sys.stdout.write(str(s / n)+"\n")
for i in range(r):
p, x = [int(x) for x in sys.stdin.readline().split()]
s = s - a[p] + x
a[p] = x
sys.stdout.write(str(s / n)+"\n")
```
| 95,025 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Allen and Bessie are playing a simple number game. They both know a function f: \{0, 1\}^n → R, i. e. the function takes n binary arguments and returns a real value. At the start of the game, the variables x_1, x_2, ..., x_n are all set to -1. Each round, with equal probability, one of Allen or Bessie gets to make a move. A move consists of picking an i such that x_i = -1 and either setting x_i → 0 or x_i → 1.
After n rounds all variables are set, and the game value resolves to f(x_1, x_2, ..., x_n). Allen wants to maximize the game value, and Bessie wants to minimize it.
Your goal is to help Allen and Bessie find the expected game value! They will play r+1 times though, so between each game, exactly one value of f changes. In other words, between rounds i and i+1 for 1 ≤ i ≤ r, f(z_1, ..., z_n) → g_i for some (z_1, ..., z_n) ∈ \{0, 1\}^n. You are to find the expected game value in the beginning and after each change.
Input
The first line contains two integers n and r (1 ≤ n ≤ 18, 0 ≤ r ≤ 2^{18}).
The next line contains 2^n integers c_0, c_1, ..., c_{2^n-1} (0 ≤ c_i ≤ 10^9), denoting the initial values of f. More specifically, f(x_0, x_1, ..., x_{n-1}) = c_x, if x = \overline{x_{n-1} … x_0} in binary.
Each of the next r lines contains two integers z and g (0 ≤ z ≤ 2^n - 1, 0 ≤ g ≤ 10^9). If z = \overline{z_{n-1} ... z_0} in binary, then this means to set f(z_0, ..., z_{n-1}) → g.
Output
Print r+1 lines, the i-th of which denotes the value of the game f during the i-th round. Your answer must have absolute or relative error within 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
2 2
0 1 2 3
2 5
0 4
Output
1.500000
2.250000
3.250000
Input
1 0
2 3
Output
2.500000
Input
2 0
1 1 1 1
Output
1.000000
Note
Consider the second test case. If Allen goes first, he will set x_1 → 1, so the final value will be 3. If Bessie goes first, then she will set x_1 → 0 so the final value will be 2. Thus the answer is 2.5.
In the third test case, the game value will always be 1 regardless of Allen and Bessie's play.
Tags: math
Correct Solution:
```
from sys import stdin
from math import fsum
def main():
n, m = map(int, input().split())
ff = list(map(float, input().split()))
scale, r = .5 ** n, fsum(ff)
res = [r * scale]
for si, sf in map(str.split, stdin.read().splitlines()):
i, f = int(si), float(sf)
r += f - ff[i]
ff[i] = f
res.append(r * scale)
print('\n'.join(map(str, res)))
if __name__ == '__main__':
main()
```
| 95,026 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Allen and Bessie are playing a simple number game. They both know a function f: \{0, 1\}^n → R, i. e. the function takes n binary arguments and returns a real value. At the start of the game, the variables x_1, x_2, ..., x_n are all set to -1. Each round, with equal probability, one of Allen or Bessie gets to make a move. A move consists of picking an i such that x_i = -1 and either setting x_i → 0 or x_i → 1.
After n rounds all variables are set, and the game value resolves to f(x_1, x_2, ..., x_n). Allen wants to maximize the game value, and Bessie wants to minimize it.
Your goal is to help Allen and Bessie find the expected game value! They will play r+1 times though, so between each game, exactly one value of f changes. In other words, between rounds i and i+1 for 1 ≤ i ≤ r, f(z_1, ..., z_n) → g_i for some (z_1, ..., z_n) ∈ \{0, 1\}^n. You are to find the expected game value in the beginning and after each change.
Input
The first line contains two integers n and r (1 ≤ n ≤ 18, 0 ≤ r ≤ 2^{18}).
The next line contains 2^n integers c_0, c_1, ..., c_{2^n-1} (0 ≤ c_i ≤ 10^9), denoting the initial values of f. More specifically, f(x_0, x_1, ..., x_{n-1}) = c_x, if x = \overline{x_{n-1} … x_0} in binary.
Each of the next r lines contains two integers z and g (0 ≤ z ≤ 2^n - 1, 0 ≤ g ≤ 10^9). If z = \overline{z_{n-1} ... z_0} in binary, then this means to set f(z_0, ..., z_{n-1}) → g.
Output
Print r+1 lines, the i-th of which denotes the value of the game f during the i-th round. Your answer must have absolute or relative error within 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
2 2
0 1 2 3
2 5
0 4
Output
1.500000
2.250000
3.250000
Input
1 0
2 3
Output
2.500000
Input
2 0
1 1 1 1
Output
1.000000
Note
Consider the second test case. If Allen goes first, he will set x_1 → 1, so the final value will be 3. If Bessie goes first, then she will set x_1 → 0 so the final value will be 2. Thus the answer is 2.5.
In the third test case, the game value will always be 1 regardless of Allen and Bessie's play.
Submitted Solution:
```
from sys import stdin
def main():
n, m = map(int, input().split())
ff = list(map(float, input().split()))
scale, r = .5 ** n, sum(ff)
res = [r * scale]
for s in stdin.read().splitlines():
i, f = map(int, s.split())
r += f - ff[i]
res.append(r * scale)
print('\n'.join(map(str, res)))
if __name__ == '__main__':
main()
```
No
| 95,027 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Allen and Bessie are playing a simple number game. They both know a function f: \{0, 1\}^n → R, i. e. the function takes n binary arguments and returns a real value. At the start of the game, the variables x_1, x_2, ..., x_n are all set to -1. Each round, with equal probability, one of Allen or Bessie gets to make a move. A move consists of picking an i such that x_i = -1 and either setting x_i → 0 or x_i → 1.
After n rounds all variables are set, and the game value resolves to f(x_1, x_2, ..., x_n). Allen wants to maximize the game value, and Bessie wants to minimize it.
Your goal is to help Allen and Bessie find the expected game value! They will play r+1 times though, so between each game, exactly one value of f changes. In other words, between rounds i and i+1 for 1 ≤ i ≤ r, f(z_1, ..., z_n) → g_i for some (z_1, ..., z_n) ∈ \{0, 1\}^n. You are to find the expected game value in the beginning and after each change.
Input
The first line contains two integers n and r (1 ≤ n ≤ 18, 0 ≤ r ≤ 2^{18}).
The next line contains 2^n integers c_0, c_1, ..., c_{2^n-1} (0 ≤ c_i ≤ 10^9), denoting the initial values of f. More specifically, f(x_0, x_1, ..., x_{n-1}) = c_x, if x = \overline{x_{n-1} … x_0} in binary.
Each of the next r lines contains two integers z and g (0 ≤ z ≤ 2^n - 1, 0 ≤ g ≤ 10^9). If z = \overline{z_{n-1} ... z_0} in binary, then this means to set f(z_0, ..., z_{n-1}) → g.
Output
Print r+1 lines, the i-th of which denotes the value of the game f during the i-th round. Your answer must have absolute or relative error within 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
2 2
0 1 2 3
2 5
0 4
Output
1.500000
2.250000
3.250000
Input
1 0
2 3
Output
2.500000
Input
2 0
1 1 1 1
Output
1.000000
Note
Consider the second test case. If Allen goes first, he will set x_1 → 1, so the final value will be 3. If Bessie goes first, then she will set x_1 → 0 so the final value will be 2. Thus the answer is 2.5.
In the third test case, the game value will always be 1 regardless of Allen and Bessie's play.
Submitted Solution:
```
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time
(n, r) = (int(i) for i in input().split())
c = [int(i) for i in input().split()]
start = time.time()
s = sum(c)
n2 = 2**n
ans = [s/n2]
for i in range(r):
(k, new) = (int(i) for i in input().split())
s += new - c[k]
c[k] = new
ans.append(s/n2)
for i in range(len(ans)):
print(ans[i], end=" ")
print()
finish = time.time()
print(finish - start)
```
No
| 95,028 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Allen and Bessie are playing a simple number game. They both know a function f: \{0, 1\}^n → R, i. e. the function takes n binary arguments and returns a real value. At the start of the game, the variables x_1, x_2, ..., x_n are all set to -1. Each round, with equal probability, one of Allen or Bessie gets to make a move. A move consists of picking an i such that x_i = -1 and either setting x_i → 0 or x_i → 1.
After n rounds all variables are set, and the game value resolves to f(x_1, x_2, ..., x_n). Allen wants to maximize the game value, and Bessie wants to minimize it.
Your goal is to help Allen and Bessie find the expected game value! They will play r+1 times though, so between each game, exactly one value of f changes. In other words, between rounds i and i+1 for 1 ≤ i ≤ r, f(z_1, ..., z_n) → g_i for some (z_1, ..., z_n) ∈ \{0, 1\}^n. You are to find the expected game value in the beginning and after each change.
Input
The first line contains two integers n and r (1 ≤ n ≤ 18, 0 ≤ r ≤ 2^{18}).
The next line contains 2^n integers c_0, c_1, ..., c_{2^n-1} (0 ≤ c_i ≤ 10^9), denoting the initial values of f. More specifically, f(x_0, x_1, ..., x_{n-1}) = c_x, if x = \overline{x_{n-1} … x_0} in binary.
Each of the next r lines contains two integers z and g (0 ≤ z ≤ 2^n - 1, 0 ≤ g ≤ 10^9). If z = \overline{z_{n-1} ... z_0} in binary, then this means to set f(z_0, ..., z_{n-1}) → g.
Output
Print r+1 lines, the i-th of which denotes the value of the game f during the i-th round. Your answer must have absolute or relative error within 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
2 2
0 1 2 3
2 5
0 4
Output
1.500000
2.250000
3.250000
Input
1 0
2 3
Output
2.500000
Input
2 0
1 1 1 1
Output
1.000000
Note
Consider the second test case. If Allen goes first, he will set x_1 → 1, so the final value will be 3. If Bessie goes first, then she will set x_1 → 0 so the final value will be 2. Thus the answer is 2.5.
In the third test case, the game value will always be 1 regardless of Allen and Bessie's play.
Submitted Solution:
```
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time
(n, r) = (int(i) for i in input().split())
c = [int(i) for i in input().split()]
start = time.time()
s = sum(c)
n2 = 2**n
ans = [s/n2]
for i in range(r):
(k, new) = (int(i) for i in input().split())
s += new - c[k]
c[k] = new
ans.append(s/n2)
print(s/n2)
finish = time.time()
print(finish - start)
```
No
| 95,029 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Allen and Bessie are playing a simple number game. They both know a function f: \{0, 1\}^n → R, i. e. the function takes n binary arguments and returns a real value. At the start of the game, the variables x_1, x_2, ..., x_n are all set to -1. Each round, with equal probability, one of Allen or Bessie gets to make a move. A move consists of picking an i such that x_i = -1 and either setting x_i → 0 or x_i → 1.
After n rounds all variables are set, and the game value resolves to f(x_1, x_2, ..., x_n). Allen wants to maximize the game value, and Bessie wants to minimize it.
Your goal is to help Allen and Bessie find the expected game value! They will play r+1 times though, so between each game, exactly one value of f changes. In other words, between rounds i and i+1 for 1 ≤ i ≤ r, f(z_1, ..., z_n) → g_i for some (z_1, ..., z_n) ∈ \{0, 1\}^n. You are to find the expected game value in the beginning and after each change.
Input
The first line contains two integers n and r (1 ≤ n ≤ 18, 0 ≤ r ≤ 2^{18}).
The next line contains 2^n integers c_0, c_1, ..., c_{2^n-1} (0 ≤ c_i ≤ 10^9), denoting the initial values of f. More specifically, f(x_0, x_1, ..., x_{n-1}) = c_x, if x = \overline{x_{n-1} … x_0} in binary.
Each of the next r lines contains two integers z and g (0 ≤ z ≤ 2^n - 1, 0 ≤ g ≤ 10^9). If z = \overline{z_{n-1} ... z_0} in binary, then this means to set f(z_0, ..., z_{n-1}) → g.
Output
Print r+1 lines, the i-th of which denotes the value of the game f during the i-th round. Your answer must have absolute or relative error within 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
2 2
0 1 2 3
2 5
0 4
Output
1.500000
2.250000
3.250000
Input
1 0
2 3
Output
2.500000
Input
2 0
1 1 1 1
Output
1.000000
Note
Consider the second test case. If Allen goes first, he will set x_1 → 1, so the final value will be 3. If Bessie goes first, then she will set x_1 → 0 so the final value will be 2. Thus the answer is 2.5.
In the third test case, the game value will always be 1 regardless of Allen and Bessie's play.
Submitted Solution:
```
from sys import stdin
from math import fsum
def main():
n, m = map(int, input().split())
ff = list(map(float, input().split()))
scale, r = .5 ** n, fsum(ff)
res = [r * scale]
for i, f in map(str.split, stdin.read().splitlines()):
r += float(f) - ff[int(i)]
res.append(r * scale)
print('\n'.join(map(str, res)))
if __name__ == '__main__':
main()
```
No
| 95,030 |
Provide a correct Python 3 solution for this coding contest problem.
Snuke, a water strider, lives in a rectangular pond that can be seen as a grid with H east-west rows and W north-south columns. Let (i,j) be the square at the i-th row from the north and j-th column from the west.
Some of the squares have a lotus leaf on it and cannot be entered. The square (i,j) has a lotus leaf on it if c_{ij} is `@`, and it does not if c_{ij} is `.`.
In one stroke, Snuke can move between 1 and K squares (inclusive) toward one of the four directions: north, east, south, and west. The move may not pass through a square with a lotus leaf. Moving to such a square or out of the pond is also forbidden.
Find the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2). If the travel from (x_1,y_1) to (x_2,y_2) is impossible, point out that fact.
Constraints
* 1 \leq H,W,K \leq 10^6
* H \times W \leq 10^6
* 1 \leq x_1,x_2 \leq H
* 1 \leq y_1,y_2 \leq W
* x_1 \neq x_2 or y_1 \neq y_2.
* c_{i,j} is `.` or `@`.
* c_{x_1,y_1} = `.`
* c_{x_2,y_2} = `.`
* All numbers in input are integers.
Input
Input is given from Standard Input in the following format:
H W K
x_1 y_1 x_2 y_2
c_{1,1}c_{1,2} .. c_{1,W}
c_{2,1}c_{2,2} .. c_{2,W}
:
c_{H,1}c_{H,2} .. c_{H,W}
Output
Print the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2), or print `-1` if the travel is impossible.
Examples
Input
3 5 2
3 2 3 4
.....
.@..@
..@..
Output
5
Input
1 6 4
1 1 1 6
......
Output
2
Input
3 3 1
2 1 2 3
.@.
.@.
.@.
Output
-1
"Correct Solution:
```
import collections
H,W,K = map(int,input().split())
s1,e1,s2,e2 = map(int,input().split())
s1-=1
e1-=1
s2-=1
e2-=1
L = []
board = []
for _ in range(H):
a = input()
L.append(a)
for i in range(H):
l = L[i]
tmp = []
for j in range(len(l)):
tmp.append(l[j])
board.append(tmp)
queue = collections.deque()
queue.append([0,s1,e1])
seen = [[float('inf')]*(W) for _ in range(H)]
destination = [(1,0),(-1,0),(0,1),(0,-1)]
while queue:
cost,x,y = queue.popleft()
seen[x][y] = cost
if x==s2 and y ==e2:
print(cost);exit()
break
for dx,dy in destination:
X,Y = x,y
for i in range(1,K+1):
NX = X +i*dx
NY = Y + i*dy
if 0>NX or NX>=H or 0>NY or NY>=W or seen[NX][NY] < seen[X][Y]+1: break
else:
if board[NX][NY]=='@':
break
elif seen[NX][NY] ==float('inf'):
queue.append([cost+1,NX,NY])
seen[NX][NY] = cost+1
print(-1)
```
| 95,031 |
Provide a correct Python 3 solution for this coding contest problem.
Snuke, a water strider, lives in a rectangular pond that can be seen as a grid with H east-west rows and W north-south columns. Let (i,j) be the square at the i-th row from the north and j-th column from the west.
Some of the squares have a lotus leaf on it and cannot be entered. The square (i,j) has a lotus leaf on it if c_{ij} is `@`, and it does not if c_{ij} is `.`.
In one stroke, Snuke can move between 1 and K squares (inclusive) toward one of the four directions: north, east, south, and west. The move may not pass through a square with a lotus leaf. Moving to such a square or out of the pond is also forbidden.
Find the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2). If the travel from (x_1,y_1) to (x_2,y_2) is impossible, point out that fact.
Constraints
* 1 \leq H,W,K \leq 10^6
* H \times W \leq 10^6
* 1 \leq x_1,x_2 \leq H
* 1 \leq y_1,y_2 \leq W
* x_1 \neq x_2 or y_1 \neq y_2.
* c_{i,j} is `.` or `@`.
* c_{x_1,y_1} = `.`
* c_{x_2,y_2} = `.`
* All numbers in input are integers.
Input
Input is given from Standard Input in the following format:
H W K
x_1 y_1 x_2 y_2
c_{1,1}c_{1,2} .. c_{1,W}
c_{2,1}c_{2,2} .. c_{2,W}
:
c_{H,1}c_{H,2} .. c_{H,W}
Output
Print the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2), or print `-1` if the travel is impossible.
Examples
Input
3 5 2
3 2 3 4
.....
.@..@
..@..
Output
5
Input
1 6 4
1 1 1 6
......
Output
2
Input
3 3 1
2 1 2 3
.@.
.@.
.@.
Output
-1
"Correct Solution:
```
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
import sys
import math
def LI(): return [int(x) for x in sys.stdin.readline().split()]
dd = [(1,0),(-1,0),(0,1),(0,-1)]
def solve():
h,w,k = LI()
y,x,s,t = LI()
y -= 1
x -= 1
s -= 1
t -= 1
c = [input() for i in range(h)]
q = deque([(y,x)])
d = [[float("inf")]*w for i in range(h)]
d[y][x] = 0
while q:
y,x = q.popleft()
if (y,x) == (s,t):
print(d[s][t])
return
nd = d[y][x] + 1
for dy,dx in dd:
ny,nx = y,x
for _ in range(k):
ny += dy
nx += dx
if 0 <= ny < h and 0 <= nx < w and c[ny][nx] != "@" and nd <= d[ny][nx]:
if nd < d[ny][nx]:
d[ny][nx] = nd
q.append((ny,nx))
continue
break
print(-1)
return
#Solve
if __name__ == "__main__":
solve()
```
| 95,032 |
Provide a correct Python 3 solution for this coding contest problem.
Snuke, a water strider, lives in a rectangular pond that can be seen as a grid with H east-west rows and W north-south columns. Let (i,j) be the square at the i-th row from the north and j-th column from the west.
Some of the squares have a lotus leaf on it and cannot be entered. The square (i,j) has a lotus leaf on it if c_{ij} is `@`, and it does not if c_{ij} is `.`.
In one stroke, Snuke can move between 1 and K squares (inclusive) toward one of the four directions: north, east, south, and west. The move may not pass through a square with a lotus leaf. Moving to such a square or out of the pond is also forbidden.
Find the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2). If the travel from (x_1,y_1) to (x_2,y_2) is impossible, point out that fact.
Constraints
* 1 \leq H,W,K \leq 10^6
* H \times W \leq 10^6
* 1 \leq x_1,x_2 \leq H
* 1 \leq y_1,y_2 \leq W
* x_1 \neq x_2 or y_1 \neq y_2.
* c_{i,j} is `.` or `@`.
* c_{x_1,y_1} = `.`
* c_{x_2,y_2} = `.`
* All numbers in input are integers.
Input
Input is given from Standard Input in the following format:
H W K
x_1 y_1 x_2 y_2
c_{1,1}c_{1,2} .. c_{1,W}
c_{2,1}c_{2,2} .. c_{2,W}
:
c_{H,1}c_{H,2} .. c_{H,W}
Output
Print the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2), or print `-1` if the travel is impossible.
Examples
Input
3 5 2
3 2 3 4
.....
.@..@
..@..
Output
5
Input
1 6 4
1 1 1 6
......
Output
2
Input
3 3 1
2 1 2 3
.@.
.@.
.@.
Output
-1
"Correct Solution:
```
# -*- coding: utf-8 -*-
H,W,K = map(int, input().split())
x1,y1,x2,y2 = map(int, input().split())
C = [input() for _ in range(H)]
T = [[-1 for _ in range(W)] for _ in range(H)]
T[x1-1][y1-1] = 0
que = {(x1-1,y1-1):0}
step = 1
while len(que)>0:
que_next = {}
for q in que.keys():
x,y = q[0],q[1]
for i in range(1,K+1):
if x-i<0:
break
if C[x-i][y]=='@' or T[x-i][y]==1:
break
if x-i==x2-1 and y==y2-1:
print(step)
exit()
que_next[(x-i,y)] = 0
for i in range(1,K+1):
if x+i>H-1:
break
if C[x+i][y]=='@' or T[x+i][y]==1:
break
if x+i==x2-1 and y==y2-1:
print(step)
exit()
que_next[(x+i,y)] = 0
for i in range(1,K+1):
if y-i<0:
break
if C[x][y-i]=='@' or T[x][y-i]==1:
break
if x==x2-1 and y-i==y2-1:
print(step)
exit()
que_next[(x,y-i)] = 0
for i in range(1,K+1):
if y+i>W-1:
break
if C[x][y+i]=='@' or T[x][y+i]==1:
break
if x==x2-1 and y+i==y2-1:
print(step)
exit()
que_next[(x,y+i)] = 0
que = que_next
for q in que.keys():
T[q[0]][q[1]] = 1
step += 1
print(-1)
```
| 95,033 |
Provide a correct Python 3 solution for this coding contest problem.
Snuke, a water strider, lives in a rectangular pond that can be seen as a grid with H east-west rows and W north-south columns. Let (i,j) be the square at the i-th row from the north and j-th column from the west.
Some of the squares have a lotus leaf on it and cannot be entered. The square (i,j) has a lotus leaf on it if c_{ij} is `@`, and it does not if c_{ij} is `.`.
In one stroke, Snuke can move between 1 and K squares (inclusive) toward one of the four directions: north, east, south, and west. The move may not pass through a square with a lotus leaf. Moving to such a square or out of the pond is also forbidden.
Find the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2). If the travel from (x_1,y_1) to (x_2,y_2) is impossible, point out that fact.
Constraints
* 1 \leq H,W,K \leq 10^6
* H \times W \leq 10^6
* 1 \leq x_1,x_2 \leq H
* 1 \leq y_1,y_2 \leq W
* x_1 \neq x_2 or y_1 \neq y_2.
* c_{i,j} is `.` or `@`.
* c_{x_1,y_1} = `.`
* c_{x_2,y_2} = `.`
* All numbers in input are integers.
Input
Input is given from Standard Input in the following format:
H W K
x_1 y_1 x_2 y_2
c_{1,1}c_{1,2} .. c_{1,W}
c_{2,1}c_{2,2} .. c_{2,W}
:
c_{H,1}c_{H,2} .. c_{H,W}
Output
Print the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2), or print `-1` if the travel is impossible.
Examples
Input
3 5 2
3 2 3 4
.....
.@..@
..@..
Output
5
Input
1 6 4
1 1 1 6
......
Output
2
Input
3 3 1
2 1 2 3
.@.
.@.
.@.
Output
-1
"Correct Solution:
```
#
import sys
from collections import deque
input=sys.stdin.readline
def main():
H,W,K=map(int,input().split())
x1,y1,x2,y2=map(int,input().split())
x1-=1
y1-=1
x2-=1
y2-=1
mas=[list(input()) for i in range(H)]
dist=[[-1]*W for i in range(H)]
dist[x1][y1]=0
qu=deque([(x1,y1)])
qup=qu.popleft
qua=qu.append
d=((-1,0),(1,0),(0,-1),(0,1))
while(len(qu)>0):
v=qup()
for di in d:
for i in range(1,K+1):
nvh=v[0]+i*di[0]
nvw=v[1]+i*di[1]
if nvh<0 or nvh>=H or nvw<0 or nvw>=W or mas[nvh][nvw]=="@":
break
if dist[nvh][nvw]!=-1 and dist[nvh][nvw]<=dist[v[0]][v[1]]:
break
if dist[nvh][nvw]==-1:
dist[nvh][nvw]=dist[v[0]][v[1]]+1
qua((nvh,nvw))
print(dist[x2][y2])
if __name__=="__main__":
main()
```
| 95,034 |
Provide a correct Python 3 solution for this coding contest problem.
Snuke, a water strider, lives in a rectangular pond that can be seen as a grid with H east-west rows and W north-south columns. Let (i,j) be the square at the i-th row from the north and j-th column from the west.
Some of the squares have a lotus leaf on it and cannot be entered. The square (i,j) has a lotus leaf on it if c_{ij} is `@`, and it does not if c_{ij} is `.`.
In one stroke, Snuke can move between 1 and K squares (inclusive) toward one of the four directions: north, east, south, and west. The move may not pass through a square with a lotus leaf. Moving to such a square or out of the pond is also forbidden.
Find the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2). If the travel from (x_1,y_1) to (x_2,y_2) is impossible, point out that fact.
Constraints
* 1 \leq H,W,K \leq 10^6
* H \times W \leq 10^6
* 1 \leq x_1,x_2 \leq H
* 1 \leq y_1,y_2 \leq W
* x_1 \neq x_2 or y_1 \neq y_2.
* c_{i,j} is `.` or `@`.
* c_{x_1,y_1} = `.`
* c_{x_2,y_2} = `.`
* All numbers in input are integers.
Input
Input is given from Standard Input in the following format:
H W K
x_1 y_1 x_2 y_2
c_{1,1}c_{1,2} .. c_{1,W}
c_{2,1}c_{2,2} .. c_{2,W}
:
c_{H,1}c_{H,2} .. c_{H,W}
Output
Print the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2), or print `-1` if the travel is impossible.
Examples
Input
3 5 2
3 2 3 4
.....
.@..@
..@..
Output
5
Input
1 6 4
1 1 1 6
......
Output
2
Input
3 3 1
2 1 2 3
.@.
.@.
.@.
Output
-1
"Correct Solution:
```
#16:41
h,w,k = map(int,input().split())
x1,y1,x2,y2 = map(lambda x:int(x)-1, input().split())
raw = []
for _ in range(h):
raw.append(input())
inf = 10**6
seen = [[inf for _ in range(w)] for _ in range(h)]
seen[x1][y1] = 0
from heapq import heappush as push
from heapq import heappop as pop
now = [[0,x1,y1]]
while now:
s,x,y = pop(now)
if x == x2 and y == y2:
print(s)
exit()
for i in range(1,k+1):
if x-i == -1 or raw[x-i][y] == '@' or seen[x-i][y] < s+1:
break
if seen[x-i][y] != s+1:
seen[x-i][y] = s+1
push(now,[s+1,x-i,y])
for i in range(1,k+1):
if y-i == -1 or raw[x][y-i] == '@' or seen[x][y-i] < s+1:
break
if seen[x][y-i] != s+1:
seen[x][y-i] = s+1
push(now,[s+1,x,y-i])
for i in range(1,k+1):
if x+i == h or raw[x+i][y] == '@' or seen[x+i][y] < s+1:
break
if seen[x+i][y] != s+1:
seen[x+i][y] = s+1
push(now,[s+1,x+i,y])
for i in range(1,k+1):
if y+i == w or raw[x][y+i] == '@' or seen[x][y+i] < s+1:
break
if seen[x][y+i] != s+1:
seen[x][y+i] = s+1
push(now,[s+1,x,y+i])
print(-1)
```
| 95,035 |
Provide a correct Python 3 solution for this coding contest problem.
Snuke, a water strider, lives in a rectangular pond that can be seen as a grid with H east-west rows and W north-south columns. Let (i,j) be the square at the i-th row from the north and j-th column from the west.
Some of the squares have a lotus leaf on it and cannot be entered. The square (i,j) has a lotus leaf on it if c_{ij} is `@`, and it does not if c_{ij} is `.`.
In one stroke, Snuke can move between 1 and K squares (inclusive) toward one of the four directions: north, east, south, and west. The move may not pass through a square with a lotus leaf. Moving to such a square or out of the pond is also forbidden.
Find the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2). If the travel from (x_1,y_1) to (x_2,y_2) is impossible, point out that fact.
Constraints
* 1 \leq H,W,K \leq 10^6
* H \times W \leq 10^6
* 1 \leq x_1,x_2 \leq H
* 1 \leq y_1,y_2 \leq W
* x_1 \neq x_2 or y_1 \neq y_2.
* c_{i,j} is `.` or `@`.
* c_{x_1,y_1} = `.`
* c_{x_2,y_2} = `.`
* All numbers in input are integers.
Input
Input is given from Standard Input in the following format:
H W K
x_1 y_1 x_2 y_2
c_{1,1}c_{1,2} .. c_{1,W}
c_{2,1}c_{2,2} .. c_{2,W}
:
c_{H,1}c_{H,2} .. c_{H,W}
Output
Print the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2), or print `-1` if the travel is impossible.
Examples
Input
3 5 2
3 2 3 4
.....
.@..@
..@..
Output
5
Input
1 6 4
1 1 1 6
......
Output
2
Input
3 3 1
2 1 2 3
.@.
.@.
.@.
Output
-1
"Correct Solution:
```
from heapq import heappop, heappush
from collections import deque
def dijkstra(xs, ys, xg, yg,h,w,k,field):
# que->(cost, x, y, direction)
inf = 1e18
dist = [[inf]*w for _ in range(h)]
dist[xs][ys] = 0
que = deque([(0, xs, ys)])
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
while que:
cost, x, y = que.popleft()
if cost > dist[x][y]:
continue
for v in range(4):
nx, ny = x, y
for _ in range(k):
nx += dx[v]
ny += dy[v]
if not field[nx][ny]:
break
if dist[nx][ny]<=dist[x][y]:
break
if dist[nx][ny] > dist[x][y]+1:
dist[nx][ny] = dist[x][y]+1
que.append((dist[nx][ny], nx, ny))
if dist[xg][yg] == inf:
print(-1)
else:
print(dist[xg][yg])
def main():
import sys
def input(): return sys.stdin.readline().rstrip()
h, w, k = map(int, input().split())
xs, ys, xg, yg = map(int, input().split())
field = [[False]*(w+2) for _ in range(h+2)]
for i in range(h):
# False で覆うことでx,yの制限をなくす。
s = [True if _ == '.' else False for _ in input()]
field[i+1] = [False]+s+[False]
h += 2
w += 2
dijkstra(xs, ys, xg, yg, h, w, k, field)
if __name__ == '__main__':
main()
```
| 95,036 |
Provide a correct Python 3 solution for this coding contest problem.
Snuke, a water strider, lives in a rectangular pond that can be seen as a grid with H east-west rows and W north-south columns. Let (i,j) be the square at the i-th row from the north and j-th column from the west.
Some of the squares have a lotus leaf on it and cannot be entered. The square (i,j) has a lotus leaf on it if c_{ij} is `@`, and it does not if c_{ij} is `.`.
In one stroke, Snuke can move between 1 and K squares (inclusive) toward one of the four directions: north, east, south, and west. The move may not pass through a square with a lotus leaf. Moving to such a square or out of the pond is also forbidden.
Find the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2). If the travel from (x_1,y_1) to (x_2,y_2) is impossible, point out that fact.
Constraints
* 1 \leq H,W,K \leq 10^6
* H \times W \leq 10^6
* 1 \leq x_1,x_2 \leq H
* 1 \leq y_1,y_2 \leq W
* x_1 \neq x_2 or y_1 \neq y_2.
* c_{i,j} is `.` or `@`.
* c_{x_1,y_1} = `.`
* c_{x_2,y_2} = `.`
* All numbers in input are integers.
Input
Input is given from Standard Input in the following format:
H W K
x_1 y_1 x_2 y_2
c_{1,1}c_{1,2} .. c_{1,W}
c_{2,1}c_{2,2} .. c_{2,W}
:
c_{H,1}c_{H,2} .. c_{H,W}
Output
Print the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2), or print `-1` if the travel is impossible.
Examples
Input
3 5 2
3 2 3 4
.....
.@..@
..@..
Output
5
Input
1 6 4
1 1 1 6
......
Output
2
Input
3 3 1
2 1 2 3
.@.
.@.
.@.
Output
-1
"Correct Solution:
```
from collections import deque
H,W,K = map(int,input().split())
x1,y1,x2,y2 = map(int,input().split())
maze = []
maze.append(["@"]*(W+2))
for _ in range(H):
row = list(input())
maze.append(["@"]+row+["@"])
maze.append(["@"]*(W+2))
q = deque([(x1,y1)])
depth = 0
while q:
for i,j in q:
maze[i][j] = "@"
new_q = set()
l = len(q)
for _ in range(l):
x,y = q.pop()
if (x,y) == (x2,y2):
print(depth)
exit()
# Up
for i in range(1, K+1):
if maze[x-i][y] == "@": break
new_q.add((x-i, y))
# Down
for i in range(1, K+1):
if maze[x+i][y] == "@": break
new_q.add((x+i, y))
# Left
for i in range(1, K+1):
if maze[x][y-i] == "@": break
new_q.add((x, y-i))
# Right
for i in range(1, K+1):
if maze[x][y+i] == "@": break
new_q.add((x, y+i))
q = deque(list(new_q))
depth += 1
print(-1)
```
| 95,037 |
Provide a correct Python 3 solution for this coding contest problem.
Snuke, a water strider, lives in a rectangular pond that can be seen as a grid with H east-west rows and W north-south columns. Let (i,j) be the square at the i-th row from the north and j-th column from the west.
Some of the squares have a lotus leaf on it and cannot be entered. The square (i,j) has a lotus leaf on it if c_{ij} is `@`, and it does not if c_{ij} is `.`.
In one stroke, Snuke can move between 1 and K squares (inclusive) toward one of the four directions: north, east, south, and west. The move may not pass through a square with a lotus leaf. Moving to such a square or out of the pond is also forbidden.
Find the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2). If the travel from (x_1,y_1) to (x_2,y_2) is impossible, point out that fact.
Constraints
* 1 \leq H,W,K \leq 10^6
* H \times W \leq 10^6
* 1 \leq x_1,x_2 \leq H
* 1 \leq y_1,y_2 \leq W
* x_1 \neq x_2 or y_1 \neq y_2.
* c_{i,j} is `.` or `@`.
* c_{x_1,y_1} = `.`
* c_{x_2,y_2} = `.`
* All numbers in input are integers.
Input
Input is given from Standard Input in the following format:
H W K
x_1 y_1 x_2 y_2
c_{1,1}c_{1,2} .. c_{1,W}
c_{2,1}c_{2,2} .. c_{2,W}
:
c_{H,1}c_{H,2} .. c_{H,W}
Output
Print the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2), or print `-1` if the travel is impossible.
Examples
Input
3 5 2
3 2 3 4
.....
.@..@
..@..
Output
5
Input
1 6 4
1 1 1 6
......
Output
2
Input
3 3 1
2 1 2 3
.@.
.@.
.@.
Output
-1
"Correct Solution:
```
#import heapq
from collections import deque
h,w,k = list(map(int, input().split()))
x1,y1,x2,y2 = list(map(int, input().split()))
x1,y1,x2,y2 = x1-1,y1-1,x2-1,y2-1
dp = [[-1]*w for _ in range(h)]
#que = []
que = deque()
c = [input() for _ in range(h)]
dp[x1][y1] = 0
#heapq.heappush(que, (dp[x1][y1], [x1,y1]))
que.append((x1,y1))
dx = [1,0,-1,0]
dy = [0,1,0,-1]
while que:
x,y=que.popleft()
if x==x2 and y==y2:
exit(print(dp[x][y]))
for dx,dy in [[1,0],[-1,0],[0,-1],[0,1]]:
for i in range(1,k+1):
xx=x+dx*i
yy=y+dy*i
if not(0<=xx<h and 0<=yy<w) or c[xx][yy]=="@":break
if 0<=dp[xx][yy]<=dp[x][y]:break
if dp[xx][yy]==-1:que.append((xx,yy))
dp[xx][yy]=dp[x][y]+1
print(-1)
```
| 95,038 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Snuke, a water strider, lives in a rectangular pond that can be seen as a grid with H east-west rows and W north-south columns. Let (i,j) be the square at the i-th row from the north and j-th column from the west.
Some of the squares have a lotus leaf on it and cannot be entered. The square (i,j) has a lotus leaf on it if c_{ij} is `@`, and it does not if c_{ij} is `.`.
In one stroke, Snuke can move between 1 and K squares (inclusive) toward one of the four directions: north, east, south, and west. The move may not pass through a square with a lotus leaf. Moving to such a square or out of the pond is also forbidden.
Find the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2). If the travel from (x_1,y_1) to (x_2,y_2) is impossible, point out that fact.
Constraints
* 1 \leq H,W,K \leq 10^6
* H \times W \leq 10^6
* 1 \leq x_1,x_2 \leq H
* 1 \leq y_1,y_2 \leq W
* x_1 \neq x_2 or y_1 \neq y_2.
* c_{i,j} is `.` or `@`.
* c_{x_1,y_1} = `.`
* c_{x_2,y_2} = `.`
* All numbers in input are integers.
Input
Input is given from Standard Input in the following format:
H W K
x_1 y_1 x_2 y_2
c_{1,1}c_{1,2} .. c_{1,W}
c_{2,1}c_{2,2} .. c_{2,W}
:
c_{H,1}c_{H,2} .. c_{H,W}
Output
Print the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2), or print `-1` if the travel is impossible.
Examples
Input
3 5 2
3 2 3 4
.....
.@..@
..@..
Output
5
Input
1 6 4
1 1 1 6
......
Output
2
Input
3 3 1
2 1 2 3
.@.
.@.
.@.
Output
-1
Submitted Solution:
```
INF=float('inf')
from collections import deque
def mbfs(G,sh,sw,H,W):
dist=[[INF]*W for i in range(H)]
dist[sh][sw]=0
d=deque()
d.append([sh,sw])
dx=[1,0,-1,0]
dy=[0,1,0,-1]
while(len(d)!=0):
h=d[0][0]
w=d[0][1]
d.popleft()
for dir in range(4):
for k in range(1,K+1):
nh=h+dx[dir]*k
nw=w+dy[dir]*k
if(nh>H-1 or nh<0 or nw>W-1 or nw<0):
break
if(G[nh][nw]=='@'):
break
if(dist[nh][nw]<=dist[h][w]):
break
if dist[nh][nw]==dist[h][w]+1:
continue
else:
dist[nh][nw]=dist[h][w]+1
d.append([nh,nw])
return dist
H,W,K=map(int,input().split())
sx,sy,gx,gy=map(lambda x:int(x)-1,input().split())
c=[list(input()) for i in range(H)]
dist=mbfs(c,sx,sy,H,W)
ans=dist[gx][gy]
print(ans if ans!=INF else -1)
```
Yes
| 95,039 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Snuke, a water strider, lives in a rectangular pond that can be seen as a grid with H east-west rows and W north-south columns. Let (i,j) be the square at the i-th row from the north and j-th column from the west.
Some of the squares have a lotus leaf on it and cannot be entered. The square (i,j) has a lotus leaf on it if c_{ij} is `@`, and it does not if c_{ij} is `.`.
In one stroke, Snuke can move between 1 and K squares (inclusive) toward one of the four directions: north, east, south, and west. The move may not pass through a square with a lotus leaf. Moving to such a square or out of the pond is also forbidden.
Find the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2). If the travel from (x_1,y_1) to (x_2,y_2) is impossible, point out that fact.
Constraints
* 1 \leq H,W,K \leq 10^6
* H \times W \leq 10^6
* 1 \leq x_1,x_2 \leq H
* 1 \leq y_1,y_2 \leq W
* x_1 \neq x_2 or y_1 \neq y_2.
* c_{i,j} is `.` or `@`.
* c_{x_1,y_1} = `.`
* c_{x_2,y_2} = `.`
* All numbers in input are integers.
Input
Input is given from Standard Input in the following format:
H W K
x_1 y_1 x_2 y_2
c_{1,1}c_{1,2} .. c_{1,W}
c_{2,1}c_{2,2} .. c_{2,W}
:
c_{H,1}c_{H,2} .. c_{H,W}
Output
Print the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2), or print `-1` if the travel is impossible.
Examples
Input
3 5 2
3 2 3 4
.....
.@..@
..@..
Output
5
Input
1 6 4
1 1 1 6
......
Output
2
Input
3 3 1
2 1 2 3
.@.
.@.
.@.
Output
-1
Submitted Solution:
```
import sys
input = sys.stdin.readline
import queue
class gridMan:
def __init__(s, L):
s.L = L
s.H = len(s.L)
s.W = len(s.L[0])
def makeWall(s, x = -2):
w = len(s.L[0]) + 2
s.L = [[x] * w] + [[x] + i + [x] for i in s.L] + [[x] * w]
def dist(s, S): #Sから上下左右に進んで何手かかるかのリスト および 最大値
T = [[-1] * s.W for _ in range(s.H)]
q = queue.Queue()
q.put([S[0], S[1]])
T[S[0]][S[1]] = 0
k = 0
while not q.empty():
h, w = q.get()
p = T[h][w]
for i, j in [[1, 0], [-1, 0], [0, 1], [0, -1]]:
for k in range(1, K + 1):
hh = h + i * k
ww = w + j * k
if s.L[hh + 1][ww + 1] >= 0:
if T[hh][ww] == -1 or T[hh][ww] > p + 1:
q.put([hh, ww])
T[hh][ww] = p + 1
elif T[hh][ww] != p + 1:
break
else:
break
return T, k
H, W, K = list(map(int, input().split()))
xy = list(map(int, input().split()))
c = []
for i in range(H):
c.append(list(input().replace("\n", "")))
for i in range(H):
for j in range(W):
if c[i][j] == ".":
c[i][j] = 0
else:
c[i][j] = -1
g = gridMan(c)
g.makeWall()
L, k = g.dist([xy[0] - 1, xy[1] - 1])
print(L[xy[2] - 1][xy[3] - 1])
```
Yes
| 95,040 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Snuke, a water strider, lives in a rectangular pond that can be seen as a grid with H east-west rows and W north-south columns. Let (i,j) be the square at the i-th row from the north and j-th column from the west.
Some of the squares have a lotus leaf on it and cannot be entered. The square (i,j) has a lotus leaf on it if c_{ij} is `@`, and it does not if c_{ij} is `.`.
In one stroke, Snuke can move between 1 and K squares (inclusive) toward one of the four directions: north, east, south, and west. The move may not pass through a square with a lotus leaf. Moving to such a square or out of the pond is also forbidden.
Find the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2). If the travel from (x_1,y_1) to (x_2,y_2) is impossible, point out that fact.
Constraints
* 1 \leq H,W,K \leq 10^6
* H \times W \leq 10^6
* 1 \leq x_1,x_2 \leq H
* 1 \leq y_1,y_2 \leq W
* x_1 \neq x_2 or y_1 \neq y_2.
* c_{i,j} is `.` or `@`.
* c_{x_1,y_1} = `.`
* c_{x_2,y_2} = `.`
* All numbers in input are integers.
Input
Input is given from Standard Input in the following format:
H W K
x_1 y_1 x_2 y_2
c_{1,1}c_{1,2} .. c_{1,W}
c_{2,1}c_{2,2} .. c_{2,W}
:
c_{H,1}c_{H,2} .. c_{H,W}
Output
Print the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2), or print `-1` if the travel is impossible.
Examples
Input
3 5 2
3 2 3 4
.....
.@..@
..@..
Output
5
Input
1 6 4
1 1 1 6
......
Output
2
Input
3 3 1
2 1 2 3
.@.
.@.
.@.
Output
-1
Submitted Solution:
```
from collections import deque
H, W, K = map(int, input().split())
sh, sw, gh, gw = map(int, input().split())
sh, sw, gh, gw = sh - 1, sw - 1, gh - 1, gw - 1
G = [list(input()) for _ in range(H)]
INF = 10 ** 9
D = [[INF] * W for _ in range(H)]
D[sh][sw] = 0
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
que = deque([(sh, sw)])
while que:
nh, nw = que.pop()
for dh, dw in directions:
for k in range(1, K + 1):
nx_h, nx_w = nh + k * dh, nw + k * dw
if not (0 <= nx_h < H and 0 <= nx_w < W):
break
if G[nx_h][nx_w] == '@':
break
if D[nx_h][nx_w] <= D[nh][nw]:
break
if D[nx_h][nx_w] > D[nh][nw] + 1:
D[nx_h][nx_w] = D[nh][nw] + 1
que.appendleft((nx_h, nx_w))
print(D[gh][gw] if D[gh][gw] != INF else -1)
```
Yes
| 95,041 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Snuke, a water strider, lives in a rectangular pond that can be seen as a grid with H east-west rows and W north-south columns. Let (i,j) be the square at the i-th row from the north and j-th column from the west.
Some of the squares have a lotus leaf on it and cannot be entered. The square (i,j) has a lotus leaf on it if c_{ij} is `@`, and it does not if c_{ij} is `.`.
In one stroke, Snuke can move between 1 and K squares (inclusive) toward one of the four directions: north, east, south, and west. The move may not pass through a square with a lotus leaf. Moving to such a square or out of the pond is also forbidden.
Find the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2). If the travel from (x_1,y_1) to (x_2,y_2) is impossible, point out that fact.
Constraints
* 1 \leq H,W,K \leq 10^6
* H \times W \leq 10^6
* 1 \leq x_1,x_2 \leq H
* 1 \leq y_1,y_2 \leq W
* x_1 \neq x_2 or y_1 \neq y_2.
* c_{i,j} is `.` or `@`.
* c_{x_1,y_1} = `.`
* c_{x_2,y_2} = `.`
* All numbers in input are integers.
Input
Input is given from Standard Input in the following format:
H W K
x_1 y_1 x_2 y_2
c_{1,1}c_{1,2} .. c_{1,W}
c_{2,1}c_{2,2} .. c_{2,W}
:
c_{H,1}c_{H,2} .. c_{H,W}
Output
Print the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2), or print `-1` if the travel is impossible.
Examples
Input
3 5 2
3 2 3 4
.....
.@..@
..@..
Output
5
Input
1 6 4
1 1 1 6
......
Output
2
Input
3 3 1
2 1 2 3
.@.
.@.
.@.
Output
-1
Submitted Solution:
```
from collections import deque
import sys
def bfs(M, sy, sx, gy, gx):
queue = deque([[sy, sx]])
M[sy][sx] = 0
while queue:
# queueには訪れた地点が入っている。そこから、4方向に移動できるか考え、queueから消す。
y, x = queue.popleft() # queueに入っていたものを消す。
if [y, x] == [gy, gx]: # もしゴールについていたならば、そのときの手数を出す。
return M[y][x]
for dx, dy in ([1, 0], [-1, 0], [0, 1], [0, -1]):
for k in range(1, K + 1):
new_x = x + dx * k
new_y = y + dy * k
if (0 <= new_y < H) and (0 <= new_x < W):
if m[new_y][new_x] == "@":
break
elif M[new_y][new_x] == -1: # まだ来たことない点だったという条件
M[new_y][new_x] = M[y][x] + 1
queue.append([new_y, new_x]) # 新しい点を足す。
elif M[new_y][new_x] < M[y][x] + 1:
break
else:
break
H, W, K = map(int, input().split())
# K = min(K, max(H, W))
x1, y1, x2, y2 = map(int, input().split())
x1, y1, x2, y2 = x1 - 1, y1 - 1, x2 - 1, y2 - 1
m = []
for i in range(H):
m.append(list(map(str, sys.stdin.readline().strip())))
M = [[-1] * W for i in range(H)]
bfs(M, x1, y1, x2, y2)
print(M[x2][y2])
```
Yes
| 95,042 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Snuke, a water strider, lives in a rectangular pond that can be seen as a grid with H east-west rows and W north-south columns. Let (i,j) be the square at the i-th row from the north and j-th column from the west.
Some of the squares have a lotus leaf on it and cannot be entered. The square (i,j) has a lotus leaf on it if c_{ij} is `@`, and it does not if c_{ij} is `.`.
In one stroke, Snuke can move between 1 and K squares (inclusive) toward one of the four directions: north, east, south, and west. The move may not pass through a square with a lotus leaf. Moving to such a square or out of the pond is also forbidden.
Find the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2). If the travel from (x_1,y_1) to (x_2,y_2) is impossible, point out that fact.
Constraints
* 1 \leq H,W,K \leq 10^6
* H \times W \leq 10^6
* 1 \leq x_1,x_2 \leq H
* 1 \leq y_1,y_2 \leq W
* x_1 \neq x_2 or y_1 \neq y_2.
* c_{i,j} is `.` or `@`.
* c_{x_1,y_1} = `.`
* c_{x_2,y_2} = `.`
* All numbers in input are integers.
Input
Input is given from Standard Input in the following format:
H W K
x_1 y_1 x_2 y_2
c_{1,1}c_{1,2} .. c_{1,W}
c_{2,1}c_{2,2} .. c_{2,W}
:
c_{H,1}c_{H,2} .. c_{H,W}
Output
Print the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2), or print `-1` if the travel is impossible.
Examples
Input
3 5 2
3 2 3 4
.....
.@..@
..@..
Output
5
Input
1 6 4
1 1 1 6
......
Output
2
Input
3 3 1
2 1 2 3
.@.
.@.
.@.
Output
-1
Submitted Solution:
```
import queue
import sys
input = sys.stdin.readline
def calc_next_point(x, y, vec, dist):
if vec == 0:
return x-dist, y
elif vec == 1:
return x, y+dist
elif vec == 2:
return x+dist, y
else:
return x, y-dist
def main():
h, w, k = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
pond = [[[-1]*4 for j in range(w+2)] for i in range(h+2)]
for i in range(1, h+1):
s = input()
for j in range(w):
if s[j] == ".":
for l in range(4):
pond[i][j+1][l] = float("inf")
qs = [queue.Queue(), queue.Queue()]
for i in range(4):
pond[x1][y1][i] = 0
qs[0].put([0, x1, y1, i])
p = 0
minans = float("inf")
while not qs[0].empty() or not qs[1].empty():
while not qs[p].empty():
cost, x, y, vec = qs[p].get()
np = (p+1)%2
if cost >= minans:
break
if x == x2 and y == y2:
if cost < minans:
minans = cost
for i in range(1, k+1):
nx, ny = calc_next_point(x, y, vec, i)
if pond[nx][ny][vec] == -1:
break
ncost = cost + 1
if pond[nx][ny][vec] < ncost:
break
pond[nx][ny][vec] = ncost
if nx == x2 and ny == y2:
if ncost < minans:
minans = ncost
for j in range(4):
if i == k:
if j != (vec+2)%4:
if j == vec or pond[nx][ny][j] > ncost:
pond[nx][ny][j] = ncost
qs[np].put([ncost, nx, ny, j])
elif i == 1:
if j != vec:
if j == (vec+2)%4 or pond[nx][ny][j] > ncost:
pond[nx][ny][j] = ncost
qs[np].put([ncost, nx, ny, j])
else:
if j != vec and j != (vec+2)%4:
if pond[nx][ny][j] > ncost:
pond[nx][ny][j] = ncost
qs[np].put([ncost, nx, ny, j])
p = (p+1)%2
if min(pond[x2][y2]) == float("inf"):
print(-1)
else:
print(min(pond[x2][y2]))
if __name__ == "__main__":
main()
```
No
| 95,043 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Snuke, a water strider, lives in a rectangular pond that can be seen as a grid with H east-west rows and W north-south columns. Let (i,j) be the square at the i-th row from the north and j-th column from the west.
Some of the squares have a lotus leaf on it and cannot be entered. The square (i,j) has a lotus leaf on it if c_{ij} is `@`, and it does not if c_{ij} is `.`.
In one stroke, Snuke can move between 1 and K squares (inclusive) toward one of the four directions: north, east, south, and west. The move may not pass through a square with a lotus leaf. Moving to such a square or out of the pond is also forbidden.
Find the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2). If the travel from (x_1,y_1) to (x_2,y_2) is impossible, point out that fact.
Constraints
* 1 \leq H,W,K \leq 10^6
* H \times W \leq 10^6
* 1 \leq x_1,x_2 \leq H
* 1 \leq y_1,y_2 \leq W
* x_1 \neq x_2 or y_1 \neq y_2.
* c_{i,j} is `.` or `@`.
* c_{x_1,y_1} = `.`
* c_{x_2,y_2} = `.`
* All numbers in input are integers.
Input
Input is given from Standard Input in the following format:
H W K
x_1 y_1 x_2 y_2
c_{1,1}c_{1,2} .. c_{1,W}
c_{2,1}c_{2,2} .. c_{2,W}
:
c_{H,1}c_{H,2} .. c_{H,W}
Output
Print the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2), or print `-1` if the travel is impossible.
Examples
Input
3 5 2
3 2 3 4
.....
.@..@
..@..
Output
5
Input
1 6 4
1 1 1 6
......
Output
2
Input
3 3 1
2 1 2 3
.@.
.@.
.@.
Output
-1
Submitted Solution:
```
from collections import deque
import sys
def bfs(xs, ys, d):
queue = deque((xs, ys, d))
M[xs][ys] = d
while queue:
# queueには訪れた地点が入っている。そこから、4方向に移動できるか考え、queueから消す。
x1, y1, d = queue.popleft() # queueに入っていたものを消す。
if [x1, y1] == [xg, yg]: # もしゴールについていたならば、そのときの手数を出す。
return
for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)):
for k in range(1, K + 1):
x2 = x1 + dx * k
y2 = y1 + dy * k
if (0 <= x2 < H) and (0 <= y2 < W):
if m[x2][y2] == "@":
break
elif M[x2][y2] == -1: # まだ来たことない点だったという条件
M[x2][y2] = d + 1
queue.append((x2, y2)) # 新しい点を足す。
elif M[x2][y2] < d + 1:
break
else:
break
H, W, K = map(int, input().split())
# K = min(K, max(H, W))
xs, ys, xg, yg = map(int, input().split())
xs, ys, xg, yg = xs - 1, ys - 1, xg - 1, yg - 1
m = []
for i in range(H):
m.append(list(map(str, sys.stdin.readline().strip())))
M = [[-1] * W for i in range(H)]
bfs(xs, ys, 0)
print(M[xg][yg])
```
No
| 95,044 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Snuke, a water strider, lives in a rectangular pond that can be seen as a grid with H east-west rows and W north-south columns. Let (i,j) be the square at the i-th row from the north and j-th column from the west.
Some of the squares have a lotus leaf on it and cannot be entered. The square (i,j) has a lotus leaf on it if c_{ij} is `@`, and it does not if c_{ij} is `.`.
In one stroke, Snuke can move between 1 and K squares (inclusive) toward one of the four directions: north, east, south, and west. The move may not pass through a square with a lotus leaf. Moving to such a square or out of the pond is also forbidden.
Find the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2). If the travel from (x_1,y_1) to (x_2,y_2) is impossible, point out that fact.
Constraints
* 1 \leq H,W,K \leq 10^6
* H \times W \leq 10^6
* 1 \leq x_1,x_2 \leq H
* 1 \leq y_1,y_2 \leq W
* x_1 \neq x_2 or y_1 \neq y_2.
* c_{i,j} is `.` or `@`.
* c_{x_1,y_1} = `.`
* c_{x_2,y_2} = `.`
* All numbers in input are integers.
Input
Input is given from Standard Input in the following format:
H W K
x_1 y_1 x_2 y_2
c_{1,1}c_{1,2} .. c_{1,W}
c_{2,1}c_{2,2} .. c_{2,W}
:
c_{H,1}c_{H,2} .. c_{H,W}
Output
Print the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2), or print `-1` if the travel is impossible.
Examples
Input
3 5 2
3 2 3 4
.....
.@..@
..@..
Output
5
Input
1 6 4
1 1 1 6
......
Output
2
Input
3 3 1
2 1 2 3
.@.
.@.
.@.
Output
-1
Submitted Solution:
```
h, w, k = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
x1, y1, x2, y2 = x1-1, y1-1, x2-1, y2-1
xxx = set()
for hh in range(h):
c = input()
for ww, cc in enumerate(c):
if cc == '@':
xxx.add((hh, ww))
from heapq import heappush, heappop
q = []
heappush(q, (0, x1, y1))
d = {}
while q:
s, x, y = heappop(q)
for i in range(1, k+1):
xx, yy = x + i, y
if xx >= h or (xx, yy) in xxx:
break
if (xx, yy) in d and d[(xx, yy)] <= s+1:
continue
if xx == x2 and yy == y2:
print(s+1)
exit(0)
d[(xx, yy)] = s+1
heappush(q, (s+1, xx, yy))
for i in range(1, k+1):
xx, yy = x - i, y
if xx < 0 or (xx, yy) in xxx:
break
if (xx, yy) in d and d[(xx, yy)] <= s+1:
continue
if xx == x2 and yy == y2:
print(s+1)
exit(0)
d[(xx, yy)] = s+1
heappush(q, (s+1, xx, yy))
for i in range(1, k+1):
xx, yy = x, y + i
if yy >= w or (xx, yy) in xxx:
break
if (xx, yy) in d and d[(xx, yy)] <= s+1:
continue
if xx == x2 and yy == y2:
print(s+1)
exit(0)
d[(xx, yy)] = s+1
heappush(q, (s+1, xx, yy))
for i in range(1, k+1):
xx, yy = x, y - i
if yy < 0 or (xx, yy) in xxx:
break
if (xx, yy) in d and d[(xx, yy)] <= s+1:
continue
if xx == x2 and yy == y2:
print(s+1)
exit(0)
d[(xx, yy)] = s+1
heappush(q, (s+1, xx, yy))
print(-1)
```
No
| 95,045 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Snuke, a water strider, lives in a rectangular pond that can be seen as a grid with H east-west rows and W north-south columns. Let (i,j) be the square at the i-th row from the north and j-th column from the west.
Some of the squares have a lotus leaf on it and cannot be entered. The square (i,j) has a lotus leaf on it if c_{ij} is `@`, and it does not if c_{ij} is `.`.
In one stroke, Snuke can move between 1 and K squares (inclusive) toward one of the four directions: north, east, south, and west. The move may not pass through a square with a lotus leaf. Moving to such a square or out of the pond is also forbidden.
Find the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2). If the travel from (x_1,y_1) to (x_2,y_2) is impossible, point out that fact.
Constraints
* 1 \leq H,W,K \leq 10^6
* H \times W \leq 10^6
* 1 \leq x_1,x_2 \leq H
* 1 \leq y_1,y_2 \leq W
* x_1 \neq x_2 or y_1 \neq y_2.
* c_{i,j} is `.` or `@`.
* c_{x_1,y_1} = `.`
* c_{x_2,y_2} = `.`
* All numbers in input are integers.
Input
Input is given from Standard Input in the following format:
H W K
x_1 y_1 x_2 y_2
c_{1,1}c_{1,2} .. c_{1,W}
c_{2,1}c_{2,2} .. c_{2,W}
:
c_{H,1}c_{H,2} .. c_{H,W}
Output
Print the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2), or print `-1` if the travel is impossible.
Examples
Input
3 5 2
3 2 3 4
.....
.@..@
..@..
Output
5
Input
1 6 4
1 1 1 6
......
Output
2
Input
3 3 1
2 1 2 3
.@.
.@.
.@.
Output
-1
Submitted Solution:
```
import sys
inf=1000002
sys.setrecursionlimit(inf)
from collections import deque
h,w,k=map(int,input().split())
x1,y1,x2,y2=map(int,input().split())
c=[input() for i in range(h)]
dp=[[inf if c[i][j]=="." else -1 for j in range(w)] for i in range(h)]
now=deque([x1-1,y1-1])
dp[x1-1][y1-1]=0
def bfs(d):
global dp,now
l=len(now)
if l==0:
return
dp_sub=deque()
cand=set()
for i in range(l):
x,y=now.popleft()
for j in range(1,min(k+1,w-y)):
if dp[x][y+j]==inf:
dp_sub.append([x,y+j,d])
cand.add((x,y+j))
else:
break
for j in range(1,min(k+1,y+1)):
if dp[x][y-j]==inf:
dp_sub.append([x,y-j,d])
cand.add((x,y-j))
else:
break
for j in range(1,min(k+1,h-x)):
if dp[x+j][y]==inf:
dp_sub.append([x+j,y,d])
cand.add((x+j,y))
else:
break
for j in range(1,min(k+1,x+1)):
if dp[x-j][y]==inf:
dp_sub.append([x-j,y,d])
cand.add((x-j,y))
else:
break
while dp_sub!=deque([]):
e=dp_sub.popleft()
dp[e[0]][e[1]]=e[2]
for i in cand:
now.append(i)
bfs(d+1)
bfs(1)
print(dp[x2-1][y2-1] if dp[x2-1][y2-1]!=inf else -1)
```
No
| 95,046 |
Provide a correct Python 3 solution for this coding contest problem.
We have N voting papers. The i-th vote (1 \leq i \leq N) has the string S_i written on it.
Print all strings that are written on the most number of votes, in lexicographical order.
Constraints
* 1 \leq N \leq 2 \times 10^5
* S_i (1 \leq i \leq N) are strings consisting of lowercase English letters.
* The length of S_i (1 \leq i \leq N) is between 1 and 10 (inclusive).
Input
Input is given from Standard Input in the following format:
N
S_1
:
S_N
Output
Print all strings in question in lexicographical order.
Examples
Input
7
beat
vet
beet
bed
vet
bet
beet
Output
beet
vet
Input
8
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
Output
buffalo
Input
7
bass
bass
kick
kick
bass
kick
kick
Output
kick
Input
4
ushi
tapu
nichia
kun
Output
kun
nichia
tapu
ushi
"Correct Solution:
```
n=int(input())
S=[input() for _ in range(n)]
from collections import Counter
d=Counter(S)
e=[]
c=d.most_common()[0][1]
for i in d.keys():
if d[i]==c:
e.append(i)
e.sort()
print(*e,sep="\n")
```
| 95,047 |
Provide a correct Python 3 solution for this coding contest problem.
We have N voting papers. The i-th vote (1 \leq i \leq N) has the string S_i written on it.
Print all strings that are written on the most number of votes, in lexicographical order.
Constraints
* 1 \leq N \leq 2 \times 10^5
* S_i (1 \leq i \leq N) are strings consisting of lowercase English letters.
* The length of S_i (1 \leq i \leq N) is between 1 and 10 (inclusive).
Input
Input is given from Standard Input in the following format:
N
S_1
:
S_N
Output
Print all strings in question in lexicographical order.
Examples
Input
7
beat
vet
beet
bed
vet
bet
beet
Output
beet
vet
Input
8
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
Output
buffalo
Input
7
bass
bass
kick
kick
bass
kick
kick
Output
kick
Input
4
ushi
tapu
nichia
kun
Output
kun
nichia
tapu
ushi
"Correct Solution:
```
N=int(input())
S={}
for n in range(N):
s=input()
S[s]=S.get(s,0)+1
maxS=max(S.values())
S=[k for k,v in S.items() if v==maxS]
print('\n'.join(sorted(S)))
```
| 95,048 |
Provide a correct Python 3 solution for this coding contest problem.
We have N voting papers. The i-th vote (1 \leq i \leq N) has the string S_i written on it.
Print all strings that are written on the most number of votes, in lexicographical order.
Constraints
* 1 \leq N \leq 2 \times 10^5
* S_i (1 \leq i \leq N) are strings consisting of lowercase English letters.
* The length of S_i (1 \leq i \leq N) is between 1 and 10 (inclusive).
Input
Input is given from Standard Input in the following format:
N
S_1
:
S_N
Output
Print all strings in question in lexicographical order.
Examples
Input
7
beat
vet
beet
bed
vet
bet
beet
Output
beet
vet
Input
8
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
Output
buffalo
Input
7
bass
bass
kick
kick
bass
kick
kick
Output
kick
Input
4
ushi
tapu
nichia
kun
Output
kun
nichia
tapu
ushi
"Correct Solution:
```
n = int(input())
d = {}
for i in range(n):
s = input()
d[s] = d.get(s,0)+1
m = max(d[i] for i in d)
for s in sorted(d):
if d[s]==m:
print(s)
```
| 95,049 |
Provide a correct Python 3 solution for this coding contest problem.
We have N voting papers. The i-th vote (1 \leq i \leq N) has the string S_i written on it.
Print all strings that are written on the most number of votes, in lexicographical order.
Constraints
* 1 \leq N \leq 2 \times 10^5
* S_i (1 \leq i \leq N) are strings consisting of lowercase English letters.
* The length of S_i (1 \leq i \leq N) is between 1 and 10 (inclusive).
Input
Input is given from Standard Input in the following format:
N
S_1
:
S_N
Output
Print all strings in question in lexicographical order.
Examples
Input
7
beat
vet
beet
bed
vet
bet
beet
Output
beet
vet
Input
8
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
Output
buffalo
Input
7
bass
bass
kick
kick
bass
kick
kick
Output
kick
Input
4
ushi
tapu
nichia
kun
Output
kun
nichia
tapu
ushi
"Correct Solution:
```
N = int(input())
d = {}
for i in range(N):
tmp = input()
d[tmp] = d.get(tmp,0) + 1
m = max(d[i] for i in d)
max_list = [i for i in d if d[i] == m]
print("\n".join(sorted(max_list)))
```
| 95,050 |
Provide a correct Python 3 solution for this coding contest problem.
We have N voting papers. The i-th vote (1 \leq i \leq N) has the string S_i written on it.
Print all strings that are written on the most number of votes, in lexicographical order.
Constraints
* 1 \leq N \leq 2 \times 10^5
* S_i (1 \leq i \leq N) are strings consisting of lowercase English letters.
* The length of S_i (1 \leq i \leq N) is between 1 and 10 (inclusive).
Input
Input is given from Standard Input in the following format:
N
S_1
:
S_N
Output
Print all strings in question in lexicographical order.
Examples
Input
7
beat
vet
beet
bed
vet
bet
beet
Output
beet
vet
Input
8
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
Output
buffalo
Input
7
bass
bass
kick
kick
bass
kick
kick
Output
kick
Input
4
ushi
tapu
nichia
kun
Output
kun
nichia
tapu
ushi
"Correct Solution:
```
from collections import Counter
n = int(input())
s = [input() for i in range(n)]
c = Counter(s)
mc = c.most_common()
ans = [i[0] for i in mc if i[1] == mc[0][1]]
ans.sort()
for i in ans:
print(i)
```
| 95,051 |
Provide a correct Python 3 solution for this coding contest problem.
We have N voting papers. The i-th vote (1 \leq i \leq N) has the string S_i written on it.
Print all strings that are written on the most number of votes, in lexicographical order.
Constraints
* 1 \leq N \leq 2 \times 10^5
* S_i (1 \leq i \leq N) are strings consisting of lowercase English letters.
* The length of S_i (1 \leq i \leq N) is between 1 and 10 (inclusive).
Input
Input is given from Standard Input in the following format:
N
S_1
:
S_N
Output
Print all strings in question in lexicographical order.
Examples
Input
7
beat
vet
beet
bed
vet
bet
beet
Output
beet
vet
Input
8
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
Output
buffalo
Input
7
bass
bass
kick
kick
bass
kick
kick
Output
kick
Input
4
ushi
tapu
nichia
kun
Output
kun
nichia
tapu
ushi
"Correct Solution:
```
from collections import defaultdict
n = int(input())
dic = defaultdict(int)
for i in range(n):
dic[input()] += 1
max_s = max(dic.values())
for k, v in sorted(dic.items()):
if v == max_s:
print(k)
```
| 95,052 |
Provide a correct Python 3 solution for this coding contest problem.
We have N voting papers. The i-th vote (1 \leq i \leq N) has the string S_i written on it.
Print all strings that are written on the most number of votes, in lexicographical order.
Constraints
* 1 \leq N \leq 2 \times 10^5
* S_i (1 \leq i \leq N) are strings consisting of lowercase English letters.
* The length of S_i (1 \leq i \leq N) is between 1 and 10 (inclusive).
Input
Input is given from Standard Input in the following format:
N
S_1
:
S_N
Output
Print all strings in question in lexicographical order.
Examples
Input
7
beat
vet
beet
bed
vet
bet
beet
Output
beet
vet
Input
8
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
Output
buffalo
Input
7
bass
bass
kick
kick
bass
kick
kick
Output
kick
Input
4
ushi
tapu
nichia
kun
Output
kun
nichia
tapu
ushi
"Correct Solution:
```
n=int(input())
c={}
for _ in range(n): s=input(); c[s]=c.get(s,0)+1
m=max(c.values())
print(*sorted(k for k,v in c.items() if v==m))
```
| 95,053 |
Provide a correct Python 3 solution for this coding contest problem.
We have N voting papers. The i-th vote (1 \leq i \leq N) has the string S_i written on it.
Print all strings that are written on the most number of votes, in lexicographical order.
Constraints
* 1 \leq N \leq 2 \times 10^5
* S_i (1 \leq i \leq N) are strings consisting of lowercase English letters.
* The length of S_i (1 \leq i \leq N) is between 1 and 10 (inclusive).
Input
Input is given from Standard Input in the following format:
N
S_1
:
S_N
Output
Print all strings in question in lexicographical order.
Examples
Input
7
beat
vet
beet
bed
vet
bet
beet
Output
beet
vet
Input
8
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
Output
buffalo
Input
7
bass
bass
kick
kick
bass
kick
kick
Output
kick
Input
4
ushi
tapu
nichia
kun
Output
kun
nichia
tapu
ushi
"Correct Solution:
```
from collections import Counter
N = int(input())
S = [input() for _ in range(N)]
c = Counter(S)
c_max = max(c.values())
[print(x) for x in sorted([s for s in set(S) if c[s] == c_max])]
```
| 95,054 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have N voting papers. The i-th vote (1 \leq i \leq N) has the string S_i written on it.
Print all strings that are written on the most number of votes, in lexicographical order.
Constraints
* 1 \leq N \leq 2 \times 10^5
* S_i (1 \leq i \leq N) are strings consisting of lowercase English letters.
* The length of S_i (1 \leq i \leq N) is between 1 and 10 (inclusive).
Input
Input is given from Standard Input in the following format:
N
S_1
:
S_N
Output
Print all strings in question in lexicographical order.
Examples
Input
7
beat
vet
beet
bed
vet
bet
beet
Output
beet
vet
Input
8
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
Output
buffalo
Input
7
bass
bass
kick
kick
bass
kick
kick
Output
kick
Input
4
ushi
tapu
nichia
kun
Output
kun
nichia
tapu
ushi
Submitted Solution:
```
import collections
n = int(input())
l = [str(input()) for i in range(n)]
c = collections.Counter(l).most_common()[::1]
res = sorted([i[0] for i in c if i[1] == c[0][1]])
[print(i) for i in res]
```
Yes
| 95,055 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have N voting papers. The i-th vote (1 \leq i \leq N) has the string S_i written on it.
Print all strings that are written on the most number of votes, in lexicographical order.
Constraints
* 1 \leq N \leq 2 \times 10^5
* S_i (1 \leq i \leq N) are strings consisting of lowercase English letters.
* The length of S_i (1 \leq i \leq N) is between 1 and 10 (inclusive).
Input
Input is given from Standard Input in the following format:
N
S_1
:
S_N
Output
Print all strings in question in lexicographical order.
Examples
Input
7
beat
vet
beet
bed
vet
bet
beet
Output
beet
vet
Input
8
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
Output
buffalo
Input
7
bass
bass
kick
kick
bass
kick
kick
Output
kick
Input
4
ushi
tapu
nichia
kun
Output
kun
nichia
tapu
ushi
Submitted Solution:
```
import collections as coll
N = int(input())
A = [input() for _ in range(N)]
C = coll.Counter(A).most_common()
C = sorted([k for k,v in C if v == C[0][1]])
print(*C, sep='\n')
```
Yes
| 95,056 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have N voting papers. The i-th vote (1 \leq i \leq N) has the string S_i written on it.
Print all strings that are written on the most number of votes, in lexicographical order.
Constraints
* 1 \leq N \leq 2 \times 10^5
* S_i (1 \leq i \leq N) are strings consisting of lowercase English letters.
* The length of S_i (1 \leq i \leq N) is between 1 and 10 (inclusive).
Input
Input is given from Standard Input in the following format:
N
S_1
:
S_N
Output
Print all strings in question in lexicographical order.
Examples
Input
7
beat
vet
beet
bed
vet
bet
beet
Output
beet
vet
Input
8
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
Output
buffalo
Input
7
bass
bass
kick
kick
bass
kick
kick
Output
kick
Input
4
ushi
tapu
nichia
kun
Output
kun
nichia
tapu
ushi
Submitted Solution:
```
from collections import Counter
N = int(input())
c = Counter([input() for _ in range(N)])
mv = max(c.values())
[print(w) for w in sorted(c.keys()) if c[w] == mv]
```
Yes
| 95,057 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have N voting papers. The i-th vote (1 \leq i \leq N) has the string S_i written on it.
Print all strings that are written on the most number of votes, in lexicographical order.
Constraints
* 1 \leq N \leq 2 \times 10^5
* S_i (1 \leq i \leq N) are strings consisting of lowercase English letters.
* The length of S_i (1 \leq i \leq N) is between 1 and 10 (inclusive).
Input
Input is given from Standard Input in the following format:
N
S_1
:
S_N
Output
Print all strings in question in lexicographical order.
Examples
Input
7
beat
vet
beet
bed
vet
bet
beet
Output
beet
vet
Input
8
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
Output
buffalo
Input
7
bass
bass
kick
kick
bass
kick
kick
Output
kick
Input
4
ushi
tapu
nichia
kun
Output
kun
nichia
tapu
ushi
Submitted Solution:
```
import collections
n = int(input())
c = collections.Counter([ input() for i in range(n) ])
ma = max(c.values())
print(*sorted([ i for i, j in c.items() if j == ma ]), sep='\n')
```
Yes
| 95,058 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have N voting papers. The i-th vote (1 \leq i \leq N) has the string S_i written on it.
Print all strings that are written on the most number of votes, in lexicographical order.
Constraints
* 1 \leq N \leq 2 \times 10^5
* S_i (1 \leq i \leq N) are strings consisting of lowercase English letters.
* The length of S_i (1 \leq i \leq N) is between 1 and 10 (inclusive).
Input
Input is given from Standard Input in the following format:
N
S_1
:
S_N
Output
Print all strings in question in lexicographical order.
Examples
Input
7
beat
vet
beet
bed
vet
bet
beet
Output
beet
vet
Input
8
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
Output
buffalo
Input
7
bass
bass
kick
kick
bass
kick
kick
Output
kick
Input
4
ushi
tapu
nichia
kun
Output
kun
nichia
tapu
ushi
Submitted Solution:
```
N = int(input())
S = []
for i in range(N):
tmp = input()
S.append(tmp)
S.sort()
if N == 1:
print(S[0])
exit()
ans = []
SS = []
count = 1
print(S)
for i in range(N-1):
if i == N-2:
if S[i] == S[i+1]:
count += 1
ans.append(count)
SS.append(S[i])
else:
ans.append(count)
SS.append(S[i])
ans.append(1)
SS.append(S[i+1])
elif S[i] == S[i+1]:
count += 1
else:
ans.append(count)
SS.append(S[i])
count = 1
m = max(ans)
for i in range(len(SS)):
if ans[i] == m:
print(SS[i])
```
No
| 95,059 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have N voting papers. The i-th vote (1 \leq i \leq N) has the string S_i written on it.
Print all strings that are written on the most number of votes, in lexicographical order.
Constraints
* 1 \leq N \leq 2 \times 10^5
* S_i (1 \leq i \leq N) are strings consisting of lowercase English letters.
* The length of S_i (1 \leq i \leq N) is between 1 and 10 (inclusive).
Input
Input is given from Standard Input in the following format:
N
S_1
:
S_N
Output
Print all strings in question in lexicographical order.
Examples
Input
7
beat
vet
beet
bed
vet
bet
beet
Output
beet
vet
Input
8
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
Output
buffalo
Input
7
bass
bass
kick
kick
bass
kick
kick
Output
kick
Input
4
ushi
tapu
nichia
kun
Output
kun
nichia
tapu
ushi
Submitted Solution:
```
N = int(input())
s = [0] * N
time = [1] * N
j = 0
for i in range(N):
inp = str(input())
if inp in s:
x = s.index(inp)
time[x] += 1
else:
s[j] = inp
j += 1
ans = max(time)
#print(s, time)
for i in range(N):
if time[i] == ans:
if s[i] != 0:
print(s[i])
```
No
| 95,060 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have N voting papers. The i-th vote (1 \leq i \leq N) has the string S_i written on it.
Print all strings that are written on the most number of votes, in lexicographical order.
Constraints
* 1 \leq N \leq 2 \times 10^5
* S_i (1 \leq i \leq N) are strings consisting of lowercase English letters.
* The length of S_i (1 \leq i \leq N) is between 1 and 10 (inclusive).
Input
Input is given from Standard Input in the following format:
N
S_1
:
S_N
Output
Print all strings in question in lexicographical order.
Examples
Input
7
beat
vet
beet
bed
vet
bet
beet
Output
beet
vet
Input
8
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
Output
buffalo
Input
7
bass
bass
kick
kick
bass
kick
kick
Output
kick
Input
4
ushi
tapu
nichia
kun
Output
kun
nichia
tapu
ushi
Submitted Solution:
```
from collections import Counter
n=int(input())
s = [input() for _ in range(n)]
c=[i for i,j in Counter(s).items() if j == max(Counter(s).values())]
for k in c:
print(k)
```
No
| 95,061 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
We have N voting papers. The i-th vote (1 \leq i \leq N) has the string S_i written on it.
Print all strings that are written on the most number of votes, in lexicographical order.
Constraints
* 1 \leq N \leq 2 \times 10^5
* S_i (1 \leq i \leq N) are strings consisting of lowercase English letters.
* The length of S_i (1 \leq i \leq N) is between 1 and 10 (inclusive).
Input
Input is given from Standard Input in the following format:
N
S_1
:
S_N
Output
Print all strings in question in lexicographical order.
Examples
Input
7
beat
vet
beet
bed
vet
bet
beet
Output
beet
vet
Input
8
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
buffalo
Output
buffalo
Input
7
bass
bass
kick
kick
bass
kick
kick
Output
kick
Input
4
ushi
tapu
nichia
kun
Output
kun
nichia
tapu
ushi
Submitted Solution:
```
import sys
input = sys.stdin.readline
N = int(input())
S = []
for x in range(N):
S.append(input().rstrip())
S_label_n = []
S_label = list(set(S))
for x in S_label:
S_label_n.append(S.count(x))
max_n =max(S_label_n)
maxIndex = [i for i, x in enumerate(S_label_n) if x ==max_n ]
ans_list = []
for x in maxIndex:
ans_list.append(S_label[x])
ans_list.sort()
for x in ans_list:
print(x)
```
No
| 95,062 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Snuke has permutations (P_0,P_1,\cdots,P_{N-1}) and (Q_0,Q_1,\cdots,Q_{N-1}) of (0,1,\cdots,N-1).
Now, he will make new permutations A and B of (0,1,\cdots,N-1), under the following conditions:
* For each i (0 \leq i \leq N-1), A_i should be i or P_i.
* For each i (0 \leq i \leq N-1), B_i should be i or Q_i.
Let us define the distance of permutations A and B as the number of indices i such that A_i \neq B_i. Find the maximum possible distance of A and B.
Constraints
* 1 \leq N \leq 100000
* 0 \leq P_i \leq N-1
* P_0,P_1,\cdots,P_{N-1} are all different.
* 0 \leq Q_i \leq N-1
* Q_0,Q_1,\cdots,Q_{N-1} are all different.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
P_0 P_1 \cdots P_{N-1}
Q_0 Q_1 \cdots Q_{N-1}
Output
Print the maximum possible distance of A and B.
Examples
Input
4
2 1 3 0
0 2 3 1
Output
3
Input
10
0 4 5 3 7 8 2 1 9 6
3 8 5 6 4 0 2 1 7 9
Output
8
Input
32
22 31 30 29 7 17 16 3 14 9 19 11 2 5 10 1 25 18 15 24 20 0 12 21 27 4 26 28 8 6 23 13
22 3 2 7 17 9 16 4 14 8 19 26 28 5 10 1 25 18 15 13 11 0 12 23 21 20 29 24 27 6 30 31
Output
28
Submitted Solution:
```
import numpy as np
n = int(input())
a = np.array(list(map(int,input().split())))
b = np.array(list(map(int,input().split())))
c = (a - b)
d = [s for s in c if s != 0]
print(len(d))
```
No
| 95,063 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Snuke has permutations (P_0,P_1,\cdots,P_{N-1}) and (Q_0,Q_1,\cdots,Q_{N-1}) of (0,1,\cdots,N-1).
Now, he will make new permutations A and B of (0,1,\cdots,N-1), under the following conditions:
* For each i (0 \leq i \leq N-1), A_i should be i or P_i.
* For each i (0 \leq i \leq N-1), B_i should be i or Q_i.
Let us define the distance of permutations A and B as the number of indices i such that A_i \neq B_i. Find the maximum possible distance of A and B.
Constraints
* 1 \leq N \leq 100000
* 0 \leq P_i \leq N-1
* P_0,P_1,\cdots,P_{N-1} are all different.
* 0 \leq Q_i \leq N-1
* Q_0,Q_1,\cdots,Q_{N-1} are all different.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
P_0 P_1 \cdots P_{N-1}
Q_0 Q_1 \cdots Q_{N-1}
Output
Print the maximum possible distance of A and B.
Examples
Input
4
2 1 3 0
0 2 3 1
Output
3
Input
10
0 4 5 3 7 8 2 1 9 6
3 8 5 6 4 0 2 1 7 9
Output
8
Input
32
22 31 30 29 7 17 16 3 14 9 19 11 2 5 10 1 25 18 15 24 20 0 12 21 27 4 26 28 8 6 23 13
22 3 2 7 17 9 16 4 14 8 19 26 28 5 10 1 25 18 15 13 11 0 12 23 21 20 29 24 27 6 30 31
Output
28
Submitted Solution:
```
from collections import deque
# 枝を表すクラス
# altは逆の枝を表す
class Edge:
def __init__(self, from_v, to_v, cap):
self.from_v = from_v
self.to_v = to_v
self.cap = cap
self.alt = None
# 頂点を表すクラス
class Vertex:
def __init__(self, id):
self.id = id
self.edge_list = []
# グラフを表すクラス
class Graph:
# 初期化する
# vnumは頂点数を表す
# s_vは始点、t_vは終点を表す
def __init__(self, vnum):
self.__vertex_list = [Vertex(i) for i in range(vnum + 2)]
self.__edge_list = []
# 枝を追加する
def add_edge(self, from_id, to_id, cap):
from_v = self.vertex(from_id)
to_v = self.vertex(to_id)
normal_edge = Edge(from_v,to_v, cap)
reverse_edge = Edge(to_v, from_v, 0)
normal_edge.alt = reverse_edge
reverse_edge.alt = normal_edge
from_v.edge_list.append(normal_edge)
to_v.edge_list.append(reverse_edge)
self.__edge_list.append(normal_edge)
self.__edge_list.append(reverse_edge)
def vertex(self, id):
return self.__vertex_list[id]
# 辺の状況をprintする
def print_edge(self):
for i in range(len(self.__edge_list)):
print("[f_v={}, ".format(self.__edge_list[i].from_v.id),end = "")
print("t_v={}, ".format(self.__edge_list[i].to_v.id),end = "")
print("cap={}]".format(self.__edge_list[i].cap))
# print(self.__edge_list.from_v)
# print(self.__edge_list.to_v)
# print(self.__edge_list.cap)
# print(self.__edge_list.alt)
class MaxFlow:
#
def __init__(self, graph, s_id, t_id):
self.graph = graph
self.s_v = graph.vertex(s_id)
self.t_v = graph.vertex(t_id)
self.dq = deque()
self.from_dict = dict()
def calc(self):
f = 0
while self.find_path():
f += 1
# デバッグ用
self.graph.print_edge()
print("f = {}".format(f))
self.from_dict.clear()
return(f)
# sからtへのパスを一つ見つけ、そのパス上の枝の容量を0にし、
# 逆の枝の容量を1にする
# 経路が見つかればTrue,見つからなければFalseを返す
def find_path(self):
# -1はfrom_idとして不正な値
self.put_q(self.s_v, None)
s_id = self.s_v.id
t_id = self.t_v.id
while len(self.dq) > 0:
v = self.dq.popleft()
v_id = v.id
if v_id == t_id:
# from_dictを使ってs_vまでの経路をたどる
while v_id != s_id:
n_e = self.from_dict[v_id]
r_e = n_e.alt
n_e.cap = 0
r_e.cap = 1
v_id = n_e.from_v.id
return True
# vに接続した枝eのうち、以下の条件を満たしたものを探す
# * eの容量が1である
# * e.toがfrom_dictに含まれていない
for e in v.edge_list:
if e.cap == 0:
continue
if e.to_v in self.from_dict:
continue
self.put_q(e.to_v, e)
return False
# vをqに積む、from_dictにvに至る枝eを記録する
def put_q(self, v, e):
self.dq.append(v)
self.from_dict[v.id] = e
if __name__ == "__main__":
def make_graph1():
g1 = Graph(2)
s1 = 0
t1 = 1
g1.add_edge(s1, t1, 1)
return g1, s1, t1
g1, s1, t1 = make_graph1()
mf1 = MaxFlow(g1, s1, t1)
f1 = mf1.calc()
print("f1 ={}".format(f1))
def make_graph2():
g = Graph(4)
s = 0
a = 1
b = 2
t = 3
g.add_edge(s, a, 1)
g.add_edge(s, b, 1)
g.add_edge(a, b, 1)
g.add_edge(a, t, 1)
g.add_edge(b, t, 1)
return g, s ,t
g2, s2, t2 = make_graph2()
mf2 = MaxFlow(g2, s2, t2)
print("f2 = {}".format(mf2.calc()))
```
No
| 95,064 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Snuke has permutations (P_0,P_1,\cdots,P_{N-1}) and (Q_0,Q_1,\cdots,Q_{N-1}) of (0,1,\cdots,N-1).
Now, he will make new permutations A and B of (0,1,\cdots,N-1), under the following conditions:
* For each i (0 \leq i \leq N-1), A_i should be i or P_i.
* For each i (0 \leq i \leq N-1), B_i should be i or Q_i.
Let us define the distance of permutations A and B as the number of indices i such that A_i \neq B_i. Find the maximum possible distance of A and B.
Constraints
* 1 \leq N \leq 100000
* 0 \leq P_i \leq N-1
* P_0,P_1,\cdots,P_{N-1} are all different.
* 0 \leq Q_i \leq N-1
* Q_0,Q_1,\cdots,Q_{N-1} are all different.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
P_0 P_1 \cdots P_{N-1}
Q_0 Q_1 \cdots Q_{N-1}
Output
Print the maximum possible distance of A and B.
Examples
Input
4
2 1 3 0
0 2 3 1
Output
3
Input
10
0 4 5 3 7 8 2 1 9 6
3 8 5 6 4 0 2 1 7 9
Output
8
Input
32
22 31 30 29 7 17 16 3 14 9 19 11 2 5 10 1 25 18 15 24 20 0 12 21 27 4 26 28 8 6 23 13
22 3 2 7 17 9 16 4 14 8 19 26 28 5 10 1 25 18 15 13 11 0 12 23 21 20 29 24 27 6 30 31
Output
28
Submitted Solution:
```
import networkx as nx
from networkx.algorithms.flow import dinitz
import sys
def input():
return sys.stdin.readline()[:-1]
G = nx.Graph()
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
G.add_nodes_from(list(range(2*n+2)))
S, T = 2*n, 2*n+1
INF = 10**6
for i in range(2*n):
if i < n:
if p[i] != i:
G.add_edge(p[i], i, capacity=INF)
if p[i] == q[i]:
G.add_edge(i+n, i, capacity=1)
else:
G.add_edge(S, i, capacity=INF)
G.add_edge(i, i+n, capacity=1)
else:
if q[i-n] != i-n:
G.add_edge(i, q[i-n]+n, capacity=INF)
else:
G.add_edge(i, T, capacity=INF)
R = dinitz(G, S, T)
cut = int(R.graph['flow_value'])
#print(cut)
ans = n - cut
print(ans)
```
No
| 95,065 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Snuke has permutations (P_0,P_1,\cdots,P_{N-1}) and (Q_0,Q_1,\cdots,Q_{N-1}) of (0,1,\cdots,N-1).
Now, he will make new permutations A and B of (0,1,\cdots,N-1), under the following conditions:
* For each i (0 \leq i \leq N-1), A_i should be i or P_i.
* For each i (0 \leq i \leq N-1), B_i should be i or Q_i.
Let us define the distance of permutations A and B as the number of indices i such that A_i \neq B_i. Find the maximum possible distance of A and B.
Constraints
* 1 \leq N \leq 100000
* 0 \leq P_i \leq N-1
* P_0,P_1,\cdots,P_{N-1} are all different.
* 0 \leq Q_i \leq N-1
* Q_0,Q_1,\cdots,Q_{N-1} are all different.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
P_0 P_1 \cdots P_{N-1}
Q_0 Q_1 \cdots Q_{N-1}
Output
Print the maximum possible distance of A and B.
Examples
Input
4
2 1 3 0
0 2 3 1
Output
3
Input
10
0 4 5 3 7 8 2 1 9 6
3 8 5 6 4 0 2 1 7 9
Output
8
Input
32
22 31 30 29 7 17 16 3 14 9 19 11 2 5 10 1 25 18 15 24 20 0 12 21 27 4 26 28 8 6 23 13
22 3 2 7 17 9 16 4 14 8 19 26 28 5 10 1 25 18 15 13 11 0 12 23 21 20 29 24 27 6 30 31
Output
28
Submitted Solution:
```
def mapcount(n):
if n != 0:
return 1
else:
return 0
import numpy as np
N = int(input())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
V = [i for i in range(N)]
npP = np.array(P)
npQ = np.array(Q)
npV = np.array(V)
countPQ = sum(map(mapcount,npP-npQ))
countQV = sum(map(mapcount,npQ-npV))
countVP = sum(map(mapcount,npV-npP))
print(max([countPQ,countQV,countVP]))
```
No
| 95,066 |
Provide a correct Python 3 solution for this coding contest problem.
Snuke has a fair N-sided die that shows the integers from 1 to N with equal probability and a fair coin. He will play the following game with them:
1. Throw the die. The current score is the result of the die.
2. As long as the score is between 1 and K-1 (inclusive), keep flipping the coin. The score is doubled each time the coin lands heads up, and the score becomes 0 if the coin lands tails up.
3. The game ends when the score becomes 0 or becomes K or above. Snuke wins if the score is K or above, and loses if the score is 0.
You are given N and K. Find the probability that Snuke wins the game.
Constraints
* 1 ≤ N ≤ 10^5
* 1 ≤ K ≤ 10^5
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
Output
Print the probability that Snuke wins the game. The output is considered correct when the absolute or relative error is at most 10^{-9}.
Examples
Input
3 10
Output
0.145833333333
Input
100000 5
Output
0.999973749998
"Correct Solution:
```
n,k=map(int,input().split());a=0
for i in range(n):
i+=1;p=1/n
while i<k:i*=2;p/=2
a+=p
print(a)
```
| 95,067 |
Provide a correct Python 3 solution for this coding contest problem.
Snuke has a fair N-sided die that shows the integers from 1 to N with equal probability and a fair coin. He will play the following game with them:
1. Throw the die. The current score is the result of the die.
2. As long as the score is between 1 and K-1 (inclusive), keep flipping the coin. The score is doubled each time the coin lands heads up, and the score becomes 0 if the coin lands tails up.
3. The game ends when the score becomes 0 or becomes K or above. Snuke wins if the score is K or above, and loses if the score is 0.
You are given N and K. Find the probability that Snuke wins the game.
Constraints
* 1 ≤ N ≤ 10^5
* 1 ≤ K ≤ 10^5
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
Output
Print the probability that Snuke wins the game. The output is considered correct when the absolute or relative error is at most 10^{-9}.
Examples
Input
3 10
Output
0.145833333333
Input
100000 5
Output
0.999973749998
"Correct Solution:
```
#abc126c
n,k=list(map(int,input().split()))
res=0
for i in range(1,n+1):
p=1
t=1
while i*t<k:
t*=2
p/=2
res+=p/n
print(res)
```
| 95,068 |
Provide a correct Python 3 solution for this coding contest problem.
Snuke has a fair N-sided die that shows the integers from 1 to N with equal probability and a fair coin. He will play the following game with them:
1. Throw the die. The current score is the result of the die.
2. As long as the score is between 1 and K-1 (inclusive), keep flipping the coin. The score is doubled each time the coin lands heads up, and the score becomes 0 if the coin lands tails up.
3. The game ends when the score becomes 0 or becomes K or above. Snuke wins if the score is K or above, and loses if the score is 0.
You are given N and K. Find the probability that Snuke wins the game.
Constraints
* 1 ≤ N ≤ 10^5
* 1 ≤ K ≤ 10^5
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
Output
Print the probability that Snuke wins the game. The output is considered correct when the absolute or relative error is at most 10^{-9}.
Examples
Input
3 10
Output
0.145833333333
Input
100000 5
Output
0.999973749998
"Correct Solution:
```
n,k=map(int,input().split())
a=0
for i in range(1,n+1):
j=0
while i<k:i*=2;j+=1
a+=1/n*.5**j
print(a)
```
| 95,069 |
Provide a correct Python 3 solution for this coding contest problem.
Snuke has a fair N-sided die that shows the integers from 1 to N with equal probability and a fair coin. He will play the following game with them:
1. Throw the die. The current score is the result of the die.
2. As long as the score is between 1 and K-1 (inclusive), keep flipping the coin. The score is doubled each time the coin lands heads up, and the score becomes 0 if the coin lands tails up.
3. The game ends when the score becomes 0 or becomes K or above. Snuke wins if the score is K or above, and loses if the score is 0.
You are given N and K. Find the probability that Snuke wins the game.
Constraints
* 1 ≤ N ≤ 10^5
* 1 ≤ K ≤ 10^5
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
Output
Print the probability that Snuke wins the game. The output is considered correct when the absolute or relative error is at most 10^{-9}.
Examples
Input
3 10
Output
0.145833333333
Input
100000 5
Output
0.999973749998
"Correct Solution:
```
n,k=map(int,input().split())
p=0
for i in range(1,n+1):
if i<k: p+=4*0.5**len(bin(~-k//i))
else: p+=1
print(p/n)
```
| 95,070 |
Provide a correct Python 3 solution for this coding contest problem.
Snuke has a fair N-sided die that shows the integers from 1 to N with equal probability and a fair coin. He will play the following game with them:
1. Throw the die. The current score is the result of the die.
2. As long as the score is between 1 and K-1 (inclusive), keep flipping the coin. The score is doubled each time the coin lands heads up, and the score becomes 0 if the coin lands tails up.
3. The game ends when the score becomes 0 or becomes K or above. Snuke wins if the score is K or above, and loses if the score is 0.
You are given N and K. Find the probability that Snuke wins the game.
Constraints
* 1 ≤ N ≤ 10^5
* 1 ≤ K ≤ 10^5
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
Output
Print the probability that Snuke wins the game. The output is considered correct when the absolute or relative error is at most 10^{-9}.
Examples
Input
3 10
Output
0.145833333333
Input
100000 5
Output
0.999973749998
"Correct Solution:
```
n,k = map(int, input().split())
ans = 0
for i in range(1,n+1):
p = 0
while i < k:
i *= 2
p += 1
ans += 1 / 2**p
print(ans/n)
```
| 95,071 |
Provide a correct Python 3 solution for this coding contest problem.
Snuke has a fair N-sided die that shows the integers from 1 to N with equal probability and a fair coin. He will play the following game with them:
1. Throw the die. The current score is the result of the die.
2. As long as the score is between 1 and K-1 (inclusive), keep flipping the coin. The score is doubled each time the coin lands heads up, and the score becomes 0 if the coin lands tails up.
3. The game ends when the score becomes 0 or becomes K or above. Snuke wins if the score is K or above, and loses if the score is 0.
You are given N and K. Find the probability that Snuke wins the game.
Constraints
* 1 ≤ N ≤ 10^5
* 1 ≤ K ≤ 10^5
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
Output
Print the probability that Snuke wins the game. The output is considered correct when the absolute or relative error is at most 10^{-9}.
Examples
Input
3 10
Output
0.145833333333
Input
100000 5
Output
0.999973749998
"Correct Solution:
```
n,k=map(int,input().split())
d=0
for i in range(1,n+1):
a=i
b=0
c=0
while a<k:
a=a*2
b=b+1
c=(1/2)**b
d=d+c
print(d/n)
```
| 95,072 |
Provide a correct Python 3 solution for this coding contest problem.
Snuke has a fair N-sided die that shows the integers from 1 to N with equal probability and a fair coin. He will play the following game with them:
1. Throw the die. The current score is the result of the die.
2. As long as the score is between 1 and K-1 (inclusive), keep flipping the coin. The score is doubled each time the coin lands heads up, and the score becomes 0 if the coin lands tails up.
3. The game ends when the score becomes 0 or becomes K or above. Snuke wins if the score is K or above, and loses if the score is 0.
You are given N and K. Find the probability that Snuke wins the game.
Constraints
* 1 ≤ N ≤ 10^5
* 1 ≤ K ≤ 10^5
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
Output
Print the probability that Snuke wins the game. The output is considered correct when the absolute or relative error is at most 10^{-9}.
Examples
Input
3 10
Output
0.145833333333
Input
100000 5
Output
0.999973749998
"Correct Solution:
```
import math
n,k = list(map(int, input().split()))
print(sum([(1/2)**max(math.ceil(math.log2(k/(i+1))),0)/n for i in range(n)]))
```
| 95,073 |
Provide a correct Python 3 solution for this coding contest problem.
Snuke has a fair N-sided die that shows the integers from 1 to N with equal probability and a fair coin. He will play the following game with them:
1. Throw the die. The current score is the result of the die.
2. As long as the score is between 1 and K-1 (inclusive), keep flipping the coin. The score is doubled each time the coin lands heads up, and the score becomes 0 if the coin lands tails up.
3. The game ends when the score becomes 0 or becomes K or above. Snuke wins if the score is K or above, and loses if the score is 0.
You are given N and K. Find the probability that Snuke wins the game.
Constraints
* 1 ≤ N ≤ 10^5
* 1 ≤ K ≤ 10^5
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
Output
Print the probability that Snuke wins the game. The output is considered correct when the absolute or relative error is at most 10^{-9}.
Examples
Input
3 10
Output
0.145833333333
Input
100000 5
Output
0.999973749998
"Correct Solution:
```
N,K=map(int,input().split())
ans=0
for i in range(1,N+1):
k=i
p=1
while k<K:
p*=2
k*=2
ans+=1/p
print(ans/N)
```
| 95,074 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Snuke has a fair N-sided die that shows the integers from 1 to N with equal probability and a fair coin. He will play the following game with them:
1. Throw the die. The current score is the result of the die.
2. As long as the score is between 1 and K-1 (inclusive), keep flipping the coin. The score is doubled each time the coin lands heads up, and the score becomes 0 if the coin lands tails up.
3. The game ends when the score becomes 0 or becomes K or above. Snuke wins if the score is K or above, and loses if the score is 0.
You are given N and K. Find the probability that Snuke wins the game.
Constraints
* 1 ≤ N ≤ 10^5
* 1 ≤ K ≤ 10^5
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
Output
Print the probability that Snuke wins the game. The output is considered correct when the absolute or relative error is at most 10^{-9}.
Examples
Input
3 10
Output
0.145833333333
Input
100000 5
Output
0.999973749998
Submitted Solution:
```
import math
n,k=map(int,input().split())
ans=0
for i in range(1,n+1):
ans+=((1/2)**max(0,math.ceil(math.log2(k/i))))*1/n
print(ans)
```
Yes
| 95,075 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Snuke has a fair N-sided die that shows the integers from 1 to N with equal probability and a fair coin. He will play the following game with them:
1. Throw the die. The current score is the result of the die.
2. As long as the score is between 1 and K-1 (inclusive), keep flipping the coin. The score is doubled each time the coin lands heads up, and the score becomes 0 if the coin lands tails up.
3. The game ends when the score becomes 0 or becomes K or above. Snuke wins if the score is K or above, and loses if the score is 0.
You are given N and K. Find the probability that Snuke wins the game.
Constraints
* 1 ≤ N ≤ 10^5
* 1 ≤ K ≤ 10^5
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
Output
Print the probability that Snuke wins the game. The output is considered correct when the absolute or relative error is at most 10^{-9}.
Examples
Input
3 10
Output
0.145833333333
Input
100000 5
Output
0.999973749998
Submitted Solution:
```
n,k=map(int,input().split())
ans=0
for i in range(1,n+1):
score=i
p=1
while(score<k):
score*=2
p/=2
ans+=p
print(ans/n)
```
Yes
| 95,076 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Snuke has a fair N-sided die that shows the integers from 1 to N with equal probability and a fair coin. He will play the following game with them:
1. Throw the die. The current score is the result of the die.
2. As long as the score is between 1 and K-1 (inclusive), keep flipping the coin. The score is doubled each time the coin lands heads up, and the score becomes 0 if the coin lands tails up.
3. The game ends when the score becomes 0 or becomes K or above. Snuke wins if the score is K or above, and loses if the score is 0.
You are given N and K. Find the probability that Snuke wins the game.
Constraints
* 1 ≤ N ≤ 10^5
* 1 ≤ K ≤ 10^5
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
Output
Print the probability that Snuke wins the game. The output is considered correct when the absolute or relative error is at most 10^{-9}.
Examples
Input
3 10
Output
0.145833333333
Input
100000 5
Output
0.999973749998
Submitted Solution:
```
n, k = map(int, input().split())
ans = 0
for i in range(1, n+1):
p = i
c = 0
while p < k:
p *= 2
c += 1
ans += (0.5)**c/n
print(ans)
```
Yes
| 95,077 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Snuke has a fair N-sided die that shows the integers from 1 to N with equal probability and a fair coin. He will play the following game with them:
1. Throw the die. The current score is the result of the die.
2. As long as the score is between 1 and K-1 (inclusive), keep flipping the coin. The score is doubled each time the coin lands heads up, and the score becomes 0 if the coin lands tails up.
3. The game ends when the score becomes 0 or becomes K or above. Snuke wins if the score is K or above, and loses if the score is 0.
You are given N and K. Find the probability that Snuke wins the game.
Constraints
* 1 ≤ N ≤ 10^5
* 1 ≤ K ≤ 10^5
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
Output
Print the probability that Snuke wins the game. The output is considered correct when the absolute or relative error is at most 10^{-9}.
Examples
Input
3 10
Output
0.145833333333
Input
100000 5
Output
0.999973749998
Submitted Solution:
```
N,K=map(int,input().split())
ans=0
for i in range(1,N+1):
s=0
while i<K:
i*=2
s+=1
ans+=(1/N)*(1/(2**s))
print(ans)
```
Yes
| 95,078 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Snuke has a fair N-sided die that shows the integers from 1 to N with equal probability and a fair coin. He will play the following game with them:
1. Throw the die. The current score is the result of the die.
2. As long as the score is between 1 and K-1 (inclusive), keep flipping the coin. The score is doubled each time the coin lands heads up, and the score becomes 0 if the coin lands tails up.
3. The game ends when the score becomes 0 or becomes K or above. Snuke wins if the score is K or above, and loses if the score is 0.
You are given N and K. Find the probability that Snuke wins the game.
Constraints
* 1 ≤ N ≤ 10^5
* 1 ≤ K ≤ 10^5
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
Output
Print the probability that Snuke wins the game. The output is considered correct when the absolute or relative error is at most 10^{-9}.
Examples
Input
3 10
Output
0.145833333333
Input
100000 5
Output
0.999973749998
Submitted Solution:
```
import math
N, K = map(int, input().split())
ANS = 0
for i in range(1,N+1):
if i < K:
ANS += (1/N) * (0.5**(math.ceil(math.log(int(K/i),2))))
else:
ANS += (1/N)*(N-K+1)
break
print(math.ceil(math.log(int(K/i),2)))
print(ANS)
```
No
| 95,079 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Snuke has a fair N-sided die that shows the integers from 1 to N with equal probability and a fair coin. He will play the following game with them:
1. Throw the die. The current score is the result of the die.
2. As long as the score is between 1 and K-1 (inclusive), keep flipping the coin. The score is doubled each time the coin lands heads up, and the score becomes 0 if the coin lands tails up.
3. The game ends when the score becomes 0 or becomes K or above. Snuke wins if the score is K or above, and loses if the score is 0.
You are given N and K. Find the probability that Snuke wins the game.
Constraints
* 1 ≤ N ≤ 10^5
* 1 ≤ K ≤ 10^5
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
Output
Print the probability that Snuke wins the game. The output is considered correct when the absolute or relative error is at most 10^{-9}.
Examples
Input
3 10
Output
0.145833333333
Input
100000 5
Output
0.999973749998
Submitted Solution:
```
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
#from itertools import combinations # (string,3) 3回
#from collections import deque
#import collections.defaultdict
#import bisect
#
# d = m - k[i] - k[j]
# if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも!!
#
#
import sys
sys.setrecursionlimit(10000)
mod = 10**9 + 7
def readInts():
return list(map(int,input().split()))
def main():
n,k = readInts()
# サイコロの出た目が1から考えてみる?
i = 1
cnt = 0
while i < k:
cnt += 1
i *= 2
#print(i,cnt)
ans = 0
for i in range(cnt,1,-1):# 分子が等比数列
#print(cnt)
ans += (1/n) * ((1/2) ** i)
for i in range(cnt+1,n+1):
ans += (1/n)
print(ans)
if __name__ == '__main__':
main()
```
No
| 95,080 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Snuke has a fair N-sided die that shows the integers from 1 to N with equal probability and a fair coin. He will play the following game with them:
1. Throw the die. The current score is the result of the die.
2. As long as the score is between 1 and K-1 (inclusive), keep flipping the coin. The score is doubled each time the coin lands heads up, and the score becomes 0 if the coin lands tails up.
3. The game ends when the score becomes 0 or becomes K or above. Snuke wins if the score is K or above, and loses if the score is 0.
You are given N and K. Find the probability that Snuke wins the game.
Constraints
* 1 ≤ N ≤ 10^5
* 1 ≤ K ≤ 10^5
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
Output
Print the probability that Snuke wins the game. The output is considered correct when the absolute or relative error is at most 10^{-9}.
Examples
Input
3 10
Output
0.145833333333
Input
100000 5
Output
0.999973749998
Submitted Solution:
```
N, K = map(int, input())
calc = 0
for i in range(1, N+1):
tmp = i
cnt = 0
while tmp <= K-1
tmp*=2
cnt+=1
calc += (1/N) * (1/(2**cnt))
print(calc)
```
No
| 95,081 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Snuke has a fair N-sided die that shows the integers from 1 to N with equal probability and a fair coin. He will play the following game with them:
1. Throw the die. The current score is the result of the die.
2. As long as the score is between 1 and K-1 (inclusive), keep flipping the coin. The score is doubled each time the coin lands heads up, and the score becomes 0 if the coin lands tails up.
3. The game ends when the score becomes 0 or becomes K or above. Snuke wins if the score is K or above, and loses if the score is 0.
You are given N and K. Find the probability that Snuke wins the game.
Constraints
* 1 ≤ N ≤ 10^5
* 1 ≤ K ≤ 10^5
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
Output
Print the probability that Snuke wins the game. The output is considered correct when the absolute or relative error is at most 10^{-9}.
Examples
Input
3 10
Output
0.145833333333
Input
100000 5
Output
0.999973749998
Submitted Solution:
```
def main():
N, K = map(int, input().split())
sum = 1
cnt = 0
while sum < K:
sum *= 2
cnt += 1
ans = 0
for i in range(N):
if cnt > 0:
ans += (1 / N) * (0.5 ** cnt)
cnt -= 1
else:
ans += (1 / N)
print(ans)
if __name__ == '__main__':
main()
```
No
| 95,082 |
Provide a correct Python 3 solution for this coding contest problem.
There are N stones, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), the height of Stone i is h_i. Here, h_1 < h_2 < \cdots < h_N holds.
There is a frog who is initially on Stone 1. He will repeat the following action some number of times to reach Stone N:
* If the frog is currently on Stone i, jump to one of the following: Stone i + 1, i + 2, \ldots, N. Here, a cost of (h_j - h_i)^2 + C is incurred, where j is the stone to land on.
Find the minimum possible total cost incurred before the frog reaches Stone N.
Constraints
* All values in input are integers.
* 2 \leq N \leq 2 \times 10^5
* 1 \leq C \leq 10^{12}
* 1 \leq h_1 < h_2 < \cdots < h_N \leq 10^6
Input
Input is given from Standard Input in the following format:
N C
h_1 h_2 \ldots h_N
Output
Print the minimum possible total cost incurred.
Examples
Input
5 6
1 2 3 4 5
Output
20
Input
2 1000000000000
500000 1000000
Output
1250000000000
Input
8 5
1 3 4 5 10 11 12 13
Output
62
"Correct Solution:
```
import sys
input = sys.stdin.readline
from collections import *
class CHT: #傾きの単調性と最小値クエリのxの単調性を仮定
def __init__(self):
self.q = deque([]) #直線群
def f(self, f1, x): #f1(x)
return f1[0]*x+f1[1]
def check(self, f1, f2, f3): #f2を削除しても良いかの判定
return (f2[0]-f1[0])*(f3[1]-f2[1])>=(f2[1]-f1[1])*(f3[0]-f2[0])
def add_line(self, a, b): #傾きa, 切片bの直線を追加
while len(self.q)>=2 and self.check(self.q[-2], self.q[-1], (a, b)):
self.q.pop()
self.q.append((a, b))
def get(self, x): #xでの最小値
while len(self.q)>=2 and self.f(self.q[0], x)>=self.f(self.q[1], x):
self.q.popleft()
return self.f(self.q[0], x)
N, C = map(int, input().split())
h = list(map(int, input().split()))
dp = [0]*N
cht = CHT()
cht.add_line(-2*h[0], h[0]**2)
for i in range(1, N):
dp[i] = h[i]**2+C+cht.get(h[i])
cht.add_line(-2*h[i], dp[i]+h[i]**2)
print(dp[N-1])
```
| 95,083 |
Provide a correct Python 3 solution for this coding contest problem.
There are N stones, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), the height of Stone i is h_i. Here, h_1 < h_2 < \cdots < h_N holds.
There is a frog who is initially on Stone 1. He will repeat the following action some number of times to reach Stone N:
* If the frog is currently on Stone i, jump to one of the following: Stone i + 1, i + 2, \ldots, N. Here, a cost of (h_j - h_i)^2 + C is incurred, where j is the stone to land on.
Find the minimum possible total cost incurred before the frog reaches Stone N.
Constraints
* All values in input are integers.
* 2 \leq N \leq 2 \times 10^5
* 1 \leq C \leq 10^{12}
* 1 \leq h_1 < h_2 < \cdots < h_N \leq 10^6
Input
Input is given from Standard Input in the following format:
N C
h_1 h_2 \ldots h_N
Output
Print the minimum possible total cost incurred.
Examples
Input
5 6
1 2 3 4 5
Output
20
Input
2 1000000000000
500000 1000000
Output
1250000000000
Input
8 5
1 3 4 5 10 11 12 13
Output
62
"Correct Solution:
```
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from collections import deque
N,C,*H = map(int,read().split())
class ConvexHullTrick:
"""
f_i = a_ix + b_i とする。f_i の追加および、min_i f(x) の取得ができるデータ構造。
ただし、傾き a_i は昇順に追加されなければならない。
また、クエリ x も昇順に実行されなければならない。
"""
def __init__(self):
self.funcs = deque()
def add(self,a,b):
funcs = self.funcs
while len(funcs) >= 2:
a1,b1 = funcs[-2]
a2,b2 = funcs[-1]
if (a2-a1)*(b-b2) < (b2-b1)*(a-a2):
break
funcs.pop()
funcs.append((a,b))
def query(self,x):
funcs = self.funcs
a,b = funcs[0]
y = a*x+b
while len(funcs) >= 2:
a2,b2 = funcs[1]
y2 = a2*x+b2
if y < y2:
break
y = y2
funcs.popleft()
return y
dp = [0] * N
cht = ConvexHullTrick()
add = cht.add; query = cht.query
h = H[0]
add(-2*h, h*h)
for i,h in enumerate(H[1:],1):
x = query(h) + h*h + C
dp[i] = x
add(-2*h, h*h + x)
answer = dp[-1]
print(answer)
```
| 95,084 |
Provide a correct Python 3 solution for this coding contest problem.
There are N stones, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), the height of Stone i is h_i. Here, h_1 < h_2 < \cdots < h_N holds.
There is a frog who is initially on Stone 1. He will repeat the following action some number of times to reach Stone N:
* If the frog is currently on Stone i, jump to one of the following: Stone i + 1, i + 2, \ldots, N. Here, a cost of (h_j - h_i)^2 + C is incurred, where j is the stone to land on.
Find the minimum possible total cost incurred before the frog reaches Stone N.
Constraints
* All values in input are integers.
* 2 \leq N \leq 2 \times 10^5
* 1 \leq C \leq 10^{12}
* 1 \leq h_1 < h_2 < \cdots < h_N \leq 10^6
Input
Input is given from Standard Input in the following format:
N C
h_1 h_2 \ldots h_N
Output
Print the minimum possible total cost incurred.
Examples
Input
5 6
1 2 3 4 5
Output
20
Input
2 1000000000000
500000 1000000
Output
1250000000000
Input
8 5
1 3 4 5 10 11 12 13
Output
62
"Correct Solution:
```
class ConvexHullTrick:
def __init__(self):
self.AB = []
def check(self,a,b):
a1,b1 = self.AB[-1][0],self.AB[-1][1]
a2,b2 = self.AB[-2][0],self.AB[-2][1]
lhs = (b2-b1)*(a -a1)
rhs = (b1-b )*(a1-a2)
return lhs>=rhs
# 一次関数 y=a*x+b を追加
# 追加の際、傾きaは単調減少(既存の傾きより最小?)であること
def append(self,a,b):
while 2<=len(self.AB) and self.check(a,b):
self.AB.pop()
self.AB.append((a,b))
return
# min_i {a[i]*x+b[i]}
def query(self,x):
def eval(ab,x):
return ab[0]*x+ab[1]
l,r = -1, len(self.AB)-1
while l+1<r:
m = (l+r)//2
if eval(self.AB[m+1],x)<=eval(self.AB[m],x):
l = m
else:
r = m
return eval(self.AB[r],x)
N,C = map(int,input().split())
h = list(map(int,input().split()))
#print(h)
cht = ConvexHullTrick()
dp = [0]*N
dp[0] = 0
cht.append(-2*h[0],h[0]**2+dp[0])
for i in range(1,N):
#best = 99999999999999
# for j in range(i):
# #cost = dp[j]+(h[i]-h[j])**2+C
# cost = (-2*h[j])*h[i]+(h[j]**2+dp[j])
# best = min(best,cost)
best = cht.query(h[i])
dp[i] = best+h[i]**2+C
cht.append(-2*h[i],h[i]**2+dp[i])
ans = dp[N-1]
print(ans)
```
| 95,085 |
Provide a correct Python 3 solution for this coding contest problem.
There are N stones, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), the height of Stone i is h_i. Here, h_1 < h_2 < \cdots < h_N holds.
There is a frog who is initially on Stone 1. He will repeat the following action some number of times to reach Stone N:
* If the frog is currently on Stone i, jump to one of the following: Stone i + 1, i + 2, \ldots, N. Here, a cost of (h_j - h_i)^2 + C is incurred, where j is the stone to land on.
Find the minimum possible total cost incurred before the frog reaches Stone N.
Constraints
* All values in input are integers.
* 2 \leq N \leq 2 \times 10^5
* 1 \leq C \leq 10^{12}
* 1 \leq h_1 < h_2 < \cdots < h_N \leq 10^6
Input
Input is given from Standard Input in the following format:
N C
h_1 h_2 \ldots h_N
Output
Print the minimum possible total cost incurred.
Examples
Input
5 6
1 2 3 4 5
Output
20
Input
2 1000000000000
500000 1000000
Output
1250000000000
Input
8 5
1 3 4 5 10 11 12 13
Output
62
"Correct Solution:
```
import sys
input = sys.stdin.readline
class CHT: #追加される直線の傾きと最小値クエリのxが単調と仮定
def __init__(self, N): #N:直線数
self.deq = [0]*N
self.l = 0
self.r = 0
def f(self, j, x): #fj(x)
return -2*h[j]*h[x]+dp[j]+h[j]**2
def check(self, j1, j2, j3): #fj2を削除して良いか判定
a1, b1 = -2*h[j1], dp[j1]+h[j1]**2
a2, b2 = -2*h[j2], dp[j2]+h[j2]**2
a3, b3 = -2*h[j3], dp[j3]+h[j3]**2
return (a2-a1)*(b3-b2)>=(b2-b1)*(a3-a2)
def add(self, j): #直線群にfjを追加
while self.l+1<self.r and self.check(self.deq[self.r-2], self.deq[self.r-1], j):
self.r -= 1
self.deq[self.r] = j
self.r += 1
def get(self, x): #fj(x)の最小値
while self.l+1<self.r and self.f(self.deq[self.l], x)>=self.f(self.deq[self.l+1], x):
self.l += 1
return self.f(self.deq[self.l], x)
N, C = map(int, input().split())
h = list(map(int, input().split()))
dp = [0]*N
cht = CHT(N)
cht.add(0)
for i in range(1, N):
dp[i] = h[i]**2+C+cht.get(i)
cht.add(i)
print(dp[N-1])
```
| 95,086 |
Provide a correct Python 3 solution for this coding contest problem.
There are N stones, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), the height of Stone i is h_i. Here, h_1 < h_2 < \cdots < h_N holds.
There is a frog who is initially on Stone 1. He will repeat the following action some number of times to reach Stone N:
* If the frog is currently on Stone i, jump to one of the following: Stone i + 1, i + 2, \ldots, N. Here, a cost of (h_j - h_i)^2 + C is incurred, where j is the stone to land on.
Find the minimum possible total cost incurred before the frog reaches Stone N.
Constraints
* All values in input are integers.
* 2 \leq N \leq 2 \times 10^5
* 1 \leq C \leq 10^{12}
* 1 \leq h_1 < h_2 < \cdots < h_N \leq 10^6
Input
Input is given from Standard Input in the following format:
N C
h_1 h_2 \ldots h_N
Output
Print the minimum possible total cost incurred.
Examples
Input
5 6
1 2 3 4 5
Output
20
Input
2 1000000000000
500000 1000000
Output
1250000000000
Input
8 5
1 3 4 5 10 11 12 13
Output
62
"Correct Solution:
```
n,C,*H=map(int,open(0).read().split());p=[0];P=[0]
for i in range(1,n):
h=H[i];f=lambda I:p[P[I]]+H[P[I]]*(H[P[I]]-2*h)
while len(P)>1and f(0)>f(1):P.pop(0)
p+=[f(0)+h*h+C];h=0;P+=[i]
while len(P)>2and(H[P[-2]]-H[P[-3]])*(f(-2)-f(-1))>(H[i]-H[P[-2]])*(f(-3)-f(-2)):P.pop(-2)
print(p[-1])
```
| 95,087 |
Provide a correct Python 3 solution for this coding contest problem.
There are N stones, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), the height of Stone i is h_i. Here, h_1 < h_2 < \cdots < h_N holds.
There is a frog who is initially on Stone 1. He will repeat the following action some number of times to reach Stone N:
* If the frog is currently on Stone i, jump to one of the following: Stone i + 1, i + 2, \ldots, N. Here, a cost of (h_j - h_i)^2 + C is incurred, where j is the stone to land on.
Find the minimum possible total cost incurred before the frog reaches Stone N.
Constraints
* All values in input are integers.
* 2 \leq N \leq 2 \times 10^5
* 1 \leq C \leq 10^{12}
* 1 \leq h_1 < h_2 < \cdots < h_N \leq 10^6
Input
Input is given from Standard Input in the following format:
N C
h_1 h_2 \ldots h_N
Output
Print the minimum possible total cost incurred.
Examples
Input
5 6
1 2 3 4 5
Output
20
Input
2 1000000000000
500000 1000000
Output
1250000000000
Input
8 5
1 3 4 5 10 11 12 13
Output
62
"Correct Solution:
```
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from collections import deque
N,C,*H = map(int,read().split())
class CHT:
"""
f_i = a_ix + b_i とする。f_i の追加および、min_i f(x) の取得ができるデータ構造。
ただし、傾き a_i は昇順に追加されなければならない。
また、クエリ x も昇順に実行されなければならない。
"""
def __init__(self):
self.funcs = deque()
def add(self,a,b):
funcs = self.funcs
while len(funcs) >= 2:
a1,b1 = funcs[-2]
a2,b2 = funcs[-1]
if (a2-a1)*(b-b2) < (b2-b1)*(a-a2):
break
funcs.pop()
funcs.append((a,b))
def query(self,x):
funcs = self.funcs
a,b = funcs[0]
y = a*x+b
while len(funcs) >= 2:
a2,b2 = self.funcs[1]
y2 = a2*x+b2
if y < y2:
break
y = y2
self.funcs.popleft()
return y
dp = [0] * N
cht = CHT()
h = H[0]
cht.add(-2*h, h*h)
for i,h in enumerate(H[1:],1):
x = cht.query(h) + h*h + C
dp[i] = x
cht.add(-2*h, h*h + x)
answer = dp[-1]
print(answer)
```
| 95,088 |
Provide a correct Python 3 solution for this coding contest problem.
There are N stones, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), the height of Stone i is h_i. Here, h_1 < h_2 < \cdots < h_N holds.
There is a frog who is initially on Stone 1. He will repeat the following action some number of times to reach Stone N:
* If the frog is currently on Stone i, jump to one of the following: Stone i + 1, i + 2, \ldots, N. Here, a cost of (h_j - h_i)^2 + C is incurred, where j is the stone to land on.
Find the minimum possible total cost incurred before the frog reaches Stone N.
Constraints
* All values in input are integers.
* 2 \leq N \leq 2 \times 10^5
* 1 \leq C \leq 10^{12}
* 1 \leq h_1 < h_2 < \cdots < h_N \leq 10^6
Input
Input is given from Standard Input in the following format:
N C
h_1 h_2 \ldots h_N
Output
Print the minimum possible total cost incurred.
Examples
Input
5 6
1 2 3 4 5
Output
20
Input
2 1000000000000
500000 1000000
Output
1250000000000
Input
8 5
1 3 4 5 10 11 12 13
Output
62
"Correct Solution:
```
# -*- coding: utf-8 -*-
#############
# Libraries #
#############
import sys
input = sys.stdin.readline
import math
#from math import gcd
import bisect
import heapq
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache
#############
# Constants #
#############
MOD = 10**9+7
INF = float('inf')
AZ = "abcdefghijklmnopqrstuvwxyz"
#############
# Functions #
#############
######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]
#####Shorten#####
def DD(arg): return defaultdict(arg)
#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)
######Combination######
kaijo_memo = []
def kaijo(n):
if(len(kaijo_memo) > n): return kaijo_memo[n]
if(len(kaijo_memo) == 0): kaijo_memo.append(1)
while(len(kaijo_memo) <= n): kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
return kaijo_memo[n]
gyaku_kaijo_memo = []
def gyaku_kaijo(n):
if(len(gyaku_kaijo_memo) > n): return gyaku_kaijo_memo[n]
if(len(gyaku_kaijo_memo) == 0): gyaku_kaijo_memo.append(1)
while(len(gyaku_kaijo_memo) <= n): gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
return gyaku_kaijo_memo[n]
def nCr(n,r):
if n == r: return 1
if n < r or r < 0: return 0
ret = 1
ret = ret * kaijo(n) % MOD
ret = ret * gyaku_kaijo(r) % MOD
ret = ret * gyaku_kaijo(n-r) % MOD
return ret
######Factorization######
def factorization(n):
arr = []
temp = n
for i in range(2, int(-(-n**0.5//1))+1):
if temp%i==0:
cnt=0
while temp%i==0:
cnt+=1
temp //= i
arr.append([i, cnt])
if temp!=1: arr.append([temp, 1])
if arr==[]: arr.append([n, 1])
return arr
#####MakeDivisors######
def make_divisors(n):
divisors = []
for i in range(1, int(n**0.5)+1):
if n % i == 0:
divisors.append(i)
if i != n // i:
divisors.append(n//i)
return divisors
#####MakePrimes######
def make_primes(N):
max = int(math.sqrt(N))
seachList = [i for i in range(2,N+1)]
primeNum = []
while seachList[0] <= max:
primeNum.append(seachList[0])
tmp = seachList[0]
seachList = [i for i in seachList if i % tmp != 0]
primeNum.extend(seachList)
return primeNum
#####GCD#####
def gcd(a, b):
while b: a, b = b, a % b
return a
#####LCM#####
def lcm(a, b):
return a * b // gcd (a, b)
#####BitCount#####
def count_bit(n):
count = 0
while n:
n &= n-1
count += 1
return count
#####ChangeBase#####
def base_10_to_n(X, n):
if X//n: return base_10_to_n(X//n, n)+[X%n]
return [X%n]
def base_n_to_10(X, n):
return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))
def base_10_to_n_without_0(X, n):
X -= 1
if X//n: return base_10_to_n_without_0(X//n, n)+[X%n]
return [X%n]
#####IntLog#####
def int_log(n, a):
count = 0
while n>=a:
n //= a
count += 1
return count
#############
# Main Code #
#############
N,C = IL()
H = IL()
from collections import deque
deq = deque()
def check(f1, f2, f3):
return (f2[0] - f1[0]) * (f3[1] - f2[1]) >= (f2[1] - f1[1]) * (f3[0] - f2[0])
def f(f1, x):
return f1[0]*x + f1[1]
# add f_i(x) = a*x + b
def add_line(a, b):
f1 = (a, b)
while len(deq) >= 2 and check(deq[-2], deq[-1], f1):
deq.pop()
deq.append(f1)
# min f_i(x)
def query(x):
while len(deq) >= 2 and f(deq[0], x) >= f(deq[1], x):
deq.popleft()
return f(deq[0], x)
dp = [0]
for i in range(1,N):
add_line(-2*H[i-1],dp[-1]+H[i-1]**2)
dp.append(query(H[i]) + H[i]**2 + C)
print(dp[-1])
while False and kokohazettaiyomarenaikarananikaitemoiindesuwahhahha: naniwarotennen
```
| 95,089 |
Provide a correct Python 3 solution for this coding contest problem.
There are N stones, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), the height of Stone i is h_i. Here, h_1 < h_2 < \cdots < h_N holds.
There is a frog who is initially on Stone 1. He will repeat the following action some number of times to reach Stone N:
* If the frog is currently on Stone i, jump to one of the following: Stone i + 1, i + 2, \ldots, N. Here, a cost of (h_j - h_i)^2 + C is incurred, where j is the stone to land on.
Find the minimum possible total cost incurred before the frog reaches Stone N.
Constraints
* All values in input are integers.
* 2 \leq N \leq 2 \times 10^5
* 1 \leq C \leq 10^{12}
* 1 \leq h_1 < h_2 < \cdots < h_N \leq 10^6
Input
Input is given from Standard Input in the following format:
N C
h_1 h_2 \ldots h_N
Output
Print the minimum possible total cost incurred.
Examples
Input
5 6
1 2 3 4 5
Output
20
Input
2 1000000000000
500000 1000000
Output
1250000000000
Input
8 5
1 3 4 5 10 11 12 13
Output
62
"Correct Solution:
```
def nasu(a, j):
return [-2*j, j * j + a]
def tami(x, ab):
a, b = ab
return a * x + b
def renritu(ab1, ab2):
a1, b1 = ab1
a2, b2 = ab2
return (b2 - b1) / (a1 - a2)
def add(a, b):
ab3 = nasu(a, b)
while len(L) >= 2:
ab1 = L[-2]
ab2 = L[-1]
x1 = renritu(ab1, ab2)
x2 = renritu(ab2, ab3)
if x1 > x2:
L.pop()
else:
break
L.append(ab3)
N, C = list(map(int, input().split()))
h = list(map(int, input().split()))
DP = [0] * N
L = [nasu(0, h[0])]
cnt = 0
for i in range(1, N):
while cnt < len(L) - 1:
x = tami(h[i], L[cnt])
y = tami(h[i], L[cnt + 1])
if x >= y:
cnt += 1
else:
break
DP[i] = h[i] ** 2 + C + tami(h[i], L[cnt])
add(DP[i], h[i])
print(DP[-1])
```
| 95,090 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N stones, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), the height of Stone i is h_i. Here, h_1 < h_2 < \cdots < h_N holds.
There is a frog who is initially on Stone 1. He will repeat the following action some number of times to reach Stone N:
* If the frog is currently on Stone i, jump to one of the following: Stone i + 1, i + 2, \ldots, N. Here, a cost of (h_j - h_i)^2 + C is incurred, where j is the stone to land on.
Find the minimum possible total cost incurred before the frog reaches Stone N.
Constraints
* All values in input are integers.
* 2 \leq N \leq 2 \times 10^5
* 1 \leq C \leq 10^{12}
* 1 \leq h_1 < h_2 < \cdots < h_N \leq 10^6
Input
Input is given from Standard Input in the following format:
N C
h_1 h_2 \ldots h_N
Output
Print the minimum possible total cost incurred.
Examples
Input
5 6
1 2 3 4 5
Output
20
Input
2 1000000000000
500000 1000000
Output
1250000000000
Input
8 5
1 3 4 5 10 11 12 13
Output
62
Submitted Solution:
```
n,C,*H=map(int,open(0).read().split());p=[0];P=[0]
for i in range(1,n):
h=H[i];g=lambda x,y:(p[P[y]]-p[P[x]])/(H[P[x]]-H[P[y]])-H[P[y]]-H[P[x]]+2*h;
while len(P)>1and g(1,0)>0:P.pop(0)
p+=[p[P[0]]+(H[P[0]]-h)**2+C];P+=[i];h=0
while len(P)>2and g(-1,-2)>g(-2,-3):P.pop(-2)
print(p[-1])
```
Yes
| 95,091 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N stones, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), the height of Stone i is h_i. Here, h_1 < h_2 < \cdots < h_N holds.
There is a frog who is initially on Stone 1. He will repeat the following action some number of times to reach Stone N:
* If the frog is currently on Stone i, jump to one of the following: Stone i + 1, i + 2, \ldots, N. Here, a cost of (h_j - h_i)^2 + C is incurred, where j is the stone to land on.
Find the minimum possible total cost incurred before the frog reaches Stone N.
Constraints
* All values in input are integers.
* 2 \leq N \leq 2 \times 10^5
* 1 \leq C \leq 10^{12}
* 1 \leq h_1 < h_2 < \cdots < h_N \leq 10^6
Input
Input is given from Standard Input in the following format:
N C
h_1 h_2 \ldots h_N
Output
Print the minimum possible total cost incurred.
Examples
Input
5 6
1 2 3 4 5
Output
20
Input
2 1000000000000
500000 1000000
Output
1250000000000
Input
8 5
1 3 4 5 10 11 12 13
Output
62
Submitted Solution:
```
from collections import deque
deq = deque()
def check(f1, f2, f3):
return (f2[0] - f1[0]) * (f3[1] - f2[1]) >= (f2[1] - f1[1]) * (f3[0] - f2[0])
def f(f1, x):
return f1[0]*x + f1[1]
# add f_i(x) = a*x + b
def add_line(a, b):
f1 = (a, b)
while len(deq) >= 2 and check(deq[-2], deq[-1], f1):
deq.pop()
deq.append(f1)
# min f_i(x)
def query(x):
while len(deq) >= 2 and f(deq[0], x) >= f(deq[1], x):
deq.popleft()
return f(deq[0], x)
N,C=map(int,input().split())
h=list(map(int,input().split()))
dp=[0]*N
dp[-1]=0
add_line(2*h[-1],h[-1]**2)
for i in range(N-2,-1,-1):
dp[i]=h[i]**2+C+query(-h[i])
add_line(2*h[i],h[i]**2+dp[i])
print(dp[0])
```
Yes
| 95,092 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N stones, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), the height of Stone i is h_i. Here, h_1 < h_2 < \cdots < h_N holds.
There is a frog who is initially on Stone 1. He will repeat the following action some number of times to reach Stone N:
* If the frog is currently on Stone i, jump to one of the following: Stone i + 1, i + 2, \ldots, N. Here, a cost of (h_j - h_i)^2 + C is incurred, where j is the stone to land on.
Find the minimum possible total cost incurred before the frog reaches Stone N.
Constraints
* All values in input are integers.
* 2 \leq N \leq 2 \times 10^5
* 1 \leq C \leq 10^{12}
* 1 \leq h_1 < h_2 < \cdots < h_N \leq 10^6
Input
Input is given from Standard Input in the following format:
N C
h_1 h_2 \ldots h_N
Output
Print the minimum possible total cost incurred.
Examples
Input
5 6
1 2 3 4 5
Output
20
Input
2 1000000000000
500000 1000000
Output
1250000000000
Input
8 5
1 3 4 5 10 11 12 13
Output
62
Submitted Solution:
```
lines = []
def fi(i, x):
a, b = lines[i]
return a*x+b
def find(x):
def f(i):
return fi(i+1,x) > fi(i,x)
mn, mx = -1, len(lines)-1
idx = (mn+mx)//2
while mx-mn>1:
if f(idx):
mx, idx = idx, (mn + idx)//2
continue
mn, idx = idx, (mx + idx)//2
return fi(idx+1, x)
def find2(x):
# x が単調増加
I = find2.I
u, v = lines[I]
while I+1<len(lines):
w, y = lines[I+1]
if u*x+v < w*x+y:
break
u, v = w, y
I += 1
find2.I = I
return u*x+v
find2.I = 0
def insert(a, b):
pass
def insert2(a, b):
# a が単調減少
if not lines:
lines.append((a,b))
return
(e, f) = lines[-1]
while len(lines)-1:
(c, d), (e, f) = (e, f), lines[-2]
if (c-e)*(b-d) < (d-f)*(a-c):
break
lines.pop()
lines.append((a, b))
N, C = map(int, input().split())
hs = map(int, input().split())
r = 0
lines = []
for i, h in enumerate(hs):
if i!= 0:
r = find(h) + h**2+C
insert2(-2*h, r+h**2)
print(r)
```
Yes
| 95,093 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N stones, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), the height of Stone i is h_i. Here, h_1 < h_2 < \cdots < h_N holds.
There is a frog who is initially on Stone 1. He will repeat the following action some number of times to reach Stone N:
* If the frog is currently on Stone i, jump to one of the following: Stone i + 1, i + 2, \ldots, N. Here, a cost of (h_j - h_i)^2 + C is incurred, where j is the stone to land on.
Find the minimum possible total cost incurred before the frog reaches Stone N.
Constraints
* All values in input are integers.
* 2 \leq N \leq 2 \times 10^5
* 1 \leq C \leq 10^{12}
* 1 \leq h_1 < h_2 < \cdots < h_N \leq 10^6
Input
Input is given from Standard Input in the following format:
N C
h_1 h_2 \ldots h_N
Output
Print the minimum possible total cost incurred.
Examples
Input
5 6
1 2 3 4 5
Output
20
Input
2 1000000000000
500000 1000000
Output
1250000000000
Input
8 5
1 3 4 5 10 11 12 13
Output
62
Submitted Solution:
```
import sys
input = lambda : sys.stdin.readline().strip()
from collections import deque
class Convex_Hull_Trick():
def __init__(self):
self.que = deque()
@staticmethod
def check(f1, f2, f3):
return (f2[0] - f1[0]) * (f3[1] - f2[1]) >= (f2[1] - f1[1]) * (f3[0] - f2[0])
@staticmethod
def f(f1, x):
return f1[0]*x + f1[1]
def add_line(self, a, b):
fi = (a, b)
while len(self.que) >= 2 and self.check(self.que[-2], self.que[-1], fi):
self.que.pop()
self.que.append(fi)
def query(self, x):
while len(self.que) >= 2 and self.f(self.que[0], x) >= self.f(self.que[1], x):
self.que.popleft()
return self.f(self.que[0], x)
n,c = map(int, input().split())
h = list(map(int, input().split()))
CHT = Convex_Hull_Trick()
CHT.add_line(-2*h[0], h[0]**2)
DP = [0]*n
for i in range(1, n):
min_cost = CHT.query(h[i])
DP[i] = min_cost + h[i]*h[i] + c
CHT.add_line(-2*h[i], h[i]**2+DP[i])
print(DP[-1])
```
Yes
| 95,094 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N stones, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), the height of Stone i is h_i. Here, h_1 < h_2 < \cdots < h_N holds.
There is a frog who is initially on Stone 1. He will repeat the following action some number of times to reach Stone N:
* If the frog is currently on Stone i, jump to one of the following: Stone i + 1, i + 2, \ldots, N. Here, a cost of (h_j - h_i)^2 + C is incurred, where j is the stone to land on.
Find the minimum possible total cost incurred before the frog reaches Stone N.
Constraints
* All values in input are integers.
* 2 \leq N \leq 2 \times 10^5
* 1 \leq C \leq 10^{12}
* 1 \leq h_1 < h_2 < \cdots < h_N \leq 10^6
Input
Input is given from Standard Input in the following format:
N C
h_1 h_2 \ldots h_N
Output
Print the minimum possible total cost incurred.
Examples
Input
5 6
1 2 3 4 5
Output
20
Input
2 1000000000000
500000 1000000
Output
1250000000000
Input
8 5
1 3 4 5 10 11 12 13
Output
62
Submitted Solution:
```
l=list(map(int,input().split()))
N=l[0]
C=l[1]
dp=[]
for i in range(N+2):
dp.append(10**30)
dp[0]=0
lst=list(map(int,input().split()))
for i in range(N):
for j in range(1,N):
if(i+j<N):
dp[i+j]=min(dp[i+j],dp[i]+(lst[i+j]-lst[i])**2+C)
print(dp[N-1])
```
No
| 95,095 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N stones, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), the height of Stone i is h_i. Here, h_1 < h_2 < \cdots < h_N holds.
There is a frog who is initially on Stone 1. He will repeat the following action some number of times to reach Stone N:
* If the frog is currently on Stone i, jump to one of the following: Stone i + 1, i + 2, \ldots, N. Here, a cost of (h_j - h_i)^2 + C is incurred, where j is the stone to land on.
Find the minimum possible total cost incurred before the frog reaches Stone N.
Constraints
* All values in input are integers.
* 2 \leq N \leq 2 \times 10^5
* 1 \leq C \leq 10^{12}
* 1 \leq h_1 < h_2 < \cdots < h_N \leq 10^6
Input
Input is given from Standard Input in the following format:
N C
h_1 h_2 \ldots h_N
Output
Print the minimum possible total cost incurred.
Examples
Input
5 6
1 2 3 4 5
Output
20
Input
2 1000000000000
500000 1000000
Output
1250000000000
Input
8 5
1 3 4 5 10 11 12 13
Output
62
Submitted Solution:
```
from heapq import heappop,heappush
n,c = map(int,input().split())
h = list(map(int,input().split()))
ans = (h[-1]-h[0])**2
hq = []
hq_done = []
dif = [0]
dif += [i-j for i,j in zip(h[1:],h[:-1])]
left = list(range(-1,n))
right = list(range(1,n+2))
for i in range(1,n-1):
di,dj = dif[i:i+2]
cost = 2*di*dj
heappush(hq,(cost,i))
now = c*(n-1)
for d in dif:
now += d**2
ans = now
def hq_pop():
while(hq_done):
if(hq_done[0] == hq[0]):
heappop(hq)
heappop(hq_done)
else:
break
return heappop(hq)
# print('')
# print(-1, now,ans)
# print(hq)
# print(hq_done)
# print(dif)
# print(left)
# print(right)
for _ in range(n-2):
cost,l = hq_pop()
now += cost - c
ans = min(ans,now)
r = right[l]
ll = left[l]
rr = right[r]
d_new = dif[l] + dif[r]
if(ll > 0):
heappush(hq_done, (2*dif[ll]*dif[l],ll))
heappush(hq,(2*dif[ll]*d_new,ll))
if(rr < n):
heappush(hq_done, (2*dif[r]*dif[rr],r))
heappush(hq,(2*dif[rr]*d_new,l))
dif[l] = d_new
dif[r] = 0
right[l] = rr
left[rr] = l
# print('')
# print(_, now,ans)
# print(hq)
# print(hq_done)
# print(dif)
# print(left)
# print(right)
print(ans)
```
No
| 95,096 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N stones, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), the height of Stone i is h_i. Here, h_1 < h_2 < \cdots < h_N holds.
There is a frog who is initially on Stone 1. He will repeat the following action some number of times to reach Stone N:
* If the frog is currently on Stone i, jump to one of the following: Stone i + 1, i + 2, \ldots, N. Here, a cost of (h_j - h_i)^2 + C is incurred, where j is the stone to land on.
Find the minimum possible total cost incurred before the frog reaches Stone N.
Constraints
* All values in input are integers.
* 2 \leq N \leq 2 \times 10^5
* 1 \leq C \leq 10^{12}
* 1 \leq h_1 < h_2 < \cdots < h_N \leq 10^6
Input
Input is given from Standard Input in the following format:
N C
h_1 h_2 \ldots h_N
Output
Print the minimum possible total cost incurred.
Examples
Input
5 6
1 2 3 4 5
Output
20
Input
2 1000000000000
500000 1000000
Output
1250000000000
Input
8 5
1 3 4 5 10 11 12 13
Output
62
Submitted Solution:
```
N, C = map(int, input().split())
h=[int(x) for x in input().split()]
dp=[0]*N
for i in range(1,N):
j=i-1
dp[i]=dp[j] + (h[i]-h[j])**2 + C
while j>0:
j=j-1
if dp[i]>(dp[j] + (h[i]-h[j])**2 + C):
dp[i]=dp[j] + (h[i]-h[j])**2 + C
break
print(dp[-1])
```
No
| 95,097 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N stones, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), the height of Stone i is h_i. Here, h_1 < h_2 < \cdots < h_N holds.
There is a frog who is initially on Stone 1. He will repeat the following action some number of times to reach Stone N:
* If the frog is currently on Stone i, jump to one of the following: Stone i + 1, i + 2, \ldots, N. Here, a cost of (h_j - h_i)^2 + C is incurred, where j is the stone to land on.
Find the minimum possible total cost incurred before the frog reaches Stone N.
Constraints
* All values in input are integers.
* 2 \leq N \leq 2 \times 10^5
* 1 \leq C \leq 10^{12}
* 1 \leq h_1 < h_2 < \cdots < h_N \leq 10^6
Input
Input is given from Standard Input in the following format:
N C
h_1 h_2 \ldots h_N
Output
Print the minimum possible total cost incurred.
Examples
Input
5 6
1 2 3 4 5
Output
20
Input
2 1000000000000
500000 1000000
Output
1250000000000
Input
8 5
1 3 4 5 10 11 12 13
Output
62
Submitted Solution:
```
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")
n,c = list(map(int, input().split()))
h = list(map(int, input().split()))
dp = [0]*n
from queue import deque
q = deque([(-2*h[0], h[0]**2)])
for i in range(1,n):
vv = float("inf")
bb = None
for ii,item in enumerate(q):
if item[0]*h[i]+item[1]<vv:
vv = item[0]*h[i]+item[1]
bb = ii
for ii in range(len(q)-bb-1):
q.pop()
# while len(q)>=2 and q[-2][0]*h[i]+q[-2][1]<=q[-1][0]*h[i]+q[-1][1]:
# q.pop()
dp[i] = vv + h[i]**2 + c
# dp[i] = q[-1][0]*h[i]+q[-1][1] + h[i]**2 + c
q.appendleft((-2*h[i], h[i]**2 +dp[i]))
ans = dp[n-1]
print(ans)
```
No
| 95,098 |
Provide a correct Python 3 solution for this coding contest problem.
Takahashi has a tower which is divided into N layers. Initially, all the layers are uncolored. Takahashi is going to paint some of the layers in red, green or blue to make a beautiful tower. He defines the beauty of the tower as follows:
* The beauty of the tower is the sum of the scores of the N layers, where the score of a layer is A if the layer is painted red, A+B if the layer is painted green, B if the layer is painted blue, and 0 if the layer is uncolored.
Here, A and B are positive integer constants given beforehand. Also note that a layer may not be painted in two or more colors.
Takahashi is planning to paint the tower so that the beauty of the tower becomes exactly K. How many such ways are there to paint the tower? Find the count modulo 998244353. Two ways to paint the tower are considered different when there exists a layer that is painted in different colors, or a layer that is painted in some color in one of the ways and not in the other.
Constraints
* 1 ≤ N ≤ 3×10^5
* 1 ≤ A,B ≤ 3×10^5
* 0 ≤ K ≤ 18×10^{10}
* All values in the input are integers.
Input
Input is given from Standard Input in the following format:
N A B K
Output
Print the number of the ways to paint tiles, modulo 998244353.
Examples
Input
4 1 2 5
Output
40
Input
2 5 6 0
Output
1
Input
90081 33447 90629 6391049189
Output
577742975
"Correct Solution:
```
# 解説AC
class Combination():
def __init__(self, N:int, P:int):
self.N = N
self.P = P
# fact[i] = (i! mod P)
self.fact = [1, 1]
# factinv[i] = ((i!)^(-1) mod P)
self.factinv = [1, 1]
# factinv 計算用
self.inv = [0, 1]
for i in range(2, N+1):
self.fact.append((self.fact[-1] * i) % P)
self.inv.append((-self.inv[P % i] * (P // i)) % P)
self.factinv.append((self.factinv[-1] * self.inv[-1]) % P)
# nCk (mod P) (ただし、n<=N)
def getComb(self, n:int, k:int):
if (k < 0) or (n < k): return 0
k = min(k, n - k)
return self.fact[n] * self.factinv[k] * self.factinv[n-k] % self.P
#########################################
N,A,B,K = map(int, input().split())
MOD = 998244353
C = Combination(N, MOD)
ans = 0
for a in range(N+1):
# bが整数 かつ 0<=b<=N
if (K - a * A) % B == 0 and 0 <= (K - a * A) // B <= N:
b = (K - a * A) // B
ans = (ans + C.getComb(N, a) * C.getComb(N, b)) % MOD
print(ans)
```
| 95,099 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.