确定数组是否可以旋转3个连续的数组元素?

时间:2018-06-16 12:15:24

标签: c++ arrays algorithm sorting

我有一系列自然数的排列,从1开始作为数组递增。如何确定是否可以使用3个连续元素的旋转对数组进行排序?

我已经实现了一种算法,其中基本上我将数组的索引与数组中该索引处的元素进行比较。如果它们不相等,那么我调用函数choose_indices(),它首先在数组中的正确位置找到要交换的元素,并在找到它之后,选择3个连续的元素,包括要交换的数字并旋转它们。在对大小为n的数组执行n-1次旋转之后,对数组进行排序。如果可以使用3个连续元素旋转对数组进行排序,则该实现返回true,但如果不能使用此方法对数组进行排序,则返回数组的超时。

using namespace std;
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<cstring>
int n;
void rotate(vector<int> &arr,int end,int mid,int start)
{
    int temp=arr[start];
    arr[start]=arr[end];
    arr[end]=arr[mid];
    arr[mid]=temp;
}
void choose_indices(vector<int> &arr,int s,int q)
{
    for(int l=q;l<n;l++)
    {
        if(arr[l]==s)
        {
            if(l-q>=2)
            {
                rotate(arr,l,l-1,l-2);
                break;
            }
            else
            {
                rotate(arr,l+1,l,l-1);
                break;
            }
        }
    }
}
int main()
{
    vector<int> arr;
    int q,count=0;
    cin>>q;
    for(int i=0;i<q;i++)
    {
        cin>>n;
        count=0;
        for(int i=0,p;i<n;i++)
        {
            cin>>p;
            arr.push_back(p);
        }
        for(int j=0,k=1;j<n && k<n; )
        {
            if(arr[j]!=k)
            {
                choose_indices(arr,k,j);
                if(arr[j]==k)
                {
                    j++;
                    k++;
                    count++;
                }
            }
            else
            {
                j++;
                k++;
                count++;
            }
        }
        if(count==n-1)
        {
            cout<<"YES"<<endl;
        }
        else
        {
           cout<<"NO"<<endl;
        }
        arr.clear();
    }
}

示例输入: 1 2 3 5 4

对于此输入,我的代码会给出运行时错误。 如何找到给定的数组是否无法使用3个连续元素的旋转进行排序?

2 个答案:

答案 0 :(得分:2)

旋转3个相邻元素将始终取消2个反转(如果存在)或者会引入2个反转

考虑一下:

1 2 3 5 4

只有1次反转,无论你旋转多少次,你都不能在不引入其他反转的情况下取消反转,因为你将始终旋转3个连续元素

所以只计算倒数的数量,如果它是奇数,那么答案是,否则。有一些有效的算法来计算反转次数(比如合并排序)。

答案 1 :(得分:0)

using namespace std;
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<cstring>
int n;
void rotate(vector<int> &arr,int end,int mid,int start)
{
    int temp=arr[start];
    arr[start]=arr[end];
    arr[end]=arr[mid];
    arr[mid]=temp;
}
void choose_indices(vector<int> &arr,int s,int q)
{
    for(int l=q;l<n;l++)
    {
        if(arr[l]==s)
        {
            if(l-q>=2)
            {
                rotate(arr,l,l-1,l-2);
                break;
            }
            else
            {
                rotate(arr,l+1,l,l-1);
                break;
            }
        }
    }
}
int main()
{
    vector<int> arr;
    int q,count=0;
    cin>>q;
    for(int i=0;i<q;i++)
    {
        cin>>n;
        count=0;
        for(int i=0,p;i<n;i++)
        {
            cin>>p;
            arr.push_back(p);
        }
        //Counting the number of inversion in the array
        int ctiv=0;
        for(int r=0;r<n;r++)
        {
            for(int s=r+1;s<n;s++)
            {
                if(arr[r]>arr[s] && r<s)
                {
                    ctiv++;
                }
            }
        }
        if(ctiv%2!=0)
        {
            cout<<"NO"<<endl;
        }
        else 
        {
            for(int j=0,k=1;j<n && k<n; )
            {
                if(arr[j]!=k)
                {
                    choose_indices(arr,k,j);
                    if(arr[j]==k)
                    {
                        j++;
                        k++;
                        count++;
                    }
                }
                else
                {
                    j++;
                    k++;
                    count++;
                }
            }
            if(count==n-1)
            {
                cout<<"YES"<<endl;
            }
            arr.clear();
        }
    }
}

这是我设计的算法,它可以查找给定的算法是否可以排序,如果可以对其进行排序,它会通过执行必要的3次连续旋转来对给定的数组进行排序。