给定一个大小为N的数组arr[]和Q 个查询,其中每个查询包含两个整数X和Y ,任务是在执行每个Q查询后找到数组的总和,以便对于每个查询,数组arr[ 中的元素]值X更新为Y 。每次查询后求数组的总和。
例子:
Input: arr[] ={1, 2, 3, 4}, Q = {(1, 2), (3, 4), (2, 4)}
Output: 11 12 16
Explanation:
1st operation is to replace each 1 with 2
So array becomes ar[ ] ={2, 2, 3, 4} ans sum = 11
2nd operation is to replace each 3 with 4
So array becomes ar[ ] ={2, 2, 4, 4} ans sum = 12
3rd operation is to replace each 2 with 4
So array becomes ar[ ] ={4, 4, 4, 4} ans sum = 16
Input: arr[] = {1}, Q = {(1, 2)}
Output: 2
朴素的方法:朴素的方法是遍历每个查询,并通过遍历数组为每个查询更新数组arr[] 中每个元素的值X 到 Y。在执行每个查询后打印arr[]中所有元素的总和。
时间复杂度: O(N*Q)
辅助空间: O(1)
有效的方法:为了优化上述方法,其思想是计算数组中所有元素的总和(比如sum ) arr[]并将所有元素的频率存储在 unordered_map(比如M )中。对于每个查询(X, Y),请执行以下操作:
- 从 unordered_map M 中找出X的频率。
- 将总和减少X*M[X] ,以排除X的总和。
- 将 sum 增加Y*M[X] ,用于排除Y的总和。
- 将Y在地图中的频率增加M[X] 。
- 最后,打印总和并从映射M 中删除X。
下面是上述方法的实现
C++14
// C++ program for the above approach
#include
using namespace std;
// Function that solves the given queries
void solve(int ar[], int n, int b[],
int c[], int q)
{
// This map will store the
// frequency of each element
unordered_map mp;
// sum stores the sum of
// all elements of array
int sum = 0;
for (int x = 0; x < n; x++) {
sum += ar[x];
mp[ar[x]]++;
}
// Process the queries
for (int x = 0; x < q; x++) {
// Find occurrence of
// b[x] from map
int occur1 = mp[b[x]];
// Decrease sum
sum = sum - occur1 * b[x];
// Erase b[x] from map
mp.erase(b[x]);
// Increase sum
sum = sum + occur1 * c[x];
// Increase frequency
// of c[x] in map
mp] += occur1;
// Print sum
cout << sum << " ";
}
}
// Driver Code
int main()
{
// Given arr[]
int ar[] = { 1, 2, 3, 4 };
int n = 4;
// Given Queries
int q = 3;
int b[] = { 1, 3, 2 };
int c[] = { 2, 4, 4 };
// Function Call
solve(ar, n, b, c, q);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function that solves the given queries
static void solve(int ar[], int n, int b[],
int c[], int q)
{
// This map will store the
// frequency of each element
Map mp = new HashMap<>();
// sum stores the sum of
// all elements of array
int sum = 0;
for(int x = 0; x < n; x++)
{
sum += ar[x];
mp.put(ar[x], mp.getOrDefault(ar[x], 0) + 1);
}
// Process the queries
for(int x = 0; x < q; x++)
{
// Find occurrence of
// b[x] from map
int occur1 = mp.get(b[x]);
// Decrease sum
sum = sum - occur1 * b[x];
// Erase b[x] from map
mp.remove(b[x]);
// Increase sum
sum = sum + occur1 * c[x];
// Increase frequency
// of c[x] in map
mp.put(c[x], mp.get(c[x]) + occur1);
// Print sum
System.out.print(sum + " ");
}
}
// Driver Code
public static void main (String[] args)
{
// Given arr[]
int ar[] = { 1, 2, 3, 4 };
int n = 4;
// Given queries
int q = 3;
int b[] = { 1, 3, 2 };
int c[] = { 2, 4, 4 };
// Function call
solve(ar, n, b, c, q);
}
}
// This code is contributed by offbeat
Python3
# Python3 program for the above approach
# Function that solves the given queries
def solve(ar, n, b, c, q):
# This map will store the
# frequency of each element
mp = {}
# sum stores the sum of
# all elements of array
sum = 0
for x in range(n):
sum += ar[x]
mp[ar[x]] = mp.get(ar[x], 0) + 1
# Process the queries
for x in range(q):
# Find occurrence of
# b[x] from map
occur1 = mp[b[x]]
# Decrease sum
sum = sum - occur1 * b[x]
# Erase b[x] from map
del mp[b[x]]
# Increase sum
sum = sum + occur1 * c[x]
# Increase frequency
# of c[x] in map
mp] += occur1
# Print sum
print(sum, end = " ")
# Driver Code
if __name__ == '__main__':
# Given arr[]
ar = [ 1, 2, 3, 4 ]
n = 4
# Given Queries
q = 3
b = [ 1, 3, 2 ]
c = [ 2, 4, 4 ]
# Function Call
solve(ar, n, b, c, q)
# This code is contributed by mohit kumar 29
C#
// C# program for
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function that solves
// the given queries
static void solve(int []ar, int n,
int []b, int []c,
int q)
{
// This map will store the
// frequency of each element
Dictionary mp = new Dictionary();
// sum stores the sum of
// all elements of array
int sum = 0;
for(int x = 0; x < n; x++)
{
sum += ar[x];
if(mp.ContainsKey(ar[x]))
mp[ar[x]] = mp[ar[x]] + 1;
else
mp.Add(ar[x], 1);
}
// Process the queries
for(int x = 0; x < q; x++)
{
// Find occurrence of
// b[x] from map
int occur1 = mp[b[x]];
// Decrease sum
sum = sum - occur1 * b[x];
// Erase b[x] from map
mp.Remove(b[x]);
// Increase sum
sum = sum + occur1 * c[x];
// Increase frequency
// of c[x] in map
if(mp.ContainsKey(c[x]))
mp] = mp] + occur1;
// Print sum
Console.Write(sum + " ");
}
}
// Driver Code
public static void Main(String[] args)
{
// Given []arr
int []ar = {1, 2, 3, 4};
int n = 4;
// Given queries
int q = 3;
int []b = {1, 3, 2};
int []c = {2, 4, 4};
// Function call
solve(ar, n, b, c, q);
}
}
// This code is contributed by Princi Singh
Javascript
11 12 16
时间复杂度: O(N + Q)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live