给定两个阵列A []和B []由N个整数,其中,A i表示第i个型和B I的甜食的量的表示的第i个甜(较高的值时为优先级)的优先级,并一个整数K ,表示一天可以吃的最大糖果数量。优先级较低的糖果不能在优先级较高的糖果之前吃,而且一天只能吃一种糖果。给定一个数组Q[][]表示形式为{Q[i][0], Q[i][1] }的查询,每个查询的任务是找出Q[i][ 0]可以在Q上被吃掉[I] [1]第一天或没有。如果可能,请打印“是”。否则,打印“否”。
例子:
Input: A[] = { 6, 3, 7, 5, 2 }, B[] = { 1, 2, 3, 4, 5 }, K = 3, Q[][] = { {4, 4}, {3, 16}, {2, 7} }
Output: Yes No Yes
Explanation:
Query 1: By eating sweets in the following order, sweets of the fourth type can be eaten on the fourth day.
Day 1: Type 5 -> 2 units
Day 2: Type 4 -> 2 units
Day 3: Type 4 -> 2 units
Day 4: Type 4 -> 1 unit
Query 2: Total sweets of type 5, 4, 3 = 2 + 5 + 7 = 14. Therefore, even after eating one sweet each day won’t leave any sweet of type 3 for Day 16.
Query 3: By eating sweets in the following order, sweets of the 2nd type can be eaten on the 7th day.
Day 1: Type 5 -> 2 units.
Day 2: Type 4 -> 3 units.
Day 3: Type 4 -> 2 units.
Day 4: Type 3 -> 3 units.
Day 5: Type 3 -> 2 units.
Day 6: Type 3 -> 2 units.
Day 7: Type 2 -> 2units.
Input: A[] = { 5, 2, 6, 4, 1 }, B[] = { 2, 1, 3, 5, 4 }, K = 4, Q[][] = { {2, 17}, {5, 6} }
Output: Yes No
方法:解决这个问题的思路是为每个sweet类型维护一个窗口。该窗口将基本上包含天数的下限和上限,在该天数内,可以使用以下条件食用该类型的糖果:
- 对于窗口的下限,考虑完成所有较高优先级的糖果所需的最少天数,即从每天的一种糖果中减去最少(K,剩余数量) 。
- 对于窗口的上限,考虑完成所有高优先级糖果的最大天数,即所有高优先级糖果总数的总和。
一旦为每种类型准备了窗口,只需检查每个查询给定的日期是否在窗口内。请按照以下步骤解决问题:
脚步:
- 维护一个容器,比如成对的向量v来存储每个甜型的优先级和数量。
- 按优先级的降序对成对的向量进行排序。
- 初始化两个变量,比如lowerbound和upperbound ,以存储每个 Sweet-type 的窗口限制。
- 遍历向量v并执行以下步骤:
- 计算最大和天的最小数目需要吃甜第i个类型MAX_DAYS = A [i]和MIN_DAYS =小区(A [I] / K)的。
- 更新上限 += max_days 。
- 存储当前甜蜜类型的窗口{lowerbound,upperbound} 。
- 更新下限 += min_days 。
- 完成上述步骤后,遍历数组Q[][]并针对每个查询检查给定甜食类型的给定日期是否落在该甜食类型 r 的窗口{lowerbound,upperbound}内。如果发现是真的,打印“是” 。否则,打印“否” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find queries to check
// if a sweet of given type can be
// eaten on a given day or not
void sweetTypeOnGivenDay(int a[], int b[],
int n, int k,
vector >& q)
{
// Stores pairs {priority, quantity}
// of each sweet-type
vector > > v;
for (int i = 0; i < n; i++)
v.push_back({ b[i], { a[i], i + 1 } });
// Sorting the order of sweets in
// decreasing order of their priority
sort(v.begin(), v.end(),
greater > >());
// Stores the window {min_days, max_days}
// for each sweet-type
map > mp;
// Variables to calculate the windows
int lowerbound = 0, upperbound = 0;
// Traverse the array
for (int i = 0; i < n; i++) {
// Calculating maximum and minimum
// number of days required to complete
// the sweets of the current type
int maxi_days = v[i].second.first;
int mini_days = v[i].second.first / k;
if (v[i].second.first % k != 0)
mini_days++;
// Creating the window and storing it
upperbound += maxi_days;
mp[v[i].second.second]
= { lowerbound, upperbound };
lowerbound += mini_days;
}
// Traversing the queries
for (int i = 0; i < q.size(); i++) {
// x: Type of sweet, y: Day
int x = q[i].first, y = q[i].second;
// Find the window for the
// sweet of type x
int e = mp[x].first;
int f = mp[x].second;
// If the given day lies
// within the window
if (y >= e && y <= f)
cout << "Yes"
<< " ";
else
cout << "No"
<< " ";
}
}
// Driver Code
int main()
{
// Quantites of sweets of each type
int A[] = { 6, 3, 7, 5, 2 };
// Priorites of each type sweet
int B[] = { 1, 2, 3, 4, 5 };
// Maximum sweet of one type
// that can be eaten on a day
int K = 3;
// Queries
vector > Queries
= { { 4, 4 }, { 3, 16 }, { 2, 7 } };
// Calculating number of types
int n = sizeof(A) / sizeof(A[0]);
sweetTypeOnGivenDay(A, B, n, K, Queries);
}
Java
// Java implementation of the above approach
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
class GFG
{
static class Pair
{
K first;
V second;
public Pair(K first, V second)
{
this.first = first;
this.second = second;
}
public static Pair of(K first, V second)
{
return new Pair<>(first, second);
}
}
// Function to find queries to check
// if a sweet of given type can be
// eaten on a given day or not
static void sweetTypeOnGivenDay(int a[], int b[],
int n, int k,
ArrayList> q)
{
// Stores pairs {priority, quantity}
// of each sweet-type
ArrayList>> v = new ArrayList<>();
for (int i = 0; i < n; i++)
v.add(Pair.of(b[i], Pair.of(a[i], i + 1)));
// Sorting the order of sweets in
// decreasing order of their priority
Collections.sort(v, new Comparator>>() {
@Override
public int compare(Pair> a, Pair> b) {
if (a.first == b.first) {
if (a.second.first == b.second.first) {
return b.second.second - a.second.second;
}
return b.second.first - a.second.first;
}
return b.first - a.first;
}
});
// Stores the window {min_days, max_days}
// for each sweet-type
Map> mp = new HashMap<>();
// Variables to calculate the windows
int lowerbound = 0, upperbound = 0;
// Traverse the array
for (int i = 0; i < n; i++) {
// Calculating maximum and minimum
// number of days required to complete
// the sweets of the current type
int maxi_days = v.get(i).second.first;
int mini_days = v.get(i).second.first / k;
if (v.get(i).second.first % k != 0)
mini_days++;
// Creating the window and storing it
upperbound += maxi_days;
mp.put(v.get(i).second.second, Pair.of(lowerbound, upperbound));
lowerbound += mini_days;
}
// Traversing the queries
for (int i = 0; i < q.size(); i++)
{
// x: Type of sweet, y: Day
int x = q.get(i).first, y = q.get(i).second;
// Find the window for the
// sweet of type x
int e = mp.get(x).first;
int f = mp.get(x).second;
// If the given day lies
// within the window
if (y >= e && y <= f)
System.out.println("Yes ");
else
System.out.println("No ");
}
}
// Driver Code
public static void main(String[] args)
{
// Quantites of sweets of each type
int A[] = { 6, 3, 7, 5, 2 };
// Priorites of each type sweet
int B[] = { 1, 2, 3, 4, 5 };
// Maximum sweet of one type
// that can be eaten on a day
int K = 3;
// Queries
ArrayList> Queries = new ArrayList<>(
Arrays.asList(Pair.of(4, 4), Pair.of(3, 16), Pair.of(2, 7)));
// Calculating number of types
int n = A.length;
sweetTypeOnGivenDay(A, B, n, K, Queries);
}
}
// This code is contributed by sanjeev2552
Yes No Yes
时间复杂度: O(N logN)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live