Problem Description
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
 
 
很有意思的一道题,首先看图片就像一个通马桶的工具。
这里只提及树状数组。
 
 
这道题考的考点数据的处理,逆序数
所以思路来了,逆序数不就比大小吗,直接就标上序号,来一个排序加上数据处理,OK! 
数据处理的方法网上一般称之为离散化,我对离散化的理解就是简化问题,使一个连续(不可解)的问题变得离散(可解)。
本题考的就是数据,直接加和会爆炸。于是用123......n,表示这些数的价值就变得可解,这个过程算是离散化。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define MAX 500050
typedef long long ll;
int a[MAX];
int c[MAX]; struct node
{
int num;
ll v;
bool operator < (const node &b ) const //重载一下运算符,这里的const可加可不加,对于不同编译器是有区别的
{
return v<b.v;
} }b[MAX];
int lowbit(int i)
{
return i&(-i);
}
void add(int x,int v)
{
while(x<=MAX)
{
c[x]+=v;
x+=lowbit(x);
}
}
int sum(int x)
{
int res=0;
while(x>0)
{
res+=c[x];
x-=lowbit(x);
}
return res;
}
int main()
{
int n;
while(scanf("%d",&n),n)
{
for(int i=1;i<=n;i++)
{
scanf("%d",&b[i].v); b[i].num=i;
} sort(b+1,b+n+1); //值排序
memset(a,0,sizeof(a));
a[b[1].num]=1; //对于最小值当然标最小啦
ll ans=0; for(int i=2;i<=n;i++)
{
if(b[i].v==b[i-1].v)
a[b[i].num]=a[b[i-1].num];
else
a[b[i].num]=i; // 记录前面比他小的数。
}
memset(c,0,sizeof(c));
for(int i=1;i<=n;i++)
{
add(a[i],1);
ans+=sum(n)-sum(a[i]); }
printf("%lld\n",ans);
}
}

  

 

POJ-2299 Ultra-QuickSort (树状数组,离散化,C++)的更多相关文章

  1. POJ 2299 Ultra-QuickSort(树状数组+离散化)

    http://poj.org/problem?id=2299 题意:给出一组数,求逆序对. 思路: 这道题可以用树状数组解决,但是在此之前,需要对数据进行一下预处理. 这道题目的数据可以大到999,9 ...

  2. POJ - 2299 Ultra-QuickSort 【树状数组+离散化】

    题目链接 http://poj.org/problem?id=2299 题意 给出一个序列 求出 这个序列要排成有序序列 至少要经过多少次交换 思路 求逆序对的过程 但是因为数据范围比较大 到 999 ...

  3. poj 2299 Ultra-QuickSort(树状数组求逆序数+离散化)

    题目链接:http://poj.org/problem?id=2299 Description In this problem, you have to analyze a particular so ...

  4. poj 2299 Ultra-QuickSort(树状数组求逆序数)

    链接:http://poj.org/problem?id=2299 题意:给出n个数,求将这n个数从小到大排序,求使用快排的需要交换的次数. 分析:由快排的性质很容易发现,只需要求每个数的逆序数累加起 ...

  5. poj 2299 Ultra-QuickSort(树状数组)

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 67681   Accepted: 25345 ...

  6. POJ 2299 Ultra-QuickSort【树状数组 ,逆序数】

    题意:给出一组数,然后求它的逆序数 先把这组数离散化,大概就是编上号的意思--- 然后利用树状数组求出每个数前面有多少个数比它小,再通过这个数的位置,就可以求出前面有多少个数比它大了 这一篇讲得很详细 ...

  7. POJ 2299 【树状数组 离散化】

    题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...

  8. hdu4605 树状数组+离散化+dfs

    Magic Ball Game Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  9. BZOJ_5055_膜法师_树状数组+离散化

    BZOJ_5055_膜法师_树状数组+离散化 Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然 ...

  10. POJ 2299 树状数组+离散化求逆序对

    给出一个序列 相邻的两个数可以进行交换 问最少交换多少次可以让他变成递增序列 每个数都是独一无二的 其实就是问冒泡往后 最多多少次 但是按普通冒泡记录次数一定会超时 冒泡记录次数的本质是每个数的逆序数 ...

随机推荐

  1. Spring中Druid链接池的配置

    本文记录了使用Druid的方法, 包括Spring和Spring boot中使用Druid的配置方法. Spring中配置Druid连接池 以链接mysql为例 1 添加druid依赖 <dep ...

  2. 刚安装Fedora 23工作站后,你必须要做的24件事

    [51CTO.com快译]Fedora 23工作站版本已发布,此后我们就一直在密切关注它.我们已经为新来读者介绍了一篇安装指南:<Fedora 23工作站版本安装指南> 还有一篇介绍如何从 ...

  3. 【Tomcat】shell 部署配置 war包

    使用shell 一次执行,将项目中的war包的配置全部修改 #!/bin/bash #----------------------------------------------- # FileNam ...

  4. Redis集群之优化系统参数

    1.最大打开文件数量 (1)编辑资源限制文件,针对redis用户做资源访问控制,在文件尾加入最后两行, sudo vim /etc/security/limits.conf (2) sudo vim ...

  5. 浅酌iOS 11兼容性

    WeTest导读 苹果在WWDC2017大会,公布了最新的iOS 11,系统新特性肯定是让不少果粉充满期待.在网上已能看到不少关于iOS 11的体验文章,那么iOS 11的新特性会对APP产生什么兼容 ...

  6. open文件操作之mode模式剖析

    Python可以使用open函数来实现文件的打开,关闭,读写操作: Python3中的open函数定义为: open(file, mode='r', buffering=None, encoding= ...

  7. JSP慕课网之application、page、pageContext、config、exception

    接下来使用getSession().forward().include()方法. 跳转的include.jsp页面:

  8. sublime中css输入分号后自动提示的烦恼

    sublime开发前端确实好用,有好多些个的插件,轻量便捷,但是在使用sublime中的一些插件的时候总是会遇到困扰,跟自己想象中的不一样,比如在使用SublimeCodeIntel插件的时候,就会遇 ...

  9. keepalive之LVS-DR架构

    author:JevonWei 版权声明:原创作品 Keepalive实战之LVS-DR 实验目的:构建LVS-DR架构,为了达到LVS的高可用目的,故在LVS-DR的Director端做Keepal ...

  10. 高阶自定义View --- 粒子变幻、隧道散列、组合文字

    高阶自定义View --- 粒子变幻.隧道散列.组合文字 作者:林冠宏 / 指尖下的幽灵 掘金:https://juejin.im/user/587f0dfe128fe100570ce2d8 博客:h ...