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], ...
随机推荐
- 在vue项目npm run build后,index.html中引入css和js 报MIME type问题
问题: 1.在vue项目中,build打包后,index页面打开会报错, MIME type ('text/html') ;报错内容:because its MIME type ('text/html ...
- 【LeetCode】105 & 106 Construct Binary Tree from (Preorder and Inorder) || (Inorder and Postorder)Traversal
Description: Given arrays recording 'Preorder and Inorder' Traversal (Problem 105) or 'Inorder and ...
- V3的普通窗体控件的使用
1.文本: 属性: 标签对齐方式: Left Right Center 值对齐方式: Left Right Center 事件: 值改变事件 值加载事件 单击标题事件 键盘按下事件 获得焦点事 ...
- 来自一个用户的体验-Alpha项目测试
软件梦之队成员:201731062305 周蓉 这个作业属于哪个课程 <课程的链接> 这个作业要求在哪里 <作业要求的链接> 团队名称 <软件梦之队>(附上团队博客 ...
- x264
x264 x264 h264 1. x264调用主要过程 x264_param_default():设置参数集结构体x264_param_t的缺省值. x264_picture_alloc():为 ...
- asp.net MVC 下拉多级联动及编辑
多级联动实现,附源码.当前,部分代码是参与博客园其它网友. 新增,前台代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 2 ...
- centos vm 桥接 --网络配置
/etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 BOOTPROTO=none BROADCAST=192.168.1.255 HWADDR= ...
- matlab数值数据的表示方法,输出数据以及相关函数
数据类型的分类: 1.整型 无符号整型和带符号整形 带符号整形的最大值是127 >>x=int8(129) 输出结果是x=127 >>x=unit8(129) 输出结果是x=1 ...
- 在Python中利用CVXOPT求解二次规划问题
工作中需要用到cvxopt,cvxopt安装有坑,大家注意下.1.首先一定要卸载numpy,无论是直接安装的,还是anaconda安装的,主要是必须用whl安装numpy才不会有包的冲突2.二次规划包 ...
- 洛谷——P1063 能量项链
P1063 能量项链 题目描述 在MarsMars星球上,每个MarsMars人都随身佩带着一串能量项链.在项链上有NN颗能量珠.能量珠是一颗有头标记与尾标记的珠子,这些标记对应着某个正整数.并且,对 ...