Knowee
Questions
Features
Study Tools

Alice and Bob came up with a rather strange game. They have an array of integers a1,a2,…,an𝑎1,𝑎2,…,𝑎𝑛. Alice chooses a certain integer k𝑘 and tells it to Bob, then the following happens:Bob chooses two integers i𝑖 and j𝑗 (1≤i<j≤n1≤𝑖<𝑗≤𝑛), and then finds the maximum among the integers ai,ai+1,…,aj𝑎𝑖,𝑎𝑖+1,…,𝑎𝑗;If the obtained maximum is strictly greater than k𝑘, Alice wins, otherwise Bob wins.Help Alice find the maximum k𝑘 at which she is guaranteed to win.InputEach test consists of multiple test cases. The first line contains a single integer t𝑡 (1≤t≤1041≤𝑡≤104) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer n𝑛 (2≤n≤5⋅1042≤𝑛≤5⋅104) — the number of elements in the array.The second line of each test case contains n𝑛 integers a1,a2,…,an𝑎1,𝑎2,…,𝑎𝑛 (1≤ai≤1091≤𝑎𝑖≤109) — the elements of the array.It is guaranteed that the sum of n𝑛 over all test cases does not exceed 5⋅1045⋅104.OutputFor each test case, output one integer — the maximum integer k𝑘 at which Alice is guaranteed to win.ExampleinputCopy642 4 1 751 2 3 4 521 1337 8 16510 10 10 10 9103 12 9 5 2 3 2 9 8 2outputCopy3101592NoteIn the first test case, all possible subsegments that Bob can choose look as follows: [2,4],[2,4,1],[2,4,1,7],[4,1],[4,1,7],[1,7][2,4],[2,4,1],[2,4,1,7],[4,1],[4,1,7],[1,7]. The maximums on the subsegments are respectively equal to 4,4,7,4,7,74,4,7,4,7,7. It can be shown that 33 is the largest integer such that any of the maximums will be strictly greater than it.In the third test case, the only segment that Bob can choose is [1,1][1,1]. So the answer is 00.

Question

Alice and Bob came up with a rather strange game. They have an array of integers a1,a2,…,an𝑎1,𝑎2,…,𝑎𝑛. Alice chooses a certain integer k𝑘 and tells it to Bob, then the following happens:Bob chooses two integers i𝑖 and j𝑗 (1≤i<j≤n1≤𝑖<𝑗≤𝑛), and then finds the maximum among the integers ai,ai+1,…,aj𝑎𝑖,𝑎𝑖+1,…,𝑎𝑗;If the obtained maximum is strictly greater than k𝑘, Alice wins, otherwise Bob wins.Help Alice find the maximum k𝑘 at which she is guaranteed to win.InputEach test consists of multiple test cases. The first line contains a single integer t𝑡 (1≤t≤1041≤𝑡≤104) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer n𝑛 (2≤n≤5⋅1042≤𝑛≤5⋅104) — the number of elements in the array.The second line of each test case contains n𝑛 integers a1,a2,…,an𝑎1,𝑎2,…,𝑎𝑛 (1≤ai≤1091≤𝑎𝑖≤109) — the elements of the array.It is guaranteed that the sum of n𝑛 over all test cases does not exceed 5⋅1045⋅104.OutputFor each test case, output one integer — the maximum integer k𝑘 at which Alice is guaranteed to win.ExampleinputCopy642 4 1 751 2 3 4 521 1337 8 16510 10 10 10 9103 12 9 5 2 3 2 9 8 2outputCopy3101592NoteIn the first test case, all possible subsegments that Bob can choose look as follows: [2,4],[2,4,1],[2,4,1,7],[4,1],[4,1,7],[1,7][2,4],[2,4,1],[2,4,1,7],[4,1],[4,1,7],[1,7]. The maximums on the subsegments are respectively equal to 4,4,7,4,7,74,4,7,4,7,7. It can be shown that 33 is the largest integer such that any of the maximums will be strictly greater than it.In the third test case, the only segment that Bob can choose is [1,1][1,1]. So the answer is 00.

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

This problem is about finding the maximum integer 'k' that Alice can choose such that she is guaranteed to win the game. The game involves Alice and Bob choosing subsegments from an array of integers and comparing the maximum value of the subsegment with 'k'. If the maximum value is strictly greater than 'k', Alice wins.

Here are the steps to solve this problem:

  1. Read the number of test cases 't'.
  2. For each test case, do the following: a. Read the number of elements in the array 'n'. b. Read the elements of the array.
  3. For each test case, find all possible subsegments that Bob can choose. A subsegment is a contiguous part of the array. For example, if the array is [2, 4, 1, 7], the possible subsegments are [2, 4], [2, 4, 1], [2, 4, 1, 7], [4, 1], [4, 1, 7], [1, 7].
  4. For each subsegment, find the maximum value.
  5. Find the largest integer 'k' such that any of the maximums will be strictly greater than it. This is the maximum 'k' at which Alice is guaranteed to win.
  6. Print the maximum 'k' for each test case.

For example, in the first test case, the maximums on the subsegments are 4, 4, 7, 4, 7, 7. The largest integer 'k' such that any of the maximums will be strictly greater than it is 3. So, the output for this test case is 3.

This problem has been solved

Similar Questions

You are given an array of positive integers nums.Alice and Bob are playing a game. In the game, Alice can choose either all single-digit numbers or all double-digit numbers from nums, and the rest of the numbers are given to Bob. Alice wins if the sum of her numbers is strictly greater than the sum of Bob's numbers.Return true if Alice can win this game, otherwise, return false. Example 1:Input: nums = [1,2,3,4,10]Output: falseExplanation:Alice cannot win by choosing either single-digit or double-digit numbers.Example 2:Input: nums = [1,2,3,4,5,14]Output: trueExplanation:Alice can win by choosing single-digit numbers which have a sum equal to 15.Example 3:Input: nums = [5,5,5,25]Output: trueExplanation:Alice can win by choosing double-digit numbers which have a sum equal to 25

Two kittens, Max and Min, play with a pair of non-negative integers x and y. As you can guess from their names, kitten Max loves to maximize and kitten Min loves to minimize. As part of this game Min wants to make sure that both numbers, x and y became negative at the same time, and kitten Max tries to prevent him from doing so.Each kitten has a set of pairs of integers available to it. Kitten Max has n pairs of non-negative integers (ai, bi) (1 ≤ i ≤ n), and kitten Min has m pairs of non-negative integers (cj, dj) (1 ≤ j ≤ m). As kitten Max makes a move, it can take any available pair (ai, bi) and add ai to x and bi to y, and kitten Min can take any available pair (cj, dj) and subtract cj from x and dj from y. Each kitten can use each pair multiple times during distinct moves.Max moves first. Kitten Min is winning if at some moment both numbers a, b are negative simultaneously. Otherwise, the winner of the game is kitten Max. Determine which kitten wins if both of them play optimally.InputThe first line contains two integers, n and m (1 ≤ n, m ≤ 100 000) — the number of pairs of numbers available to Max and Min, correspondingly.The second line contains two integers x, y (1 ≤ x, y ≤ 109) — the initial values of numbers with which the kittens are playing.Next n lines contain the pairs of numbers ai, bi (1 ≤ ai, bi ≤ 109) — the pairs available to Max.The last m lines contain pairs of numbers cj, dj (1 ≤ cj, dj ≤ 109) — the pairs available to Min.OutputPrint «Max» (without the quotes), if kitten Max wins, or "Min" (without the quotes), if kitten Min wins.

You are given an array 𝐴A of length 𝑁N, and an integer 𝐾K.You can perform the following operation:Choose any index 𝑖i (1≤𝑖≤𝑁1≤i≤N), and increase 𝐴𝑖A i​ by 𝐾K.Find the minimum possible value of max⁡(𝐴)−min⁡(𝐴)max(A)−min(A) attainable, if you can perform this operation as many times as you like (possibly, zero times).Input FormatThe first line of input will contain a single integer 𝑇T, denoting the number of test cases.Each test case consists of two lines of input.The first line of each test case contains two space-separated integers 𝑁N and 𝐾K — the length of the array and the parameter 𝐾K.The second line contains 𝑁N space-separated integers 𝐴1,𝐴2,…,𝐴𝑁A 1​ ,A 2​ ,…,A N​ — the initial values of the array elements.Output FormatFor each test case, output on a new line the answer: the minimum possible value of max⁡(𝐴)−min⁡(𝐴)max(A)−min(A) if you can perform the given operation any number of times.Constraints1≤𝑇≤1051≤T≤10 5 1≤𝑁≤2⋅1051≤N≤2⋅10 5 1≤𝐾≤1091≤K≤10 9 1≤𝐴𝑖≤1091≤A i​ ≤10 9 The sum of 𝑁N over all test cases won't exceed 2⋅1052⋅10 5 .Sample 1:InputOutput43 41 5 43 212 8 44 11 43 62 8256 121 2 4 128 130 1311008Explanation:Test case 11: Increase the first element by 𝐾=4K=4 to obtain the array [5,5,4][5,5,4].Here, max⁡−min⁡=5−4=1max−min=5−4=1, which is the best possible.Test case 22: The second and third elements can be increased by 22 till they reach 1212, at which point all the elements of the array are equal, so max⁡(𝐴)−min⁡(𝐴)=0max(A)−min(A)=0.Test case 33: Since 𝐾=1K=1, again it's possible to make all the elements equal.Test case 44: Do the following:Increase 𝐴1A 1​ by 1212 repeatedly to make it 133133.Increase 𝐴2A 2​ by 1212 repeatedly to make it 134134.Increase 𝐴3A 3​ by 1212 repeatedly to make it 136136.The array is now [133,134,136,128,130,131][133,134,136,128,130,131].For this array, max⁡(𝐴)−min⁡(𝐴)=136−128=8max(A)−min(A)=136−128=8.It can be shown that this is optimal.

ai is creating a program to find the maximum number from the given two integers using pointers.Help him with the task.Input format :The input consists of two space-separated integers.Output format :The output prints the maximum number.Code constraints :In this scenario, the test cases fall under the following constraints:1 ≤ input integers ≤ 100Sample test cases :

A contest contains n𝑛 problems and the difficulty of the i𝑖-th problem is expected to be at most bi𝑏𝑖. There are already n𝑛 problem proposals and the difficulty of the i𝑖-th problem is ai𝑎𝑖.Some of the problems may be more difficult than expected, so the writers must propose more problems. When a new problem with difficulty w𝑤 is proposed, the most difficult problem will be deleted from the contest, and the problems will be sorted in a way that the difficulties are non-decreasing.In other words, in each operation, you choose an integer w𝑤, insert it into the array a𝑎, sort array a𝑎 in non-decreasing order, and remove the last element from it.Find the minimum number of new problems to make ai≤bi𝑎𝑖≤𝑏𝑖 for all i𝑖.InputEach test contains multiple test cases. The first line contains the number of test cases t𝑡 (1≤t≤1001≤𝑡≤100). The description of the test cases follows.The first line of each test case contains only one positive integer n𝑛 (1≤n≤1001≤𝑛≤100), representing the number of problems.The second line of each test case contains an array a𝑎 of length n𝑛 (1≤a1≤a2≤⋯≤an≤1091≤𝑎1≤𝑎2≤⋯≤𝑎𝑛≤109).The third line of each test case contains an array b𝑏 of length n𝑛 (1≤b1≤b2≤⋯≤bn≤1091≤𝑏1≤𝑏2≤⋯≤𝑏𝑛≤109).OutputFor each test case, print an integer as your answer in a new line.ExampleinputCopy261000 1400 2000 2000 2200 2700800 1200 1500 1800 2200 300064 5 6 7 8 91 2 3 4 5 6outputCopy23

1/2

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.