Data structures 将队列转换为最小移动的非递减顺序,仅在末尾插入操作

Data structures 将队列转换为最小移动的非递减顺序,仅在末尾插入操作,data-structures,queue,logic,Data Structures,Queue,Logic,X希望按照学生身高的非降序排列学生队列。他们现在是随意站着的。如果唯一允许的操作是选择一个学生并将该学生发送到队列的末尾,那么X可以完成多少个最小移动 Example: input: 3 3 2 1 output: 2 将2按到底,然后按3。 我不知道如何找出我的逻辑是否正确,在一次比赛中我被要求编写这个代码,但在比赛结束时我没有得到任何指导。有人能告诉我逻辑是否正确吗?我已经添加了注释来解释代码。我在codeforces或hackerrank上找不到相同的问题,我不知道如何搜索这样的

X希望按照学生身高的非降序排列学生队列。他们现在是随意站着的。如果唯一允许的操作是选择一个学生并将该学生发送到队列的末尾,那么X可以完成多少个最小移动

Example: 
input:
3 
3 2 1 
output: 
2
将2按到底,然后按3。
我不知道如何找出我的逻辑是否正确,在一次比赛中我被要求编写这个代码,但在比赛结束时我没有得到任何指导。有人能告诉我逻辑是否正确吗?我已经添加了注释来解释代码。我在codeforces或hackerrank上找不到相同的问题,我不知道如何搜索这样的问题。提前谢谢

#include <iostream>
#include <algorithm>
using namespace std;

int main() {

    int n,i,j,k,cnt;
    cin>>n;

    //n is the number of elements in the array

    int a[n+n],b[n+n];

    //reading the elements, and storing them in 2 arrays a and b
    for(i=0;i<n;i++)
    {
        cin>>a[i];
        b[i]=a[i];
    }
    sort(a,a+n);

    //array a has elements in sorted order
    //array b has elements of the given order as per question
    //cnt holds the answer required


//We pick each height from sorted array, check its number of inversions in 
//original array. Add this to count, add the heights to end of original 
//array, set these numbers to -1 in their actual position and assume that in 
//original array after 'n', elements are sorted. This will ensure optimal answer, I think.    


    for(i=0,cnt=0,k=n;i<n;i++)
    {
        for(j=0;j<n && a[i]!=b[j];j++)
        {
            if(b[j]>a[i] && b[j]!=-1)
            {
                cnt++;
                b[k++]=b[j];
                b[j]=-1;
            }
        }
    }
    cout<<cnt;
    return 0;
}
#包括
#包括
使用名称空间std;
int main(){
int n,i,j,k,cnt;
cin>>n;
//n是数组中的元素数
int a[n+n],b[n+n];
//读取元素,并将其存储在2个数组a和b中
对于(i=0;i>a[i];
b[i]=a[i];
}
排序(a,a+n);
//数组a具有按排序顺序排列的元素
//数组b具有给定顺序的元素,如问题所示
//cnt持有所需的答案
//我们从排序后的数组中选取每个高度,在
//原始数组。将此添加到计数,将高度添加到原始数组的末尾
//数组中,将这些数字的实际位置设置为-1,并假设
//在“n”之后的原始数组,元素被排序。我认为这将确保最佳答案。

对于(i=0,cnt=0,k=n;在您的示例中,iIs
4
是一个打字错误?我的错,它是3。编辑它。