Educational Codeforces Round 99 (Rated for Div. 2) D. Sequence and Swaps (模拟)

数据太小 模拟就完事了

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<math.h>
#include<cstdio>
#include<string.h>
#include<map>
#include<vector>
#include<deque>
#define ll long long
using namespace std;
#define sc(x) scanf("%lld",&x)
#define pt(x) printf("%lld\n",x)
ll a[505];
int main()
{
    ll t;
    sc(t);
    while(t--)
    {
        ll n,k;
        sc(n);sc(k);
        for(int i=1;i<=n;i++)sc(a[i]);
        ll pp=1;
        a[0]=-1;
        for(int i=1;i<=n;i++)
        {
            if(a[i]<a[i-1])
            {
                pp=0;
                break;
            }
        }
        if(pp)
        {
            cout<<0<<'\n';

            continue;
        }
        ll ind;
        for(int i=1;i<=n;i++)
        {
            if(k<a[i])
            {
                ind=i;
                break;
            }
        }
        ll ans=0;
        for(int i=ind;i<=n;i++)
        {
            if(a[i]>k)
            {
                swap(a[i],k);
                ans++;
                ll ff=1;
                for(int j=ind;j<=n;j++)
                {
                    if(a[j]<a[j-1])
                    {
                        ff=0;
                        break;
                    }
                }
                if(ff)break;
            }
        }
        ll flag=1;
        a[0]=-1;
        for(int i=1;i<=n;i++)
        {
            if(a[i]<a[i-1])
            {
                flag=0;
                break;
            }
        }
        if(flag)cout<<ans<<'\n';
        else cout<<-1<<'\n';
    }
}
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页