Codeforces 584E Anton and Ira【思维+贪心】好题~
来源:互联网 发布:上瘾网络剧微博 编辑:程序博客网 时间:2024/05/29 08:26
Anton loves transforming one permutation into another one by swapping elements for money, and Ira doesn't like paying for stupid games. Help them obtain the required permutation by paying as little money as possible.
More formally, we have two permutations, p ands of numbers from 1 ton. We can swap pi andpj, by paying|i - j| coins for it. Find and print the smallest number of coins required to obtain permutations from permutation p. Also print the sequence of swap operations at which we obtain a solution.
The first line contains a single number n (1 ≤ n ≤ 2000) — the length of the permutations.
The second line contains a sequence of n numbers from1 to n — permutationp. Each number from 1 to n occurs exactly once in this line.
The third line contains a sequence of n numbers from1 to n — permutations. Each number from 1 to n occurs once in this line.
In the first line print the minimum number of coins that you need to spend to transform permutationp into permutation s.
In the second line print number k (0 ≤ k ≤ 2·106) — the number of operations needed to get the solution.
In the next k lines print the operations. Each line must contain two numbersi and j (1 ≤ i, j ≤ n,i ≠ j), which means that you need to swappi andpj.
It is guaranteed that the solution exists.
44 2 1 33 2 4 1
324 33 1
In the first sample test we swap numbers on positions 3 and 4 and permutation p becomes 4 2 3 1. We pay |3 - 4| = 1 coins for that. On second turn we swap numbers on positions 1 and 3 and get permutation3241 equal to s. We pay|3 - 1| = 2 coins for that. In total we pay three coins.
题目大意:
给你长度为N的两个序列,上边序列表示当前序列,下边序列表示目标序列。
我们可以进行无限次操作,使得交换两个数的位子,使得当前序列变成目标序列。
现在交换位子i,j的两个数,要花费abs(i-j);
问最小花费的方式,使得当前序列变成目标序列。
思路:
1、序列中无非有三种数:
①不用动的
②想要往右动的
③想要往左动的
我们抛开①类数不谈,那么只剩下②③两种数。
很明显,如果我们交换了两个都想向右动的数,那么肯定要带来不必要的多余的操作,因为其中一个数肯定是向右动了,对于这个数来讲是距离自己想要到达的目标近了些,但是另外一个数向左动了,相当于让他距离他的目标位子更加远了。
2、那么根据上述简单的分析,显然我们每次交换一个想向右动的数和一个想向左动的数是最优的。
那么我们预处理出来所有数想要动的方向,暴力预处理即可,时间复杂度O(n^2)。
接下来同样暴力模拟,我们每一次找到最右边的那个想要向右动的数的位子,然后让他向右依次和想要向左边动的数进行交换,直到到达了目的地为止(这里保证一定是可以到达目的地的,因为这个数是序列最右边的那个想要向右动的数的位子,那么目标位子上边的那个数一定是想要往左动的,那么这里就能保证一定到达目的地)。
过程维护花费和操作情况即可。
时间复杂度O(n^2);
3、注意一些代码实现的细节别的就没有什么了。
Ac代码:
#include<stdio.h>#include<string.h>#include<algorithm>#include<iostream>using namespace std;struct node{ int x,y;}p[2050000];int dir[2005];//l = 1 r = 2;int a[2005];int b[2005];int main(){ int n; while(~scanf("%d",&n)) { for(int i=1;i<=n;i++)scanf("%d",&a[i]); for(int i=1;i<=n;i++)scanf("%d",&b[i]); for(int i=1;i<=n;i++) { if(a[i]==b[i])dir[i]=0; else { for(int j=1;j<=n;j++) { if(a[i]==b[j]) { if(j<i)dir[i]=1; else dir[i]=2; } } } } int cnt=0; int ans=0; int pos=0; for(int i=1;i<=n;i++)if(dir[i]==2)pos=i; while(1) { for(int i=pos;i>=0;i--) { if(dir[i]==2) { pos=i; break; } } int now=pos; for(int j=pos+1;j<=n;j++) { if(dir[j]==1) { p[cnt].x=now; p[cnt].y=j; cnt++; swap(a[now],a[j]); swap(dir[now],dir[j]); ans+=abs(now-j); now=j; } if(a[now]==b[now])break; } pos--; int flag=0; for(int i=1;i<=n;i++) { if(a[i]==b[i])dir[i]=0; if(dir[i]!=0)flag=1; } if(flag==0)break; } printf("%d\n",ans); printf("%d\n",cnt); for(int i=0;i<cnt;i++) { printf("%d %d\n",p[i].x,p[i].y); } }}
- Codeforces 584E Anton and Ira【思维+贪心】好题~
- Codeforces 584E. Anton and Ira (排列好题)
- 51nod 1574 || Codeforces 584 E. Anton and Ira 思维+构造+贪心
- Codeforces 584E Anton and Ira
- CodeForces 584E Anton and Ira
- 【25.00%】【codeforces 584E】Anton and Ira
- codeforces 324# E. Anton and Ira (暴力枚举+贪心)
- CF 584E(Anton and Ira-构造+贪心)
- Codeforces Round #324 (Div. 2) E. Anton and Ira(贪心)
- [排列组合]Codeforces Round #324 (Div. 2)E - Anton and Ira
- Codeforces Round #324 Anton and Ira(构造+贪心)
- 【Codeforces Round 324 (Div 2)E】【贪心 构造】Anton and Ira 全排列交换 最小距离成本
- 构造+贪心 Codeforces584E Anton and Ira
- CF Round#324 (Div2) E. Anton and Ira
- Codeforces 734E Anton and Tree【并查集缩点+树的直径】好题~
- Codeforces 320E Kalila and Dimna in the Logging Industry【思维+贪心+斜率优化Dp】好题!
- Codeforces 593B Anton and Lines 【思维】
- CodeForces 593B - Anton and Lines(思维)
- Android之vold进程启动源码分析
- 关于windows中一些系统dll的卸载套路
- JZOJ1490.2017.04.02【NOIP 普及组】模拟赛C组 T2士兵
- JAVA(3)——阶乘
- caffe prototxt文件 参数配置介绍
- Codeforces 584E Anton and Ira【思维+贪心】好题~
- 鸟瞰图(立体视觉)
- jzoj P1028【GDOI2005】飞越原野
- 字符串重新排列
- Android Service之MountService源码分析
- 符号三角形问题
- appium安装
- 焦点轮播图
- 第一篇博客,说说Ubuntu Server16.04服务器的安装吧