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. java api1.8中文版(由谷歌,百度,有道,必应翻译).md

    大家如果想看java1.8 的中文api可以点击我下面提供的链接,同时感谢把这些资源放到网上供我们学习的朋友,废话不说了下面就是链接: java 1.6 帮助文档 中文 链接:http://downl ...

  2. 利用CSS hover伪类改变其他元素的总结

    :hover 伪类经常用于页面的一些鼠标交互.链接点击变化,增强页面的用户体验,但是可以用来改变其他元素样式,可以在不使用JS 的情况下,达到想要的页面效果. 1.hover改变自身的效果: 鼠标悬浮 ...

  3. 谈谈Golang中goroutine的调度问题

    goroutine的调度问题,同样也是我之前面试的问题,不过这个问题我当时并不是很清楚,回来以后立马查阅资料,现整理出来备忘. 有一些预备知识需要说明,就是操作系统中的线程.操作系统中的线程分为两种: ...

  4. CRF技能词识别过程

    最近在用CRF做未登录技能词识别,虽然艰难,但是感觉很爽,效率非常高. (1)数据准备: 选取30000行精语料作为训练数据.每一个br作为一条数据.使用已有的技能词典对数据进行无标注分词. (2)训 ...

  5. hdu3713 Double Maze

    Problem Description Unlike single maze, double maze requires a common sequence of commands to solve ...

  6. JavaScript封装一个实用的select控件

    最近一直把精力放在项目上面,导致忽略的一些底层的东西.以前就一直觉得原有的select控件很丑,正好周末有时间,试着做了一个简单封装,实现了它的基本功能.我总结了一下,大概分为三个部分: 1.对显示样 ...

  7. js,jQuery实现可关闭悬浮框广告特效,兼容(谷歌,火狐,Ie)

    注意不能直接用close()命名关闭广告函数,避免冲突. javascript实现方法: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tr ...

  8. python学习总结(函数进阶)

    -------------------程序运行原理------------------- 1.模块的内建__name__属性,主模块其值为__main__,导入模块其值为模块名     1.创建时间, ...

  9. 字符编码知识简介和iconv函数的简单使用

    字符编码知识简介和iconv函数的简单使用 字符编码知识简介 我们知道,在计算机的世界其实只有0和1.期初计算机主要用于科学计算,而我们知道一个数,除了用我们常用对10进制表示,也可以用2进制表示,所 ...

  10. python中字符串中一些函数的用法

    1..capitalize():字符串的首字母大写: 2..count():字符串中的某个字母的个数: 3..center(50,'-'):对象居中,且左右用'-'补齐: 4..encode():吧字 ...