message
stringlengths 2
39.6k
| message_type
stringclasses 2
values | message_id
int64 0
1
| conversation_id
int64 450
109k
| cluster
float64 2
2
| __index_level_0__
int64 900
217k
|
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
problem
You want to get a treasure on the Nth basement floor of a dungeon. First, you are on the 1st basement floor and your health is H (H is a positive integer). Go downstairs. Sometimes physical strength is consumed. The physical strength consumed when descending to the lower floor on each floor is known in advance. On the other hand, there is one recovery fountain on each floor, and you can recover with each use of the fountain. Each has its own health. You will die if your health drops below 0. Also, your health will never be higher than H. You can use the Recovery Fountain as many times as you like, but it will take time to recover. Because of this, you want to use the fountain as few times as possible.
N, H, when given the health consumed when descending downstairs on each floor, and the health to recover when using the recovery fountain once on each floor, underground N without reducing health to 0 or less. Create a program to find the minimum number of times the fountain has been used to reach the floor.
Also, once you go downstairs, you can't go upstairs until you get the treasure.
input
The input consists of N lines. The first line of the input contains two integers N, H (2 ≤ N ≤ 100000 = 105, 1 ≤ H ≤ 10000000 = 107) separated by blanks. N is underground. It means that there is a treasure on the Nth floor, and H is the initial physical strength (physical strength at the stage of arriving at the basement 1st floor of the dungeon) and the maximum physical strength (recovery does not make the physical strength larger than H). ).
In the following N − 1 line, two integers are written separated by a space. For the integers di and hi on the 1 + i line (1 ≤ i ≤ N − 1), di is from the basement i floor to the basement i. + The physical strength consumed when descending to the 1st floor, hi represents the physical strength to recover when using the spring once in the basement i floor (0 ≤ di <H, 1 ≤ hi <H).
There is a way to reach the Nth basement floor in any scoring data. Of the scoring data, 10% of the points satisfy N ≤ 1000 and H ≤ 1000. For 30% of the points, N Satisfy ≤ 1000. For 50% of the points, the minimum number of times the fountain is used does not exceed 106.
Example
Input
10 10
4 2
2 5
6 1
7 3
6 4
9 6
0 8
4 1
9 4
Output
10
Submitted Solution:
```
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0553
"""
import sys
from sys import stdin
from heapq import heappop, heappush
input = stdin.readline
def main(args):
N, H = map(int, input().split())
MAX_H = H
costs = []
wells = []
for _ in range(N - 1):
di, hi = map(int, input().split())
costs.append(di)
wells.append(hi)
costs.append(1e7)
wells.append(0)
ans = 0
floor = 1
while floor < N:
# ?£????????????§N????????§??°?????§??????????????§????????????
pq = []
while H > 0:
if H != MAX_H:
heappush(pq, [-1 * min(MAX_H-H, wells[floor - 1]), -floor, wells[floor - 1], H])
c = costs[floor - 1]
H -= c
floor += 1
if floor > N:
break
# ??°?????§??????????????°??????????????§??????????????§??????????????????????????°?????§????????????????????????
# ????????§???????????? min(MAX_H - H, ???????????????) ????????????
# ?????????????????¨??????????¬???????????????? MAX_H - (H + ???????????????) ??¨?????????
replenish, f, replenish_orig, orig_H = heappop(pq)
# if replenish == -replenish_orig:
# if H == 0:
# rep = 1
# else:
# rep = -H // replenish_orig
# if H % replenish_orig != 0:
# rep += 1
#
# H = orig_H + (replenish_orig * rep)
# ans += rep
# else:
H = orig_H - replenish
ans += 1
# ???????????¨???????????¨????????????H?????????????????§????????????????????¢????????£????????¨???????????¢?´¢???????????´??????
floor = -f
print(ans)
if __name__ == '__main__':
main(sys.argv[1:])
```
|
instruction
| 0
| 22,790
| 2
| 45,580
|
No
|
output
| 1
| 22,790
| 2
| 45,581
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
problem
You want to get a treasure on the Nth basement floor of a dungeon. First, you are on the 1st basement floor and your health is H (H is a positive integer). Go downstairs. Sometimes physical strength is consumed. The physical strength consumed when descending to the lower floor on each floor is known in advance. On the other hand, there is one recovery fountain on each floor, and you can recover with each use of the fountain. Each has its own health. You will die if your health drops below 0. Also, your health will never be higher than H. You can use the Recovery Fountain as many times as you like, but it will take time to recover. Because of this, you want to use the fountain as few times as possible.
N, H, when given the health consumed when descending downstairs on each floor, and the health to recover when using the recovery fountain once on each floor, underground N without reducing health to 0 or less. Create a program to find the minimum number of times the fountain has been used to reach the floor.
Also, once you go downstairs, you can't go upstairs until you get the treasure.
input
The input consists of N lines. The first line of the input contains two integers N, H (2 ≤ N ≤ 100000 = 105, 1 ≤ H ≤ 10000000 = 107) separated by blanks. N is underground. It means that there is a treasure on the Nth floor, and H is the initial physical strength (physical strength at the stage of arriving at the basement 1st floor of the dungeon) and the maximum physical strength (recovery does not make the physical strength larger than H). ).
In the following N − 1 line, two integers are written separated by a space. For the integers di and hi on the 1 + i line (1 ≤ i ≤ N − 1), di is from the basement i floor to the basement i. + The physical strength consumed when descending to the 1st floor, hi represents the physical strength to recover when using the spring once in the basement i floor (0 ≤ di <H, 1 ≤ hi <H).
There is a way to reach the Nth basement floor in any scoring data. Of the scoring data, 10% of the points satisfy N ≤ 1000 and H ≤ 1000. For 30% of the points, N Satisfy ≤ 1000. For 50% of the points, the minimum number of times the fountain is used does not exceed 106.
Example
Input
10 10
4 2
2 5
6 1
7 3
6 4
9 6
0 8
4 1
9 4
Output
10
Submitted Solution:
```
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0553
"""
import sys
from sys import stdin
from heapq import heappop, heappush
input = stdin.readline
def solve():
pass
def main(args):
N, H = map(int, input().split())
MAX_H = H
costs = []
wells = []
for _ in range(N - 1):
di, hi = map(int, input().split())
costs.append(di)
wells.append(hi)
costs.append(0)
wells.append(0)
ans = 0
floor = 1
repeat = True
while floor < N:
# ?£????????????§N????????§??°?????§??????????????§????????????
pq = []
while H > 0:
heappush(pq, [-1 * min(MAX_H - H, wells[floor - 1]), floor, H])
c = costs[floor - 1]
H -= c
if floor >= N or H < 1:
break
floor += 1
if H > 0:
break
# ??°?????§??????????????°??????????????§??????????????§??????????????????????????°?????§????????????????????????
# ????????§???????????? min(MAX_H - H, ???????????????) ????????????
# ?????????????????¨??????????¬???????????????? MAX_H - (H + ???????????????) ??¨?????????
replenish, f, orig_H = heappop(pq)
H = min(orig_H - replenish, MAX_H)
ans += 1
# ???????????¨???????????¨????????????H?????????????????§????????????????????¢????????£????????¨???????????¢?´¢???????????´??????
floor = f
print(ans)
if __name__ == '__main__':
main(sys.argv[1:])
```
|
instruction
| 0
| 22,791
| 2
| 45,582
|
No
|
output
| 1
| 22,791
| 2
| 45,583
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
problem
You want to get a treasure on the Nth basement floor of a dungeon. First, you are on the 1st basement floor and your health is H (H is a positive integer). Go downstairs. Sometimes physical strength is consumed. The physical strength consumed when descending to the lower floor on each floor is known in advance. On the other hand, there is one recovery fountain on each floor, and you can recover with each use of the fountain. Each has its own health. You will die if your health drops below 0. Also, your health will never be higher than H. You can use the Recovery Fountain as many times as you like, but it will take time to recover. Because of this, you want to use the fountain as few times as possible.
N, H, when given the health consumed when descending downstairs on each floor, and the health to recover when using the recovery fountain once on each floor, underground N without reducing health to 0 or less. Create a program to find the minimum number of times the fountain has been used to reach the floor.
Also, once you go downstairs, you can't go upstairs until you get the treasure.
input
The input consists of N lines. The first line of the input contains two integers N, H (2 ≤ N ≤ 100000 = 105, 1 ≤ H ≤ 10000000 = 107) separated by blanks. N is underground. It means that there is a treasure on the Nth floor, and H is the initial physical strength (physical strength at the stage of arriving at the basement 1st floor of the dungeon) and the maximum physical strength (recovery does not make the physical strength larger than H). ).
In the following N − 1 line, two integers are written separated by a space. For the integers di and hi on the 1 + i line (1 ≤ i ≤ N − 1), di is from the basement i floor to the basement i. + The physical strength consumed when descending to the 1st floor, hi represents the physical strength to recover when using the spring once in the basement i floor (0 ≤ di <H, 1 ≤ hi <H).
There is a way to reach the Nth basement floor in any scoring data. Of the scoring data, 10% of the points satisfy N ≤ 1000 and H ≤ 1000. For 30% of the points, N Satisfy ≤ 1000. For 50% of the points, the minimum number of times the fountain is used does not exceed 106.
Example
Input
10 10
4 2
2 5
6 1
7 3
6 4
9 6
0 8
4 1
9 4
Output
10
Submitted Solution:
```
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0553
"""
import sys
from sys import stdin
from heapq import heappop, heappush
import time
input = stdin.readline
class RMQ(object):
INT_MAX = 2**31 - 1
def __init__(self, nn):
self.n = 1
while self.n < nn:
self.n *= 2
self.dat = [RMQ.INT_MAX] * ((2 * self.n)-1)
def update(self, k, a):
# A[k]???a?????´??°?????????????????°???????????¨?????´??°
k += (self.n - 1)
self.dat[k] = a
while k > 0:
k = (k - 1) // 2 # ??????index??????
self.dat[k] = min(self.dat[k * 2 + 1], self.dat[k * 2 + 2]) # ???????????¨??????????????????????°?????????????????????´??°
def add(self, k, a):
# A[k]???a??????????????????????????°???????????¨?????´??°
k += (self.n - 1)
self.dat[k] += a
while k > 0:
k = (k - 1) // 2 # ??????index??????
self.dat[k] = min(self.dat[k * 2 + 1], self.dat[k * 2 + 2]) # ???????????¨??????????????????????°?????????????????????´??°
def query(self, a, b, k, l, r):
# [a, b)???????°????????±??????? (0????????§b???????????????)
# ??????????????¨?????????query(a, b, 0, 0, n)?????¢??§??????????????????k, l, r???????????????????????´??°?????????
if r <= a or b <= l: # ?????????????????§??¨??????????????????
return RMQ.INT_MAX
if a <=l and r <= b:
return self.dat[k] # ???????????°???????????????????????§????????°????????¨??????????????????????°?????????????????????????
else:
vl = self.query(a, b, k*2+1, l, (l+r)//2) # ????????°????????????????°????
vr = self.query(a, b, k*2+2, (l+r)//2, r) # ????????°????????????????°????
return min(vl, vr)
def find(self, s, t):
return self.query(s, t+1, 0, 0, self.n)
def solve_hq(N, H, costs, wells):
MAX_H = H
ans = 0
floor = 1
repeat = True
while floor < N:
# ?£????????????§N????????§??°?????§??????????????§????????????
pq = []
while H > 0:
deficit = MAX_H - H
if deficit != 0:
heappush(pq, [-1 * min(MAX_H - H, wells[floor - 1]), wells[floor - 1], floor, H])
c = costs[floor - 1]
H -= c
if floor >= N or H < 1:
break
floor += 1
if H > 0:
return ans
# ??°?????§??????????????°??????????????§??????????????§??????????????????????????°?????§????????????????????????
# ????????§???????????? min(MAX_H - H, ???????????????) ????????????
# ?????????????????¨??????????¬???????????????? MAX_H - (H + ???????????????) ??¨?????????
replenish, replenish_orig, f, orig_H = heappop(pq)
H = min(orig_H - replenish, MAX_H)
ans += 1
# ???????????¨???????????¨????????????H?????????????????§????????????????????¢????????£????????¨???????????¢?´¢???????????´??????
floor = f
def main(args):
# start = time.clock()
N, H = map(int, input().split())
costs = []
wells = []
for _ in range(N - 1):
di, hi = map(int, input().split())
costs.append(di)
wells.append(hi)
costs.append(0)
wells.append(0)
ans = solve_hq(N, H, costs, wells)
print(ans)
# end = time.clock()
# print('elapsed: {}'.format(end - start))
if __name__ == '__main__':
main(sys.argv[1:])
```
|
instruction
| 0
| 22,792
| 2
| 45,584
|
No
|
output
| 1
| 22,792
| 2
| 45,585
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
After realizing that Zookeeper is just a duck, the animals have overthrown Zookeeper. They now have to decide a new ruler among themselves through a fighting tournament of the following format:
Initially, animal 0 is king, while everyone else queues up with animal 1 at the front of the queue and animal n-1 at the back. The animal at the front of the queue will challenge the king to a fight, and the animal with greater strength will win the fight. The winner will become king, while the loser joins the back of the queue.
An animal who wins 3 times consecutively will be crowned ruler for the whole zoo. The strength of each animal depends on how many consecutive fights he won. Animal i has strength A_i with 0 consecutive win, B_i with 1 consecutive win, and C_i with 2 consecutive wins. Initially, everyone has 0 consecutive win.
For all animals, A_i > B_i and C_i > B_i. Also, the values of A_i, B_i, C_i are distinct (all 3n values are pairwise different).
In other words, an animal who is not a king has strength A_i. A king usually has a strength of B_i or C_i. The exception is on the first turn, the first king (animal 0) has strength A_i.
Who is the new ruler, and after how many fights? Or will it end up that animals fight forever with no one ending up as ruler?
Input
The first line contains one integer n (4 ≤ n ≤ 6000) — number of the animals.
i-th of the next n lines contains 3 integers A_i, B_i and C_i (0 ≤ A_i, B_i, C_i ≤ 10^9).
It is guaranteed that A_i > B_i and C_i > B_i, and that all values of A_i, B_i and C_i are distinct.
Output
Output two integers in a single line. The first is the index of the animal that will become ruler, and the second is the number of fights passed until some animal becomes the ruler.
If the animals will fight for infinitely long, output -1 -1 instead.
Examples
Input
4
5 1 2
10 8 11
9 0 3
7 4 6
Output
-1 -1
Input
5
11 7 12
8 6 14
2 1 10
13 0 9
5 3 4
Output
1 7
Note
The following describes the sequence of events for the second sample. Note that in fight 1, the king (animal 0) has strength A_0. The tournament ends at fight 7 as animal 1 wins fight 5, 6 and 7. <image>
Submitted Solution:
```
total_animal = int(input())
king_data = []
win = 0
match = 1
limit = (total_animal * 3) + 1
animals = []
for i in range(0, total_animal):
animal = input()
powers = animal.split(" ")
powers = [int(power) for power in powers]
powers.append(i)
if not king_data:
king_data = powers
continue
if king_data[win] > powers[0]:
win += 1
animals.append(powers)
else:
animals.append(king_data)
king_data = powers
win = 1
if win >= 3:
print(f"{king_data[-1]} {match}")
break
match += 1
while True:
b = animals[0]
if king_data[win] > b[0]:
win += 1
animal = animals.pop(0)
animals.append(animal)
else:
animal = animals.pop(0)
animals.append(king_data)
king_data = animal
win = 1
if win >= 3:
print(f"{king_data[-1]} {match}")
break
match += 1
if match > limit:
break
if win < 3:
print("-1 -1")
```
|
instruction
| 0
| 23,094
| 2
| 46,188
|
No
|
output
| 1
| 23,094
| 2
| 46,189
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
After realizing that Zookeeper is just a duck, the animals have overthrown Zookeeper. They now have to decide a new ruler among themselves through a fighting tournament of the following format:
Initially, animal 0 is king, while everyone else queues up with animal 1 at the front of the queue and animal n-1 at the back. The animal at the front of the queue will challenge the king to a fight, and the animal with greater strength will win the fight. The winner will become king, while the loser joins the back of the queue.
An animal who wins 3 times consecutively will be crowned ruler for the whole zoo. The strength of each animal depends on how many consecutive fights he won. Animal i has strength A_i with 0 consecutive win, B_i with 1 consecutive win, and C_i with 2 consecutive wins. Initially, everyone has 0 consecutive win.
For all animals, A_i > B_i and C_i > B_i. Also, the values of A_i, B_i, C_i are distinct (all 3n values are pairwise different).
In other words, an animal who is not a king has strength A_i. A king usually has a strength of B_i or C_i. The exception is on the first turn, the first king (animal 0) has strength A_i.
Who is the new ruler, and after how many fights? Or will it end up that animals fight forever with no one ending up as ruler?
Input
The first line contains one integer n (4 ≤ n ≤ 6000) — number of the animals.
i-th of the next n lines contains 3 integers A_i, B_i and C_i (0 ≤ A_i, B_i, C_i ≤ 10^9).
It is guaranteed that A_i > B_i and C_i > B_i, and that all values of A_i, B_i and C_i are distinct.
Output
Output two integers in a single line. The first is the index of the animal that will become ruler, and the second is the number of fights passed until some animal becomes the ruler.
If the animals will fight for infinitely long, output -1 -1 instead.
Examples
Input
4
5 1 2
10 8 11
9 0 3
7 4 6
Output
-1 -1
Input
5
11 7 12
8 6 14
2 1 10
13 0 9
5 3 4
Output
1 7
Note
The following describes the sequence of events for the second sample. Note that in fight 1, the king (animal 0) has strength A_0. The tournament ends at fight 7 as animal 1 wins fight 5, 6 and 7. <image>
Submitted Solution:
```
def AnimalFightsh():
num_animals = input(" ")
animals = []
for i in range(num_animals):
strengths = input(" ").split(" ")
animals.strengths([tuple([int(a) for a in strengths]),0,i])
fights = 0
max_win = 0
while win != 3:
if animals[0][0][animals[0][1]] > animals[1][0][animals[1][1]]:
carry = animals[1]
animals.pop(1)
animals.append(carry)
animals[0][1] += 1
elif animals[0][0][animals[0][1]] < animals[1][0][animals[1][1]]:
carry = animals[0]
animals.pop(0)
animals.append(carry)
animals[1][1] += 1
win = animals[0][1]
fights += 1
if fights > len(animals)*5:
print("-1 -1")
print("%s %s"%(animals[0][2],fights))
```
|
instruction
| 0
| 23,095
| 2
| 46,190
|
No
|
output
| 1
| 23,095
| 2
| 46,191
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
After realizing that Zookeeper is just a duck, the animals have overthrown Zookeeper. They now have to decide a new ruler among themselves through a fighting tournament of the following format:
Initially, animal 0 is king, while everyone else queues up with animal 1 at the front of the queue and animal n-1 at the back. The animal at the front of the queue will challenge the king to a fight, and the animal with greater strength will win the fight. The winner will become king, while the loser joins the back of the queue.
An animal who wins 3 times consecutively will be crowned ruler for the whole zoo. The strength of each animal depends on how many consecutive fights he won. Animal i has strength A_i with 0 consecutive win, B_i with 1 consecutive win, and C_i with 2 consecutive wins. Initially, everyone has 0 consecutive win.
For all animals, A_i > B_i and C_i > B_i. Also, the values of A_i, B_i, C_i are distinct (all 3n values are pairwise different).
In other words, an animal who is not a king has strength A_i. A king usually has a strength of B_i or C_i. The exception is on the first turn, the first king (animal 0) has strength A_i.
Who is the new ruler, and after how many fights? Or will it end up that animals fight forever with no one ending up as ruler?
Input
The first line contains one integer n (4 ≤ n ≤ 6000) — number of the animals.
i-th of the next n lines contains 3 integers A_i, B_i and C_i (0 ≤ A_i, B_i, C_i ≤ 10^9).
It is guaranteed that A_i > B_i and C_i > B_i, and that all values of A_i, B_i and C_i are distinct.
Output
Output two integers in a single line. The first is the index of the animal that will become ruler, and the second is the number of fights passed until some animal becomes the ruler.
If the animals will fight for infinitely long, output -1 -1 instead.
Examples
Input
4
5 1 2
10 8 11
9 0 3
7 4 6
Output
-1 -1
Input
5
11 7 12
8 6 14
2 1 10
13 0 9
5 3 4
Output
1 7
Note
The following describes the sequence of events for the second sample. Note that in fight 1, the king (animal 0) has strength A_0. The tournament ends at fight 7 as animal 1 wins fight 5, 6 and 7. <image>
Submitted Solution:
```
total_animal = int(input())
king_data = None
win = 0
match = 1
animals = []
last_animal = None
last_animal_match = 0
for i in range(0, total_animal):
animal = input()
powers = animal.split(" ")
powers = [int(power) for power in powers]
powers.append(i)
powers.append(0)
if not king_data:
king_data = powers
continue
if king_data[win] > powers[0]:
win += 1
king_data[-1] += 1
powers[-1] += 1
animals.append(powers)
else:
king_data[-1] += 1
powers[-1] += 1
animals.append(king_data)
king_data = powers
win = 1
if win >= 3:
print(f"{king_data[-2]} {match}")
break
match += 1
if i >= total_animal - 1:
last_animal = powers
while True:
if king_data[win] > animals[0][0]:
win += 1
king_data[-1] += 1
animals[0][-1] += 1
animal = animals.pop(0)
animals.append(animal)
else:
king_data[-1] += 1
animals[0][-1] += 1
animal = animals.pop(0)
animals.append(king_data)
king_data = animal
win = 1
if win >= 3:
print(f"{king_data[-2]} {match}")
break
match += 1
if king_data[-1] >= 4 and animals[0][-1] >= 4:
break
if win < 3:
print("-1 -1")
```
|
instruction
| 0
| 23,096
| 2
| 46,192
|
No
|
output
| 1
| 23,096
| 2
| 46,193
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
After realizing that Zookeeper is just a duck, the animals have overthrown Zookeeper. They now have to decide a new ruler among themselves through a fighting tournament of the following format:
Initially, animal 0 is king, while everyone else queues up with animal 1 at the front of the queue and animal n-1 at the back. The animal at the front of the queue will challenge the king to a fight, and the animal with greater strength will win the fight. The winner will become king, while the loser joins the back of the queue.
An animal who wins 3 times consecutively will be crowned ruler for the whole zoo. The strength of each animal depends on how many consecutive fights he won. Animal i has strength A_i with 0 consecutive win, B_i with 1 consecutive win, and C_i with 2 consecutive wins. Initially, everyone has 0 consecutive win.
For all animals, A_i > B_i and C_i > B_i. Also, the values of A_i, B_i, C_i are distinct (all 3n values are pairwise different).
In other words, an animal who is not a king has strength A_i. A king usually has a strength of B_i or C_i. The exception is on the first turn, the first king (animal 0) has strength A_i.
Who is the new ruler, and after how many fights? Or will it end up that animals fight forever with no one ending up as ruler?
Input
The first line contains one integer n (4 ≤ n ≤ 6000) — number of the animals.
i-th of the next n lines contains 3 integers A_i, B_i and C_i (0 ≤ A_i, B_i, C_i ≤ 10^9).
It is guaranteed that A_i > B_i and C_i > B_i, and that all values of A_i, B_i and C_i are distinct.
Output
Output two integers in a single line. The first is the index of the animal that will become ruler, and the second is the number of fights passed until some animal becomes the ruler.
If the animals will fight for infinitely long, output -1 -1 instead.
Examples
Input
4
5 1 2
10 8 11
9 0 3
7 4 6
Output
-1 -1
Input
5
11 7 12
8 6 14
2 1 10
13 0 9
5 3 4
Output
1 7
Note
The following describes the sequence of events for the second sample. Note that in fight 1, the king (animal 0) has strength A_0. The tournament ends at fight 7 as animal 1 wins fight 5, 6 and 7. <image>
Submitted Solution:
```
import time
start_time = time.time()
n=int(input())
l=[]
king=[int(i) for i in input().split()]+[0]
for pip in range(1,n):
u=[int(i) for i in input().split()]+[pip]
l.append(u)
old=[]
fights=0
wins=0
while(True):
fights+=1
if king[wins] >=l[0][0]:
wins+=1
l=l[1:]+[l[0]]
if wins==3:
print(str(king[3])+" "+str(fights))
break
else:
wins=1
l.append(king)
king=l.pop(0)
if (time.time() - start_time +0.02 > 3):
print("-1 -1")
break
```
|
instruction
| 0
| 23,097
| 2
| 46,194
|
No
|
output
| 1
| 23,097
| 2
| 46,195
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Valya and Tolya are an ideal pair, but they quarrel sometimes. Recently, Valya took offense at her boyfriend because he came to her in t-shirt with lettering that differs from lettering on her pullover. Now she doesn't want to see him and Tolya is seating at his room and crying at her photos all day long.
This story could be very sad but fairy godmother (Tolya's grandmother) decided to help them and restore their relationship. She secretly took Tolya's t-shirt and Valya's pullover and wants to make the letterings on them same. In order to do this, for one unit of mana she can buy a spell that can change some letters on the clothes. Your task is calculate the minimum amount of mana that Tolya's grandmother should spend to rescue love of Tolya and Valya.
More formally, letterings on Tolya's t-shirt and Valya's pullover are two strings with same length n consisting only of lowercase English letters. Using one unit of mana, grandmother can buy a spell of form (c1, c2) (where c1 and c2 are some lowercase English letters), which can arbitrary number of times transform a single letter c1 to c2 and vise-versa on both Tolya's t-shirt and Valya's pullover. You should find the minimum amount of mana that grandmother should spend to buy a set of spells that can make the letterings equal. In addition you should output the required set of spells.
Input
The first line contains a single integer n (1 ≤ n ≤ 105) — the length of the letterings.
The second line contains a string with length n, consisting of lowercase English letters — the lettering on Valya's pullover.
The third line contains the lettering on Tolya's t-shirt in the same format.
Output
In the first line output a single integer — the minimum amount of mana t required for rescuing love of Valya and Tolya.
In the next t lines output pairs of space-separated lowercase English letters — spells that Tolya's grandmother should buy. Spells and letters in spells can be printed in any order.
If there are many optimal answers, output any.
Examples
Input
3
abb
dad
Output
2
a d
b a
Input
8
drpepper
cocacola
Output
7
l e
e d
d c
c p
p o
o r
r a
Note
In first example it's enough to buy two spells: ('a','d') and ('b','a'). Then first letters will coincide when we will replace letter 'a' with 'd'. Second letters will coincide when we will replace 'b' with 'a'. Third letters will coincide when we will at first replace 'b' with 'a' and then 'a' with 'd'.
|
instruction
| 0
| 23,510
| 2
| 47,020
|
Tags: dfs and similar, dsu, graphs, greedy, strings
Correct Solution:
```
n = int(input())
first, second = input(), input()
letters = {i:i for i in map(lambda i: chr(i), range(ord('a'), ord('z')+1))}
# print(letters)
# res = []
for i in range(n):
s1, s2 = first[i], second[i]
if letters[s1] == letters[s2]:
continue
if s1 > s2:
s1, s2 = s2, s1
frm, to = letters[s2], letters[s1]
for key in letters:
if letters[key] == frm:
letters[key] = to
result = []
for k, v in letters.items():
if k != v:
result.append((k, v))
print(len(result))
for r in result:
print(f'{r[0]} {r[1]}')
```
|
output
| 1
| 23,510
| 2
| 47,021
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Valya and Tolya are an ideal pair, but they quarrel sometimes. Recently, Valya took offense at her boyfriend because he came to her in t-shirt with lettering that differs from lettering on her pullover. Now she doesn't want to see him and Tolya is seating at his room and crying at her photos all day long.
This story could be very sad but fairy godmother (Tolya's grandmother) decided to help them and restore their relationship. She secretly took Tolya's t-shirt and Valya's pullover and wants to make the letterings on them same. In order to do this, for one unit of mana she can buy a spell that can change some letters on the clothes. Your task is calculate the minimum amount of mana that Tolya's grandmother should spend to rescue love of Tolya and Valya.
More formally, letterings on Tolya's t-shirt and Valya's pullover are two strings with same length n consisting only of lowercase English letters. Using one unit of mana, grandmother can buy a spell of form (c1, c2) (where c1 and c2 are some lowercase English letters), which can arbitrary number of times transform a single letter c1 to c2 and vise-versa on both Tolya's t-shirt and Valya's pullover. You should find the minimum amount of mana that grandmother should spend to buy a set of spells that can make the letterings equal. In addition you should output the required set of spells.
Input
The first line contains a single integer n (1 ≤ n ≤ 105) — the length of the letterings.
The second line contains a string with length n, consisting of lowercase English letters — the lettering on Valya's pullover.
The third line contains the lettering on Tolya's t-shirt in the same format.
Output
In the first line output a single integer — the minimum amount of mana t required for rescuing love of Valya and Tolya.
In the next t lines output pairs of space-separated lowercase English letters — spells that Tolya's grandmother should buy. Spells and letters in spells can be printed in any order.
If there are many optimal answers, output any.
Examples
Input
3
abb
dad
Output
2
a d
b a
Input
8
drpepper
cocacola
Output
7
l e
e d
d c
c p
p o
o r
r a
Note
In first example it's enough to buy two spells: ('a','d') and ('b','a'). Then first letters will coincide when we will replace letter 'a' with 'd'. Second letters will coincide when we will replace 'b' with 'a'. Third letters will coincide when we will at first replace 'b' with 'a' and then 'a' with 'd'.
|
instruction
| 0
| 23,511
| 2
| 47,022
|
Tags: dfs and similar, dsu, graphs, greedy, strings
Correct Solution:
```
n = int(input())
S = set()
Slovar = []
sviter = input()
futbol = input()
res = 0
Ans = []
for i in range(n):
if sviter[i] != futbol[i]:
if sviter[i] not in S and futbol[i] not in S:
Slovar.append(sviter[i] + futbol[i])
S.add(sviter[i])
S.add(futbol[i])
res += 1
Ans.append([sviter[i], futbol[i]])
elif sviter[i] in S and futbol[i] not in S:
S.add(futbol[i])
for j in range(len(Slovar)):
if sviter[i] in Slovar[j]:
Slovar[j] += futbol[i]
res += 1
Ans.append([sviter[i], futbol[i]])
break
elif sviter[i] not in S and futbol[i] in S:
S.add(sviter[i])
for j in range(len(Slovar)):
if futbol[i] in Slovar[j]:
Slovar[j] += sviter[i]
res += 1
Ans.append([sviter[i], futbol[i]])
break
elif sviter[i] in S and futbol[i] in S:
for j in range(len(Slovar)):
if futbol[i] in Slovar[j]:
a = j
if sviter[i] in Slovar[j]:
b = j
if a != b:
Slovar[a] = Slovar[a] + Slovar[b]
Slovar[b] = ''
res += 1
Ans.append([sviter[i], futbol[i]])
#print(i, res, Slovar, S)
print(res)
for i in range(res):
print(*Ans[i])
```
|
output
| 1
| 23,511
| 2
| 47,023
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Valya and Tolya are an ideal pair, but they quarrel sometimes. Recently, Valya took offense at her boyfriend because he came to her in t-shirt with lettering that differs from lettering on her pullover. Now she doesn't want to see him and Tolya is seating at his room and crying at her photos all day long.
This story could be very sad but fairy godmother (Tolya's grandmother) decided to help them and restore their relationship. She secretly took Tolya's t-shirt and Valya's pullover and wants to make the letterings on them same. In order to do this, for one unit of mana she can buy a spell that can change some letters on the clothes. Your task is calculate the minimum amount of mana that Tolya's grandmother should spend to rescue love of Tolya and Valya.
More formally, letterings on Tolya's t-shirt and Valya's pullover are two strings with same length n consisting only of lowercase English letters. Using one unit of mana, grandmother can buy a spell of form (c1, c2) (where c1 and c2 are some lowercase English letters), which can arbitrary number of times transform a single letter c1 to c2 and vise-versa on both Tolya's t-shirt and Valya's pullover. You should find the minimum amount of mana that grandmother should spend to buy a set of spells that can make the letterings equal. In addition you should output the required set of spells.
Input
The first line contains a single integer n (1 ≤ n ≤ 105) — the length of the letterings.
The second line contains a string with length n, consisting of lowercase English letters — the lettering on Valya's pullover.
The third line contains the lettering on Tolya's t-shirt in the same format.
Output
In the first line output a single integer — the minimum amount of mana t required for rescuing love of Valya and Tolya.
In the next t lines output pairs of space-separated lowercase English letters — spells that Tolya's grandmother should buy. Spells and letters in spells can be printed in any order.
If there are many optimal answers, output any.
Examples
Input
3
abb
dad
Output
2
a d
b a
Input
8
drpepper
cocacola
Output
7
l e
e d
d c
c p
p o
o r
r a
Note
In first example it's enough to buy two spells: ('a','d') and ('b','a'). Then first letters will coincide when we will replace letter 'a' with 'd'. Second letters will coincide when we will replace 'b' with 'a'. Third letters will coincide when we will at first replace 'b' with 'a' and then 'a' with 'd'.
|
instruction
| 0
| 23,512
| 2
| 47,024
|
Tags: dfs and similar, dsu, graphs, greedy, strings
Correct Solution:
```
from collections import defaultdict
from queue import Queue
IS_WAY_CACHE = set()
def is_way(char_map, ch1, ch2):
global IS_WAY_CACHE
q = Queue()
q.put(ch1)
visited = {ch1}
while not q.empty():
cur_node = q.get()
if cur_node == ch2:
return True
visited.add(cur_node)
neighbours = char_map[cur_node]
for n in neighbours:
IS_WAY_CACHE.add((ch1, n))
if n not in visited:
q.put(n)
return False
def is_way_cache(ch1, ch2):
return (ch1, ch2) in IS_WAY_CACHE
def main():
input()
one, two = input(), input()
char_map = defaultdict(set)
res = []
for ch1, ch2 in zip(one, two):
if ch1 == ch2:
continue
if is_way_cache(ch1, ch2):
continue
if is_way(char_map, ch1, ch2):
continue
res.append((ch1, ch2))
char_map[ch1].add(ch2)
char_map[ch2].add(ch1)
print(len(res))
for ch1, ch2 in res:
print(ch1, ch2)
if __name__ == '__main__':
main()
```
|
output
| 1
| 23,512
| 2
| 47,025
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Valya and Tolya are an ideal pair, but they quarrel sometimes. Recently, Valya took offense at her boyfriend because he came to her in t-shirt with lettering that differs from lettering on her pullover. Now she doesn't want to see him and Tolya is seating at his room and crying at her photos all day long.
This story could be very sad but fairy godmother (Tolya's grandmother) decided to help them and restore their relationship. She secretly took Tolya's t-shirt and Valya's pullover and wants to make the letterings on them same. In order to do this, for one unit of mana she can buy a spell that can change some letters on the clothes. Your task is calculate the minimum amount of mana that Tolya's grandmother should spend to rescue love of Tolya and Valya.
More formally, letterings on Tolya's t-shirt and Valya's pullover are two strings with same length n consisting only of lowercase English letters. Using one unit of mana, grandmother can buy a spell of form (c1, c2) (where c1 and c2 are some lowercase English letters), which can arbitrary number of times transform a single letter c1 to c2 and vise-versa on both Tolya's t-shirt and Valya's pullover. You should find the minimum amount of mana that grandmother should spend to buy a set of spells that can make the letterings equal. In addition you should output the required set of spells.
Input
The first line contains a single integer n (1 ≤ n ≤ 105) — the length of the letterings.
The second line contains a string with length n, consisting of lowercase English letters — the lettering on Valya's pullover.
The third line contains the lettering on Tolya's t-shirt in the same format.
Output
In the first line output a single integer — the minimum amount of mana t required for rescuing love of Valya and Tolya.
In the next t lines output pairs of space-separated lowercase English letters — spells that Tolya's grandmother should buy. Spells and letters in spells can be printed in any order.
If there are many optimal answers, output any.
Examples
Input
3
abb
dad
Output
2
a d
b a
Input
8
drpepper
cocacola
Output
7
l e
e d
d c
c p
p o
o r
r a
Note
In first example it's enough to buy two spells: ('a','d') and ('b','a'). Then first letters will coincide when we will replace letter 'a' with 'd'. Second letters will coincide when we will replace 'b' with 'a'. Third letters will coincide when we will at first replace 'b' with 'a' and then 'a' with 'd'.
|
instruction
| 0
| 23,513
| 2
| 47,026
|
Tags: dfs and similar, dsu, graphs, greedy, strings
Correct Solution:
```
A='abcdefghijklmnopqrstuvwxyz'
n=int(input());q={};d={};z=[]
for i in range(26):d[A[i]]=i;q[i]=A[i]
for a,b in zip(input(),input()):
if d[a]!=d[b]:
z+=(a,b),
for i in q.pop(d[b]):d[i]=d[a];q[d[a]]+=i
print(len(z))
for i in z:print(*i)
```
|
output
| 1
| 23,513
| 2
| 47,027
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Valya and Tolya are an ideal pair, but they quarrel sometimes. Recently, Valya took offense at her boyfriend because he came to her in t-shirt with lettering that differs from lettering on her pullover. Now she doesn't want to see him and Tolya is seating at his room and crying at her photos all day long.
This story could be very sad but fairy godmother (Tolya's grandmother) decided to help them and restore their relationship. She secretly took Tolya's t-shirt and Valya's pullover and wants to make the letterings on them same. In order to do this, for one unit of mana she can buy a spell that can change some letters on the clothes. Your task is calculate the minimum amount of mana that Tolya's grandmother should spend to rescue love of Tolya and Valya.
More formally, letterings on Tolya's t-shirt and Valya's pullover are two strings with same length n consisting only of lowercase English letters. Using one unit of mana, grandmother can buy a spell of form (c1, c2) (where c1 and c2 are some lowercase English letters), which can arbitrary number of times transform a single letter c1 to c2 and vise-versa on both Tolya's t-shirt and Valya's pullover. You should find the minimum amount of mana that grandmother should spend to buy a set of spells that can make the letterings equal. In addition you should output the required set of spells.
Input
The first line contains a single integer n (1 ≤ n ≤ 105) — the length of the letterings.
The second line contains a string with length n, consisting of lowercase English letters — the lettering on Valya's pullover.
The third line contains the lettering on Tolya's t-shirt in the same format.
Output
In the first line output a single integer — the minimum amount of mana t required for rescuing love of Valya and Tolya.
In the next t lines output pairs of space-separated lowercase English letters — spells that Tolya's grandmother should buy. Spells and letters in spells can be printed in any order.
If there are many optimal answers, output any.
Examples
Input
3
abb
dad
Output
2
a d
b a
Input
8
drpepper
cocacola
Output
7
l e
e d
d c
c p
p o
o r
r a
Note
In first example it's enough to buy two spells: ('a','d') and ('b','a'). Then first letters will coincide when we will replace letter 'a' with 'd'. Second letters will coincide when we will replace 'b' with 'a'. Third letters will coincide when we will at first replace 'b' with 'a' and then 'a' with 'd'.
|
instruction
| 0
| 23,514
| 2
| 47,028
|
Tags: dfs and similar, dsu, graphs, greedy, strings
Correct Solution:
```
from collections import defaultdict
from queue import Queue
IS_WAY_CACHE = set()
def read_nums():
return [int(x) for x in input().split()]
def is_way(char_map, ch1, ch2):
global IS_WAY_CACHE
q = Queue()
q.put(ch1)
visited = {ch1}
while not q.empty():
cur_node = q.get()
if cur_node == ch2:
return True
visited.add(cur_node)
neighbours = char_map[cur_node]
for n in neighbours:
IS_WAY_CACHE.add((ch1, n))
if n not in visited:
q.put(n)
return False
def is_way_cache(ch1, ch2):
return (ch1, ch2) in IS_WAY_CACHE
def main():
input()
one, two = input(), input()
char_map = defaultdict(set)
res = []
for ch1, ch2 in zip(one, two):
if ch1 == ch2:
continue
if is_way_cache(ch1, ch2):
continue
if is_way_cache(ch2, ch1):
continue
if is_way(char_map, ch1, ch2):
continue
if is_way(char_map, ch2, ch1):
continue
res.append((ch1, ch2))
char_map[ch1].add(ch2)
char_map[ch2].add(ch1)
print(len(res))
for ch1, ch2 in res:
print(ch1, ch2)
if __name__ == '__main__':
main()
```
|
output
| 1
| 23,514
| 2
| 47,029
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Valya and Tolya are an ideal pair, but they quarrel sometimes. Recently, Valya took offense at her boyfriend because he came to her in t-shirt with lettering that differs from lettering on her pullover. Now she doesn't want to see him and Tolya is seating at his room and crying at her photos all day long.
This story could be very sad but fairy godmother (Tolya's grandmother) decided to help them and restore their relationship. She secretly took Tolya's t-shirt and Valya's pullover and wants to make the letterings on them same. In order to do this, for one unit of mana she can buy a spell that can change some letters on the clothes. Your task is calculate the minimum amount of mana that Tolya's grandmother should spend to rescue love of Tolya and Valya.
More formally, letterings on Tolya's t-shirt and Valya's pullover are two strings with same length n consisting only of lowercase English letters. Using one unit of mana, grandmother can buy a spell of form (c1, c2) (where c1 and c2 are some lowercase English letters), which can arbitrary number of times transform a single letter c1 to c2 and vise-versa on both Tolya's t-shirt and Valya's pullover. You should find the minimum amount of mana that grandmother should spend to buy a set of spells that can make the letterings equal. In addition you should output the required set of spells.
Input
The first line contains a single integer n (1 ≤ n ≤ 105) — the length of the letterings.
The second line contains a string with length n, consisting of lowercase English letters — the lettering on Valya's pullover.
The third line contains the lettering on Tolya's t-shirt in the same format.
Output
In the first line output a single integer — the minimum amount of mana t required for rescuing love of Valya and Tolya.
In the next t lines output pairs of space-separated lowercase English letters — spells that Tolya's grandmother should buy. Spells and letters in spells can be printed in any order.
If there are many optimal answers, output any.
Examples
Input
3
abb
dad
Output
2
a d
b a
Input
8
drpepper
cocacola
Output
7
l e
e d
d c
c p
p o
o r
r a
Note
In first example it's enough to buy two spells: ('a','d') and ('b','a'). Then first letters will coincide when we will replace letter 'a' with 'd'. Second letters will coincide when we will replace 'b' with 'a'. Third letters will coincide when we will at first replace 'b' with 'a' and then 'a' with 'd'.
|
instruction
| 0
| 23,515
| 2
| 47,030
|
Tags: dfs and similar, dsu, graphs, greedy, strings
Correct Solution:
```
def find(x,parent,rank):
if(parent[x]!=x):
parent[x]=find(parent[x],parent,rank)
return parent[x]
def union(x,y,parent,rank):
xroot=parent[x]
yroot=parent[y]
if(rank[xroot]<rank[yroot]):
parent[xroot]=yroot
elif(rank[xroot]>rank[yroot]):
parent[yroot]=xroot
else:
parent[yroot]=xroot
rank[xroot]+=1
n=int(input())
str1=input()
str2=input()
hashd=dict()
rev=dict()
i=0
k=0
while(i<len(str1)):
if(str1[i] not in hashd):
hashd[str1[i]]=k
rev[k]=str1[i]
k+=1
i+=1
i=0
while(i<len(str2)):
if(str2[i] not in hashd):
hashd[str2[i]]=k
rev[k]=str2[i]
k+=1
i+=1
rank=[0 for x in range(k)]
parent=[x for x in range(k)]
sums=0
i=0
arr1=[]
arr2=[]
while(i<n):
if(find(hashd[str1[i]],parent,rank)!=find(hashd[str2[i]],parent,rank)):
sums+=1
union(hashd[str1[i]],hashd[str2[i]],parent,rank)
arr1.append(str1[i])
arr2.append(str2[i])
i+=1
print(sums)
i=0
while(i<len(arr1)):
print(arr1[i]+' '+arr2[i])
i+=1
```
|
output
| 1
| 23,515
| 2
| 47,031
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Valya and Tolya are an ideal pair, but they quarrel sometimes. Recently, Valya took offense at her boyfriend because he came to her in t-shirt with lettering that differs from lettering on her pullover. Now she doesn't want to see him and Tolya is seating at his room and crying at her photos all day long.
This story could be very sad but fairy godmother (Tolya's grandmother) decided to help them and restore their relationship. She secretly took Tolya's t-shirt and Valya's pullover and wants to make the letterings on them same. In order to do this, for one unit of mana she can buy a spell that can change some letters on the clothes. Your task is calculate the minimum amount of mana that Tolya's grandmother should spend to rescue love of Tolya and Valya.
More formally, letterings on Tolya's t-shirt and Valya's pullover are two strings with same length n consisting only of lowercase English letters. Using one unit of mana, grandmother can buy a spell of form (c1, c2) (where c1 and c2 are some lowercase English letters), which can arbitrary number of times transform a single letter c1 to c2 and vise-versa on both Tolya's t-shirt and Valya's pullover. You should find the minimum amount of mana that grandmother should spend to buy a set of spells that can make the letterings equal. In addition you should output the required set of spells.
Input
The first line contains a single integer n (1 ≤ n ≤ 105) — the length of the letterings.
The second line contains a string with length n, consisting of lowercase English letters — the lettering on Valya's pullover.
The third line contains the lettering on Tolya's t-shirt in the same format.
Output
In the first line output a single integer — the minimum amount of mana t required for rescuing love of Valya and Tolya.
In the next t lines output pairs of space-separated lowercase English letters — spells that Tolya's grandmother should buy. Spells and letters in spells can be printed in any order.
If there are many optimal answers, output any.
Examples
Input
3
abb
dad
Output
2
a d
b a
Input
8
drpepper
cocacola
Output
7
l e
e d
d c
c p
p o
o r
r a
Note
In first example it's enough to buy two spells: ('a','d') and ('b','a'). Then first letters will coincide when we will replace letter 'a' with 'd'. Second letters will coincide when we will replace 'b' with 'a'. Third letters will coincide when we will at first replace 'b' with 'a' and then 'a' with 'd'.
|
instruction
| 0
| 23,516
| 2
| 47,032
|
Tags: dfs and similar, dsu, graphs, greedy, strings
Correct Solution:
```
n=int(input())
s=input()
t=input()
parent=[i for i in range(27)]
ans=0
res=[]
def find(target):
if parent[target] == target:
return target
parent[target] = find(parent[target])
return parent[target]
def union(a, b):
a = find(a)
b = find(b)
if a < b:
parent[b] = a
else:
parent[a] = b
for j in range(n):
a,b=ord(s[j])-97,ord(t[j])-97
if find(a)!=find(b):
res.append([s[j],t[j]])
ans+=1
union(a,b)
print(ans)
for j in res:
print(*j)
```
|
output
| 1
| 23,516
| 2
| 47,033
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Valya and Tolya are an ideal pair, but they quarrel sometimes. Recently, Valya took offense at her boyfriend because he came to her in t-shirt with lettering that differs from lettering on her pullover. Now she doesn't want to see him and Tolya is seating at his room and crying at her photos all day long.
This story could be very sad but fairy godmother (Tolya's grandmother) decided to help them and restore their relationship. She secretly took Tolya's t-shirt and Valya's pullover and wants to make the letterings on them same. In order to do this, for one unit of mana she can buy a spell that can change some letters on the clothes. Your task is calculate the minimum amount of mana that Tolya's grandmother should spend to rescue love of Tolya and Valya.
More formally, letterings on Tolya's t-shirt and Valya's pullover are two strings with same length n consisting only of lowercase English letters. Using one unit of mana, grandmother can buy a spell of form (c1, c2) (where c1 and c2 are some lowercase English letters), which can arbitrary number of times transform a single letter c1 to c2 and vise-versa on both Tolya's t-shirt and Valya's pullover. You should find the minimum amount of mana that grandmother should spend to buy a set of spells that can make the letterings equal. In addition you should output the required set of spells.
Input
The first line contains a single integer n (1 ≤ n ≤ 105) — the length of the letterings.
The second line contains a string with length n, consisting of lowercase English letters — the lettering on Valya's pullover.
The third line contains the lettering on Tolya's t-shirt in the same format.
Output
In the first line output a single integer — the minimum amount of mana t required for rescuing love of Valya and Tolya.
In the next t lines output pairs of space-separated lowercase English letters — spells that Tolya's grandmother should buy. Spells and letters in spells can be printed in any order.
If there are many optimal answers, output any.
Examples
Input
3
abb
dad
Output
2
a d
b a
Input
8
drpepper
cocacola
Output
7
l e
e d
d c
c p
p o
o r
r a
Note
In first example it's enough to buy two spells: ('a','d') and ('b','a'). Then first letters will coincide when we will replace letter 'a' with 'd'. Second letters will coincide when we will replace 'b' with 'a'. Third letters will coincide when we will at first replace 'b' with 'a' and then 'a' with 'd'.
|
instruction
| 0
| 23,517
| 2
| 47,034
|
Tags: dfs and similar, dsu, graphs, greedy, strings
Correct Solution:
```
n=int(input())
a=input()
b=input()
diff=[]
for i in range(n):
if(a[i]!=b[i]):
posai=-1
posbi=-1
for j in range(len(diff)):
if(a[i] in diff[j]):
posai=j
if(b[i] in diff[j]):
posbi=j
if(posbi!=-1 and posai!=-1):
if(posbi==posai):
pass
else:
diff[posai]=diff[posai].union(diff[posbi])
del diff[posbi]
elif(posbi!=-1):
diff[posbi].add(a[i])
elif(posai!=-1):
diff[posai].add(b[i])
else:
diff.append({a[i],b[i]})
#print(diff)
diff2=[]
for i in diff:
diff2.append([])
for j in i:
diff2[-1].append(j)
ans=0
for i in diff2:
ans+=len(i)-1
print(ans)
for i in diff2:
for j in range(1,len(i)):
print(i[j-1],i[j])
```
|
output
| 1
| 23,517
| 2
| 47,035
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valya and Tolya are an ideal pair, but they quarrel sometimes. Recently, Valya took offense at her boyfriend because he came to her in t-shirt with lettering that differs from lettering on her pullover. Now she doesn't want to see him and Tolya is seating at his room and crying at her photos all day long.
This story could be very sad but fairy godmother (Tolya's grandmother) decided to help them and restore their relationship. She secretly took Tolya's t-shirt and Valya's pullover and wants to make the letterings on them same. In order to do this, for one unit of mana she can buy a spell that can change some letters on the clothes. Your task is calculate the minimum amount of mana that Tolya's grandmother should spend to rescue love of Tolya and Valya.
More formally, letterings on Tolya's t-shirt and Valya's pullover are two strings with same length n consisting only of lowercase English letters. Using one unit of mana, grandmother can buy a spell of form (c1, c2) (where c1 and c2 are some lowercase English letters), which can arbitrary number of times transform a single letter c1 to c2 and vise-versa on both Tolya's t-shirt and Valya's pullover. You should find the minimum amount of mana that grandmother should spend to buy a set of spells that can make the letterings equal. In addition you should output the required set of spells.
Input
The first line contains a single integer n (1 ≤ n ≤ 105) — the length of the letterings.
The second line contains a string with length n, consisting of lowercase English letters — the lettering on Valya's pullover.
The third line contains the lettering on Tolya's t-shirt in the same format.
Output
In the first line output a single integer — the minimum amount of mana t required for rescuing love of Valya and Tolya.
In the next t lines output pairs of space-separated lowercase English letters — spells that Tolya's grandmother should buy. Spells and letters in spells can be printed in any order.
If there are many optimal answers, output any.
Examples
Input
3
abb
dad
Output
2
a d
b a
Input
8
drpepper
cocacola
Output
7
l e
e d
d c
c p
p o
o r
r a
Note
In first example it's enough to buy two spells: ('a','d') and ('b','a'). Then first letters will coincide when we will replace letter 'a' with 'd'. Second letters will coincide when we will replace 'b' with 'a'. Third letters will coincide when we will at first replace 'b' with 'a' and then 'a' with 'd'.
Submitted Solution:
```
n = int(input())
a = input()
b = input()
f = lambda c: ord(c) - 97
g = lambda x: chr(97 + x)
fa = [i for i in range(26)]
get_fa = lambda x: x if fa[x] == x else get_fa(fa[x])
for i in range(n):
child = f(b[i])
father = get_fa(child)
while (child != father):
child = fa[child]
fa[child] = get_fa(f(a[i]))
print(sum(i != get_fa(i) for i in range(26)))
for i in range(26):
l = i
r = fa[i]
if l != r:
print(g(l), g(r))
```
|
instruction
| 0
| 23,518
| 2
| 47,036
|
Yes
|
output
| 1
| 23,518
| 2
| 47,037
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valya and Tolya are an ideal pair, but they quarrel sometimes. Recently, Valya took offense at her boyfriend because he came to her in t-shirt with lettering that differs from lettering on her pullover. Now she doesn't want to see him and Tolya is seating at his room and crying at her photos all day long.
This story could be very sad but fairy godmother (Tolya's grandmother) decided to help them and restore their relationship. She secretly took Tolya's t-shirt and Valya's pullover and wants to make the letterings on them same. In order to do this, for one unit of mana she can buy a spell that can change some letters on the clothes. Your task is calculate the minimum amount of mana that Tolya's grandmother should spend to rescue love of Tolya and Valya.
More formally, letterings on Tolya's t-shirt and Valya's pullover are two strings with same length n consisting only of lowercase English letters. Using one unit of mana, grandmother can buy a spell of form (c1, c2) (where c1 and c2 are some lowercase English letters), which can arbitrary number of times transform a single letter c1 to c2 and vise-versa on both Tolya's t-shirt and Valya's pullover. You should find the minimum amount of mana that grandmother should spend to buy a set of spells that can make the letterings equal. In addition you should output the required set of spells.
Input
The first line contains a single integer n (1 ≤ n ≤ 105) — the length of the letterings.
The second line contains a string with length n, consisting of lowercase English letters — the lettering on Valya's pullover.
The third line contains the lettering on Tolya's t-shirt in the same format.
Output
In the first line output a single integer — the minimum amount of mana t required for rescuing love of Valya and Tolya.
In the next t lines output pairs of space-separated lowercase English letters — spells that Tolya's grandmother should buy. Spells and letters in spells can be printed in any order.
If there are many optimal answers, output any.
Examples
Input
3
abb
dad
Output
2
a d
b a
Input
8
drpepper
cocacola
Output
7
l e
e d
d c
c p
p o
o r
r a
Note
In first example it's enough to buy two spells: ('a','d') and ('b','a'). Then first letters will coincide when we will replace letter 'a' with 'd'. Second letters will coincide when we will replace 'b' with 'a'. Third letters will coincide when we will at first replace 'b' with 'a' and then 'a' with 'd'.
Submitted Solution:
```
def wordcount(a,b):
c=0
for i in range(0,len(a)):
if(b==a[i]):
c=c+1
return c
def applyChange(a,ar):
news=""
for i in range(0,len(a)):
if(a[i]==ar[0]):
news=news+ar[1]
else:
news=news+a[i]
return news
n=int(input())
Valya=input()
Tolya=input()
spells=[]
for i in range(0,n):
if(Valya[i]!=Tolya[i]):
v=wordcount(Valya,Valya[i])
t=wordcount(Tolya,Tolya[i])
if(v>=t):
u=[Valya[i],Tolya[i]]
spells.append(u)
Valya=applyChange(Valya,u)
Tolya=applyChange(Tolya,u)
else:
w=[Tolya[i], Valya[i]]
spells.append(w)
Valya=applyChange(Valya,w)
Tolya=applyChange(Tolya,w)
print(len(spells))
for j in range(0,len(spells)):
print(str((spells[j])[0])+" "+str((spells[j])[1]))
```
|
instruction
| 0
| 23,519
| 2
| 47,038
|
Yes
|
output
| 1
| 23,519
| 2
| 47,039
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valya and Tolya are an ideal pair, but they quarrel sometimes. Recently, Valya took offense at her boyfriend because he came to her in t-shirt with lettering that differs from lettering on her pullover. Now she doesn't want to see him and Tolya is seating at his room and crying at her photos all day long.
This story could be very sad but fairy godmother (Tolya's grandmother) decided to help them and restore their relationship. She secretly took Tolya's t-shirt and Valya's pullover and wants to make the letterings on them same. In order to do this, for one unit of mana she can buy a spell that can change some letters on the clothes. Your task is calculate the minimum amount of mana that Tolya's grandmother should spend to rescue love of Tolya and Valya.
More formally, letterings on Tolya's t-shirt and Valya's pullover are two strings with same length n consisting only of lowercase English letters. Using one unit of mana, grandmother can buy a spell of form (c1, c2) (where c1 and c2 are some lowercase English letters), which can arbitrary number of times transform a single letter c1 to c2 and vise-versa on both Tolya's t-shirt and Valya's pullover. You should find the minimum amount of mana that grandmother should spend to buy a set of spells that can make the letterings equal. In addition you should output the required set of spells.
Input
The first line contains a single integer n (1 ≤ n ≤ 105) — the length of the letterings.
The second line contains a string with length n, consisting of lowercase English letters — the lettering on Valya's pullover.
The third line contains the lettering on Tolya's t-shirt in the same format.
Output
In the first line output a single integer — the minimum amount of mana t required for rescuing love of Valya and Tolya.
In the next t lines output pairs of space-separated lowercase English letters — spells that Tolya's grandmother should buy. Spells and letters in spells can be printed in any order.
If there are many optimal answers, output any.
Examples
Input
3
abb
dad
Output
2
a d
b a
Input
8
drpepper
cocacola
Output
7
l e
e d
d c
c p
p o
o r
r a
Note
In first example it's enough to buy two spells: ('a','d') and ('b','a'). Then first letters will coincide when we will replace letter 'a' with 'd'. Second letters will coincide when we will replace 'b' with 'a'. Third letters will coincide when we will at first replace 'b' with 'a' and then 'a' with 'd'.
Submitted Solution:
```
n = int(input())
s1 = input()
s2 = input()
seps = [i for i in range(26)]
ranks = [1 for i in range(26)]
outs = []
def parent(a):
global seps
na =a
while seps[na] != na:
na = seps[na]
na2 = a
while seps[na2] != na2:
na2, seps[na2] = seps[na2], na
return na
def cons(a,b):
return parent(a) == parent(b)
def union(a,b):
global seps, ranks
if cons(a,b): return
pa, pb = parent(a), parent(b)
if ranks[pa] == ranks[pb]:
ranks[pa] += 1
seps[pb] = pa
elif ranks[pa] < ranks[pb]:
seps[pa] = pb
else:
seps[pb] = pa
i = 0
while i < n:
if cons(ord(s1[i])-97, ord(s2[i])-97):
i += 1
continue
else:
outs.append((s1[i], s2[i]))
union(ord(s1[i])-97, ord(s2[i])-97)
i += 1
print(len(outs))
for u in outs: print(u[0], u[1])
```
|
instruction
| 0
| 23,520
| 2
| 47,040
|
Yes
|
output
| 1
| 23,520
| 2
| 47,041
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valya and Tolya are an ideal pair, but they quarrel sometimes. Recently, Valya took offense at her boyfriend because he came to her in t-shirt with lettering that differs from lettering on her pullover. Now she doesn't want to see him and Tolya is seating at his room and crying at her photos all day long.
This story could be very sad but fairy godmother (Tolya's grandmother) decided to help them and restore their relationship. She secretly took Tolya's t-shirt and Valya's pullover and wants to make the letterings on them same. In order to do this, for one unit of mana she can buy a spell that can change some letters on the clothes. Your task is calculate the minimum amount of mana that Tolya's grandmother should spend to rescue love of Tolya and Valya.
More formally, letterings on Tolya's t-shirt and Valya's pullover are two strings with same length n consisting only of lowercase English letters. Using one unit of mana, grandmother can buy a spell of form (c1, c2) (where c1 and c2 are some lowercase English letters), which can arbitrary number of times transform a single letter c1 to c2 and vise-versa on both Tolya's t-shirt and Valya's pullover. You should find the minimum amount of mana that grandmother should spend to buy a set of spells that can make the letterings equal. In addition you should output the required set of spells.
Input
The first line contains a single integer n (1 ≤ n ≤ 105) — the length of the letterings.
The second line contains a string with length n, consisting of lowercase English letters — the lettering on Valya's pullover.
The third line contains the lettering on Tolya's t-shirt in the same format.
Output
In the first line output a single integer — the minimum amount of mana t required for rescuing love of Valya and Tolya.
In the next t lines output pairs of space-separated lowercase English letters — spells that Tolya's grandmother should buy. Spells and letters in spells can be printed in any order.
If there are many optimal answers, output any.
Examples
Input
3
abb
dad
Output
2
a d
b a
Input
8
drpepper
cocacola
Output
7
l e
e d
d c
c p
p o
o r
r a
Note
In first example it's enough to buy two spells: ('a','d') and ('b','a'). Then first letters will coincide when we will replace letter 'a' with 'd'. Second letters will coincide when we will replace 'b' with 'a'. Third letters will coincide when we will at first replace 'b' with 'a' and then 'a' with 'd'.
Submitted Solution:
```
from sys import stdin
def dfs(x, res):
if vis[x]:
return
vis[x] = True
res.append(x)
for y in g[x]:
dfs(y, res)
n = int(stdin.readline())
s = stdin.readline()
t = stdin.readline()
vis = [False] * 26
g = [set() for _ in range(26)]
for i in range(n):
if s[i] == t[i]:
continue
x = ord(s[i]) - ord('a')
y = ord(t[i]) - ord('a')
g[x].add(y)
g[y].add(x)
ans = []
for x in range(26):
if not vis[x]:
res = []
dfs(x, res)
for i in range(1, len(res)):
x = chr(res[0] + ord('a'))
y = chr(res[i] + ord('a'))
ans.append((x, y))
print(len(ans))
for i in ans:
print(*i)
```
|
instruction
| 0
| 23,521
| 2
| 47,042
|
Yes
|
output
| 1
| 23,521
| 2
| 47,043
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valya and Tolya are an ideal pair, but they quarrel sometimes. Recently, Valya took offense at her boyfriend because he came to her in t-shirt with lettering that differs from lettering on her pullover. Now she doesn't want to see him and Tolya is seating at his room and crying at her photos all day long.
This story could be very sad but fairy godmother (Tolya's grandmother) decided to help them and restore their relationship. She secretly took Tolya's t-shirt and Valya's pullover and wants to make the letterings on them same. In order to do this, for one unit of mana she can buy a spell that can change some letters on the clothes. Your task is calculate the minimum amount of mana that Tolya's grandmother should spend to rescue love of Tolya and Valya.
More formally, letterings on Tolya's t-shirt and Valya's pullover are two strings with same length n consisting only of lowercase English letters. Using one unit of mana, grandmother can buy a spell of form (c1, c2) (where c1 and c2 are some lowercase English letters), which can arbitrary number of times transform a single letter c1 to c2 and vise-versa on both Tolya's t-shirt and Valya's pullover. You should find the minimum amount of mana that grandmother should spend to buy a set of spells that can make the letterings equal. In addition you should output the required set of spells.
Input
The first line contains a single integer n (1 ≤ n ≤ 105) — the length of the letterings.
The second line contains a string with length n, consisting of lowercase English letters — the lettering on Valya's pullover.
The third line contains the lettering on Tolya's t-shirt in the same format.
Output
In the first line output a single integer — the minimum amount of mana t required for rescuing love of Valya and Tolya.
In the next t lines output pairs of space-separated lowercase English letters — spells that Tolya's grandmother should buy. Spells and letters in spells can be printed in any order.
If there are many optimal answers, output any.
Examples
Input
3
abb
dad
Output
2
a d
b a
Input
8
drpepper
cocacola
Output
7
l e
e d
d c
c p
p o
o r
r a
Note
In first example it's enough to buy two spells: ('a','d') and ('b','a'). Then first letters will coincide when we will replace letter 'a' with 'd'. Second letters will coincide when we will replace 'b' with 'a'. Third letters will coincide when we will at first replace 'b' with 'a' and then 'a' with 'd'.
Submitted Solution:
```
x = input()
fw = sorted(set(input()))
sw = sorted(set(input()))
bound = sorted(set(fw+sw))
print(len(bound)-1)
for x in range(0, len(bound)-1):
print(bound[x], bound[x+1])
```
|
instruction
| 0
| 23,522
| 2
| 47,044
|
No
|
output
| 1
| 23,522
| 2
| 47,045
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valya and Tolya are an ideal pair, but they quarrel sometimes. Recently, Valya took offense at her boyfriend because he came to her in t-shirt with lettering that differs from lettering on her pullover. Now she doesn't want to see him and Tolya is seating at his room and crying at her photos all day long.
This story could be very sad but fairy godmother (Tolya's grandmother) decided to help them and restore their relationship. She secretly took Tolya's t-shirt and Valya's pullover and wants to make the letterings on them same. In order to do this, for one unit of mana she can buy a spell that can change some letters on the clothes. Your task is calculate the minimum amount of mana that Tolya's grandmother should spend to rescue love of Tolya and Valya.
More formally, letterings on Tolya's t-shirt and Valya's pullover are two strings with same length n consisting only of lowercase English letters. Using one unit of mana, grandmother can buy a spell of form (c1, c2) (where c1 and c2 are some lowercase English letters), which can arbitrary number of times transform a single letter c1 to c2 and vise-versa on both Tolya's t-shirt and Valya's pullover. You should find the minimum amount of mana that grandmother should spend to buy a set of spells that can make the letterings equal. In addition you should output the required set of spells.
Input
The first line contains a single integer n (1 ≤ n ≤ 105) — the length of the letterings.
The second line contains a string with length n, consisting of lowercase English letters — the lettering on Valya's pullover.
The third line contains the lettering on Tolya's t-shirt in the same format.
Output
In the first line output a single integer — the minimum amount of mana t required for rescuing love of Valya and Tolya.
In the next t lines output pairs of space-separated lowercase English letters — spells that Tolya's grandmother should buy. Spells and letters in spells can be printed in any order.
If there are many optimal answers, output any.
Examples
Input
3
abb
dad
Output
2
a d
b a
Input
8
drpepper
cocacola
Output
7
l e
e d
d c
c p
p o
o r
r a
Note
In first example it's enough to buy two spells: ('a','d') and ('b','a'). Then first letters will coincide when we will replace letter 'a' with 'd'. Second letters will coincide when we will replace 'b' with 'a'. Third letters will coincide when we will at first replace 'b' with 'a' and then 'a' with 'd'.
Submitted Solution:
```
def check(a,b,arr):
column=ord(b)-97
row=[]
for i in range(26):
if(arr[i][column]):
row.append(i)
rowb=[]
for i in range(26):
if(arr[ord(a)-97][i]):
rowb.append(i)
for i in row:
for j in rowb:
if(i==j):
return 1
x=ord(a)-97
arr[x][column]=1
arr[column][x]=1
return 0
n=int(input())
a=input()
b=input()
arr=[]
for i in range(26):
arr.append([])
alist=[]
for i in range(26):
temp=[]
for j in range(26):
if(i==j):
temp.append(1)
else:
temp.append(0)
alist.append(temp)
ans=[]
t=0
for i in range(n):
if(a[i]!=b[i]):
if(not check(a[i],b[i],alist)):
t+=1
ans.append([a[i],b[i]])
else:
alist[ord(a[i])-97][ord(b[i])-97]=1
alist[ord(b[i])-97][ord(a[i])-97]=1
print(t)
for i in ans:
print(i[0],i[1])
```
|
instruction
| 0
| 23,523
| 2
| 47,046
|
No
|
output
| 1
| 23,523
| 2
| 47,047
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valya and Tolya are an ideal pair, but they quarrel sometimes. Recently, Valya took offense at her boyfriend because he came to her in t-shirt with lettering that differs from lettering on her pullover. Now she doesn't want to see him and Tolya is seating at his room and crying at her photos all day long.
This story could be very sad but fairy godmother (Tolya's grandmother) decided to help them and restore their relationship. She secretly took Tolya's t-shirt and Valya's pullover and wants to make the letterings on them same. In order to do this, for one unit of mana she can buy a spell that can change some letters on the clothes. Your task is calculate the minimum amount of mana that Tolya's grandmother should spend to rescue love of Tolya and Valya.
More formally, letterings on Tolya's t-shirt and Valya's pullover are two strings with same length n consisting only of lowercase English letters. Using one unit of mana, grandmother can buy a spell of form (c1, c2) (where c1 and c2 are some lowercase English letters), which can arbitrary number of times transform a single letter c1 to c2 and vise-versa on both Tolya's t-shirt and Valya's pullover. You should find the minimum amount of mana that grandmother should spend to buy a set of spells that can make the letterings equal. In addition you should output the required set of spells.
Input
The first line contains a single integer n (1 ≤ n ≤ 105) — the length of the letterings.
The second line contains a string with length n, consisting of lowercase English letters — the lettering on Valya's pullover.
The third line contains the lettering on Tolya's t-shirt in the same format.
Output
In the first line output a single integer — the minimum amount of mana t required for rescuing love of Valya and Tolya.
In the next t lines output pairs of space-separated lowercase English letters — spells that Tolya's grandmother should buy. Spells and letters in spells can be printed in any order.
If there are many optimal answers, output any.
Examples
Input
3
abb
dad
Output
2
a d
b a
Input
8
drpepper
cocacola
Output
7
l e
e d
d c
c p
p o
o r
r a
Note
In first example it's enough to buy two spells: ('a','d') and ('b','a'). Then first letters will coincide when we will replace letter 'a' with 'd'. Second letters will coincide when we will replace 'b' with 'a'. Third letters will coincide when we will at first replace 'b' with 'a' and then 'a' with 'd'.
Submitted Solution:
```
def wordcount(a,b):
c=0
for i in range(0,len(a)):
if(b==a[i]):
c=c+1
return c
def applyChange(a,ar):
news=""
for i in range(0,len(a)):
if(a[i]==ar[0]):
news=news+ar[1]
else:
news=news+a[i]
return news
def newApplychange(dastrongs,ar):
news=[]
news1=""
news2=""
for i in range(0,len(dastrongs[0])):
if(((dastrongs[0])[i]!=(dastrongs[1])[i]) and ((dastrongs[0])[i]==ar[0] and (dastrongs[1])[i]==ar[1])):
news1=news1+ar[1]
else:
news1=news1+(dastrongs[0])[i]
for j in range(0,len(dastrongs[1])):
if(((dastrongs[0])[j]!=(dastrongs[1])[j]) and ((dastrongs[1])[j]==ar[0] and (dastrongs[0])[j]==ar[1])):
news2=news2+ar[1]
else:
news2=news2+(dastrongs[1])[j]
news.append(news1)
news.append(news2)
return news
n=int(input())
Valya=input()
Tolya=input()
spells=[]
for i in range(0,n):
if(Valya[i]!=Tolya[i]):
dual=""+Valya+Tolya
v=wordcount(dual,Valya[i])
t=wordcount(dual,Tolya[i])
if(v>=t):
u=[Valya[i],Tolya[i]]
spells.append(u)
#Valya=applyChange(Valya,u)
#Tolya=applyChange(Tolya,u)
e=newApplychange([Valya,Tolya],u)
Valya=e[0]
Tolya=e[1]
else:
w=[Tolya[i], Valya[i]]
spells.append(w)
#Valya=applyChange(Valya,w)
#Tolya=applyChange(Tolya,w)
e=newApplychange([Valya,Tolya],w)
Valya=e[0]
Tolya=e[1]
print(len(spells))
for j in range(0,len(spells)):
print(str((spells[j])[0])+" "+str((spells[j])[1]))
```
|
instruction
| 0
| 23,524
| 2
| 47,048
|
No
|
output
| 1
| 23,524
| 2
| 47,049
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Valya and Tolya are an ideal pair, but they quarrel sometimes. Recently, Valya took offense at her boyfriend because he came to her in t-shirt with lettering that differs from lettering on her pullover. Now she doesn't want to see him and Tolya is seating at his room and crying at her photos all day long.
This story could be very sad but fairy godmother (Tolya's grandmother) decided to help them and restore their relationship. She secretly took Tolya's t-shirt and Valya's pullover and wants to make the letterings on them same. In order to do this, for one unit of mana she can buy a spell that can change some letters on the clothes. Your task is calculate the minimum amount of mana that Tolya's grandmother should spend to rescue love of Tolya and Valya.
More formally, letterings on Tolya's t-shirt and Valya's pullover are two strings with same length n consisting only of lowercase English letters. Using one unit of mana, grandmother can buy a spell of form (c1, c2) (where c1 and c2 are some lowercase English letters), which can arbitrary number of times transform a single letter c1 to c2 and vise-versa on both Tolya's t-shirt and Valya's pullover. You should find the minimum amount of mana that grandmother should spend to buy a set of spells that can make the letterings equal. In addition you should output the required set of spells.
Input
The first line contains a single integer n (1 ≤ n ≤ 105) — the length of the letterings.
The second line contains a string with length n, consisting of lowercase English letters — the lettering on Valya's pullover.
The third line contains the lettering on Tolya's t-shirt in the same format.
Output
In the first line output a single integer — the minimum amount of mana t required for rescuing love of Valya and Tolya.
In the next t lines output pairs of space-separated lowercase English letters — spells that Tolya's grandmother should buy. Spells and letters in spells can be printed in any order.
If there are many optimal answers, output any.
Examples
Input
3
abb
dad
Output
2
a d
b a
Input
8
drpepper
cocacola
Output
7
l e
e d
d c
c p
p o
o r
r a
Note
In first example it's enough to buy two spells: ('a','d') and ('b','a'). Then first letters will coincide when we will replace letter 'a' with 'd'. Second letters will coincide when we will replace 'b' with 'a'. Third letters will coincide when we will at first replace 'b' with 'a' and then 'a' with 'd'.
Submitted Solution:
```
n, a, b = int(input()), input(), input()
sa, sb = [], []
for i in range(26):
sa.append(set())
sb.append(set())
sa, sb = tuple(sa), tuple(sb)
ans = []
count = 0
for i in range(n):
if a[i] != b[i]:
if not (b[i] in sa[ord(a[i]) - 97] and a[i] in sb[ord(b[i]) - 97]):
intersection = sa[ord(a[i]) - 97].intersection(sb[ord(b[i]) - 97])
if len(intersection) == 0:
ans.append((a[i], b[i]))
count += 1
sa[ord(a[i]) - 97].add(b[i])
sb[ord(b[i]) - 97].add(a[i])
print(count)
for each in ans:
print(*each)
```
|
instruction
| 0
| 23,525
| 2
| 47,050
|
No
|
output
| 1
| 23,525
| 2
| 47,051
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations.
Initially, there are n superheroes in avengers team having powers a_1, a_2, …, a_n, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by 1. They can do at most m operations. Also, on a particular superhero at most k operations can be done.
Can you help the avengers team to maximize the average power of their crew?
Input
The first line contains three integers n, k and m (1 ≤ n ≤ 10^{5}, 1 ≤ k ≤ 10^{5}, 1 ≤ m ≤ 10^{7}) — the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^{6}) — the initial powers of the superheroes in the cast of avengers.
Output
Output a single number — the maximum final average power.
Your answer is considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
2 4 6
4 7
Output
11.00000000000000000000
Input
4 2 6
1 3 2 3
Output
5.00000000000000000000
Note
In the first example, the maximum average is obtained by deleting the first element and increasing the second element four times.
In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by 2 each.
|
instruction
| 0
| 23,897
| 2
| 47,794
|
Tags: brute force, implementation, math
Correct Solution:
```
n, k, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse = True)
if n == 1:
print(a[0] + min(m, k))
exit()
pr = [0 for i in range(n + 1)]
pr[0] = 0
mx = 0
eps = 0.0000001
for i in range(1, n + 1):
pr[i] = pr[i - 1] + a[i - 1]
for c in range(max(n - m, 1), n + 1):
l = n - c
if ((pr[c] + min(c * k, m - n + c)) / c) - mx > eps:
mx = (pr[c] + min(c * k, m - l)) / c
print(mx)
```
|
output
| 1
| 23,897
| 2
| 47,795
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations.
Initially, there are n superheroes in avengers team having powers a_1, a_2, …, a_n, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by 1. They can do at most m operations. Also, on a particular superhero at most k operations can be done.
Can you help the avengers team to maximize the average power of their crew?
Input
The first line contains three integers n, k and m (1 ≤ n ≤ 10^{5}, 1 ≤ k ≤ 10^{5}, 1 ≤ m ≤ 10^{7}) — the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^{6}) — the initial powers of the superheroes in the cast of avengers.
Output
Output a single number — the maximum final average power.
Your answer is considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
2 4 6
4 7
Output
11.00000000000000000000
Input
4 2 6
1 3 2 3
Output
5.00000000000000000000
Note
In the first example, the maximum average is obtained by deleting the first element and increasing the second element four times.
In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by 2 each.
|
instruction
| 0
| 23,898
| 2
| 47,796
|
Tags: brute force, implementation, math
Correct Solution:
```
n, k, m = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
hau = sum(A)
heks = (sum(A) + min(m, n*k)) / n
for i in range(n - 1):
if m > 0:
m -= 1
hau -= A[i]
heks = max(heks, (hau + min(m, (n - 1 - i) * k)) / (n - i - 1))
print(heks)
```
|
output
| 1
| 23,898
| 2
| 47,797
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations.
Initially, there are n superheroes in avengers team having powers a_1, a_2, …, a_n, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by 1. They can do at most m operations. Also, on a particular superhero at most k operations can be done.
Can you help the avengers team to maximize the average power of their crew?
Input
The first line contains three integers n, k and m (1 ≤ n ≤ 10^{5}, 1 ≤ k ≤ 10^{5}, 1 ≤ m ≤ 10^{7}) — the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^{6}) — the initial powers of the superheroes in the cast of avengers.
Output
Output a single number — the maximum final average power.
Your answer is considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
2 4 6
4 7
Output
11.00000000000000000000
Input
4 2 6
1 3 2 3
Output
5.00000000000000000000
Note
In the first example, the maximum average is obtained by deleting the first element and increasing the second element four times.
In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by 2 each.
|
instruction
| 0
| 23,899
| 2
| 47,798
|
Tags: brute force, implementation, math
Correct Solution:
```
#------------------------------warmup----------------------------
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
#-------------------game starts now-----------------------------------------------------
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
a.sort(reverse=True)
s=[]+a
if m*n<k:
s=a[:max(1,n-k+m*n)]
s1=(sum(s)+min(m*len(s),k))/len(s)
if k>=n-1:
ind=n-1-a[::-1].index(max(a))
l=a[:max(n-k,ind+1)]
#print(l)
sd=min(m*len(l),k-(n-len(l)))
#print(sd)
l1=a[0]
k-=n-1
l1+=min(k,m)
print(max(s1,l1,(sum(l)+sd)/len(l)))
else:
ind=n-1-a[::-1].index(max(a))
l=a[:max(n-k,ind+1)]
#print(l)
sd=k-(n-len(l))
#print(sd)
l1=a[:n-k]
s2=sum(l1)/(n-k)
print(max(s1,s2,(sum(l)+sd)/len(l)))
```
|
output
| 1
| 23,899
| 2
| 47,799
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations.
Initially, there are n superheroes in avengers team having powers a_1, a_2, …, a_n, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by 1. They can do at most m operations. Also, on a particular superhero at most k operations can be done.
Can you help the avengers team to maximize the average power of their crew?
Input
The first line contains three integers n, k and m (1 ≤ n ≤ 10^{5}, 1 ≤ k ≤ 10^{5}, 1 ≤ m ≤ 10^{7}) — the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^{6}) — the initial powers of the superheroes in the cast of avengers.
Output
Output a single number — the maximum final average power.
Your answer is considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
2 4 6
4 7
Output
11.00000000000000000000
Input
4 2 6
1 3 2 3
Output
5.00000000000000000000
Note
In the first example, the maximum average is obtained by deleting the first element and increasing the second element four times.
In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by 2 each.
|
instruction
| 0
| 23,900
| 2
| 47,800
|
Tags: brute force, implementation, math
Correct Solution:
```
n,k,m=map(int,input().split())
a=list(map(int,input().split()))
a=sorted(a)
s=sum(a)
ans=0
for i in range(min(n,m+1)):
s+=min(k*(n-i),m-i)
ans=max(ans,s/(n-i))
s-=min(k*(n-i),m-i)
s-=a[i]
print(ans)
```
|
output
| 1
| 23,900
| 2
| 47,801
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations.
Initially, there are n superheroes in avengers team having powers a_1, a_2, …, a_n, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by 1. They can do at most m operations. Also, on a particular superhero at most k operations can be done.
Can you help the avengers team to maximize the average power of their crew?
Input
The first line contains three integers n, k and m (1 ≤ n ≤ 10^{5}, 1 ≤ k ≤ 10^{5}, 1 ≤ m ≤ 10^{7}) — the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^{6}) — the initial powers of the superheroes in the cast of avengers.
Output
Output a single number — the maximum final average power.
Your answer is considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
2 4 6
4 7
Output
11.00000000000000000000
Input
4 2 6
1 3 2 3
Output
5.00000000000000000000
Note
In the first example, the maximum average is obtained by deleting the first element and increasing the second element four times.
In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by 2 each.
|
instruction
| 0
| 23,901
| 2
| 47,802
|
Tags: brute force, implementation, math
Correct Solution:
```
n,k,m=map(int,input().split())
a=list(map(int,input().split()))
Sum=sum(a)
max_avg=Sum/n
a=sorted(a)
#print(a)
if m>n:
if m-n>=k and max_avg<a[-1]+k:
max_avg=a[-1]+k
else:
max_avg=a[-1]+m-n+1
elif m==n:
if max_avg<a[-1]+1:
max_avg=a[-1]+1
else:
if max_avg<(Sum+m)/(n):
max_avg=(Sum+m)/n
for i in range(m):
Sum-=a[i]
max_avg=max(max_avg,(Sum+min(m-i-1,k*(n-1-i)))/(n-i-1))
print(max_avg)
```
|
output
| 1
| 23,901
| 2
| 47,803
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations.
Initially, there are n superheroes in avengers team having powers a_1, a_2, …, a_n, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by 1. They can do at most m operations. Also, on a particular superhero at most k operations can be done.
Can you help the avengers team to maximize the average power of their crew?
Input
The first line contains three integers n, k and m (1 ≤ n ≤ 10^{5}, 1 ≤ k ≤ 10^{5}, 1 ≤ m ≤ 10^{7}) — the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^{6}) — the initial powers of the superheroes in the cast of avengers.
Output
Output a single number — the maximum final average power.
Your answer is considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
2 4 6
4 7
Output
11.00000000000000000000
Input
4 2 6
1 3 2 3
Output
5.00000000000000000000
Note
In the first example, the maximum average is obtained by deleting the first element and increasing the second element four times.
In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by 2 each.
|
instruction
| 0
| 23,902
| 2
| 47,804
|
Tags: brute force, implementation, math
Correct Solution:
```
n,k,m=map(int,input().split())
a=sorted(list(map(int,input().split())))
av=(sum(a)+min(n*k,m))/n
r=0
l=sum(a)
for i in range(min(n-1,m)):
r+=a[i]
av=max(av,(l-r+min(m-i-1,(n-i-1)*k))/(n-i-1))
print(av)
```
|
output
| 1
| 23,902
| 2
| 47,805
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations.
Initially, there are n superheroes in avengers team having powers a_1, a_2, …, a_n, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by 1. They can do at most m operations. Also, on a particular superhero at most k operations can be done.
Can you help the avengers team to maximize the average power of their crew?
Input
The first line contains three integers n, k and m (1 ≤ n ≤ 10^{5}, 1 ≤ k ≤ 10^{5}, 1 ≤ m ≤ 10^{7}) — the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^{6}) — the initial powers of the superheroes in the cast of avengers.
Output
Output a single number — the maximum final average power.
Your answer is considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
2 4 6
4 7
Output
11.00000000000000000000
Input
4 2 6
1 3 2 3
Output
5.00000000000000000000
Note
In the first example, the maximum average is obtained by deleting the first element and increasing the second element four times.
In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by 2 each.
|
instruction
| 0
| 23,903
| 2
| 47,806
|
Tags: brute force, implementation, math
Correct Solution:
```
n,k,m=map(int,input().split())
A=sorted(list(map(int,input().split())))
list.reverse(A)
Sum=.0; Ans=.0
for i in range(n):
Sum+=A[i]
t=n-i-1 #deled
if m>=n-i-1:
Ans=max(Ans,(Sum+min(m-t,(i+1)*k))/(i+1))
print(Ans)
```
|
output
| 1
| 23,903
| 2
| 47,807
|
Provide tags and a correct Python 3 solution for this coding contest problem.
Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations.
Initially, there are n superheroes in avengers team having powers a_1, a_2, …, a_n, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by 1. They can do at most m operations. Also, on a particular superhero at most k operations can be done.
Can you help the avengers team to maximize the average power of their crew?
Input
The first line contains three integers n, k and m (1 ≤ n ≤ 10^{5}, 1 ≤ k ≤ 10^{5}, 1 ≤ m ≤ 10^{7}) — the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^{6}) — the initial powers of the superheroes in the cast of avengers.
Output
Output a single number — the maximum final average power.
Your answer is considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
2 4 6
4 7
Output
11.00000000000000000000
Input
4 2 6
1 3 2 3
Output
5.00000000000000000000
Note
In the first example, the maximum average is obtained by deleting the first element and increasing the second element four times.
In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by 2 each.
|
instruction
| 0
| 23,904
| 2
| 47,808
|
Tags: brute force, implementation, math
Correct Solution:
```
#In The Name Of GOD
n, k, m = map(int, input().split())
a = [int(s) for s in input().split()]
a.sort()
q = sum(a)
ans = 0
for i in range(min(n, m + 1)):
if m - i >= (n - i) * k:
ans = max(ans, (q + (n - i) * k) / (n - i))
else:
ans = max(ans, (q + m - i) / (n - i))
q -= a[i]
print(ans)
```
|
output
| 1
| 23,904
| 2
| 47,809
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations.
Initially, there are n superheroes in avengers team having powers a_1, a_2, …, a_n, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by 1. They can do at most m operations. Also, on a particular superhero at most k operations can be done.
Can you help the avengers team to maximize the average power of their crew?
Input
The first line contains three integers n, k and m (1 ≤ n ≤ 10^{5}, 1 ≤ k ≤ 10^{5}, 1 ≤ m ≤ 10^{7}) — the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^{6}) — the initial powers of the superheroes in the cast of avengers.
Output
Output a single number — the maximum final average power.
Your answer is considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
2 4 6
4 7
Output
11.00000000000000000000
Input
4 2 6
1 3 2 3
Output
5.00000000000000000000
Note
In the first example, the maximum average is obtained by deleting the first element and increasing the second element four times.
In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by 2 each.
Submitted Solution:
```
n,k,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
ans=sum(a)
r=0
for i in range(n):
if(i<=m):
r=max(r,(ans+min(k*(n-i),m-i))/(n-i))
ans-=a[i];
print(r)
```
|
instruction
| 0
| 23,905
| 2
| 47,810
|
Yes
|
output
| 1
| 23,905
| 2
| 47,811
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations.
Initially, there are n superheroes in avengers team having powers a_1, a_2, …, a_n, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by 1. They can do at most m operations. Also, on a particular superhero at most k operations can be done.
Can you help the avengers team to maximize the average power of their crew?
Input
The first line contains three integers n, k and m (1 ≤ n ≤ 10^{5}, 1 ≤ k ≤ 10^{5}, 1 ≤ m ≤ 10^{7}) — the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^{6}) — the initial powers of the superheroes in the cast of avengers.
Output
Output a single number — the maximum final average power.
Your answer is considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
2 4 6
4 7
Output
11.00000000000000000000
Input
4 2 6
1 3 2 3
Output
5.00000000000000000000
Note
In the first example, the maximum average is obtained by deleting the first element and increasing the second element four times.
In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by 2 each.
Submitted Solution:
```
import sys
n, k, m = [int(tmp) for tmp in sys.stdin.readline().split()]
a = [int(tmp) for tmp in sys.stdin.readline().split()]
a.sort(reverse=True)
sa = sum(a)
mr = m
nr = n
sar = sa
while nr >= 2 and mr != 0:
avsidau = (sar - a[nr-1] + min([mr-1, (nr-1)*k])) / (nr-1)
avsiu = (sar + min([nr*k, mr])) / nr
if avsidau > avsiu:
mr -= 1
nr -= 1
sar -= a[nr]
else:
break
sar += min([nr*k, mr])
avs = sar / nr
print(avs)
```
|
instruction
| 0
| 23,906
| 2
| 47,812
|
Yes
|
output
| 1
| 23,906
| 2
| 47,813
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations.
Initially, there are n superheroes in avengers team having powers a_1, a_2, …, a_n, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by 1. They can do at most m operations. Also, on a particular superhero at most k operations can be done.
Can you help the avengers team to maximize the average power of their crew?
Input
The first line contains three integers n, k and m (1 ≤ n ≤ 10^{5}, 1 ≤ k ≤ 10^{5}, 1 ≤ m ≤ 10^{7}) — the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^{6}) — the initial powers of the superheroes in the cast of avengers.
Output
Output a single number — the maximum final average power.
Your answer is considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
2 4 6
4 7
Output
11.00000000000000000000
Input
4 2 6
1 3 2 3
Output
5.00000000000000000000
Note
In the first example, the maximum average is obtained by deleting the first element and increasing the second element four times.
In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by 2 each.
Submitted Solution:
```
n,k,m=map(int,input().split())
a=list(map(int,input().split()))
a=sorted(a)
s=sum(a)
if m<=k*n:
ans=(s+m)/n
else:
ans=(s+k*n)/n
for i in range(min(n-1,m)):
s=s-a[i]
m-=1
if m<=k*(n-i-1):
t=(s+m)/(n-i-1)
else:
t=(s+k*(n-i-1))/(n-i-1)
if t>ans:
ans=t
print(ans)
```
|
instruction
| 0
| 23,907
| 2
| 47,814
|
Yes
|
output
| 1
| 23,907
| 2
| 47,815
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations.
Initially, there are n superheroes in avengers team having powers a_1, a_2, …, a_n, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by 1. They can do at most m operations. Also, on a particular superhero at most k operations can be done.
Can you help the avengers team to maximize the average power of their crew?
Input
The first line contains three integers n, k and m (1 ≤ n ≤ 10^{5}, 1 ≤ k ≤ 10^{5}, 1 ≤ m ≤ 10^{7}) — the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^{6}) — the initial powers of the superheroes in the cast of avengers.
Output
Output a single number — the maximum final average power.
Your answer is considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
2 4 6
4 7
Output
11.00000000000000000000
Input
4 2 6
1 3 2 3
Output
5.00000000000000000000
Note
In the first example, the maximum average is obtained by deleting the first element and increasing the second element four times.
In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by 2 each.
Submitted Solution:
```
n, k, m = map(int, input().split())
a = sorted(map(int, input().split()))
s = sum(a)
ans = 0.0
for i in range(n) :
ans = max(ans, (s + min((n - i) * k, m)) / (n - i))
s -= a[i]
if m == 0 : break
m -= 1
print('%.8f' % ans)
```
|
instruction
| 0
| 23,908
| 2
| 47,816
|
Yes
|
output
| 1
| 23,908
| 2
| 47,817
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations.
Initially, there are n superheroes in avengers team having powers a_1, a_2, …, a_n, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by 1. They can do at most m operations. Also, on a particular superhero at most k operations can be done.
Can you help the avengers team to maximize the average power of their crew?
Input
The first line contains three integers n, k and m (1 ≤ n ≤ 10^{5}, 1 ≤ k ≤ 10^{5}, 1 ≤ m ≤ 10^{7}) — the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^{6}) — the initial powers of the superheroes in the cast of avengers.
Output
Output a single number — the maximum final average power.
Your answer is considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
2 4 6
4 7
Output
11.00000000000000000000
Input
4 2 6
1 3 2 3
Output
5.00000000000000000000
Note
In the first example, the maximum average is obtained by deleting the first element and increasing the second element four times.
In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by 2 each.
Submitted Solution:
```
n, k, m = [int(s) for s in input().split()]
a = [int(s) for s in input().split()]
a.sort()
b = []
operation_used = 0
summ = 0
all = len(a)
for elem in a:
summ += elem
i = 0
while operation_used < m:
if i < len(a) and all - 1 > 0 and (summ - a[i])/(all-1) >= (summ+1)/all:
summ = summ - a[i]
all -= 1
operation_used += 1
else:
keks = m - operation_used
if keks >= all*k:
summ += all*k
break
else:
summ += keks
break
i += 1
print(summ/all)
```
|
instruction
| 0
| 23,909
| 2
| 47,818
|
No
|
output
| 1
| 23,909
| 2
| 47,819
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations.
Initially, there are n superheroes in avengers team having powers a_1, a_2, …, a_n, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by 1. They can do at most m operations. Also, on a particular superhero at most k operations can be done.
Can you help the avengers team to maximize the average power of their crew?
Input
The first line contains three integers n, k and m (1 ≤ n ≤ 10^{5}, 1 ≤ k ≤ 10^{5}, 1 ≤ m ≤ 10^{7}) — the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^{6}) — the initial powers of the superheroes in the cast of avengers.
Output
Output a single number — the maximum final average power.
Your answer is considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
2 4 6
4 7
Output
11.00000000000000000000
Input
4 2 6
1 3 2 3
Output
5.00000000000000000000
Note
In the first example, the maximum average is obtained by deleting the first element and increasing the second element four times.
In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by 2 each.
Submitted Solution:
```
nl,k,m=map(int,input().split())
a=list(map(int,input().split()))
d=max(a)
a.sort()
c=0
for i in range(len(a)):
if a[i]!=d:
m=m-1
a[i]=0
else:
c=c+1
if m==0:
break
a=a[::-1]
if m==0:
print(sum(a[:len(a)-m])/(len(a)-m))
else:
for i in range(len(a)):
if a[i]>0:
a[i]=a[i]+min(m,k)
m=m-k
if m<=0:
break
print(sum(a)/c)
```
|
instruction
| 0
| 23,910
| 2
| 47,820
|
No
|
output
| 1
| 23,910
| 2
| 47,821
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations.
Initially, there are n superheroes in avengers team having powers a_1, a_2, …, a_n, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by 1. They can do at most m operations. Also, on a particular superhero at most k operations can be done.
Can you help the avengers team to maximize the average power of their crew?
Input
The first line contains three integers n, k and m (1 ≤ n ≤ 10^{5}, 1 ≤ k ≤ 10^{5}, 1 ≤ m ≤ 10^{7}) — the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^{6}) — the initial powers of the superheroes in the cast of avengers.
Output
Output a single number — the maximum final average power.
Your answer is considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
2 4 6
4 7
Output
11.00000000000000000000
Input
4 2 6
1 3 2 3
Output
5.00000000000000000000
Note
In the first example, the maximum average is obtained by deleting the first element and increasing the second element four times.
In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by 2 each.
Submitted Solution:
```
a = [int(i) for i in input().split()]
n = a[0]
k = a[1]
m = a[2]
a = [int(i) for i in input().split()]
sum1 = sum(a)
def boost(q, energy):
max_boost = (energy//q)*q
if max_boost > k*q :
max_boost = k*q
return max_boost
max_boost = boost(n, m)
average = (sum1 + max_boost)/n
d = n
energy = m
for i in range(n):
if d - 1 != 0 and energy - 1 != 0:
max_boost = boost(d-1, energy - 1)
tmp = (sum1 - a[i] + max_boost)/(d-1)
if average < tmp:
energy -= 1
d -= 1
sum1 -= a[i]
average = tmp
print(average)
```
|
instruction
| 0
| 23,911
| 2
| 47,822
|
No
|
output
| 1
| 23,911
| 2
| 47,823
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations.
Initially, there are n superheroes in avengers team having powers a_1, a_2, …, a_n, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by 1. They can do at most m operations. Also, on a particular superhero at most k operations can be done.
Can you help the avengers team to maximize the average power of their crew?
Input
The first line contains three integers n, k and m (1 ≤ n ≤ 10^{5}, 1 ≤ k ≤ 10^{5}, 1 ≤ m ≤ 10^{7}) — the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^{6}) — the initial powers of the superheroes in the cast of avengers.
Output
Output a single number — the maximum final average power.
Your answer is considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≤ 10^{-6}.
Examples
Input
2 4 6
4 7
Output
11.00000000000000000000
Input
4 2 6
1 3 2 3
Output
5.00000000000000000000
Note
In the first example, the maximum average is obtained by deleting the first element and increasing the second element four times.
In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by 2 each.
Submitted Solution:
```
n, k, m=map(int,input().split())
l=list(map(int,input().split()))
l.sort()
s=sum(l)
maxavg=(s+min(m, (n)*k))/n
for i in range(min(n-1,m+1)):
s=s-l[i]
newsum=s+min(m-i-1, (n-i-1)*k)
newavg=newsum/(n-i-1)
#print(newavg)
maxavg=max(maxavg,newavg)
print(maxavg)
```
|
instruction
| 0
| 23,912
| 2
| 47,824
|
No
|
output
| 1
| 23,912
| 2
| 47,825
|
Provide tags and a correct Python 3 solution for this coding contest problem.
You are fighting with Zmei Gorynich — a ferocious monster from Slavic myths, a huge dragon-like reptile with multiple heads!
<image>
Initially Zmei Gorynich has x heads. You can deal n types of blows. If you deal a blow of the i-th type, you decrease the number of Gorynich's heads by min(d_i, curX), there curX is the current number of heads. But if after this blow Zmei Gorynich has at least one head, he grows h_i new heads. If curX = 0 then Gorynich is defeated.
You can deal each blow any number of times, in any order.
For example, if curX = 10, d = 7, h = 10 then the number of heads changes to 13 (you cut 7 heads off, but then Zmei grows 10 new ones), but if curX = 10, d = 11, h = 100 then number of heads changes to 0 and Zmei Gorynich is considered defeated.
Calculate the minimum number of blows to defeat Zmei Gorynich!
You have to answer t independent queries.
Input
The first line contains one integer t (1 ≤ t ≤ 100) – the number of queries.
The first line of each query contains two integers n and x (1 ≤ n ≤ 100, 1 ≤ x ≤ 10^9) — the number of possible types of blows and the number of heads Zmei initially has, respectively.
The following n lines of each query contain the descriptions of types of blows you can deal. The i-th line contains two integers d_i and h_i (1 ≤ d_i, h_i ≤ 10^9) — the description of the i-th blow.
Output
For each query print the minimum number of blows you have to deal to defeat Zmei Gorynich.
If Zmei Gorynuch cannot be defeated print -1.
Example
Input
3
3 10
6 3
8 2
1 4
4 10
4 1
3 2
2 6
1 100
2 15
10 11
14 100
Output
2
3
-1
Note
In the first query you can deal the first blow (after that the number of heads changes to 10 - 6 + 3 = 7), and then deal the second blow.
In the second query you just deal the first blow three times, and Zmei is defeated.
In third query you can not defeat Zmei Gorynich. Maybe it's better to convince it to stop fighting?
|
instruction
| 0
| 23,920
| 2
| 47,840
|
Tags: greedy, math
Correct Solution:
```
t = int(input())
for _ in range(t):
n,x = [int(i) for i in input().split()]
s = []
for _ in range(n):
s.append([int(i) for i in input().split()])
g = 0
j = 0
c = 0
for d,h in s:
if d-h>g:
g = d-h
if d>j:
j = d
f = x-j
if f<=0:
print(1)
else:
c+=1
if g==0:
print(-1)
else:
print((f+g-1)//g+c)
```
|
output
| 1
| 23,920
| 2
| 47,841
|
Provide tags and a correct Python 3 solution for this coding contest problem.
You are fighting with Zmei Gorynich — a ferocious monster from Slavic myths, a huge dragon-like reptile with multiple heads!
<image>
Initially Zmei Gorynich has x heads. You can deal n types of blows. If you deal a blow of the i-th type, you decrease the number of Gorynich's heads by min(d_i, curX), there curX is the current number of heads. But if after this blow Zmei Gorynich has at least one head, he grows h_i new heads. If curX = 0 then Gorynich is defeated.
You can deal each blow any number of times, in any order.
For example, if curX = 10, d = 7, h = 10 then the number of heads changes to 13 (you cut 7 heads off, but then Zmei grows 10 new ones), but if curX = 10, d = 11, h = 100 then number of heads changes to 0 and Zmei Gorynich is considered defeated.
Calculate the minimum number of blows to defeat Zmei Gorynich!
You have to answer t independent queries.
Input
The first line contains one integer t (1 ≤ t ≤ 100) – the number of queries.
The first line of each query contains two integers n and x (1 ≤ n ≤ 100, 1 ≤ x ≤ 10^9) — the number of possible types of blows and the number of heads Zmei initially has, respectively.
The following n lines of each query contain the descriptions of types of blows you can deal. The i-th line contains two integers d_i and h_i (1 ≤ d_i, h_i ≤ 10^9) — the description of the i-th blow.
Output
For each query print the minimum number of blows you have to deal to defeat Zmei Gorynich.
If Zmei Gorynuch cannot be defeated print -1.
Example
Input
3
3 10
6 3
8 2
1 4
4 10
4 1
3 2
2 6
1 100
2 15
10 11
14 100
Output
2
3
-1
Note
In the first query you can deal the first blow (after that the number of heads changes to 10 - 6 + 3 = 7), and then deal the second blow.
In the second query you just deal the first blow three times, and Zmei is defeated.
In third query you can not defeat Zmei Gorynich. Maybe it's better to convince it to stop fighting?
|
instruction
| 0
| 23,921
| 2
| 47,842
|
Tags: greedy, math
Correct Solution:
```
import sys
def line2ints():
return [int(x) for x in sys.stdin.readline().split()]
t = int(sys.stdin.readline())
for query in range(t):
n, x = line2ints()
deltas = []
max_blow = 0
for i in range(n):
d, h = line2ints()
deltas.append(d - h)
max_blow = max(max_blow, abs(d))
deltas = sorted([x for x in deltas if x > 0], reverse=True)
# print("n: %d, x: %d, deltas: %r" % (n, x, deltas))
if len(deltas) < 1:
print(1 if x <= max_blow else -1)
continue
if max_blow >= x:
print(1)
continue
ans = (x - max_blow) // deltas[0]
if (x - max_blow) % deltas[0] > 0: ans += 1
ans += 1
print(ans)
```
|
output
| 1
| 23,921
| 2
| 47,843
|
Provide tags and a correct Python 3 solution for this coding contest problem.
You are fighting with Zmei Gorynich — a ferocious monster from Slavic myths, a huge dragon-like reptile with multiple heads!
<image>
Initially Zmei Gorynich has x heads. You can deal n types of blows. If you deal a blow of the i-th type, you decrease the number of Gorynich's heads by min(d_i, curX), there curX is the current number of heads. But if after this blow Zmei Gorynich has at least one head, he grows h_i new heads. If curX = 0 then Gorynich is defeated.
You can deal each blow any number of times, in any order.
For example, if curX = 10, d = 7, h = 10 then the number of heads changes to 13 (you cut 7 heads off, but then Zmei grows 10 new ones), but if curX = 10, d = 11, h = 100 then number of heads changes to 0 and Zmei Gorynich is considered defeated.
Calculate the minimum number of blows to defeat Zmei Gorynich!
You have to answer t independent queries.
Input
The first line contains one integer t (1 ≤ t ≤ 100) – the number of queries.
The first line of each query contains two integers n and x (1 ≤ n ≤ 100, 1 ≤ x ≤ 10^9) — the number of possible types of blows and the number of heads Zmei initially has, respectively.
The following n lines of each query contain the descriptions of types of blows you can deal. The i-th line contains two integers d_i and h_i (1 ≤ d_i, h_i ≤ 10^9) — the description of the i-th blow.
Output
For each query print the minimum number of blows you have to deal to defeat Zmei Gorynich.
If Zmei Gorynuch cannot be defeated print -1.
Example
Input
3
3 10
6 3
8 2
1 4
4 10
4 1
3 2
2 6
1 100
2 15
10 11
14 100
Output
2
3
-1
Note
In the first query you can deal the first blow (after that the number of heads changes to 10 - 6 + 3 = 7), and then deal the second blow.
In the second query you just deal the first blow three times, and Zmei is defeated.
In third query you can not defeat Zmei Gorynich. Maybe it's better to convince it to stop fighting?
|
instruction
| 0
| 23,922
| 2
| 47,844
|
Tags: greedy, math
Correct Solution:
```
import math
def testcase():
n, x = map(int, input().split())
D = []
H = []
best_damage = 0
instant_kill = 0
for _ in range(n):
d, h = map(int, input().split())
D.append(d)
H.append(h)
best_damage = max(best_damage, d - h)
instant_kill = max(instant_kill, d)
if instant_kill < x and best_damage == 0:
return -1
x -= instant_kill
if x <= 0:
return 1
return math.ceil(x / best_damage) + 1
for _ in range(int(input())):
print(testcase())
```
|
output
| 1
| 23,922
| 2
| 47,845
|
Provide tags and a correct Python 3 solution for this coding contest problem.
You are fighting with Zmei Gorynich — a ferocious monster from Slavic myths, a huge dragon-like reptile with multiple heads!
<image>
Initially Zmei Gorynich has x heads. You can deal n types of blows. If you deal a blow of the i-th type, you decrease the number of Gorynich's heads by min(d_i, curX), there curX is the current number of heads. But if after this blow Zmei Gorynich has at least one head, he grows h_i new heads. If curX = 0 then Gorynich is defeated.
You can deal each blow any number of times, in any order.
For example, if curX = 10, d = 7, h = 10 then the number of heads changes to 13 (you cut 7 heads off, but then Zmei grows 10 new ones), but if curX = 10, d = 11, h = 100 then number of heads changes to 0 and Zmei Gorynich is considered defeated.
Calculate the minimum number of blows to defeat Zmei Gorynich!
You have to answer t independent queries.
Input
The first line contains one integer t (1 ≤ t ≤ 100) – the number of queries.
The first line of each query contains two integers n and x (1 ≤ n ≤ 100, 1 ≤ x ≤ 10^9) — the number of possible types of blows and the number of heads Zmei initially has, respectively.
The following n lines of each query contain the descriptions of types of blows you can deal. The i-th line contains two integers d_i and h_i (1 ≤ d_i, h_i ≤ 10^9) — the description of the i-th blow.
Output
For each query print the minimum number of blows you have to deal to defeat Zmei Gorynich.
If Zmei Gorynuch cannot be defeated print -1.
Example
Input
3
3 10
6 3
8 2
1 4
4 10
4 1
3 2
2 6
1 100
2 15
10 11
14 100
Output
2
3
-1
Note
In the first query you can deal the first blow (after that the number of heads changes to 10 - 6 + 3 = 7), and then deal the second blow.
In the second query you just deal the first blow three times, and Zmei is defeated.
In third query you can not defeat Zmei Gorynich. Maybe it's better to convince it to stop fighting?
|
instruction
| 0
| 23,923
| 2
| 47,846
|
Tags: greedy, math
Correct Solution:
```
import math
inf=10**12
for _ in range(int(input())):
n,k=map(int,input().split())
ma=-1
di=-inf
for i in range(n):
x,y=map(int,input().split())
di=max(di,x-y)
ma=max(ma,x)
if k<=ma:
print(1)
elif di<=0:
print(-1)
else:
print(math.ceil((k-ma)/di) + 1)
```
|
output
| 1
| 23,923
| 2
| 47,847
|
Provide tags and a correct Python 3 solution for this coding contest problem.
You are fighting with Zmei Gorynich — a ferocious monster from Slavic myths, a huge dragon-like reptile with multiple heads!
<image>
Initially Zmei Gorynich has x heads. You can deal n types of blows. If you deal a blow of the i-th type, you decrease the number of Gorynich's heads by min(d_i, curX), there curX is the current number of heads. But if after this blow Zmei Gorynich has at least one head, he grows h_i new heads. If curX = 0 then Gorynich is defeated.
You can deal each blow any number of times, in any order.
For example, if curX = 10, d = 7, h = 10 then the number of heads changes to 13 (you cut 7 heads off, but then Zmei grows 10 new ones), but if curX = 10, d = 11, h = 100 then number of heads changes to 0 and Zmei Gorynich is considered defeated.
Calculate the minimum number of blows to defeat Zmei Gorynich!
You have to answer t independent queries.
Input
The first line contains one integer t (1 ≤ t ≤ 100) – the number of queries.
The first line of each query contains two integers n and x (1 ≤ n ≤ 100, 1 ≤ x ≤ 10^9) — the number of possible types of blows and the number of heads Zmei initially has, respectively.
The following n lines of each query contain the descriptions of types of blows you can deal. The i-th line contains two integers d_i and h_i (1 ≤ d_i, h_i ≤ 10^9) — the description of the i-th blow.
Output
For each query print the minimum number of blows you have to deal to defeat Zmei Gorynich.
If Zmei Gorynuch cannot be defeated print -1.
Example
Input
3
3 10
6 3
8 2
1 4
4 10
4 1
3 2
2 6
1 100
2 15
10 11
14 100
Output
2
3
-1
Note
In the first query you can deal the first blow (after that the number of heads changes to 10 - 6 + 3 = 7), and then deal the second blow.
In the second query you just deal the first blow three times, and Zmei is defeated.
In third query you can not defeat Zmei Gorynich. Maybe it's better to convince it to stop fighting?
|
instruction
| 0
| 23,924
| 2
| 47,848
|
Tags: greedy, math
Correct Solution:
```
import math
import sys
t=int(input())
for te in range(t):
d=[]
h=[]
n,x=map(int,input().split())
for i in range(n):
d1, h1 = map(int, input().split())
d.append(d1)
h.append(h1)
dmax = max(d)
if dmax >= x:
print(1)
else:
dhmax = -sys.maxsize
for i in range(n):
dhmax = max(d[i] - h[i], dhmax)
if dhmax <= 0:
print(-1)
else:
print(math.ceil((x - dmax) / dhmax)+1)
```
|
output
| 1
| 23,924
| 2
| 47,849
|
Provide tags and a correct Python 3 solution for this coding contest problem.
You are fighting with Zmei Gorynich — a ferocious monster from Slavic myths, a huge dragon-like reptile with multiple heads!
<image>
Initially Zmei Gorynich has x heads. You can deal n types of blows. If you deal a blow of the i-th type, you decrease the number of Gorynich's heads by min(d_i, curX), there curX is the current number of heads. But if after this blow Zmei Gorynich has at least one head, he grows h_i new heads. If curX = 0 then Gorynich is defeated.
You can deal each blow any number of times, in any order.
For example, if curX = 10, d = 7, h = 10 then the number of heads changes to 13 (you cut 7 heads off, but then Zmei grows 10 new ones), but if curX = 10, d = 11, h = 100 then number of heads changes to 0 and Zmei Gorynich is considered defeated.
Calculate the minimum number of blows to defeat Zmei Gorynich!
You have to answer t independent queries.
Input
The first line contains one integer t (1 ≤ t ≤ 100) – the number of queries.
The first line of each query contains two integers n and x (1 ≤ n ≤ 100, 1 ≤ x ≤ 10^9) — the number of possible types of blows and the number of heads Zmei initially has, respectively.
The following n lines of each query contain the descriptions of types of blows you can deal. The i-th line contains two integers d_i and h_i (1 ≤ d_i, h_i ≤ 10^9) — the description of the i-th blow.
Output
For each query print the minimum number of blows you have to deal to defeat Zmei Gorynich.
If Zmei Gorynuch cannot be defeated print -1.
Example
Input
3
3 10
6 3
8 2
1 4
4 10
4 1
3 2
2 6
1 100
2 15
10 11
14 100
Output
2
3
-1
Note
In the first query you can deal the first blow (after that the number of heads changes to 10 - 6 + 3 = 7), and then deal the second blow.
In the second query you just deal the first blow three times, and Zmei is defeated.
In third query you can not defeat Zmei Gorynich. Maybe it's better to convince it to stop fighting?
|
instruction
| 0
| 23,925
| 2
| 47,850
|
Tags: greedy, math
Correct Solution:
```
from sys import stdin
inp=lambda:stdin.readline().split()
for _ in range(int(stdin.readline())):
n,x=map(int,inp())
item,D=0,0
for i in range(n):
d,h=map(int,inp())
D=max(D,d)
item=max(item,d-h)
res,x=1,max(0,x-D)
if item>0:
div,mod=x//item,x%item
if mod>0:div+=1
res+=div
print(res)
else:
if x==0:print(1)
else:print(-1)
```
|
output
| 1
| 23,925
| 2
| 47,851
|
Provide tags and a correct Python 3 solution for this coding contest problem.
You are fighting with Zmei Gorynich — a ferocious monster from Slavic myths, a huge dragon-like reptile with multiple heads!
<image>
Initially Zmei Gorynich has x heads. You can deal n types of blows. If you deal a blow of the i-th type, you decrease the number of Gorynich's heads by min(d_i, curX), there curX is the current number of heads. But if after this blow Zmei Gorynich has at least one head, he grows h_i new heads. If curX = 0 then Gorynich is defeated.
You can deal each blow any number of times, in any order.
For example, if curX = 10, d = 7, h = 10 then the number of heads changes to 13 (you cut 7 heads off, but then Zmei grows 10 new ones), but if curX = 10, d = 11, h = 100 then number of heads changes to 0 and Zmei Gorynich is considered defeated.
Calculate the minimum number of blows to defeat Zmei Gorynich!
You have to answer t independent queries.
Input
The first line contains one integer t (1 ≤ t ≤ 100) – the number of queries.
The first line of each query contains two integers n and x (1 ≤ n ≤ 100, 1 ≤ x ≤ 10^9) — the number of possible types of blows and the number of heads Zmei initially has, respectively.
The following n lines of each query contain the descriptions of types of blows you can deal. The i-th line contains two integers d_i and h_i (1 ≤ d_i, h_i ≤ 10^9) — the description of the i-th blow.
Output
For each query print the minimum number of blows you have to deal to defeat Zmei Gorynich.
If Zmei Gorynuch cannot be defeated print -1.
Example
Input
3
3 10
6 3
8 2
1 4
4 10
4 1
3 2
2 6
1 100
2 15
10 11
14 100
Output
2
3
-1
Note
In the first query you can deal the first blow (after that the number of heads changes to 10 - 6 + 3 = 7), and then deal the second blow.
In the second query you just deal the first blow three times, and Zmei is defeated.
In third query you can not defeat Zmei Gorynich. Maybe it's better to convince it to stop fighting?
|
instruction
| 0
| 23,926
| 2
| 47,852
|
Tags: greedy, math
Correct Solution:
```
import math
t = int(input())
for _ in range(t):
n, x = map(int, input().split())
max_damage_hit = -1
max_damage_per_round_hit = -1
for __ in range(n):
di, hi = map(int, input().split())
max_damage_hit = max(di, max_damage_hit)
max_damage_per_round_hit = max(di - hi, max_damage_per_round_hit)
if max_damage_per_round_hit <= 0:
print(1 if max_damage_hit >= x else -1)
continue
hits = max(math.ceil((x - max_damage_hit) / max_damage_per_round_hit), 0) + 1
print(hits)
```
|
output
| 1
| 23,926
| 2
| 47,853
|
Provide tags and a correct Python 3 solution for this coding contest problem.
You are fighting with Zmei Gorynich — a ferocious monster from Slavic myths, a huge dragon-like reptile with multiple heads!
<image>
Initially Zmei Gorynich has x heads. You can deal n types of blows. If you deal a blow of the i-th type, you decrease the number of Gorynich's heads by min(d_i, curX), there curX is the current number of heads. But if after this blow Zmei Gorynich has at least one head, he grows h_i new heads. If curX = 0 then Gorynich is defeated.
You can deal each blow any number of times, in any order.
For example, if curX = 10, d = 7, h = 10 then the number of heads changes to 13 (you cut 7 heads off, but then Zmei grows 10 new ones), but if curX = 10, d = 11, h = 100 then number of heads changes to 0 and Zmei Gorynich is considered defeated.
Calculate the minimum number of blows to defeat Zmei Gorynich!
You have to answer t independent queries.
Input
The first line contains one integer t (1 ≤ t ≤ 100) – the number of queries.
The first line of each query contains two integers n and x (1 ≤ n ≤ 100, 1 ≤ x ≤ 10^9) — the number of possible types of blows and the number of heads Zmei initially has, respectively.
The following n lines of each query contain the descriptions of types of blows you can deal. The i-th line contains two integers d_i and h_i (1 ≤ d_i, h_i ≤ 10^9) — the description of the i-th blow.
Output
For each query print the minimum number of blows you have to deal to defeat Zmei Gorynich.
If Zmei Gorynuch cannot be defeated print -1.
Example
Input
3
3 10
6 3
8 2
1 4
4 10
4 1
3 2
2 6
1 100
2 15
10 11
14 100
Output
2
3
-1
Note
In the first query you can deal the first blow (after that the number of heads changes to 10 - 6 + 3 = 7), and then deal the second blow.
In the second query you just deal the first blow three times, and Zmei is defeated.
In third query you can not defeat Zmei Gorynich. Maybe it's better to convince it to stop fighting?
|
instruction
| 0
| 23,927
| 2
| 47,854
|
Tags: greedy, math
Correct Solution:
```
import sys
from collections import deque
IS_LOCAL = False
def read_one(dtype=int):
return dtype(input())
def read_multiple(f, dtype=int):
return f(map(dtype, input().split()))
def swap(x, y):
return y, x
def solve(a, curX):
max_i, max_diff = 0, -1
for i, (d, h) in enumerate(a):
if d >= curX:
print(1)
return
if d - h > max_diff:
max_diff = d - h
max_i = i
if max_diff <= 0:
print(-1)
return
max_d = max([d for d, _ in a])
d, h = a[max_i]
x = (curX - max_d - 1) // max_diff + 1
print(x + 1)
def main():
if not IS_LOCAL:
T = read_one(int)
for _ in range(T):
n, curX = read_multiple(tuple)
a = []
for _ in range(n):
a.append(read_multiple(tuple))
solve(a, curX)
else:
T = 1
n, curX = 1, 6
# a = [[11, 1], [8, 2], [1, 4]]
a = [[3, 1]]
solve(a, curX)
if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] == 'True':
IS_LOCAL = True
main()
```
|
output
| 1
| 23,927
| 2
| 47,855
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are fighting with Zmei Gorynich — a ferocious monster from Slavic myths, a huge dragon-like reptile with multiple heads!
<image>
Initially Zmei Gorynich has x heads. You can deal n types of blows. If you deal a blow of the i-th type, you decrease the number of Gorynich's heads by min(d_i, curX), there curX is the current number of heads. But if after this blow Zmei Gorynich has at least one head, he grows h_i new heads. If curX = 0 then Gorynich is defeated.
You can deal each blow any number of times, in any order.
For example, if curX = 10, d = 7, h = 10 then the number of heads changes to 13 (you cut 7 heads off, but then Zmei grows 10 new ones), but if curX = 10, d = 11, h = 100 then number of heads changes to 0 and Zmei Gorynich is considered defeated.
Calculate the minimum number of blows to defeat Zmei Gorynich!
You have to answer t independent queries.
Input
The first line contains one integer t (1 ≤ t ≤ 100) – the number of queries.
The first line of each query contains two integers n and x (1 ≤ n ≤ 100, 1 ≤ x ≤ 10^9) — the number of possible types of blows and the number of heads Zmei initially has, respectively.
The following n lines of each query contain the descriptions of types of blows you can deal. The i-th line contains two integers d_i and h_i (1 ≤ d_i, h_i ≤ 10^9) — the description of the i-th blow.
Output
For each query print the minimum number of blows you have to deal to defeat Zmei Gorynich.
If Zmei Gorynuch cannot be defeated print -1.
Example
Input
3
3 10
6 3
8 2
1 4
4 10
4 1
3 2
2 6
1 100
2 15
10 11
14 100
Output
2
3
-1
Note
In the first query you can deal the first blow (after that the number of heads changes to 10 - 6 + 3 = 7), and then deal the second blow.
In the second query you just deal the first blow three times, and Zmei is defeated.
In third query you can not defeat Zmei Gorynich. Maybe it's better to convince it to stop fighting?
Submitted Solution:
```
import math
t=int(input())
for _ in range(t):
n,x=list(map(int,input().split()))
max_d,max_dh=-100,-100
for t1 in range(n):
d,h=list(map(int,input().split()))
max_d=max(max_d,d)
max_dh=max(max_dh,d-h)
if(x<=max_d):
print(1)
continue
if(max_dh<=0):
print(-1)
continue
ans=math.ceil(((x-max_d)/max_dh))
if(ans<0):
ans=0
print(ans+1)
```
|
instruction
| 0
| 23,928
| 2
| 47,856
|
Yes
|
output
| 1
| 23,928
| 2
| 47,857
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are fighting with Zmei Gorynich — a ferocious monster from Slavic myths, a huge dragon-like reptile with multiple heads!
<image>
Initially Zmei Gorynich has x heads. You can deal n types of blows. If you deal a blow of the i-th type, you decrease the number of Gorynich's heads by min(d_i, curX), there curX is the current number of heads. But if after this blow Zmei Gorynich has at least one head, he grows h_i new heads. If curX = 0 then Gorynich is defeated.
You can deal each blow any number of times, in any order.
For example, if curX = 10, d = 7, h = 10 then the number of heads changes to 13 (you cut 7 heads off, but then Zmei grows 10 new ones), but if curX = 10, d = 11, h = 100 then number of heads changes to 0 and Zmei Gorynich is considered defeated.
Calculate the minimum number of blows to defeat Zmei Gorynich!
You have to answer t independent queries.
Input
The first line contains one integer t (1 ≤ t ≤ 100) – the number of queries.
The first line of each query contains two integers n and x (1 ≤ n ≤ 100, 1 ≤ x ≤ 10^9) — the number of possible types of blows and the number of heads Zmei initially has, respectively.
The following n lines of each query contain the descriptions of types of blows you can deal. The i-th line contains two integers d_i and h_i (1 ≤ d_i, h_i ≤ 10^9) — the description of the i-th blow.
Output
For each query print the minimum number of blows you have to deal to defeat Zmei Gorynich.
If Zmei Gorynuch cannot be defeated print -1.
Example
Input
3
3 10
6 3
8 2
1 4
4 10
4 1
3 2
2 6
1 100
2 15
10 11
14 100
Output
2
3
-1
Note
In the first query you can deal the first blow (after that the number of heads changes to 10 - 6 + 3 = 7), and then deal the second blow.
In the second query you just deal the first blow three times, and Zmei is defeated.
In third query you can not defeat Zmei Gorynich. Maybe it's better to convince it to stop fighting?
Submitted Solution:
```
import math
n=int(input())
for i in range(0,n):
p=input().rstrip().split(' ')
l=[]
q=[]
w=[]
S=[]
D=[]
for j in range(0,int(p[0])):
o=input().rstrip().split(' ')
l.append(int(o[0]))
q.append(int(o[1]))
w.append(int(o[0]) - int(o[1]))
V=max(w)
if V<=0:
I=0;
G=-1;
J=len(l)-1;
l.sort(key=int)
while(I<=J):
m=(I+J)//2;
if l[m] >= int(p[1]):
G=1;
break;
else:
I=m+1;
print(G)
else:
A=w.index(V)
if V>=int(p[1]):
print(1)
else:
W=max(l)
if W>=int(p[1]):
print(1)
else:
D=0;
N=int(p[1])
C=math.ceil((N-(W))/V)
D+=C;
D+=1;
print(D)
```
|
instruction
| 0
| 23,929
| 2
| 47,858
|
Yes
|
output
| 1
| 23,929
| 2
| 47,859
|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
You are fighting with Zmei Gorynich — a ferocious monster from Slavic myths, a huge dragon-like reptile with multiple heads!
<image>
Initially Zmei Gorynich has x heads. You can deal n types of blows. If you deal a blow of the i-th type, you decrease the number of Gorynich's heads by min(d_i, curX), there curX is the current number of heads. But if after this blow Zmei Gorynich has at least one head, he grows h_i new heads. If curX = 0 then Gorynich is defeated.
You can deal each blow any number of times, in any order.
For example, if curX = 10, d = 7, h = 10 then the number of heads changes to 13 (you cut 7 heads off, but then Zmei grows 10 new ones), but if curX = 10, d = 11, h = 100 then number of heads changes to 0 and Zmei Gorynich is considered defeated.
Calculate the minimum number of blows to defeat Zmei Gorynich!
You have to answer t independent queries.
Input
The first line contains one integer t (1 ≤ t ≤ 100) – the number of queries.
The first line of each query contains two integers n and x (1 ≤ n ≤ 100, 1 ≤ x ≤ 10^9) — the number of possible types of blows and the number of heads Zmei initially has, respectively.
The following n lines of each query contain the descriptions of types of blows you can deal. The i-th line contains two integers d_i and h_i (1 ≤ d_i, h_i ≤ 10^9) — the description of the i-th blow.
Output
For each query print the minimum number of blows you have to deal to defeat Zmei Gorynich.
If Zmei Gorynuch cannot be defeated print -1.
Example
Input
3
3 10
6 3
8 2
1 4
4 10
4 1
3 2
2 6
1 100
2 15
10 11
14 100
Output
2
3
-1
Note
In the first query you can deal the first blow (after that the number of heads changes to 10 - 6 + 3 = 7), and then deal the second blow.
In the second query you just deal the first blow three times, and Zmei is defeated.
In third query you can not defeat Zmei Gorynich. Maybe it's better to convince it to stop fighting?
Submitted Solution:
```
from sys import stdin
def ler():
for line in stdin:
yield line
def solve(s):
n, vida = [int(x) for x in next(s).split(' ')]
maior_dano = 0
melhor_dano_regen = 0
for i in range(n):
dano, regen = [int(x) for x in next(s).split(' ')]
maior_dano = max(dano, maior_dano)
melhor_dano_regen = max(melhor_dano_regen, dano - regen)
vida -= maior_dano
if vida <= 0:
return 1
if melhor_dano_regen < 1:
return -1
return 1 + vida // melhor_dano_regen + (1 if vida % melhor_dano_regen > 0 else 0)
s = ler()
a = int(next(s))
while a > 0:
print(solve(s))
a -= 1
```
|
instruction
| 0
| 23,930
| 2
| 47,860
|
Yes
|
output
| 1
| 23,930
| 2
| 47,861
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.