task_type
stringclasses 1
value | problem
stringlengths 261
3.34k
| answer
stringlengths 35
6.15k
| problem_tokens
int64 62
774
| answer_tokens
int64 12
2.04k
|
|---|---|---|---|---|
coding
|
Solve the programming task below in a Python markdown code block.
There are $ n $ acitivities with start times $ \\ {s_i \\} $ and finish times $ \\ {t_i \\} $. Assuming that a person can only work on a single activity at a time, find the maximum number of activities that can be performed by a single person.
Constraints
* $ 1 \ le n \ le 10 ^ 5 $
* $ 1 \ le s_i \ lt t_i \ le 10 ^ 9 (1 \ le i \ le n) $
Input
$ n $
$ s_1 $ $ t_1 $
$ s_2 $ $ t_2 $
::
$ s_n $ $ t_n $
The first line consists of the integer $ n $. In the following $ n $ lines, the start time $ s_i $ and the finish time $ t_i $ of the activity $ i $ are given.
output
Print the maximum number of activities in a line.
Examples
Input
5
1 2
3 9
3 5
5 9
6 8
Output
3
Input
3
1 5
3 4
2 5
Output
1
Input
3
1 2
2 3
3 4
Output
2
|
{"inputs": ["3\n1 1\n2 3\n3 4", "3\n0 5\n3 4\n2 5", "3\n1 1\n2 4\n3 4", "3\n1 1\n2 4\n3 3", "3\n2 1\n2 4\n3 3", "3\n4 1\n2 4\n3 3", "3\n4 1\n2 4\n3 2", "3\n4 1\n4 4\n3 2"], "outputs": ["2\n", "1\n", "2\n", "2\n", "2\n", "2\n", "2\n", "3\n"]}
| 279
| 158
|
coding
|
Solve the programming task below in a Python markdown code block.
Rose loves to play games. This problem is about a game he recently played. In the game there are N locations, numbered 0 through N-1. Each location has one entrance and one exit. You are given an array aka A with N elements.For each i, A[i] describes the exit from location i. If A[i] is a number between 0 and N-1, inclusive, it means that the exit from location i leads to the entrance of location A[i]. Otherwise, A[i] will be -1 and it means that if the player reaches this exit, they win the game.
Rose started the game by entering location 0. Print "Win" (quotes for clarity) if he can win the game. Otherwise, print "Lose". Note that the return value is case-sensitive.
Input - First line contains the no. of testcases and each testcase consist of 2 lines first containing the no. of elements in array and next line contiaing those elements.
Output - Print "Win" if Russy wins otherwise "Lose".
SAMPLE INPUT
3
2
1 -1
3
1 0 -1
3
0 1 2
SAMPLE OUTPUT
Win
Lose
Lose
Explanation
Testcase 1 - Rose will start in location 0. The exit from this location will bring him to location 1, and when he reaches the exit from location 1 he wins the game.
Testcase 2 - Rose will go back and forth between locations 0 and 1. He is unable to reach the exit from location 2.
Testcase 3 - The exit from location 0 leads back to location 0. Russy is unable to reach the other locations.
|
{"inputs": ["10\n2\n1 -1\n3\n1 0 -1\n3\n0 1 2\n37\n29 33 28 16 -1 11 10 14 6 31 7 35 34 8 15 17 26 12 13 22 1 20 2 21 -1 5 19 9 18 4 25 32 3 30 23 10 27\n48\n17 43 20 41 42 15 18 35 -1 31 7 33 23 33 -1 -1 0 33 19 12 42 -1 -1 9 9 -1 39 -1 31 46 -1 20 44 41 -1 -1 12 -1 36 -1 -1 6 47 10 2 4 1 29\n6\n3 1 1 2 -1 4\n47\n15 10 18 36 32 38 2 4 21 33 12 13 -1 30 44 -1 28 19 25 14 42 22 27 7 3 41 5 46 17 26 40 20 37 34 23 43 11 45 6 16 9 31 35 39 1 29 24\n47\n27 28 6 11 24 2 41 4 22 5 8 17 9 15 45 18 23 21 -1 37 44 10 14 1 25 32 30 3 31 7 -1 40 38 16 42 43 19 29 33 12 39 20 35 13 46 36 26\n47\n15 19 16 24 5 8 37 13 40 25 14 29 45 28 30 31 27 3 34 7 36 17 43 38 44 42 -1 21 11 23 2 1 20 22 32 33 39 26 35 4 41 12 10 46 6 9 18\n47\n14 6 7 5 18 12 31 33 46 44 -1 38 30 36 15 34 29 40 11 4 39 41 3 43 35 10 22 16 45 19 9 37 27 28 17 5 23 26 8 13 2 42 24 21 20 32 25", "11\n47\n16 38 18 8 24 32 42 45 2 10 30 26 23 -1 15 35 39 46 37 1 33 19 28 44 43 17 5 34 -1 11 40 41 3 13 6 4 7 31 36 21 27 25 29 20 9 14 22\n47\n30 9 24 10 8 36 4 46 2 -1 1 33 17 28 35 5 25 19 22 34 15 26 12 45 29 43 38 14 37 7 40 18 16 39 3 21 6 42 41 44 23 11 27 -1 31 20 32\n48\n10 7 22 28 44 25 1 47 24 37 5 8 31 30 18 4 39 34 -1 9 12 46 11 45 20 15 21 36 33 3 29 6 2 17 26 41 43 35 42 14 32 13 16 8 19 38 23 27\n48\n42 27 38 43 30 22 24 39 33 7 35 7 40 36 46 45 26 2 10 16 29 8 13 37 11 18 12 32 14 19 34 25 28 41 1 23 17 20 9 3 47 -1 31 21 15 4 6 44\n48\n6 5 8 2 32 16 18 36 21 12 27 35 37 46 20 39 11 9 31 3 29 1 38 4 17 -1 41 40 -1 13 19 47 45 25 22 15 43 7 44 34 42 10 24 30 23 14 33 26\n48\n28 40 16 35 -1 7 33 20 12 34 11 38 4 31 3 10 44 23 8 21 18 45 43 15 37 27 29 46 39 36 1 19 47 5 14 42 13 6 41 22 32 30 2 9 17 25 -1 26\n3\n2 -1 0\n1\n-1\n22\n1 15 1 -1 10 14 3 10 7 14 10 4 14 11 3 19 16 18 19 19 16 15\n6\n3 4 1 2 4 5\n46\n5 1 39 18 5 22 3 21 21 44 19 2 21 41 30 5 0 12 8 15 45 29 41 22 43 27 41 17 21 38 23 4 45 31 31 9 14 25 2 14 19 11 32 33 15 14"], "outputs": ["Win\nLose\nLose\nWin\nWin\nLose\nWin\nWin\nWin\nWin", "Win\nWin\nWin\nWin\nWin\nWin\nLose\nWin\nLose\nLose\nLose"]}
| 372
| 1,895
|
coding
|
Solve the programming task below in a Python markdown code block.
Ilya plays a card game by the following rules.
A player has several cards. Each card contains two non-negative integers inscribed, one at the top of the card and one at the bottom. At the beginning of the round the player chooses one of his cards to play it. If the top of the card contains number ai, and the bottom contains number bi, then when the player is playing the card, he gets ai points and also gets the opportunity to play additional bi cards. After the playing the card is discarded.
More formally: let's say that there is a counter of the cards that can be played. At the beginning of the round the counter equals one. When a card is played, the counter decreases by one for the played card and increases by the number bi, which is written at the bottom of the card. Then the played card is discarded. If after that the counter is not equal to zero, the player gets the opportunity to play another card from the remaining cards. The round ends when the counter reaches zero or the player runs out of cards.
Of course, Ilya wants to get as many points as possible. Can you determine the maximum number of points he can score provided that you know his cards?
Input
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of cards Ilya has.
Each of the next n lines contains two non-negative space-separated integers — ai and bi (0 ≤ ai, bi ≤ 104) — the numbers, written at the top and the bottom of the i-th card correspondingly.
Output
Print the single number — the maximum number of points you can score in one round by the described rules.
Examples
Input
2
1 0
2 0
Output
2
Input
3
1 0
2 0
0 2
Output
3
Note
In the first sample none of two cards brings extra moves, so you should play the one that will bring more points.
In the second sample you should first play the third card that doesn't bring any points but lets you play both remaining cards.
|
{"inputs": ["1\n7 0\n", "1\n0 0\n", "1\n7 1\n", "1\n1 0\n", "1\n3 1\n", "1\n0 1\n", "1\n1 1\n", "1\n2 0\n"], "outputs": ["7\n", "0\n", "7\n", "1\n", "3\n", "0\n", "1\n", "2\n"]}
| 451
| 102
|
coding
|
Solve the programming task below in a Python markdown code block.
Noora is a student of one famous high school. It's her final year in school — she is going to study in university next year. However, she has to get an «A» graduation certificate in order to apply to a prestigious one.
In school, where Noora is studying, teachers are putting down marks to the online class register, which are integers from 1 to k. The worst mark is 1, the best is k. Mark that is going to the certificate, is calculated as an average of all the marks, rounded to the closest integer. If several answers are possible, rounding up is produced. For example, 7.3 is rounded to 7, but 7.5 and 7.8784 — to 8.
For instance, if Noora has marks [8, 9], then the mark to the certificate is 9, because the average is equal to 8.5 and rounded to 9, but if the marks are [8, 8, 9], Noora will have graduation certificate with 8.
To graduate with «A» certificate, Noora has to have mark k.
Noora got n marks in register this year. However, she is afraid that her marks are not enough to get final mark k. Noora decided to ask for help in the internet, where hacker Leha immediately responded to her request. He is ready to hack class register for Noora and to add Noora any number of additional marks from 1 to k. At the same time, Leha want his hack be unseen to everyone, so he decided to add as less as possible additional marks. Please help Leha to calculate the minimal number of marks he has to add, so that final Noora's mark will become equal to k.
-----Input-----
The first line contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 100) denoting the number of marks, received by Noora and the value of highest possible mark.
The second line contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ k) denoting marks received by Noora before Leha's hack.
-----Output-----
Print a single integer — minimal number of additional marks, that Leha has to add in order to change Noora's final mark to k.
-----Examples-----
Input
2 10
8 9
Output
4
Input
3 5
4 4 4
Output
3
-----Note-----
Consider the first example testcase.
Maximal mark is 10, Noora received two marks — 8 and 9, so current final mark is 9. To fix it, Leha can add marks [10, 10, 10, 10] (4 marks in total) to the registry, achieving Noora having average mark equal to $\frac{8 + 9 + 10 + 10 + 10 + 10}{6} = \frac{57}{6} = 9.5$. Consequently, new final mark is 10. Less number of marks won't fix the situation.
In the second example Leha can add [5, 5, 5] to the registry, so that making average mark equal to 4.5, which is enough to have 5 in the certificate.
|
{"inputs": ["1 1\n1\n", "1 2\n2\n", "1 3\n3\n", "1 4\n4\n", "1 5\n5\n", "1 7\n1\n", "1 5\n1\n", "1 9\n9\n"], "outputs": ["0", "0", "0", "0", "0", "11", "7", "0"]}
| 729
| 95
|
coding
|
Solve the programming task below in a Python markdown code block.
Description
THE BY DOLM @ STER is a training simulation game scheduled to be released on EXIDNA by 1rem on April 1, 2010. For the time being, it probably has nothing to do with an arcade game where the network connection service stopped earlier this month.
This game is a game in which members of the unit (formation) to be produced are selected from the bidders, and through lessons and communication with the members, they (they) are raised to the top of the bidders, the top biddles.
Each bidle has three parameters, vocal, dance, and looks, and the unit's ability score is the sum of all the parameters of the biddles that belong to the unit. The highest of the three stats of a unit is the rank of the unit.
There is no limit to the number of people in a unit, and you can work as a unit with one, three, or 100 members. Of course, you can hire more than one of the same bidle, but hiring a bidle is expensive and must be taken into account.
As a producer, you decided to write and calculate a program to make the best unit.
Input
The input consists of multiple test cases.
The first line of each test case is given the number N of bid dollars and the available cost M. (1 <= N, M <= 300) The next 2 * N lines contain information about each bidle.
The name of the bidle is on the first line of the bidle information. Biddle names consist of alphabets and spaces, with 30 characters or less. Also, there is no bidle with the same name.
The second line is given the integers C, V, D, L. C is the cost of hiring one Biddle, V is the vocal, D is the dance, and L is the stat of the looks. (1 <= C, V, D, L <= 300)
Input ends with EOF.
Output
Answer the maximum rank of units that can be made within the given cost.
If you cannot make a unit, output 0.
Example
Input
3 10
Dobkeradops
7 5 23 10
PataPata
1 1 2 1
dop
5 3 11 14
2 300
Bydo System Alpha
7 11 4 7
Green Inferno
300 300 300 300
Output
29
462
|
{"inputs": ["3 10\nDobkeradops\n7 3 18 0\nPaatQata\n1 0 6 1\ndop\n5 9 28 13\n2 2\nBydp syStem ahplA\n7 11 4 0\nFedrn Infermo\n243 80 32 80", "3 10\nDobkesadops\n7 1 18 0\nPaatQaua\n1 0 0 1\ndop\n8 9 28 13\n2 3\nBydp syStem ahplA\n20 1 4 0\nEedrn Infeqmo\n438 102 15 80", "3 18\nDobkesadops\n7 1 18 0\nPaatQaua\n1 0 0 1\ndop\n8 9 28 13\n2 3\nBydp syStem ahplA\n40 1 1 0\nEedrn Infeqmo\n438 102 15 80", "3 4\nDobkfradops\n5 3 23 1\nataPataP\n1 1 3 1\ndop\n5 3 21 13\n2 300\nBydp System Alaph\n8 2 4 2\nGseen Inferno\n277 205 32 243", "3 6\nDobkeradopr\n8 1 19 9\nPataPasa\n1 0 4 1\ndop\n5 15 34 2\n2 110\npcyB Sytsem Alpha\n13 5 1 0\nmeerG Inferno\n45 219 32 884", "3 18\nDobkeradops\n7 2 6 13\nPataPata\n1 1 3 1\ndop\n5 17 9 29\n2 300\npdyB Stsyem Alpha\n7 11 4 3\nGreen Inferno\n300 219 4 493", "3 18\nDobkeradops\n20 2 6 13\nPataPata\n1 1 3 1\ndop\n5 17 9 15\n2 48\npdyB Stsyem Alpha\n7 11 4 3\nGreen Inferno\n300 219 4 493", "3 18\nDobkeradops\n10 3 6 2\nPataPata\n1 1 3 2\ndop\n5 17 9 15\n2 300\npdyB Stsyem Alpha\n7 13 4 3\nGreen Inferno\n300 219 4 493"], "outputs": ["60\n0\n", "28\n0\n", "56\n0\n", "12\n247\n", "38\n1768\n", "90\n493\n", "54\n66\n", "54\n546\n"]}
| 557
| 782
|
coding
|
Solve the programming task below in a Python markdown code block.
Create a program that calculates and outputs the surface distance by inputting the north latitude and east longitude of two cities on the earth. However, the earth is a sphere with a radius of 6,378.1 km, and the surface distance between two points is the shortest distance along this sphere. Also, in the southern hemisphere, we will use 0 to -90 degrees north latitude without using south latitude, and 180 to 360 degrees east longitude without using west longitude even to the west of the Greenwich meridional line. Calculate the ground surface distance in km, round off to the nearest whole number, and output as an integer value.
Below are examples of north latitude and east longitude of major cities.
Place name | North latitude (degree) | East longitude (degree)
--- | --- | ---
Tokyo | 35.68 | 139.77
Singapore | 1.37 | 103.92
Sydney | -33.95 | 151.18
Chicago | 41.78 | 272.25
Buenos Aires | -34.58 | 301.52
London | 51.15 | 359.82
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by -1 four lines. Each dataset is given in the following format:
a b c d
The first city's north latitude a, the first city's east longitude b, the second city's north latitude c, and the second city's east longitude d are given on one line, separated by blanks. All inputs are given in real numbers.
The number of datasets does not exceed 30.
Output
Outputs the surface distances of two cities on one line for each dataset.
Example
Input
35.68 139.77 51.15 359.82
1.37 103.92 41.78 272.25
51.15 359.82 -34.58 301.52
-1 -1 -1 -1
Output
9609
15092
11112
|
{"inputs": ["35.68 139.77 51.15 359.82\n1.37 103.92 41.78 272.25\n51.15 359.82 -34.58 301.52\n-1 -1 -1 -1", "35.68 139.77 51.15 360.3067312023825\n1.37 103.92 41.78 272.25\n51.15 359.82 -34.58 301.52\n-1 -1 -1 -1", "35.68 139.77 51.15 359.82\n1.37 103.92 41.78 272.25\n51.15 359.82 -34.03920544232073 301.52\n-1 -1 -1 -1", "35.68 139.77 51.15 359.82\n1.37 103.92 41.78 272.25\n51.99657446150969 359.82 -34.58 301.52\n-1 -1 -1 -1", "35.997652258458864 139.77 51.15 359.82\n1.37 103.92 41.78 272.25\n51.15 359.82 -34.58 301.52\n-1 -1 -1 -1", "35.70387221923033 139.77 51.15 360.3067312023825\n1.37 103.92 41.78 272.25\n51.15 359.82 -34.58 301.52\n-1 -1 -1 -1", "35.68 139.77 51.15 359.82\n1.37 103.92 42.40751195136165 272.25\n51.15 359.82 -34.03920544232073 301.52\n-1 -1 -1 -1", "35.997652258458864 139.77 51.15 359.82\n1.37 103.92 41.78 272.25\n51.15 359.82 -34.58 302.04903635880555\n-1 -1 -1 -1"], "outputs": ["9609\n15092\n11112", "9591\n15092\n11112\n", "9609\n15092\n11062\n", "9609\n15092\n11178\n", "9577\n15092\n11112\n", "9589\n15092\n11112\n", "9609\n15025\n11062\n", "9577\n15092\n11086\n"]}
| 513
| 973
|
coding
|
Solve the programming task below in a Python markdown code block.
Simple, given a string of words, return the length of the shortest word(s).
String will never be empty and you do not need to account for different data types.
Also feel free to reuse/extend the following starter code:
```python
def find_short(s):
```
|
{"functional": "_inputs = [['bitcoin take over the world maybe who knows perhaps'], ['turns out random test cases are easier than writing out basic ones'], ['lets talk about javascript the best language'], ['i want to travel the world writing code one day'], ['Lets all go on holiday somewhere very cold']]\n_outputs = [[3], [3], [3], [1], [2]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(find_short(*i), o[0])"}
| 68
| 220
|
coding
|
Solve the programming task below in a Python markdown code block.
There is a large pile of socks that must be paired by color. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are.
Example
$n=7$
$ar=[1,2,1,2,1,3,2]$
There is one pair of color $\mbox{I}$ and one of color $2$. There are three odd socks left, one of each color. The number of pairs is $2$.
Function Description
Complete the sockMerchant function in the editor below.
sockMerchant has the following parameter(s):
int n: the number of socks in the pile
int ar[n]: the colors of each sock
Returns
int: the number of pairs
Input Format
The first line contains an integer $n$, the number of socks represented in $\boldsymbol{ar}$.
The second line contains $n$ space-separated integers, $ar[i]$, the colors of the socks in the pile.
Constraints
$1\leq n\leq100$
$1\leq ar[i]\leq100$ where $0\leq i<n$
Sample Input
STDIN Function
----- --------
9 n = 9
10 20 20 10 10 30 50 10 20 ar = [10, 20, 20, 10, 10, 30, 50, 10, 20]
Sample Output
3
Explanation
There are three pairs of socks.
|
{"inputs": ["9\n10 20 20 10 10 30 50 10 20"], "outputs": ["3"]}
| 356
| 39
|
coding
|
Solve the programming task below in a Python markdown code block.
Your task is to get Zodiac Sign using input ```day``` and ```month```.
For exapmle:
```python
get_zodiac_sign(1,5) => 'Taurus'
get_zodiac_sign(10,10) => 'Libra'
```
Correct answers are (preloaded):
```python
SIGNS = ['Capricorn', 'Aquarius', 'Pisces', 'Aries', 'Taurus', 'Gemini', 'Cancer', 'Leo', 'Virgo', 'Libra', 'Scorpio', 'Sagittarius']
```
P.S. Each argument is correct integer number.
WESTERN ASTROLOGY STAR SIGN DATES
* Aries (March 21-April 19)
* Taurus (April 20-May 20)
* Gemini (May 21-June 20)
* Cancer (June 21-July 22)
* Leo (July 23-August 22)
* Virgo (August 23-September 22)
* Libra (September 23-October 22)
* Scorpio (October 23-November 21)
* Sagittarius (November 22-December 21)
* Capricorn (December 22-January 19)
* Aquarius (January 20 to February 18)
* Pisces (February 19 to March 20)
Also feel free to reuse/extend the following starter code:
```python
def get_zodiac_sign(day, month):
```
|
{"functional": "_inputs = [[10, 10], [1, 5], [6, 9], [25, 11]]\n_outputs = [['Libra'], ['Taurus'], ['Virgo'], ['Sagittarius']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(get_zodiac_sign(*i), o[0])"}
| 351
| 196
|
coding
|
Solve the programming task below in a Python markdown code block.
Given an array, find the duplicates in that array, and return a new array of those duplicates. The elements of the returned array should appear in the order when they first appeared as duplicates.
__*Note*__: numbers and their corresponding string representations should not be treated as duplicates (i.e., `"1" != 1`).
## Examples
```
[1, 2, 4, 4, 3, 3, 1, 5, 3, "5"] ==> [4, 3, 1]
[0, 1, 2, 3, 4, 5] ==> []
```
Also feel free to reuse/extend the following starter code:
```python
def duplicates(array):
```
|
{"functional": "_inputs = [[[1, 2, 4, 4, 3, 3, 1, 5, 3, '5']], [[0, 1, 2, 3, 4, 5]], [[1, 1, 2, 3, 4, 5, 4]]]\n_outputs = [[[4, 3, 1]], [[]], [[1, 4]]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(duplicates(*i), o[0])"}
| 171
| 234
|
coding
|
Solve the programming task below in a Python markdown code block.
One day Mr. Takahashi picked up a dictionary containing all of the N! permutations of integers 1 through N. The dictionary has N! pages, and page i (1 ≤ i ≤ N!) contains the i-th permutation in the lexicographical order.
Mr. Takahashi wanted to look up a certain permutation of length N in this dictionary, but he forgot some part of it.
His memory of the permutation is described by a sequence P_1, P_2, ..., P_N. If P_i = 0, it means that he forgot the i-th element of the permutation; otherwise, it means that he remembered the i-th element of the permutation and it is P_i.
He decided to look up all the possible permutations in the dictionary. Compute the sum of the page numbers of the pages he has to check, modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 500000
* 0 ≤ P_i ≤ N
* P_i ≠ P_j if i ≠ j (1 ≤ i, j ≤ N), P_i ≠ 0 and P_j ≠ 0.
Input
The input is given from Standard Input in the following format:
N
P_1 P_2 ... P_N
Output
Print the sum of the page numbers of the pages he has to check, as modulo 10^9 + 7.
Examples
Input
4
0 2 3 0
Output
23
Input
3
0 0 0
Output
21
Input
5
1 2 3 5 4
Output
2
Input
1
0
Output
1
Input
10
0 3 0 0 1 0 4 0 0 0
Output
953330050
|
{"inputs": ["1\n1", "1\n0", "3\n0 0 1", "3\n0 0 2", "3\n0 0 3", "3\n0 1 0", "3\n1 3 0", "3\n1 0 0"], "outputs": ["1\n", "1", "10\n", "7\n", "4\n", "8\n", "2\n", "3\n"]}
| 402
| 102
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problems statements [Bengali] , [Mandarin chinese] , [Russian] and [Vietnamese] as well.
You are given an integer sequence $A$ with length $N$. In one move, you are allowed to choose one element of this sequence and change it to an arbitrary integer. Compute the minimum number of moves needed to make the sequence a permutation of the integers $1$ through $N$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \dots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer — the minimum required number of moves.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 10^{5}$
the sum of $N$ for all test cases does not exceed $2 \cdot 10^{5}$
$1 ≤ A_{i} ≤ 10^{9}$ for each valid $i$
----- Sample Input 1 ------
2
4
1 2 3 3
3
2 6 2
----- Sample Output 1 ------
1
2
----- explanation 1 ------
Example case 1: One possible way is to change the last element to $4$. This creates the sequence $1, 2, 3, 4$, which is a permutation.
Example case 2: We can change the first element to $1$ and the second element to $3$, creating the permutation $1, 3, 2$.
|
{"inputs": ["2\n4\n1 2 3 3\n3\n2 6 2"], "outputs": ["1\n2"]}
| 393
| 32
|
coding
|
Solve the programming task below in a Python markdown code block.
There are N monsters, numbered 1, 2, ..., N.
Initially, the health of Monster i is A_i.
Below, a monster with at least 1 health is called alive.
Until there is only one alive monster, the following is repeated:
- A random alive monster attacks another random alive monster.
- As a result, the health of the monster attacked is reduced by the amount equal to the current health of the monster attacking.
Find the minimum possible final health of the last monster alive.
-----Constraints-----
- All values in input are integers.
- 2 \leq N \leq 10^5
- 1 \leq A_i \leq 10^9
-----Input-----
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
-----Output-----
Print the minimum possible final health of the last monster alive.
-----Sample Input-----
4
2 10 8 40
-----Sample Output-----
2
When only the first monster keeps on attacking, the final health of the last monster will be 2, which is minimum.
|
{"inputs": ["4\n1 1 1 2", "4\n1 1 1 3", "4\n1 1 1 6", "4\n1 1 2 6", "4\n1 1 4 6", "4\n1 1 0 6", "4\n2 1 0 6", "4\n2 1 0 1"], "outputs": ["1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n", "1\n"]}
| 247
| 126
|
coding
|
Solve the programming task below in a Python markdown code block.
# Task
Your task is to find the smallest number which is evenly divided by all numbers between `m` and `n` (both inclusive).
# Example
For `m = 1, n = 2`, the output should be `2`.
For `m = 2, n = 3`, the output should be `6`.
For `m = 3, n = 2`, the output should be `6` too.
For `m = 1, n = 10`, the output should be `2520`.
# Input/Output
- `[input]` integer `m`
`1 ≤ m ≤ 25`
- `[input]` integer `n`
`1 ≤ n ≤ 25`
- `[output]` an integer
Also feel free to reuse/extend the following starter code:
```python
def mn_lcm(m, n):
```
|
{"functional": "_inputs = [[1, 2], [1, 5], [5, 1], [1, 10], [2, 3], [3, 5], [10, 20], [1, 25], [24, 25]]\n_outputs = [[2], [60], [60], [2520], [6], [60], [232792560], [26771144400], [600]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(mn_lcm(*i), o[0])"}
| 208
| 263
|
coding
|
Solve the programming task below in a Python markdown code block.
Vasya's birthday is approaching and Lena decided to sew a patterned handkerchief to him as a present. Lena chose digits from 0 to n as the pattern. The digits will form a rhombus. The largest digit n should be located in the centre. The digits should decrease as they approach the edges. For example, for n = 5 the handkerchief pattern should look like that:
0
0 1 0
0 1 2 1 0
0 1 2 3 2 1 0
0 1 2 3 4 3 2 1 0
0 1 2 3 4 5 4 3 2 1 0
0 1 2 3 4 3 2 1 0
0 1 2 3 2 1 0
0 1 2 1 0
0 1 0
0
Your task is to determine the way the handkerchief will look like by the given n.
Input
The first line contains the single integer n (2 ≤ n ≤ 9).
Output
Print a picture for the given n. You should strictly observe the number of spaces before the first digit on each line. Every two adjacent digits in the same line should be separated by exactly one space. There should be no spaces after the last digit at the end of each line.
Examples
Input
2
Output
0
0 1 0
0 1 2 1 0
0 1 0
0
Input
3
Output
0
0 1 0
0 1 2 1 0
0 1 2 3 2 1 0
0 1 2 1 0
0 1 0
0
|
{"inputs": ["4\n", "5\n", "9\n", "7\n", "8\n", "6\n", "3\n", "2\n"], "outputs": [" 0\n 0 1 0\n 0 1 2 1 0\n 0 1 2 3 2 1 0\n0 1 2 3 4 3 2 1 0\n 0 1 2 3 2 1 0\n 0 1 2 1 0\n 0 1 0\n 0\n", " 0\n 0 1 0\n 0 1 2 1 0\n 0 1 2 3 2 1 0\n 0 1 2 3 4 3 2 1 0\n0 1 2 3 4 5 4 3 2 1 0\n 0 1 2 3 4 3 2 1 0\n 0 1 2 3 2 1 0\n 0 1 2 1 0\n 0 1 0\n 0\n", " 0\n 0 1 0\n 0 1 2 1 0\n 0 1 2 3 2 1 0\n 0 1 2 3 4 3 2 1 0\n 0 1 2 3 4 5 4 3 2 1 0\n 0 1 2 3 4 5 6 5 4 3 2 1 0\n 0 1 2 3 4 5 6 7 6 5 4 3 2 1 0\n 0 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 0\n0 1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 0\n 0 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 0\n 0 1 2 3 4 5 6 7 6 5 4 3 2 1 0\n 0 1 2 3 4 5 6 5 4 3 2 1 0\n 0 1 2 3 4 5 4 3 2 1 0\n 0 1 2 3 4 3 2 1 0\n 0 1 2 3 2 1 0\n 0 1 2 1 0\n 0 1 0\n 0\n", " 0\n 0 1 0\n 0 1 2 1 0\n 0 1 2 3 2 1 0\n 0 1 2 3 4 3 2 1 0\n 0 1 2 3 4 5 4 3 2 1 0\n 0 1 2 3 4 5 6 5 4 3 2 1 0\n0 1 2 3 4 5 6 7 6 5 4 3 2 1 0\n 0 1 2 3 4 5 6 5 4 3 2 1 0\n 0 1 2 3 4 5 4 3 2 1 0\n 0 1 2 3 4 3 2 1 0\n 0 1 2 3 2 1 0\n 0 1 2 1 0\n 0 1 0\n 0\n", " 0\n 0 1 0\n 0 1 2 1 0\n 0 1 2 3 2 1 0\n 0 1 2 3 4 3 2 1 0\n 0 1 2 3 4 5 4 3 2 1 0\n 0 1 2 3 4 5 6 5 4 3 2 1 0\n 0 1 2 3 4 5 6 7 6 5 4 3 2 1 0\n0 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 0\n 0 1 2 3 4 5 6 7 6 5 4 3 2 1 0\n 0 1 2 3 4 5 6 5 4 3 2 1 0\n 0 1 2 3 4 5 4 3 2 1 0\n 0 1 2 3 4 3 2 1 0\n 0 1 2 3 2 1 0\n 0 1 2 1 0\n 0 1 0\n 0\n", " 0\n 0 1 0\n 0 1 2 1 0\n 0 1 2 3 2 1 0\n 0 1 2 3 4 3 2 1 0\n 0 1 2 3 4 5 4 3 2 1 0\n0 1 2 3 4 5 6 5 4 3 2 1 0\n 0 1 2 3 4 5 4 3 2 1 0\n 0 1 2 3 4 3 2 1 0\n 0 1 2 3 2 1 0\n 0 1 2 1 0\n 0 1 0\n 0\n", " 0\n 0 1 0\n 0 1 2 1 0\n0 1 2 3 2 1 0\n 0 1 2 1 0\n 0 1 0\n 0\n", " 0\n 0 1 0\n0 1 2 1 0\n 0 1 0\n 0\n"]}
| 463
| 1,558
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are the manager of a basketball team. For the upcoming tournament, you want to choose the team with the highest overall score. The score of the team is the sum of scores of all the players in the team.
However, the basketball team is not allowed to have conflicts. A conflict exists if a younger player has a strictly higher score than an older player. A conflict does not occur between players of the same age.
Given two lists, scores and ages, where each scores[i] and ages[i] represents the score and age of the ith player, respectively, return the highest overall score of all possible basketball teams.
Please complete the following python code precisely:
```python
class Solution:
def bestTeamScore(self, scores: List[int], ages: List[int]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(scores = [1,3,5,10,15], ages = [1,2,3,4,5]) == 34\n assert candidate(scores = [4,5,6,5], ages = [2,1,2,1]) == 16\n assert candidate(scores = [1,2,3,5], ages = [8,9,10,1]) == 6\n\n\ncheck(Solution().bestTeamScore)"}
| 179
| 116
|
coding
|
Solve the programming task below in a Python markdown code block.
Devu has an array A consisting of N positive integers. He would like to perform following operation on array.
- Pick some two elements a, b in the array (a could be same as b, but their corresponding indices in the array should not be same).
Remove both the elements a and b and instead add a number x such that x lies between min(a, b) and max(a, b), both inclusive, (i.e. min(a, b) ≤ x ≤ max(a, b)).
Now, as you know after applying the above operation N - 1 times, Devu will end up with a single number in the array. He is wondering whether it is possible to do the operations in such a way that he ends up a number t.
He asks your help in answering Q such queries, each of them will contain an integer t and you have to tell whether it is possible to end up t.
-----Input-----
There is only one test case per test file.
First line of the input contains two space separated integers N, Q denoting number of elements in A and number of queries for which Devu asks your help, respectively
Second line contains N space separated integers denoting the content of array A.
Each of the next Q lines, will contain a single integer t corresponding to the query.
-----Output-----
Output Q lines, each containing "Yes" or "No" (both without quotes) corresponding to the answer of corresponding query.
-----Constraints-----
- 1 ≤ N, Q ≤ 105
- 0 ≤ t ≤ 109
-----Subtasks-----
Subtask #1 : 30 points
- 1 ≤ Ai ≤ 2
Subtask #2 : 70 points
- 1 ≤ Ai ≤ 109
-----Example-----
Input 1:
1 2
1
1
2
Output:
Yes
No
Input 2:
2 4
1 3
1
2
3
4
Output:
Yes
Yes
Yes
No
-----Explanation-----
In the first example, Devu can't apply any operation. So the final element in the array will be 1 itself.
In the second example,
Devu can replace 1 and 3 with any of the numbers among 1, 2, 3. Hence final element of the array could be 1, 2 or 3.
|
{"inputs": ["1 2\n1\n1\n2", "1 1\n1\n0\n0", "1 1\n0\n0\n0", "1 2\n1\n1\n0", "1 2\n1\n0\n0", "0 2\n0\n0\n0", "1 2\n1\n1\n2", "1 1\n0\n-1\n0"], "outputs": ["Yes\nNo", "No\n", "Yes\n", "Yes\nNo\n", "No\nNo\n", "Yes\nYes\n", "Yes\nNo\n", "No\n"]}
| 511
| 136
|
coding
|
Solve the programming task below in a Python markdown code block.
Polycarp decided to relax on his weekend and visited to the performance of famous ropewalkers: Agafon, Boniface and Konrad.
The rope is straight and infinite in both directions. At the beginning of the performance, Agafon, Boniface and Konrad are located in positions $a$, $b$ and $c$ respectively. At the end of the performance, the distance between each pair of ropewalkers was at least $d$.
Ropewalkers can walk on the rope. In one second, only one ropewalker can change his position. Every ropewalker can change his position exactly by $1$ (i. e. shift by $1$ to the left or right direction on the rope). Agafon, Boniface and Konrad can not move at the same time (Only one of them can move at each moment). Ropewalkers can be at the same positions at the same time and can "walk past each other".
You should find the minimum duration (in seconds) of the performance. In other words, find the minimum number of seconds needed so that the distance between each pair of ropewalkers can be greater or equal to $d$.
Ropewalkers can walk to negative coordinates, due to the rope is infinite to both sides.
-----Input-----
The only line of the input contains four integers $a$, $b$, $c$, $d$ ($1 \le a, b, c, d \le 10^9$). It is possible that any two (or all three) ropewalkers are in the same position at the beginning of the performance.
-----Output-----
Output one integer — the minimum duration (in seconds) of the performance.
-----Examples-----
Input
5 2 6 3
Output
2
Input
3 1 5 6
Output
8
Input
8 3 3 2
Output
2
Input
2 3 10 4
Output
3
-----Note-----
In the first example: in the first two seconds Konrad moves for 2 positions to the right (to the position $8$), while Agafon and Boniface stay at their positions. Thus, the distance between Agafon and Boniface will be $|5 - 2| = 3$, the distance between Boniface and Konrad will be $|2 - 8| = 6$ and the distance between Agafon and Konrad will be $|5 - 8| = 3$. Therefore, all three pairwise distances will be at least $d=3$, so the performance could be finished within 2 seconds.
|
{"inputs": ["5 2 6 3\n", "3 1 5 6\n", "8 3 3 2\n", "1 3 2 5\n", "2 3 1 6\n", "9 6 2 5\n", "1 1 1 1\n", "1 2 1 1\n"], "outputs": ["2\n", "8\n", "2\n", "8\n", "10\n", "3\n", "2\n", "1\n"]}
| 572
| 119
|
coding
|
Solve the programming task below in a Python markdown code block.
It can be shown that any positive integer x can be uniquely represented as x = 1 + 2 + 4 + ... + 2^{k} - 1 + r, where k and r are integers, k ≥ 0, 0 < r ≤ 2^{k}. Let's call that representation prairie partition of x.
For example, the prairie partitions of 12, 17, 7 and 1 are: 12 = 1 + 2 + 4 + 5,
17 = 1 + 2 + 4 + 8 + 2,
7 = 1 + 2 + 4,
1 = 1.
Alice took a sequence of positive integers (possibly with repeating elements), replaced every element with the sequence of summands in its prairie partition, arranged the resulting numbers in non-decreasing order and gave them to Borys. Now Borys wonders how many elements Alice's original sequence could contain. Find all possible options!
-----Input-----
The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of numbers given from Alice to Borys.
The second line contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^12; a_1 ≤ a_2 ≤ ... ≤ a_{n}) — the numbers given from Alice to Borys.
-----Output-----
Output, in increasing order, all possible values of m such that there exists a sequence of positive integers of length m such that if you replace every element with the summands in its prairie partition and arrange the resulting numbers in non-decreasing order, you will get the sequence given in the input.
If there are no such values of m, output a single integer -1.
-----Examples-----
Input
8
1 1 2 2 3 4 5 8
Output
2
Input
6
1 1 1 2 2 2
Output
2 3
Input
5
1 2 4 4 4
Output
-1
-----Note-----
In the first example, Alice could get the input sequence from [6, 20] as the original sequence.
In the second example, Alice's original sequence could be either [4, 5] or [3, 3, 3].
|
{"inputs": ["1\n1\n", "1\n2\n", "1\n2\n", "1\n1\n", "1\n3\n", "1\n6\n", "1\n4\n", "1\n5\n"], "outputs": ["1 \n", "-1\n", "-1\n", "1 \n", "-1\n", "-1\n", "-1\n", "-1\n"]}
| 524
| 88
|
coding
|
Solve the programming task below in a Python markdown code block.
In this kata you will be given a random string of letters and tasked with returning them as a string of comma-separated sequences sorted alphabetically, with each sequence starting with an uppercase character followed by `n-1` lowercase characters, where `n` is the letter's alphabet position `1-26`.
## Example
```python
alpha_seq("ZpglnRxqenU") -> "Eeeee,Ggggggg,Llllllllllll,Nnnnnnnnnnnnnn,Nnnnnnnnnnnnnn,Pppppppppppppppp,Qqqqqqqqqqqqqqqqq,Rrrrrrrrrrrrrrrrrr,Uuuuuuuuuuuuuuuuuuuuu,Xxxxxxxxxxxxxxxxxxxxxxxx,Zzzzzzzzzzzzzzzzzzzzzzzzzz"
```
## Technical Details
- The string will include only letters.
- The first letter of each sequence is uppercase followed by `n-1` lowercase.
- Each sequence is separated with a comma.
- Return value needs to be a string.
Also feel free to reuse/extend the following starter code:
```python
def alpha_seq(string):
```
|
{"functional": "_inputs = [['BfcFA'], ['ZpglnRxqenU'], ['NyffsGeyylB'], ['MjtkuBovqrU'], ['EvidjUnokmM'], ['HbideVbxncC']]\n_outputs = [['A,Bb,Ccc,Ffffff,Ffffff'], ['Eeeee,Ggggggg,Llllllllllll,Nnnnnnnnnnnnnn,Nnnnnnnnnnnnnn,Pppppppppppppppp,Qqqqqqqqqqqqqqqqq,Rrrrrrrrrrrrrrrrrr,Uuuuuuuuuuuuuuuuuuuuu,Xxxxxxxxxxxxxxxxxxxxxxxx,Zzzzzzzzzzzzzzzzzzzzzzzzzz'], ['Bb,Eeeee,Ffffff,Ffffff,Ggggggg,Llllllllllll,Nnnnnnnnnnnnnn,Sssssssssssssssssss,Yyyyyyyyyyyyyyyyyyyyyyyyy,Yyyyyyyyyyyyyyyyyyyyyyyyy,Yyyyyyyyyyyyyyyyyyyyyyyyy'], ['Bb,Jjjjjjjjjj,Kkkkkkkkkkk,Mmmmmmmmmmmmm,Ooooooooooooooo,Qqqqqqqqqqqqqqqqq,Rrrrrrrrrrrrrrrrrr,Tttttttttttttttttttt,Uuuuuuuuuuuuuuuuuuuuu,Uuuuuuuuuuuuuuuuuuuuu,Vvvvvvvvvvvvvvvvvvvvvv'], ['Dddd,Eeeee,Iiiiiiiii,Jjjjjjjjjj,Kkkkkkkkkkk,Mmmmmmmmmmmmm,Mmmmmmmmmmmmm,Nnnnnnnnnnnnnn,Ooooooooooooooo,Uuuuuuuuuuuuuuuuuuuuu,Vvvvvvvvvvvvvvvvvvvvvv'], ['Bb,Bb,Ccc,Ccc,Dddd,Eeeee,Hhhhhhhh,Iiiiiiiii,Nnnnnnnnnnnnnn,Vvvvvvvvvvvvvvvvvvvvvv,Xxxxxxxxxxxxxxxxxxxxxxxx']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(alpha_seq(*i), o[0])"}
| 253
| 574
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given a positive integer n, there exists a 0-indexed array called powers, composed of the minimum number of powers of 2 that sum to n. The array is sorted in non-decreasing order, and there is only one way to form the array.
You are also given a 0-indexed 2D integer array queries, where queries[i] = [lefti, righti]. Each queries[i] represents a query where you have to find the product of all powers[j] with lefti <= j <= righti.
Return an array answers, equal in length to queries, where answers[i] is the answer to the ith query. Since the answer to the ith query may be too large, each answers[i] should be returned modulo 109 + 7.
Please complete the following python code precisely:
```python
class Solution:
def productQueries(self, n: int, queries: List[List[int]]) -> List[int]:
```
|
{"functional": "def check(candidate):\n assert candidate(n = 15, queries = [[0,1],[2,2],[0,3]]) == [2,4,64]\n assert candidate(n = 2, queries = [[0,0]]) == [2]\n\n\ncheck(Solution().productQueries)"}
| 214
| 73
|
coding
|
Solve the programming task below in a Python markdown code block.
Description
Since the cubic equation: ax ^ 3 + bx ^ 2 + cx + d = 0 is given, please check the number of positive real roots and the number of negative real roots, respectively.
The number of roots shall be counted including the multiple roots.
Input
The input consists of multiple test cases, and the number is recorded on the first line.
From the second line onward, the coefficients of the cubic equation are written in the form of a b c d. a is not 0, and each number is an integer value from -100 to 100.
Output
For a given cubic equation, output the number of positive real roots and the number of negative real roots separated by a space.
Example
Input
2
1 3 3 1
-10 0 0 0
Output
0 3
0 0
|
{"inputs": ["2\n1 4 3 0\n-1 0 1 1", "2\n1 4 3 0\n-1 0 0 0", "2\n1 4 3 0\n-1 0 1 0", "2\n2 4 3 0\n-1 0 1 0", "2\n1 3 3 1\n-8 0 0 0", "2\n1 4 5 0\n-1 0 0 1", "2\n1 4 5 0\n-1 0 0 0", "2\n1 4 3 1\n-3 0 0 1"], "outputs": ["0 2\n1 0\n", "0 2\n0 0\n", "0 2\n1 1\n", "0 0\n1 1\n", "0 3\n0 0\n", "0 0\n1 0\n", "0 0\n0 0\n", "0 1\n1 0\n"]}
| 196
| 246
|
coding
|
Solve the programming task below in a Python markdown code block.
Extended Euclid Algorithm
Given positive integers a and b, find the integer solution (x, y) to ax + by = gcd(a, b), where gcd(a, b) is the greatest common divisor of a and b.
Constraints
* 1 ≤ a, b ≤ 109
Input
a b
Two positive integers a and b are given separated by a space in a line.
Output
Print two integers x and y separated by a space. If there are several pairs of such x and y, print that pair for which |x| + |y| is the minimal (primarily) and x ≤ y (secondarily).
Examples
Input
4 12
Output
1 0
Input
3 8
Output
3 -1
|
{"inputs": ["5 8", "5 1", "5 2", "6 8", "5 4", "5 3", "8 3", "7 2"], "outputs": ["-3 2\n", "0 1\n", "1 -2\n", "-1 1\n", "1 -1\n", "-1 2\n", "-1 3\n", "1 -3\n"]}
| 173
| 95
|
coding
|
Solve the programming task below in a Python markdown code block.
Omkar is building a waterslide in his water park, and he needs your help to ensure that he does it as efficiently as possible.
Omkar currently has $n$ supports arranged in a line, the $i$-th of which has height $a_i$. Omkar wants to build his waterslide from the right to the left, so his supports must be nondecreasing in height in order to support the waterslide. In $1$ operation, Omkar can do the following: take any contiguous subsegment of supports which is nondecreasing by heights and add $1$ to each of their heights.
Help Omkar find the minimum number of operations he needs to perform to make his supports able to support his waterslide!
An array $b$ is a subsegment of an array $c$ if $b$ can be obtained from $c$ by deletion of several (possibly zero or all) elements from the beginning and several (possibly zero or all) elements from the end.
An array $b_1, b_2, \dots, b_n$ is called nondecreasing if $b_i\le b_{i+1}$ for every $i$ from $1$ to $n-1$.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \leq t \leq 100$). Description of the test cases follows.
The first line of each test case contains an integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the number of supports Omkar has.
The second line of each test case contains $n$ integers $a_{1},a_{2},...,a_{n}$ $(0 \leq a_{i} \leq 10^9)$ — the heights of the supports.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output a single integer — the minimum number of operations Omkar needs to perform to make his supports able to support his waterslide.
-----Example-----
Input
3
4
5 3 2 5
5
1 2 3 5 3
3
1 1 1
Output
3
2
0
-----Note-----
The subarray with which Omkar performs the operation is bolded.
In the first test case:
First operation:
$[5, 3, \textbf{2}, 5] \to [5, 3, \textbf{3}, 5]$
Second operation:
$[5, \textbf{3}, \textbf{3}, 5] \to [5, \textbf{4}, \textbf{4}, 5]$
Third operation:
$[5, \textbf{4}, \textbf{4}, 5] \to [5, \textbf{5}, \textbf{5}, 5]$
In the third test case, the array is already nondecreasing, so Omkar does $0$ operations.
|
{"inputs": ["1\n1\n1\n", "1\n1\n1\n", "1\n1\n0\n", "1\n4\n6 5 5 6\n", "1\n4\n6 5 5 6\n", "1\n4\n6 5 5 7\n", "1\n4\n6 4 5 7\n", "1\n4\n6 4 9 7\n"], "outputs": ["0\n", "0\n", "0\n", "1\n", "1\n", "1\n", "2\n", "4\n"]}
| 685
| 132
|
coding
|
Solve the programming task below in a Python markdown code block.
There is unrest in the Galactic Senate. Several thousand solar systems have declared their intentions to leave the Republic. Master Heidi needs to select the Jedi Knights who will go on peacekeeping missions throughout the galaxy. It is well-known that the success of any peacekeeping mission depends on the colors of the lightsabers of the Jedi who will go on that mission.
Heidi has n Jedi Knights standing in front of her, each one with a lightsaber of one of m possible colors. She knows that for the mission to be the most effective, she needs to select some contiguous interval of knights such that there are exactly k_1 knights with lightsabers of the first color, k_2 knights with lightsabers of the second color, ..., k_{m} knights with lightsabers of the m-th color. Help her find out if this is possible.
-----Input-----
The first line of the input contains n (1 ≤ n ≤ 100) and m (1 ≤ m ≤ n). The second line contains n integers in the range {1, 2, ..., m} representing colors of the lightsabers of the subsequent Jedi Knights. The third line contains m integers k_1, k_2, ..., k_{m} (with $1 \leq \sum_{i = 1}^{m} k_{i} \leq n$) – the desired counts of lightsabers of each color from 1 to m.
-----Output-----
Output YES if an interval with prescribed color counts exists, or output NO if there is none.
-----Example-----
Input
5 2
1 1 2 2 1
1 2
Output
YES
|
{"inputs": ["1 1\n1\n1\n", "1 1\n1\n1\n", "2 1\n1 1\n1\n", "2 1\n1 1\n2\n", "2 1\n1 1\n1\n", "2 1\n1 1\n2\n", "2 2\n1 2\n1 1\n", "2 2\n2 2\n0 2\n"], "outputs": ["YES\n", "YES\n", "YES\n", "YES\n", "YES\n", "YES\n", "YES\n", "YES\n"]}
| 360
| 134
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given an integer n. You have to apply m operations to it.
In a single operation, you must replace every digit d of the number with the decimal representation of integer d + 1. For example, 1912 becomes 21023 after applying the operation once.
You have to find the length of n after applying m operations. Since the answer can be very large, print it modulo 10^9+7.
Input
The first line contains a single integer t (1 ≤ t ≤ 2 ⋅ 10^5) — the number of test cases.
The only line of each test case contains two integers n (1 ≤ n ≤ 10^9) and m (1 ≤ m ≤ 2 ⋅ 10^5) — the initial number and the number of operations.
Output
For each test case output the length of the resulting number modulo 10^9+7.
Example
Input
5
1912 1
5 6
999 1
88 2
12 100
Output
5
2
6
4
2115
Note
For the first test, 1912 becomes 21023 after 1 operation which is of length 5.
For the second test, 5 becomes 21 after 6 operations which is of length 2.
For the third test, 999 becomes 101010 after 1 operation which is of length 6.
For the fourth test, 88 becomes 1010 after 2 operations which is of length 4.
|
{"inputs": ["5\n22 1\n26 9\n2 11\n3 6\n3 96\n", "5\n22 1\n26 61\n2 11\n3 6\n3 96\n", "5\n4 1\n42 61\n10 11\n3 1\n3 47\n", "5\n4 1\n42 61\n13 11\n3 1\n3 63\n", "5\n41 1\n26 13\n2 14\n3 6\n3 96\n", "5\n41 1\n26 13\n2 14\n3 6\n5 96\n", "5\n1 1\n42 61\n13 11\n3 1\n5 63\n", "5\n41 1\n26 13\n2 14\n3 6\n5 18\n"], "outputs": ["2\n4\n2\n1\n1023\n", "2\n170\n2\n1\n1023\n", "1\n135\n4\n1\n32\n", "1\n135\n4\n1\n86\n", "2\n5\n2\n1\n1023\n", "2\n5\n2\n1\n1035\n", "1\n135\n4\n1\n121\n", "2\n5\n2\n1\n4\n"]}
| 368
| 357
|
coding
|
Solve the programming task below in a Python markdown code block.
Given a time in AM/PM format as a string, convert it to military (24-hour) time as a string.
Midnight is 12:00:00AM on a 12-hour clock, and 00:00:00 on a 24-hour clock. Noon is 12:00:00PM on a 12-hour clock, and 12:00:00 on a 24-hour clock
Sample Input: 07:05:45PM
Sample Output: 19:05:45
Try not to use built in DateTime libraries.
For more information on military time, check the wiki https://en.wikipedia.org/wiki/24-hour_clock#Military_time
Also feel free to reuse/extend the following starter code:
```python
def get_military_time(time):
```
|
{"functional": "_inputs = [['12:00:01AM'], ['01:02:03AM'], ['02:04:05AM'], ['03:06:07AM'], ['04:08:09AM'], ['05:10:11AM'], ['06:12:13AM'], ['07:14:15AM'], ['08:16:17AM'], ['09:18:19AM'], ['10:20:21AM'], ['11:22:23AM'], ['12:24:25PM'], ['01:26:27PM'], ['02:28:29PM'], ['03:30:31PM'], ['04:32:33PM'], ['05:34:35PM'], ['06:36:37PM'], ['07:38:39PM'], ['08:40:41PM'], ['09:42:43PM'], ['10:44:45PM'], ['11:46:47PM']]\n_outputs = [['00:00:01'], ['01:02:03'], ['02:04:05'], ['03:06:07'], ['04:08:09'], ['05:10:11'], ['06:12:13'], ['07:14:15'], ['08:16:17'], ['09:18:19'], ['10:20:21'], ['11:22:23'], ['12:24:25'], ['13:26:27'], ['14:28:29'], ['15:30:31'], ['16:32:33'], ['17:34:35'], ['18:36:37'], ['19:38:39'], ['20:40:41'], ['21:42:43'], ['22:44:45'], ['23:46:47']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(get_military_time(*i), o[0])"}
| 200
| 654
|
coding
|
Solve the programming task below in a Python markdown code block.
This is a hard version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different.
You are given a string $s$ consisting of $n$ lowercase Latin letters.
You have to color all its characters the minimum number of colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in $s$).
After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times.
The goal is to make the string sorted, i.e. all characters should be in alphabetical order.
Your task is to find the minimum number of colors which you have to color the given string in so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps.
-----Input-----
The first line of the input contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of $s$.
The second line of the input contains the string $s$ consisting of exactly $n$ lowercase Latin letters.
-----Output-----
In the first line print one integer $res$ ($1 \le res \le n$) — the minimum number of colors in which you have to color the given string so that after coloring it can become sorted by some sequence of swaps.
In the second line print any possible coloring that can be used to sort the string using some sequence of swaps described in the problem statement. The coloring is the array $c$ of length $n$, where $1 \le c_i \le res$ and $c_i$ means the color of the $i$-th character.
-----Examples-----
Input
9
abacbecfd
Output
2
1 1 2 1 2 1 2 1 2
Input
8
aaabbcbb
Output
2
1 2 1 2 1 2 1 1
Input
7
abcdedc
Output
3
1 1 1 1 1 2 3
Input
5
abcde
Output
1
1 1 1 1 1
|
{"inputs": ["1\nr\n", "1\nr\n", "1\ns\n", "2\ntj\n", "2\ntj\n", "2\nti\n", "2\nit\n", "5\nabcde\n"], "outputs": ["1\n1 \n", "1\n1 \n", "1\n1 \n", "2\n1 2 \n", "2\n1 2 \n", "2\n1 2 \n", "1\n1 1 \n", "1\n1 1 1 1 1 \n"]}
| 514
| 130
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
There is an undirected tree with n nodes labeled from 0 to n - 1, rooted at node 0. You are given a 2D integer array edges of length n - 1 where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree.
At every node i, there is a gate. You are also given an array of even integers amount, where amount[i] represents:
the price needed to open the gate at node i, if amount[i] is negative, or,
the cash reward obtained on opening the gate at node i, otherwise.
The game goes on as follows:
Initially, Alice is at node 0 and Bob is at node bob.
At every second, Alice and Bob each move to an adjacent node. Alice moves towards some leaf node, while Bob moves towards node 0.
For every node along their path, Alice and Bob either spend money to open the gate at that node, or accept the reward. Note that:
If the gate is already open, no price will be required, nor will there be any cash reward.
If Alice and Bob reach the node simultaneously, they share the price/reward for opening the gate there. In other words, if the price to open the gate is c, then both Alice and Bob pay c / 2 each. Similarly, if the reward at the gate is c, both of them receive c / 2 each.
If Alice reaches a leaf node, she stops moving. Similarly, if Bob reaches node 0, he stops moving. Note that these events are independent of each other.
Return the maximum net income Alice can have if she travels towards the optimal leaf node.
Please complete the following python code precisely:
```python
class Solution:
def mostProfitablePath(self, edges: List[List[int]], bob: int, amount: List[int]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(edges = [[0,1],[1,2],[1,3],[3,4]], bob = 3, amount = [-2,4,2,-4,6]) == 6\n assert candidate(edges = [[0,1]], bob = 1, amount = [-7280,2350]) == -7280\n\n\ncheck(Solution().mostProfitablePath)"}
| 409
| 100
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given an integer array nums, return the length of the longest strictly increasing subsequence.
Please complete the following python code precisely:
```python
class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(nums = [10,9,2,5,3,7,101,18]) == 4\n assert candidate(nums = [0,1,0,3,2,3]) == 4\n assert candidate(nums = [7,7,7,7,7,7,7]) == 1\n\n\ncheck(Solution().lengthOfLIS)"}
| 67
| 97
|
coding
|
Solve the programming task below in a Python markdown code block.
This time minions are celebrating Diwali Festival. There are N minions in total. Each of them owns a house. On this Festival, Each of them wants to decorate their house. But none of them have enough money to do that. One of the minion, Kevin, requested Gru for money. Gru agreed for money distribution but he will be giving money to a minion if and only if demanded money is less than or equal to the money Gru have. Now Gru wonders if he can spend all the money or not.
-----Input-----
First line have number of test cases T. Each test case consist of Two Lines. First line contains two space separated integers N and K i.e. Number of minions and Amount of Money Gru have. Next line contains N space separated integers A1,A2,A3,.....,AN representing amount of money demanded by ith minion.
-----Output-----
Output YES if Gru can spend his all of the money on minions i.e. after distribution Gru have zero amount of money else NO.
-----Constraints-----
- 1 ≤ T ≤ 105
- 1 ≤ N ≤ 102
- 1 ≤ K,Ai ≤ 109
-----Example-----
Input:
2
4 9
5 2 2 4
4 9
5 2 18 3
Output:
YES
NO
-----Explanation-----
Example case 1.At first Gru is having 9 Rs. If he gives 5 Rs. to first minion then remaining 4 Rs. can be given to 2nd and 3rd minion or to the 4th minion. Which will leave zero amount of money in the hands of Gru.
Example case 2.At first Gru is having 9 Rs. If he gives 5 Rs. to the first minion then from remaining 4 Rs. either he can give 2 Rs. to the 2nd minion or 3 Rs. to the fourth minion. Which will leave either 2 Rs. or 1 Rs. in the hands of Gru.
|
{"inputs": ["2\n4 9\n5 2 2 4\n4 9\n5 2 18 3"], "outputs": ["YES\nNO"]}
| 442
| 39
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given a 0-indexed 2D integer array brackets where brackets[i] = [upperi, percenti] means that the ith tax bracket has an upper bound of upperi and is taxed at a rate of percenti. The brackets are sorted by upper bound (i.e. upperi-1 < upperi for 0 < i < brackets.length).
Tax is calculated as follows:
The first upper0 dollars earned are taxed at a rate of percent0.
The next upper1 - upper0 dollars earned are taxed at a rate of percent1.
The next upper2 - upper1 dollars earned are taxed at a rate of percent2.
And so on.
You are given an integer income representing the amount of money you earned. Return the amount of money that you have to pay in taxes. Answers within 10-5 of the actual answer will be accepted.
Please complete the following python code precisely:
```python
class Solution:
def calculateTax(self, brackets: List[List[int]], income: int) -> float:
```
|
{"functional": "def check(candidate):\n assert candidate(brackets = [[3,50],[7,10],[12,25]], income = 10) == 2.65000\n assert candidate(brackets = [[1,0],[4,25],[5,50]], income = 2) == 0.25000\n assert candidate(brackets = [[2,50]], income = 0) == 0.00000\n\n\ncheck(Solution().calculateTax)"}
| 232
| 121
|
coding
|
Solve the programming task below in a Python markdown code block.
Initially, you have the array $a$ consisting of one element $1$ ($a = [1]$).
In one move, you can do one of the following things:
Increase some (single) element of $a$ by $1$ (choose some $i$ from $1$ to the current length of $a$ and increase $a_i$ by one); Append the copy of some (single) element of $a$ to the end of the array (choose some $i$ from $1$ to the current length of $a$ and append $a_i$ to the end of the array).
For example, consider the sequence of five moves:
You take the first element $a_1$, append its copy to the end of the array and get $a = [1, 1]$. You take the first element $a_1$, increase it by $1$ and get $a = [2, 1]$. You take the second element $a_2$, append its copy to the end of the array and get $a = [2, 1, 1]$. You take the first element $a_1$, append its copy to the end of the array and get $a = [2, 1, 1, 2]$. You take the fourth element $a_4$, increase it by $1$ and get $a = [2, 1, 1, 3]$.
Your task is to find the minimum number of moves required to obtain the array with the sum at least $n$.
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. Then $t$ test cases follow.
The only line of the test case contains one integer $n$ ($1 \le n \le 10^9$) — the lower bound on the sum of the array.
-----Output-----
For each test case, print the answer: the minimum number of moves required to obtain the array with the sum at least $n$.
-----Example-----
Input
5
1
5
42
1337
1000000000
Output
0
3
11
72
63244
|
{"inputs": ["5\n2\n2\n18\n2\n1000001010\n", "5\n1\n4\n9\n129\n1000001000\n", "5\n2\n2\n16\n13\n1000000000\n", "5\n2\n13\n78\n7\n1000001100\n", "5\n2\n2\n18\n13\n1000001000\n", "5\n2\n7\n9\n284\n1000001000\n", "5\n1\n2\n9\n129\n1000001000\n", "5\n1\n4\n9\n110\n1000001010\n"], "outputs": ["1\n1\n7\n1\n63244\n", "0\n2\n4\n21\n63244\n", "1\n1\n6\n6\n63244\n", "1\n6\n16\n4\n63244\n", "1\n1\n7\n6\n63244\n", "1\n4\n4\n32\n63244\n", "0\n1\n4\n21\n63244\n", "0\n2\n4\n19\n63244\n"]}
| 518
| 338
|
coding
|
Solve the programming task below in a Python markdown code block.
As Sherlock Holmes was investigating a crime, he identified n suspects. He knows for sure that exactly one of them committed the crime. To find out which one did it, the detective lines up the suspects and numbered them from 1 to n. After that, he asked each one: "Which one committed the crime?". Suspect number i answered either "The crime was committed by suspect number ai", or "Suspect number ai didn't commit the crime". Also, the suspect could say so about himself (ai = i).
Sherlock Holmes understood for sure that exactly m answers were the truth and all other answers were a lie. Now help him understand this: which suspect lied and which one told the truth?
Input
The first line contains two integers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ n) — the total number of suspects and the number of suspects who told the truth. Next n lines contain the suspects' answers. The i-th line contains either "+ai" (without the quotes), if the suspect number i says that the crime was committed by suspect number ai, or "-ai" (without the quotes), if the suspect number i says that the suspect number ai didn't commit the crime (ai is an integer, 1 ≤ ai ≤ n).
It is guaranteed that at least one suspect exists, such that if he committed the crime, then exactly m people told the truth.
Output
Print n lines. Line number i should contain "Truth" if suspect number i has told the truth for sure. Print "Lie" if the suspect number i lied for sure and print "Not defined" if he could lie and could tell the truth, too, depending on who committed the crime.
Examples
Input
1 1
+1
Output
Truth
Input
3 2
-1
-2
-3
Output
Not defined
Not defined
Not defined
Input
4 1
+2
-3
+4
-1
Output
Lie
Not defined
Lie
Not defined
Note
The first sample has the single person and he confesses to the crime, and Sherlock Holmes knows that one person is telling the truth. That means that this person is telling the truth.
In the second sample there are three suspects and each one denies his guilt. Sherlock Holmes knows that only two of them are telling the truth. Any one of them can be the criminal, so we don't know for any of them, whether this person is telling the truth or not.
In the third sample the second and the fourth suspect defend the first and the third one. But only one is telling the truth, thus, the first or the third one is the criminal. Both of them can be criminals, so the second and the fourth one can either be lying or telling the truth. The first and the third one are lying for sure as they are blaming the second and the fourth one.
|
{"inputs": ["1 0\n-1\n", "1 1\n+1\n", "2 1\n+2\n+1\n", "2 0\n-2\n-2\n", "2 2\n+1\n+1\n", "3 1\n+2\n+3\n+3\n", "3 2\n-1\n+2\n+3\n", "3 0\n-2\n-2\n-2\n"], "outputs": ["Lie\n", "Truth\n", "Not defined\nNot defined\n", "Lie\nLie\n", "Truth\nTruth\n", "Truth\nLie\nLie\n", "Truth\nNot defined\nNot defined\n", "Lie\nLie\nLie\n"]}
| 615
| 159
|
coding
|
Solve the programming task below in a Python markdown code block.
Takahashi, Aoki and Snuke love cookies. They have A, B and C cookies, respectively. Now, they will exchange those cookies by repeating the action below:
* Each person simultaneously divides his cookies in half and gives one half to each of the other two persons.
This action will be repeated until there is a person with odd number of cookies in hand.
How many times will they repeat this action? Note that the answer may not be finite.
Constraints
* 1 ≤ A,B,C ≤ 10^9
Input
Input is given from Standard Input in the following format:
A B C
Output
Print the number of times the action will be performed by the three people, if this number is finite. If it is infinite, print `-1` instead.
Examples
Input
4 12 20
Output
3
Input
14 14 14
Output
-1
Input
454 414 444
Output
1
|
{"inputs": ["0 0 0", "4 0 13", "3 0 13", "6 0 13", "6 0 24", "1 25 5", "1 25 0", "2 25 0"], "outputs": ["-1\n", "0\n", "0\n", "0\n", "1\n", "0\n", "0\n", "0\n"]}
| 223
| 102
|
coding
|
Solve the programming task below in a Python markdown code block.
You invited $n$ guests to dinner! You plan to arrange one or more circles of chairs. Each chair is going to be either occupied by one guest, or be empty. You can make any number of circles.
Your guests happen to be a little bit shy, so the $i$-th guest wants to have a least $l_i$ free chairs to the left of his chair, and at least $r_i$ free chairs to the right. The "left" and "right" directions are chosen assuming all guests are going to be seated towards the center of the circle. Note that when a guest is the only one in his circle, the $l_i$ chairs to his left and $r_i$ chairs to his right may overlap.
What is smallest total number of chairs you have to use?
-----Input-----
First line contains one integer $n$ — number of guests, ($1 \leqslant n \leqslant 10^5$).
Next $n$ lines contain $n$ pairs of space-separated integers $l_i$ and $r_i$ ($0 \leqslant l_i, r_i \leqslant 10^9$).
-----Output-----
Output a single integer — the smallest number of chairs you have to use.
-----Examples-----
Input
3
1 1
1 1
1 1
Output
6
Input
4
1 2
2 1
3 5
5 3
Output
15
Input
1
5 6
Output
7
-----Note-----
In the second sample the only optimal answer is to use two circles: a circle with $5$ chairs accomodating guests $1$ and $2$, and another one with $10$ chairs accomodationg guests $3$ and $4$.
In the third sample, you have only one circle with one person. The guest should have at least five free chairs to his left, and at least six free chairs to his right to the next person, which is in this case the guest herself. So, overall number of chairs should be at least 6+1=7.
|
{"inputs": ["1\n5 6\n", "1\n0 0\n", "1\n0 0\n", "1\n5 8\n", "1\n0 8\n", "1\n0 9\n", "1\n5 6\n", "2\n6 6\n3 3\n"], "outputs": ["7\n", "1\n", "1\n", "9\n", "9\n", "10\n", "7\n", "11\n"]}
| 468
| 108
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a string S consisting of lowercase English letters.
Find the lexicographically (alphabetically) smallest lowercase English letter that does not occur in S.
If every lowercase English letter occurs in S, print None instead.
-----Constraints-----
- 1 \leq |S| \leq 10^5 (|S| is the length of string S.)
- S consists of lowercase English letters.
-----Input-----
Input is given from Standard Input in the following format:
S
-----Output-----
Print the lexicographically smallest lowercase English letter that does not occur in S.
If every lowercase English letter occurs in S, print None instead.
-----Sample Input-----
atcoderregularcontest
-----Sample Output-----
b
The string atcoderregularcontest contains a, but does not contain b.
|
{"inputs": ["atcoderregularcontest\n", "abcdefghijklmnopqrstuvwxyz\n", "fajsonlslfepbjtsaayxbymeskptcumtwrmkkinjxnnucagfrg\n"], "outputs": ["b\n", "None\n", "d\n"]}
| 175
| 59
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given an m x n grid grid of values 0, 1, or 2, where:
each 0 marks an empty land that you can pass by freely,
each 1 marks a building that you cannot pass through, and
each 2 marks an obstacle that you cannot pass through.
You want to build a house on an empty land that reaches all buildings in the shortest total travel distance. You can only move up, down, left, and right.
Return the shortest travel distance for such a house. If it is not possible to build such a house according to the above rules, return -1.
The total travel distance is the sum of the distances between the houses of the friends and the meeting point.
The distance is calculated using Manhattan Distance, where distance(p1, p2) = |p2.x - p1.x| + |p2.y - p1.y|.
Please complete the following python code precisely:
```python
class Solution:
def shortestDistance(self, grid: List[List[int]]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(grid = [[1,0,2,0,1],[0,0,0,0,0],[0,0,1,0,0]]) == 7\n assert candidate(grid = [[1,0]]) == 1\n assert candidate(grid = [[1]]) == -1\n\n\ncheck(Solution().shortestDistance)"}
| 233
| 86
|
coding
|
Solve the programming task below in a Python markdown code block.
You have a laser with P amount of energy in it. You are playing a game where you have to use the laser to destroy some crystals, each with some health of their own. Initially, you have 0 points.
Each crystal has an associated health. The health of the ith crystal is given by health[i]. You can perform one of the two actions:
-
At the cost of health[i] energy, you can destroy the ith crystal and gain 1 point. You can only perform this action if your laser has atleast health[i] energy in it.
-
At the cost of 1 point, you can destroy the ith crystal and refuel the laser's energy by an amount equal to health[i]. This action can only be performed if you have atleast one point.
Note: Each crystal can only be destroyed once.
Determine the maximum number of points you can obtain by destroying any number of crystals and performing either action as you wish.
-----Input:-----
- First line will contain n, number of crystals.
- Second line will contain space separated integers, health of each crystal.
- Third line will contain an integer P, initial energy of the laser.
-----Output:-----
Print the largest number of points we can have after destroying any number of crystals.
-----Constraints-----
- health.length <= 1000
- 0 <= health[i] < 10000
- 0 <= P < 10000
-----Subtasks-----
- 40 points : 1 <= health.length <= 100
- 60 points : health.length > 100
-----Sample Input 1:-----
1
200
100
-----Sample Output 1:-----
0
-----Explanation:-----
The laser initially has only 100 energy. Since the only crystal requires 200 energy to destroy, we cannot perform any action here, and the game ends. The number of points is zero.
-----Sample Input 2:-----
2
100 200
150
-----Sample Output 2:-----
1
-----Explanation:-----
The laser has 150 energy. We can consume 100 energy and destroy the first crystal and gain 1 point. The remaining energy is 50, which is not enough to destroy the other crystal. We end the game here as we have the maximum possible points attainable.
|
{"inputs": ["1\n200\n100"], "outputs": ["0"]}
| 514
| 20
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
There are n cities numbered from 1 to n. You are given an array edges of size n-1, where edges[i] = [ui, vi] represents a bidirectional edge between cities ui and vi. There exists a unique path between each pair of cities. In other words, the cities form a tree.
A subtree is a subset of cities where every city is reachable from every other city in the subset, where the path between each pair passes through only the cities from the subset. Two subtrees are different if there is a city in one subtree that is not present in the other.
For each d from 1 to n-1, find the number of subtrees in which the maximum distance between any two cities in the subtree is equal to d.
Return an array of size n-1 where the dth element (1-indexed) is the number of subtrees in which the maximum distance between any two cities is equal to d.
Notice that the distance between the two cities is the number of edges in the path between them.
Please complete the following python code precisely:
```python
class Solution:
def countSubgraphsForEachDiameter(self, n: int, edges: List[List[int]]) -> List[int]:
```
|
{"functional": "def check(candidate):\n assert candidate(n = 4, edges = [[1,2],[2,3],[2,4]]) == [3,4,0]\n assert candidate(n = 2, edges = [[1,2]]) == [1]\n assert candidate(n = 3, edges = [[1,2],[2,3]]) == [2,1]\n\n\ncheck(Solution().countSubgraphsForEachDiameter)"}
| 272
| 101
|
coding
|
Solve the programming task below in a Python markdown code block.
Find the sum of the integers between 1 and N (inclusive), whose sum of digits written in base 10 is between A and B (inclusive).
-----Constraints-----
- 1 \leq N \leq 10^4
- 1 \leq A \leq B \leq 36
- All input values are integers.
-----Input-----
Input is given from Standard Input in the following format:
N A B
-----Output-----
Print the sum of the integers between 1 and N (inclusive), whose sum of digits written in base 10 is between A and B (inclusive).
-----Sample Input-----
20 2 5
-----Sample Output-----
84
Among the integers not greater than 20, the ones whose sums of digits are between 2 and 5, are: 2,3,4,5,11,12,13,14 and 20. We should print the sum of these, 84.
|
{"inputs": ["4 1 6", "2 0 8", "2 2 2", "3 0 4", "6 2 3", "6 2 5", "4 4 6", "5 1 0"], "outputs": ["10\n", "3\n", "2\n", "6\n", "5\n", "14\n", "4\n", "0\n"]}
| 223
| 96
|
coding
|
Solve the programming task below in a Python markdown code block.
As we know, DZY loves playing games. One day DZY decided to play with a n × m matrix. To be more precise, he decided to modify the matrix with exactly k operations.
Each modification is one of the following:
1. Pick some row of the matrix and decrease each element of the row by p. This operation brings to DZY the value of pleasure equal to the sum of elements of the row before the decreasing.
2. Pick some column of the matrix and decrease each element of the column by p. This operation brings to DZY the value of pleasure equal to the sum of elements of the column before the decreasing.
DZY wants to know: what is the largest total value of pleasure he could get after performing exactly k modifications? Please, help him to calculate this value.
Input
The first line contains four space-separated integers n, m, k and p (1 ≤ n, m ≤ 103; 1 ≤ k ≤ 106; 1 ≤ p ≤ 100).
Then n lines follow. Each of them contains m integers representing aij (1 ≤ aij ≤ 103) — the elements of the current row of the matrix.
Output
Output a single integer — the maximum possible total pleasure value DZY could get.
Examples
Input
2 2 2 2
1 3
2 4
Output
11
Input
2 2 5 2
1 3
2 4
Output
11
Note
For the first sample test, we can modify: column 2, row 2. After that the matrix becomes:
1 1
0 0
For the second sample test, we can modify: column 2, row 2, row 1, column 1, column 2. After that the matrix becomes:
-3 -3
-2 -2
|
{"inputs": ["2 2 2 2\n1 6\n2 4\n", "2 2 5 2\n1 3\n2 8\n", "2 2 2 2\n1 6\n2 0\n", "2 2 5 2\n1 3\n0 8\n", "2 2 5 2\n1 3\n0 6\n", "2 2 5 2\n2 3\n0 6\n", "2 2 2 2\n1 3\n2 4\n", "2 2 5 2\n1 3\n2 4\n"], "outputs": ["16\n", "26\n", "11\n", "22\n", "14\n", "15\n", "11\n", "11\n"]}
| 423
| 190
|
coding
|
Solve the programming task below in a Python markdown code block.
Given an array of integers, find the longest subarray where the absolute difference between any two elements is less than or equal to $\mbox{1}$.
Example
$a=[1,1,2,2,4,4,5,5,5]$
There are two subarrays meeting the criterion: $[1,1,2,2]$ and $[4,4,5,5,5]$. The maximum length subarray has $5$ elements.
Function Description
Complete the pickingNumbers function in the editor below.
pickingNumbers has the following parameter(s):
int a[n]: an array of integers
Returns
int: the length of the longest subarray that meets the criterion
Input Format
The first line contains a single integer $n$, the size of the array $\boldsymbol{a}$.
The second line contains $n$ space-separated integers, each an $a[i]$.
Constraints
$2\leq n\leq100$
$0<a[i]<100$
The answer will be $\geq2$.
Sample Input 0
6
4 6 5 3 3 1
Sample Output 0
3
Explanation 0
We choose the following multiset of integers from the array: $\{4,3,3\}$. Each pair in the multiset has an absolute difference $\leq1$ (i.e., $|4-3|=1$ and $|3-3|=0$), so we print the number of chosen integers, $3$, as our answer.
Sample Input 1
6
1 2 2 3 1 2
Sample Output 1
5
Explanation 1
We choose the following multiset of integers from the array: $\{1,2,2,1,2\}$. Each pair in the multiset has an absolute difference $\leq1$ (i.e., $|1-2|=1$, $|1-1|=0$, and $|2-2|=0$), so we print the number of chosen integers, $5$, as our answer.
|
{"inputs": ["6\n4 6 5 3 3 1\n", "6\n1 2 2 3 1 2\n"], "outputs": ["3\n", "5\n"]}
| 465
| 46
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a string $a$, consisting of $n$ characters, $n$ is even. For each $i$ from $1$ to $n$ $a_i$ is one of 'A', 'B' or 'C'.
A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)"), and ")(", "(" and ")" are not.
You want to find a string $b$ that consists of $n$ characters such that:
$b$ is a regular bracket sequence;
if for some $i$ and $j$ ($1 \le i, j \le n$) $a_i=a_j$, then $b_i=b_j$.
In other words, you want to replace all occurrences of 'A' with the same type of bracket, then all occurrences of 'B' with the same type of bracket and all occurrences of 'C' with the same type of bracket.
Your task is to determine if such a string $b$ exists.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the descriptions of $t$ testcases follow.
The only line of each testcase contains a string $a$. $a$ consists only of uppercase letters 'A', 'B' and 'C'. Let $n$ be the length of $a$. It is guaranteed that $n$ is even and $2 \le n \le 50$.
-----Output-----
For each testcase print "YES" if there exists such a string $b$ that:
$b$ is a regular bracket sequence;
if for some $i$ and $j$ ($1 \le i, j \le n$) $a_i=a_j$, then $b_i=b_j$.
Otherwise, print "NO".
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).
-----Examples-----
Input
4
AABBAC
CACA
BBBBAC
ABCA
Output
YES
YES
NO
NO
-----Note-----
In the first testcase one of the possible strings $b$ is "(())()".
In the second testcase one of the possible strings $b$ is "()()".
|
{"inputs": ["3\nAABB\nCAAB\nABCA\n", "3\nAABB\nCAAB\nABCA\n", "3\nAABB\nBAAC\nABCA\n", "3\nAAAB\nBAAC\nABCA\n", "3\nBAAA\nAABC\nABCA\n", "3\nAABB\nACAB\nABCA\n", "3\nBAAA\nAABB\nBACA\n", "3\nBAAA\nBAAC\nABCA\n"], "outputs": ["YES\nNO\nNO\n", "YES\nNO\nNO\n", "YES\nNO\nNO\n", "NO\nNO\nNO\n", "NO\nYES\nNO\n", "YES\nYES\nNO\n", "NO\nYES\nYES\n", "NO\nNO\nNO\n"]}
| 556
| 174
|
coding
|
Solve the programming task below in a Python markdown code block.
Given an array of integers your solution should find the smallest integer.
For example:
- Given `[34, 15, 88, 2]` your solution will return `2`
- Given `[34, -345, -1, 100]` your solution will return `-345`
You can assume, for the purpose of this kata, that the supplied array will not be empty.
Also feel free to reuse/extend the following starter code:
```python
def find_smallest_int(arr):
```
|
{"functional": "_inputs = [[[78, 56, 232, 12, 11, 43]], [[78, 56, -2, 12, 8, -33]], [[0, -18446744073709551615, 18446744073709551616]], [[-133, -5666, -89, -12341, -321423, 18446744073709551616]], [[0, 18446744073709551616, -18446744073709551617, 18446744073709551616, 18446744073709551616]], [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]], [[-1, -2, -3, -4, -5, -6, -7, -8, -9, -10]], [[-78, 56, 232, 12, 8]], [[78, 56, -2, 12, -8]]]\n_outputs = [[11], [-33], [-18446744073709551615], [-321423], [-18446744073709551617], [1], [-10], [-78], [-8]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(find_smallest_int(*i), o[0])"}
| 127
| 564
|
coding
|
Solve the programming task below in a Python markdown code block.
Inspired by the development team at Vooza, write the function `howManyLightsabersDoYouOwn`/`how_many_light_sabers_do_you_own` that
* accepts the name of a programmer, and
* returns the number of lightsabers owned by that person.
The only person who owns lightsabers is Zach, by the way. He owns 18, which is an awesome number of lightsabers. Anyone else owns 0.
```if:coffeescript,javascript,php,python,ruby,typescript
**Note**: your function should have a default parameter.
```
```c#
Kata.HowManyLightsabersDoYouOwn("Adam") == 0
Kata.HowManyLightsabersDoYouOwn("Zach") == 18
```
```python
how_many_light_sabers_do_you_own('Zach') == 18
how_many_light_sabers_do_you_own('Adam') == 0
how_many_light_sabers_do_you_own() == 0
```
Also feel free to reuse/extend the following starter code:
```python
def how_many_light_sabers_do_you_own(*name):
```
|
{"functional": "_inputs = [['Zach'], ['zach']]\n_outputs = [[18], [0]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(how_many_light_sabers_do_you_own(*i), o[0])"}
| 266
| 171
|
coding
|
Solve the programming task below in a Python markdown code block.
Recently Vova found $n$ candy wrappers. He remembers that he bought $x$ candies during the first day, $2x$ candies during the second day, $4x$ candies during the third day, $\dots$, $2^{k-1} x$ candies during the $k$-th day. But there is an issue: Vova remembers neither $x$ nor $k$ but he is sure that $x$ and $k$ are positive integers and $k > 1$.
Vova will be satisfied if you tell him any positive integer $x$ so there is an integer $k>1$ that $x + 2x + 4x + \dots + 2^{k-1} x = n$. It is guaranteed that at least one solution exists. Note that $k > 1$.
You have to answer $t$ independent test cases.
-----Input-----
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow.
The only line of the test case contains one integer $n$ ($3 \le n \le 10^9$) — the number of candy wrappers Vova found. It is guaranteed that there is some positive integer $x$ and integer $k>1$ that $x + 2x + 4x + \dots + 2^{k-1} x = n$.
-----Output-----
Print one integer — any positive integer value of $x$ so there is an integer $k>1$ that $x + 2x + 4x + \dots + 2^{k-1} x = n$.
-----Example-----
Input
7
3
6
7
21
28
999999999
999999984
Output
1
2
1
7
4
333333333
333333328
-----Note-----
In the first test case of the example, one of the possible answers is $x=1, k=2$. Then $1 \cdot 1 + 2 \cdot 1$ equals $n=3$.
In the second test case of the example, one of the possible answers is $x=2, k=2$. Then $1 \cdot 2 + 2 \cdot 2$ equals $n=6$.
In the third test case of the example, one of the possible answers is $x=1, k=3$. Then $1 \cdot 1 + 2 \cdot 1 + 4 \cdot 1$ equals $n=7$.
In the fourth test case of the example, one of the possible answers is $x=7, k=2$. Then $1 \cdot 7 + 2 \cdot 7$ equals $n=21$.
In the fifth test case of the example, one of the possible answers is $x=4, k=3$. Then $1 \cdot 4 + 2 \cdot 4 + 4 \cdot 4$ equals $n=28$.
|
{"inputs": ["1\n6\n", "1\n7\n", "1\n6\n", "1\n7\n", "1\n9\n", "1\n3\n", "1\n14\n", "1\n27\n"], "outputs": ["2\n", "1\n", "2\n", "1\n", "3\n", "1\n", "2\n", "9\n"]}
| 698
| 88
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Please complete the following python code precisely:
```python
class Solution:
def strStr(self, haystack: str, needle: str) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(haystack = \"sadbutsad\", needle = \"sad\") == 0\n assert candidate(haystack = \"leetcode\", needle = \"leeto\") == -1\n\n\ncheck(Solution().strStr)"}
| 81
| 61
|
coding
|
Solve the programming task below in a Python markdown code block.
An integer X is called a Harshad number if X is divisible by f(X), where f(X) is the sum of the digits in X when written in base 10.
Given an integer N, determine whether it is a Harshad number.
-----Constraints-----
- 1?N?10^8
- N is an integer.
-----Input-----
Input is given from Standard Input in the following format:
N
-----Output-----
Print Yes if N is a Harshad number; print No otherwise.
-----Sample Input-----
12
-----Sample Output-----
Yes
f(12)=1+2=3. Since 12 is divisible by 3, 12 is a Harshad number.
|
{"inputs": ["1", "2", "3", "6", "8", "5", "4", "9"], "outputs": ["Yes\n", "Yes\n", "Yes\n", "Yes\n", "Yes\n", "Yes\n", "Yes\n", "Yes\n"]}
| 164
| 62
|
coding
|
Solve the programming task below in a Python markdown code block.
You are beta testing the new secret Terraria update. This update will add quests to the game!
Simply, the world map can be represented as an array of length $n$, where the $i$-th column of the world has height $a_i$.
There are $m$ quests you have to test. The $j$-th of them is represented by two integers $s_j$ and $t_j$. In this quest, you have to go from the column $s_j$ to the column $t_j$. At the start of the quest, you are appearing at the column $s_j$.
In one move, you can go from the column $x$ to the column $x-1$ or to the column $x+1$. In this version, you have Spectre Boots, which allow you to fly. Since it is a beta version, they are bugged, so they only allow you to fly when you are going up and have infinite fly duration. When you are moving from the column with the height $p$ to the column with the height $q$, then you get some amount of fall damage. If the height $p$ is greater than the height $q$, you get $p - q$ fall damage, otherwise you fly up and get $0$ damage.
For each of the given quests, determine the minimum amount of fall damage you can get during this quest.
-----Input-----
The first line of the input contains two integers $n$ and $m$ ($2 \le n \le 10^5; 1 \le m \le 10^5$) — the number of columns in the world and the number of quests you have to test, respectively.
The second line of the input contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the height of the $i$-th column of the world.
The next $m$ lines describe quests. The $j$-th of them contains two integers $s_j$ and $t_j$ ($1 \le s_j, t_j \le n; s_j \ne t_j$), which means you have to move from the column $s_j$ to the column $t_j$ during the $j$-th quest.
Note that $s_j$ can be greater than $t_j$.
-----Output-----
Print $m$ integers. The $j$-th of them should be the minimum amount of fall damage you can get during the $j$-th quest completion.
-----Examples-----
Input
7 6
10 8 9 6 8 12 7
1 2
1 7
4 6
7 1
3 5
4 2
Output
2
10
0
7
3
1
-----Note-----
None
|
{"inputs": ["10 1\n5 5 5 3 5 5 5 5 5 1\n10 7\n", "7 6\n10 8 9 6 8 12 7\n1 2\n1 7\n4 6\n7 1\n3 5\n4 2\n", "8 6\n1 2 3 4 5 6 7 8\n1 8\n7 1\n1 2\n2 3\n4 5\n4 5\n", "10 4\n1 1 1 1 1 1 1 1 1 1\n1 10\n10 1\n10 9\n1 2\n", "38 1\n2 999999999 2 999999999 2 999999999 2 999999999 2 999999999 2 999999999 2 999999999 2 999999999 2 999999999 2 999999999 2 999999999 2 999999999 2 999999999 2 999999999 2 999999999 2 999999999 2 999999999 2 999999999 2 999999999\n7 2\n"], "outputs": ["0\n", "2\n10\n0\n7\n3\n1\n", "0\n6\n0\n0\n0\n0\n", "0\n0\n0\n0\n", "1999999994\n"]}
| 630
| 471
|
coding
|
Solve the programming task below in a Python markdown code block.
There are $N$ friends in a group. Each of them have $A_{i}$ candies.
Can they share all of these candies among themselves such that each one of them have equal no. of candies.
-----Input:-----
- First line will contain $T$, number of testcases. Then the testcases follow.
- First line of each testcase contains of a single line of input, an integer $N$ denoting no. of friends in the group.
- Next line contains $N$ space separated integers $A_{i}$ denoting the no. candies $i^{th}$ friend has.
-----Output:-----
For each testcase, output $"Yes"$ if it is possible to share equally else $"No"$ (without " ").
-----Constraints-----
- $1 \leq T \leq 10$
- $1 \leq N \leq 100$
- $0 \leq A_{i} \leq 1000$
-----Sample Input:-----
1
3
1 2 3
-----Sample Output:-----
Yes
-----EXPLANATION:-----
Each of them have $2$ candies after sharing.
|
{"inputs": ["1\n3\n1 2 3"], "outputs": ["Yes"]}
| 259
| 20
|
coding
|
Solve the programming task below in a Python markdown code block.
This is another problem about Indraneel's library. His library has one long shelf. His books are numbered and he identifies the books by their number. Each book has a distinct number.
He has lost many books, since many of his friends borrow his books and never bother to return them. He does not want to lose any more books and has decided to keep a record of all books that he lends to his friends. To make the task of borrowing a book a little difficult, he has given the following instructions to his friends: when they borrow a book, they must record in a register its position from the left among the books currently on the shelf.
Suppose there are $5$ books in the library and they are arranged as follows:
261421532614215326 \quad 1 \quad 42 \quad 15 \quad 3
If someone walks in and borrows the book $42$, then he will record $3$ in the register because this book is the third from the left on the shelf. Now the shelf looks like this:
26115326115326 \quad 1 \quad 15 \quad 3
If the next person borrow the book $3$, he writes down $4$ in the register since this is currently the fourth book from the left on the shelf, and so on.
Indraneel knows the initial arrangement of the books in his library at the time that he introduced the register system. After a while he examines his register and would like to know which books have been borrowed. Your task is to write a program to help Indraneel solve this problem.
-----Input:-----
The first line of the input contains a single integer $M$ indicating the number of books in Indraneel's library. The next line contains $M$ distinct positive integers describing the sequence in which the books are arranged on the library shelf. The third line of input contains a single integer $N$ indicating the number of entries in the register. This, in turn, is followed by $N$ lines (lines $4$ to $N+3$), each containing one positive integer. The integer on line $3+i$ indicates the position from left of the book ith book borrowed. (You may assume that the number on line $3+i$ is at most $M-i+1$.)
-----Output:-----
$N$ lines with one positive integer on each line. The number on line $i$ is the book borrowed by the ith borrower.
-----Constraints:-----
- $1 \leq M \leq 1000000$.
- $1 \leq N \leq 4000$.
-----Sample Input-----
5
26 1 42 15 3
2
3
4
-----Sample Output-----
42
3
|
{"inputs": ["5\n26 1 42 15 3\n2\n3\n4"], "outputs": ["42\n3"]}
| 622
| 34
|
coding
|
Solve the programming task below in a Python markdown code block.
We have a string S of length N consisting of A, T, C, and G.
Strings T_1 and T_2 of the same length are said to be complementary when, for every i (1 \leq i \leq l), the i-th character of T_1 and the i-th character of T_2 are complementary. Here, A and T are complementary to each other, and so are C and G.
Find the number of non-empty contiguous substrings T of S that satisfies the following condition:
- There exists a string that is a permutation of T and is complementary to T.
Here, we distinguish strings that originate from different positions in S, even if the contents are the same.
-----Constraints-----
- 1 \leq N \leq 5000
- S consists of A, T, C, and G.
-----Input-----
Input is given from Standard Input in the following format:
N S
-----Output-----
Print the number of non-empty contiguous substrings T of S that satisfies the condition.
-----Sample Input-----
4 AGCT
-----Sample Output-----
2
The following two substrings satisfy the condition:
- GC (the 2-nd through 3-rd characters) is complementary to CG, which is a permutation of GC.
- AGCT (the 1-st through 4-th characters) is complementary to TCGA, which is a permutation of AGCT.
|
{"inputs": ["1 A\n", "4 AGCT\n", "4 ATAT\n", "10 AAATACCGCG\n"], "outputs": ["0\n", "2\n", "4\n", "6\n"]}
| 307
| 49
|
coding
|
Solve the programming task below in a Python markdown code block.
Chef wants to give a burger party to all his N friends i.e. he wants to buy one burger for each of his friends.
The cost of each burger is X rupees while Chef has a total of K rupees.
Determine whether he has enough money to buy a burger for each of his friends or not.
------ Input Format ------
- The first line contains a single integer T - the number of test cases. Then the test cases follow.
- The first and only line of each test case contains the three integers N, X, and K - the number of Chef's friends, the cost of each burger, and the total money Chef has, respectively.
------ Output Format ------
For each test case, output YES if the Chef can give a party to all his N friends. Otherwise, output NO.
You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical).
------ Constraints ------
$1 ≤T ≤1000$
$1 ≤N, X ≤100$
$1 ≤K ≤10000$
----- Sample Input 1 ------
4
5 10 70
5 10 40
10 40 400
14 14 150
----- Sample Output 1 ------
YES
NO
YES
NO
----- explanation 1 ------
- Test case $1$: Chef has $5$ friends. The cost of buying a burger for each of them will be $10 \times 5 = 50$ while Chef has $70$ rupees. Therefore, he can buy a burger for all of them.
- Test case $2$: Chef has $5$ friends. The cost of buying a burger for each of them will be $10 \times 5 = 50$ while Chef has $40$ rupees. Therefore, he can not buy a burger for all of them.
- Test case $3$: Chef has $10$ friends. The cost of buying a burger for each of them will be $40 \times 10 = 400$ and Chef has $400$ rupees. Therefore, he can buy a burger for all of them.
- Test case $4$: Chef has $14$ friends. The cost of buying a burger for each of them will be $14 \times 14 = 196$ while Chef has $150$ rupees. Therefore, he can not buy a burger for all of them.
|
{"inputs": ["4\n5 10 70\n5 10 40\n10 40 400\n14 14 150\n"], "outputs": ["YES\nNO\nYES\nNO\n"]}
| 555
| 56
|
coding
|
Solve the programming task below in a Python markdown code block.
Write a program of the Bubble Sort algorithm which sorts a sequence A in ascending order. The algorithm should be based on the following pseudocode:
BubbleSort(A)
1 for i = 0 to A.length-1
2 for j = A.length-1 downto i+1
3 if A[j] < A[j-1]
4 swap A[j] and A[j-1]
Note that, indices for array elements are based on 0-origin.
Your program should also print the number of swap operations defined in line 4 of the pseudocode.
Constraints
1 ≤ N ≤ 100
Input
The first line of the input includes an integer N, the number of elements in the sequence.
In the second line, N elements of the sequence are given separated by spaces characters.
Output
The output consists of 2 lines.
In the first line, please print the sorted sequence. Two contiguous elements of the sequence should be separated by a space character.
In the second line, please print the number of swap operations.
Examples
Input
5
5 3 2 4 1
Output
1 2 3 4 5
8
Input
6
5 2 4 6 1 3
Output
1 2 3 4 5 6
9
|
{"inputs": ["5\n7 3 2 4 1", "5\n7 3 2 3 1", "5\n7 3 2 3 0", "5\n7 3 4 3 0", "5\n7 1 4 3 0", "5\n7 1 4 3 1", "5\n4 2 4 3 1", "5\n4 2 5 3 1"], "outputs": ["1 2 3 4 7\n8\n", "1 2 3 3 7\n8\n", "0 2 3 3 7\n8\n", "0 3 3 4 7\n8\n", "0 1 3 4 7\n8\n", "1 1 3 4 7\n7\n", "1 2 3 4 4\n7\n", "1 2 3 4 5\n7\n"]}
| 288
| 222
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given an n x n grid containing only values 0 and 1, where 0 represents water and 1 represents land, find a water cell such that its distance to the nearest land cell is maximized, and return the distance. If no land or water exists in the grid, return -1.
The distance used in this problem is the Manhattan distance: the distance between two cells (x0, y0) and (x1, y1) is |x0 - x1| + |y0 - y1|.
Please complete the following python code precisely:
```python
class Solution:
def maxDistance(self, grid: List[List[int]]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(grid = [[1,0,1],[0,0,0],[1,0,1]]) == 2\n assert candidate(grid = [[1,0,0],[0,0,0],[0,0,0]]) == 4\n\n\ncheck(Solution().maxDistance)"}
| 163
| 75
|
coding
|
Solve the programming task below in a Python markdown code block.
There are two rival donut shops.
The first shop sells donuts at retail: each donut costs a dollars.
The second shop sells donuts only in bulk: box of b donuts costs c dollars. So if you want to buy x donuts from this shop, then you have to buy the smallest number of boxes such that the total number of donuts in them is greater or equal to x.
You want to determine two positive integer values:
1. how many donuts can you buy so that they are strictly cheaper in the first shop than in the second shop?
2. how many donuts can you buy so that they are strictly cheaper in the second shop than in the first shop?
If any of these values doesn't exist then that value should be equal to -1. If there are multiple possible answers, then print any of them.
The printed values should be less or equal to 10^9. It can be shown that under the given constraints such values always exist if any values exist at all.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
Each of the next t lines contains three integers a, b and c (1 ≤ a ≤ 10^9, 2 ≤ b ≤ 10^9, 1 ≤ c ≤ 10^9).
Output
For each testcase print two positive integers. For both shops print such x that buying x donuts in this shop is strictly cheaper than buying x donuts in the other shop. x should be greater than 0 and less or equal to 10^9.
If there is no such x, then print -1. If there are multiple answers, then print any of them.
Example
Input
4
5 10 4
4 5 20
2 2 3
1000000000 1000000000 1000000000
Output
-1 20
8 -1
1 2
-1 1000000000
Note
In the first testcase buying any number of donuts will be cheaper in the second shop. For example, for 3 or 5 donuts you'll have to buy a box of 10 donuts for 4 dollars. 3 or 5 donuts in the first shop would cost you 15 or 25 dollars, respectively, however. For 20 donuts you'll have to buy two boxes for 8 dollars total. Note that 3 and 5 are also valid answers for the second shop, along with many other answers.
In the second testcase buying any number of donuts will be either cheaper in the first shop or the same price. 8 donuts cost 32 dollars in the first shop and 40 dollars in the second shop (because you have to buy two boxes). 10 donuts will cost 40 dollars in both shops, so 10 is not a valid answer for any of the shops.
In the third testcase 1 donut costs 2 and 3 dollars, respectively. 2 donuts cost 4 and 3 dollars. Thus, 1 is a valid answer for the first shop and 2 is a valid answer for the second shop.
In the fourth testcase 10^9 donuts cost 10^{18} dollars in the first shop and 10^9 dollars in the second shop.
|
{"inputs": ["1\n8042 1 2\n", "1\n9353 1 2\n", "1\n12313 2 2\n", "1\n12313 1 2\n", "1\n13806 1 2\n", "1\n165 82 165\n", "1\n165 82 173\n", "1\n165 165 165\n"], "outputs": ["-1 1\n", "-1 1\n", "-1 2\n", "-1 1\n", "-1 1\n", "-1 82\n", "1 82\n", "-1 165\n"]}
| 762
| 173
|
coding
|
Solve the programming task below in a Python markdown code block.
Theatre Square in the capital city of Berland has a rectangular shape with the size n × m meters. On the occasion of the city's anniversary, a decision was taken to pave the Square with square granite flagstones. Each flagstone is of the size a × a.
What is the least number of flagstones needed to pave the Square? It's allowed to cover the surface larger than the Theatre Square, but the Square has to be covered. It's not allowed to break the flagstones. The sides of flagstones should be parallel to the sides of the Square.
Input
The input contains three positive integer numbers in the first line: n, m and a (1 ≤ n, m, a ≤ 10^9).
Output
Print the needed number of flagstones in new line.
Examples
input
6 6 4
output
4
|
{"inputs": ["s\ninput\n6 6 4\noutput\n4"], "outputs": [""]}
| 214
| 23
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
In a string s of lowercase letters, these letters form consecutive groups of the same character.
For example, a string like s = "abbxxxxzyy" has the groups "a", "bb", "xxxx", "z", and "yy".
A group is identified by an interval [start, end], where start and end denote the start and end indices (inclusive) of the group. In the above example, "xxxx" has the interval [3,6].
A group is considered large if it has 3 or more characters.
Return the intervals of every large group sorted in increasing order by start index.
Please complete the following python code precisely:
```python
class Solution:
def largeGroupPositions(self, s: str) -> List[List[int]]:
```
|
{"functional": "def check(candidate):\n assert candidate(s = \"abbxxxxzzy\") == [[3,6]]\n assert candidate(s = \"abc\") == []\n assert candidate(s = \"abcdddeeeeaabbbcd\") == [[3,5],[6,9],[12,14]]\n assert candidate(s = \"aba\") == []\n\n\ncheck(Solution().largeGroupPositions)"}
| 191
| 96
|
coding
|
Solve the programming task below in a Python markdown code block.
Takahashi has a deposit of 100 yen (the currency of Japan) in AtCoder Bank.
The bank pays an annual interest rate of 1 % compounded annually. (A fraction of less than one yen is discarded.)
Assuming that nothing other than the interest affects Takahashi's balance, in how many years does the balance reach X yen or above for the first time?
-----Constraints-----
- 101 \le X \le 10^{18}
- All values in input are integers.
-----Input-----
Input is given from Standard Input in the following format:
X
-----Output-----
Print the number of years it takes for Takahashi's balance to reach X yen or above for the first time.
-----Sample Input-----
103
-----Sample Output-----
3
- The balance after one year is 101 yen.
- The balance after two years is 102 yen.
- The balance after three years is 103 yen.
Thus, it takes three years for the balance to reach 103 yen or above.
|
{"inputs": ["121", "888", "435", "684", "394", "124", "143", "110"], "outputs": ["21\n", "271\n", "193\n", "243\n", "182\n", "24\n", "43\n", "10\n"]}
| 238
| 90
|
coding
|
Solve the programming task below in a Python markdown code block.
There is a directed graph G with N vertices and M edges. The vertices are numbered 1, 2, \ldots, N, and for each i (1 \leq i \leq M), the i-th directed edge goes from Vertex x_i to y_i. G does not contain directed cycles.
Find the length of the longest directed path in G. Here, the length of a directed path is the number of edges in it.
Constraints
* All values in input are integers.
* 2 \leq N \leq 10^5
* 1 \leq M \leq 10^5
* 1 \leq x_i, y_i \leq N
* All pairs (x_i, y_i) are distinct.
* G does not contain directed cycles.
Input
Input is given from Standard Input in the following format:
N M
x_1 y_1
x_2 y_2
:
x_M y_M
Output
Print the length of the longest directed path in G.
Examples
Input
4 5
1 2
1 3
3 2
2 4
3 4
Output
3
Input
6 3
2 3
4 5
5 6
Output
2
Input
5 8
5 3
2 3
2 4
5 2
5 1
1 4
4 3
1 3
Output
3
|
{"inputs": ["6 3\n2 6\n4 5\n5 6", "6 3\n2 6\n4 6\n5 6", "6 3\n4 3\n3 5\n5 6", "7 3\n2 3\n4 5\n5 6", "7 3\n2 3\n1 5\n5 6", "6 3\n2 6\n4 5\n4 6", "7 1\n2 3\n4 5\n5 6", "6 3\n2 6\n4 6\n5 2"], "outputs": ["2\n", "1\n", "3\n", "2\n", "2\n", "1\n", "1\n", "2\n"]}
| 328
| 174
|
coding
|
Solve the programming task below in a Python markdown code block.
Complete the function to determine the number of bits required to convert integer `A` to integer `B` (where `A` and `B` >= 0)
The upper limit for `A` and `B` is 2^(16), `int.MaxValue` or similar.
For example, you can change 31 to 14 by flipping the 4th and 0th bit:
```
31 0 0 0 1 1 1 1 1
14 0 0 0 0 1 1 1 0
--- ---------------
bit 7 6 5 4 3 2 1 0
```
Thus `31` and `14` should return `2`.
Also feel free to reuse/extend the following starter code:
```python
def convert_bits(a, b):
```
|
{"functional": "_inputs = [[31, 14], [7, 17], [31, 0], [0, 0], [127681, 127681], [312312312, 5645657], [43, 2009989843]]\n_outputs = [[2], [3], [5], [0], [0], [13], [17]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(convert_bits(*i), o[0])"}
| 204
| 251
|
coding
|
Solve the programming task below in a Python markdown code block.
We have a string of letters 'a' and 'b'. We want to perform some operations on it. On each step we choose one of substrings "ab" in the string and replace it with the string "bba". If we have no "ab" as a substring, our job is done. Print the minimum number of steps we should perform to make our job done modulo 109 + 7.
The string "ab" appears as a substring if there is a letter 'b' right after the letter 'a' somewhere in the string.
Input
The first line contains the initial string consisting of letters 'a' and 'b' only with length from 1 to 106.
Output
Print the minimum number of steps modulo 109 + 7.
Examples
Input
ab
Output
1
Input
aab
Output
3
Note
The first example: "ab" → "bba".
The second example: "aab" → "abba" → "bbaba" → "bbbbaa".
|
{"inputs": ["ba\n", "ca\n", "ab\n", "aab\n", "abbaa\n", "bbbaa\n", "aabbb\n", "aabaa\n"], "outputs": ["0\n", "0\n", "1", "3", "2", "0\n", "9\n", "3\n"]}
| 239
| 73
|
coding
|
Solve the programming task below in a Python markdown code block.
For a non-negative integer K, we define a fractal of level K as follows:
* A fractal of level 0 is a grid with just one white square.
* When K > 0, a fractal of level K is a 3^K \times 3^K grid. If we divide this grid into nine 3^{K-1} \times 3^{K-1} subgrids:
* The central subgrid consists of only black squares.
* Each of the other eight subgrids is a fractal of level K-1.
For example, a fractal of level 2 is as follows:
A fractal of level 2
In a fractal of level 30, let (r, c) denote the square at the r-th row from the top and the c-th column from the left.
You are given Q quadruples of integers (a_i, b_i, c_i, d_i). For each quadruple, find the distance from (a_i, b_i) to (c_i, d_i).
Here the distance from (a, b) to (c, d) is the minimum integer n that satisfies the following condition:
* There exists a sequence of white squares (x_0, y_0), \ldots, (x_n, y_n) satisfying the following conditions:
* (x_0, y_0) = (a, b)
* (x_n, y_n) = (c, d)
* For every i (0 \leq i \leq n-1), (x_i, y_i) and (x_{i+1}, y_{i+1}) share a side.
Constraints
* 1 \leq Q \leq 10000
* 1 \leq a_i, b_i, c_i, d_i \leq 3^{30}
* (a_i, b_i) \neq (c_i, d_i)
* (a_i, b_i) and (c_i, d_i) are white squares.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
Q
a_1 \ b_1 \ c_1 \ d_1
:
a_Q \ b_Q \ c_Q \ d_Q
Output
Print Q lines. The i-th line should contain the distance from (a_i, b_i) to (c_i, d_i).
Example
Input
2
4 2 7 4
9 9 1 9
Output
5
8
|
{"inputs": ["2\n3 2 7 4\n9 9 1 9", "2\n3 2 7 4\n9 3 1 9", "2\n3 2 4 4\n7 3 1 9", "2\n3 2 6 4\n7 3 1 9", "2\n3 2 6 4\n4 3 1 9", "2\n3 2 6 4\n4 3 2 9", "2\n3 2 6 4\n4 3 2 4", "2\n3 4 6 4\n4 3 2 4"], "outputs": ["6\n8\n", "6\n14\n", "3\n12\n", "5\n12\n", "5\n9\n", "5\n8\n", "5\n3\n", "3\n3\n"]}
| 556
| 209
|
coding
|
Solve the programming task below in a Python markdown code block.
Alice likes snow a lot! Unfortunately, this year's winter is already over, and she can't expect to have any more of it. Bob has thus bought her a gift — a large snow maker. He plans to make some amount of snow every day. On day i he will make a pile of snow of volume V_{i} and put it in her garden.
Each day, every pile will shrink a little due to melting. More precisely, when the temperature on a given day is T_{i}, each pile will reduce its volume by T_{i}. If this would reduce the volume of a pile to or below zero, it disappears forever. All snow piles are independent of each other.
Note that the pile made on day i already loses part of its volume on the same day. In an extreme case, this may mean that there are no piles left at the end of a particular day.
You are given the initial pile sizes and the temperature on each day. Determine the total volume of snow melted on each day.
-----Input-----
The first line contains a single integer N (1 ≤ N ≤ 10^5) — the number of days.
The second line contains N integers V_1, V_2, ..., V_{N} (0 ≤ V_{i} ≤ 10^9), where V_{i} is the initial size of a snow pile made on the day i.
The third line contains N integers T_1, T_2, ..., T_{N} (0 ≤ T_{i} ≤ 10^9), where T_{i} is the temperature on the day i.
-----Output-----
Output a single line with N integers, where the i-th integer represents the total volume of snow melted on day i.
-----Examples-----
Input
3
10 10 5
5 7 2
Output
5 12 4
Input
5
30 25 20 15 10
9 10 12 4 13
Output
9 20 35 11 25
-----Note-----
In the first sample, Bob first makes a snow pile of volume 10, which melts to the size of 5 on the same day. On the second day, he makes another pile of size 10. Since it is a bit warmer than the day before, the first pile disappears completely while the second pile shrinks to 3. At the end of the second day, he has only a single pile of size 3. On the third day he makes a smaller pile than usual, but as the temperature dropped too, both piles survive till the end of the day.
|
{"inputs": ["1\n4\n5\n", "1\n5\n4\n", "1\n5\n5\n", "1\n5\n4\n", "1\n4\n5\n", "1\n5\n5\n", "1\n5\n2\n", "1\n4\n6\n"], "outputs": ["4\n", "4\n", "5\n", "4 ", "4 ", "5 ", "2\n", "4\n"]}
| 578
| 99
|
coding
|
Solve the programming task below in a Python markdown code block.
Pete and his mate Phil are out in the countryside shooting clay pigeons with a shotgun - amazing fun.
They decide to have a competition. 3 rounds, 2 shots each. Winner is the one with the most hits.
Some of the clays have something attached to create lots of smoke when hit, guarenteed by the packaging to generate 'real excitement!' (genuinely this happened). None of the explosive things actually worked, but for this kata lets say they did.
For each round you will receive the following format:
[{P1:'XX', P2:'XO'}, true]
That is an array containing an object and a boolean. Pl represents Pete, P2 represents Phil. X represents a hit and O represents a miss. If the boolean is true, any hit is worth 2. If it is false, any hit is worth 1.
Find out who won. If it's Pete, return 'Pete Wins!'. If it is Phil, return 'Phil Wins!'. If the scores are equal, return 'Draw!'.
Note that as there are three rounds, the actual input (x) will look something like this:
[[{P1:'XX', P2:'XO'}, true], [{P1:'OX', P2:'OO'}, false], [{P1:'XX', P2:'OX'}, true]]
Also feel free to reuse/extend the following starter code:
```python
def shoot(results):
```
|
{"functional": "_inputs = [[[[{'P1': 'XX', 'P2': 'XO'}, True], [{'P1': 'OX', 'P2': 'OO'}, False], [{'P1': 'XX', 'P2': 'OX'}, True]]], [[[{'P1': 'XX', 'P2': 'XO'}, False], [{'P1': 'OX', 'P2': 'XX'}, False], [{'P1': 'OO', 'P2': 'XX'}, True]]], [[[{'P1': 'OO', 'P2': 'XX'}, False], [{'P1': 'OO', 'P2': 'XX'}, False], [{'P1': 'XX', 'P2': 'OO'}, True]]], [[[{'P1': 'XX', 'P2': 'XX'}, True], [{'P1': 'XX', 'P2': 'OX'}, False], [{'P1': 'OO', 'P2': 'OX'}, True]]], [[[{'P1': 'XX', 'P2': 'XX'}, True], [{'P1': 'OO', 'P2': 'OO'}, False], [{'P1': 'XX', 'P2': 'XX'}, True]]]]\n_outputs = [['Pete Wins!'], ['Phil Wins!'], ['Draw!'], ['Phil Wins!'], ['Draw!']]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(shoot(*i), o[0])"}
| 312
| 425
|
coding
|
Solve the programming task below in a Python markdown code block.
There are N boxes arranged in a row from left to right. The i-th box from the left contains A_i candies.
You will take out the candies from some consecutive boxes and distribute them evenly to M children.
Such being the case, find the number of the pairs (l, r) that satisfy the following:
- l and r are both integers and satisfy 1 \leq l \leq r \leq N.
- A_l + A_{l+1} + ... + A_r is a multiple of M.
-----Constraints-----
- All values in input are integers.
- 1 \leq N \leq 10^5
- 2 \leq M \leq 10^9
- 1 \leq A_i \leq 10^9
-----Input-----
Input is given from Standard Input in the following format:
N M
A_1 A_2 ... A_N
-----Output-----
Print the number of the pairs (l, r) that satisfy the conditions.
Note that the number may not fit into a 32-bit integer type.
-----Sample Input-----
3 2
4 1 5
-----Sample Output-----
3
The sum A_l + A_{l+1} + ... + A_r for each pair (l, r) is as follows:
- Sum for (1, 1): 4
- Sum for (1, 2): 5
- Sum for (1, 3): 10
- Sum for (2, 2): 1
- Sum for (2, 3): 6
- Sum for (3, 3): 5
Among these, three are multiples of 2.
|
{"inputs": ["1 2\n1\n", "1 2\n2\n", "3 2\n5 2 7", "3 2\n4 1 7", "3 2\n4 2 7", "3 2\n5 2 6", "3 1\n5 2 6", "3 2\n4 1 5"], "outputs": ["0\n", "1\n", "2\n", "3\n", "3\n", "3\n", "6\n", "3"]}
| 375
| 119
|
coding
|
Solve the programming task below in a Python markdown code block.
Petya has a rectangular Board of size $n \times m$. Initially, $k$ chips are placed on the board, $i$-th chip is located in the cell at the intersection of $sx_i$-th row and $sy_i$-th column.
In one action, Petya can move all the chips to the left, right, down or up by $1$ cell.
If the chip was in the $(x, y)$ cell, then after the operation: left, its coordinates will be $(x, y - 1)$; right, its coordinates will be $(x, y + 1)$; down, its coordinates will be $(x + 1, y)$; up, its coordinates will be $(x - 1, y)$.
If the chip is located by the wall of the board, and the action chosen by Petya moves it towards the wall, then the chip remains in its current position.
Note that several chips can be located in the same cell.
For each chip, Petya chose the position which it should visit. Note that it's not necessary for a chip to end up in this position.
Since Petya does not have a lot of free time, he is ready to do no more than $2nm$ actions.
You have to find out what actions Petya should do so that each chip visits the position that Petya selected for it at least once. Or determine that it is not possible to do this in $2nm$ actions.
-----Input-----
The first line contains three integers $n, m, k$ ($1 \le n, m, k \le 200$) — the number of rows and columns of the board and the number of chips, respectively.
The next $k$ lines contains two integers each $sx_i, sy_i$ ($ 1 \le sx_i \le n, 1 \le sy_i \le m$) — the starting position of the $i$-th chip.
The next $k$ lines contains two integers each $fx_i, fy_i$ ($ 1 \le fx_i \le n, 1 \le fy_i \le m$) — the position that the $i$-chip should visit at least once.
-----Output-----
In the first line print the number of operations so that each chip visits the position that Petya selected for it at least once.
In the second line output the sequence of operations. To indicate operations left, right, down, and up, use the characters $L, R, D, U$ respectively.
If the required sequence does not exist, print -1 in the single line.
-----Examples-----
Input
3 3 2
1 2
2 1
3 3
3 2
Output
3
DRD
Input
5 4 3
3 4
3 1
3 3
5 3
1 3
1 4
Output
9
DDLUUUURR
|
{"inputs": ["2 2 1\n1 1\n1 1\n", "2 2 1\n1 1\n2 2\n", "2 2 1\n1 1\n1 1\n", "2 2 1\n1 1\n2 2\n", "3 2 1\n1 1\n1 1\n", "2 2 1\n2 1\n2 2\n", "3 3 2\n1 2\n2 1\n3 3\n3 2\n", "2 5 2\n1 1\n1 1\n1 1\n1 1\n"], "outputs": ["5\nULRDL", "5\nULRDL", "5\nULRDL", "5\nULRDL", "8\nUULRDLDR", "5\nULRDL", "12\nUULLRRDLLDRR", "14\nULLLLRRRRDLLLL"]}
| 648
| 216
|
coding
|
Solve the programming task below in a Python markdown code block.
# Task
You are given a positive integer `n`. We intend to make some ascending sequences according to the following rules:
1. Make a sequence of length 1: [ n ]
2. Or, insert a number to the left side of the sequence. But this number can not exceed half of the first number of the sequence.
3. Follow rule 2, continue insert number to the left side of the sequence.
Your task is to count the number of all possible sequences, and return it.
If you do not understand the task, please read the rewritten version below:
You are given a positive integer `n`. Your task is to count the number of such sequences:
- It should be an ascending sequence;
- It should end with number `n`.
- Each number in the sequence should smaller or equals to the half of its right, except for the last number `n`.
- We define that a sequence containing only a number `n` is a valid ascending sequence.
# Examples
For `n = 6`, the output should be `6`.
All sequences we made are:
```
[6]
insert a number to the left:
[1,6]
[2,6]
[3,6]
continue insert number:
[1,2,6]
[1,3,6]
```
There are 6 sequences in total.
For `n = 10`, the output should be `14`.
All sequences we made are:
```
[10]
insert a number to the left:
[1,10]
[2,10]
[3,10]
[4,10]
[5,10]
continue insert number:
[1,2,10]
[1,3,10]
[1,4,10]
[2,4,10]
[1,5,10]
[2,5,10]
continue insert number:
[1,2,4,10]
[1,2,5,10]
```
There are 14 sequences in total.
# Note
- `1 <= n <= 1000`
- `3` fixed testcases
- `100` random testcases, testing for correctness of solution
- All inputs are valid.
- If my reference solution gives the wrong result in the random tests, please let me know(post an issue).
Also feel free to reuse/extend the following starter code:
```python
def make_sequences(n):
```
|
{"functional": "_inputs = [[6], [10], [1000]]\n_outputs = [[6], [14], [1981471878]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(make_sequences(*i), o[0])"}
| 534
| 180
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given three positive integers: n, index, and maxSum. You want to construct an array nums (0-indexed) that satisfies the following conditions:
nums.length == n
nums[i] is a positive integer where 0 <= i < n.
abs(nums[i] - nums[i+1]) <= 1 where 0 <= i < n-1.
The sum of all the elements of nums does not exceed maxSum.
nums[index] is maximized.
Return nums[index] of the constructed array.
Note that abs(x) equals x if x >= 0, and -x otherwise.
Please complete the following python code precisely:
```python
class Solution:
def maxValue(self, n: int, index: int, maxSum: int) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(n = 4, index = 2, maxSum = 6) == 2\n assert candidate(n = 6, index = 1, maxSum = 10) == 3\n\n\ncheck(Solution().maxValue)"}
| 180
| 68
|
coding
|
Solve the programming task below in a Python markdown code block.
One upon a time there were three best friends Abhinav, Harsh, and Akash decided to form a
team and take part in ICPC from KIIT. Participants are usually offered several problems during
the programming contest. Long before the start, the friends decided that they will implement a
problem if at least two of them are sure about the solution. Otherwise, friends won't write the
problem's solution.
This contest offers $N$ problems to the participants. For each problem we know, which friend is
sure about the solution. Help the KIITians find the number of problems for which they will write a
solution.
Then n lines contain three integers each, each integer is either 0 or 1. If the first number in the
line equals 1, then Abhinav is sure about the problem's solution, otherwise, he isn't sure. The
second number shows Harsh's view on the solution, the third number shows Akash's view. The
numbers on the lines are
-----Input:-----
- A single integer will contain $N$, number of problems.
-----Output:-----
Print a single integer — the number of problems the friends will implement on the contest.
-----Constraints-----
- $1 \leq N \leq 1000$
-----Sample Input:-----
3
1 1 0
1 1 1
1 0 0
-----Sample Output:-----
2
-----EXPLANATION:-----
In the first sample, Abhinav and Harsh are sure that they know how to solve the first problem
and all three of them know how to solve the second problem. That means that they will write
solutions for these problems. Only Abhinav is sure about the solution for the third problem, but
that isn't enough, so the group won't take it.
|
{"inputs": ["3\n1 1 0\n1 1 1\n1 0 0"], "outputs": ["2"]}
| 403
| 30
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a positive integer N. Find the number of the pairs of integers u and v (0≦u,v≦N) such that there exist two non-negative integers a and b satisfying a xor b=u and a+b=v. Here, xor denotes the bitwise exclusive OR. Since it can be extremely large, compute the answer modulo 10^9+7.
Constraints
* 1≦N≦10^{18}
Input
The input is given from Standard Input in the following format:
N
Output
Print the number of the possible pairs of integers u and v, modulo 10^9+7.
Examples
Input
3
Output
5
Input
1422
Output
52277
Input
1000000000000000000
Output
787014179
|
{"inputs": ["4", "0", "1", "2", "7", "5", "8", "6"], "outputs": ["8\n", "1\n", "2\n", "4\n", "14\n", "10\n", "18\n", "13\n"]}
| 201
| 66
|
coding
|
Solve the programming task below in a Python markdown code block.
This is the easy version of the problem. The difference in the constraints between both versions is colored below in red. You can make hacks only if all versions of the problem are solved.
Marin and Gojou are playing hide-and-seek with an array.
Gojou initially performs the following steps:
First, Gojou chooses $2$ integers $l$ and $r$ such that $l \leq r$.
Then, Gojou makes an array $a$ of length $r-l+1$ which is a permutation of the array $[l,l+1,\ldots,r]$.
Finally, Gojou chooses a secret integer $x$ and sets $a_i$ to $a_i \oplus x$ for all $i$ (where $\oplus$ denotes the bitwise XOR operation ).
Marin is then given the values of $l,r$ and the final array $a$. She needs to find the secret integer $x$ to win. Can you help her?
Note that there may be multiple possible $x$ that Gojou could have chosen. Marin can find any possible $x$ that could have resulted in the final value of $a$.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) — the number of test cases.
In the first line of each test case contains two integers $l$ and $r$ ($ {\boldsymbol{0} \boldsymbol{=} \boldsymbol{l}} \le r < 2^{17}$).
The second line contains $r - l + 1$ integers of $a_1,a_2,\ldots,a_{r-l+1}$ ($0 \le a_i < 2^{17}$). It is guaranteed that $a$ can be generated using the steps performed by Gojou.
It is guaranteed that the sum of $r - l + 1$ over all test cases does not exceed $2^{17}$.
-----Output-----
For each test case print an integer $x$. If there are multiple answers, print any.
-----Examples-----
Input
3
0 3
3 2 1 0
0 3
4 7 6 5
0 2
1 2 3
Output
0
4
3
-----Note-----
In the first test case, the original array is $[3, 2, 1, 0]$.
In the second test case, the original array is $[0, 3, 2, 1]$.
In the third test case, the original array is $[2, 1, 0]$.
|
{"inputs": ["3\n0 3\n3 2 1 0\n0 3\n4 7 6 5\n0 2\n1 2 3\n"], "outputs": ["0\n4\n3\n"]}
| 578
| 52
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a graph with N vertices (numbered 1 to N) and M bidirectional edges, which doesn't contain multiple edges or self-loops — that is, the given graph is a simple undirected graph.
For each pair of vertices a, b such that 1 ≤ a, b ≤ N, it is possible to add a new edge between vertices a and b to the graph, with a cost of (a - b)^2.
Find the minimum cost of adding edges so that vertex N is reachable from vertex 1.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains 2 space-separated integers, N and M.
- The i^{th} of the next M lines contains 2 space-separated integers u_{i} and v_{i}, denoting a bidirectional edge between vertex u_{i} and vertex v_{i} in the graph.
------ Output Format ------
For each test case, output the answer in a single line.
------ Constraints ------
$1 ≤ T ≤ 1000$
$2 ≤ N ≤ 2 \cdot 10^{5}$
$0 ≤ M ≤ 2 \cdot 10^{5}$
$1 ≤ u_{i}, v_{i} ≤ N$
- The graph doesn't contain self-loops or multiple edges
- Sum of $N$ over all testcases is no more than $2 \cdot 10^{5}$.
- Sum of $M$ over all testcases is no more than $2 \cdot 10^{5}$.
----- Sample Input 1 ------
3
2 0
5 4
3 2
1 4
5 3
4 2
6 3
1 2
3 2
1 3
----- Sample Output 1 ------
1
0
3
----- explanation 1 ------
Test Case $1$: Add an edge between vertices $1$ and $2$, with cost $(1-2)^2 = 1$.
Test Case $2$: Vertices $1$ and $5$ are already connected, so no edges need to be added.
|
{"inputs": ["3\n2 0\n5 4\n3 2\n1 4\n5 3\n4 2\n6 3\n1 2\n3 2\n1 3\n"], "outputs": ["1\n0\n3\n"]}
| 490
| 58
|
coding
|
Solve the programming task below in a Python markdown code block.
In one little known, but very beautiful country called Waterland, lives a lovely shark Valerie. Like all the sharks, she has several rows of teeth, and feeds on crucians. One of Valerie's distinguishing features is that while eating one crucian she uses only one row of her teeth, the rest of the teeth are "relaxing".
For a long time our heroine had been searching the sea for crucians, but a great misfortune happened. Her teeth started to ache, and she had to see the local dentist, lobster Ashot. As a professional, Ashot quickly relieved Valerie from her toothache. Moreover, he managed to determine the cause of Valerie's developing caries (for what he was later nicknamed Cap).
It turned that Valerie eats too many crucians. To help Valerie avoid further reoccurrence of toothache, Ashot found for each Valerie's tooth its residual viability. Residual viability of a tooth is a value equal to the amount of crucians that Valerie can eat with this tooth. Every time Valerie eats a crucian, viability of all the teeth used for it will decrease by one. When the viability of at least one tooth becomes negative, the shark will have to see the dentist again.
Unhappy, Valerie came back home, where a portion of crucians was waiting for her. For sure, the shark couldn't say no to her favourite meal, but she had no desire to go back to the dentist. That's why she decided to eat the maximum amount of crucians from the portion but so that the viability of no tooth becomes negative.
As Valerie is not good at mathematics, she asked you to help her to find out the total amount of crucians that she can consume for dinner.
We should remind you that while eating one crucian Valerie uses exactly one row of teeth and the viability of each tooth from this row decreases by one.
Input
The first line contains three integers n, m, k (1 ≤ m ≤ n ≤ 1000, 0 ≤ k ≤ 106) — total amount of Valerie's teeth, amount of tooth rows and amount of crucians in Valerie's portion for dinner. Then follow n lines, each containing two integers: r (1 ≤ r ≤ m) — index of the row, where belongs the corresponding tooth, and c (0 ≤ c ≤ 106) — its residual viability.
It's guaranteed that each tooth row has positive amount of teeth.
Output
In the first line output the maximum amount of crucians that Valerie can consume for dinner.
Examples
Input
4 3 18
2 3
1 2
3 6
2 3
Output
11
Input
2 2 13
1 13
2 12
Output
13
|
{"inputs": ["1 1 0\n1 3\n", "2 2 13\n1 13\n2 12\n", "1 1 1000000\n1 1000000\n", "4 2 4\n1 9\n1 0\n1 4\n2 6\n", "4 2 4\n1 9\n1 1\n1 4\n2 6\n", "4 2 4\n1 4\n1 1\n1 4\n2 6\n", "4 2 8\n1 9\n1 10\n1 4\n2 6\n", "4 2 4\n1 9\n1 10\n1 4\n2 6\n"], "outputs": ["0\n", "13\n", "1000000\n", "4\n", "4\n", "4\n", "8\n", "4\n"]}
| 590
| 222
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
A sequence is special if it consists of a positive number of 0s, followed by a positive number of 1s, then a positive number of 2s.
For example, [0,1,2] and [0,0,1,1,1,2] are special.
In contrast, [2,1,0], [1], and [0,1,2,0] are not special.
Given an array nums (consisting of only integers 0, 1, and 2), return the number of different subsequences that are special. Since the answer may be very large, return it modulo 109 + 7.
A subsequence of an array is a sequence that can be derived from the array by deleting some or no elements without changing the order of the remaining elements. Two subsequences are different if the set of indices chosen are different.
Please complete the following python code precisely:
```python
class Solution:
def countSpecialSubsequences(self, nums: List[int]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(nums = [0,1,2,2]) == 3\n assert candidate(nums = [2,2,0,0]) == 0\n assert candidate(nums = [0,1,2,0,1,2]) == 7\n\n\ncheck(Solution().countSpecialSubsequences)"}
| 234
| 79
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well.
You are a warrior and you have to fight $N$ enemy warriors (numbered $1$ through $N$) one by one, in any order you choose. You have to win as many of these fights as possible.
Each warrior has some amount of power, which changes when the warrior fights. For each $i$, the $i$-th enemy warrior has power $P_{i}$. When you have power $x$ and you fight an enemy warrior with power $y$, the following happens:
if $x > y$, you kill the enemy warrior and your power changes to $2(x-y)$
otherwise (if $x ≤ y$), the enemy warrior kills you
You should answer $Q$ queries. In each query, you are given your initial power $X$ and you should find the maximum number of warriors you can kill if you are starting with power $X$.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $Q$.
The second line contains $N$ space-separated integers $P_{1}, P_{2}, \ldots, P_{N}$.
$Q$ lines follow. Each of these lines contains a single integer $X$ describing a query.
------ Output ------
For each query, print a single line containing one integer — the maximum number of warriors you can kill.
------ Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N, Q ≤ 10^{5}$
$1 ≤ P_{i} ≤ 10^{9}$ for each valid $i$
$1 ≤ X ≤ 10^{9}$
----- Sample Input 1 ------
1
3 4
1 2 1
10
2
3
1
----- Sample Output 1 ------
3
2
3
0
|
{"inputs": ["1\n3 4\n1 2 1\n10\n2\n3\n1"], "outputs": ["3\n2\n3\n0"]}
| 462
| 37
|
coding
|
Solve the programming task below in a Python markdown code block.
There are N stones arranged in a row. The i-th stone from the left is painted in the color C_i.
Snuke will perform the following operation zero or more times:
* Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones.
Find the number of possible final sequences of colors of the stones, modulo 10^9+7.
Constraints
* 1 \leq N \leq 2\times 10^5
* 1 \leq C_i \leq 2\times 10^5(1\leq i\leq N)
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
C_1
:
C_N
Output
Print the number of possible final sequences of colors of the stones, modulo 10^9+7.
Examples
Input
5
1
2
1
2
2
Output
3
Input
6
4
2
5
4
2
4
Output
5
Input
7
1
3
1
2
3
3
2
Output
5
|
{"inputs": ["5\n1\n4\n1\n2\n2", "5\n1\n2\n1\n2\n3", "5\n1\n2\n1\n2\n1", "5\n1\n2\n1\n3\n2", "5\n1\n2\n1\n5\n2", "5\n1\n2\n1\n1\n2", "5\n1\n3\n1\n1\n2", "5\n2\n3\n1\n1\n2"], "outputs": ["2\n", "3\n", "5\n", "3\n", "3\n", "3\n", "2\n", "2\n"]}
| 267
| 142
|
coding
|
Solve the programming task below in a Python markdown code block.
The Chef is given an array of numbers and asked to find the first balance point of an array. A balance point is an index in the array N where the sum of the integers to the left of N is equal to the sum of the integers to the right of N. If there is no such balance point, print -1
-----Input-----
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains a single integer N denoting the number of weights on the array. The second line contains N space-separated integers A1, A2, ..., AN denoting the weights
-----Output-----
For each test case, output a single line containing the index of the first balance point. If no such index exists, output -1
-----Constraints-----
For Subtask 1:
- 1 ≤ T ≤ 10
- 1 ≤ N ≤ 10
- -1000 ≤ A1, A2, ..., AN ≤ 1000
For Subtask 1:
- 1 ≤ T ≤ 10
- 1 ≤ N ≤ 1000
- -10000 ≤ A1, A2, ..., AN ≤ 10000
-----Example-----
Input:
2
7
7 8 9 1 9 8 7
6
1 100 50 -51 1 1
Output:
3
1
-----Explanation-----
Example case 1. In the first case, at the midpoint of the array 1, the sum of the left side {7,8,9} is equal to the sum of the right side {9,8,7}
Example case 1. In the second case, at index 1, the sum of left side =1 is equal to the sum of the right side {50,-51,1,1}
|
{"inputs": ["2\n7\n7 8 9 1 9 8 7\n6\n1 100 50 -51 1 1"], "outputs": ["3\n1"]}
| 426
| 48
|
coding
|
Solve the programming task below in a Python markdown code block.
Marcin is a coach in his university. There are n students who want to attend a training camp. Marcin is a smart coach, so he wants to send only the students that can work calmly with each other.
Let's focus on the students. They are indexed with integers from 1 to n. Each of them can be described with two integers a_i and b_i; b_i is equal to the skill level of the i-th student (the higher, the better). Also, there are 60 known algorithms, which are numbered with integers from 0 to 59. If the i-th student knows the j-th algorithm, then the j-th bit (2^j) is set in the binary representation of a_i. Otherwise, this bit is not set.
Student x thinks that he is better than student y if and only if x knows some algorithm which y doesn't know. Note that two students can think that they are better than each other. A group of students can work together calmly if no student in this group thinks that he is better than everyone else in this group.
Marcin wants to send a group of at least two students which will work together calmly and will have the maximum possible sum of the skill levels. What is this sum?
Input
The first line contains one integer n (1 ≤ n ≤ 7000) — the number of students interested in the camp.
The second line contains n integers. The i-th of them is a_i (0 ≤ a_i < 2^{60}).
The third line contains n integers. The i-th of them is b_i (1 ≤ b_i ≤ 10^9).
Output
Output one integer which denotes the maximum sum of b_i over the students in a group of students which can work together calmly. If no group of at least two students can work together calmly, print 0.
Examples
Input
4
3 2 3 6
2 8 5 10
Output
15
Input
3
1 2 3
1 2 3
Output
0
Input
1
0
1
Output
0
Note
In the first sample test, it's optimal to send the first, the second and the third student to the camp. It's also possible to send only the first and the third student, but they'd have a lower sum of b_i.
In the second test, in each group of at least two students someone will always think that he is better than everyone else in the subset.
|
{"inputs": ["1\n1\n1\n", "1\n2\n1\n", "1\n2\n2\n", "1\n2\n0\n", "1\n3\n0\n", "1\n5\n1\n", "1\n5\n0\n", "1\n4\n0\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
| 538
| 102
|
coding
|
Solve the programming task below in a Python markdown code block.
As AtCoder Beginner Contest 100 is taking place, the office of AtCoder, Inc. is decorated with a sequence of length N, a = {a_1, a_2, a_3, ..., a_N}.
Snuke, an employee, would like to play with this sequence.
Specifically, he would like to repeat the following operation as many times as possible:
For every i satisfying 1 \leq i \leq N, perform one of the following: "divide a_i by 2" and "multiply a_i by 3".
Here, choosing "multiply a_i by 3" for every i is not allowed, and the value of a_i after the operation must be an integer.
At most how many operations can be performed?
-----Constraints-----
- N is an integer between 1 and 10 \ 000 (inclusive).
- a_i is an integer between 1 and 1 \ 000 \ 000 \ 000 (inclusive).
-----Input-----
Input is given from Standard Input in the following format:
N
a_1 a_2 a_3 ... a_N
-----Output-----
Print the maximum number of operations that Snuke can perform.
-----Sample Input-----
3
5 2 4
-----Sample Output-----
3
The sequence is initially {5, 2, 4}. Three operations can be performed as follows:
- First, multiply a_1 by 3, multiply a_2 by 3 and divide a_3 by 2. The sequence is now {15, 6, 2}.
- Next, multiply a_1 by 3, divide a_2 by 2 and multiply a_3 by 3. The sequence is now {45, 3, 6}.
- Finally, multiply a_1 by 3, multiply a_2 by 3 and divide a_3 by 2. The sequence is now {135, 9, 3}.
|
{"inputs": ["3\n5 2 8", "3\n5 3 8", "3\n5 2 7", "3\n3 3 8", "3\n5 2 4", "3\n5 2 4\n", "3\n3 3 11", "3\n3 5 11"], "outputs": ["4\n", "3\n", "1\n", "3\n", "3", "3\n", "0\n", "0\n"]}
| 436
| 112
|
coding
|
Solve the programming task below in a Python markdown code block.
### Vaccinations for children under 5
You have been put in charge of administrating vaccinations for children in your local area. Write a function that will generate a list of vaccines for each child presented for vaccination, based on the child's age and vaccination history, and the month of the year.
#### The function takes three parameters: age, status and month
- The parameter 'age' will be given in weeks up to 16 weeks, and thereafter in months. You can assume that children presented will be scheduled for vaccination (eg '16 weeks', '12 months' etc).
- The parameter 'status' indicates if the child has missed a scheduled vaccination, and the argument will be a string that says 'up-to-date', or a scheduled stage (eg '8 weeks') that has been missed, in which case you need to add any missing shots to the list. Only one missed vaccination stage will be passed in per function call.
- If the month is 'september', 'october' or 'november' add 'offer fluVaccine' to the list.
- Make sure there are no duplicates in the returned list, and sort it alphabetically.
#### Example input and output
~~~~
input ('12 weeks', 'up-to-date', 'december')
output ['fiveInOne', 'rotavirus']
input ('12 months', '16 weeks', 'june')
output ['fiveInOne', 'hibMenC', 'measlesMumpsRubella', 'meningitisB', 'pneumococcal']
input ('40 months', '12 months', 'october')
output ['hibMenC', 'measlesMumpsRubella', 'meningitisB', 'offer fluVaccine', 'preSchoolBooster']
~~~~
#### To save you typing it up, here is the vaccinations list
~~~~
fiveInOne : ['8 weeks', '12 weeks', '16 weeks'],
//Protects against: diphtheria, tetanus, whooping cough, polio and Hib (Haemophilus influenzae type b)
pneumococcal : ['8 weeks', '16 weeks'],
//Protects against: some types of pneumococcal infection
rotavirus : ['8 weeks', '12 weeks'],
//Protects against: rotavirus infection, a common cause of childhood diarrhoea and sickness
meningitisB : ['8 weeks', '16 weeks', '12 months'],
//Protects against: meningitis caused by meningococcal type B bacteria
hibMenC : ['12 months'],
//Protects against: Haemophilus influenzae type b (Hib), meningitis caused by meningococcal group C bacteria
measlesMumpsRubella : ['12 months', '40 months'],
//Protects against: measles, mumps and rubella
fluVaccine : ['september','october','november'],
//Given at: annually in Sept/Oct
preSchoolBooster : ['40 months']
//Protects against: diphtheria, tetanus, whooping cough and polio
~~~~
Also feel free to reuse/extend the following starter code:
```python
def vaccine_list(age, status, month):
```
|
{"functional": "_inputs = [['12 weeks', 'up-to-date', 'december'], ['12 months', '16 weeks', 'june'], ['40 months', '12 months', 'october']]\n_outputs = [[['fiveInOne', 'rotavirus']], [['fiveInOne', 'hibMenC', 'measlesMumpsRubella', 'meningitisB', 'pneumococcal']], [['hibMenC', 'measlesMumpsRubella', 'meningitisB', 'offer fluVaccine', 'preSchoolBooster']]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(vaccine_list(*i), o[0])"}
| 707
| 267
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problems statements in [Mandarin Chinese], [Russian], and [Bengali] as well.
You are given an array $A$ with $N$ integers. An array's score is defined as the bitwise AND of all its elements. You need to find the bitwise OR of the scores of all possible non-empty subarrays of $A$.
Furthermore, there are $Q$ queries. Each query consists of two integers $X$ and $V$. You need to change the value of the element at index $X$ to $V$. After each query, you again need to find the bitwise OR of the scores of all possible non-empty subarrays.
See the example for more clarification.
------ Input: ------
The first line of the input contains a single integer $T$ - the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $Q$ - the size of the array and the number of queries, respectively.
The second line contains $N$ space-separated integers $A_{1},\ldots,A_{N}$.
Each of the next $Q$ lines contains two space-separated integers $X$ and $V$ - the position and the new value of the query, respectively.
------ Output: ------
For each test case print $Q+1$ lines. In the first line print the answer for the original array and in the next $Q$ lines print the answer after every query.
------ Constraints ------
$1 ≤ T ≤ 100$
$1 ≤ N, Q ≤ 10^{5}$
$0 ≤ A_{i} ≤ 2^{31}-1$
$1 ≤ X ≤ N$
$0 ≤ V ≤ 2^{31}-1$
The sum of $N$ over all test cases does not exceed $10^{5}$
The sum of $Q$ over all test cases does not exceed $10^{5}$
----- Sample Input 1 ------
2
3 2
1 2 3
1 4
3 0
4 1
1 2 3 4
4 0
----- Sample Output 1 ------
3
7
6
7
3
----- explanation 1 ------
Example case 1: For the original array, all possible subarrays and their scores are as follows.
$AND(1) = 1$, $AND(2) = 2$, $AND(3) = 3$, $AND(1,2) = 0$, $AND(2,3) = 2$, $AND(1,2,3) = 0$.
The bitwise OR of all possible subarray's score is $OR(1,2,3,0,2,0) = 3$.
After the first query new array will be $[4,2,3]$ and the answer will be $7$.
After the second query new array will be $[4,2,0]$ and the answer will be $6$.
|
{"inputs": ["2\n3 2\n1 2 3\n1 4\n3 0\n4 1\n1 2 3 4\n4 0"], "outputs": ["3\n7\n6\n7\n3"]}
| 658
| 54
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
A subarray of a 0-indexed integer array is a contiguous non-empty sequence of elements within an array.
The alternating subarray sum of a subarray that ranges from index i to j (inclusive, 0 <= i <= j < nums.length) is nums[i] - nums[i+1] + nums[i+2] - ... +/- nums[j].
Given a 0-indexed integer array nums, return the maximum alternating subarray sum of any subarray of nums.
Please complete the following python code precisely:
```python
class Solution:
def maximumAlternatingSubarraySum(self, nums: List[int]) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(nums = [3,-1,1,2]) == 5\n assert candidate(nums = [2,2,2,2,2]) == 2\n assert candidate(nums = [1]) == 1\n\n\ncheck(Solution().maximumAlternatingSubarraySum)"}
| 150
| 73
|
coding
|
Solve the programming task below in a Python markdown code block.
Reading books is one of Sasha's passions. Once while he was reading one book, he became acquainted with an unusual character. The character told about himself like that: "Many are my names in many countries. Mithrandir among the Elves, Tharkûn to the Dwarves, Olórin I was in my youth in the West that is forgotten, in the South Incánus, in the North Gandalf; to the East I go not."
And at that moment Sasha thought, how would that character be called in the East? In the East all names are palindromes. A string is a palindrome if it reads the same backward as forward. For example, such strings as "kazak", "oo" and "r" are palindromes, but strings "abb" and "ij" are not.
Sasha believed that the hero would be named after one of the gods of the East. As long as there couldn't be two equal names, so in the East people did the following: they wrote the original name as a string on a piece of paper, then cut the paper minimum number of times k, so they got k+1 pieces of paper with substrings of the initial string, and then unite those pieces together to get a new string. Pieces couldn't be turned over, they could be shuffled.
In this way, it's possible to achive a string abcdefg from the string f|de|abc|g using 3 cuts (by swapping papers with substrings f and abc). The string cbadefg can't be received using the same cuts.
More formally, Sasha wants for the given palindrome s find such minimum k, that you can cut this string into k + 1 parts, and then unite them in such a way that the final string will be a palindrome and it won't be equal to the initial string s. It there is no answer, then print "Impossible" (without quotes).
Input
The first line contains one string s (1 ≤ |s| ≤ 5 000) — the initial name, which consists only of lowercase Latin letters. It is guaranteed that s is a palindrome.
Output
Print one integer k — the minimum number of cuts needed to get a new name, or "Impossible" (without quotes).
Examples
Input
nolon
Output
2
Input
otto
Output
1
Input
qqqq
Output
Impossible
Input
kinnikkinnik
Output
1
Note
In the first example, you can cut the string in those positions: no|l|on, and then unite them as follows on|l|no. It can be shown that there is no solution with one cut.
In the second example, you can cut the string right in the middle, and swap peaces, so you get toot.
In the third example, you can't make a string, that won't be equal to the initial one.
In the fourth example, you can cut the suffix nik and add it to the beginning, so you get nikkinnikkin.
|
{"inputs": ["f\n", "a\n", "g\n", "b\n", "e\n", "c\n", "h\n", "d\n"], "outputs": ["Impossible\n", "Impossible\n", "Impossible\n", "Impossible\n", "Impossible\n", "Impossible\n", "Impossible\n", "Impossible\n"]}
| 650
| 70
|
coding
|
Solve the programming task below in a Python markdown code block.
"Point reflection" or "point symmetry" is a basic concept in geometry where a given point, P, at a given position relative to a mid-point, Q has a corresponding point, P1, which is the same distance from Q but in the opposite direction.
## Task
Given two points P and Q, output the symmetric point of point P about Q.
Each argument is a two-element array of integers representing the point's X and Y coordinates. Output should be in the same format, giving the X and Y coordinates of point P1. You do not have to validate the input.
This kata was inspired by the Hackerrank challenge [Find Point](https://www.hackerrank.com/challenges/find-point)
Also feel free to reuse/extend the following starter code:
```python
def symmetric_point(p, q):
```
|
{"functional": "_inputs = [[[0, 0], [1, 1]], [[2, 6], [-2, -6]], [[10, -10], [-10, 10]], [[1, -35], [-12, 1]], [[1000, 15], [-7, -214]], [[0, 0], [0, 0]]]\n_outputs = [[[2, 2]], [[-6, -18]], [[-30, 30]], [[-25, 37]], [[-1014, -443]], [[0, 0]]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(symmetric_point(*i), o[0])"}
| 185
| 283
|
coding
|
Solve the programming task below in a Python markdown code block.
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Chef has $N$ dishes, numbered $1$ through $N$. For each valid $i$, dish $i$ is described by a string $D_{i}$ containing only lowercase vowels, i.e. characters 'a', 'e', 'i', 'o', 'u'.
A *meal* consists of exactly two dishes. Preparing a meal from dishes $i$ and $j$ ($i \neq j$) means concatenating the strings $D_{i}$ and $D_{j}$ in an arbitrary order into a string $M$ describing the meal. Chef *likes* this meal if the string $M$ contains each lowercase vowel at least once.
Now, Chef is wondering - what is the total number of (unordered) pairs of dishes such that he likes the meal prepared from these dishes?
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each valid $i$, the $i$-th of these lines contains a single string $D_{i}$.
------ Output ------
For each test case, print a single line containing one integer - the number of ways to prepare a meal Chef likes.
------ Constraints ------
$1 ≤ T ≤ 1,000$
$1 ≤ N ≤ 10^{5}$
$1 ≤ |D_{i}| ≤ 1,000$ for each valid $i$
the sum of all |D_{i}| over all test cases does not exceed $3 \cdot 10^{7}$
------ Subtasks ------
Subtask #1 (20 points):
$1 ≤ T ≤ 100$
$1 ≤ N ≤ 100$
the sum of all |D_{i}| over all test cases does not exceed $20000$
Subtask #2 (80 points): original constraints
----- Sample Input 1 ------
1
3
aaooaoaooa
uiieieiieieuuu
aeioooeeiiaiei
----- Sample Output 1 ------
2
----- explanation 1 ------
Example case 1: There are three possible meals:
- A meal prepared from dishes $1$ and $2$ (for example "aaooaoaooauiieieiieieuuu") contains all vowels.
- A meal prepared from dishes $1$ and $3$ (for example "aaooaoaooaaeioooeeiiaiei") does not contain 'u'.
- A meal prepared from dishes $2$ and $3$ (for example "uiieieiieieuuuaeioooeeiiaiei") contains all vowels.
|
{"inputs": ["1\n3\naaooaoaooa\nuiieieiieieuuu\naeioooeeiiaiei"], "outputs": ["2"]}
| 636
| 36
|
coding
|
Solve the programming task below in a Python markdown code block.
Probably everyone has experienced an awkward situation due to shared armrests between seats in cinemas. A highly accomplished cinema manager named "Chef" decided to solve this problem.
When a customer wants to buy a ticket, the clerk at the ticket window asks the visitor if they need the armrests, and if so, which of them: left, right, or both. We know that out of the audience expected to show up, L of them only need the left armrest, R of them need just the right one, Z need none and B need both. Your task is to calculate the maximum number of people that can attend the show. In the cinema hall there are N rows with M seats each. There is only one armrest between two adjacent seats. Seats at the beginning and at the end of the row have two armrests
-----Input-----
Input begins with an integer T: the number of test cases.
Each test case consists of a single line with 6 space-separated integers: N, M, Z, L, R, B.
-----Output-----
For each test case, output a line containing the answer for the task.
-----Constraints and Subtasks-----
- 1 ≤ T ≤ 105
Subtask 1 : 10 points
- 1 ≤ N, M ≤ 3
- 0 ≤ Z, L, R, B ≤ 3
Subtask 2 : 20 points
- 1 ≤ N, M ≤ 30
- 0 ≤ Z, L, R ≤ 30
- 0 ≤ B ≤ 109
Subtask 3 : 30 points
- 1 ≤ N, M ≤ 106
- 0 ≤ Z, L, R ≤ 106
- 0 ≤ B ≤ 1016
Subtask 4 : 40 points
- 1 ≤ N, M ≤ 108
- 0 ≤ Z, L, R, B ≤ 1016
-----Example-----
Input:2
2 2 3 2 1 1
3 3 1 2 0 9
Output:4
8
-----Explanation-----
'L' - needs left
'R - needs right
'Z' - doesn't need any
'B' - needs both
'-' - empty place
Example case 1.
ZZ
ZB
Example case 2.
LLB
BZB
B-B
|
{"inputs": ["2\n2 2 3 2 1 1\n3 3 1 2 0 9"], "outputs": ["4\n8"]}
| 531
| 38
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given a 32-bit integer num, return a string representing its hexadecimal representation. For negative integers, two’s complement method is used.
All the letters in the answer string should be lowercase characters, and there should not be any leading zeros in the answer except for the zero itself.
Note: You are not allowed to use any built-in library method to directly solve this problem.
Please complete the following python code precisely:
```python
class Solution:
def toHex(self, num: int) -> str:
```
|
{"functional": "def check(candidate):\n assert candidate(num = 26) == \"1a\"\n assert candidate(num = -1) == \"ffffffff\"\n\n\ncheck(Solution().toHex)"}
| 124
| 47
|
coding
|
Solve the programming task below in a Python markdown code block.
# Pythagorean Triples
A Pythagorean triplet is a set of three numbers a, b, and c where `a^2 + b^2 = c^2`. In this Kata, you will be tasked with finding the Pythagorean triplets whose product is equal to `n`, the given argument to the function `pythagorean_triplet`.
## Your task
In this Kata, you will be tasked with finding the Pythagorean triplets whose product is equal to `n`, the given argument to the function, where `0 < n < 10000000`
## Examples
One such triple is `3, 4, 5`. For this challenge, you would be given the value `60` as the argument to your function, and then it would return the Pythagorean triplet in an array `[3, 4, 5]` which is returned in increasing order. `3^2 + 4^2 = 5^2` since `9 + 16 = 25` and then their product (`3 * 4 * 5`) is `60`.
More examples:
| **argument** | **returns** |
| ---------|---------|
| 60 | [3, 4, 5] |
| 780 | [5, 12, 13] |
| 2040 | [8, 15, 17] |
Also feel free to reuse/extend the following starter code:
```python
def pythagorean_triplet(n):
```
|
{"functional": "_inputs = [[60], [780], [2040], [4200], [12180], [120120], [192720], [328860], [907200], [1440600]]\n_outputs = [[[3, 4, 5]], [[5, 12, 13]], [[8, 15, 17]], [[7, 24, 25]], [[20, 21, 29]], [[33, 56, 65]], [[48, 55, 73]], [[60, 63, 87]], [[42, 144, 150]], [[49, 168, 175]]]\nimport math\ndef _deep_eq(a, b, tol=1e-5):\n if isinstance(a, float) or isinstance(b, float):\n return math.isclose(a, b, rel_tol=tol, abs_tol=tol)\n if isinstance(a, (list, tuple)):\n if len(a) != len(b): return False\n return all(_deep_eq(x, y, tol) for x, y in zip(a, b))\n return a == b\n\nfor i, o in zip(_inputs, _outputs):\n assert _deep_eq(pythagorean_triplet(*i), o[0])"}
| 349
| 338
|
coding
|
Solve the programming task below in a Python markdown code block.
Let us consider a grid of squares with 10^9 rows and N columns. Let (i, j) be the square at the i-th column (1 \leq i \leq N) from the left and j-th row (1 \leq j \leq 10^9) from the bottom.
Snuke has cut out some part of the grid so that, for each i = 1, 2, ..., N, the bottom-most h_i squares are remaining in the i-th column from the left. Now, he will paint the remaining squares in red and blue. Find the number of the ways to paint the squares so that the following condition is satisfied:
* Every remaining square is painted either red or blue.
* For all 1 \leq i \leq N-1 and 1 \leq j \leq min(h_i, h_{i+1})-1, there are exactly two squares painted red and two squares painted blue among the following four squares: (i, j), (i, j+1), (i+1, j) and (i+1, j+1).
Since the number of ways can be extremely large, print the count modulo 10^9+7.
Constraints
* 1 \leq N \leq 100
* 1 \leq h_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
h_1 h_2 ... h_N
Output
Print the number of the ways to paint the squares, modulo 10^9+7.
Examples
Input
9
2 3 5 4 1 2 4 2 1
Output
12800
Input
2
2 2
Output
6
Input
5
2 1 2 1 2
Output
256
Input
9
27 18 28 18 28 45 90 45 23
Output
844733013
|
{"inputs": ["2\n4 2", "2\n2 2", "5\n2 1 2 1 2", "9\n3 3 5 4 1 2 4 2 1", "9\n2 3 7 4 1 2 4 2 1", "9\n2 3 4 4 1 2 4 2 1", "9\n2 3 2 4 1 2 4 2 1", "9\n2 3 2 4 1 2 4 2 2"], "outputs": ["24", "6", "256", "11520", "51200", "6400", "23040", "20736"]}
| 462
| 184
|
coding
|
Solve the programming task below in a Python markdown code block.
In the current semester, you have taken X RTP courses, Y Audit courses and Z Non-RTP courses.
The credit distribution for the courses are:
4 credits for clearing each RTP course.
2 credits for clearing each Audit course.
No credits for clearing a Non-RTP course.
Assuming that you cleared all your courses, report the number of credits you obtain this semester.
------ Input Format ------
The first line contains a single integer T, the number of test cases. T test cases follow. Each test case consists of one line, containing 3 integers separated by spaces.
- The first integer is X, the number of RTP courses.
- The second integer is Y, the number of Audit courses.
- The third integer is Z, the number of non-RTP courses.
------ Output Format ------
The output must consist of T lines. The i^{th} should consist of a single integer: the number of credits one gets for the i^{th} test case.
------ Constraints ------
$1 ≤T ≤10$
$1 ≤X, Y, Z ≤10$
----- Sample Input 1 ------
4
6 6 5
8 7 2
9 3 8
9 2 4
----- Sample Output 1 ------
36
46
42
40
----- explanation 1 ------
Test case $1$: You obtain $4$ credits for each of the RTP courses, accounting for $4 \cdot 6 = 24$ credits. You also obtain $2$ credits for each audit course, accounting for $2 \cdot 6 = 12$ credits. Finally, you get $0$ credits for each of the non-RTP courses, accounting for $0 \cdot 5 = 0$ credits. This accounts for a total of $24 + 12 + 0 = 36$ credits.
Test case $2$: You obtain $4$ credits for each of the RTP courses, accounting for $4 \cdot 8 = 32$ credits. You also obtain $2$ credits for each audit course, accounting for $2 \cdot 7 = 14$ credits. Finally, you get $0$ credits for each of the non-RTP courses, accounting for $0 \cdot 2 = 0$ credits. This accounts for a total of $32 + 14 + 0 = 46$ credits.
Test case $3$: You obtain $4$ credits for each of the RTP courses, accounting for $4 \cdot 9 = 36$ credits. You also obtain $2$ credits for each audit course, accounting for $2 \cdot 3 = 6$ credits. Finally, you get $0$ credits for each of the non-RTP courses, accounting for $0 \cdot 8 = 0$ credits. This accounts for a total of $36 + 6 + 0 = 42$ credits.
Test case $4$: You obtain $4$ credits for each of the RTP courses, accounting for $4 \cdot 9 = 36$ credits. You also obtain $2$ credits for each audit course, accounting for $2 \cdot 2 = 4$ credits. Finally, you get $0$ credits for each of the non-RTP courses, accounting for $0 \cdot 4 = 0$ credits. This accounts for a total of $36 + 4 + 0 = 40$ credits.
|
{"inputs": ["4\n6 6 5\n8 7 2\n9 3 8\n9 2 4\n"], "outputs": ["36\n46\n42\n40\n"]}
| 746
| 48
|
coding
|
Solve the programming task below in a Python markdown code block.
Counting sort can be used for sorting elements in an array which each of the n input elements is an integer in the range 0 to k. The idea of counting sort is to determine, for each input element x, the number of elements less than x as C[x]. This information can be used to place element x directly into its position in the output array B. This scheme must be modified to handle the situation in which several elements have the same value. Please see the following pseudocode for the detail:
Counting-Sort(A, B, k)
1 for i = 0 to k
2 do C[i] = 0
3 for j = 1 to length[A]
4 do C[A[j]] = C[A[j]]+1
5 /* C[i] now contains the number of elements equal to i */
6 for i = 1 to k
7 do C[i] = C[i] + C[i-1]
8 /* C[i] now contains the number of elements less than or equal to i */
9 for j = length[A] downto 1
10 do B[C[A[j]]] = A[j]
11 C[A[j]] = C[A[j]]-1
Write a program which sorts elements of given array ascending order based on the counting sort.
Constraints
* 1 ≤ n ≤ 2,000,000
* 0 ≤ A[i] ≤ 10,000
Input
The first line of the input includes an integer n, the number of elements in the sequence.
In the second line, n elements of the sequence are given separated by spaces characters.
Output
Print the sorted sequence. Two contiguous elements of the sequence should be separated by a space character.
Example
Input
7
2 5 1 3 2 3 0
Output
0 1 2 2 3 3 5
|
{"inputs": ["7\n2 5 1 3 2 3 1", "7\n2 5 1 3 4 3 1", "7\n2 5 2 3 4 3 1", "7\n4 5 2 3 4 3 1", "7\n4 9 2 3 4 3 1", "7\n4 9 2 3 4 3 2", "7\n4 9 2 1 4 3 2", "7\n4 9 2 0 4 3 3"], "outputs": ["1 1 2 2 3 3 5\n", "1 1 2 3 3 4 5\n", "1 2 2 3 3 4 5\n", "1 2 3 3 4 4 5\n", "1 2 3 3 4 4 9\n", "2 2 3 3 4 4 9\n", "1 2 2 3 4 4 9\n", "0 2 3 3 4 4 9\n"]}
| 421
| 270
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
You are given an array points containing the coordinates of points on a 2D plane, sorted by the x-values, where points[i] = [xi, yi] such that xi < xj for all 1 <= i < j <= points.length. You are also given an integer k.
Return the maximum value of the equation yi + yj + |xi - xj| where |xi - xj| <= k and 1 <= i < j <= points.length.
It is guaranteed that there exists at least one pair of points that satisfy the constraint |xi - xj| <= k.
Please complete the following python code precisely:
```python
class Solution:
def findMaxValueOfEquation(self, points: List[List[int]], k: int) -> int:
```
|
{"functional": "def check(candidate):\n assert candidate(points = [[1,3],[2,0],[5,10],[6,-10]], k = 1) == 4\n assert candidate(points = [[0,0],[3,0],[9,2]], k = 3) == 3\n\n\ncheck(Solution().findMaxValueOfEquation)"}
| 180
| 83
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given a string $s$ consisting of lowercase Latin letters. Let the length of $s$ be $|s|$. You may perform several operations on this string.
In one operation, you can choose some index $i$ and remove the $i$-th character of $s$ ($s_i$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $s_i$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $i$ should satisfy the condition $1 \le i \le |s|$ during each operation.
For the character $s_i$ adjacent characters are $s_{i-1}$ and $s_{i+1}$. The first and the last characters of $s$ both have only one adjacent character (unless $|s| = 1$).
Consider the following example. Let $s=$ bacabcab. During the first move, you can remove the first character $s_1=$ b because $s_2=$ a. Then the string becomes $s=$ acabcab. During the second move, you can remove the fifth character $s_5=$ c because $s_4=$ b. Then the string becomes $s=$ acabab. During the third move, you can remove the sixth character $s_6=$'b' because $s_5=$ a. Then the string becomes $s=$ acaba. During the fourth move, the only character you can remove is $s_4=$ b, because $s_3=$ a (or $s_5=$ a). The string becomes $s=$ acaa and you cannot do anything with it.
Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally.
-----Input-----
The first line of the input contains one integer $|s|$ ($1 \le |s| \le 100$) — the length of $s$.
The second line of the input contains one string $s$ consisting of $|s|$ lowercase Latin letters.
-----Output-----
Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally.
-----Examples-----
Input
8
bacabcab
Output
4
Input
4
bcda
Output
3
Input
6
abbbbb
Output
5
-----Note-----
The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $4$.
In the second example, you can remove all but one character of $s$. The only possible answer follows. During the first move, remove the third character $s_3=$ d, $s$ becomes bca. During the second move, remove the second character $s_2=$ c, $s$ becomes ba. And during the third move, remove the first character $s_1=$ b, $s$ becomes a.
|
{"inputs": ["1\na\n", "1\nt\n", "1\nz\n", "1\nt\n", "1\nz\n", "1\na\n", "1\ns\n", "1\ny\n"], "outputs": ["0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n", "0\n"]}
| 683
| 86
|
coding
|
Solve the programming task below in a Python markdown code block.
Given an array A of length N, your task is to find the element which repeats in A maximum number of times as well as the corresponding count. In case of ties, choose the smaller element first.
-----Input-----
First line of input contains an integer T, denoting the number of test cases. Then follows description of T cases. Each case begins with a single integer N, the length of A. Then follow N space separated integers in next line. Assume that 1 <= T <= 100, 1 <= N <= 100 and for all i in [1..N] : 1 <= A[i] <= 10000
-----Output-----
For each test case, output two space separated integers V & C. V is the value which occurs maximum number of times and C is its count.
-----Example-----
Input:
2
5
1 2 3 2 5
6
1 2 2 1 1 2
Output:
2 2
1 3
Description:
In first case 2 occurs twice whereas all other elements occur only once.
In second case, both 1 and 2 occur 3 times but 1 is smaller than 2.
|
{"inputs": ["2\n5\n1 2 3 2 5\n6\n1 2 2 1 1 2\n\n"], "outputs": ["2 2\n1 3"]}
| 269
| 46
|
coding
|
Solve the programming task below in a Python markdown code block.
Chef has two numbers X and Y. Chef wants to find three integers A, B, and C such that:
-1000 ≤ A, B, C ≤ 1000
mean([A, B, C]) = X
median([A, B, C]) = Y
Can you help Chef find such three integers?
As a reminder, mean([P, Q, R]) = \frac{P + Q + R}{3} and median([P, Q, R]) is the element at the 2^{nd} (middle) position after we sort [P, Q, R] in non-decreasing order.
------ Input Format ------
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first and only line of each test case contains two space-separated integers X and Y — the required mean and median of the three integers.
------ Output Format ------
For each test case, output three integers A, B, C which satisfy the given conditions.
It is guaranteed that an answer always exists under the given constraints.
If multiple answers exist, output any.
------ Constraints ------
$1 ≤ T ≤ 10^{5}$
$-100 ≤X, Y ≤100$
----- Sample Input 1 ------
3
5 5
67 100
4 5
----- Sample Output 1 ------
5 5 5
0 100 101
0 5 7
----- explanation 1 ------
Test Case 1: $mean([5, 5, 5]) = \frac{5 + 5 + 5}{3} = 5$, $median([5, 5, 5]) = 5$.
Test Case 2: $mean([0, 100, 101]) = \frac{0 + 100 + 101}{3} = \frac{201}{3} = 67$, $median([0, 100, 101]) = 100$.
Test Case 3: $mean([0, 5, 7]) = \frac{0 + 5 + 7}{3} = 4$, $median([0, 5, 7]) = 5$.
|
{"inputs": ["3\n5 5\n67 100\n4 5\n"], "outputs": ["5 5 5\n0 100 101\n0 5 7"]}
| 513
| 48
|
coding
|
Solve the programming task below in a Python markdown code block.
You are given an integer K.
Consider an integer sequence A = [A_{1}, A_{2}, \ldots, A_{N}].
Define another sequence S of length N, such that S_{i} = A_{1} + A_{2} + \ldots + A_{i} for each 1 ≤ i ≤ N.
A is said to be *interesting* if A_{i} + S_{i} = K for every 1 ≤ i ≤ N.
Find the maximum length of an interesting sequence. If there are no interesting sequences, print 0.
------ Input Format ------
- The first line of input contains an integer T, denoting the number of test cases. The description of T test cases follows.
- Each test case consists of a single line of input, which contains a single integer K.
------ Output Format ------
- For each test case, print a new line containing one integer — the maximum length of an *interesting* sequence for a given value of K.
------ Constraints ------
$1 ≤ T ≤ 5000$
$1 ≤ K ≤ 10^{9}$
----- Sample Input 1 ------
2
8
4
----- Sample Output 1 ------
3
2
----- explanation 1 ------
Test Case $1$: Consider the sequence $A = [4, 2, 1]$. $S = [4, 4 + 2, 4 + 2 + 1] = [4, 6, 7]$.
The sum of the corresponding elements of $A$ and $S$ are $4 + 4 = 8$, $2 + 6 = 8$ and $1 + 7 = 8$. Thus $A$ is an interesting sequence of size $3$.
Test Case $2$: Consider the sequence $A = [2, 1]$. $S = [2, 2 + 1] = [2, 3]$.
The sum of the corresponding elements of $A$ and $S$ are $2 + 2 = 4$ and $1 + 3 = 4$. Thus $A$ is an interesting sequence of size $2$.
It's guaranteed that, in both examples above, there doesn't exist a longer sequence satisfying the problem constraints.
|
{"inputs": ["2\n8\n4"], "outputs": ["3\n2"]}
| 498
| 18
|
coding
|
Solve the programming task below in a Python markdown code block.
Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n elements a_1, a_2, ..., a_{n} in ascending order. He is bored of this so simple algorithm, so he invents his own graph. The graph (let's call it G) initially has n vertices and 0 edges. During Bubble Sort execution, edges appear as described in the following algorithm (pseudocode).
procedure bubbleSortGraph()
build a graph G with n vertices and 0 edges
repeat
swapped = false
for i = 1 to n - 1 inclusive do:
if a[i] > a[i + 1] then
add an undirected edge in G between a[i] and a[i + 1]
swap( a[i], a[i + 1] )
swapped = true
end if
end for
until not swapped
/* repeat the algorithm as long as swapped value is true. */
end procedure
For a graph, an independent set is a set of vertices in a graph, no two of which are adjacent (so there are no edges between vertices of an independent set). A maximum independent set is an independent set which has maximum cardinality. Given the permutation, find the size of the maximum independent set of graph G, if we use such permutation as the premutation a in procedure bubbleSortGraph.
-----Input-----
The first line of the input contains an integer n (2 ≤ n ≤ 10^5). The next line contains n distinct integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ n).
-----Output-----
Output a single integer — the answer to the problem.
-----Examples-----
Input
3
3 1 2
Output
2
-----Note-----
Consider the first example. Bubble sort swaps elements 3 and 1. We add edge (1, 3). Permutation is now [1, 3, 2]. Then bubble sort swaps elements 3 and 2. We add edge (2, 3). Permutation is now sorted. We have a graph with 3 vertices and 2 edges (1, 3) and (2, 3). Its maximal independent set is [1, 2].
|
{"inputs": ["3\n3 1 2\n", "3\n3 1 3\n", "3\n2 1 3\n", "3\n3 1 2\n", "5\n4 2 1 3 5\n", "5\n4 2 1 3 5\n", "5\n4 2 1 2 5\n", "10\n1 9 8 10 2 3 4 6 5 7\n"], "outputs": ["2\n", "2\n", "2\n", "2\n", "3\n", "3\n", "3\n", "6\n"]}
| 496
| 146
|
coding
|
Please solve the programming task below using a self-contained code snippet in a markdown code block.
Given an integer n, return true if it is a power of two. Otherwise, return false.
An integer n is a power of two, if there exists an integer x such that n == 2x.
Please complete the following python code precisely:
```python
class Solution:
def isPowerOfTwo(self, n: int) -> bool:
```
|
{"functional": "def check(candidate):\n assert candidate(n = 1) == True\n assert candidate(n = 16) == True\n assert candidate(n = 3) == False\n\n\ncheck(Solution().isPowerOfTwo)"}
| 92
| 55
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.