zoukankan      html  css  js  c++  java
  • Codeforces 584E

    题目链接:https://codeforces.com/contest/584/problem/E

    题意:

    给两个 $1 sim n$ 的排列 $p,s$,交换 $p_i,p_j$ 两个元素需要花费 $|i-j|$,要求你用最少的钱交换 $p$ 中的元素使得其变成 $s$。

    题解:

    思路很简单,给个例子如下:

    $p$:$5,1,2,3,4,6$

    $s$:$3,4,1,6,2,5$

    我们不难发现,像:

    $p$:$5,1,underline{2},underline{3},4,6$

    $s$:$underline{3},4,1,6,underline{2},5$

    或者

    $p$:$5,1,underline{2},3,underline{4},6$

    $s$:$3,underline{4},1,6,underline{2},5$

    这样的情况,交换两个数字必定是不会亏的,而且是必须要花费的,所以每次都直接暴力枚举找符合的 $(i,j)$,一旦遇到就立刻把它俩交换即可。

    当然不能太过暴力地 $O(n^2)$ 去找 $(i,j)$,需要一点剪枝和区间控制。

    AC代码:

    #include<bits/stdc++.h>
    using namespace std;
    typedef pair<int,int> P;
    #define mk(x,y) make_pair(x,y)
    #define fi first
    #define se second
    const int maxn=2e3+10;
    int n;
    int s[maxn],t[maxn];
    int pos[maxn];
    
    P Find()
    {
        int x,y;
        for(int i=1;i<=n;i++)
        {
            x=pos[s[i]];
            if(i<x)
            {
                for(int j=i+1;j<=x;j++)
                {
                    y=pos[s[j]];
                    if(y<=i) return mk(i,j);
                }
            }
        }
    }
    
    int main()
    {
        ios::sync_with_stdio(0);
        cin.tie(0), cout.tie(0);
    
        cin>>n;
        for(int i=1;i<=n;i++) cin>>s[i];
        for(int i=1;i<=n;i++) cin>>t[i], pos[t[i]]=i;
    
        int diff=0;
        for(int i=1;i<=n;i++) diff+=(s[i]!=t[i]);
    
        vector<P> op;
        int cost=0;
        while(diff)
        {
            P r=Find();
            swap(s[r.fi],s[r.se]);
            //printf("%d <-> %d
    ",r.fi,r.se);
            op.push_back(r);
            cost+=abs(r.fi-r.se);
            diff-=(s[r.fi]==t[r.fi]);
            diff-=(s[r.se]==t[r.se]);
        }
    
        cout<<cost<<'
    ';
        cout<<op.size()<<'
    ';
        for(auto x:op) cout<<x.fi<<" "<<x.se<<'
    ';
    }
  • 相关阅读:
    WKT转换工具terraformers
    关于微信公众号投票结果的通告
    个人博客03
    个人博客02
    个人博客01
    四则运算2
    学习进度条
    构建之法阅读笔记01
    错误随笔
    软件工程概论第一节
  • 原文地址:https://www.cnblogs.com/dilthey/p/10543258.html
Copyright © 2011-2022 走看看