zoukankan      html  css  js  c++  java
  • Codeforces 584E Anton and Ira

    Anton and Ira

    我们把点分为三类, 向左走的, 向右走的, 不动的。

    最完美的情况就是每个点没有走反方向。

    每次我们挑选最右边的向右走的去把向左走的交换过来,这样能保证最优。

    #include<bits/stdc++.h>
    #define LL long long
    #define fi first
    #define se second
    #define mk make_pair
    #define PLL pair<LL, LL>
    #define PLI pair<LL, int>
    #define PII pair<int, int>
    #define SZ(x) ((int)x.size())
    #define ull unsigned long long
    using namespace std;
    
    const int N = 2000 + 7;
    const int inf = 0x3f3f3f3f;
    const LL INF = 0x3f3f3f3f3f3f3f3f;
    const int mod = 1e9 + 7;
    const double eps = 1e-8;
    
    int n, cost, a[N], b[N], to[N];
    vector<PII> ans;
    
    int main() {
        scanf("%d", &n);
        for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
        for(int i = 1; i <= n; i++) scanf("%d", &b[i]), to[b[i]] = i;
        for(int i = n; i >= 1; i--) {
            if(to[a[i]] > i) {
                int pre = i;
                for(int j = i + 1; j <= n && to[a[pre]] != pre; j++) {
                    if(to[a[j]] == j) continue;
                    ans.push_back(mk(pre, j));
                    cost += abs(j - pre);
                    swap(a[pre], a[j]);
                    pre = j;
                }
            }
        }
        printf("%d
    ", cost);
        printf("%d
    ", SZ(ans));
        for(auto& t : ans) printf("%d %d
    ", t.fi, t.se);
        return 0;
    }
    
    /*
    */
  • 相关阅读:
    uva 562
    uva 624
    hdu 3826
    CF 89 div2 A
    看视屏学编程之接口、抽象类
    看视频学编程之SQL数据库基础
    看视频学编程之WinForm
    看视频学编程之骑士飞行棋—自己做(单机版)
    看视频学编程之继承
    看视频学编程之C#中的类
  • 原文地址:https://www.cnblogs.com/CJLHY/p/10400925.html
Copyright © 2011-2022 走看看