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

#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. Python基础类型(二) str 字符串

    字符串str ' ' 字符串+ 都是字符串的时候才能相加 a = 'alex' b = 'wusir' print(a+b) #字符串拼接 字符串* 字符串和数字相乘 a = 6 b = 'alex' ...

  2. ASP.NET MVC5 之 AspNetUsers 表增加字段

    MVC5 执行数据库迁移时,会生成一些默认的数据表,但是在实际的工作中.若用到的时候,难免要增添一些字段. 1.AspNetUsers 增加字段 A.打开MVC中的 IdentityModels.cs ...

  3. CTSC+APIO+THUACM游记

    退役之前,写点破事乐呵乐呵.. (同DaD3zZ) CTSC Day0 来到丽都 哈哈哈这可是四星级豪华酒店啊   想想要在这住7天  美滋滋 换了半天的房间 也没有换到一起   最后yzy& ...

  4. ACM_出题人这样不好吧

    出题人这样不好吧 Time Limit: 2000/1000ms (Java/Others) Problem Description: 作为编协的第一次月赛,肯定是要有防AK(ALL KILL)的题目 ...

  5. ACM_Power Mouth

    Power Mouth Time Limit: 2000/1000ms (Java/Others) Problem Description: Your task is to calculate the ...

  6. 6CSS之文本

    CSS文本:文本缩进(text-indent).文本对齐(text-align).文本修饰(text-decoration).文本大小写(text-transform).字符距离(letter-spa ...

  7. [转]mysql触发器的作用及语法

    转自:http://blog.csdn.net/cloudday/article/details/6905590 触发器是一种特殊的存储过程,它在插入,删除或修改特定表中的数据时触发执行,它比数据库本 ...

  8. Java——Spring介绍

    spring 是一个开源框架,是为了解决企业应用程序开发. 功能如下:1.目的:解决企业应用开发的复杂性.2.功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能.3.范围:任何Ja ...

  9. Android Unable to add window -- token android.os.BinderProxy@3a067204 is not valid错误分析记录

    打开APP时,出现闪退的情况,查看android studio报错信息,主要为: Unable to add window -- token android.os.BinderProxy@3a0672 ...

  10. C# winform启动外部exe后,如何完全阻断父界面接收事件,扩展waitforexit

    公司的系统搭载了好多奇奇怪怪的exe,以前启动exe后,系统还能接着操作.但是后面又提出额外的需求,说是打开外部exe之后,启动exe的父界面要完全不能进行任何操作.当然按常人所想再加一句waitfo ...