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 ...
随机推荐
- 本文转自 MyEclipse 2015反编译插件安装
本文转自MyEclipse 2015反编译插件安装 分享一下下载插件的地址,百度网盘:链接:http://pan.baidu.com/s/1nturiAH 密码:yk73 其次:我来说下具体操作步骤: ...
- vue的单选框
- NIO(三)
使用直接缓冲区完成文件的复制(内存映射文件) package com.cppdy.nio; import java.nio.MappedByteBuffer; import java.nio.chan ...
- EasyUI Layout 布局
1.在整个页面上创建布局(Layout) <!DOCTYPE html> <html> <head> <title>吹泡泡的魚-主页</title ...
- Linux磁盘与文件系统管理笔记
### Linux磁盘与文件系统管理 linux 最传统的文件系统格式是EXT2,centos7 默认文件系统是xfs(日志式文件系统) 磁盘的组成: 盘片 机械手臂 主轴马达 (机械硬盘) 磁盘格式 ...
- LeetCode(100):相同的树
Easy! 题目描述: 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1 / \ / \ 2 3 2 3 ...
- CF1000G
蜜汁树形dp... 首先分析一下:他要求一条边至多只能经过两次,那么很容易会发现:从x到y这一条路径上的所有边都只会被经过一次.(如果过去再回来那么还要过去,这样就三次了,显然不合法) 那么其他能产生 ...
- 2017-2018-2 20165314实验二《Java面向对象程序设计》实验报告
实验报告封面 实验一 实验要求 参考 http://www.cnblogs.com/rocedu/p/6371315.html#SECUNITTEST 完成单元测试的学习提交最后三个JUnit测试用例 ...
- MySQL监控系统Lepus的搭建
现在流行的监控系统很多,选择一个合适自己的就可以了,例如Zabbix.Nagios:监控MySQL为主的有MySQLMTOP.Lepus.本文主要介绍快速部署lepus以及监控MySQL,因为作为DB ...
- python内置的魔术命令(builtin magic commands)
在ipython或者jupyter notebook中,会出现"%"开头并且一个很短的命令,例如交互式的matlablib绘图: %matplotlib inline 之前一直不知 ...