In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequence
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

The input contains several test cases. Every test case begins with a line that contains a single integer n < 500,000 -- the length of the input sequence. Each of the the following n lines contains a single integer 0 ≤ a[i] ≤ 999,999,999, the i-th input sequence element. Input is terminated by a sequence of length n = 0. This sequence must not be processed.

Output

For every input sequence, your program prints a single line containing an integer number op, the minimum number of swap operations necessary to sort the given input sequence.

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 (逆序对)的更多相关文章

  1. POJ 2299 逆序对

    Crossings Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463 Description I ...

  2. POJ 1804 逆序对数量 / 归并排序

    Brainman Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12175   Accepted: 6147 Descrip ...

  3. poj 2299 逆序数

    http://poj.org/problem?id=2299 坑:答案是long long 输出……!!!!! 题意是:求一个数组进行冒泡排序交换的次数 题解:求逆序数 题解Ⅰ: 归并排序求逆序数 归 ...

  4. poj2299——逆序对

    题目:http://poj.org/problem?id=2299 逆序对,注意树状数组维护后缀和. 代码如下: #include<iostream> #include<cstdio ...

  5. 【POJ】2299 Ultra-QuickSort(逆序对)

    http://poj.org/problem?id=2299 在两个元素相同的数列里,其中一个数列要移动到另一个数列相同元素相同的位置,那么要移动的次数就是这个数列关于另一个数列的逆序对数(hash后 ...

  6. 树状数组求逆序对:POJ 2299、3067

    前几天开始看树状数组了,然后开始找题来刷. 首先是 POJ 2299 Ultra-QuickSort: http://poj.org/problem?id=2299 这题是指给你一个无序序列,只能交换 ...

  7. POJ 2299 Ultra-QuickSort 离散化加树状数组求逆序对

    http://poj.org/problem?id=2299 题意:求逆序对 题解:用树状数组.每读入一个数x,另a[x]=1.那么a数列的前缀和s[x]即为x前面(或者说,再x之前读入)小于x的个数 ...

  8. POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)

    POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...

  9. poj 2299 树状数组求逆序对数+离散化

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 54883   Accepted: 20184 ...

  10. Poj 2299 - Ultra-QuickSort 离散化,树状数组,逆序对

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 52306   Accepted: 19194 ...

随机推荐

  1. textarea中的回车识别问题

    <textarea name="" id="aa" cols="30" rows="10" wrap=" ...

  2. log4net使用的两种方式

    1.首先添加log4net.dll引用(可以使用  管理NuGet程序包添加引用,也可以下载下来手动去添加引用) 2.在app.config文件中配置 3.log4net使用的2终方式 log4net ...

  3. Confluence 6 使用 Decorator 宏

    Decorator 宏(Macros)是 Velocity  宏.这个宏可以被用来在页面编辑 Custom decorators 中创建复杂或者可变的部分,例如菜单,页面其他部分等.Decorator ...

  4. Confluence 6 布局高级自定义

    重载 Velocity 模板 velocity 目录是 Confluence Velocity 模板文件进行搜索时候需要的文件夹.例如,你可以通过将你的 Velocity 文件使用正确的文件名放置到正 ...

  5. centos7查看yum安装的软件及路径

    rpm -qa 查看所有已安装软件名称 rpm -ql 软件名 显示软件的安装路径

  6. 好用的JS拖拽插件

    下载artDialog插件的时候发现它把拖拽单独封装成了一个方法,挺好用的,使用方法如下... 第一种拖拽方式-点击容器指定区域进行拖拽 $('.ui-dialog').on(DragEvent.ty ...

  7. 最长上升子序列(dp)

    链接:https://www.nowcoder.com/questionTerminal/d83721575bd4418eae76c916483493de来源:牛客网 广场上站着一支队伍,她们是来自全 ...

  8. react 使用draft.js富文本编辑器

    参照网址:https://www.cnblogs.com/3body/p/6224010.html 参看网址:https://www.cnblogs.com/mosquito18/p/9787816. ...

  9. Centos6.8部署jumpserver(完整版)

    环境: 系统 Centos6.8 IP:192.168.66.131 关闭selinux和防火墙 # 修改字符集,否则可能报 input/output error的问题,因为日志里打印了中文 # lo ...

  10. HTMLTestRunner 美化版本

    前言 ​最近小伙伴们在学玩python,,看着那HTMLTestRunner生成的测试报告,左右看不顺眼,终觉得太丑.搜索了一圈没有找到合适的美化报告,于是忍不住自已动手进行了修改,因习惯python ...