0% found this document useful (0 votes)
26 views15 pages

Soal Binary Search

Uploaded by

Mamat Rahmat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views15 pages

Soal Binary Search

Uploaded by

Mamat Rahmat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

[Lanjutan] Latihan Binary Search Aug 31, 2024

Problem A. Binary Search - Basic


OS Linux

You need to find index (0-based) of a given key in a sorted array. Use only Binary Search.

Input Format

The first line contains N.Next line contains N integers of the array. The next line contains
the key to be searched. If element is not found print -1.

Constraints

N<=1000000

Output Format

Single Interger index or -1.

Input Output

5 3
1 2 3 4 5
4

Explanation

Element 4 is present at 3rd index.

-
[Lanjutan] Latihan Binary Search Aug 31, 2024

Problem B. Interesting drink


Time limit 2000 ms
Mem limit 262144 kB

Vasiliy likes to rest after a hard work, so you may often meet him in some bar nearby. As all
programmers do, he loves the famous drink "Beecola", which can be bought in n different
shops in the city. It's known that the price of one bottle in the shop i is equal to xi coins.

Vasiliy plans to buy his favorite drink for q consecutive days. He knows, that on the i-th day
he will be able to spent mi coins. Now, for each of the days he want to know in how many

different shops he can buy a bottle of "Beecola".

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of
shops in the city that sell Vasiliy's favourite drink.

The second line contains n integers xi (1 ≤ xi ≤ 100 000) — prices of the bottles of the drink

in the i-th shop.

The third line contains a single integer q (1 ≤ q ≤ 100 000) — the number of days Vasiliy
plans to buy the drink.

Then follow q lines each containing one integer mi (1 ≤ mi ≤ 109) — the number of coins

Vasiliy can spent on the i-th day.

Output

Print q integers. The i-th of them should be equal to the number of shops where Vasiliy will
be able to buy a bottle of the drink on the i-th day.

Examples

-
[Lanjutan] Latihan Binary Search Aug 31, 2024

Input Output

5 0
3 10 8 6 11 4
4 1
1 5
10
3
11

Note

On the first day, Vasiliy won't be able to buy a drink in any of the shops.

On the second day, Vasiliy can buy a drink in the shops 1, 2, 3 and 4.

On the third day, Vasiliy can buy a drink only in the shop number 1.

Finally, on the last day Vasiliy can buy a drink in any shop.

-
[Lanjutan] Latihan Binary Search Aug 31, 2024

Problem C. Buy an Integer


Time limit 2000 ms
Mem limit 1048576 kB

Problem Statement

Takahashi has come to an integer shop to buy an integer.

The shop sells the integers from 1 through 109 . The integer N is sold for A × N +B×
d(N ) yen (the currency of Japan), where d(N ) is the number of digits in the decimal
notation of N .

Find the largest integer that Takahashi can buy when he has X yen. If no integer can be
bought, print 0.

Constraints

All values in input are integers.


1 ≤ A ≤ 109
1 ≤ B ≤ 109
1 ≤ X ≤ 1018

Input

Input is given from Standard Input in the following format:

A B X

Output

Print the greatest integer that Takahashi can buy. If no integer can be bought, print 0.

Sample 1
Input Output

10 7 100 9

-
[Lanjutan] Latihan Binary Search Aug 31, 2024

The integer 9 is sold for 10 × 9 + 7 × 1 = 97 yen, and this is the greatest integer that can be
bought. Some of the other integers are sold for the following prices:

10 : 10 × 10 + 7 × 2 = 114 yen
100 : 10 × 100 + 7 × 3 = 1021 yen
12345 : 10 × 12345 + 7 × 5 = 123485 yen

Sample 2
Input Output

2 1 100000000000 1000000000

He can buy the largest integer that is sold. Note that input may not fit into a 32-bit integer
type.

Sample 3
Input Output

1000000000 1000000000 100 0

Sample 4
Input Output

1234 56789 314159265 254309

-
[Lanjutan] Latihan Binary Search Aug 31, 2024

Problem D. To Add or Not to Add


Time limit 2000 ms
Mem limit 262144 kB
Input file stdin
Output file stdout

A piece of paper contains an array of n integers a1, a2, ..., an. Your task is to find a number
that occurs the maximum number of times in this array.

However, before looking for such number, you are allowed to perform not more than k
following operations — choose an arbitrary element from the array and add 1 to it. In other
words, you are allowed to increase some array element by 1 no more than k times (you are
allowed to increase the same element of the array multiple times).

Your task is to find the maximum number of occurrences of some number in the array after
performing no more than k allowed operations. If there are several such numbers, your task
is to find the minimum one.

Input

The first line contains two integers n and k (1 ≤ n ≤ 105; 0 ≤ k ≤ 109) — the number of
elements in the array and the number of operations you are allowed to perform,
correspondingly.

The third line contains a sequence of n integers a1, a2, ..., an (|ai| ≤ 109) — the initial array.

The numbers in the lines are separated by single spaces.

Output

In a single line print two numbers — the maximum number of occurrences of some number
in the array after at most k allowed operations are performed, and the minimum number
that reaches the given maximum. Separate the printed numbers by whitespaces.

Examples

-
[Lanjutan] Latihan Binary Search Aug 31, 2024

Input Output

5 3 3 4
6 3 4 0 2

Input Output

3 4 3 5
5 5 5

Input Output

5 3 4 2
3 1 2 2 1

Note

In the first sample your task is to increase the second element of the array once and increase
the fifth element of the array twice. Thus, we get sequence 6, 4, 4, 0, 4, where number 4
occurs 3 times.

In the second sample you don't need to perform a single operation or increase each element
by one. If we do nothing, we get array 5, 5, 5, if we increase each by one, we get 6, 6, 6. In
both cases the maximum number of occurrences equals 3. So we should do nothing, as
number 5 is less than number 6.

In the third sample we should increase the second array element once and the fifth element
once. Thus, we get sequence 3, 2, 2, 2, 2, where number 2 occurs 4 times.

-
[Lanjutan] Latihan Binary Search Aug 31, 2024

Problem E. Primes on Interval


Time limit 1000 ms
Mem limit 262144 kB
Input file stdin
Output file stdout

You've decided to carry out a survey in the theory of prime numbers. Let us remind you that
a prime number is a positive integer that has exactly two distinct positive integer divisors.

Consider positive integers a, a + 1, ..., b (a ≤ b). You want to find the minimum integer l
(1 ≤ l ≤ b - a + 1) such that for any integer x (a ≤ x ≤ b - l + 1) among l integers x, x + 1, ...,
x + l - 1 there are at least k prime numbers.

Find and print the required minimum l. If no value l meets the described limitations, print
-1.

Input

A single line contains three space-separated integers a, b, k (1 ≤ a, b, k ≤ 10


6; a ≤ b).

Output

In a single line print a single integer — the required minimum l. If there's no solution, print
-1.

Examples
Input Output

2 4 2 3

Input Output

6 13 1 4

Input Output

1 4 3 -1

-
[Lanjutan] Latihan Binary Search Aug 31, 2024

Problem F. Logs
Time limit 2000 ms
Mem limit 1048576 kB

Problem Statement

We have N logs of lengths A1 , A2 , ⋯ AN .


​ ​ ​

We can cut these logs at most K times in total. When a log of length L is cut at a point
whose distance from an end of the log is t (0 < t < L), it becomes two logs of lengths t and
L − t.

Find the shortest possible length of the longest log after at most K cuts, and print it after
rounding up to an integer.

Constraints

1 ≤ N ≤ 2 × 105
0 ≤ K ≤ 109
1 ≤ Ai ≤ 109

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
A1 A2 ⋯ AN
​ ​ ​

Output

Print an integer representing the answer.

Sample 1

-
[Lanjutan] Latihan Binary Search Aug 31, 2024

Input Output

2 3 4
7 9

First, we will cut the log of length 7 at a point whose distance from an end of the log is
3.5, resulting in two logs of length 3.5 each.
Next, we will cut the log of length 9 at a point whose distance from an end of the log is
3, resulting in two logs of length 3 and 6.
Lastly, we will cut the log of length 6 at a point whose distance from an end of the log
is 3.3, resulting in two logs of length 3.3 and 2.7.

In this case, the longest length of a log will be 3.5, which is the shortest possible result.
After rounding up to an integer, the output should be 4.

Sample 2
Input Output

3 0 5
3 4 5

Sample 3
Input Output

10 10 292638192
158260522 877914575 602436426 24979445
861648772 623690081 433933447 476190629
262703497 211047202

-
[Lanjutan] Latihan Binary Search Aug 31, 2024

Problem G. Close Tuples (hard version)


Time limit 4000 ms
Mem limit 262144 kB

This is the hard version of this problem. The only difference between the easy and hard
versions is the constraints on k and m. In this version of the problem, you need to output
the answer by modulo 109 + 7.

You are given a sequence a of length n consisting of integers from 1 to n. The sequence may
contain duplicates (i.e. some elements can be equal).

Find the number of tuples of m elements such that the maximum number in the tuple
differs from the minimum by no more than k . Formally, you need to find the number of
tuples of m indices i1 ​
< i2 < … < im , such that
​ ​

max(ai1 , ai2 , … , aim ) − min(ai1 , ai2 , … , aim ) ≤ k.








For example, if n = 4, m = 3, k = 2, a = [1, 2, 4, 3], then there are two such triples (i =
1, j = 2, z = 4 and i = 2, j = 3, z = 4). If n = 4, m = 2, k = 1, a = [1, 1, 1, 1], then all six
possible pairs are suitable.

As the result can be very large, you should print the value modulo 109 + 7 (the remainder
when divided by 109 + 7).

Input

The first line contains a single integer t (1 ≤ t ≤ 2 ⋅ 105 ) — the number of test cases. Then t
test cases follow.

The first line of each test case contains three integers n, m, k (1 ≤ n ≤ 2 ⋅ 105 , 1 ≤ m ≤
100, 1 ≤ k ≤ n) — the length of the sequence a, number of elements in the tuples and the
maximum difference of elements in the tuple.

The next line contains n integers a1 , a2 , … , an (1 ​ ​ ≤ ai ≤ n) — the sequence a.


It is guaranteed that the sum of n for all test cases does not exceed 2 ⋅ 105 .

Output

-
[Lanjutan] Latihan Binary Search Aug 31, 2024

Output t answers to the given test cases. Each answer is the required number of tuples of m
elements modulo 109
+ 7, such that the maximum value in the tuple differs from the
minimum by no more than k .

Examples
Input Output

4 2
4 3 2 6
1 2 4 3 1
4 2 1 20
1 1 1 1
1 1 1
1
10 4 3
5 6 1 3 2 9 8 1 2 4

-
[Lanjutan] Latihan Binary Search Aug 31, 2024

Problem H. Tea Tasting


Time limit 2000 ms
Mem limit 262144 kB

A tea manufacturer decided to conduct a massive tea tasting. n sorts of tea will be tasted by
n tasters. Both the sorts of tea and the tasters are numbered from 1 to n. The manufacturer
prepared ai milliliters of the i-th sort of tea. The j -th taster can drink bj milliliters of tea at
​ ​

once.

The tasting will be conducted in steps. During the first step, the i-th taster tastes the i-th
sort of tea. The i-th taster drinks min(ai , bi ) tea (how much is available of the i-th sort and
​ ​

how much the i-th taster can drink). ai also decreases by this amount.

Then all tasters move to the previous sort of tea. Thus, during the second step, the i-th
taster tastes the (i − 1)-st sort of tea. The i-th taster drinks min(ai−1 , bi ) tea. The 1-st
​ ​

person ends the tasting.

During the third step, the i-th taster tastes the (i − 2)-nd sort of tea. The 2-nd taster ends
the tasting. This goes on until everyone ends the tasting.

Take a look at the tasting process for n = 3, a = [10, 20, 15], b = [9, 8, 6]. In the left row,
there are the current amounts of each sort of tea. In the right column, there are current
amounts of tea each taster has drunk in total. The arrow tells which taster each tea goes to
on the current step. The number on the arrow is the amount — minimum of how much is
available of the sort of tea and how much the taster can drink.

For each taster, print how many milliliters of tea he/she will drink in total.

-
[Lanjutan] Latihan Binary Search Aug 31, 2024

Input

The first line contains a single integer t (1 ≤ t ≤ 104 ) — the number of testcases.

The first line of each testcase contains a single integer n (1 ≤ n ≤ 2 ⋅ 105 ) — the number of
sorts of tea and the number of tasters.

The second line contains n integers a1 , a2 , … , an (1


​ ​ ​ ≤ ai ≤ 109 ) — the amount of each

sort of tea.

The third line contains n integers b1 , b2 , … , bn (1


​ ​ ​ ≤ bi ≤ 109 ) — the amount of tea each

taster can drink at once.

The sum of n over all testcases doesn't exceed 2 ⋅ 105 .

Output

For each testcase, print n integers — the i-th value should be equal to the total amount of
tea the i-th taster will drink.

Examples
Input Output

4 9 9 12
3 5
10 20 15 3 8 6 4
9 8 6 1 2 2999999997
1
5
7
4
13 8 5 4
3 4 2 1
3
1000000000 1000000000 1000000000
1 1 1000000000

Note

The first testcase is described in the statement. Here are the remaining amounts of each
sort of tea after each step and the total amount of tea each taster has drunk:

a = [1, 12, 9], ans = [9, 8, 6]


a = [0, 6, 9], ans = [9, 9, 12]

-
[Lanjutan] Latihan Binary Search Aug 31, 2024

a = [0, 6, 9], ans = [9, 9, 12]

In the second testcase, the only taster drinks min(5, 7) milliliters of tea of the only sort.

Here are the remaining amounts of each sort of tea after each step and the total amount of
tea each taster has drunk for the third testcase:

a = [10, 4, 3, 3], ans = [3, 4, 2, 1];


a = [6, 2, 2, 3], ans = [3, 8, 4, 2];
a = [4, 1, 2, 3], ans = [3, 8, 6, 3];
a = [3, 1, 2, 3], ans = [3, 8, 6, 4].

Here are the remaining amounts of each sort of tea after each step and the total amount of
tea each taster has drunk for the fourth testcase:

a = [999999999, 999999999, 0], ans = [1, 1, 1000000000];


a = [999999998, 0, 0], ans = [1, 2, 1999999999];
a = [0, 0, 0], ans = [1, 2, 2999999997].

You might also like