题目链接:http://poj.org/problem?id=2299

Ultra-QuickSort
Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 75831   Accepted: 28402

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

Source

Waterloo local 2005.02.05
题意:找到给定数组中有多少逆序对
思路:树状数组离散化,存一个找一次 不需要全部存进去再找(这样是找不出来的) 存的时候就找,这样就知道有多少个是逆序的了
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
typedef long long LL;
const LL mod=1e9+7;
const LL INF=1e9+7;
const int maxn=5e5+50;
LL a[maxn],b[maxn],c[maxn];
LL len;
LL lowbit(LL x)
{return x&(-x);
}
LL query(LL x)
{LL ret=0;while(x>0){ret+=c[x];x-=lowbit(x);}return ret;
}
void update(LL x)
{while(x<=500000){c[x]++;x+=lowbit(x);}
}
int main()
{LL N;while(scanf("%lld",&N)!=EOF){if(N==0) break;memset(a,0,sizeof(a));memset(b,0,sizeof(b));memset(c,0,sizeof(c));LL ans=0;for(int i=1;i<=N;i++){scanf("%lld",&a[i]);b[i]=a[i];}sort(b+1,b+N+1);len=unique(b+1,b+N+1)-b;for(int i=1;i<=N;i++){//        cout<<"*"<<endl;LL x=lower_bound(b+1,b+len+1,a[i])-b;//找到a[i]在b数组中所在位置ans+=i-query(x-1)-1;//query找在x之前的数有多少个  这些都是顺序对的
            update(x);}printf("%lld\n",ans);}return 0;
}

转载于:https://www.cnblogs.com/caijiaming/p/10896713.html

Ultra-QuickSort(离散化)相关推荐

  1. 【逆序对】Ultra - Quicksort

    POJ 2299 Ultra-QuickSort 只允许交换,比较相邻的元素, 求最少多少次交换可以使得序列有序 冒泡排序的次数-->数列中逆序对的个数减1-->最终为0 -->答案 ...

  2. H - Parity game-poj1733(需要离散化)

    题意:给一个序列这个序列都是由0和1组成,现在随意拿出来一个序列,然后说出他的和是奇数还是偶数,因为有可能存在假话,让你判断前多少条没有假话,也就是查找第一个假话的位置-1 // 这道题很像D题,都是 ...

  3. usaco Picture(离散化求线段周长)

    usac前面有一题是递归求矩形覆盖面积的,学到不少东西 离散化 把所有矩形离散化(就是将整个平面分成许多"竖条"或"横条",对其操作),每个矩形都由四条边组成, ...

  4. 线性连续时间状态空间模型的离散化及实例

    线性连续时间状态空间模型的离散化(Discretization of Linear Continuous-Time State-Space Models) 1 .状态空间模型 非线性连续时间状态空间模 ...

  5. HDU 6229 Wandering Robots 找规律+离散化

    题目链接:Wandering Robots 题解:先讲一下规律,对于每一个格子它可以从多少个地方来有一个值(可以从自己到自己),然后答案就是统计合法格子上的数与所有格子的数的比值 比如说样例的3 0格 ...

  6. pandas数据预处理(标准化归一化、离散化/分箱/分桶、分类数据处理、时间类型数据处理、样本类别分布不均衡数据处理、数据抽样)

    1. 数值型数据的处理 1.1 标准化&归一化 数据标准化是一个常用的数据预处理操作,目的是处理不同规模和量纲的数据,使其缩放到相同的数据区间和范围,以减少规模.特征.分布差异等对模型的影响. ...

  7. pandas高级处理-数据离散化

    pandas高级处理-数据离散化 1 为什么要离散化 连续属性离散化的目的是为了简化数据结构,数据离散化技术可以用来减少给定连续属性值的个数.离散化方法经常作为数据挖掘的工具.[简化数据,让数据用起来 ...

  8. P1502 窗口的星星 离散化+扫描线

    题意: 一个二维平面上有些点有权值. 问给你一个H*W的窗口,问窗口星星亮度总和的最大值. H∗W范围:1e6H*W 范围:1e6H∗W范围:1e6 坐标范围:1e9坐标范围:1e9坐标范围:1e9 ...

  9. poj2528贴海报(线段树离散化)

    //poj2528贴海报(线段树离散化) #include<cstring> #include<iostream> #include<cstdio> #includ ...

  10. POJ Mayor's posters——线段树+离散化

    原文:http://blog.163.com/cuiqiongjie@126/blog/static/85642734201261151553308/ 大致题意: 有一面墙,被等分为1QW份,一份的宽 ...

最新文章

  1. c++输出的值精确到小数点后5位_C的探查之路05-基本类型
  2. 化整为零,一步一步教你搭建Prometheus监控报警系统
  3. 《R语言实战》第1章
  4. 函数计算是如何工作的?
  5. [转].NET 数字格式化:忽略末尾零
  6. 每天一道LeetCode-----寻找地增序列中第一个大于等于目标元素的位置
  7. 小程序学习(1):微信开发者工具安装
  8. java 多态_Java的多态
  9. 深度学习TF—8.经典CNN模型—LeNet-5、VGG13、AlexNet、GoogLeNet、ResNet、DenseNet
  10. MVC系列博客之排球计分(六)Controller的实现(二)
  11. C#温故而知新学习系列之面向对象编程—自动属性(十一)
  12. Image Enhancement
  13. 电影《Green book》观后感_已补全:携带着种族歧视的“光环”,艰难地获得朋友的相互依赖,依然得享受生活的酸甜苦咸。...
  14. 西门子三开接线图解_西门子二开三控开关接线最好有图
  15. vim 删除重复部分的行 vim 技巧
  16. 足球视频AI(一)——位置与平面坐标的转换
  17. win7系统双硬盘双系统问题解决
  18. uvalive 3523 Knights of the Round Table 圆桌骑士(强连通+二分图)
  19. JAVA练习243-唯一摩尔斯密码词
  20. 解决无线网卡打不开的问题(by quqi99)

热门文章

  1. 发布一个域安全级别的无代码InfoPath表单作为文档库模版 (InfoPath 一)
  2. Python语言importError:cannot import name ‘InvalidArgumentException‘报错的解决方法:
  3. 【干货】2021新消费品牌STEP增长方法论:品牌营销与生意增长Playbook.pdf(附下载链接)...
  4. 【报告分享】5G时代新型基础设施建设白皮书.pdf
  5. 【报告分享】2019年12月郭广昌混沌课程ppt(附下载链接)
  6. 【岗位详情】腾讯广告后台开发工程师(北京)
  7. Leetcode每日一题:36.valid-sudoku(有效的数独)
  8. VScode快捷键(持续更新)
  9. LeetCode:10.regular-expression-matching(正则式表达)
  10. Stanford CS230深度学习(七)RNN和LSTM