给定一个列表S ,该列表最初包含单个值0 。以下是以下类型的Q查询:
- 0 X :在列表中插入X
- 1 X :对于S中的每个元素A,将其替换为A XORX。
任务是在执行给定的Q查询之后,以升序打印列表中的所有元素。
例子:
Input: Q[][] = { {0, 6}, {0, 3}, {0, 2}, {1, 4}, {1, 5} }
Output: 1 2 3 7
Explanation:
[0] (initial value)
[0 6] (add 6 to list)
[0 6 3] (add 3 to list)
[0 6 3 2] (add 2 to list)
[4 2 7 6] (XOR each element by 4)
[1 7 2 3] (XOR each element by 5)
Thus sorted order after performing queries is [1 2 3 7]
Input: Q[][]= {{0, 2}, {1, 3}, {0, 5} }
Output: 1 3 5
Explanation:
[0] (initial value)
[0 2] (add 2 to list)
[3 1] (XOR each element by 3)
[3 1 5] (add 5 to list)
Thus sorted order after performing queries is [1 3 5]
天真的方法:解决问题的最简单方法是:
- 用0初始化一个列表,然后遍历每个查询并执行以下操作:
- 对于查询类型0,请在列表中添加给定值。
- 对于查询类型1遍历列表,并使用value将每个元素各自的按位XOR更新。
- 执行完所有查询后,按升序打印最终列表。
时间复杂度: O(N * Q),其中N是列表的长度,Q是查询的数量
辅助空间: O(1)
高效的方法:通过跟踪从右到左的累积按位XOR运算,以相反的顺序处理数字要容易得多。请按照以下步骤解决问题:
- 用0初始化变量xor 。
- 从右到左遍历查询数组,在查询类型为0时将xor ^ value添加到列表中,否则用xor ^ value更新xor。
- 遍历查询后,将xor添加到列表中并对最终列表进行排序。
- 完成上述操作后,打印最终列表。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
#define N 5
#define M 2
// Function to return required list
// after performing all the queries
list ConstructList(int Q[N][M])
{
// Store cumulative Bitwise XOR
int xr = 0;
// Initialize final list to return
list ans;
// Perform each query
for (int i = N - 1; i >= 0; i--)
{
if (Q[i][0] == 0)
ans.push_back(Q[i][1] ^ xr);
else
xr ^= Q[i][1];
}
// The initial value of 0
ans.push_back(xr);
// Sort the list
ans.sort();
// Return final list
return ans;
}
// Driver Code
int main()
{
// Given Queries
int Q[N][M] = {{ 0, 6 }, { 0, 3 },
{ 0, 2 }, { 1, 4 },
{ 1, 5 }};
// Function Call
list ans = ConstructList(Q);
for (auto it = ans.begin(); it != ans.end(); ++it)
cout << ' ' << *it;
}
// This code is contributed by gauravrajput1
Java
// Java program for the above approach
import java.util.*;
class GFG {
// Function to return required list
// after performing all the queries
static List ConstructList(int[][] Q)
{
// Store cumulative Bitwise XOR
int xor = 0;
// Initialize final list to return
List ans = new ArrayList<>();
// Perform each query
for (int i = Q.length - 1; i >= 0; i--) {
if (Q[i][0] == 0)
ans.add(Q[i][1] ^ xor);
else
xor ^= Q[i][1];
}
// The initial value of 0
ans.add(xor);
// Sort the list
Collections.sort(ans);
// Return final list
return ans;
}
// Driver Code
public static void main(String[] args)
{
// Given Queries
int[][] Q = {
{ 0, 6 }, { 0, 3 }, { 0, 2 },
{ 1, 4 }, { 1, 5 }
};
// Function Call
System.out.println(ConstructList(Q));
}
}
Python3
# Python3 program for the above approach
# Function to return required list
# after performing all the queries
def ConstructList(Q):
# Store cumulative Bitwise XOR
xor = 0
# Initialize final list to return
ans = []
# Perform each query
for i in range(len(Q) - 1, -1, -1):
if(Q[i][0] == 0):
ans.append(Q[i][1] ^ xor)
else:
xor ^= Q[i][1]
# The initial value of 0
ans.append(xor)
# Sort the list
ans.sort()
# Return final list
return ans
# Driver Code
# Given Queries
Q = [ [0, 6], [0, 3],
[0, 2], [1, 4],
[1, 5] ]
# Function call
print(ConstructList(Q))
# This code is contributed by Shivam Singh
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to return required list
// after performing all the queries
static List ConstructList(int[,] Q)
{
// Store cumulative Bitwise XOR
int xor = 0;
// Initialize readonly list to return
List ans = new List();
// Perform each query
for (int i = Q.GetLength(0) - 1; i >= 0; i--)
{
if (Q[i, 0] == 0)
ans.Add(Q[i, 1] ^ xor);
else
xor ^= Q[i, 1];
}
// The initial value of 0
ans.Add(xor);
// Sort the list
ans.Sort();
// Return readonly list
return ans;
}
// Driver Code
public static void Main(String[] args)
{
// Given Queries
int[,] Q = {{ 0, 6 }, { 0, 3 }, { 0, 2 },
{ 1, 4 }, { 1, 5 }};
// Function Call
foreach( int i in ConstructList(Q))
Console.Write(i + ", ");
}
}
// This code is contributed by Princi Singh
[1, 2, 3, 7]
时间复杂度: O(Q * log(Q)) ,其中Q是查询数。
辅助空间: O(N) ,其中N是结果列表中元素的数量。