给定两个数组A []和B [] 。其中A []的大小表示的行数和A [I]表示的箱数的第i行。数组B []表示球的数组,其中B [i]表示球上的数字。假定将球i (具有值B [i])放置在从开始位置为B [i](主要行)的盒子中。任务是找到与每个B [i]对应的框的行和列。
例子:
Input: A[] = {2, 3, 4, 5}, B[] = {1, 4, 6, 3}
Output:
1, 1
2, 2
3, 1
2, 1
B[0] = 1, hence Box position will be 1st row, 1st column
B[1] = 4, hence Box position will be 2nd row, 2nd column
B[2] = 6, hence Box position will be 3rd row, 1st column
B[3] = 3, hence Box position will be 2nd row, 1st column
Input: A[] = {2, 2, 2, 2}, B[] = {1, 2, 3, 4}
Output:
1, 1
1, 2
2, 1
2, 2
方法:根据问题陈述,在第一行A [0]中类似地放置许多盒子,在第二行A [1]中也存在许多盒子。因此,如果要将球放置在第二行的任何框中,则其值必须大于A [0] 。因此,为了找到要放置球B [i]的盒子的实际位置,首先找到数组A []的累加和,然后找到元素的位置在累加和数组中,该位置刚好大于B [i]将是行号,并且要在该特定行中找到框号,请找到B [i]的值–累积数组中的值刚好小于B [i] 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to print the position of each boxes
// where a ball has to be placed
void printPosition(int A[], int B[], int sizeOfA, int sizeOfB)
{
// Find the cumulative sum of array A[]
for (int i = 1; i < sizeOfA; i++)
A[i] += A[i - 1];
// Find the position of box for each ball
for (int i = 0; i < sizeOfB; i++) {
// Row number
int row = lower_bound(A, A + sizeOfA, B[i]) - A;
// Column (position of box in particular row)
int boxNumber = (row >= 1) ? B[i] - A[row - 1] : B[i];
// Row + 1 denotes row if indexing of array start from 1
cout << row + 1 << ", " << boxNumber << "\n";
}
}
// Driver code
int main()
{
int A[] = { 2, 2, 2, 2 };
int B[] = { 1, 2, 3, 4 };
int sizeOfA = sizeof(A) / sizeof(A[0]);
int sizeOfB = sizeof(B) / sizeof(B[0]);
printPosition(A, B, sizeOfA, sizeOfB);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to print the position of each boxes
// where a ball has to be placed
static void printPosition(int A[], int B[],
int sizeOfA, int sizeOfB)
{
// Find the cumulative sum of array A[]
for (int i = 1; i < sizeOfA; i++)
{
A[i] += A[i - 1];
}
// Find the position of box for each ball
for (int i = 0; i < sizeOfB; i++)
{
// Row number
int row = lower_bound(A, 0, A.length, B[i]);
// Column (position of box in particular row)
int boxNumber = (row >= 1) ? B[i] - A[row - 1] : B[i];
// Row + 1 denotes row if indexing of array start from 1
System.out.print(row + 1 + ", " + boxNumber + "\n");
}
}
private static int lower_bound(int[] a, int low, int high, int element)
{
while (low < high)
{
int middle = low + (high - low) / 2;
if (element > a[middle])
{
low = middle + 1;
}
else
{
high = middle;
}
}
return low;
}
// Driver code
public static void main(String[] args)
{
int A[] = {2, 2, 2, 2};
int B[] = {1, 2, 3, 4};
int sizeOfA = A.length;
int sizeOfB = B.length;
printPosition(A, B, sizeOfA, sizeOfB);
}
}
// This code has been contributed by 29AjayKumar
Python3
# Python3 implementation of the approach
import bisect
# Function to print the position of each boxes
# where a ball has to be placed
def printPosition(A, B, sizeOfA, sizeOfB):
# Find the cumulative sum of array A[]
for i in range(1, sizeOfA):
A[i] += A[i - 1]
# Find the position of box for each ball
for i in range(sizeOfB):
# Row number
row = bisect.bisect_left(A, B[i])
# Column (position of box in particular row)
if row >= 1:
boxNumber = B[i] - A[row - 1]
else:
boxNumber = B[i]
# Row + 1 denotes row
# if indexing of array start from 1
print(row + 1, ",", boxNumber)
# Driver code
A = [2, 2, 2, 2]
B = [1, 2, 3, 4]
sizeOfA = len(A)
sizeOfB = len(B)
printPosition(A, B, sizeOfA, sizeOfB)
# This code is contributed by Mohit Kumar
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to print the position of each boxes
// where a ball has to be placed
static void printPosition(int []A, int []B,
int sizeOfA, int sizeOfB)
{
// Find the cumulative sum of array A[]
for (int i = 1; i < sizeOfA; i++)
{
A[i] += A[i - 1];
}
// Find the position of box for each ball
for (int i = 0; i < sizeOfB; i++)
{
// Row number
int row = lower_bound(A, 0, A.Length, B[i]);
// Column (position of box in particular row)
int boxNumber = (row >= 1) ? B[i] - A[row - 1] : B[i];
// Row + 1 denotes row if indexing of array start from 1
Console.WriteLine(row + 1 + ", " + boxNumber + "\n");
}
}
private static int lower_bound(int[] a, int low,
int high, int element)
{
while (low < high)
{
int middle = low + (high - low) / 2;
if (element > a[middle])
{
low = middle + 1;
}
else
{
high = middle;
}
}
return low;
}
// Driver code
static public void Main ()
{
int []A = {2, 2, 2, 2};
int []B = {1, 2, 3, 4};
int sizeOfA = A.Length;
int sizeOfB = B.Length;
printPosition(A, B, sizeOfA, sizeOfB);
}
}
// This code has been contributed by Tushil.
PHP
$value)
{
if($valueTosearch <= $value)
return $row;
$row++;
}
return $row+1;
}
// Function to print the position of each boxes
// where a ball has to be placed
function printPosition($A, $B, $sizeOfA, $sizeOfB)
{
// Find the cumulative sum of array A[]
for ($i = 1; $i <$sizeOfA; $i++)
$A[$i] += $A[$i - 1];
// Find the position of box for each ball
for ($i = 0; $i < $sizeOfB; $i++)
{
// Row number
$row = lower_bound($A, $B[$i]) ;
// Column (position of box in particular row)
$boxNumber = ($row >= 1) ? $B[$i] - $A[$row - 1] : $B[$i];
// Row + 1 denotes row if indexing of array start from 1
print_r($row+1 .", ".$boxNumber);
echo "\n";
}
}
// Driver code
$A = array(2, 2, 2, 2 );
$B = array( 1, 2, 3, 4 );
$sizeOfA =count($A);
$sizeOfB = count($B);
printPosition($A, $B, $sizeOfA, $sizeOfB);
// This code is contributed by Shivam.Pradhan
?>
1, 1
1, 2
2, 1
2, 2