Ultra-QuickSort (POJ 2299)树状数组+离散化
题目链接
Description

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 ...
随机推荐
- HHVM中文网
HHVM中文网 百科资料 下载安装 HHVM文档 HHVM&Hack博客 <Hack与HHVM权威指南> 更多Facebook技术站 HHVM中文网 Facebook出品高性能服 ...
- 合成冷色黑暗恐怖魔法师图片的PS教程
教程主要使用Photoshop合成黑暗风格的魔法师施法场景,整体的场景效果以冷色风格为主,加上素材的叠加完成最终效果图,希望朋友可以喜欢.效果图: 先把背景拖进去,用工具吧字母弄掉. 加一个调色图层 ...
- 连接redis
- 想要打动HR的心,UX设计师求职信究竟应该怎么写?
在努力准备申请一份UX设计师职位时,你最烦心和担忧的事哪一个环节?是写一份UX设计师简历?回答面试官的问题?还是在一遍遍的煎熬中等待一个面试电话?是的,这些都是不轻松的事儿,但还有一个同样糟心的事,那 ...
- 什么是adb命令?以及如果高效使用他们?
接下来 我会为大家讲解 最通俗易懂的adb命令 希望大家能够喜欢...
- 日期处理相关 - “Fri Dec 11 00:00:00 CST 2015”日期格式解析
1.后台处理方式: /* 精简版解析 - 推荐 */ String a= "Fri Dec 11 00:00:00 CST 2015"; Date d = new Date(a); ...
- 关闭文件描述符-close
头文件:#include<unistd.h> 原型:int close(int fd); 返回值:成功返回0,失败返回-1.
- HDU 2136 Largest prime factor (素数打表。。。)
题意:给你一个数,让你求它的最大因子在素数表的位置. 析:看起来挺简单的题,可是我却WA了一晚上,后来终于明白了,这个第一层循环不是到平方根, 这个题和判断素数不一样,只要明白了这一点,就很简单了. ...
- Oracle零碎总结:结构-工具-创建语句
前言:Oracle内部的存储及管理结构是1.数据库系统:2.数据库实例:3.表空间,系统用户system,普通用户:表,视图,触发器,存储过程等: 一.Oracle数据库系统和数据库实例的对应关系是一 ...
- Creating a Simple Web Service and Client with JAX-WS
Creating a Simple Web Service and Client with JAX-WS 发布服务 package cn.zno.service.impl; import javax. ...