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. 关于HttpSession

    HttpSession  使用Cookie有一个非常大的局限,就是如果Cookie很多,则无形的增加了客户端与服务端的数据传输量.而且由于浏览器对Cookie数量的限制,注定我们不能再Cookie中保 ...

  2. M4—按键识别

    三.KEY 3.1  初始化 1.相应端口时钟使能 2.配置GPIO为输入模式 3.根据实际电路图 配置浮空输入,不用上下拉 3.2  按键识别 (1)一般按键步骤(延时消抖) 1. 判断相关的管脚是 ...

  3. 【CentOS7】Could not retrieve mirrorlist http://mirrorlist.centos.org/?...

    在执行命令 sudo yum clean expire-cache 清理完过期的缓存后,再执行yum install 或 update命令都失败了.原因是清理过期缓存结果不该被清理的也删掉了,可能是y ...

  4. 【Ubuntu 16】DEB软件包管理

    一.背景介绍 开源软件最早的时候没有软件包和软件包管理器,用户只能下载源码包自行配置 编译 安装. 后来linux各发行版本推出了软件包格式和软件包管理程序 Red Hat.Centos使用RPM格式 ...

  5. Yii2 关于yii\db\Connection::open耗时很久的问题(1000ms左右)

    关于这个问题我纠结了很久,每次打开网页yii\db\Connection::open几乎都耗时1000ms. 其实这个问题很好解决:只要把config\db.php配置信息里的localhost,改成 ...

  6. 如何获取Azure Storage Blob的MD5值

    问题表述 直接使用CloudBlockBlob对象获取的Properties是空的,无法获取到对象的MD5值,后台并未进行属性值的填充 前提:blob属性本省包含md5值,某些方式上传的blob默认并 ...

  7. geotrellis使用(三十一)使用geotrellis直接将GeoTiff发布为TMS服务

    前言 传统上我们需要先将Tiff中存储的影像等数据先切割成瓦片,而后再对外提供服务.这样的好处是服务器响应快,典型的用空间来换时间的操作.然而这样造成的问题是空间的巨大浪费,一般情况下均需要存储1-1 ...

  8. 使用Java连接Redis

    下载redis的驱动包:Jedis.下载地址:https://mvnrepository.com/artifact/redis.clients/jedis/2.9.0 打开IDE,我使用的是Eclip ...

  9. spring mvc 处理流程整理

    1.  首先用户发送请求-->DispatcherServlet,前端控制器收到请求后自己不进行处理,而是委托给其他的解析器进行处理,作为统一访问点,进行全局的流程控制: 2.  Dispatc ...

  10. [2012-08-06]awk多文件合并并按文件名分段

    以下代码满足这样的需求: 多个文件内容合并到一个文件A中(如果没有下面这条,使用cat就能解决) 文件A中每段内容之前保留原先的文件名 awk 'tmp!=FILENAME{tmp=FILENAME; ...