Codeforces 584E Anton and Ira

编程入门 行业动态 更新时间:2024-10-10 13:17:33

<a href=https://www.elefans.com/category/jswz/34/1770097.html style=Codeforces 584E Anton and Ira"/>

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\n", cost);printf("%d\n", SZ(ans));for(auto& t : ans) printf("%d %d\n", t.fi, t.se);return 0;
}/*
*/

 

转载于:.html

更多推荐

Codeforces 584E Anton and Ira

本文发布于:2024-03-23 01:47:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1739017.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Codeforces   Ira   Anton

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!