Ultra-QuickSort (POJ 2299)树状数组+离散化
题目链接
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
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
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)树状数组+离散化的更多相关文章
- POJ 2299 树状数组+离散化求逆序对
给出一个序列 相邻的两个数可以进行交换 问最少交换多少次可以让他变成递增序列 每个数都是独一无二的 其实就是问冒泡往后 最多多少次 但是按普通冒泡记录次数一定会超时 冒泡记录次数的本质是每个数的逆序数 ...
- poj 2299 树状数组求逆序数+离散化
http://poj.org/problem?id=2299 最初做离散化的时候没太确定可是写完发现对的---由于后缀数组学的时候,,这样的思维习惯了吧 1.初始化as[i]=i:对as数组依照num ...
- poj 2299 树状数组求逆序对数+离散化
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 54883 Accepted: 20184 ...
- POJ 2299树状数组求逆序对
求逆序对最常用的方法就是树状数组了,确实,树状数组是非常优秀的一种算法.在做POJ2299时,接触到了这个算法,理解起来还是有一定难度的,那么下面我就总结一下思路: 首先:因为题目中a[i]可以到99 ...
- Ultra-QuickSort POJ - 2299 树状数组求逆序对
In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a seque ...
- POJ 2299 【树状数组 离散化】
题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...
- hdu4605 树状数组+离散化+dfs
Magic Ball Game Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- BZOJ_5055_膜法师_树状数组+离散化
BZOJ_5055_膜法师_树状数组+离散化 Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然 ...
- POJ 2299 Ultra-QuickSort(树状数组+离散化)
http://poj.org/problem?id=2299 题意:给出一组数,求逆序对. 思路: 这道题可以用树状数组解决,但是在此之前,需要对数据进行一下预处理. 这道题目的数据可以大到999,9 ...
随机推荐
- process_进程池
from multiprocessing import Poolimport os,timeimport multiprocessingdef func(msg): print("msg:& ...
- 去掉字符串前后的空格和Tab
<script language=Javascript>function String.prototype.Trim() {return this.replace(/(^\s*)|(\s* ...
- Mac下PHP+MySQL+Apache2环境搭建
本机系统信息如下: -------------------------------------------------------------------------------------- OS: ...
- 551. Student Attendance Record I
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- 2018.09.11 poj1845Sumdiv(质因数分解+二分求数列和)
传送门 显然需要先求出ab" role="presentation" style="position: relative;">abab的所有质因 ...
- 安装hue-3.11.0
1) 配置HDFS HttpFS和WebHDFS 如果HDFS是HA方式部署的,则只能使用HttpFS,而不能用WebHDFS. 2) 安装依赖: apr-iconv-1.2.1 confuse-3. ...
- 201709013工作日记--Android异步通信AsyncTask
参考相关博客:http://blog.csdn.net/liuhe688/article/details/6532519 在Android中实现异步任务机制有两种方式,Handler和AsyncTas ...
- struts2从浅至深(三)拦截器
一:拦截器概述 Struts2中的很多功能都是由拦截器完成的. 是AOP编程思想的一种应用形式. 二:拦截器执行时机 interceptor表示 ...
- hdu 5024 最长的L型
http://acm.hdu.edu.cn/showproblem.php?pid=5024 找到一个最长的L型,L可以是斜着的 简单的模拟 #include <cstdio> #incl ...
- Billman_ford货币升值——正权回路
2240和1860那个题目很像啊 都是问货币能不能增多,钻社会制度得空子啊哈哈 唯一不同得是你的起点是任意一个点,这个比较麻烦了,多了一层循环嘞 处理货币名可以用map分配id 然后就是老套的Bill ...