Codeforces Round #743 (Div. 2) B. Swaps(思维)

https://codeforces.com/contest/1573/problem/B

给定两个长度为n的数组,数组a和数组b
数组a包含从1到2*n的任意顺序的奇数,数组b包含从1到2*n的任意偶数

可执行的操作如下:

从两个数组中选择一个,从1到n-1中选择一个索引
交换第i和第i+1个元素
计算使得数组a在字典序上小于数组b的所需要的最少的移动次数。

input
3
2
3 1
4 2
3
5 3 1
2 4 6
5
7 5 9 1 3
2 4 6 10 8
output
0
2
3

  • 从数组a的小个数依从往右边取,合成minn
  • 与此同时,数组b也需要达到首位数字大于数组a的首位数字的效果
  • 就可以在从左往右中同步取最终最小的移动代价
#include<bits/stdc++.h>
using namespace std;
const int N=200200,M=2002;
int a[N],b[N];
int main()
{
    cin.tie(0); ios::sync_with_stdio(false);
    int T=1;
    cin>>T;
    while(T--)
    {
        int n;
        cin>>n;
        map<int,int> mp;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
            mp[a[i]]=i;
        }
        for(int i=1;i<=n;i++)
        {
            cin>>b[i];
            mp[b[i]]=i;
        }
        sort(a+1,a+1+n);
        sort(b+1,b+1+n);
        //for(int i=1;i<=n;i++) cout<<a[i]<<" "; cout<<endl;
        // for(int i=1;i<=n;i++) cout<<b[i]<<" "; cout<<endl;
        int minn=1e9,ans=1e9;
        for(int i=1;i<=n;i++)
        {
            minn=min(minn,mp[a[i]]);
            ans=min(ans,minn+mp[b[i]]-2);
            //cout<<minn<<" "<<ans<<endl;
        }
        cout<<ans<<endl;
    }
    return 0;
}

posted @ 2022-08-21 17:39  高尔赛凡尔娟  阅读(31)  评论(0编辑  收藏  举报