📌  相关文章
📜  查询以确定给定类型的甜食是否可以在给定的一天食用

📅  最后修改于: 2021-04-22 03:04:56             🧑  作者: Mango

给定两个阵列A []B []N个整数,其中,A i表示i型和B I的甜食的量的表示的i甜(较高的值时为优先级)的优先级,并整数K ,表示一天中可以食用的最大糖果数量。优先级较低的糖果不能先于优先级较高的糖果食用,并且一天只能食用一种类型的糖果。给定一个表示形式为{Q [i] [0],Q [i] [1]}的查询的数组Q [] [] ,每个查询的任务是查找类型Q [i] [ 0]可以在Q上被吃掉[I] [1]一天或没有。如果可能,打印“”。否则,打印“ No ”。

例子:

方法:解决此问题的想法是为每个甜蜜类型维护一个窗口。该窗口将基本上包含天数的上下限,可以在以下条件下食用该天数的糖果:

  • 对于窗口的下限,请考虑完成所有优先级较高的糖果所需的最少天数,即从每天类型的糖果中减去最小值(K,剩余量)
  • 对于窗口的上限,请考虑完成所有优先级较高的糖果的最长天数,即完成优先级较高的所有糖果总数的总和。

为每种类型准备好窗口后,只需检查每个查询,即可确定给定的日期是否在窗口内。请按照以下步骤解决问题:

脚步:

  • 维护一个容器,说成对v的向量,以存储每种甜食类型的优先级数量
  • 按优先级从高到低的顺序对向量对进行排序。
  • 初始化两个变量,例如lowerboundupperbound ,以存储每种甜蜜类型的窗口限制。
  • 遍历向量v并执行以下步骤:
    • 计算最大和天的最小数目需要吃甜i类型MAX_DAYS = A [i]MIN_DAYS =小区(A [I] / K)
    • 更新上限+ = max_days
    • 存储当前Sweet类型的窗口{lowerbound,upperbound}
    • 更新下限+ = min_days
  • 完成上述步骤后,遍历数组Q [] []并针对每个查询检查给定甜蜜类型的给定日期是否落在该甜蜜类型r的窗口{lowerbound,upperbound}内。如果发现是真的,则打印“是” 。否则,打印“否”

下面是上述方法的实现:

C++
// C++ program for the above aproach
 
#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)