King‘s Task(模拟)

King's Task - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

Problem - 1510K - Codeforces

1.将全部相邻的数交换,
2将前半段和 后半段的数交换
如果连续进行一种操作两次就会相互抵消,那么答案一定是两种操作交替进行的
看看哪个操作先进行,所得的结果会更优
#include <bits/stdc++.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
#define x first
#define int long long
#define y second
#define ios ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
using namespace std;

typedef long long LL;
typedef pair<int,int> PII;
typedef pair<char,int> PCI;
typedef pair<LL,LL> PLL;
//typedef __int128 i128;
typedef unsigned long long ULL;
const int N=2e5+10,INF = 1e9 ,mod = 1e9 + 7 ;
const double eps = 1e-7;


int n,a[N],b[N],c[N];

bool same(int a[],int b[])
{
    for(int i=1;i <= 2* n ; i++ )
    if(a[i] != b[i]) return 0;
    return 1;
}

int ans=INF;

void solve()
{
    cin >> n;
    for(int i=1;i <= 2 * n ;i ++ ) 
    {
        int t;cin >> t;a[i] = b[i] = c[i] = t;
    }
    
    
    bool flag =1;
    if(!is_sorted(a+1,a+2*n + 1)) flag =0;
    
    
    if(flag)
    {
        cout << 0 << endl;return;
    }
    int op=0,id =1;
    while(!same(a,b) || !op)//先进行第一种操作 
    {
        op ++;
        if(id)
        {
            for(int i=1;i+1<=2 *n ;i += 2)
            swap(b[i],b[i + 1] );
        }
        else 
        {
            for(int i=1;i<=n; i++ )
            swap(b[i],b[i + n]);
        }
        id ^= 1;
        
        if(is_sorted(b+1,b+1+2*n)) 
        {
            ans =min(ans,op);break;
        }
    }
    id = op = 0 ;
    while(!same(a,c) || !op)
    {
        op ++;
        if(id)
        {
            for(int i=1;i+1<=2 *n ;i += 2)
            swap(c[i],c[i + 1] );
        }
        else 
        {
            for(int i=1;i<=n; i++ )
            swap(c[i],c[i + n]);
        }
        id ^= 1;
        
        if(is_sorted(c+1,c+1+n*2)) 
        {
            ans = min(ans,op);break;
        }
    }
    
    if(ans== INF ) ans = -1;
    
    cout << ans << endl;
    
    
    
}



signed main()
{
//    freopen("1.txt","r",stdin);
    ios
    LL T=1;
//    cin>>T;
    while(T -- )
    {
        solve();
    }



    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值