HihoCoder - 1781: Another Bubble Sort (冒泡排序&逆序对)
Sample Input
3
9
8 7 5 1 9 2 6 4 3
1 2 3 4 5 6 7 8 9
9
8 7 5 1 9 2 6 4 3
1 2 5 4 3 6 7 8 9
9
8 7 5 1 9 2 6 4 3
1 2 5 6 4 3 7 8 9
Sample Output
Case #1: 6
Case #2: 4
Case #3: -1
Prof.Q is a sophisticated professor who has insights into quadrillions of sorting algorithms, especially into bubble sort. Bubble sort is a simple algorithm that repeatedly steps through the array to be sorted, compares each pair of adjacent elements and swaps them if they are in the wrong order. In brief, bubble sort executes the following iteration over and over again until the array is sorted.
This is your first day becoming a student of Prof.Q, so he gives you two arrays A[1..N] and B[1..N] of length N
as a placement test. Your task is to check whether it is possible to
execute the aforementioned iteration several times on the array A and then transform it into the array B. Furthermore, determine the minimum times of iteration to achieve it if it is possible.
Input
The first line contains one integer T, indicating the number of test cases.
The following lines describe all the test cases. For each test case:
The first line contains one integer N.
The second line contains N integers A[1], A[2], · · · , A[N].
The third line contains N integers B[1], B[2], · · · , B[N].
1 ≤ T ≤ 1000, 1 ≤ N ≤ 105 , 1 ≤ ai ≤ 109 (i = 1, 2, · · · , N).
It is guaranteed that the sum of N in all the test cases does not exceed 106.
Output
For each test case, print "Case #x: y" (without quotes) in one line, indicating that this is the x-th test case and the minimum number of iterations for this test case is y if it is possible, print y as −1 otherwise.
题意:冒泡排序,两个for语句,第一个表示进行了几轮,第二个表示从左往右遍历,如果左边的大于右边的,则交换。现在给定A数组,B数组。 问A数组是否可以根据上述的规则进行K轮排序得到B, 如果可以,求出K,否则输出-1;
思路:我们不难根据位置的变化得到K; 然后我们可以需要求出A数组进行K轮排序后的数组。 这里根据逆序对+二分来求得。
因为每一轮排序下来,新的逆序对rev和上一轮的关系是:rev[i]=max(pre[i+1],0);所以我们得到K轮后的结果,和B对比即可。
(注意这个数组的大小是1e9,我们需要离散化,即根据大小为第一关键字,位置为第二关键字排序。
#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
int a[maxn],rev[maxn],b[maxn],ans[maxn],pos[maxn],sum[maxn],N;
void add(int x,int v){ for(;x<=N;x+=(-x)&x) sum[x]+=v; }
int query(int x){int res=; for(;x;x-=(-x)&x) res+=sum[x]; return res;}
struct in{int x,pos; }s[maxn];
bool cmp(in w,in v ){ return w.x==v.x?w.pos<v.pos:w.x<v.x;}
void fcy(int f[]){
rep(i,,N) s[i].x=f[i],s[i].pos=i; sort(s+,s+N+,cmp);
rep(i,,N) f[s[i].pos]=i;
}
int main()
{
int T,C=,K;
scanf("%d",&T);
while(T--){
scanf("%d",&N); K=;
rep(i,,N) scanf("%d",&a[i]);
rep(i,,N) scanf("%d",&b[i]);
fcy(a); fcy(b); //离散化
rep(i,,N) pos[a[i]]=i;
rep(i,,N) K=max(K,pos[b[i]]-i); //求K
rep(i,,N) rev[i]=sum[i]=;
rep(i,,N) {
rev[i]=i--query(a[i]);
add(a[i],);
}//求A数组的逆序对
rep(i,,N) rev[i]=max(,i+K>N?:rev[i+K]-K);
rep(i,,N) sum[i]=;
rep(i,,N) add(i,);
for(int i=N;i>=;i--){
int L=,R=N,res;
while(L<=R){
int Mid=(L+R)>>;
if(query(Mid)>=i-rev[i]) res=Mid,R=Mid-;
else L=Mid+;
} ans[i]=res; add(res,-);
}//二分定位答案
rep(i,,N) if(ans[i]!=b[i]) {K=-; break;}
printf("Case #%d: %d\n",++C,K);
}
return ;
}
HihoCoder - 1781: Another Bubble Sort (冒泡排序&逆序对)的更多相关文章
- Bubble Sort冒泡排序
冒泡排序是一种简单的排序算法. 它每次重复的访问过要排序的数列, 一次比较两个元素, 如果他们的顺错误, 就把他们交换过来. 下面这种图很清晰的解释了什么是冒泡算法. 具体算法描述如下: 1. 比较相 ...
- Bubble Sort 冒泡排序
//Bubble Sort ( O(n²)) public class TestBubbleSort { public int[] bubbleSortArray(int[] arr){ ; i &l ...
- HihoCoder - 1801 :剪切字符串 (置换与逆序对)
Sample Input 6 5 11 Sample Output 6 小Hi有一个长度为N的字符串,这个字符串每个位置上的字符两两不同.现在小Hi可以进行一种剪切操作: 选择任意一段连续的K个字符, ...
- c++算法联系,冒泡排序,bubble sort,插入排序,insert sort,
#include <iostream.h> #define MAX 100 void dispaly(int a[],int n) { for(int i=0;i<n;i+ ...
- HDU 5775 Bubble Sort(冒泡排序)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- Java中的经典算法之冒泡排序(Bubble Sort)
Java中的经典算法之冒泡排序(Bubble Sort) 神话丿小王子的博客主页 原理:比较两个相邻的元素,将值大的元素交换至右端. 思路:依次比较相邻的两个数,将小数放在前面,大数放在后面.即在第一 ...
- Summary: Merge Sort of Array && 求逆序对
常用算法(后面有inplace版本): package ArrayMergeSort; import java.util.Arrays; public class Solution { public ...
- 冒泡排序(Bubble Sort)
常见的排序算法有Bubble Sort.Merge Sort.Quick Sort 等,所有排序算的基本法思想都是把一个无限大的数据规模通过算法一步步缩小,指导最后完成排序. 这里分享一下Buuble ...
- [算法] 冒泡排序 Bubble Sort
冒泡排序(Bubble Sort,台湾另外一种译名为:泡沫排序)是一种简单的排序算法.它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到没 ...
随机推荐
- Beta冲刺一《WAP团队》
β冲刺第一天 1. 今日完成任务情况以及遇到的问题. ①马麒.杜有海:管理员审核表的完善 ②郝明宇:登录.注册界面的完善 ③马宏伟.周欣:前端数据借用与后台的连接 ④乌勒扎:登录与注册功能的测试 2 ...
- windows 网页打不开github网站
gitbub是外网,经常会遇到访问不了的问题,并且有时能访问也网速好慢. 解决这个问题的方法是 更改hosts文件,地址:C:\Windows\System32\Drivers\etc 我在hosts ...
- 关于angular5的惰性加载报错问题
之前为了测试一个模块优化问题,于是用angular-cli快速搭建了个ng5的脚手架demo,在应用惰性加载功能的时候发现浏览器报错如下: ERROR Error: Uncaught (in prom ...
- [html]点击button后画面被刷新原因:未设置type="button"
一.问题原因解析: 在form表单里的button, type 属性未设置的情况下,Internet Explorer 的默认类型是 "button",而其他浏览器中(包括 W3C ...
- Java 访问权限修饰符以及protected修饰符的理解
2017-11-04 22:28:39 访问权限修饰符的权限 访问修饰符protected的权限理解 在Core Java中有这样一段话“在Object类中,clone方法被声明为protected, ...
- Oracle数据库system用户忘记了密码怎么办
1.在运行里面输入cmd调出dos窗口,然后在dos窗口中输入sqlplus /nolog 如:D:\oracle\ora92\bin>sqlplus /nolog 2.输入连接命令 如:SQL ...
- centos7: iptables保存(配置完nginx的web规则后)
centos7: iptables保存(配置完nginx的web规则后) 以本地虚拟机为例: 添加规则:入站规则 iptables -I INPUT -p tcp --dport 80 -j ACCE ...
- Android出现declaration of org.apache.http.message.BasicLineFormatter appears in /system/framework/
这是由于使用了CloseableHttpClient造成的,把 CloseableHttpClient httpclient = HttpClients.createDefault(); Closea ...
- P2048 [NOI2010]超级钢琴 (RMQ,堆)
大意: 给定n元素序列a, 定义一个区间的权值为区间内所有元素和, 求前k大的长度在[L,R]范围内的区间的权值和. 固定右端点, 转为查询左端点最小的前缀和, 可以用RMQ O(1)查询. 要求的是 ...
- hdu多校2C
题意:找多条路径覆盖所有的边,求最小路径数,要求输出路径 题解:新建一个点n+1,所有奇点向它连边,然后跑欧拉回路,最后把新加的边删去,一段连续的边就是一条路径 = =但是由于太久没写欧拉回路以及之前 ...