第 Q 个人的最大杆长
给定数组a[]中 n 个杆的长度。如果任何人选择任何杆,最长杆的一半(或 (max + 1) / 2 )被分配,剩余部分 (max – 1) / 2 被放回。可以假设总是有足够数量的杆可用,回答数组 q[] 中给出的 M 个查询以找到第 i个人可用的最大杆长度,前提是q i是从 1 开始的有效人员编号。
例子 :
Input : a[] = {6, 5, 9, 10, 12}
q[] = {1, 3}
Output : 12 9
The first person gets maximum length as 12.
We remove 12 from array and put back (12 -1) / 2 = 5.
Second person gets maximum length as 10.
We put back (10 - 1)/2 which is 4.
Third person gets maximum length as 9.
Input : a[] = {6, 5, 9, 10, 12}
q[] = {3, 1, 2, 7, 4, 8, 9, 5, 10, 6}
Output : 9 12 10 5 6 4 3 6 3 5
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。
方法 :
使用堆栈和队列。首先对所有长度进行排序并将它们压入堆栈。现在,取栈顶元素,除以 2 并将剩余长度推入队列。现在,从下一个客户开始:
- 如果堆栈为空,则弹出前队列并推回队列。如果非零,则为一半(前 / 2)。
- 如果队列为空,则从堆栈中弹出并将其推入队列,如果非零,则为一半(顶部 / 2)。
- 如果两者都不为空,则比较 top 和 front,取较大者应弹出,除以 2,然后推回。
- 如果两者都为空,则商店为空!停在这里!
在上面的每个步骤中,将第 i个客户可用的长度存储在单独的数组中,比如“ans”。现在,通过给出 ans[Q i ] 作为输出开始回答查询。
以下是上述方法的实现:
C++
// CPP code to find the length of largest
// rod available for Q-th customer
#include
using namespace std;
// function to find largest length of
// rod available for Q-th customer
vector maxRodLength(int ar[],
int n, int m)
{
queue q;
// sort the rods according to lengths
sort(ar, ar + n);
// Push sorted elements to a stack
stack s;
for (int i = 0; i < n; i++)
s.push(ar[i]);
vector ans;
while (!s.empty() || !q.empty()) {
int val;
// If queue is empty -> pop from stack
// and push to queue it’s half(top/2),
// if non zero.
if (q.empty()) {
val = s.top();
ans.push_back(val);
s.pop();
val /= 2;
if (val)
q.push(val);
}
// If stack is empty -> pop front from
// queue and push back to queue it’s
// half(front/2), if non zero.
else if (s.empty()) {
val = q.front();
ans.push_back(val);
q.pop();
val /= 2;
if (val != 0)
q.push(val);
}
// If both are non empty ->
// compare top and front, whichsoever is
// larger should be popped, divided by 2
// and then pushed back.
else {
val = s.top();
int fr = q.front();
if (fr > val) {
ans.push_back(fr);
q.pop();
fr /= 2;
if (fr)
q.push(fr);
}
else {
ans.push_back(val);
s.pop();
val /= 2;
if (val)
q.push(val);
}
}
}
return ans;
}
// Driver code
int main()
{
// n : number of rods
// m : number of queries
int n = 5, m = 10;
int ar[n] = { 6, 5, 9, 10, 12 };
vector ans = maxRodLength(ar, n, m);
int query[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int size = sizeof(query) / sizeof(query[0]);
for (int i = 0; i < size; i++)
cout << ans[query[i] - 1] << " ";
return 0;
}
Java
// JAVA code to find the length of largest
// rod available for Q-th customer
import java.util.*;
class GFG
{
// function to find largest length of
// rod available for Q-th customer
static Vector maxRodLength(int ar[],
int n, int m)
{
Queue q = new LinkedList<>();
// sort the rods according to lengths
Arrays.sort(ar);
// Push sorted elements to a stack
Stack s = new Stack();
for (int i = 0; i < n; i++)
s.add(ar[i]);
Vector ans = new Vector();
while (!s.isEmpty() || !q.isEmpty())
{
int val;
// If queue is empty.pop from stack
// and push to queue its half(top/2),
// if non zero.
if (q.isEmpty())
{
val = s.peek();
ans.add(val);
s.pop();
val /= 2;
if (val > 0)
q.add(val);
}
// If stack is empty.pop front from
// queue and push back to queue its
// half(front/2), if non zero.
else if (s.isEmpty())
{
val = q.peek();
ans.add(val);
q.remove();
val /= 2;
if (val != 0)
q.add(val);
}
// If both are non empty .
// compare top and front, whichsoever is
// larger should be popped, divided by 2
// and then pushed back.
else
{
val = s.peek();
int fr = q.peek();
if (fr > val)
{
ans.add(fr);
q.remove();
fr /= 2;
if (fr > 0)
q.add(fr);
}
else
{
ans.add(val);
s.pop();
val /= 2;
if (val > 0)
q.add(val);
}
}
}
return ans;
}
// Driver code
public static void main(String[] args)
{
// n : number of rods
// m : number of queries
int n = 5, m = 10;
int []ar = { 6, 5, 9, 10, 12 };
Vector ans = maxRodLength(ar, n, m);
int query[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int size = query.length;
for (int i = 0; i < size; i++)
System.out.print(ans.get(query[i] - 1) + " ");
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 code to find the length of largest
# rod available for Q-th customer
# function to find largest length of
# rod available for Q-th customer
def maxRodLength(ar, n, m):
q = []
# sort the rods according to lengths
ar.sort()
# Push sorted elements to a stack
s = []
for i in range(n):
s.append(ar[i])
ans = []
while len(s) != 0 or len(q) != 0 :
# If queue is empty.pop from stack
# and push to queue its half(top/2),
# if non zero.
if len(q) == 0:
val = s[-1]
ans.append(val)
s.pop()
val = int(val / 2)
if (val > 0):
q.append(val)
# If stack is empty.pop front from
# queue and push back to queue its
# half(front/2), if non zero.
elif len(s) == 0:
val = q[0]
ans.append(val)
q.pop(0)
val = int(val / 2)
if (val != 0):
q.append(val)
# If both are non empty .
# compare top and front, whichsoever is
# larger should be popped, divided by 2
# and then pushed back.
else:
val = s[-1]
fr = q[0]
if (fr > val):
ans.append(fr)
q.pop(0)
fr = int(fr / 2)
if (fr > 0):
q.append(fr)
else:
ans.append(val)
s.pop()
val = int(val / 2)
if (val > 0):
q.append(val)
return ans
# n : number of rods
# m : number of queries
n, m = 5, 10
ar = [ 6, 5, 9, 10, 12 ]
ans = maxRodLength(ar, n, m)
query = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
size = len(query)
for i in range(size):
print(ans[query[i] - 1], end = " ")
# This code is contributed by decode2207.
C#
// C# code to find the length of largest
// rod available for Q-th customer
using System;
using System.Collections.Generic;
class GFG {
// function to find largest length of
// rod available for Q-th customer
static List maxRodLength(int[] ar, int n, int m)
{
Queue q = new Queue();
// sort the rods according to lengths
Array.Sort(ar);
// Push sorted elements to a stack
Stack s = new Stack();
for (int i = 0; i < n; i++)
s.Push(ar[i]);
List ans = new List();
while (s.Count > 0 || q.Count > 0)
{
int val;
// If queue is empty.pop from stack
// and push to queue its half(top/2),
// if non zero.
if (q.Count == 0)
{
val = s.Peek();
ans.Add(val);
s.Pop();
val /= 2;
if (val > 0)
q.Enqueue(val);
}
// If stack is empty.pop front from
// queue and push back to queue its
// half(front/2), if non zero.
else if (s.Count == 0)
{
val = q.Peek();
ans.Add(val);
q.Dequeue();
val /= 2;
if (val != 0)
q.Enqueue(val);
}
// If both are non empty .
// compare top and front, whichsoever is
// larger should be popped, divided by 2
// and then pushed back.
else
{
val = s.Peek();
int fr = q.Peek();
if (fr > val)
{
ans.Add(fr);
q.Dequeue();
fr /= 2;
if (fr > 0)
q.Enqueue(fr);
}
else
{
ans.Add(val);
s.Pop();
val /= 2;
if (val > 0)
q.Enqueue(val);
}
}
}
return ans;
}
// Driver code
static void Main()
{
// n : number of rods
// m : number of queries
int n = 5, m = 10;
int[] ar = { 6, 5, 9, 10, 12 };
List ans = maxRodLength(ar, n, m);
int[] query = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int size = query.Length;
for (int i = 0; i < size; i++)
{
Console.Write(ans[query[i] - 1] + " ");
}
}
}
// This code is contributed by divyesh072019
Javascript
输出:
12 10 9 6 6 5 5 4 3 3
时间复杂度: O(N log(N))