📌  相关文章
📜  执行K of Q查询以最大化数组元素的总和

📅  最后修改于: 2021-04-29 17:25:13             🧑  作者: Mango

给定一个由n个整数和一个整数K组成的数组arr [] 。还给出了具有两个数字LR的Q查询。对于每个查询,您都可以将索引范围[L,R]中数组的所有元素增加1 。任务是从Q个查询中准确选择K个查询,以使末尾的数组总和最大化。在执行K个此类查询后打印总和

例子:

幼稚的方法:幼稚的方法是使用动态编程和组合技术,其中我们从Q中选择任何K个查询。给出数组最大和的组合将是答案。
时间复杂度:O(N * N * K)

高效方法:由于我们需要在最后最大化数组的总和。我们只需要选择那些影响数组中元素最大数量(即范围更大)的查询即可。如果选择了每个查询,则会增加(R – L +1) 。执行此类查询后,数组元素的总和为(数组的初始总和+(K个查询的贡献))

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to perform K queries out
// of Q to maximize the final sum
int getFinalSum(int a[], int n, pair queries[],
                int q, int k)
{
    int answer = 0;
  
    // Get the initial sum
    // of the array
    for (int i = 0; i < n; i++)
        answer += a[i];
  
    vector contribution;
  
    // Stores the contriution of every query
    for (int i = 0; i < q; i++) {
        contribution.push_back(queries[i].second
                               - queries[i].first + 1);
    }
  
    // Sort the contribution of queries
    // in descending order
    sort(contribution.begin(), contribution.end(),
         greater());
  
    int i = 0;
  
    // Get the K most contributions
    while (i < k) {
        answer += contribution[i];
        i++;
    }
  
    return answer;
}
  
// Driver code
int main()
{
    int a[] = { 1, 1, 2, 2, 2, 3 };
    int n = sizeof(a) / sizeof(a[0]);
  
    pair queries[] = { { 0, 4 },
                                 { 1, 2 },
                                 { 2, 5 },
                                 { 2, 3 },
                                 { 2, 4 } };
    int q = sizeof(queries) / sizeof(queries[0]);
  
    int k = 3;
  
    cout << getFinalSum(a, n, queries, q, k);
  
    return 0;
}


Java
// Java implementation of the approach 
import java.util.*;
  
class GFG
{
  
//pair class
static class pair
{
    int first,second;
    pair(int f,int s)
    {
        first = f;
        second = s;
    }
}
  
// Function to perform K queries out 
// of Q to maximize the final sum 
static int getFinalSum(int a[], int n, pair queries[], 
                                    int q, int k) 
{ 
    int answer = 0; 
  
    // Get the initial sum 
    // of the array 
    for (int i = 0; i < n; i++) 
        answer += a[i]; 
  
    Vector contribution = new Vector(); 
  
    // Stores the contriution of every query 
    for (int i = 0; i < q; i++)
    { 
        contribution.add(queries[i].second 
                            - queries[i].first + 1); 
    } 
      
    //compartor 
    Comparator Comp = new Comparator()
    {
            public int compare(Integer e1,Integer e2)
            {
                if(e1 > e2)
                return -1;
                else
                return 1;
            }
        };
          
    // Sort the contribution of queries 
    // in descending order 
    Collections.sort(contribution,Comp); 
  
    int i = 0; 
  
    // Get the K most contributions 
    while (i < k) 
    { 
        answer += (int) contribution.get(i); 
        i++; 
    } 
  
    return answer; 
} 
  
// Driver code 
public static void main(String args[]) 
{ 
    int a[] = { 1, 1, 2, 2, 2, 3 }; 
    int n = a.length; 
  
    pair queries[] = new pair[5];
    queries[0] = new pair( 0, 4 );
    queries[1] = new pair( 1, 2 );
    queries[2] = new pair( 2, 5 );
    queries[3] = new pair( 2, 3 );
    queries[4] = new pair( 2, 4 ); 
    int q = queries.length; 
  
    int k = 3; 
    System.out.println( getFinalSum(a, n, queries, q, k)); 
} 
}
  
// This code is contributed by Arnab Kundu


Python3
# Python 3 implementation of the approach
  
# Function to perform K queries out
# of Q to maximize the final sum
def getFinalSum(a, n, queries, q, k):
    answer = 0
  
    # Get the initial sum
    # of the array
    for i in range(n):
        answer += a[i]
  
    contribution = []
  
    # Stores the contriution of every query
    for i in range(q):
        contribution.append(queries[i][1]- 
                            queries[i][0] + 1)
  
    # Sort the contribution of queries
    # in descending order
    contribution.sort(reverse = True)
  
    i = 0
  
    # Get the K most contributions
    while (i < k):
        answer += contribution[i]
        i += 1
  
    return answer
  
# Driver code
if __name__ == '__main__':
    a = [1, 1, 2, 2, 2, 3]
    n = len(a)
  
    queries = [[0, 4], [1, 2], 
               [2, 5], [2, 3],
               [2, 4]]
    q = len(queries);
  
    k = 3
  
    print(getFinalSum(a, n, queries, q, k))
  
# This code is contributed by
# Surendra_Gangwar


输出:
23