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,表示这些数的价值就变得可解,这个过程算是离散化。
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4. #include<cstring>
  5. using namespace std;
  6. #define MAX 500050
  7. typedef long long ll;
  8. int a[MAX];
  9. int c[MAX];
  10.  
  11. struct node
  12. {
  13. int num;
  14. ll v;
  15. bool operator < (const node &b ) const //重载一下运算符,这里的const可加可不加,对于不同编译器是有区别的
  16. {
  17. return v<b.v;
  18. }
  19.  
  20. }b[MAX];
  21. int lowbit(int i)
  22. {
  23. return i&(-i);
  24. }
  25. void add(int x,int v)
  26. {
  27. while(x<=MAX)
  28. {
  29. c[x]+=v;
  30. x+=lowbit(x);
  31. }
  32. }
  33. int sum(int x)
  34. {
  35. int res=0;
  36. while(x>0)
  37. {
  38. res+=c[x];
  39. x-=lowbit(x);
  40. }
  41. return res;
  42. }
  43. int main()
  44. {
  45. int n;
  46. while(scanf("%d",&n),n)
  47. {
  48. for(int i=1;i<=n;i++)
  49. {
  50. scanf("%d",&b[i].v);
  51.  
  52. b[i].num=i;
  53. }
  54.  
  55. sort(b+1,b+n+1); //值排序
  56. memset(a,0,sizeof(a));
  57. a[b[1].num]=1; //对于最小值当然标最小啦
  58. ll ans=0;
  59.  
  60. for(int i=2;i<=n;i++)
  61. {
  62. if(b[i].v==b[i-1].v)
  63. a[b[i].num]=a[b[i-1].num];
  64. else
  65. a[b[i].num]=i; // 记录前面比他小的数。
  66. }
  67. memset(c,0,sizeof(c));
  68. for(int i=1;i<=n;i++)
  69. {
  70. add(a[i],1);
  71. ans+=sum(n)-sum(a[i]);
  72.  
  73. }
  74. printf("%lld\n",ans);
  75. }
  76. }

  

 

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. Could not establish trust relationship for the SSL/TLS secure channel 问题解决方法

    最近在写一个跟第三方对接的数据同步服务,在本地都没有问题,今天放到生产环境测试报错: System.Net.WebException: The underlying connection was cl ...

  2. 【Hibernate】版本错误 org/hibernate/Query : Unsupported major.minor version 52.0

    报错原因:jdk1.7不支持 hibernate的最新版本5.2.0,把hibernate的版本换成5.1.3或更早的版本. 补充:mysql-connector-java-6.0.x也不被hiber ...

  3. 基于Vue实现后台系统权限控制

    原文地址:http://refined-x.com/2017/08/29/基于Vue实现后台系统权限控制/,转载请注明出处. 用Vue/React这类双向绑定框架做后台系统再适合不过,后台系统相比普通 ...

  4. Django编写RESTful API(五):添加超链接提高模型间的关联性

    前言 在第四篇中,加入了用户模型,以及相关的认证和权限的功能.但是我们在使用的时候,会发现在访问http://127.0.0.1:8000/users/时看到的用户列表,不能够直接点击某个链接然后查看 ...

  5. TensorFlow框架(5)之机器学习实践

    1. Iris data set Iris数据集是常用的分类实验数据集,由Fisher, 1936收集整理.Iris也称鸢尾花卉数据集,是一类多重变量分析的数据集.数据集包含150个数据集,分为3类, ...

  6. Linux巩固记录(1) J2EE开发环境搭建及网络配置

    由于要近期使用hadoop等进行相关任务执行,操作linux时候就多了 以前只在linux上配置J2EE项目执行环境,无非配置下jdk,部署tomcat,再通过docker或者jenkins自动部署上 ...

  7. 迈向angularjs2系列(3):组件详解

    一: 以组件开发一个to-do list应用 todo组件分为导入.接口定义.顶层组件.控制器.启动5个部分. app.ts: //导入 import {Component} from '@angul ...

  8. Java基础---继承、抽象、接口

    一.概述         继承是面向对象的一个重要特征.当多个类中存在相同属性和行为时,将这些内容抽取到单独一个类中,那么多个类无需再定义这些属性和行为,只要继那个类即可.这时,多个类可以称为子类,单 ...

  9. [2016-09-09]IIS站点发布、同步和备份工具MSdeploy(WebDeploy)介绍

    前提准备:完整安装Microsoft Web Deploy 3 下载页面:WebDeploy_amd64_zh-CN.msi msdeploy 同步站点 命令所在目录C:\Program Files\ ...

  10. XWPFRun属性详解

    XWPFRun是XWPFDocument中的一段文本对象(就是一段文字) 创建文档对象 XWPFDocument docxDocument = new XWPFDocument(); 创建段落对象 X ...