📜  在双音序列中插入元素的查询

📅  最后修改于: 2022-05-13 01:57:47.932000             🧑  作者: Mango

在双音序列中插入元素的查询

给定一个双音序列“S”“Q”没有。的查询。每个查询包含一个整数x i , 1 <= i <= Q。任务是在为每个查询插入整数后打印双音序列的长度。此外,在所有查询之后打印双音序列。
例子:

方法:这个想法是制作两组,一组用于递增序列,另一组用于递减序列。

  1. 现在,对于每个查询,首先检查 x i是否大于双音序列中的最大元素。
  2. 如果是,则更新最大序列并将该元素包含在集合中以增加序列。
  3. 如果不是,则检查该元素是否存在于递增序列集中,如果不包含在递增序列集中,则将其包含在递减序列集中。

下面是这种方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
 
// Function to find the bitonic sequence
void solveQuery(int a[], int n, int x[], int q)
{
    int maxx = INT_MIN;
 
    // Finding the maximum element
    for (int i = 0; i < n; i++)
        maxx = max(maxx, a[i]);
 
    set s1, s2;
 
    s1.insert(a[0]);
    s2.insert(a[n - 1]);
 
    // set to include increasing sequence element
    for (int i = 1; i < n; i++)
        if (a[i] > a[i - 1])
            s1.insert(a[i]);
 
    // set to include decreasing sequence element
    for (int i = n - 2; i >= 0; i--)
        if (a[i] > a[i + 1])
            s2.insert(a[i]);
 
    // removing maximum element from
    // decreasing sequence set
    s2.erase(s2.find(maxx));
 
    // for each query
    for (int i = 0; i < q; i++) {
 
        // checking if x is greater than
        // maximum element or not.
        if (maxx <= x[i]) {
            maxx = x[i];
            s1.insert(x[i]);
        }
 
        else {
 
            // checking if x lie in increasing sequence
            if (s1.find(x[i]) == s1.end())
                s1.insert(x[i]);
 
            // else insert into decreasing sequence set
            else
                s2.insert(x[i]);
        }
 
        // finding the length
        int ans = s1.size() + s2.size();
        cout << ans << "\n";
    }
 
    // printing the sequence
    set::iterator it;
    for (it = s1.begin(); it != s1.end(); it++)
        cout << (*it) << " ";
 
    set::reverse_iterator rit;
 
    for (rit = s2.rbegin(); rit != s2.rend(); rit++)
        cout << (*rit) << " ";
}
 
// Driver code
int main()
{
    int a[] = { 1, 2, 5, 2 };
    int n = sizeof(a) / sizeof(a[0]);
    int x[] = { 5, 1, 3, 2 };
    int q = sizeof(x) / sizeof(x[0]);
 
    solveQuery(a, n, x, q);
    return 0;
}


Java
// Java implementation of above approach
import java.util.*;
 
class GFG
{
 
// Function to find the bitonic sequence
static void solveQuery(int a[], int n, int x[], int q)
{
    int maxx = Integer.MIN_VALUE;
 
    // Finding the maximum element
    for (int i = 0; i < n; i++)
        maxx = Math.max(maxx, a[i]);
 
    Set s1 = new HashSet<>(), s2 = new HashSet<>();
 
    s1.add(a[0]);
    s2.add(a[n - 1]);
 
    // set to include increasing sequence element
    for (int i = 1; i < n; i++)
        if (a[i] > a[i - 1])
            s1.add(a[i]);
 
    // set to include decreasing sequence element
    for (int i = n - 2; i >= 0; i--)
        if (a[i] > a[i + 1])
            s2.add(a[i]);
 
    // removing maximum element from
    // decreasing sequence set
    s2.remove(maxx);
 
    // for each query
    for (int i = 0; i < q; i++)
    {
 
        // checking if x is greater than
        // maximum element or not.
        if (maxx <= x[i])
        {
            maxx = x[i];
            s1.add(x[i]);
        }
 
        else
        {
 
            // checking if x lie in increasing sequence
            if (!s1.contains(x[i]))
                s1.add(x[i]);
 
            // else insert into decreasing sequence set
            else
                s2.add(x[i]);
        }
 
        // finding the length
        int ans = s1.size() + s2.size();
        System.out.print(ans + "\n");
    }
 
    // printing the sequence
    for (Integer it : s1)
        System.out.print((it) + " ");
 
    for (Integer it : s2)
        System.out.print((it) + " ");
}
 
// Driver code
public static void main(String[] args)
{
    int a[] = { 1, 2, 5, 2 };
    int n = a.length;
    int x[] = { 5, 1, 3, 2 };
    int q = x.length;
 
    solveQuery(a, n, x, q);
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation of above approach
import sys
 
# Function to find the bitonic sequence
def solveQuery(a, n, x, q):
    maxx = -sys.maxsize
 
    # Finding the maximum element
    for i in range(n):
        maxx = max(maxx, a[i])
 
    s1, s2 = set(), set()
 
    s1.add(a[0])
    s2.add(a[n - 1])
 
    # set to include increasing sequence element
    for i in range(1, n):
        if a[i] > a[i - 1]:
            s1.add(a[i])
 
    # set to include decreasing sequence element
    for i in range(n - 2, -1, -1):
        if a[i] > a[i + 1]:
            s2.add(a[i])
 
    # removing maximum element from
    # decreasing sequence set
    s2.remove(maxx)
 
    # for each query
    for i in range(q):
 
        # checking if x is greater than
        # maximum element or not.
        if maxx <= x[i]:
            maxx = x[i]
            s1.add(x[i])
        else:
 
            # checking if x lie in increasing sequence
            if x[i] not in s1:
                s1.add(x[i])
 
            # else insert into decreasing sequence set
            else:
                s2.add(x[i])
 
        # finding the length
        ans = len(s1) + len(s2)
        print(ans)
 
    # printing the sequence
    for i in s1:
        print(i, end = " ")
 
    for i in reversed(list(s2)):
        print(i, end = " ")
 
# Driver Code
if __name__ == "__main__":
 
    a = [1, 2, 5, 2]
    n = len(a)
    x = [5, 1, 3, 2]
    q = len(x)
 
    solveQuery(a, n, x, q)
 
# This code is contributed by
# sanjeev2552


C#
// C# implementation of above approach
using System;
using System.Collections.Generic;
 
class GFG
{
 
// Function to find the bitonic sequence
static void solveQuery(int []a, int n, int []x, int q)
{
    int maxx = int.MinValue;
 
    // Finding the maximum element
    for (int i = 0; i < n; i++)
        maxx = Math.Max(maxx, a[i]);
 
    HashSet s1 = new HashSet(), s2 = new HashSet();
 
    s1.Add(a[0]);
    s2.Add(a[n - 1]);
 
    // set to include increasing sequence element
    for (int i = 1; i < n; i++)
        if (a[i] > a[i - 1])
            s1.Add(a[i]);
 
    // set to include decreasing sequence element
    for (int i = n - 2; i >= 0; i--)
        if (a[i] > a[i + 1])
            s2.Add(a[i]);
 
    // removing maximum element from
    // decreasing sequence set
    s2.Remove(maxx);
 
    // for each query
    for (int i = 0; i < q; i++)
    {
 
        // checking if x is greater than
        // maximum element or not.
        if (maxx <= x[i])
        {
            maxx = x[i];
            s1.Add(x[i]);
        }
 
        else
        {
 
            // checking if x lie in increasing sequence
            if (!s1.Contains(x[i]))
                s1.Add(x[i]);
 
            // else insert into decreasing sequence set
            else
                s2.Add(x[i]);
        }
 
        // finding the length
        int ans = s1.Count + s2.Count;
        Console.Write(ans + "\n");
    }
 
    // printing the sequence
    foreach (int it in s1)
        Console.Write((it) + " ");
 
    foreach (int it in s2)
        Console.Write((it) + " ");
}
 
// Driver code
public static void Main(String[] args)
{
    int []a = { 1, 2, 5, 2 };
    int n = a.Length;
    int []x = { 5, 1, 3, 2 };
    int q = x.Length;
 
    solveQuery(a, n, x, q);
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
4
5
6
6
1 2 3 5 2 1