Ultra-QuickSort POJ - 2299 (逆序对)
9 1 0 5 4 ,
Ultra-QuickSort produces the output
0 1 4 5 9 .
Your task is to determine how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.
Input
Output
Sample Input
- 5
- 9
- 1
- 0
- 5
- 4
- 3
- 1
- 2
- 3
- 0
Sample Output
- 6
- 0
- 题意:将一个序列从小到大排序,如果只能交换相邻的数,最少需要交换多少次
思路:和冒泡排序一样,一个数需要交换的次数就是它的逆序对数,所以就是求总的逆序对的个数- 求逆序对可以用两种方法
①归并排序:
- #include<cstdio>
- #include<iostream>
- using namespace std;
- int n;
- const int maxn = 5e5+;
- int num[maxn];
- typedef long long ll;
- ll Mersort(int l,int r)
- {
- int mid = (l+r)/;
- int i=l,j=mid+;
- int b[r-l+];
- int k=;
- ll ans = ;
- while(i <= mid && j <= r)
- {
- if(num[i] <= num[j])
- b[k++] = num[i++];
- else
- b[k++] = num[j++],ans+=mid-i+;
- }
- while(i <= mid)
- {
- b[k++] = num[i++];
- }
- while(j <= r)
- {
- b[k++] = num[j++];
- }
- for(int i=l; i<=r; i++)
- {
- num[i] = b[i-l+];
- }
- return ans;
- }
- int Merge(int l,int r,ll &ans)
- {
- int mid = (l+r)/;
- if(l == r)
- return ;
- Merge(l,mid,ans);
- Merge(mid+,r,ans);
- ans += Mersort(l,r);
- }
- int main()
- {
- while(~scanf("%d",&n) && n)
- {
- for(int i=; i<=n; i++)
- scanf("%d",&num[i]);
- ll ans = ;
- Merge(,n,ans);
- printf("%lld\n",ans);
- }
- }
- ②树状数组:(要注意离散,离散可以二分,也可以map)
- #include<cstdio>
- #include<iostream>
- #include<algorithm>
- #include<string.h>
- using namespace std;
- const int maxn = 5e5+;
- int n;
- int tree[maxn];
- typedef long long ll;
- int lowbit(int x)
- {
- return x&(-x);
- }
- void add(int x)
- {
- for(int i=x;i<=n;i+=lowbit(i))
- {
- tree[i]++;
- }
- }
- int Query(int x)
- {
- int ans = ;
- for(int i=x;i>;i-=lowbit(i))
- {
- ans+=tree[i];
- }
- return ans;
- }
- int query(int x,int n,int *b)
- {
- return lower_bound(b+,b++n,x) - b;
- }
- int main()
- {
- while(~scanf("%d",&n) && n)
- {
- memset(tree,,sizeof(tree));
- int a[n+],b[n+];
- for(int i=;i<=n;i++)scanf("%d",&a[i]),b[i] = a[i];
- sort(b+,b++n);
- int len = unique(b+,b++n)-b-;
- ll ans = ;
- for(int i=;i<=n;i++)
- {
- int pos = query(a[i],len,b);
- add(pos);
- ans += pos - - Query(pos-);
- }
- printf("%lld\n",ans);
- }
- }
Ultra-QuickSort POJ - 2299 (逆序对)的更多相关文章
- POJ 2299 逆序对
Crossings Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463 Description I ...
- POJ 1804 逆序对数量 / 归并排序
Brainman Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12175 Accepted: 6147 Descrip ...
- poj 2299 逆序数
http://poj.org/problem?id=2299 坑:答案是long long 输出……!!!!! 题意是:求一个数组进行冒泡排序交换的次数 题解:求逆序数 题解Ⅰ: 归并排序求逆序数 归 ...
- poj2299——逆序对
题目:http://poj.org/problem?id=2299 逆序对,注意树状数组维护后缀和. 代码如下: #include<iostream> #include<cstdio ...
- 【POJ】2299 Ultra-QuickSort(逆序对)
http://poj.org/problem?id=2299 在两个元素相同的数列里,其中一个数列要移动到另一个数列相同元素相同的位置,那么要移动的次数就是这个数列关于另一个数列的逆序对数(hash后 ...
- 树状数组求逆序对:POJ 2299、3067
前几天开始看树状数组了,然后开始找题来刷. 首先是 POJ 2299 Ultra-QuickSort: http://poj.org/problem?id=2299 这题是指给你一个无序序列,只能交换 ...
- POJ 2299 Ultra-QuickSort 离散化加树状数组求逆序对
http://poj.org/problem?id=2299 题意:求逆序对 题解:用树状数组.每读入一个数x,另a[x]=1.那么a数列的前缀和s[x]即为x前面(或者说,再x之前读入)小于x的个数 ...
- POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)
POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...
- poj 2299 树状数组求逆序对数+离散化
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 54883 Accepted: 20184 ...
- Poj 2299 - Ultra-QuickSort 离散化,树状数组,逆序对
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 52306 Accepted: 19194 ...
随机推荐
- HTML阻止冒泡事件的发生
阻止事件冒泡函数(低级标签的点击事件触发后,上级标签的点击事件再触发,此函数就是防止冒泡事件发生) function stopEventBubble(event){ var e=event || wi ...
- bat脚本(转)
偶尔用到,搜到不错的资料,所以转一下: windows bat脚本for循环中对变量循环赋值 http://blog.csdn.net/u010161379/article/details/50956 ...
- Confluence 6 找到在创建 XML 备份的时候出现的错误
错误可能是因为数据库突然不可访问而产生.如果你在你的日志中看到了错误 'Couldn't backup database data' ,这个指南将会帮助你更正这个错误.我们强烈推荐你备份 Confl ...
- vuex action 与mutations 的区别
面试没说清楚.这个太丢人回来整理下: 事实上在 vuex 里面 actions 只是一个架构性的概念,并不是必须的,说到底只是一个函数,你在里面想干嘛都可以,只要最后触发 mutation 就行.异步 ...
- Netty简单聊天室
1.创建maven项目,在pom.xml中引入netty的jar包 <project xmlns="http://maven.apache.org/POM/4.0.0" xm ...
- ZenMap扫描笔记
1.软件界面如下,ZenMap 扫描工具是kali linu中对WEB渗透扫描的一款工具
- Eclipse搭建C++\C开发环境
1.最近使用visualStudio IDE开发Unity 3D使用的编程语言是C#但是发现visualStudio12 版本在自己主机上运行速度比够快,怀疑是不是处理器或者是版本问题,所以该卸载了, ...
- poj1155 依赖背包
/* 依赖背包 dp[i][j]表示i结点为根的树选择j个用户时的最大剩余费用 即背包容量是j,价值是最大费用 */ #include<iostream> #include<cstr ...
- python+selenium十四:xpath和contains模糊匹配
xpath可以以标签定位,也可以@任意属性: 如:以input标签定位:driver.find_element_by_xpath("//input[@id='kw']") 如:@t ...
- vue和stylus在subline中显示高亮
首先: 安装这两个插件 Vue Syntax Highlight 和 stylus 1.按住 ctrl + shift + p 2.输入:install Package 3.输入: V ...