📌  相关文章
📜  在不改变顺序的情况下将数组分成递增和递减子序列

📅  最后修改于: 2021-10-26 05:11:30             🧑  作者: Mango

给定一个由两个合并的序列组成的合并序列,其中一个严格递增,另一个严格递减。递增序列的元素插入递减序列的元素之间,而不改变顺序。

给定一个合并序列,任务是找到任何两个合适的初始序列,其中一个应该严格递增,另一个应该严格递减。
:空序列和由一个元素组成的序列可视为递增或递减。
例子:

方法1:我们可以修改最长递增序列)并解决所需的问题。这将需要 O(nlogn) 时间。

方法二:我们也可以只在一次遍历中解决这个问题。这里使用的想法是维护两个已排序的数组。
对于新元素x

  • 如果它只能附加到数组之一,则附加它。
  • 如果它不能附加到任何一个,那么答案是-1
  • 如果它可以附加到两个元素,则检查下一个元素y ,如果y > x则将x附加到增加的元素,否则将x附加到减少的元素。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to print strictly increasing and
// strictly decreasing sequence if possible
void Find_Sequence(int arr[], int n)
{
    // Arrays to store strictly increasing and
    // decreasing sequence
    vector inc_arr, dec_arr;
 
    // Initializing last element of both sequence
    int flag = 0;
    long inc = -1, dec = 1e7;
 
    // Iterating through the array
    for (int i = 0; i < n; i++)
    {
        // If current element can be appended
        // to both the sequences
        if (inc < arr[i] && arr[i] < dec)
        {
            // If next element is greater than
            // the current element
            // Then append it to the strictly
            // increasing array
            if (arr[i] < arr[i + 1])
            {
                inc = arr[i];
                inc_arr.emplace_back(arr[i]);
            }
 
            // Otherwise append it to the
            // strictly decreasing array
            else
            {
                dec = arr[i];
                dec_arr.emplace_back(arr[i]);
            }
        }
         
        // If current element can be appended
        // to the increasing sequence only
        else if (inc < arr[i])
        {
            inc = arr[i];
            inc_arr.emplace_back(arr[i]);
        }
         
        // If current element can be appended
        // to the decreasing sequence only
        else if (dec > arr[i])
        {
            dec = arr[i];
            dec_arr.emplace_back(arr[i]);
        }
         
        // Else we can not make such sequences
        // from the given array
        else
        {
            cout << -1 << endl;
            flag = 1;
            break;
        }
    }
     
    // Print the required sequences
    if (!flag)
    {
        for (auto i = inc_arr.begin();
                  i != inc_arr.end(); i++)
            cout << *i << " ";
        cout << endl;
 
        for (auto i = dec_arr.begin();
                  i != dec_arr.end(); i++)
            cout << *i << " ";
        cout << endl;
    }
}
 
// Driver code
int main()
{
    int arr[] = { 5, 1, 3, 6, 8, 2, 9, 0, 10 };
    int n = sizeof(arr) / sizeof(arr[0]);
    Find_Sequence(arr, n);
}
 
// This code is contributed by sanjeev2552


Java
// Java implementation of the approach
import java.util.*;
 
class GFG
{
 
    // Function to print strictly increasing and
    // strictly decreasing sequence if possible
    static void Find_Sequence(int[] arr, int n)
    {
 
        // Arrays to store strictly increasing and
        // decreasing sequence
        Vector inc_arr = new Vector<>(),
                        dec_arr = new Vector<>();
 
        // Initializing last element of both sequence
        int flag = 0;
        long inc = -1, dec = (long) 1e7;
 
        // Iterating through the array
        for (int i = 0; i < n; i++)
        {
 
            // If current element can be appended
            // to both the sequences
            if (inc < arr[i] && arr[i] < dec)
            {
 
                // If next element is greater than
                // the current element
                // Then append it to the strictly
                // increasing array
                if (arr[i] < arr[i + 1])
                {
                    inc = arr[i];
                    inc_arr.add(arr[i]);
                }
 
                // Otherwise append it to the
                // strictly decreasing array
                else
                {
                    dec = arr[i];
                    dec_arr.add(arr[i]);
                }
            }
 
            // If current element can be appended
            // to the increasing sequence only
            else if (inc < arr[i])
            {
                inc = arr[i];
                inc_arr.add(arr[i]);
            }
 
            // If current element can be appended
            // to the decreasing sequence only
            else if (dec > arr[i])
            {
                dec = arr[i];
                dec_arr.add(arr[i]);
            }
 
            // Else we can not make such sequences
            // from the given array
            else
            {
                System.out.println(-1);
                flag = 1;
                break;
            }
        }
 
        // Print the required sequences
        if (flag == 0)
        {
            for (int i : inc_arr)
                System.out.print(i + " ");
            System.out.println();
 
            for (int i : dec_arr)
                System.out.print(i + " ");
            System.out.println();
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int[] arr = { 5, 1, 3, 6, 8, 2, 9, 0, 10 };
        int n = arr.length;
        Find_Sequence(arr, n);
    }
}
 
// This code is contributed by
// sanjeev2552


Python3
# Python3 implementation of the approach
 
# Function to print strictly increasing and
# strictly decreasing sequence if possible
def Find_Sequence(array, n):
 
    # Arrays to store strictly increasing and
    # decreasing sequence
    inc_arr, dec_arr =[], []
 
    # Initializing last element of both sequence
    inc, dec = -1, 1e7
 
    # Iterating through the array
    for i in range(n):
 
        # If current element can be appended
        # to both the sequences
        if inc < array[i] < dec:
 
            # If next element is greater than
            # the current element
            # Then append it to the strictly
            # increasing array
            if array[i] < array[i + 1]:
                inc = array[i]
                inc_arr.append(array[i])
 
            # Otherwise append it to the
            # strictly decreasing array
            else:
                dec = array[i]
                dec_arr.append(array[i])
 
        # If current element can be appended
        # to the increasing sequence only
        elif inc < array[i]:
            inc = array[i]
            inc_arr.append(array[i])
 
        # If current element can be appended
        # to the decreasing sequence only
        elif dec > array[i]:
            dec = array[i]
            dec_arr.append(array[i])
 
        # Else we can not make such sequences
        # from the given array
        else:
            print('-1')
            break
 
    # Print the required sequences
    else:
        print(inc_arr, dec_arr)
 
# Driver code
arr = [5, 1, 3, 6, 8, 2, 9, 0, 10]
n = len(arr)
Find_Sequence(arr, n)


C#
// C# implementation of the approach
using System;
using System.Collections;
using System.Collections.Generic; 
 
class GFG{
   
// Function to print strictly increasing and
// strictly decreasing sequence if possible
static void Find_Sequence(int[] arr, int n)
{
 
    // Arrays to store strictly increasing and
    // decreasing sequence
    ArrayList inc_arr = new ArrayList();
    ArrayList dec_arr = new ArrayList();
 
    // Initializing last element of both sequence
    int flag = 0;
    long inc = -1, dec = (long)1e7;
 
    // Iterating through the array
    for(int i = 0; i < n; i++)
    {
 
        // If current element can be appended
        // to both the sequences
        if (inc < arr[i] && arr[i] < dec)
        {
 
            // If next element is greater than
            // the current element
            // Then append it to the strictly
            // increasing array
            if (arr[i] < arr[i + 1])
            {
                inc = arr[i];
                inc_arr.Add(arr[i]);
            }
 
            // Otherwise append it to the
            // strictly decreasing array
            else
            {
                dec = arr[i];
                dec_arr.Add(arr[i]);
            }
        }
 
        // If current element can be appended
        // to the increasing sequence only
        else if (inc < arr[i])
        {
            inc = arr[i];
            inc_arr.Add(arr[i]);
        }
 
        // If current element can be appended
        // to the decreasing sequence only
        else if (dec > arr[i])
        {
            dec = arr[i];
            dec_arr.Add(arr[i]);
        }
 
        // Else we can not make such sequences
        // from the given array
        else
        {
            Console.Write(-1);
            flag = 1;
            break;
        }
    }
 
    // Print the required sequences
    if (flag == 0)
    {
        foreach(int i in inc_arr)
            Console.Write(i + " ");
             
        Console.Write('\n');
 
        foreach(int i in dec_arr)
            Console.Write(i + " ");
             
        Console.Write('\n');
    }
}
 
// Driver Code
public static void Main(string[] args)
{
    int[] arr = { 5, 1, 3, 6, 8,
                  2, 9, 0, 10 };
    int n = arr.Length;
     
    Find_Sequence(arr, n);
}
}
 
// This code is contributed by rutvik_56


PHP
 $arr[$i])
        {
            $dec = $arr[$i];
            array_push($dec_arr, $arr[$i]);
        }
 
        // Else we can not make such sequences
        // from the given array
        else
        {
            echo '-1';
            break;
        }
    }
     
    // Print the required sequences
    print_r($inc_arr);
    print_r($dec_arr);
}
 
// Driver code
$arr = array(5, 1, 3, 6, 8, 2, 9, 0, 10);
$n = count($arr);
Find_Sequence($arr, $n);
 
// This code is contributed by Ryuga
?>


Javascript


输出:
[1, 3, 6, 8, 9, 10] [5, 2, 0]

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程