CF #324 DIV2 E题
这题很简单,把目标位置排序,把目标位置在当前位置前面的往前交换,每次都是贪心选择第一个满足这样要求的数字。
#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题的更多相关文章
- CF #324 DIV2 C题
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...
- CF #323 DIV2 D题
可以知道,当T较大时,对于LIS,肯定会有很长的一部分是重复的,而这重复的部分,只能是一个block中出现次数最多的数字组成一序列.所以,对于T>1000时,可以直接求出LIS,剩下T-=100 ...
- CF #316 DIV2 D题
D. Tree Requests time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- 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, ...
- CF#345 div2 A\B\C题
A题: 贪心水题,注意1,1这组数据,坑了不少人 #include <iostream> #include <cstring> using namespace std; int ...
- 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 ...
- 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 ...
- Codeforces round 419 div2 补题 CF 816 A-E
A Karen and Morning 水题 注意进位即可 #include<bits/stdc++.h> using namespace std; typedef long long i ...
- codeforces round 418 div2 补题 CF 814 A-E
A An abandoned sentiment from past 水题 #include<bits/stdc++.h> using namespace std; int a[300], ...
随机推荐
- JavaScript(JS)的简单使用
一.什么是JS(Javascript)? Javascript是一种脚本语言,被广泛用于Web应用开发,常用来为网页添加各式各样的功能,为用户提供更加流畅的浏览效果. Javascript严格区分大小 ...
- 自动调整速率的Actor设计模式
问题背景 与数据库或者存储系统交互是所有应用软件都必不可少的功能之一,akka开发的系统也不例外.但akka特殊的地方在于,会尽可能的将所有的功能都设计成异步的,以避免Actor阻塞,然而无法避免IO ...
- mariadb的安装
mysql (分支 mariadb)1.安装mariadb -yum -源码编译安装 -下载rpm安装 yum和源码编译安装的区别? 1.路径区别-yum安装的软件是他自定义的,源码安装的软件./co ...
- android 二维码 扫描,生成,竖屏
最近公司有用到二维码,生成,扫描,所以学习了一下,和大家分享: demo 见下面链接,已经改成竖屏: http://download.csdn.net/detail/q610098308/868101 ...
- 再战primer——decltype 和引用
刷primer看到原文讲到"引用从来都作为其所指对象的同义词出现,只有用在decltype处是一个例外.",我很是好奇. 这个“引用”当然是指引用类型,like this: ; i ...
- Unity学习-预制(四)
预制即克隆 比如要模拟一个下雨的场景,天下掉下来一颗一颗的雨滴.如果此时,我们给每一个雨滴创建一个对象的话,那会很浪费资源,而且也没必要,因为所有的雨滴是相同的.这个时候就使用到了预制,一种可以被重复 ...
- 联想 A5(L18011) 免解锁BL 免rec Magisk Xposed ROOT 救砖 ZUI 3.9.068
>>>重点介绍<<< 第一:本刷机包可卡刷可线刷,刷机包比较大的原因是采用同时兼容卡刷和线刷的格式,所以比较大第二:[卡刷方法]卡刷不要解压刷机包,直接传入手机后用 ...
- 图论 Make Unique:有向图和无向图的一些算法
计算机科学入门资料之一的<算法与数据结构-C语言版>,覆盖了基础算法的几乎所有分支,其中的一个典型分支为图理论. 一个简介:图论基础-图数据结构基础 一个简洁的博客:图论基础,简列一本书 ...
- (转)Hibernate中的多表操作
http://blog.csdn.net/yerenyuan_pku/article/details/70556208 Hibernate中的多表操作 在实际开发中,我们不可能只是简简单单地去操作单表 ...
- js 获取 鼠标位置 和获取元素位置
]; body.addEventListener("mousemove", outpostion); function outpostion() { console.log(&qu ...