题目链接

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

题目a[i]范围9e9+9,emmm,似乎会爆空间,似乎写过类似题,似乎必须离散化,似乎就这么写了。。。。。。

然后用树状数组解题并解出逆序数就...巴拉巴拉

/************************************************
┆ ┏┓   ┏┓ ┆
┆┏┛┻━━━┛┻┓ ┆
┆┃       ┃ ┆
┆┃   ━   ┃ ┆
┆┃ ┳┛ ┗┳ ┃ ┆
┆┃       ┃ ┆
┆┃   ┻   ┃ ┆
┆┗━┓   ┏━┛ ┆
┆  ┃   ┃  ┆      
┆  ┃   ┗━━━┓ ┆
┆  ┃  AC代马   ┣┓┆
┆  ┃    ┏┛┆
┆  ┗┓┓┏━┳┓┏┛ ┆
┆   ┃┫┫ ┃┫┫ ┆
┆   ┗┻┛ ┗┻┛ ┆
************************************************ */
//#include<bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <string>
#include <ctype.h>
#include <map>
#include <vector>
#include <set>
using namespace std;
const int maxn=5e5+;
int Tree[maxn],b[maxn];
struct node
{
int val,pos;
}a[maxn];
bool cmp(node x,node y)
{
return x.val<y.val;
}
inline int lowbit(int x) //计算2^k
{
return (x&-x);
}
void add(int x,int value)
{
for(int i=x; i<maxn; i+=lowbit(i))
Tree[i]+=value;
}
int get(int x)
{
int sum=;
for(int i=x; i>; i-=lowbit(i))
sum+=Tree[i];
return sum;
}
int main()
{
int n;
while(~scanf("%d",&n)&&n)
{
memset(Tree,,sizeof(Tree));
for(int i=;i<=n;i++)
{
scanf("%d",&a[i].val);;
a[i].pos=i;
}
sort(a+,a+n+,cmp);
for(int i=;i<=n;i++) b[a[i].pos]=i;
long long ans=;
for(int i=;i<=n;i++)
{
add(b[i],);
ans+=i-get(b[i]);
}
printf("%I64d\n",ans);
}
return ;
}

Ultra-QuickSort (POJ 2299)树状数组+离散化的更多相关文章

  1. POJ 2299 树状数组+离散化求逆序对

    给出一个序列 相邻的两个数可以进行交换 问最少交换多少次可以让他变成递增序列 每个数都是独一无二的 其实就是问冒泡往后 最多多少次 但是按普通冒泡记录次数一定会超时 冒泡记录次数的本质是每个数的逆序数 ...

  2. poj 2299 树状数组求逆序数+离散化

    http://poj.org/problem?id=2299 最初做离散化的时候没太确定可是写完发现对的---由于后缀数组学的时候,,这样的思维习惯了吧 1.初始化as[i]=i:对as数组依照num ...

  3. poj 2299 树状数组求逆序对数+离散化

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 54883   Accepted: 20184 ...

  4. POJ 2299树状数组求逆序对

    求逆序对最常用的方法就是树状数组了,确实,树状数组是非常优秀的一种算法.在做POJ2299时,接触到了这个算法,理解起来还是有一定难度的,那么下面我就总结一下思路: 首先:因为题目中a[i]可以到99 ...

  5. Ultra-QuickSort POJ - 2299 树状数组求逆序对

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

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

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

  7. hdu4605 树状数组+离散化+dfs

    Magic Ball Game Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  8. BZOJ_5055_膜法师_树状数组+离散化

    BZOJ_5055_膜法师_树状数组+离散化 Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然 ...

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

    http://poj.org/problem?id=2299 题意:给出一组数,求逆序对. 思路: 这道题可以用树状数组解决,但是在此之前,需要对数据进行一下预处理. 这道题目的数据可以大到999,9 ...

随机推荐

  1. Strand Specific mRNA sequencing 之重要性与分析

    Strand Specific mRNA sequencing 之重要性与分析 发表评论 2,761 A+ 所属分类:Bioinformatics   收  藏 研究生物基因转录体的方法有许多种,而使 ...

  2. code4906 删数问题

    题目: 键盘输入一个高精度的正整数n(<=240位), 去掉任意s个数字后剩下的数字按原左右次序将组成一个新的正整数. 编程对给定的n和s,寻找一种方案,使得剩下的数最小. Simple Inp ...

  3. Laravel 本地化定义

    1.配置本地化语言Laravel 的本地化语言配置项位于config/app.php: [php] view plain copy 'locale' => 'zh',//当前语言 'fallba ...

  4. 【Sikuli】Sikuli 文档

    http://sikulix-2014.readthedocs.io/en/latest/index.html

  5. db2 托管事务未设置方法有问题

    https://q.cnblogs.com/q/63862

  6. 编译 link

    --generating dsym file change the appropriate one from 'DWARF with dSYM file' to just 'DWARF',This s ...

  7. 二进制搭建kubernetes多master集群【一、使用TLS证书搭建etcd集群】

    上一篇我们介绍了kubernetes集群架构以及系统参数配置,参考:二进制搭建kubernetes多master集群[开篇.集群环境和功能介绍] 下面本文etcd集群才用三台centos7.5搭建完成 ...

  8. yii框架场景的用法

    1.在 model 里面定义一下场景 类名必须是 scenarios() public function scenarios() { return [ 'create' => ['title', ...

  9. javascript 字符数组转换成以逗号隔开的字符串

    var ids = [];angular.forEach(pulsarServers,function (server) { ids.push(server.id);});ids = ids.join ...

  10. Java带图片预览功能的图片上传兼容火狐ie

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...