Codeforces 584E - Anton and Ira - [贪心]
题目链接:https://codeforces.com/contest/584/problem/E
题意:
给两个 $1 \sim n$ 的排列 $p,s$,交换 $p_i,p_j$ 两个元素需要花费 $|i-j|$,要求你用最少的钱交换 $p$ 中的元素使得其变成 $s$。
题解:
思路很简单,给个例子如下:
$p$:$5,1,2,3,4,6$
$s$:$3,4,1,6,2,5$
我们不难发现,像:
$p$:$5,1,\underline{2},\underline{3},4,6$
$s$:$\underline{3},4,1,6,\underline{2},5$
或者
$p$:$5,1,\underline{2},3,\underline{4},6$
$s$:$3,\underline{4},1,6,\underline{2},5$
这样的情况,交换两个数字必定是不会亏的,而且是必须要花费的,所以每次都直接暴力枚举找符合的 $(i,j)$,一旦遇到就立刻把它俩交换即可。
当然不能太过暴力地 $O(n^2)$ 去找 $(i,j)$,需要一点剪枝和区间控制。
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
#define mk(x,y) make_pair(x,y)
#define fi first
#define se second
const int maxn=2e3+;
int n;
int s[maxn],t[maxn];
int pos[maxn]; P Find()
{
int x,y;
for(int i=;i<=n;i++)
{
x=pos[s[i]];
if(i<x)
{
for(int j=i+;j<=x;j++)
{
y=pos[s[j]];
if(y<=i) return mk(i,j);
}
}
}
} int main()
{
ios::sync_with_stdio();
cin.tie(), cout.tie(); cin>>n;
for(int i=;i<=n;i++) cin>>s[i];
for(int i=;i<=n;i++) cin>>t[i], pos[t[i]]=i; int diff=;
for(int i=;i<=n;i++) diff+=(s[i]!=t[i]); vector<P> op;
int cost=;
while(diff)
{
P r=Find();
swap(s[r.fi],s[r.se]);
//printf("%d <-> %d\n",r.fi,r.se);
op.push_back(r);
cost+=abs(r.fi-r.se);
diff-=(s[r.fi]==t[r.fi]);
diff-=(s[r.se]==t[r.se]);
} cout<<cost<<'\n';
cout<<op.size()<<'\n';
for(auto x:op) cout<<x.fi<<" "<<x.se<<'\n';
}
Codeforces 584E - Anton and Ira - [贪心]的更多相关文章
- Codeforces 584E Anton and Ira
Anton and Ira 我们把点分为三类, 向左走的, 向右走的, 不动的. 最完美的情况就是每个点没有走反方向. 每次我们挑选最右边的向右走的去把向左走的交换过来,这样能保证最优. #inclu ...
- codeforces 584E Anton and Ira [想法题]
题意简述: 给定一个$1$到$n(n<=2000)$的初始排列以及最终排列 我们每次可以选取位置为$i$和$j$的 并交换它们的位置 花费为$ |i-j| $ 求从初始状态变换到末状态所需最小花 ...
- Codeforces Round #324 (Div. 2) E. Anton and Ira 贪心
E. Anton and Ira Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/584/probl ...
- codeforces Gym 100338E Numbers (贪心,实现)
题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...
- [Codeforces 1214A]Optimal Currency Exchange(贪心)
[Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...
- 【25.00%】【codeforces 584E】Anton and Ira
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- Codeforces Round #329 (Div. 2)B. Anton and Lines 贪心
B. Anton and Lines The teacher gave Anton a large geometry homework, but he didn't do it (as usual ...
- Codeforces 734E. Anton and Tree 搜索
E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...
- codeforces 349B Color the Fence 贪心,思维
1.codeforces 349B Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1 ...
随机推荐
- linux 安装nvm,通过nvm安装node
1,nvm git地址点击打开链接,安装命令 curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh ...
- JAVA与C#的区别
Java和C#都是编程的语言,它们是两个不同方向的两种语言 相同点: 他们都是面向对象的语言,也就是说,它们都能实现面向对象的思想(封装,继承,多态) 区别: 1.c#中的命名空间是namespace ...
- Java之NIO
想要学习Java的Socket通信,首先要学习Java的IO和NIO基础,这方面可以阅读<Java NIO 系列教程>. 下面展示自己代码熟悉Java的NIO编程的笔记. 1.缓冲区(Bu ...
- [转]Anatomy of a Program in Memory
Memory management is the heart of operating systems; it is crucial for both programming and system a ...
- Redis-Sentinel Redis的哨兵模式
Redis-Sentinel Redis的哨兵模式Redis Sentinel 模式简介Redis-Sentinel是官方推荐的高可用解决方案,当redis在做master-slave的高可用方案时, ...
- .NET Core +NuGet 创建打包发布自己的类库包
1. 创建类库项目 你可以使用现有的 .NET 类库项目用于要打包的代码,或者创建一个简单的项目,.NET CORE 2.1 项目的 类库如下所示: NugetDemo.class using Sys ...
- dedecms 5.7sp2 20170405运行PHP7.1的大坑(dedecms PHP7.1)
今天一个小站用了dedecms最新版,也就是5.7SP220170405版,(见下图) 点进去到下载页面下载,用了UTF8版本的.(见下图) 下载完成后,自己新开发了一套模板,听说PHP7.1性能提升 ...
- 机器学习&深度学习基础(机器学习基础的算法概述及代码)
参考:机器学习&深度学习算法及代码实现 Python3机器学习 传统机器学习算法 决策树.K邻近算法.支持向量机.朴素贝叶斯.神经网络.Logistic回归算法,聚类等. 一.机器学习算法及代 ...
- Ubuntu下安装antlr-4.7.1
简介:antlr工具将语法文件转换成可以识别该语法文件所描述的语言的程序. 例如:给定一个识别json的语法,antlr工具将会根据该语法生成一个程序,该程序可以通过antlr运行库来识别输入的jso ...
- badgeview
https://github.com/AlexLiuSheng/BadgeView include: compile 'com.allenliu.badgeview:library:1.1.1'(ne ...