这题很简单,把目标位置排序,把目标位置在当前位置前面的往前交换,每次都是贪心选择第一个满足这样要求的数字。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std; const int MAX=2005; int p1[MAX],p2[MAX],mp[MAX];
int pos[MAX],goal[MAX];
bool rip[MAX]; vector<pair<int,int> >ans; int main(){
int n,counts,ans_counts,cost;
while(scanf("%d",&n)!=EOF){
counts=0;
ans.clear();
cost=0;
for(int i=1;i<=n;i++) scanf("%d",&p1[i]);
for(int i=1;i<=n;i++){
scanf("%d",&p2[i]);
mp[p2[i]]=i;
}
for(int i=1;i<=n;i++){
if(p1[i]!=p2[i]){
pos[++counts]=i;
goal[counts]=mp[p1[i]];
}
}
/* for(int i=1;i<=counts;i++)
cout<<pos[i]<<" ";
cout<<endl;
for(int i=1;i<=counts;i++){
cout<<goal[i]<<" ";
}
cout<<endl;*/
memset(rip,false,sizeof(rip));
if(counts==0){
puts("0");
puts("0");
continue;
}
int lmin=1;
for(int i=1;i<=counts;i++){
if(goal[i]<pos[i]){
int rmax=i-1,last=i,g=goal[i];
while(pos[last]!=g){
if(!rip[rmax]){
ans.push_back(make_pair(pos[last],pos[rmax]));
swap(goal[last],goal[rmax]);
if(goal[last]==pos[last]) rip[last]=true;
cost+=abs(pos[last]-pos[rmax]);
last=rmax;
}
rmax--;
}
rip[last]=true;
for(;;lmin++) if(!rip[lmin]) break;
}
}
printf("%d\n",cost);
printf("%d\n",ans.size());
int sz=ans.size();
for(int i=0;i<sz;i++){
printf("%d %d\n",ans[i].first,ans[i].second);
}
}
return 0;
}

  

CF #324 DIV2 E题的更多相关文章

  1. CF #324 DIV2 C题

    #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...

  2. CF #323 DIV2 D题

    可以知道,当T较大时,对于LIS,肯定会有很长的一部分是重复的,而这重复的部分,只能是一个block中出现次数最多的数字组成一序列.所以,对于T>1000时,可以直接求出LIS,剩下T-=100 ...

  3. CF #316 DIV2 D题

    D. Tree Requests time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  4. cf 442 div2 F. Ann and Books(莫队算法)

    cf 442 div2 F. Ann and Books(莫队算法) 题意: \(给出n和k,和a_i,sum_i表示前i个数的和,有q个查询[l,r]\) 每次查询区间\([l,r]内有多少对(i, ...

  5. CF#345 div2 A\B\C题

    A题: 贪心水题,注意1,1这组数据,坑了不少人 #include <iostream> #include <cstring> using namespace std; int ...

  6. codeforces round 422 div2 补题 CF 822 A-F

    A I'm bored with life 水题 #include<bits/stdc++.h> using namespace std; typedef long long int LL ...

  7. codeforces round 421 div2 补题 CF 820 A-E

    A Mister B and Book Reading  O(n)暴力即可 #include<bits/stdc++.h> using namespace std; typedef lon ...

  8. Codeforces round 419 div2 补题 CF 816 A-E

    A Karen and Morning 水题 注意进位即可 #include<bits/stdc++.h> using namespace std; typedef long long i ...

  9. codeforces round 418 div2 补题 CF 814 A-E

    A An abandoned sentiment from past 水题 #include<bits/stdc++.h> using namespace std; int a[300], ...

随机推荐

  1. Java Itext 生成PDF文件

    利用Java Itext生成PDF文件并导出,实现效果如下: PDFUtil.java package com.jeeplus.modules.order.util; import java.io.O ...

  2. NS2学习笔记(五)

    对无线网络,生成nam文件要使用namtrace-all-wireless, 而不是namtrace-all: set nf [open test_1.nam w] $ns_ namtrace-all ...

  3. 1、Web MVC简介

  4. define与typedef的区别

    define: 发生在预处理阶段,也就是编译之前,仅仅文本替换,不做任何的类型检查 没有作用域的限制 typedef: 多用于简化复杂的类型声明,比如函数指针声明:typedef bool (*fun ...

  5. Data URI scheme:data:image/jpeg;

    今天在用一个croppic的jQuery裁剪图片的插件的时候,发现在后台获取图片时,无法通过Request.File获取了,但是通过Request.Form[]可以.用firebug跟了一下发现,图片 ...

  6. JdbcTemplate:Jdbc模板和数据库元数据

    通过 Jdbc .C3P0 .Druid 的使用我们会发现即使我们做了工具的封装,但重复性的代码依旧很多.我们可以通过 JdbcTemplate 即 Jdbc 模板来使我们的代码更加简洁,逻辑更加清晰 ...

  7. ZUK Z2 Pro(Z2121) 免解锁BL 免rec Magisk Xposed ROOT 救砖 ZUI 4.0.247

    >>>重点介绍<<< 第一:本刷机包可卡刷可线刷,刷机包比较大的原因是采用同时兼容卡刷和线刷的格式,所以比较大第二:[卡刷方法]卡刷不要解压刷机包,直接传入手机后用 ...

  8. JS——scroll封装

    DTD未声明:document.body.scrollTop DTD已声明:document.documentElement.scrollTop 火狐谷歌IE9:window.pageYOffset ...

  9. CSS——padding

    padding是盒子内容与边框的距离. padding:10px;/*上下左右都是10px*/ padding:10px 20px;/*上下是10px 左右是20px*/ padding:10px 2 ...

  10. Linux下的文件结构,及对应文件夹的作用

    Linux下的文件结构,及对应文件夹的作用 /bin 二进制可执行命令 /dev 设备特殊文件 /etc 系统管理和配置文件 /etc/rc.d 启动的配置文件和脚本 /home 用户主目录的基点,比 ...