Ultra-QuickSort
Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 53777   Accepted: 19766

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

题意:求解一个串的逆序数的个数是多少??

题解:离散化数组变成下标,然后每次将离散化的下标放进树状数组,放进去之后统计小于他的数的个数是多少。用 i - getsum(a[i])即为大于它的数的个数,其中 i 为当前已经插入的数的个数。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;
const int N = ; struct Node{
int v,id;
}node[N];
int a[N],c[N],n;
int lowbit(int x){
return x&(-x);
}
void update(int idx,int v){
for(int i=idx;i<=N;i+=lowbit(i)){
c[i]+=v;
}
}
int getsum(int idx){
int sum = ;
for(int i=idx;i>=;i-=lowbit(i)){
sum+=c[i];
}
return sum;
}
int cmp(Node a,Node b){
return a.v<b.v;
}
int main()
{
while(scanf("%d",&n)!=EOF,n){
memset(c,,sizeof(c));
for(int i=;i<=n;i++){
scanf("%d",&node[i].v);
node[i].id = i;
}
sort(node+,node+n+,cmp);
for(int i=;i<=n;i++){
a[node[i].id] = i;
}
long long cnt = ;
for(int i=;i<=n;i++){
update(a[i],);
cnt=cnt+ i - getsum(a[i]);
}
printf("%lld\n",cnt);
}
return ;
}

poj 2299(离散化+树状数组)的更多相关文章

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

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

  2. POJ 2299 Ultra-QuickSort (树状数组 && 离散化&&逆序)

    题意 : 给出一个数n(n<500,000), 再给出n个数的序列 a1.a2.....an每一个ai的范围是 0~999,999,999  要求出当通过相邻两项交换的方法进行升序排序时需要交换 ...

  3. POJ 2299 Ultra-QuickSort (树状数组 && 离散化)

    题意 : 给出一个数n(n<500,000), 再给出n个数的序列 a1.a2.....an每一个ai的范围是 0~999,999,999  要求出当通过相邻两项交换的方法进行升序排序时需要交换 ...

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

    In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a seque ...

  5. CodeForces 540E - Infinite Inversions(离散化+树状数组)

    花了近5个小时,改的乱七八糟,终于A了. 一个无限数列,1,2,3,4,...,n....,给n个数对<i,j>把数列的i,j两个元素做交换.求交换后数列的逆序对数. 很容易想到离散化+树 ...

  6. Ultra-QuickSort(归并排序+离散化树状数组)

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 50517   Accepted: 18534 ...

  7. HDU 5862 Counting Intersections(离散化+树状数组)

    HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...

  8. BZOJ_4627_[BeiJing2016]回转寿司_离散化+树状数组

    BZOJ_4627_[BeiJing2016]回转寿司_离散化+树状数组 Description 酷爱日料的小Z经常光顾学校东门外的回转寿司店.在这里,一盘盘寿司通过传送带依次呈现在小Z眼前.不同的寿 ...

  9. poj-----Ultra-QuickSort(离散化+树状数组)

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 38258   Accepted: 13784 ...

随机推荐

  1. Linux命令之---cd

    命令简介 Linux cd 命令是Linux中最基本的命令语句,其他的命令语句要进行操作,都是建立在使用 cd 命令上的. 命令格式 cd [目录名] 命令功能 切换当前目录至dirName 常用范例 ...

  2. SparkSQL查询程序的两种方法,及其对比

    import包: import org.apache.spark.{SparkConf, SparkContext}import org.apache.spark.rdd.RDDimport org. ...

  3. SSRS 报表管理器 http://localhost/Reports HTTP500 内部错误处理过程

    原文地址:http://www.cnblogs.com/zzry/p/5716056.html 安装了很多机器的sqlserverBI 组件 初始安装配置下 浏览报表管理器 http://localh ...

  4. jq阻止ajax进行多次提交

    在函数定义全局变量..var Stch=falseif (Stch==true){alert('请不要重新提交');}else{Stch=true;$.ajax({type:"POST&qu ...

  5. 【Distinct Subsequences】cpp

    题目: Given a string S and a string T, count the number of distinct subsequences of T in S. A subseque ...

  6. php 判断一个点是否在一个多边形区域内

    <?php class pointMap{ private static $coordArray; private static $vertx = []; private static $ver ...

  7. kickstart配置文件详解和system-config-kickstart

    kickstart是什么         许多系统管理员宁愿使用自动化的安装方法来安装红帽企业 Linux.为了满足这种需要,红帽创建了kickstart安装方法.使用kickstart,系统管理员可 ...

  8. [oldboy-django][6其他]navicat远程登录没有权限

    day6-17-1204 # 增加远程访问mysql的权限(就是其他ip地址远程访问另外一个ip地址的数据库) -- step1 修改配置文件,bind_address, 允许所有ip地址都可以访问m ...

  9. Tomcat源码分析(二)------ 一次完整请求的里里外外

    Tomcat源码分析(二)------ 一次完整请求的里里外外   前几天分析了一下Tomcat的架构和启动过程,今天开始研究它的运转机制.Tomcat最本质就是个能运行JSP/Servlet的Web ...

  10. poj 2251 Dungeon Master 3维bfs(水水)

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21230   Accepted: 8261 D ...