message stringlengths 2 22.8k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 16 109k | cluster float64 1 1 | __index_level_0__ int64 32 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya the programmer lives in the middle of the Programming subway branch. He has two girlfriends: Dasha and Masha, who live at the different ends of the branch, each one is unaware of the other one's existence.
When Vasya has some free time, he goes to one of his girlfriends. He descends into the subway at some time, waits the first train to come and rides on it to the end of the branch to the corresponding girl. However, the trains run with different frequencies: a train goes to Dasha's direction every a minutes, but a train goes to Masha's direction every b minutes. If two trains approach at the same time, Vasya goes toward the direction with the lower frequency of going trains, that is, to the girl, to whose directions the trains go less frequently (see the note to the third sample).
We know that the trains begin to go simultaneously before Vasya appears. That is the train schedule is such that there exists a moment of time when the two trains arrive simultaneously.
Help Vasya count to which girlfriend he will go more often.
Input
The first line contains two integers a and b (a β b, 1 β€ a, b β€ 106).
Output
Print "Dasha" if Vasya will go to Dasha more frequently, "Masha" if he will go to Masha more frequently, or "Equal" if he will go to both girlfriends with the same frequency.
Examples
Input
3 7
Output
Dasha
Input
5 3
Output
Masha
Input
2 3
Output
Equal
Note
Let's take a look at the third sample. Let the trains start to go at the zero moment of time. It is clear that the moments of the trains' arrival will be periodic with period 6. That's why it is enough to show that if Vasya descends to the subway at a moment of time inside the interval (0, 6], he will go to both girls equally often.
If he descends to the subway at a moment of time from 0 to 2, he leaves for Dasha on the train that arrives by the second minute.
If he descends to the subway at a moment of time from 2 to 3, he leaves for Masha on the train that arrives by the third minute.
If he descends to the subway at a moment of time from 3 to 4, he leaves for Dasha on the train that arrives by the fourth minute.
If he descends to the subway at a moment of time from 4 to 6, he waits for both trains to arrive by the sixth minute and goes to Masha as trains go less often in Masha's direction.
In sum Masha and Dasha get equal time β three minutes for each one, thus, Vasya will go to both girlfriends equally often. | instruction | 0 | 88,924 | 1 | 177,848 |
Tags: implementation, math
Correct Solution:
```
from math import gcd
def lcm(a, b):
return (a * b) // gcd(a, b)
i = 0
a, b = map(int, input().split())
first = False
second = False
masha = 0
dasha = 0
while i < lcm(a, b):
if first:
masha += b - (i % b)
i += b - (i % b)
elif second:
dasha += a - (i % a)
i += a - (i % a)
if i % a == 0 and i % b == 0:
if a > b:
first = True
second = False
else:
first = False
second = True
elif first:
second = True
first = False
else:
first = True
second = False
if masha > dasha:
print("Dasha")
elif dasha > masha:
print("Masha")
else:
print("Equal")
``` | output | 1 | 88,924 | 1 | 177,849 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya the programmer lives in the middle of the Programming subway branch. He has two girlfriends: Dasha and Masha, who live at the different ends of the branch, each one is unaware of the other one's existence.
When Vasya has some free time, he goes to one of his girlfriends. He descends into the subway at some time, waits the first train to come and rides on it to the end of the branch to the corresponding girl. However, the trains run with different frequencies: a train goes to Dasha's direction every a minutes, but a train goes to Masha's direction every b minutes. If two trains approach at the same time, Vasya goes toward the direction with the lower frequency of going trains, that is, to the girl, to whose directions the trains go less frequently (see the note to the third sample).
We know that the trains begin to go simultaneously before Vasya appears. That is the train schedule is such that there exists a moment of time when the two trains arrive simultaneously.
Help Vasya count to which girlfriend he will go more often.
Input
The first line contains two integers a and b (a β b, 1 β€ a, b β€ 106).
Output
Print "Dasha" if Vasya will go to Dasha more frequently, "Masha" if he will go to Masha more frequently, or "Equal" if he will go to both girlfriends with the same frequency.
Examples
Input
3 7
Output
Dasha
Input
5 3
Output
Masha
Input
2 3
Output
Equal
Note
Let's take a look at the third sample. Let the trains start to go at the zero moment of time. It is clear that the moments of the trains' arrival will be periodic with period 6. That's why it is enough to show that if Vasya descends to the subway at a moment of time inside the interval (0, 6], he will go to both girls equally often.
If he descends to the subway at a moment of time from 0 to 2, he leaves for Dasha on the train that arrives by the second minute.
If he descends to the subway at a moment of time from 2 to 3, he leaves for Masha on the train that arrives by the third minute.
If he descends to the subway at a moment of time from 3 to 4, he leaves for Dasha on the train that arrives by the fourth minute.
If he descends to the subway at a moment of time from 4 to 6, he waits for both trains to arrive by the sixth minute and goes to Masha as trains go less often in Masha's direction.
In sum Masha and Dasha get equal time β three minutes for each one, thus, Vasya will go to both girlfriends equally often. | instruction | 0 | 88,925 | 1 | 177,850 |
Tags: implementation, math
Correct Solution:
```
d, m = map(int, input().split())
g, l = max(d, m), min(d, m)
while True:
if not(g % l): break
g, l = l, g%l
lcm = (d * m) // l
dcnt = (lcm // d) - 1
mcnt = (lcm // m) - 1
if d > m: dcnt += 1
else: mcnt += 1
if (dcnt == mcnt): print("Equal")
elif (dcnt > mcnt): print("Dasha")
else: print("Masha")
``` | output | 1 | 88,925 | 1 | 177,851 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya the programmer lives in the middle of the Programming subway branch. He has two girlfriends: Dasha and Masha, who live at the different ends of the branch, each one is unaware of the other one's existence.
When Vasya has some free time, he goes to one of his girlfriends. He descends into the subway at some time, waits the first train to come and rides on it to the end of the branch to the corresponding girl. However, the trains run with different frequencies: a train goes to Dasha's direction every a minutes, but a train goes to Masha's direction every b minutes. If two trains approach at the same time, Vasya goes toward the direction with the lower frequency of going trains, that is, to the girl, to whose directions the trains go less frequently (see the note to the third sample).
We know that the trains begin to go simultaneously before Vasya appears. That is the train schedule is such that there exists a moment of time when the two trains arrive simultaneously.
Help Vasya count to which girlfriend he will go more often.
Input
The first line contains two integers a and b (a β b, 1 β€ a, b β€ 106).
Output
Print "Dasha" if Vasya will go to Dasha more frequently, "Masha" if he will go to Masha more frequently, or "Equal" if he will go to both girlfriends with the same frequency.
Examples
Input
3 7
Output
Dasha
Input
5 3
Output
Masha
Input
2 3
Output
Equal
Note
Let's take a look at the third sample. Let the trains start to go at the zero moment of time. It is clear that the moments of the trains' arrival will be periodic with period 6. That's why it is enough to show that if Vasya descends to the subway at a moment of time inside the interval (0, 6], he will go to both girls equally often.
If he descends to the subway at a moment of time from 0 to 2, he leaves for Dasha on the train that arrives by the second minute.
If he descends to the subway at a moment of time from 2 to 3, he leaves for Masha on the train that arrives by the third minute.
If he descends to the subway at a moment of time from 3 to 4, he leaves for Dasha on the train that arrives by the fourth minute.
If he descends to the subway at a moment of time from 4 to 6, he waits for both trains to arrive by the sixth minute and goes to Masha as trains go less often in Masha's direction.
In sum Masha and Dasha get equal time β three minutes for each one, thus, Vasya will go to both girlfriends equally often. | instruction | 0 | 88,926 | 1 | 177,852 |
Tags: implementation, math
Correct Solution:
```
import math
import sys
input=sys.stdin.readline
a,b=list(map(int,input().split()))
x=math.gcd(a,b)
a,b=a//x,b//x
if abs(a-b)==1:
print('Equal')
elif a<b:
print('Dasha')
else:
print('Masha')
``` | output | 1 | 88,926 | 1 | 177,853 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya the programmer lives in the middle of the Programming subway branch. He has two girlfriends: Dasha and Masha, who live at the different ends of the branch, each one is unaware of the other one's existence.
When Vasya has some free time, he goes to one of his girlfriends. He descends into the subway at some time, waits the first train to come and rides on it to the end of the branch to the corresponding girl. However, the trains run with different frequencies: a train goes to Dasha's direction every a minutes, but a train goes to Masha's direction every b minutes. If two trains approach at the same time, Vasya goes toward the direction with the lower frequency of going trains, that is, to the girl, to whose directions the trains go less frequently (see the note to the third sample).
We know that the trains begin to go simultaneously before Vasya appears. That is the train schedule is such that there exists a moment of time when the two trains arrive simultaneously.
Help Vasya count to which girlfriend he will go more often.
Input
The first line contains two integers a and b (a β b, 1 β€ a, b β€ 106).
Output
Print "Dasha" if Vasya will go to Dasha more frequently, "Masha" if he will go to Masha more frequently, or "Equal" if he will go to both girlfriends with the same frequency.
Examples
Input
3 7
Output
Dasha
Input
5 3
Output
Masha
Input
2 3
Output
Equal
Note
Let's take a look at the third sample. Let the trains start to go at the zero moment of time. It is clear that the moments of the trains' arrival will be periodic with period 6. That's why it is enough to show that if Vasya descends to the subway at a moment of time inside the interval (0, 6], he will go to both girls equally often.
If he descends to the subway at a moment of time from 0 to 2, he leaves for Dasha on the train that arrives by the second minute.
If he descends to the subway at a moment of time from 2 to 3, he leaves for Masha on the train that arrives by the third minute.
If he descends to the subway at a moment of time from 3 to 4, he leaves for Dasha on the train that arrives by the fourth minute.
If he descends to the subway at a moment of time from 4 to 6, he waits for both trains to arrive by the sixth minute and goes to Masha as trains go less often in Masha's direction.
In sum Masha and Dasha get equal time β three minutes for each one, thus, Vasya will go to both girlfriends equally often. | instruction | 0 | 88,927 | 1 | 177,854 |
Tags: implementation, math
Correct Solution:
```
#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase
#from bisect import bisect_left as bl #c++ lowerbound bl(array,element)
#from bisect import bisect_right as br #c++ upperbound br(array,element)
import math
def main():
# sys.stdin = open('input.txt', 'r')
# sys.stdout = open('output.txt', 'w')
a,b=map(int,input().split(" "))
lcm=(a*b)//math.gcd(a,b)
if abs(lcm//a-lcm//b)<=1:
print("Equal")
elif lcm//a<lcm//b:
print("Masha")
else:
print("Dasha")
#-----------------------------BOSS-------------------------------------!
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
main()
``` | output | 1 | 88,927 | 1 | 177,855 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Vasya the programmer lives in the middle of the Programming subway branch. He has two girlfriends: Dasha and Masha, who live at the different ends of the branch, each one is unaware of the other one's existence.
When Vasya has some free time, he goes to one of his girlfriends. He descends into the subway at some time, waits the first train to come and rides on it to the end of the branch to the corresponding girl. However, the trains run with different frequencies: a train goes to Dasha's direction every a minutes, but a train goes to Masha's direction every b minutes. If two trains approach at the same time, Vasya goes toward the direction with the lower frequency of going trains, that is, to the girl, to whose directions the trains go less frequently (see the note to the third sample).
We know that the trains begin to go simultaneously before Vasya appears. That is the train schedule is such that there exists a moment of time when the two trains arrive simultaneously.
Help Vasya count to which girlfriend he will go more often.
Input
The first line contains two integers a and b (a β b, 1 β€ a, b β€ 106).
Output
Print "Dasha" if Vasya will go to Dasha more frequently, "Masha" if he will go to Masha more frequently, or "Equal" if he will go to both girlfriends with the same frequency.
Examples
Input
3 7
Output
Dasha
Input
5 3
Output
Masha
Input
2 3
Output
Equal
Note
Let's take a look at the third sample. Let the trains start to go at the zero moment of time. It is clear that the moments of the trains' arrival will be periodic with period 6. That's why it is enough to show that if Vasya descends to the subway at a moment of time inside the interval (0, 6], he will go to both girls equally often.
If he descends to the subway at a moment of time from 0 to 2, he leaves for Dasha on the train that arrives by the second minute.
If he descends to the subway at a moment of time from 2 to 3, he leaves for Masha on the train that arrives by the third minute.
If he descends to the subway at a moment of time from 3 to 4, he leaves for Dasha on the train that arrives by the fourth minute.
If he descends to the subway at a moment of time from 4 to 6, he waits for both trains to arrive by the sixth minute and goes to Masha as trains go less often in Masha's direction.
In sum Masha and Dasha get equal time β three minutes for each one, thus, Vasya will go to both girlfriends equally often. | instruction | 0 | 88,928 | 1 | 177,856 |
Tags: implementation, math
Correct Solution:
```
a, b = input().split()
a = int(a); b = int(b)
def gcd(a, b):
if a < b:
return gcd(b, a)
elif not a%b:
return b
return gcd(b, a%b)
lcm = a*b//gcd(a, b)
if a > b:
d_count = lcm//a
m_count = lcm//b - 1
if m_count > d_count:
print("Masha")
elif m_count == d_count:
print("Equal")
else:
print("Dasha")
else:
d_count = lcm//a - 1
m_count = lcm//b
if m_count > d_count:
print("Masha")
elif m_count == d_count:
print("Equal")
else:
print("Dasha")
``` | output | 1 | 88,928 | 1 | 177,857 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya the programmer lives in the middle of the Programming subway branch. He has two girlfriends: Dasha and Masha, who live at the different ends of the branch, each one is unaware of the other one's existence.
When Vasya has some free time, he goes to one of his girlfriends. He descends into the subway at some time, waits the first train to come and rides on it to the end of the branch to the corresponding girl. However, the trains run with different frequencies: a train goes to Dasha's direction every a minutes, but a train goes to Masha's direction every b minutes. If two trains approach at the same time, Vasya goes toward the direction with the lower frequency of going trains, that is, to the girl, to whose directions the trains go less frequently (see the note to the third sample).
We know that the trains begin to go simultaneously before Vasya appears. That is the train schedule is such that there exists a moment of time when the two trains arrive simultaneously.
Help Vasya count to which girlfriend he will go more often.
Input
The first line contains two integers a and b (a β b, 1 β€ a, b β€ 106).
Output
Print "Dasha" if Vasya will go to Dasha more frequently, "Masha" if he will go to Masha more frequently, or "Equal" if he will go to both girlfriends with the same frequency.
Examples
Input
3 7
Output
Dasha
Input
5 3
Output
Masha
Input
2 3
Output
Equal
Note
Let's take a look at the third sample. Let the trains start to go at the zero moment of time. It is clear that the moments of the trains' arrival will be periodic with period 6. That's why it is enough to show that if Vasya descends to the subway at a moment of time inside the interval (0, 6], he will go to both girls equally often.
If he descends to the subway at a moment of time from 0 to 2, he leaves for Dasha on the train that arrives by the second minute.
If he descends to the subway at a moment of time from 2 to 3, he leaves for Masha on the train that arrives by the third minute.
If he descends to the subway at a moment of time from 3 to 4, he leaves for Dasha on the train that arrives by the fourth minute.
If he descends to the subway at a moment of time from 4 to 6, he waits for both trains to arrive by the sixth minute and goes to Masha as trains go less often in Masha's direction.
In sum Masha and Dasha get equal time β three minutes for each one, thus, Vasya will go to both girlfriends equally often.
Submitted Solution:
```
a, b = map(int, input().split())
flag = 0
if a > b:
flag = 1
a, b = b, a
k, x, y = 1, 0, 0
while b * k % a:
x += b - b * k % a
y += b * k % a
k += 1
x += b - a
y += a
print([['Masha', 'Equal'][x == y], 'Dasha'][flag and y > x or not flag and x > y])
``` | instruction | 0 | 88,929 | 1 | 177,858 |
Yes | output | 1 | 88,929 | 1 | 177,859 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya the programmer lives in the middle of the Programming subway branch. He has two girlfriends: Dasha and Masha, who live at the different ends of the branch, each one is unaware of the other one's existence.
When Vasya has some free time, he goes to one of his girlfriends. He descends into the subway at some time, waits the first train to come and rides on it to the end of the branch to the corresponding girl. However, the trains run with different frequencies: a train goes to Dasha's direction every a minutes, but a train goes to Masha's direction every b minutes. If two trains approach at the same time, Vasya goes toward the direction with the lower frequency of going trains, that is, to the girl, to whose directions the trains go less frequently (see the note to the third sample).
We know that the trains begin to go simultaneously before Vasya appears. That is the train schedule is such that there exists a moment of time when the two trains arrive simultaneously.
Help Vasya count to which girlfriend he will go more often.
Input
The first line contains two integers a and b (a β b, 1 β€ a, b β€ 106).
Output
Print "Dasha" if Vasya will go to Dasha more frequently, "Masha" if he will go to Masha more frequently, or "Equal" if he will go to both girlfriends with the same frequency.
Examples
Input
3 7
Output
Dasha
Input
5 3
Output
Masha
Input
2 3
Output
Equal
Note
Let's take a look at the third sample. Let the trains start to go at the zero moment of time. It is clear that the moments of the trains' arrival will be periodic with period 6. That's why it is enough to show that if Vasya descends to the subway at a moment of time inside the interval (0, 6], he will go to both girls equally often.
If he descends to the subway at a moment of time from 0 to 2, he leaves for Dasha on the train that arrives by the second minute.
If he descends to the subway at a moment of time from 2 to 3, he leaves for Masha on the train that arrives by the third minute.
If he descends to the subway at a moment of time from 3 to 4, he leaves for Dasha on the train that arrives by the fourth minute.
If he descends to the subway at a moment of time from 4 to 6, he waits for both trains to arrive by the sixth minute and goes to Masha as trains go less often in Masha's direction.
In sum Masha and Dasha get equal time β three minutes for each one, thus, Vasya will go to both girlfriends equally often.
Submitted Solution:
```
from math import gcd
def lcm(x , y):
return (x*y) // gcd(x , y)
a , b = map(int,input().split())
r1 = lcm(a , b)
r2 = r1 // a
r3 = r1 // b
r2 -= 1
r3 -= 1
if r2 < r3 :
r2 += 1
if r3 < r2 :
r3 += 1
if r2 == r3:
print('Equal')
elif r2 > r3 :
print('Dasha')
else:
print('Masha')
``` | instruction | 0 | 88,930 | 1 | 177,860 |
Yes | output | 1 | 88,930 | 1 | 177,861 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya the programmer lives in the middle of the Programming subway branch. He has two girlfriends: Dasha and Masha, who live at the different ends of the branch, each one is unaware of the other one's existence.
When Vasya has some free time, he goes to one of his girlfriends. He descends into the subway at some time, waits the first train to come and rides on it to the end of the branch to the corresponding girl. However, the trains run with different frequencies: a train goes to Dasha's direction every a minutes, but a train goes to Masha's direction every b minutes. If two trains approach at the same time, Vasya goes toward the direction with the lower frequency of going trains, that is, to the girl, to whose directions the trains go less frequently (see the note to the third sample).
We know that the trains begin to go simultaneously before Vasya appears. That is the train schedule is such that there exists a moment of time when the two trains arrive simultaneously.
Help Vasya count to which girlfriend he will go more often.
Input
The first line contains two integers a and b (a β b, 1 β€ a, b β€ 106).
Output
Print "Dasha" if Vasya will go to Dasha more frequently, "Masha" if he will go to Masha more frequently, or "Equal" if he will go to both girlfriends with the same frequency.
Examples
Input
3 7
Output
Dasha
Input
5 3
Output
Masha
Input
2 3
Output
Equal
Note
Let's take a look at the third sample. Let the trains start to go at the zero moment of time. It is clear that the moments of the trains' arrival will be periodic with period 6. That's why it is enough to show that if Vasya descends to the subway at a moment of time inside the interval (0, 6], he will go to both girls equally often.
If he descends to the subway at a moment of time from 0 to 2, he leaves for Dasha on the train that arrives by the second minute.
If he descends to the subway at a moment of time from 2 to 3, he leaves for Masha on the train that arrives by the third minute.
If he descends to the subway at a moment of time from 3 to 4, he leaves for Dasha on the train that arrives by the fourth minute.
If he descends to the subway at a moment of time from 4 to 6, he waits for both trains to arrive by the sixth minute and goes to Masha as trains go less often in Masha's direction.
In sum Masha and Dasha get equal time β three minutes for each one, thus, Vasya will go to both girlfriends equally often.
Submitted Solution:
```
def GCD(a,b):
if b==0:
return a
else:
return GCD(b,a%b)
s = input()
a = int(s.split()[0])
b = int(s.split()[1])
g = GCD(a,b)
l = a*b//g
x = l//a
y = l//b
if x==y or abs(x-y)==1:
print('Equal')
elif x>y:
print('Dasha')
else:
print('Masha')
``` | instruction | 0 | 88,931 | 1 | 177,862 |
Yes | output | 1 | 88,931 | 1 | 177,863 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya the programmer lives in the middle of the Programming subway branch. He has two girlfriends: Dasha and Masha, who live at the different ends of the branch, each one is unaware of the other one's existence.
When Vasya has some free time, he goes to one of his girlfriends. He descends into the subway at some time, waits the first train to come and rides on it to the end of the branch to the corresponding girl. However, the trains run with different frequencies: a train goes to Dasha's direction every a minutes, but a train goes to Masha's direction every b minutes. If two trains approach at the same time, Vasya goes toward the direction with the lower frequency of going trains, that is, to the girl, to whose directions the trains go less frequently (see the note to the third sample).
We know that the trains begin to go simultaneously before Vasya appears. That is the train schedule is such that there exists a moment of time when the two trains arrive simultaneously.
Help Vasya count to which girlfriend he will go more often.
Input
The first line contains two integers a and b (a β b, 1 β€ a, b β€ 106).
Output
Print "Dasha" if Vasya will go to Dasha more frequently, "Masha" if he will go to Masha more frequently, or "Equal" if he will go to both girlfriends with the same frequency.
Examples
Input
3 7
Output
Dasha
Input
5 3
Output
Masha
Input
2 3
Output
Equal
Note
Let's take a look at the third sample. Let the trains start to go at the zero moment of time. It is clear that the moments of the trains' arrival will be periodic with period 6. That's why it is enough to show that if Vasya descends to the subway at a moment of time inside the interval (0, 6], he will go to both girls equally often.
If he descends to the subway at a moment of time from 0 to 2, he leaves for Dasha on the train that arrives by the second minute.
If he descends to the subway at a moment of time from 2 to 3, he leaves for Masha on the train that arrives by the third minute.
If he descends to the subway at a moment of time from 3 to 4, he leaves for Dasha on the train that arrives by the fourth minute.
If he descends to the subway at a moment of time from 4 to 6, he waits for both trains to arrive by the sixth minute and goes to Masha as trains go less often in Masha's direction.
In sum Masha and Dasha get equal time β three minutes for each one, thus, Vasya will go to both girlfriends equally often.
Submitted Solution:
```
a,b=map(int,input().split())
x,y=a,b
while(y):
x, y = y, x % y
n=a*b//x
d=n/a
m=n/b
if(a>b):
d+=1
if(b>a):
m+=1
if d>m:
print('Dasha')
elif m>d:
print('Masha')
else:
print('Equal')
``` | instruction | 0 | 88,932 | 1 | 177,864 |
Yes | output | 1 | 88,932 | 1 | 177,865 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya the programmer lives in the middle of the Programming subway branch. He has two girlfriends: Dasha and Masha, who live at the different ends of the branch, each one is unaware of the other one's existence.
When Vasya has some free time, he goes to one of his girlfriends. He descends into the subway at some time, waits the first train to come and rides on it to the end of the branch to the corresponding girl. However, the trains run with different frequencies: a train goes to Dasha's direction every a minutes, but a train goes to Masha's direction every b minutes. If two trains approach at the same time, Vasya goes toward the direction with the lower frequency of going trains, that is, to the girl, to whose directions the trains go less frequently (see the note to the third sample).
We know that the trains begin to go simultaneously before Vasya appears. That is the train schedule is such that there exists a moment of time when the two trains arrive simultaneously.
Help Vasya count to which girlfriend he will go more often.
Input
The first line contains two integers a and b (a β b, 1 β€ a, b β€ 106).
Output
Print "Dasha" if Vasya will go to Dasha more frequently, "Masha" if he will go to Masha more frequently, or "Equal" if he will go to both girlfriends with the same frequency.
Examples
Input
3 7
Output
Dasha
Input
5 3
Output
Masha
Input
2 3
Output
Equal
Note
Let's take a look at the third sample. Let the trains start to go at the zero moment of time. It is clear that the moments of the trains' arrival will be periodic with period 6. That's why it is enough to show that if Vasya descends to the subway at a moment of time inside the interval (0, 6], he will go to both girls equally often.
If he descends to the subway at a moment of time from 0 to 2, he leaves for Dasha on the train that arrives by the second minute.
If he descends to the subway at a moment of time from 2 to 3, he leaves for Masha on the train that arrives by the third minute.
If he descends to the subway at a moment of time from 3 to 4, he leaves for Dasha on the train that arrives by the fourth minute.
If he descends to the subway at a moment of time from 4 to 6, he waits for both trains to arrive by the sixth minute and goes to Masha as trains go less often in Masha's direction.
In sum Masha and Dasha get equal time β three minutes for each one, thus, Vasya will go to both girlfriends equally often.
Submitted Solution:
```
x=input().split()
d =int(x[0])
m =int(x[1])
b= max(d,m); s= min(d,m); a=0
while(True):
if (b%d==0 and b%m==0):
c=b
break
a+=1
b=b*a
y = c/d
z = c/m
if d>m:
y+=1
else:
z+=1
if y>z:
print("Dasha")
elif y<z:
print("Masha")
else:
print("Equal")
``` | instruction | 0 | 88,933 | 1 | 177,866 |
No | output | 1 | 88,933 | 1 | 177,867 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya the programmer lives in the middle of the Programming subway branch. He has two girlfriends: Dasha and Masha, who live at the different ends of the branch, each one is unaware of the other one's existence.
When Vasya has some free time, he goes to one of his girlfriends. He descends into the subway at some time, waits the first train to come and rides on it to the end of the branch to the corresponding girl. However, the trains run with different frequencies: a train goes to Dasha's direction every a minutes, but a train goes to Masha's direction every b minutes. If two trains approach at the same time, Vasya goes toward the direction with the lower frequency of going trains, that is, to the girl, to whose directions the trains go less frequently (see the note to the third sample).
We know that the trains begin to go simultaneously before Vasya appears. That is the train schedule is such that there exists a moment of time when the two trains arrive simultaneously.
Help Vasya count to which girlfriend he will go more often.
Input
The first line contains two integers a and b (a β b, 1 β€ a, b β€ 106).
Output
Print "Dasha" if Vasya will go to Dasha more frequently, "Masha" if he will go to Masha more frequently, or "Equal" if he will go to both girlfriends with the same frequency.
Examples
Input
3 7
Output
Dasha
Input
5 3
Output
Masha
Input
2 3
Output
Equal
Note
Let's take a look at the third sample. Let the trains start to go at the zero moment of time. It is clear that the moments of the trains' arrival will be periodic with period 6. That's why it is enough to show that if Vasya descends to the subway at a moment of time inside the interval (0, 6], he will go to both girls equally often.
If he descends to the subway at a moment of time from 0 to 2, he leaves for Dasha on the train that arrives by the second minute.
If he descends to the subway at a moment of time from 2 to 3, he leaves for Masha on the train that arrives by the third minute.
If he descends to the subway at a moment of time from 3 to 4, he leaves for Dasha on the train that arrives by the fourth minute.
If he descends to the subway at a moment of time from 4 to 6, he waits for both trains to arrive by the sixth minute and goes to Masha as trains go less often in Masha's direction.
In sum Masha and Dasha get equal time β three minutes for each one, thus, Vasya will go to both girlfriends equally often.
Submitted Solution:
```
from math import *
a,b=map(int,input().split())
a1=0
b1=0
for i in range(1,gcd(a,b)+1):
if i%a==0 and i%b!=0:a1+=1
if i%b==0 and i%a!=0:b1+=1
if i%a==0 and i%b==0:
if a>b:a1+=1
else:b1+=1
if a1>b1:
print('Dasha')
elif b1>a1:
print('Masha')
else:print('Equal')
``` | instruction | 0 | 88,934 | 1 | 177,868 |
No | output | 1 | 88,934 | 1 | 177,869 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya the programmer lives in the middle of the Programming subway branch. He has two girlfriends: Dasha and Masha, who live at the different ends of the branch, each one is unaware of the other one's existence.
When Vasya has some free time, he goes to one of his girlfriends. He descends into the subway at some time, waits the first train to come and rides on it to the end of the branch to the corresponding girl. However, the trains run with different frequencies: a train goes to Dasha's direction every a minutes, but a train goes to Masha's direction every b minutes. If two trains approach at the same time, Vasya goes toward the direction with the lower frequency of going trains, that is, to the girl, to whose directions the trains go less frequently (see the note to the third sample).
We know that the trains begin to go simultaneously before Vasya appears. That is the train schedule is such that there exists a moment of time when the two trains arrive simultaneously.
Help Vasya count to which girlfriend he will go more often.
Input
The first line contains two integers a and b (a β b, 1 β€ a, b β€ 106).
Output
Print "Dasha" if Vasya will go to Dasha more frequently, "Masha" if he will go to Masha more frequently, or "Equal" if he will go to both girlfriends with the same frequency.
Examples
Input
3 7
Output
Dasha
Input
5 3
Output
Masha
Input
2 3
Output
Equal
Note
Let's take a look at the third sample. Let the trains start to go at the zero moment of time. It is clear that the moments of the trains' arrival will be periodic with period 6. That's why it is enough to show that if Vasya descends to the subway at a moment of time inside the interval (0, 6], he will go to both girls equally often.
If he descends to the subway at a moment of time from 0 to 2, he leaves for Dasha on the train that arrives by the second minute.
If he descends to the subway at a moment of time from 2 to 3, he leaves for Masha on the train that arrives by the third minute.
If he descends to the subway at a moment of time from 3 to 4, he leaves for Dasha on the train that arrives by the fourth minute.
If he descends to the subway at a moment of time from 4 to 6, he waits for both trains to arrive by the sixth minute and goes to Masha as trains go less often in Masha's direction.
In sum Masha and Dasha get equal time β three minutes for each one, thus, Vasya will go to both girlfriends equally often.
Submitted Solution:
```
import sys
a, b = input().split()
a = int(a)
b = int(b)
def gcd(a,b):
if(b==0):
return a
else:
return gcd(b,a%b)
a = a / gcd(a,b)
b = b / gcd(a,b)
if(abs(a-b)==1):
print('Equal')
sys.exit()
if(a < b):
print('Dasha')
sys.exit()
if(a > b):
print('Masha')
sys.exit()
``` | instruction | 0 | 88,935 | 1 | 177,870 |
No | output | 1 | 88,935 | 1 | 177,871 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Vasya the programmer lives in the middle of the Programming subway branch. He has two girlfriends: Dasha and Masha, who live at the different ends of the branch, each one is unaware of the other one's existence.
When Vasya has some free time, he goes to one of his girlfriends. He descends into the subway at some time, waits the first train to come and rides on it to the end of the branch to the corresponding girl. However, the trains run with different frequencies: a train goes to Dasha's direction every a minutes, but a train goes to Masha's direction every b minutes. If two trains approach at the same time, Vasya goes toward the direction with the lower frequency of going trains, that is, to the girl, to whose directions the trains go less frequently (see the note to the third sample).
We know that the trains begin to go simultaneously before Vasya appears. That is the train schedule is such that there exists a moment of time when the two trains arrive simultaneously.
Help Vasya count to which girlfriend he will go more often.
Input
The first line contains two integers a and b (a β b, 1 β€ a, b β€ 106).
Output
Print "Dasha" if Vasya will go to Dasha more frequently, "Masha" if he will go to Masha more frequently, or "Equal" if he will go to both girlfriends with the same frequency.
Examples
Input
3 7
Output
Dasha
Input
5 3
Output
Masha
Input
2 3
Output
Equal
Note
Let's take a look at the third sample. Let the trains start to go at the zero moment of time. It is clear that the moments of the trains' arrival will be periodic with period 6. That's why it is enough to show that if Vasya descends to the subway at a moment of time inside the interval (0, 6], he will go to both girls equally often.
If he descends to the subway at a moment of time from 0 to 2, he leaves for Dasha on the train that arrives by the second minute.
If he descends to the subway at a moment of time from 2 to 3, he leaves for Masha on the train that arrives by the third minute.
If he descends to the subway at a moment of time from 3 to 4, he leaves for Dasha on the train that arrives by the fourth minute.
If he descends to the subway at a moment of time from 4 to 6, he waits for both trains to arrive by the sixth minute and goes to Masha as trains go less often in Masha's direction.
In sum Masha and Dasha get equal time β three minutes for each one, thus, Vasya will go to both girlfriends equally often.
Submitted Solution:
```
a,b=map(int,input().split())
g=lambda x,y:g(y%x,x)if x else y
g=g(a,b)
c=a//g*b
A=c//a-g
B=c//b-g
if a<b:B+=g
if a>b:A+=g
if A>B:r='Dasha'
elif A<B:r='Masha'
else:r='Equal'
print(r)
``` | instruction | 0 | 88,936 | 1 | 177,872 |
No | output | 1 | 88,936 | 1 | 177,873 |
Provide a correct Python 3 solution for this coding contest problem.
In Aizuwakamatsu Village, which is located far north of Aizuwakamatsu City, a bridge called "Yabashi" is the only way to move to the surrounding villages. Despite the large number of passers-by, the bridge is so old that it is almost broken.
<image>
Yabashi is strong enough to withstand up to 150 [kg]. For example, 80 [kg] people and 50 [kg] people can cross at the same time, but if 90 [kg] people start crossing while 80 [kg] people are crossing, Yabashi will It will break.
If the Ya Bridge is broken, the people of Aizu Komatsu Village will lose the means to move to the surrounding villages. So, as the only programmer in the village, you decided to write a program to determine if the bridge would break based on how you crossed the bridge, in order to protect the lives of the villagers.
Number of passersby crossing the bridge n (1 β€ n β€ 100), weight of each passer mi (1 β€ mi β€ 100), time to start crossing the bridge ai, time to finish crossing bi (0 β€ ai, bi <231) If the bridge does not break, output "OK", and if it breaks, output "NG". If the total weight of passers-by on the bridge exceeds 150 [kg], the bridge will be destroyed. Also, at the time of ai, the passersby are on the bridge, but at the time of bi, they are not on the bridge.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
m1 a1 b1
m2 a2 b2
::
mn an ββbn
The number of datasets does not exceed 1200.
Output
For each input dataset, prints on one line whether the bridge will break or not.
Example
Input
3
80 0 30
50 5 25
90 27 50
3
80 0 30
70 5 25
71 30 50
0
Output
NG
OK | instruction | 0 | 89,158 | 1 | 178,316 |
"Correct Solution:
```
from operator import itemgetter
while 1:
n = int(input())
if n == 0:
break
t_w = []
for _ in range(n):
m, a, b = map(int, input().split())
t_w.append((a, m, 5))
t_w.append((b, -m, 3))
t_w.sort(key=itemgetter(0, 2))
ans, cur = 'OK', 0
for t, w, pri in t_w:
cur += w
# print('t', t, 'w', w, 'cur', cur)
if cur > 150:
ans = 'NG'
break
print(ans)
``` | output | 1 | 89,158 | 1 | 178,317 |
Provide a correct Python 3 solution for this coding contest problem.
In Aizuwakamatsu Village, which is located far north of Aizuwakamatsu City, a bridge called "Yabashi" is the only way to move to the surrounding villages. Despite the large number of passers-by, the bridge is so old that it is almost broken.
<image>
Yabashi is strong enough to withstand up to 150 [kg]. For example, 80 [kg] people and 50 [kg] people can cross at the same time, but if 90 [kg] people start crossing while 80 [kg] people are crossing, Yabashi will It will break.
If the Ya Bridge is broken, the people of Aizu Komatsu Village will lose the means to move to the surrounding villages. So, as the only programmer in the village, you decided to write a program to determine if the bridge would break based on how you crossed the bridge, in order to protect the lives of the villagers.
Number of passersby crossing the bridge n (1 β€ n β€ 100), weight of each passer mi (1 β€ mi β€ 100), time to start crossing the bridge ai, time to finish crossing bi (0 β€ ai, bi <231) If the bridge does not break, output "OK", and if it breaks, output "NG". If the total weight of passers-by on the bridge exceeds 150 [kg], the bridge will be destroyed. Also, at the time of ai, the passersby are on the bridge, but at the time of bi, they are not on the bridge.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
m1 a1 b1
m2 a2 b2
::
mn an ββbn
The number of datasets does not exceed 1200.
Output
For each input dataset, prints on one line whether the bridge will break or not.
Example
Input
3
80 0 30
50 5 25
90 27 50
3
80 0 30
70 5 25
71 30 50
0
Output
NG
OK | instruction | 0 | 89,159 | 1 | 178,318 |
"Correct Solution:
```
import operator
while 1:
n = int(input())
if n == 0:break
s = [list(map(int,input().split())) for i in range(n)]
e = s[:]
s.sort(key = operator.itemgetter(1))
e.sort(key = operator.itemgetter(2))
s_i = e_i = 0
w = 0
for i in range(n*2):
if e_i < len(e) and s[s_i][1] >= e[e_i][2]:
w -= e[e_i][0]
e_i += 1
else:
w += s[s_i][0]
s_i += 1
if w > 150:
print('NG')
break
if i == n*2-1 or s_i >= len(s):
print('OK')
break
``` | output | 1 | 89,159 | 1 | 178,319 |
Provide a correct Python 3 solution for this coding contest problem.
In Aizuwakamatsu Village, which is located far north of Aizuwakamatsu City, a bridge called "Yabashi" is the only way to move to the surrounding villages. Despite the large number of passers-by, the bridge is so old that it is almost broken.
<image>
Yabashi is strong enough to withstand up to 150 [kg]. For example, 80 [kg] people and 50 [kg] people can cross at the same time, but if 90 [kg] people start crossing while 80 [kg] people are crossing, Yabashi will It will break.
If the Ya Bridge is broken, the people of Aizu Komatsu Village will lose the means to move to the surrounding villages. So, as the only programmer in the village, you decided to write a program to determine if the bridge would break based on how you crossed the bridge, in order to protect the lives of the villagers.
Number of passersby crossing the bridge n (1 β€ n β€ 100), weight of each passer mi (1 β€ mi β€ 100), time to start crossing the bridge ai, time to finish crossing bi (0 β€ ai, bi <231) If the bridge does not break, output "OK", and if it breaks, output "NG". If the total weight of passers-by on the bridge exceeds 150 [kg], the bridge will be destroyed. Also, at the time of ai, the passersby are on the bridge, but at the time of bi, they are not on the bridge.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
m1 a1 b1
m2 a2 b2
::
mn an ββbn
The number of datasets does not exceed 1200.
Output
For each input dataset, prints on one line whether the bridge will break or not.
Example
Input
3
80 0 30
50 5 25
90 27 50
3
80 0 30
70 5 25
71 30 50
0
Output
NG
OK | instruction | 0 | 89,160 | 1 | 178,320 |
"Correct Solution:
```
while 1:
N = int(input())
if N == 0:
break
q = []
M = {}
for i in range(N):
m, a, b = map(int, input().split())
M[a] = M.get(a, 0) + m
M[b] = M.get(b, 0) - m
*q, = M.items()
q.sort()
ok = 1
cur = 0
for t, v in q:
cur += v
if cur > 150:
ok = 0
print("OK" if ok else "NG")
``` | output | 1 | 89,160 | 1 | 178,321 |
Provide a correct Python 3 solution for this coding contest problem.
In Aizuwakamatsu Village, which is located far north of Aizuwakamatsu City, a bridge called "Yabashi" is the only way to move to the surrounding villages. Despite the large number of passers-by, the bridge is so old that it is almost broken.
<image>
Yabashi is strong enough to withstand up to 150 [kg]. For example, 80 [kg] people and 50 [kg] people can cross at the same time, but if 90 [kg] people start crossing while 80 [kg] people are crossing, Yabashi will It will break.
If the Ya Bridge is broken, the people of Aizu Komatsu Village will lose the means to move to the surrounding villages. So, as the only programmer in the village, you decided to write a program to determine if the bridge would break based on how you crossed the bridge, in order to protect the lives of the villagers.
Number of passersby crossing the bridge n (1 β€ n β€ 100), weight of each passer mi (1 β€ mi β€ 100), time to start crossing the bridge ai, time to finish crossing bi (0 β€ ai, bi <231) If the bridge does not break, output "OK", and if it breaks, output "NG". If the total weight of passers-by on the bridge exceeds 150 [kg], the bridge will be destroyed. Also, at the time of ai, the passersby are on the bridge, but at the time of bi, they are not on the bridge.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
m1 a1 b1
m2 a2 b2
::
mn an ββbn
The number of datasets does not exceed 1200.
Output
For each input dataset, prints on one line whether the bridge will break or not.
Example
Input
3
80 0 30
50 5 25
90 27 50
3
80 0 30
70 5 25
71 30 50
0
Output
NG
OK | instruction | 0 | 89,161 | 1 | 178,322 |
"Correct Solution:
```
while True:
N = int(input())
if not N:
break
e = []
for i in range(N):
m, a, b = map(int, input().split())
e.append((a, m))
e.append((b, -m))
M = 0
ot = 0
for t, m in sorted(e):
if M > 150 and ot != t:
print('NG')
break
M += m
ot = t
else:
print('OK')
``` | output | 1 | 89,161 | 1 | 178,323 |
Provide a correct Python 3 solution for this coding contest problem.
In Aizuwakamatsu Village, which is located far north of Aizuwakamatsu City, a bridge called "Yabashi" is the only way to move to the surrounding villages. Despite the large number of passers-by, the bridge is so old that it is almost broken.
<image>
Yabashi is strong enough to withstand up to 150 [kg]. For example, 80 [kg] people and 50 [kg] people can cross at the same time, but if 90 [kg] people start crossing while 80 [kg] people are crossing, Yabashi will It will break.
If the Ya Bridge is broken, the people of Aizu Komatsu Village will lose the means to move to the surrounding villages. So, as the only programmer in the village, you decided to write a program to determine if the bridge would break based on how you crossed the bridge, in order to protect the lives of the villagers.
Number of passersby crossing the bridge n (1 β€ n β€ 100), weight of each passer mi (1 β€ mi β€ 100), time to start crossing the bridge ai, time to finish crossing bi (0 β€ ai, bi <231) If the bridge does not break, output "OK", and if it breaks, output "NG". If the total weight of passers-by on the bridge exceeds 150 [kg], the bridge will be destroyed. Also, at the time of ai, the passersby are on the bridge, but at the time of bi, they are not on the bridge.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
m1 a1 b1
m2 a2 b2
::
mn an ββbn
The number of datasets does not exceed 1200.
Output
For each input dataset, prints on one line whether the bridge will break or not.
Example
Input
3
80 0 30
50 5 25
90 27 50
3
80 0 30
70 5 25
71 30 50
0
Output
NG
OK | instruction | 0 | 89,162 | 1 | 178,324 |
"Correct Solution:
```
while True:
n = int(input())
if n == 0:
break
tlst = []
qlst = []
for _ in range(n):
m, a, b = map(int, input().split())
qlst.append((m, a, b))
tlst.append(a)
tlst.append(b)
tlst.append(b - 1)
tlst = sorted(list(set(tlst)))
tlst.sort()
tdic = {}
for i, t in enumerate(tlst):
tdic[t] = i
lent = len(tlst)
mp = [0] * lent
for m, a, b in qlst:
a, b = tdic[a], tdic[b]
mp[a] += m
mp[b] -= m
acc = 0
for i in range(lent):
acc += mp[i]
mp[i] = acc
if acc > 150:
print("NG")
break
else:
print("OK")
``` | output | 1 | 89,162 | 1 | 178,325 |
Provide a correct Python 3 solution for this coding contest problem.
In Aizuwakamatsu Village, which is located far north of Aizuwakamatsu City, a bridge called "Yabashi" is the only way to move to the surrounding villages. Despite the large number of passers-by, the bridge is so old that it is almost broken.
<image>
Yabashi is strong enough to withstand up to 150 [kg]. For example, 80 [kg] people and 50 [kg] people can cross at the same time, but if 90 [kg] people start crossing while 80 [kg] people are crossing, Yabashi will It will break.
If the Ya Bridge is broken, the people of Aizu Komatsu Village will lose the means to move to the surrounding villages. So, as the only programmer in the village, you decided to write a program to determine if the bridge would break based on how you crossed the bridge, in order to protect the lives of the villagers.
Number of passersby crossing the bridge n (1 β€ n β€ 100), weight of each passer mi (1 β€ mi β€ 100), time to start crossing the bridge ai, time to finish crossing bi (0 β€ ai, bi <231) If the bridge does not break, output "OK", and if it breaks, output "NG". If the total weight of passers-by on the bridge exceeds 150 [kg], the bridge will be destroyed. Also, at the time of ai, the passersby are on the bridge, but at the time of bi, they are not on the bridge.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
m1 a1 b1
m2 a2 b2
::
mn an ββbn
The number of datasets does not exceed 1200.
Output
For each input dataset, prints on one line whether the bridge will break or not.
Example
Input
3
80 0 30
50 5 25
90 27 50
3
80 0 30
70 5 25
71 30 50
0
Output
NG
OK | instruction | 0 | 89,163 | 1 | 178,326 |
"Correct Solution:
```
# -*- coding: utf-8 -*-
"""
Dangerous Bridge
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0231
"""
import sys
def solve(n):
events = []
for _ in range(n):
m, a, b = map(int, input().split())
events.append([a, m])
events.append([b, -m])
weight = 0
for t, m in sorted(events):
weight += m
if weight > 150:
return 'NG'
return 'OK'
def main(args):
while True:
n = int(input())
if n == 0:
break
ans = solve(n)
print(ans)
if __name__ == '__main__':
main(sys.argv[1:])
``` | output | 1 | 89,163 | 1 | 178,327 |
Provide a correct Python 3 solution for this coding contest problem.
In Aizuwakamatsu Village, which is located far north of Aizuwakamatsu City, a bridge called "Yabashi" is the only way to move to the surrounding villages. Despite the large number of passers-by, the bridge is so old that it is almost broken.
<image>
Yabashi is strong enough to withstand up to 150 [kg]. For example, 80 [kg] people and 50 [kg] people can cross at the same time, but if 90 [kg] people start crossing while 80 [kg] people are crossing, Yabashi will It will break.
If the Ya Bridge is broken, the people of Aizu Komatsu Village will lose the means to move to the surrounding villages. So, as the only programmer in the village, you decided to write a program to determine if the bridge would break based on how you crossed the bridge, in order to protect the lives of the villagers.
Number of passersby crossing the bridge n (1 β€ n β€ 100), weight of each passer mi (1 β€ mi β€ 100), time to start crossing the bridge ai, time to finish crossing bi (0 β€ ai, bi <231) If the bridge does not break, output "OK", and if it breaks, output "NG". If the total weight of passers-by on the bridge exceeds 150 [kg], the bridge will be destroyed. Also, at the time of ai, the passersby are on the bridge, but at the time of bi, they are not on the bridge.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
m1 a1 b1
m2 a2 b2
::
mn an ββbn
The number of datasets does not exceed 1200.
Output
For each input dataset, prints on one line whether the bridge will break or not.
Example
Input
3
80 0 30
50 5 25
90 27 50
3
80 0 30
70 5 25
71 30 50
0
Output
NG
OK | instruction | 0 | 89,164 | 1 | 178,328 |
"Correct Solution:
```
# coding: utf-8
# Your code here!
class Sch:
def __init__(self, m, t):
self.m = m
self.t = t
def __lt__(self,other):
if self.t == other.t:
return self.m < other.m
else:
return self.t < other.t
while True:
N = int(input())
if N == 0:
break
schedule = []
for l in range(N):
m,a,b = [int(i) for i in input().split()]
schedule.append(Sch(m,a))
schedule.append(Sch(-m,b))
schedule.sort()
M = 0
ans = "OK"
for i in range(len(schedule)):
M = M + schedule[i].m
if M > 150:
ans = "NG"
break
print(ans)
``` | output | 1 | 89,164 | 1 | 178,329 |
Provide a correct Python 3 solution for this coding contest problem.
In Aizuwakamatsu Village, which is located far north of Aizuwakamatsu City, a bridge called "Yabashi" is the only way to move to the surrounding villages. Despite the large number of passers-by, the bridge is so old that it is almost broken.
<image>
Yabashi is strong enough to withstand up to 150 [kg]. For example, 80 [kg] people and 50 [kg] people can cross at the same time, but if 90 [kg] people start crossing while 80 [kg] people are crossing, Yabashi will It will break.
If the Ya Bridge is broken, the people of Aizu Komatsu Village will lose the means to move to the surrounding villages. So, as the only programmer in the village, you decided to write a program to determine if the bridge would break based on how you crossed the bridge, in order to protect the lives of the villagers.
Number of passersby crossing the bridge n (1 β€ n β€ 100), weight of each passer mi (1 β€ mi β€ 100), time to start crossing the bridge ai, time to finish crossing bi (0 β€ ai, bi <231) If the bridge does not break, output "OK", and if it breaks, output "NG". If the total weight of passers-by on the bridge exceeds 150 [kg], the bridge will be destroyed. Also, at the time of ai, the passersby are on the bridge, but at the time of bi, they are not on the bridge.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
m1 a1 b1
m2 a2 b2
::
mn an ββbn
The number of datasets does not exceed 1200.
Output
For each input dataset, prints on one line whether the bridge will break or not.
Example
Input
3
80 0 30
50 5 25
90 27 50
3
80 0 30
70 5 25
71 30 50
0
Output
NG
OK | instruction | 0 | 89,165 | 1 | 178,330 |
"Correct Solution:
```
while True:
n = int(input())
if n == 0:
break
tlst = []
qlst = []
for _ in range(n):
m, a, b = map(int, input().split())
qlst.append((m, a, b))
tlst.append(a)
tlst.append(b)
tlst = sorted(list(set(tlst)))
tlst.sort()
tdic = {}
for i, t in enumerate(tlst):
tdic[t] = i
lent = len(tlst)
mp = [0] * lent
for m, a, b in qlst:
a, b = tdic[a], tdic[b]
mp[a] += m
mp[b] -= m
acc = 0
for i in range(lent):
acc += mp[i]
if acc > 150:
print("NG")
break
else:
print("OK")
``` | output | 1 | 89,165 | 1 | 178,331 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In Aizuwakamatsu Village, which is located far north of Aizuwakamatsu City, a bridge called "Yabashi" is the only way to move to the surrounding villages. Despite the large number of passers-by, the bridge is so old that it is almost broken.
<image>
Yabashi is strong enough to withstand up to 150 [kg]. For example, 80 [kg] people and 50 [kg] people can cross at the same time, but if 90 [kg] people start crossing while 80 [kg] people are crossing, Yabashi will It will break.
If the Ya Bridge is broken, the people of Aizu Komatsu Village will lose the means to move to the surrounding villages. So, as the only programmer in the village, you decided to write a program to determine if the bridge would break based on how you crossed the bridge, in order to protect the lives of the villagers.
Number of passersby crossing the bridge n (1 β€ n β€ 100), weight of each passer mi (1 β€ mi β€ 100), time to start crossing the bridge ai, time to finish crossing bi (0 β€ ai, bi <231) If the bridge does not break, output "OK", and if it breaks, output "NG". If the total weight of passers-by on the bridge exceeds 150 [kg], the bridge will be destroyed. Also, at the time of ai, the passersby are on the bridge, but at the time of bi, they are not on the bridge.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
m1 a1 b1
m2 a2 b2
::
mn an ββbn
The number of datasets does not exceed 1200.
Output
For each input dataset, prints on one line whether the bridge will break or not.
Example
Input
3
80 0 30
50 5 25
90 27 50
3
80 0 30
70 5 25
71 30 50
0
Output
NG
OK
Submitted Solution:
```
# AOJ 0231: Dangerous Bridge
# Python3 2018.6.25 bal4u
# ζιζι γ½γΌγ
while True:
n = int(input())
if n == 0: break
tbl = []
for i in range(n):
m, a, b = map(int, input().split())
tbl.append((a, m))
tbl.append((b, -m))
tbl.sort()
sum, t, s = 0, -1, 0
for i in range(len(tbl)):
if t != tbl[i][0]:
if (sum + s) > 150: break
sum += s
t, s = tbl[i][0], tbl[i][1]
else: s += tbl[i][1]
print("OK" if sum+s <= 150 else "NG")
``` | instruction | 0 | 89,166 | 1 | 178,332 |
Yes | output | 1 | 89,166 | 1 | 178,333 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In Aizuwakamatsu Village, which is located far north of Aizuwakamatsu City, a bridge called "Yabashi" is the only way to move to the surrounding villages. Despite the large number of passers-by, the bridge is so old that it is almost broken.
<image>
Yabashi is strong enough to withstand up to 150 [kg]. For example, 80 [kg] people and 50 [kg] people can cross at the same time, but if 90 [kg] people start crossing while 80 [kg] people are crossing, Yabashi will It will break.
If the Ya Bridge is broken, the people of Aizu Komatsu Village will lose the means to move to the surrounding villages. So, as the only programmer in the village, you decided to write a program to determine if the bridge would break based on how you crossed the bridge, in order to protect the lives of the villagers.
Number of passersby crossing the bridge n (1 β€ n β€ 100), weight of each passer mi (1 β€ mi β€ 100), time to start crossing the bridge ai, time to finish crossing bi (0 β€ ai, bi <231) If the bridge does not break, output "OK", and if it breaks, output "NG". If the total weight of passers-by on the bridge exceeds 150 [kg], the bridge will be destroyed. Also, at the time of ai, the passersby are on the bridge, but at the time of bi, they are not on the bridge.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
m1 a1 b1
m2 a2 b2
::
mn an ββbn
The number of datasets does not exceed 1200.
Output
For each input dataset, prints on one line whether the bridge will break or not.
Example
Input
3
80 0 30
50 5 25
90 27 50
3
80 0 30
70 5 25
71 30 50
0
Output
NG
OK
Submitted Solution:
```
while True:
num = int(input())
if num == 0:
break
L = []
start = set()
for _ in range(num):
w, a, b = [int(x) for x in input().split()]
L.append([w,a,b])
start.add(a)
f = 0
for t in sorted(start):
wt = 0
for l in L:
if t >= l[1] and t < l[2]:
wt += l[0]
if wt > 150:
f += 1
if f > 0:
print("NG")
else:
print("OK")
``` | instruction | 0 | 89,167 | 1 | 178,334 |
Yes | output | 1 | 89,167 | 1 | 178,335 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In Aizuwakamatsu Village, which is located far north of Aizuwakamatsu City, a bridge called "Yabashi" is the only way to move to the surrounding villages. Despite the large number of passers-by, the bridge is so old that it is almost broken.
<image>
Yabashi is strong enough to withstand up to 150 [kg]. For example, 80 [kg] people and 50 [kg] people can cross at the same time, but if 90 [kg] people start crossing while 80 [kg] people are crossing, Yabashi will It will break.
If the Ya Bridge is broken, the people of Aizu Komatsu Village will lose the means to move to the surrounding villages. So, as the only programmer in the village, you decided to write a program to determine if the bridge would break based on how you crossed the bridge, in order to protect the lives of the villagers.
Number of passersby crossing the bridge n (1 β€ n β€ 100), weight of each passer mi (1 β€ mi β€ 100), time to start crossing the bridge ai, time to finish crossing bi (0 β€ ai, bi <231) If the bridge does not break, output "OK", and if it breaks, output "NG". If the total weight of passers-by on the bridge exceeds 150 [kg], the bridge will be destroyed. Also, at the time of ai, the passersby are on the bridge, but at the time of bi, they are not on the bridge.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
m1 a1 b1
m2 a2 b2
::
mn an ββbn
The number of datasets does not exceed 1200.
Output
For each input dataset, prints on one line whether the bridge will break or not.
Example
Input
3
80 0 30
50 5 25
90 27 50
3
80 0 30
70 5 25
71 30 50
0
Output
NG
OK
Submitted Solution:
```
while True:
n = int(input())
if n == 0:
break
tlst = []
qlst = []
for _ in range(n):
m, a, b = map(int, input().split())
qlst.append((m, a, b))
tlst.append(a)
tlst.append(b)
tlst.append(b - 1)
tlst = sorted(list(set(tlst)))
tlst.sort()
tdic = {}
for i, t in enumerate(tlst):
tdic[t] = i
lent = len(tlst)
mp = [0] * lent
for m, a, b in qlst:
a, b = tdic[a], tdic[b]
mp[a] += m
mp[b] -= m
acc = 0
for i in range(lent):
acc += mp[i]
if acc > 150:
print("NG")
break
else:
print("OK")
``` | instruction | 0 | 89,168 | 1 | 178,336 |
Yes | output | 1 | 89,168 | 1 | 178,337 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In Aizuwakamatsu Village, which is located far north of Aizuwakamatsu City, a bridge called "Yabashi" is the only way to move to the surrounding villages. Despite the large number of passers-by, the bridge is so old that it is almost broken.
<image>
Yabashi is strong enough to withstand up to 150 [kg]. For example, 80 [kg] people and 50 [kg] people can cross at the same time, but if 90 [kg] people start crossing while 80 [kg] people are crossing, Yabashi will It will break.
If the Ya Bridge is broken, the people of Aizu Komatsu Village will lose the means to move to the surrounding villages. So, as the only programmer in the village, you decided to write a program to determine if the bridge would break based on how you crossed the bridge, in order to protect the lives of the villagers.
Number of passersby crossing the bridge n (1 β€ n β€ 100), weight of each passer mi (1 β€ mi β€ 100), time to start crossing the bridge ai, time to finish crossing bi (0 β€ ai, bi <231) If the bridge does not break, output "OK", and if it breaks, output "NG". If the total weight of passers-by on the bridge exceeds 150 [kg], the bridge will be destroyed. Also, at the time of ai, the passersby are on the bridge, but at the time of bi, they are not on the bridge.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
m1 a1 b1
m2 a2 b2
::
mn an ββbn
The number of datasets does not exceed 1200.
Output
For each input dataset, prints on one line whether the bridge will break or not.
Example
Input
3
80 0 30
50 5 25
90 27 50
3
80 0 30
70 5 25
71 30 50
0
Output
NG
OK
Submitted Solution:
```
while True:
n = int(input())
if n == 0:
break
v = []
for _ in range(n):
m, a, b = map(int, input().split())
v.append((a,m))
v.append((b,-m))
v.sort()
s = 0
ans = 'OK'
for x in v:
s += x[1]
if s > 150:
ans = 'NG'
print(ans)
``` | instruction | 0 | 89,169 | 1 | 178,338 |
Yes | output | 1 | 89,169 | 1 | 178,339 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In Aizuwakamatsu Village, which is located far north of Aizuwakamatsu City, a bridge called "Yabashi" is the only way to move to the surrounding villages. Despite the large number of passers-by, the bridge is so old that it is almost broken.
<image>
Yabashi is strong enough to withstand up to 150 [kg]. For example, 80 [kg] people and 50 [kg] people can cross at the same time, but if 90 [kg] people start crossing while 80 [kg] people are crossing, Yabashi will It will break.
If the Ya Bridge is broken, the people of Aizu Komatsu Village will lose the means to move to the surrounding villages. So, as the only programmer in the village, you decided to write a program to determine if the bridge would break based on how you crossed the bridge, in order to protect the lives of the villagers.
Number of passersby crossing the bridge n (1 β€ n β€ 100), weight of each passer mi (1 β€ mi β€ 100), time to start crossing the bridge ai, time to finish crossing bi (0 β€ ai, bi <231) If the bridge does not break, output "OK", and if it breaks, output "NG". If the total weight of passers-by on the bridge exceeds 150 [kg], the bridge will be destroyed. Also, at the time of ai, the passersby are on the bridge, but at the time of bi, they are not on the bridge.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
m1 a1 b1
m2 a2 b2
::
mn an ββbn
The number of datasets does not exceed 1200.
Output
For each input dataset, prints on one line whether the bridge will break or not.
Example
Input
3
80 0 30
50 5 25
90 27 50
3
80 0 30
70 5 25
71 30 50
0
Output
NG
OK
Submitted Solution:
```
while True:
N = int(input())
if not N:
break
e = []
for i in range(N):
m, a, b = map(int, input().split())
e.append((a, m))
e.append((b, -m))
M = 0
ot = -1
for t, m in sorted(e):
M += m
if M >= 150 and ot != t:
print('NG')
break
ot = t
else:
print('OK')
``` | instruction | 0 | 89,170 | 1 | 178,340 |
No | output | 1 | 89,170 | 1 | 178,341 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In Aizuwakamatsu Village, which is located far north of Aizuwakamatsu City, a bridge called "Yabashi" is the only way to move to the surrounding villages. Despite the large number of passers-by, the bridge is so old that it is almost broken.
<image>
Yabashi is strong enough to withstand up to 150 [kg]. For example, 80 [kg] people and 50 [kg] people can cross at the same time, but if 90 [kg] people start crossing while 80 [kg] people are crossing, Yabashi will It will break.
If the Ya Bridge is broken, the people of Aizu Komatsu Village will lose the means to move to the surrounding villages. So, as the only programmer in the village, you decided to write a program to determine if the bridge would break based on how you crossed the bridge, in order to protect the lives of the villagers.
Number of passersby crossing the bridge n (1 β€ n β€ 100), weight of each passer mi (1 β€ mi β€ 100), time to start crossing the bridge ai, time to finish crossing bi (0 β€ ai, bi <231) If the bridge does not break, output "OK", and if it breaks, output "NG". If the total weight of passers-by on the bridge exceeds 150 [kg], the bridge will be destroyed. Also, at the time of ai, the passersby are on the bridge, but at the time of bi, they are not on the bridge.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
m1 a1 b1
m2 a2 b2
::
mn an ββbn
The number of datasets does not exceed 1200.
Output
For each input dataset, prints on one line whether the bridge will break or not.
Example
Input
3
80 0 30
50 5 25
90 27 50
3
80 0 30
70 5 25
71 30 50
0
Output
NG
OK
Submitted Solution:
```
while True:
N = int(input())
if not N:
break
e = []
for i in range(N):
m, a, b = map(int, input().split())
e.append((a, m))
e.append((b, -m))
M = 0
ot = -1
for t, m in sorted(e):
M += m
if M > 150 and ot != t:
print('NG')
break
ot = t
else:
print('OK')
``` | instruction | 0 | 89,171 | 1 | 178,342 |
No | output | 1 | 89,171 | 1 | 178,343 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In Aizuwakamatsu Village, which is located far north of Aizuwakamatsu City, a bridge called "Yabashi" is the only way to move to the surrounding villages. Despite the large number of passers-by, the bridge is so old that it is almost broken.
<image>
Yabashi is strong enough to withstand up to 150 [kg]. For example, 80 [kg] people and 50 [kg] people can cross at the same time, but if 90 [kg] people start crossing while 80 [kg] people are crossing, Yabashi will It will break.
If the Ya Bridge is broken, the people of Aizu Komatsu Village will lose the means to move to the surrounding villages. So, as the only programmer in the village, you decided to write a program to determine if the bridge would break based on how you crossed the bridge, in order to protect the lives of the villagers.
Number of passersby crossing the bridge n (1 β€ n β€ 100), weight of each passer mi (1 β€ mi β€ 100), time to start crossing the bridge ai, time to finish crossing bi (0 β€ ai, bi <231) If the bridge does not break, output "OK", and if it breaks, output "NG". If the total weight of passers-by on the bridge exceeds 150 [kg], the bridge will be destroyed. Also, at the time of ai, the passersby are on the bridge, but at the time of bi, they are not on the bridge.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
m1 a1 b1
m2 a2 b2
::
mn an ββbn
The number of datasets does not exceed 1200.
Output
For each input dataset, prints on one line whether the bridge will break or not.
Example
Input
3
80 0 30
50 5 25
90 27 50
3
80 0 30
70 5 25
71 30 50
0
Output
NG
OK
Submitted Solution:
```
from operator import itemgetter
while 1:
n = int(input())
if n == 0:
break
t_w = []
for _ in range(n):
m, a, b = map(int, input().split())
t_w.append((a, m, 5))
t_w.append((b, -m, 3))
t_w.sort(key=itemgetter(0, 2))
ans, cur = 'OK', 0
for t, w, pri in t_w:
cur += w
# print('t', t, 'w', w, 'cur', cur)
if cur > 150:
ans = 'NG'
break
print(ans)
``` | instruction | 0 | 89,172 | 1 | 178,344 |
No | output | 1 | 89,172 | 1 | 178,345 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In Aizuwakamatsu Village, which is located far north of Aizuwakamatsu City, a bridge called "Yabashi" is the only way to move to the surrounding villages. Despite the large number of passers-by, the bridge is so old that it is almost broken.
<image>
Yabashi is strong enough to withstand up to 150 [kg]. For example, 80 [kg] people and 50 [kg] people can cross at the same time, but if 90 [kg] people start crossing while 80 [kg] people are crossing, Yabashi will It will break.
If the Ya Bridge is broken, the people of Aizu Komatsu Village will lose the means to move to the surrounding villages. So, as the only programmer in the village, you decided to write a program to determine if the bridge would break based on how you crossed the bridge, in order to protect the lives of the villagers.
Number of passersby crossing the bridge n (1 β€ n β€ 100), weight of each passer mi (1 β€ mi β€ 100), time to start crossing the bridge ai, time to finish crossing bi (0 β€ ai, bi <231) If the bridge does not break, output "OK", and if it breaks, output "NG". If the total weight of passers-by on the bridge exceeds 150 [kg], the bridge will be destroyed. Also, at the time of ai, the passersby are on the bridge, but at the time of bi, they are not on the bridge.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
m1 a1 b1
m2 a2 b2
::
mn an ββbn
The number of datasets does not exceed 1200.
Output
For each input dataset, prints on one line whether the bridge will break or not.
Example
Input
3
80 0 30
50 5 25
90 27 50
3
80 0 30
70 5 25
71 30 50
0
Output
NG
OK
Submitted Solution:
```
while 1:
n = int(input())
if n == 0:
break
t_w = []
for _ in range(n):
m, a, b = map(int, input().split())
t_w.append((b, -m))
t_w.append((a, m))
t_w.sort(key=lambda x: x[0])
ans, cur = 'OK', 0
for t, w in t_w:
cur += w
# print('t', t, 'w', w, 'cur', cur)
if cur > 150:
ans = 'NG'
break
print(ans)
``` | instruction | 0 | 89,173 | 1 | 178,346 |
No | output | 1 | 89,173 | 1 | 178,347 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled.
Alice received a set of Toy Trainβ’ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station i is station i+1 if 1 β€ i < n or station 1 if i = n. It takes the train 1 second to travel to its next station as described.
Bob gave Alice a fun task before he left: to deliver m candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 1 through m. Candy i (1 β€ i β€ m), now at station a_i, should be delivered to station b_i (a_i β b_i).
<image> The blue numbers on the candies correspond to b_i values. The image corresponds to the 1-st example.
The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible.
Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there.
Input
The first line contains two space-separated integers n and m (2 β€ n β€ 100; 1 β€ m β€ 200) β the number of stations and the number of candies, respectively.
The i-th of the following m lines contains two space-separated integers a_i and b_i (1 β€ a_i, b_i β€ n; a_i β b_i) β the station that initially contains candy i and the destination station of the candy, respectively.
Output
In the first and only line, print n space-separated integers, the i-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station i.
Examples
Input
5 7
2 4
5 1
2 3
3 4
4 1
5 3
3 5
Output
10 9 10 10 9
Input
2 3
1 2
1 2
1 2
Output
5 6
Note
Consider the second sample.
If the train started at station 1, the optimal strategy is as follows.
1. Load the first candy onto the train.
2. Proceed to station 2. This step takes 1 second.
3. Deliver the first candy.
4. Proceed to station 1. This step takes 1 second.
5. Load the second candy onto the train.
6. Proceed to station 2. This step takes 1 second.
7. Deliver the second candy.
8. Proceed to station 1. This step takes 1 second.
9. Load the third candy onto the train.
10. Proceed to station 2. This step takes 1 second.
11. Deliver the third candy.
Hence, the train needs 5 seconds to complete the tasks.
If the train were to start at station 2, however, it would need to move to station 1 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1 = 6 seconds. | instruction | 0 | 89,316 | 1 | 178,632 |
Tags: brute force, greedy
Correct Solution:
```
from collections import defaultdict
def dist(a, b, n):
if b >= a:
return b - a
else:
return n - a + b
n, m = (int(x) for x in input().split())
stations = defaultdict(list)
for _ in range(m):
a, b = (int(x) for x in input().split())
stations[a].append(b)
needs = {}
for station, candies in stations.items():
if not candies:
continue
loops = len(candies)-1
closest = min(candies, key=lambda x:dist(station, x, n))
needs[station] = (loops, closest)
maxloops = max([pair[0] for pair in needs.values()])
maxstats = []
finishes = []
for station, pair in needs.items():
if pair[0] == maxloops:
maxstats.append(station)
result = []
for start in range(1, n+1):
time = 0
for station in needs:
if not needs[station]:
continue
loops, closest = needs[station]
t = dist(start, station, n) + loops*n + dist(station, closest, n)
time = max(time, t)
result.append(str(time))
print(' '.join(result))
``` | output | 1 | 89,316 | 1 | 178,633 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled.
Alice received a set of Toy Trainβ’ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station i is station i+1 if 1 β€ i < n or station 1 if i = n. It takes the train 1 second to travel to its next station as described.
Bob gave Alice a fun task before he left: to deliver m candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 1 through m. Candy i (1 β€ i β€ m), now at station a_i, should be delivered to station b_i (a_i β b_i).
<image> The blue numbers on the candies correspond to b_i values. The image corresponds to the 1-st example.
The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible.
Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there.
Input
The first line contains two space-separated integers n and m (2 β€ n β€ 100; 1 β€ m β€ 200) β the number of stations and the number of candies, respectively.
The i-th of the following m lines contains two space-separated integers a_i and b_i (1 β€ a_i, b_i β€ n; a_i β b_i) β the station that initially contains candy i and the destination station of the candy, respectively.
Output
In the first and only line, print n space-separated integers, the i-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station i.
Examples
Input
5 7
2 4
5 1
2 3
3 4
4 1
5 3
3 5
Output
10 9 10 10 9
Input
2 3
1 2
1 2
1 2
Output
5 6
Note
Consider the second sample.
If the train started at station 1, the optimal strategy is as follows.
1. Load the first candy onto the train.
2. Proceed to station 2. This step takes 1 second.
3. Deliver the first candy.
4. Proceed to station 1. This step takes 1 second.
5. Load the second candy onto the train.
6. Proceed to station 2. This step takes 1 second.
7. Deliver the second candy.
8. Proceed to station 1. This step takes 1 second.
9. Load the third candy onto the train.
10. Proceed to station 2. This step takes 1 second.
11. Deliver the third candy.
Hence, the train needs 5 seconds to complete the tasks.
If the train were to start at station 2, however, it would need to move to station 1 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1 = 6 seconds. | instruction | 0 | 89,317 | 1 | 178,634 |
Tags: brute force, greedy
Correct Solution:
```
#Code by Sounak, IIESTS
#------------------------------warmup----------------------------
import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
#-------------------game starts now-----------------------------------------------------
mii=lambda:map(int,input().split())
n,m=mii()
a=[0 for _ in range(n)]
c=[123456 for _ in range(n)]
for _ in range(m):
u,v=mii()
u%=n
v%=n
if v<u: v+=n
a[u]+=1
if c[u]>v: c[u]=v
ans=[]
for i in list(range(1,n))+[0]:
out=0
for j in range(i,n):
if not a[j]: continue
tmp=(j-i)+(a[j]-1)*n+(c[j]-j)
out=max(out,tmp)
#print(1,i,j,tmp)
for j in range(i):
if not a[j]: continue
tmp=(j+n-i)+(a[j]-1)*n+(c[j]-j)
out=max(out,tmp)
#print(2,i,j,tmp)
ans.append(out)
print(" ".join(map(str,ans)))
``` | output | 1 | 89,317 | 1 | 178,635 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled.
Alice received a set of Toy Trainβ’ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station i is station i+1 if 1 β€ i < n or station 1 if i = n. It takes the train 1 second to travel to its next station as described.
Bob gave Alice a fun task before he left: to deliver m candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 1 through m. Candy i (1 β€ i β€ m), now at station a_i, should be delivered to station b_i (a_i β b_i).
<image> The blue numbers on the candies correspond to b_i values. The image corresponds to the 1-st example.
The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible.
Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there.
Input
The first line contains two space-separated integers n and m (2 β€ n β€ 100; 1 β€ m β€ 200) β the number of stations and the number of candies, respectively.
The i-th of the following m lines contains two space-separated integers a_i and b_i (1 β€ a_i, b_i β€ n; a_i β b_i) β the station that initially contains candy i and the destination station of the candy, respectively.
Output
In the first and only line, print n space-separated integers, the i-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station i.
Examples
Input
5 7
2 4
5 1
2 3
3 4
4 1
5 3
3 5
Output
10 9 10 10 9
Input
2 3
1 2
1 2
1 2
Output
5 6
Note
Consider the second sample.
If the train started at station 1, the optimal strategy is as follows.
1. Load the first candy onto the train.
2. Proceed to station 2. This step takes 1 second.
3. Deliver the first candy.
4. Proceed to station 1. This step takes 1 second.
5. Load the second candy onto the train.
6. Proceed to station 2. This step takes 1 second.
7. Deliver the second candy.
8. Proceed to station 1. This step takes 1 second.
9. Load the third candy onto the train.
10. Proceed to station 2. This step takes 1 second.
11. Deliver the third candy.
Hence, the train needs 5 seconds to complete the tasks.
If the train were to start at station 2, however, it would need to move to station 1 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1 = 6 seconds. | instruction | 0 | 89,318 | 1 | 178,636 |
Tags: brute force, greedy
Correct Solution:
```
def dist(a,b):
return (b-a)%n
n, m = map(int, input().split())
cnd = [0 for x in range(n+1)]
mn = [5000 for x in range(n+1)]
for i in range(m):
a, b = map(int, input().split())
# print(a,b,dist(a,b))
cnd[a] += 1
mn[a] = min(mn[a], dist(a,b))
# print(mn)
# print(cnd)
for i in range(1,n+1):
ans = 0
for j in range(1,n+1):
if cnd[j] > 0:
ans = max(ans, dist(i,j) + n*(cnd[j]-1) + mn[j])
print(ans, end=" ")
``` | output | 1 | 89,318 | 1 | 178,637 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled.
Alice received a set of Toy Trainβ’ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station i is station i+1 if 1 β€ i < n or station 1 if i = n. It takes the train 1 second to travel to its next station as described.
Bob gave Alice a fun task before he left: to deliver m candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 1 through m. Candy i (1 β€ i β€ m), now at station a_i, should be delivered to station b_i (a_i β b_i).
<image> The blue numbers on the candies correspond to b_i values. The image corresponds to the 1-st example.
The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible.
Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there.
Input
The first line contains two space-separated integers n and m (2 β€ n β€ 100; 1 β€ m β€ 200) β the number of stations and the number of candies, respectively.
The i-th of the following m lines contains two space-separated integers a_i and b_i (1 β€ a_i, b_i β€ n; a_i β b_i) β the station that initially contains candy i and the destination station of the candy, respectively.
Output
In the first and only line, print n space-separated integers, the i-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station i.
Examples
Input
5 7
2 4
5 1
2 3
3 4
4 1
5 3
3 5
Output
10 9 10 10 9
Input
2 3
1 2
1 2
1 2
Output
5 6
Note
Consider the second sample.
If the train started at station 1, the optimal strategy is as follows.
1. Load the first candy onto the train.
2. Proceed to station 2. This step takes 1 second.
3. Deliver the first candy.
4. Proceed to station 1. This step takes 1 second.
5. Load the second candy onto the train.
6. Proceed to station 2. This step takes 1 second.
7. Deliver the second candy.
8. Proceed to station 1. This step takes 1 second.
9. Load the third candy onto the train.
10. Proceed to station 2. This step takes 1 second.
11. Deliver the third candy.
Hence, the train needs 5 seconds to complete the tasks.
If the train were to start at station 2, however, it would need to move to station 1 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1 = 6 seconds. | instruction | 0 | 89,319 | 1 | 178,638 |
Tags: brute force, greedy
Correct Solution:
```
#Code by Sounak, IIESTS
#------------------------------warmup----------------------------
import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
from collections import defaultdict
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
#-------------------game starts now-----------------------------------------------------
def dist(a,b):
return (b-a)%n
n, m = map(int, input().split())
cnd = [0 for x in range(n+1)]
mn = [5000 for x in range(n+1)]
for i in range(m):
a, b = map(int, input().split())
# print(a,b,dist(a,b))
cnd[a] += 1
mn[a] = min(mn[a], dist(a,b))
# print(mn)
# print(cnd)
for i in range(1,n+1):
ans = 0
for j in range(1,n+1):
if cnd[j] > 0:
ans = max(ans, dist(i,j) + n*(cnd[j]-1) + mn[j])
print(ans, end=" ")
``` | output | 1 | 89,319 | 1 | 178,639 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled.
Alice received a set of Toy Trainβ’ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station i is station i+1 if 1 β€ i < n or station 1 if i = n. It takes the train 1 second to travel to its next station as described.
Bob gave Alice a fun task before he left: to deliver m candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 1 through m. Candy i (1 β€ i β€ m), now at station a_i, should be delivered to station b_i (a_i β b_i).
<image> The blue numbers on the candies correspond to b_i values. The image corresponds to the 1-st example.
The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible.
Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there.
Input
The first line contains two space-separated integers n and m (2 β€ n β€ 100; 1 β€ m β€ 200) β the number of stations and the number of candies, respectively.
The i-th of the following m lines contains two space-separated integers a_i and b_i (1 β€ a_i, b_i β€ n; a_i β b_i) β the station that initially contains candy i and the destination station of the candy, respectively.
Output
In the first and only line, print n space-separated integers, the i-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station i.
Examples
Input
5 7
2 4
5 1
2 3
3 4
4 1
5 3
3 5
Output
10 9 10 10 9
Input
2 3
1 2
1 2
1 2
Output
5 6
Note
Consider the second sample.
If the train started at station 1, the optimal strategy is as follows.
1. Load the first candy onto the train.
2. Proceed to station 2. This step takes 1 second.
3. Deliver the first candy.
4. Proceed to station 1. This step takes 1 second.
5. Load the second candy onto the train.
6. Proceed to station 2. This step takes 1 second.
7. Deliver the second candy.
8. Proceed to station 1. This step takes 1 second.
9. Load the third candy onto the train.
10. Proceed to station 2. This step takes 1 second.
11. Deliver the third candy.
Hence, the train needs 5 seconds to complete the tasks.
If the train were to start at station 2, however, it would need to move to station 1 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1 = 6 seconds. | instruction | 0 | 89,320 | 1 | 178,640 |
Tags: brute force, greedy
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
from collections import defaultdict, deque, Counter, OrderedDict
import threading
from copy import deepcopy
def main():
n,m = map(int,input().split())
station = [[] for _ in range(n+1)]
time = [0]*(n+1)
ans = [0]*(n+1)
for i in range(m):
a,b = map(int,input().split())
station[a].append((b+n-a)%n)
for i in range(1,n+1):
station[i] = sorted(station[i])
for i in range(1,n+1):
if len(station[i]):
time[i] = (len(station[i])-1)*n + station[i][0]
for i in range(1,n+1):
for j in range(1,n+1):
if time[j] != 0: ans[i] = max(ans[i],time[j]+(j+n-i)%n)
print(*ans[1::])
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
"""threading.stack_size(40960000)
thread = threading.Thread(target=main)
thread.start()"""
main()
``` | output | 1 | 89,320 | 1 | 178,641 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled.
Alice received a set of Toy Trainβ’ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station i is station i+1 if 1 β€ i < n or station 1 if i = n. It takes the train 1 second to travel to its next station as described.
Bob gave Alice a fun task before he left: to deliver m candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 1 through m. Candy i (1 β€ i β€ m), now at station a_i, should be delivered to station b_i (a_i β b_i).
<image> The blue numbers on the candies correspond to b_i values. The image corresponds to the 1-st example.
The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible.
Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there.
Input
The first line contains two space-separated integers n and m (2 β€ n β€ 100; 1 β€ m β€ 200) β the number of stations and the number of candies, respectively.
The i-th of the following m lines contains two space-separated integers a_i and b_i (1 β€ a_i, b_i β€ n; a_i β b_i) β the station that initially contains candy i and the destination station of the candy, respectively.
Output
In the first and only line, print n space-separated integers, the i-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station i.
Examples
Input
5 7
2 4
5 1
2 3
3 4
4 1
5 3
3 5
Output
10 9 10 10 9
Input
2 3
1 2
1 2
1 2
Output
5 6
Note
Consider the second sample.
If the train started at station 1, the optimal strategy is as follows.
1. Load the first candy onto the train.
2. Proceed to station 2. This step takes 1 second.
3. Deliver the first candy.
4. Proceed to station 1. This step takes 1 second.
5. Load the second candy onto the train.
6. Proceed to station 2. This step takes 1 second.
7. Deliver the second candy.
8. Proceed to station 1. This step takes 1 second.
9. Load the third candy onto the train.
10. Proceed to station 2. This step takes 1 second.
11. Deliver the third candy.
Hence, the train needs 5 seconds to complete the tasks.
If the train were to start at station 2, however, it would need to move to station 1 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1 = 6 seconds. | instruction | 0 | 89,321 | 1 | 178,642 |
Tags: brute force, greedy
Correct Solution:
```
if __name__ == "__main__":
from sys import stdin
n, m = list(map(int, stdin.readline().split()))
c = {}
for _ in range(m):
a, b = list(map(int, stdin.readline().split()))
if (a-1) not in c.keys():
c[a-1] = []
x = b-a + (n if b<a else 0)
c[a-1].append(x)
for k, l in c.items():
c[k] = min(l) + ((len(l)-1)*n)
toprint = []
for x in range(n):
res = 0
for y, v in c.items():
s = y-x + (n if y<x else 0)
res = max(res, v+s)
toprint.append(res)
print(*toprint)
``` | output | 1 | 89,321 | 1 | 178,643 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled.
Alice received a set of Toy Trainβ’ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station i is station i+1 if 1 β€ i < n or station 1 if i = n. It takes the train 1 second to travel to its next station as described.
Bob gave Alice a fun task before he left: to deliver m candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 1 through m. Candy i (1 β€ i β€ m), now at station a_i, should be delivered to station b_i (a_i β b_i).
<image> The blue numbers on the candies correspond to b_i values. The image corresponds to the 1-st example.
The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible.
Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there.
Input
The first line contains two space-separated integers n and m (2 β€ n β€ 100; 1 β€ m β€ 200) β the number of stations and the number of candies, respectively.
The i-th of the following m lines contains two space-separated integers a_i and b_i (1 β€ a_i, b_i β€ n; a_i β b_i) β the station that initially contains candy i and the destination station of the candy, respectively.
Output
In the first and only line, print n space-separated integers, the i-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station i.
Examples
Input
5 7
2 4
5 1
2 3
3 4
4 1
5 3
3 5
Output
10 9 10 10 9
Input
2 3
1 2
1 2
1 2
Output
5 6
Note
Consider the second sample.
If the train started at station 1, the optimal strategy is as follows.
1. Load the first candy onto the train.
2. Proceed to station 2. This step takes 1 second.
3. Deliver the first candy.
4. Proceed to station 1. This step takes 1 second.
5. Load the second candy onto the train.
6. Proceed to station 2. This step takes 1 second.
7. Deliver the second candy.
8. Proceed to station 1. This step takes 1 second.
9. Load the third candy onto the train.
10. Proceed to station 2. This step takes 1 second.
11. Deliver the third candy.
Hence, the train needs 5 seconds to complete the tasks.
If the train were to start at station 2, however, it would need to move to station 1 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1 = 6 seconds. | instruction | 0 | 89,322 | 1 | 178,644 |
Tags: brute force, greedy
Correct Solution:
```
import sys
n, m = (int(t) for t in input().split(' '))
candies_total = [0 for _ in range(n)]
closest_distance = [n for _ in range(n)]
candies = sys.stdin.readlines()
for i in range(m):
from_, to = (int(t) - 1 for t in candies[i].split(' '))
candies_total[from_] += 1
distance = (to + n - from_) % n
if distance < closest_distance[from_]:
closest_distance[from_] = distance
stations_best = [0 for _ in range(n)]
for i in range(n):
stations_best[i] = (candies_total[i] - 1) * n + closest_distance[i]
answer = []
for i in range(n):
cost = max([stations_best[j] + (j + n - i) % n for j in range(n) if stations_best[j] > 0])
answer.append(cost)
print(*answer)
``` | output | 1 | 89,322 | 1 | 178,645 |
Provide tags and a correct Python 3 solution for this coding contest problem.
This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled.
Alice received a set of Toy Trainβ’ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station i is station i+1 if 1 β€ i < n or station 1 if i = n. It takes the train 1 second to travel to its next station as described.
Bob gave Alice a fun task before he left: to deliver m candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 1 through m. Candy i (1 β€ i β€ m), now at station a_i, should be delivered to station b_i (a_i β b_i).
<image> The blue numbers on the candies correspond to b_i values. The image corresponds to the 1-st example.
The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible.
Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there.
Input
The first line contains two space-separated integers n and m (2 β€ n β€ 100; 1 β€ m β€ 200) β the number of stations and the number of candies, respectively.
The i-th of the following m lines contains two space-separated integers a_i and b_i (1 β€ a_i, b_i β€ n; a_i β b_i) β the station that initially contains candy i and the destination station of the candy, respectively.
Output
In the first and only line, print n space-separated integers, the i-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station i.
Examples
Input
5 7
2 4
5 1
2 3
3 4
4 1
5 3
3 5
Output
10 9 10 10 9
Input
2 3
1 2
1 2
1 2
Output
5 6
Note
Consider the second sample.
If the train started at station 1, the optimal strategy is as follows.
1. Load the first candy onto the train.
2. Proceed to station 2. This step takes 1 second.
3. Deliver the first candy.
4. Proceed to station 1. This step takes 1 second.
5. Load the second candy onto the train.
6. Proceed to station 2. This step takes 1 second.
7. Deliver the second candy.
8. Proceed to station 1. This step takes 1 second.
9. Load the third candy onto the train.
10. Proceed to station 2. This step takes 1 second.
11. Deliver the third candy.
Hence, the train needs 5 seconds to complete the tasks.
If the train were to start at station 2, however, it would need to move to station 1 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1 = 6 seconds. | instruction | 0 | 89,323 | 1 | 178,646 |
Tags: brute force, greedy
Correct Solution:
```
def dist(a, b):
return (b - a) % n
n, m = list(map(int, input().split(" ")))
sweets = {i: [] for i in range(n)}
for i in range(m):
s, t = list(map(int, input().split(" ")))
sweets[s - 1].append(t - 1)
t = {i: 0 for i in range(n)}
for i in range(n):
sweets[i] = sorted(sweets[i], key=lambda x: -dist(i, x))
if len(sweets[i]):
t[i] = (len(sweets[i]) - 1) * n + dist(i, sweets[i][-1])
result = []
for s in range(n):
max_dist = 0
for i in range(n):
if t[i] and t[i] + dist(s, i) > max_dist:
max_dist = t[i] + dist(s, i)
result.append(max_dist)
print(" ".join(map(str, result)))
``` | output | 1 | 89,323 | 1 | 178,647 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled.
Alice received a set of Toy Trainβ’ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station i is station i+1 if 1 β€ i < n or station 1 if i = n. It takes the train 1 second to travel to its next station as described.
Bob gave Alice a fun task before he left: to deliver m candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 1 through m. Candy i (1 β€ i β€ m), now at station a_i, should be delivered to station b_i (a_i β b_i).
<image> The blue numbers on the candies correspond to b_i values. The image corresponds to the 1-st example.
The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible.
Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there.
Input
The first line contains two space-separated integers n and m (2 β€ n β€ 100; 1 β€ m β€ 200) β the number of stations and the number of candies, respectively.
The i-th of the following m lines contains two space-separated integers a_i and b_i (1 β€ a_i, b_i β€ n; a_i β b_i) β the station that initially contains candy i and the destination station of the candy, respectively.
Output
In the first and only line, print n space-separated integers, the i-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station i.
Examples
Input
5 7
2 4
5 1
2 3
3 4
4 1
5 3
3 5
Output
10 9 10 10 9
Input
2 3
1 2
1 2
1 2
Output
5 6
Note
Consider the second sample.
If the train started at station 1, the optimal strategy is as follows.
1. Load the first candy onto the train.
2. Proceed to station 2. This step takes 1 second.
3. Deliver the first candy.
4. Proceed to station 1. This step takes 1 second.
5. Load the second candy onto the train.
6. Proceed to station 2. This step takes 1 second.
7. Deliver the second candy.
8. Proceed to station 1. This step takes 1 second.
9. Load the third candy onto the train.
10. Proceed to station 2. This step takes 1 second.
11. Deliver the third candy.
Hence, the train needs 5 seconds to complete the tasks.
If the train were to start at station 2, however, it would need to move to station 1 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1 = 6 seconds.
Submitted Solution:
```
n, m = map(int, input().split())
def dist(a, b):
return (n + b - a) % n
def main():
inp1 = [0] * (n + 1)
inp2 = [n] * (n + 1)
for _ in range(m):
a, b = map(int, input().split())
inp1[a] += 1
inp2[a] = min(inp2[a], dist(a, b))
inp = tuple((((r1 - 1) * n + r2) for r1, r2 in zip(inp1, inp2)))
print(*(max((dist(i, j) + inp[j] for j in range(1, n + 1) if inp[j])) for i in range(1, n + 1)))
main()
``` | instruction | 0 | 89,324 | 1 | 178,648 |
Yes | output | 1 | 89,324 | 1 | 178,649 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled.
Alice received a set of Toy Trainβ’ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station i is station i+1 if 1 β€ i < n or station 1 if i = n. It takes the train 1 second to travel to its next station as described.
Bob gave Alice a fun task before he left: to deliver m candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 1 through m. Candy i (1 β€ i β€ m), now at station a_i, should be delivered to station b_i (a_i β b_i).
<image> The blue numbers on the candies correspond to b_i values. The image corresponds to the 1-st example.
The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible.
Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there.
Input
The first line contains two space-separated integers n and m (2 β€ n β€ 100; 1 β€ m β€ 200) β the number of stations and the number of candies, respectively.
The i-th of the following m lines contains two space-separated integers a_i and b_i (1 β€ a_i, b_i β€ n; a_i β b_i) β the station that initially contains candy i and the destination station of the candy, respectively.
Output
In the first and only line, print n space-separated integers, the i-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station i.
Examples
Input
5 7
2 4
5 1
2 3
3 4
4 1
5 3
3 5
Output
10 9 10 10 9
Input
2 3
1 2
1 2
1 2
Output
5 6
Note
Consider the second sample.
If the train started at station 1, the optimal strategy is as follows.
1. Load the first candy onto the train.
2. Proceed to station 2. This step takes 1 second.
3. Deliver the first candy.
4. Proceed to station 1. This step takes 1 second.
5. Load the second candy onto the train.
6. Proceed to station 2. This step takes 1 second.
7. Deliver the second candy.
8. Proceed to station 1. This step takes 1 second.
9. Load the third candy onto the train.
10. Proceed to station 2. This step takes 1 second.
11. Deliver the third candy.
Hence, the train needs 5 seconds to complete the tasks.
If the train were to start at station 2, however, it would need to move to station 1 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1 = 6 seconds.
Submitted Solution:
```
import sys
import math as mt
input=sys.stdin.buffer.readline
#t=int(input())
t=1
for ___ in range(t):
n,m=map(int,input().split())
d={}
for i in range(n+1):
d[i]=[]
for __ in range(m):
a,b=map(int,input().split())
d[a].append(b)
for i in range(n+1):
d[i].sort()
#print(d)
sub=[0]*(n+1)
for i in d:
mini=10000
for j in range(len(d[i])):
mini=min(mini,(d[i][j]-i)%n)
sub[i]=mini
for k in range(1,n+1):
loop=0
maxi=0
for i in range(1,n+1):
if len(d[i])>0:
loop=(len(d[i])-1)*n+(i-k)%n+(sub[i])
maxi=max(maxi,loop)
print(maxi,end=" ")
``` | instruction | 0 | 89,325 | 1 | 178,650 |
Yes | output | 1 | 89,325 | 1 | 178,651 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled.
Alice received a set of Toy Trainβ’ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station i is station i+1 if 1 β€ i < n or station 1 if i = n. It takes the train 1 second to travel to its next station as described.
Bob gave Alice a fun task before he left: to deliver m candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 1 through m. Candy i (1 β€ i β€ m), now at station a_i, should be delivered to station b_i (a_i β b_i).
<image> The blue numbers on the candies correspond to b_i values. The image corresponds to the 1-st example.
The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible.
Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there.
Input
The first line contains two space-separated integers n and m (2 β€ n β€ 100; 1 β€ m β€ 200) β the number of stations and the number of candies, respectively.
The i-th of the following m lines contains two space-separated integers a_i and b_i (1 β€ a_i, b_i β€ n; a_i β b_i) β the station that initially contains candy i and the destination station of the candy, respectively.
Output
In the first and only line, print n space-separated integers, the i-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station i.
Examples
Input
5 7
2 4
5 1
2 3
3 4
4 1
5 3
3 5
Output
10 9 10 10 9
Input
2 3
1 2
1 2
1 2
Output
5 6
Note
Consider the second sample.
If the train started at station 1, the optimal strategy is as follows.
1. Load the first candy onto the train.
2. Proceed to station 2. This step takes 1 second.
3. Deliver the first candy.
4. Proceed to station 1. This step takes 1 second.
5. Load the second candy onto the train.
6. Proceed to station 2. This step takes 1 second.
7. Deliver the second candy.
8. Proceed to station 1. This step takes 1 second.
9. Load the third candy onto the train.
10. Proceed to station 2. This step takes 1 second.
11. Deliver the third candy.
Hence, the train needs 5 seconds to complete the tasks.
If the train were to start at station 2, however, it would need to move to station 1 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1 = 6 seconds.
Submitted Solution:
```
def dista(start, n):
return lambda end: n - (start - end) if(start > end) else end - start
n, m = map(int,input().split(' '))
dicta = [[] for i in range(n+1)]
for i in range(m):
s, d = map(int,input().split(' '))
dicta[s].append(d)
for i in range(n+1):
dicta[i].sort(key = dista(i, n))
maxlen = max((map(len, dicta)))
result = (maxlen-1)*n
minadd = 0
ansans = []
for k in range(1, n+1):
disk = dista(k, n)
minadd = 0
for i in range(1, n+1):
lndicta = len(dicta[i])
tmp = 0
if(lndicta == maxlen-1):
if lndicta != 0:
tmp = min(map(lambda j: (disk(i) + dista(i, n)(j)), dicta[i])) - n
elif(lndicta == maxlen):
tmp = min(map(lambda j: (disk(i) + dista(i, n)(j)), dicta[i]))
if(tmp > minadd):
minadd = tmp
ansans.append(str(minadd + result))
print(' '.join(ansans))
``` | instruction | 0 | 89,326 | 1 | 178,652 |
Yes | output | 1 | 89,326 | 1 | 178,653 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled.
Alice received a set of Toy Trainβ’ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station i is station i+1 if 1 β€ i < n or station 1 if i = n. It takes the train 1 second to travel to its next station as described.
Bob gave Alice a fun task before he left: to deliver m candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 1 through m. Candy i (1 β€ i β€ m), now at station a_i, should be delivered to station b_i (a_i β b_i).
<image> The blue numbers on the candies correspond to b_i values. The image corresponds to the 1-st example.
The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible.
Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there.
Input
The first line contains two space-separated integers n and m (2 β€ n β€ 100; 1 β€ m β€ 200) β the number of stations and the number of candies, respectively.
The i-th of the following m lines contains two space-separated integers a_i and b_i (1 β€ a_i, b_i β€ n; a_i β b_i) β the station that initially contains candy i and the destination station of the candy, respectively.
Output
In the first and only line, print n space-separated integers, the i-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station i.
Examples
Input
5 7
2 4
5 1
2 3
3 4
4 1
5 3
3 5
Output
10 9 10 10 9
Input
2 3
1 2
1 2
1 2
Output
5 6
Note
Consider the second sample.
If the train started at station 1, the optimal strategy is as follows.
1. Load the first candy onto the train.
2. Proceed to station 2. This step takes 1 second.
3. Deliver the first candy.
4. Proceed to station 1. This step takes 1 second.
5. Load the second candy onto the train.
6. Proceed to station 2. This step takes 1 second.
7. Deliver the second candy.
8. Proceed to station 1. This step takes 1 second.
9. Load the third candy onto the train.
10. Proceed to station 2. This step takes 1 second.
11. Deliver the third candy.
Hence, the train needs 5 seconds to complete the tasks.
If the train were to start at station 2, however, it would need to move to station 1 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1 = 6 seconds.
Submitted Solution:
```
import sys
#sys.stdin=open("data.txt")
input=sys.stdin.readline
mii=lambda:map(int,input().split())
n,m=mii()
a=[0 for _ in range(n)]
c=[123456 for _ in range(n)]
for _ in range(m):
u,v=mii()
u%=n
v%=n
if v<u: v+=n
a[u]+=1
if c[u]>v: c[u]=v
ans=[]
for i in list(range(1,n))+[0]:
out=0
for j in range(i,n):
if not a[j]: continue
tmp=(j-i)+(a[j]-1)*n+(c[j]-j)
out=max(out,tmp)
#print(1,i,j,tmp)
for j in range(i):
if not a[j]: continue
tmp=(j+n-i)+(a[j]-1)*n+(c[j]-j)
out=max(out,tmp)
#print(2,i,j,tmp)
ans.append(out)
print(" ".join(map(str,ans)))
``` | instruction | 0 | 89,327 | 1 | 178,654 |
Yes | output | 1 | 89,327 | 1 | 178,655 |
Evaluate the correctness of the submitted Python 2 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled.
Alice received a set of Toy Trainβ’ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station i is station i+1 if 1 β€ i < n or station 1 if i = n. It takes the train 1 second to travel to its next station as described.
Bob gave Alice a fun task before he left: to deliver m candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 1 through m. Candy i (1 β€ i β€ m), now at station a_i, should be delivered to station b_i (a_i β b_i).
<image> The blue numbers on the candies correspond to b_i values. The image corresponds to the 1-st example.
The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible.
Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there.
Input
The first line contains two space-separated integers n and m (2 β€ n β€ 100; 1 β€ m β€ 200) β the number of stations and the number of candies, respectively.
The i-th of the following m lines contains two space-separated integers a_i and b_i (1 β€ a_i, b_i β€ n; a_i β b_i) β the station that initially contains candy i and the destination station of the candy, respectively.
Output
In the first and only line, print n space-separated integers, the i-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station i.
Examples
Input
5 7
2 4
5 1
2 3
3 4
4 1
5 3
3 5
Output
10 9 10 10 9
Input
2 3
1 2
1 2
1 2
Output
5 6
Note
Consider the second sample.
If the train started at station 1, the optimal strategy is as follows.
1. Load the first candy onto the train.
2. Proceed to station 2. This step takes 1 second.
3. Deliver the first candy.
4. Proceed to station 1. This step takes 1 second.
5. Load the second candy onto the train.
6. Proceed to station 2. This step takes 1 second.
7. Deliver the second candy.
8. Proceed to station 1. This step takes 1 second.
9. Load the third candy onto the train.
10. Proceed to station 2. This step takes 1 second.
11. Deliver the third candy.
Hence, the train needs 5 seconds to complete the tasks.
If the train were to start at station 2, however, it would need to move to station 1 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1 = 6 seconds.
Submitted Solution:
```
from sys import stdin, stdout
from collections import Counter, defaultdict
from itertools import permutations, combinations
raw_input = stdin.readline
pr = stdout.write
def in_num():
return int(raw_input())
def in_arr():
return map(int,raw_input().split())
def pr_num(n):
stdout.write(str(n)+'\n')
def pr_arr(arr):
pr(' '.join(map(str,arr))+'\n')
# fast read function for total integer input
def inp():
# this function returns whole input of
# space/line seperated integers
# Use Ctrl+D to flush stdin.
return map(int,stdin.read().split())
range = xrange # not for python 3.0+
# main code
n,m=in_arr()
d=[[] for i in range(n+1)]
for i in range(m):
u,v=in_arr()
d[u].append(v)
dp=[0]*(n+1)
for i in range(1,n+1):
if d[i]:
val=10**18
for j in d[i]:
val=min(val,(j-i)%n)
dp[i]=val+(n*(len(d[i])-1))
ans=[0]*n
for i in range(1,n+1):
val=0
for j in range(1,n+1):
if not dp[j]:
continue
val=max(val,((j-i)%n)+dp[j])
ans[i-1]=val
pr_arr(ans)
``` | instruction | 0 | 89,328 | 1 | 178,656 |
Yes | output | 1 | 89,328 | 1 | 178,657 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled.
Alice received a set of Toy Trainβ’ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station i is station i+1 if 1 β€ i < n or station 1 if i = n. It takes the train 1 second to travel to its next station as described.
Bob gave Alice a fun task before he left: to deliver m candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 1 through m. Candy i (1 β€ i β€ m), now at station a_i, should be delivered to station b_i (a_i β b_i).
<image> The blue numbers on the candies correspond to b_i values. The image corresponds to the 1-st example.
The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible.
Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there.
Input
The first line contains two space-separated integers n and m (2 β€ n β€ 100; 1 β€ m β€ 200) β the number of stations and the number of candies, respectively.
The i-th of the following m lines contains two space-separated integers a_i and b_i (1 β€ a_i, b_i β€ n; a_i β b_i) β the station that initially contains candy i and the destination station of the candy, respectively.
Output
In the first and only line, print n space-separated integers, the i-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station i.
Examples
Input
5 7
2 4
5 1
2 3
3 4
4 1
5 3
3 5
Output
10 9 10 10 9
Input
2 3
1 2
1 2
1 2
Output
5 6
Note
Consider the second sample.
If the train started at station 1, the optimal strategy is as follows.
1. Load the first candy onto the train.
2. Proceed to station 2. This step takes 1 second.
3. Deliver the first candy.
4. Proceed to station 1. This step takes 1 second.
5. Load the second candy onto the train.
6. Proceed to station 2. This step takes 1 second.
7. Deliver the second candy.
8. Proceed to station 1. This step takes 1 second.
9. Load the third candy onto the train.
10. Proceed to station 2. This step takes 1 second.
11. Deliver the third candy.
Hence, the train needs 5 seconds to complete the tasks.
If the train were to start at station 2, however, it would need to move to station 1 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1 = 6 seconds.
Submitted Solution:
```
import os
import sys
from io import BytesIO, IOBase
from collections import defaultdict, deque, Counter, OrderedDict
import threading
from copy import deepcopy
def main():
n,m = map(int,input().split())
station = [[] for _ in range(n+1)]
time = [0]*(n+1)
ans = [0]*(n+1)
for i in range(m):
a,b = map(int,input().split())
if a > b: station[a].append(b+n)
else: station[a].append(b)
for i in range(1,n+1):
station[i].sort()
time_max = [-1,[]]
for i in range(1,n+1):
if len(station[i]):time[i] = (len(station[i])-1)*n + station[i][0]-i
if time[i] == time_max[0]:
time_max[1].append(i)
if time[i] > time_max[0]:
time_max = [time[i],[i]]
for i in range(1,n+1):
ans[i] = time_max[0]
z = 0
for nx in time_max[1]:
if i > nx: z = max(z,nx+n-i)
else: z = max(z,nx-i)
ans[i]+=z
print(*ans[1::])
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
"""threading.stack_size(40960000)
thread = threading.Thread(target=main)
thread.start()"""
main()
``` | instruction | 0 | 89,329 | 1 | 178,658 |
No | output | 1 | 89,329 | 1 | 178,659 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled.
Alice received a set of Toy Trainβ’ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station i is station i+1 if 1 β€ i < n or station 1 if i = n. It takes the train 1 second to travel to its next station as described.
Bob gave Alice a fun task before he left: to deliver m candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 1 through m. Candy i (1 β€ i β€ m), now at station a_i, should be delivered to station b_i (a_i β b_i).
<image> The blue numbers on the candies correspond to b_i values. The image corresponds to the 1-st example.
The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible.
Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there.
Input
The first line contains two space-separated integers n and m (2 β€ n β€ 100; 1 β€ m β€ 200) β the number of stations and the number of candies, respectively.
The i-th of the following m lines contains two space-separated integers a_i and b_i (1 β€ a_i, b_i β€ n; a_i β b_i) β the station that initially contains candy i and the destination station of the candy, respectively.
Output
In the first and only line, print n space-separated integers, the i-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station i.
Examples
Input
5 7
2 4
5 1
2 3
3 4
4 1
5 3
3 5
Output
10 9 10 10 9
Input
2 3
1 2
1 2
1 2
Output
5 6
Note
Consider the second sample.
If the train started at station 1, the optimal strategy is as follows.
1. Load the first candy onto the train.
2. Proceed to station 2. This step takes 1 second.
3. Deliver the first candy.
4. Proceed to station 1. This step takes 1 second.
5. Load the second candy onto the train.
6. Proceed to station 2. This step takes 1 second.
7. Deliver the second candy.
8. Proceed to station 1. This step takes 1 second.
9. Load the third candy onto the train.
10. Proceed to station 2. This step takes 1 second.
11. Deliver the third candy.
Hence, the train needs 5 seconds to complete the tasks.
If the train were to start at station 2, however, it would need to move to station 1 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1 = 6 seconds.
Submitted Solution:
```
import os
import sys
from io import BytesIO, IOBase
from collections import defaultdict, deque, Counter, OrderedDict
import threading
from copy import deepcopy
def main():
n,m = map(int,input().split())
station = [[] for _ in range(n+1)]
time = [0]*(n+1)
ans = [0]*(n+1)
for i in range(m):
a,b = map(int,input().split())
if a > b: station[a].append(b+n)
else: station[a].append(b)
for i in range(1,n+1):
station[i].sort()
for i in range(1,n+1):
if len(station[i]):
time[i] = (len(station[i])-1)*n + station[i][0]-i
for i in range(1,n+1):
for j in range(1,n+1):
ans[i] = max(ans[i],time[j] + (j+n-i)%n)
print(*ans[1::])
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
if __name__ == "__main__":
"""threading.stack_size(40960000)
thread = threading.Thread(target=main)
thread.start()"""
main()
``` | instruction | 0 | 89,330 | 1 | 178,660 |
No | output | 1 | 89,330 | 1 | 178,661 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled.
Alice received a set of Toy Trainβ’ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station i is station i+1 if 1 β€ i < n or station 1 if i = n. It takes the train 1 second to travel to its next station as described.
Bob gave Alice a fun task before he left: to deliver m candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 1 through m. Candy i (1 β€ i β€ m), now at station a_i, should be delivered to station b_i (a_i β b_i).
<image> The blue numbers on the candies correspond to b_i values. The image corresponds to the 1-st example.
The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible.
Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there.
Input
The first line contains two space-separated integers n and m (2 β€ n β€ 100; 1 β€ m β€ 200) β the number of stations and the number of candies, respectively.
The i-th of the following m lines contains two space-separated integers a_i and b_i (1 β€ a_i, b_i β€ n; a_i β b_i) β the station that initially contains candy i and the destination station of the candy, respectively.
Output
In the first and only line, print n space-separated integers, the i-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station i.
Examples
Input
5 7
2 4
5 1
2 3
3 4
4 1
5 3
3 5
Output
10 9 10 10 9
Input
2 3
1 2
1 2
1 2
Output
5 6
Note
Consider the second sample.
If the train started at station 1, the optimal strategy is as follows.
1. Load the first candy onto the train.
2. Proceed to station 2. This step takes 1 second.
3. Deliver the first candy.
4. Proceed to station 1. This step takes 1 second.
5. Load the second candy onto the train.
6. Proceed to station 2. This step takes 1 second.
7. Deliver the second candy.
8. Proceed to station 1. This step takes 1 second.
9. Load the third candy onto the train.
10. Proceed to station 2. This step takes 1 second.
11. Deliver the third candy.
Hence, the train needs 5 seconds to complete the tasks.
If the train were to start at station 2, however, it would need to move to station 1 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1 = 6 seconds.
Submitted Solution:
```
n, m = map(int, input().split())
dict_station = dict()
s = ''
for i in range(m):
a, b = map(int, input().split())
cs = dict_station.get(a, [])
if cs:
if b < cs[0]:
cs.append(b)
else:
cs.insert(0, b)
else:
cs.append(b)
dict_station[a] = cs
for i in range(1, n + 1):
count = m
total = 0
ns = i + 1
cs = []
dict_now = dict()
dict_now[i] = list(dict_station.get(i, []))
if dict_now[i]:
cs.append(dict_now[i].pop(0))
while count > 0 or cs:
if ns > n:
ns = 1
length = len(cs)
for j in range(length - 1, -1, -1):
if cs[j] == ns:
count -= 1
del cs[j]
if dict_now.get(ns, None) is None:
dict_now[ns] = list(dict_station.get(ns, []))
if dict_now.get(ns, []):
cs.append(dict_now[ns].pop(0))
ns += 1
total += 1
s += ' ' + str(total)
print(s[1:])
``` | instruction | 0 | 89,331 | 1 | 178,662 |
No | output | 1 | 89,331 | 1 | 178,663 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled.
Alice received a set of Toy Trainβ’ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station i is station i+1 if 1 β€ i < n or station 1 if i = n. It takes the train 1 second to travel to its next station as described.
Bob gave Alice a fun task before he left: to deliver m candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 1 through m. Candy i (1 β€ i β€ m), now at station a_i, should be delivered to station b_i (a_i β b_i).
<image> The blue numbers on the candies correspond to b_i values. The image corresponds to the 1-st example.
The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible.
Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there.
Input
The first line contains two space-separated integers n and m (2 β€ n β€ 100; 1 β€ m β€ 200) β the number of stations and the number of candies, respectively.
The i-th of the following m lines contains two space-separated integers a_i and b_i (1 β€ a_i, b_i β€ n; a_i β b_i) β the station that initially contains candy i and the destination station of the candy, respectively.
Output
In the first and only line, print n space-separated integers, the i-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station i.
Examples
Input
5 7
2 4
5 1
2 3
3 4
4 1
5 3
3 5
Output
10 9 10 10 9
Input
2 3
1 2
1 2
1 2
Output
5 6
Note
Consider the second sample.
If the train started at station 1, the optimal strategy is as follows.
1. Load the first candy onto the train.
2. Proceed to station 2. This step takes 1 second.
3. Deliver the first candy.
4. Proceed to station 1. This step takes 1 second.
5. Load the second candy onto the train.
6. Proceed to station 2. This step takes 1 second.
7. Deliver the second candy.
8. Proceed to station 1. This step takes 1 second.
9. Load the third candy onto the train.
10. Proceed to station 2. This step takes 1 second.
11. Deliver the third candy.
Hence, the train needs 5 seconds to complete the tasks.
If the train were to start at station 2, however, it would need to move to station 1 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1 = 6 seconds.
Submitted Solution:
```
''' CODED WITH LOVE BY SATYAM KUMAR '''
from sys import stdin, stdout
import cProfile, math
from collections import Counter
from bisect import bisect_left,bisect,bisect_right
import itertools
from copy import deepcopy
from fractions import Fraction
import sys, threading
import operator as op
from functools import reduce
sys.setrecursionlimit(10**6) # max depth of recursion
threading.stack_size(2**27) # new thread will get stack of such size
fac_warmup = False
printHeap = str()
memory_constrained = False
P = 10**9+7
import sys
class Operation:
def __init__(self, name, function, function_on_equal, neutral_value=0):
self.name = name
self.f = function
self.f_on_equal = function_on_equal
def add_multiple(x, count):
return x * count
def min_multiple(x, count):
return x
def max_multiple(x, count):
return x
sum_operation = Operation("sum", sum, add_multiple, 0)
min_operation = Operation("min", min, min_multiple, 1e9)
max_operation = Operation("max", max, max_multiple, -1e9)
class SegmentTree:
def __init__(self,
array,
operations=[sum_operation, min_operation, max_operation]):
self.array = array
if type(operations) != list:
raise TypeError("operations must be a list")
self.operations = {}
for op in operations:
self.operations[op.name] = op
self.root = SegmentTreeNode(0, len(array) - 1, self)
def query(self, start, end, operation_name):
if self.operations.get(operation_name) == None:
raise Exception("This operation is not available")
return self.root._query(start, end, self.operations[operation_name])
def summary(self):
return self.root.values
def update(self, position, value):
self.root._update(position, value)
def update_range(self, start, end, value):
self.root._update_range(start, end, value)
def __repr__(self):
return self.root.__repr__()
class SegmentTreeNode:
def __init__(self, start, end, segment_tree):
self.range = (start, end)
self.parent_tree = segment_tree
self.range_value = None
self.values = {}
self.left = None
self.right = None
if start == end:
self._sync()
return
self.left = SegmentTreeNode(start, start + (end - start) // 2,
segment_tree)
self.right = SegmentTreeNode(start + (end - start) // 2 + 1, end,
segment_tree)
self._sync()
def _query(self, start, end, operation):
if end < self.range[0] or start > self.range[1]:
return None
if start <= self.range[0] and self.range[1] <= end:
return self.values[operation.name]
self._push()
left_res = self.left._query(start, end,
operation) if self.left else None
right_res = self.right._query(start, end,
operation) if self.right else None
if left_res is None:
return right_res
if right_res is None:
return left_res
return operation.f([left_res, right_res])
def _update(self, position, value):
if position < self.range[0] or position > self.range[1]:
return
if position == self.range[0] and self.range[1] == position:
self.parent_tree.array[position] = value
self._sync()
return
self._push()
self.left._update(position, value)
self.right._update(position, value)
self._sync()
def _update_range(self, start, end, value):
if end < self.range[0] or start > self.range[1]:
return
if start <= self.range[0] and self.range[1] <= end:
self.range_value = value
self._sync()
return
self._push()
self.left._update_range(start, end, value)
self.right._update_range(start, end, value)
self._sync()
def _sync(self):
if self.range[0] == self.range[1]:
for op in self.parent_tree.operations.values():
current_value = self.parent_tree.array[self.range[0]]
if self.range_value is not None:
current_value = self.range_value
self.values[op.name] = op.f([current_value])
else:
for op in self.parent_tree.operations.values():
result = op.f(
[self.left.values[op.name], self.right.values[op.name]])
if self.range_value is not None:
bound_length = self.range[1] - self.range[0] + 1
result = op.f_on_equal(self.range_value, bound_length)
self.values[op.name] = result
def _push(self):
if self.range_value is None:
return
if self.left:
self.left.range_value = self.range_value
self.right.range_value = self.range_value
self.left._sync()
self.right._sync()
self.range_value = None
def __repr__(self):
ans = "({}, {}): {}\n".format(self.range[0], self.range[1],
self.values)
if self.left:
ans += self.left.__repr__()
if self.right:
ans += self.right.__repr__()
return ans
def display(string_to_print):
stdout.write(str(string_to_print) + "\n")
def primeFactors(n): #n**0.5 complex
factors = dict()
for i in range(2,math.ceil(math.sqrt(n))+1):
while n % i== 0:
if i in factors:
factors[i]+=1
else: factors[i]=1
n = n // i
if n>2:
factors[n]=1
return (factors)
def binary(n,digits = 20):
b = bin(n)[2:]
b = '0'*(20-len(b))+b
return b
def isprime(n):
"""Returns True if n is prime."""
if n < 4:
return True
if n % 2 == 0:
return False
if n % 3 == 0:
return False
i = 5
w = 2
while i * i <= n:
if n % i == 0:
return False
i += w
w = 6 - w
return True
factorial_modP = []
def warm_up_fac(MOD):
global factorial_modP,fac_warmup
if fac_warmup: return
factorial_modP= [1 for _ in range(fac_warmup_size+1)]
for i in range(2,fac_warmup_size):
factorial_modP[i]= (factorial_modP[i-1]*i) % MOD
fac_warmup = True
def InverseEuler(n,MOD):
return pow(n,MOD-2,MOD)
def nCr(n, r, MOD):
global fac_warmup,factorial_modP
if not fac_warmup:
warm_up_fac(MOD)
fac_warmup = True
return (factorial_modP[n]*((pow(factorial_modP[r], MOD-2, MOD) * pow(factorial_modP[n-r], MOD-2, MOD)) % MOD)) % MOD
def test_print(*args):
if testingMode:
print(args)
def display_list(list1, sep=" "):
stdout.write(sep.join(map(str, list1)) + "\n")
def get_int():
return int(stdin.readline().strip())
def get_tuple():
return map(int, stdin.readline().split())
def get_list():
return list(map(int, stdin.readline().split()))
import heapq,itertools
pq = [] # list of entries arranged in a heap
entry_finder = {} # mapping of tasks to entries
REMOVED = '<removed-task>'
def add_task(task, priority=0):
'Add a new task or update the priority of an existing task'
if task in entry_finder:
remove_task(task)
count = next(counter)
entry = [priority, count, task]
entry_finder[task] = entry
heapq.heappush(pq, entry)
def remove_task(task):
'Mark an existing task as REMOVED. Raise KeyError if not found.'
entry = entry_finder.pop(task)
entry[-1] = REMOVED
def pop_task():
'Remove and return the lowest priority task. Raise KeyError if empty.'
while pq:
priority, count, task = heapq.heappop(pq)
if task is not REMOVED:
del entry_finder[task]
return task
raise KeyError('pop from an empty priority queue')
memory = dict()
def clear_cache():
global memory
memory = dict()
def cached_fn(fn, *args):
global memory
if args in memory:
return memory[args]
else:
result = fn(*args)
memory[args] = result
return result
def binary_serach(i,li):
#print("Search for ",i)
fn = lambda x: li[x]-x//i
x = -1
b = len(li)
while b>=1:
#print(b,x)
while b+x<len(li) and fn(b+x)>0: #Change this condition 2 to whatever you like
x+=b
b=b//2
return x
# -------------------------------------------------------------- MAIN PROGRAM
TestCases = False
testingMode = False
fac_warmup_size = 10**5+100
optimiseForReccursion = True #Can not be used clubbed with TestCases
def main():
n, m = get_tuple()
deliverables = [[] for _ in range(n)]
for _ in range(m):
a,b = get_tuple()
deliverables[a-1].append(b-1)
fixed = len(max(deliverables,key = lambda x: len(x)))
#print(fixed,deliverables)
k = [[i,x] for i,x in enumerate(deliverables)]
processed = []
for li in k:
index = li[0]
g = n
for lis in li[1]:
g = min(g,(lis-index)%n)
processed.append([index,g%n])
#print(processed)
res = []
for i in range(n):
#cost = fixed*n -n
extra = 0
for j in processed:
extra = max (extra, (j[0]-i)%n + j[1]) if len(deliverables[j[0]])==fixed else max(extra,n - (-j[0]+i)%n + j[1] ) if len(j)==fixed-1 else extra
#print(i,j,extra)
cost = (fixed-1)*n + extra
res.append(cost)
display_list(res)
# --------------------------------------------------------------------- END=
if TestCases:
for _ in range(get_int()):
cProfile.run('main()') if testingMode else main()
else: (cProfile.run('main()') if testingMode else main()) if not optimiseForReccursion else threading.Thread(target=main).start()
``` | instruction | 0 | 89,332 | 1 | 178,664 |
No | output | 1 | 89,332 | 1 | 178,665 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n cities numbered from 1 to n in Berland. Some of them are connected by two-way roads. Each road has its own length β an integer number from 1 to 1000. It is known that from each city it is possible to get to any other city by existing roads. Also for each pair of cities it is known the shortest distance between them. Berland Government plans to build k new roads. For each of the planned road it is known its length, and what cities it will connect. To control the correctness of the construction of new roads, after the opening of another road Berland government wants to check the sum of the shortest distances between all pairs of cities. Help them β for a given matrix of shortest distances on the old roads and plans of all new roads, find out how the sum of the shortest distances between all pairs of cities changes after construction of each road.
Input
The first line contains integer n (2 β€ n β€ 300) β amount of cities in Berland. Then there follow n lines with n integer numbers each β the matrix of shortest distances. j-th integer in the i-th row β di, j, the shortest distance between cities i and j. It is guaranteed that di, i = 0, di, j = dj, i, and a given matrix is a matrix of shortest distances for some set of two-way roads with integer lengths from 1 to 1000, such that from each city it is possible to get to any other city using these roads.
Next line contains integer k (1 β€ k β€ 300) β amount of planned roads. Following k lines contain the description of the planned roads. Each road is described by three space-separated integers ai, bi, ci (1 β€ ai, bi β€ n, ai β bi, 1 β€ ci β€ 1000) β ai and bi β pair of cities, which the road connects, ci β the length of the road. It can be several roads between a pair of cities, but no road connects the city with itself.
Output
Output k space-separated integers qi (1 β€ i β€ k). qi should be equal to the sum of shortest distances between all pairs of cities after the construction of roads with indexes from 1 to i. Roads are numbered from 1 in the input order. Each pair of cities should be taken into account in the sum exactly once, i. e. we count unordered pairs.
Examples
Input
2
0 5
5 0
1
1 2 3
Output
3
Input
3
0 4 5
4 0 9
5 9 0
2
2 3 8
1 2 1
Output
17 12 | instruction | 0 | 89,585 | 1 | 179,170 |
Tags: graphs, shortest paths
Correct Solution:
```
from sys import stdin
input = stdin.readline
n = int(input())
m = []
for ni in range(n): m.append([int(x) for x in input().strip().split()])
q = int(input())
ans = []
for qi in range(q):
s, e, v = [int(x) for x in input().strip().split()]
s-=1
e-=1
for i in range(n):
for j in range(n):
nd = m[i][s] + v + m[e][j]
if(nd<m[i][j]):
m[i][j] = nd
m[j][i] = nd
td = 0
for i in range(n):
for j in range(i+1, n):
td += m[i][j]
ans.append(td)
#for mi in m:
# print(mi)
print(*ans)
``` | output | 1 | 89,585 | 1 | 179,171 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n cities numbered from 1 to n in Berland. Some of them are connected by two-way roads. Each road has its own length β an integer number from 1 to 1000. It is known that from each city it is possible to get to any other city by existing roads. Also for each pair of cities it is known the shortest distance between them. Berland Government plans to build k new roads. For each of the planned road it is known its length, and what cities it will connect. To control the correctness of the construction of new roads, after the opening of another road Berland government wants to check the sum of the shortest distances between all pairs of cities. Help them β for a given matrix of shortest distances on the old roads and plans of all new roads, find out how the sum of the shortest distances between all pairs of cities changes after construction of each road.
Input
The first line contains integer n (2 β€ n β€ 300) β amount of cities in Berland. Then there follow n lines with n integer numbers each β the matrix of shortest distances. j-th integer in the i-th row β di, j, the shortest distance between cities i and j. It is guaranteed that di, i = 0, di, j = dj, i, and a given matrix is a matrix of shortest distances for some set of two-way roads with integer lengths from 1 to 1000, such that from each city it is possible to get to any other city using these roads.
Next line contains integer k (1 β€ k β€ 300) β amount of planned roads. Following k lines contain the description of the planned roads. Each road is described by three space-separated integers ai, bi, ci (1 β€ ai, bi β€ n, ai β bi, 1 β€ ci β€ 1000) β ai and bi β pair of cities, which the road connects, ci β the length of the road. It can be several roads between a pair of cities, but no road connects the city with itself.
Output
Output k space-separated integers qi (1 β€ i β€ k). qi should be equal to the sum of shortest distances between all pairs of cities after the construction of roads with indexes from 1 to i. Roads are numbered from 1 in the input order. Each pair of cities should be taken into account in the sum exactly once, i. e. we count unordered pairs.
Examples
Input
2
0 5
5 0
1
1 2 3
Output
3
Input
3
0 4 5
4 0 9
5 9 0
2
2 3 8
1 2 1
Output
17 12 | instruction | 0 | 89,586 | 1 | 179,172 |
Tags: graphs, shortest paths
Correct Solution:
```
n = int(input())
a = []
for i in range(n):
a.append(list(map(int,input().split())))
k = int(input())
for y in range(k):
u,v,d = map(int,input().split())
u = u-1
v = v-1
a[u][v] = min(a[u][v],d)
for i in range(n):
for j in range(n):
s = a[i][u] + a[u][v] + a[v][j]
s2 = a[i][v] + a[u][v] + a[u][j]
s1 = a[i][j]
a[i][j] = min(s,s1,s2)
sum1 = 0
for i in range(n):
sum1 += sum(a[i])
print(sum1//2,end = " ")
print()
``` | output | 1 | 89,586 | 1 | 179,173 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n cities numbered from 1 to n in Berland. Some of them are connected by two-way roads. Each road has its own length β an integer number from 1 to 1000. It is known that from each city it is possible to get to any other city by existing roads. Also for each pair of cities it is known the shortest distance between them. Berland Government plans to build k new roads. For each of the planned road it is known its length, and what cities it will connect. To control the correctness of the construction of new roads, after the opening of another road Berland government wants to check the sum of the shortest distances between all pairs of cities. Help them β for a given matrix of shortest distances on the old roads and plans of all new roads, find out how the sum of the shortest distances between all pairs of cities changes after construction of each road.
Input
The first line contains integer n (2 β€ n β€ 300) β amount of cities in Berland. Then there follow n lines with n integer numbers each β the matrix of shortest distances. j-th integer in the i-th row β di, j, the shortest distance between cities i and j. It is guaranteed that di, i = 0, di, j = dj, i, and a given matrix is a matrix of shortest distances for some set of two-way roads with integer lengths from 1 to 1000, such that from each city it is possible to get to any other city using these roads.
Next line contains integer k (1 β€ k β€ 300) β amount of planned roads. Following k lines contain the description of the planned roads. Each road is described by three space-separated integers ai, bi, ci (1 β€ ai, bi β€ n, ai β bi, 1 β€ ci β€ 1000) β ai and bi β pair of cities, which the road connects, ci β the length of the road. It can be several roads between a pair of cities, but no road connects the city with itself.
Output
Output k space-separated integers qi (1 β€ i β€ k). qi should be equal to the sum of shortest distances between all pairs of cities after the construction of roads with indexes from 1 to i. Roads are numbered from 1 in the input order. Each pair of cities should be taken into account in the sum exactly once, i. e. we count unordered pairs.
Examples
Input
2
0 5
5 0
1
1 2 3
Output
3
Input
3
0 4 5
4 0 9
5 9 0
2
2 3 8
1 2 1
Output
17 12 | instruction | 0 | 89,587 | 1 | 179,174 |
Tags: graphs, shortest paths
Correct Solution:
```
n = int(input())
mt = [[] for _i in range(n)]
for i in range(n):
mt[i] = list(map(int,input().split()))
# print(mt)
kk = int(input())
for i in range(kk):
a,b,c = map(int,input().split())
a-=1
b-=1
if mt[a][b]>c:
mt[a][b] = c
mt[b][a] = c
for i in range(n):
for j in range(n):
mt[i][j] = min(mt[i][j],mt[i][a]+mt[b][j]+c,mt[i][b]+mt[a][j]+c)
ans = 0
for i in range(n):
ans+= sum(mt[i])
print(ans//2)
``` | output | 1 | 89,587 | 1 | 179,175 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n cities numbered from 1 to n in Berland. Some of them are connected by two-way roads. Each road has its own length β an integer number from 1 to 1000. It is known that from each city it is possible to get to any other city by existing roads. Also for each pair of cities it is known the shortest distance between them. Berland Government plans to build k new roads. For each of the planned road it is known its length, and what cities it will connect. To control the correctness of the construction of new roads, after the opening of another road Berland government wants to check the sum of the shortest distances between all pairs of cities. Help them β for a given matrix of shortest distances on the old roads and plans of all new roads, find out how the sum of the shortest distances between all pairs of cities changes after construction of each road.
Input
The first line contains integer n (2 β€ n β€ 300) β amount of cities in Berland. Then there follow n lines with n integer numbers each β the matrix of shortest distances. j-th integer in the i-th row β di, j, the shortest distance between cities i and j. It is guaranteed that di, i = 0, di, j = dj, i, and a given matrix is a matrix of shortest distances for some set of two-way roads with integer lengths from 1 to 1000, such that from each city it is possible to get to any other city using these roads.
Next line contains integer k (1 β€ k β€ 300) β amount of planned roads. Following k lines contain the description of the planned roads. Each road is described by three space-separated integers ai, bi, ci (1 β€ ai, bi β€ n, ai β bi, 1 β€ ci β€ 1000) β ai and bi β pair of cities, which the road connects, ci β the length of the road. It can be several roads between a pair of cities, but no road connects the city with itself.
Output
Output k space-separated integers qi (1 β€ i β€ k). qi should be equal to the sum of shortest distances between all pairs of cities after the construction of roads with indexes from 1 to i. Roads are numbered from 1 in the input order. Each pair of cities should be taken into account in the sum exactly once, i. e. we count unordered pairs.
Examples
Input
2
0 5
5 0
1
1 2 3
Output
3
Input
3
0 4 5
4 0 9
5 9 0
2
2 3 8
1 2 1
Output
17 12 | instruction | 0 | 89,588 | 1 | 179,176 |
Tags: graphs, shortest paths
Correct Solution:
```
from sys import stdin, stdout
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, bisect
mod = pow(10, 9) + 7
mod2 = 998244353
def inp(): return stdin.readline().strip()
def iinp(): return int(inp())
def out(var, end="\n"): stdout.write(str(var)+"\n")
def outa(*var, end="\n"): stdout.write(' '.join(map(str, var)) + end)
def lmp(): return list(mp())
def mp(): return map(int, inp().split())
def smp(): return map(str, inp().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(m, val) for j in range(n)]
def remadd(x, y): return 1 if x%y else 0
def ceil(a,b): return (a+b-1)//b
def isprime(x):
if x<=1: return False
if x in (2, 3): return True
if x%2 == 0: return False
for i in range(3, int(sqrt(x))+1, 2):
if x%i == 0: return False
return True
n = iinp()
dist = [lmp() for i in range(n)]
k = iinp()
quer = []
for i in range(k):
a, b, c = mp()
quer.append((a, b, c))
for q in quer:
x, y, d = q
s = 0
for i in range(n):
for j in range(i, n):
p = min(dist[i][j], dist[i][x-1]+d+dist[y-1][j], dist[i][y-1]+d+dist[x-1][j])
dist[i][j] = p
dist[j][i] = p
s+=dist[i][j]
print(s, end=' ')
print()
``` | output | 1 | 89,588 | 1 | 179,177 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.