poj 2299 Ultra-QuickSort 题解
Description
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
element. Input is terminated by a sequence of length n = 0. This sequence must not be processed.
Output
sequence.
---------------------------------------------我是分割线-----------------------------------------------
树状树的推导:
树状数组的结构图
预备代码:
lowbit:快速求与该数相关的之前的数.
add(或change):把x位置的值加上d(当然,其实为改变之后的值)
sum:求1-x的和。
(详细代码见下)
---------------------------------------------我是分割线-----------------------------------------------
思路:数据小的时候,我们开一个数组cnt,其范围是一组数中的最大值。cnt[i]表示数字i出现的次数(一开始清零)
我们从头操作,设每次取出的数为x,则它产生的逆序对(以它为较小者的逆序对)就是所有在此之前的比他大的数的个数。
究竟怎么表示呢? 借鉴于前缀和,我们可以用sum(tot)(tot是最大值)-sum(x-1),这就是目前记录的比它大的数(既然已经记录过,它们一定在x的前面。然而,数据范围有很多9,我们得另想办法。观察n<=50万,我们可以把n个数映射到最多为n的数组里再进行处理。别忘了去重。
代码:
#include<stdio.h>
#include<algorithm>
using namespace std;
struct arr{long x,y;}a[500001];
long cnt[500001],tot,n,i;
long long ans;
long lowbit(long x) {return x&(-x);}
void add(long x,long d)
{
while (x<=n){cnt[x]+=d;x+=lowbit(x);}
}
long sum(long x)
{
long ans=0;
while (x>0){ans+=cnt[x];x-=lowbit(x);}
return ans;
}
bool cmp(arr a,arr b){return (a.x<b.x);}
int main()
{
scanf("%ld",&n);
while (n>0)
{
ans=0;
for (i=1;i<=n;i++)
{
scanf("%ld",&a[i].x);
if (a[i].x==a[i-1].x) a[i].y=a[i-1].y;else a[i].y=a[i-1].y+1;//a[i].x记录数据,a[i].y作为映射,在排序里排进去。
}
tot=a[n].y; //tot为数组个数,开始为最大值。
sort(a+1,a+n+1,cmp);
for (i=1;i<=tot;i++) cnt[i]=0; //清零
for (i=1;i<=n;i++)
{
ans+=sum(tot)-sum(a[i].y-1); //加上当前数产生的逆序对。
add(a[i].y,1); //修改cnt
}
printf("%lld\n",ans);
scanf("%ld",&n);
}
return 0;
}
poj 2299 Ultra-QuickSort 题解的更多相关文章
- POJ 2299 Ultra-QuickSort 简单题解
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 68874 Accepted: 25813 ...
- POJ 2823 Sliding Window 题解
POJ 2823 Sliding Window 题解 Description An array of size n ≤ 106 is given to you. There is a sliding ...
- 逆序数 POJ 2299 Ultra-QuickSort
题目传送门 /* 题意:就是要求冒泡排序的交换次数. 逆序数:在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序. 一个排列中逆序的总数就称为这个排列的逆 ...
- 树状数组求逆序对:POJ 2299、3067
前几天开始看树状数组了,然后开始找题来刷. 首先是 POJ 2299 Ultra-QuickSort: http://poj.org/problem?id=2299 这题是指给你一个无序序列,只能交换 ...
- POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)
POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...
- POJ 2299 【树状数组 离散化】
题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...
- POJ 2299 Ultra-QuickSort(线段树+离散化)
题目地址:POJ 2299 这题以前用归并排序做过.线段树加上离散化也能够做.一般线段树的话会超时. 这题的数字最大到10^10次方,显然太大,可是能够利用下标,下标总共仅仅有50w.能够从数字大的開 ...
- 题解报告:poj 2299 Ultra-QuickSort(BIT求逆序数)
Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...
- poj 2299 逆序数
http://poj.org/problem?id=2299 坑:答案是long long 输出……!!!!! 题意是:求一个数组进行冒泡排序交换的次数 题解:求逆序数 题解Ⅰ: 归并排序求逆序数 归 ...
随机推荐
- iOS crash日志分析
iOS crash日志分析 一. 寻找crash文件:手机崩溃后的ips或者crash文件(ips文件可以直接修改成crash文件,直接改后缀名就可以了),这里说下如何拿到crash文件 1. 把运行 ...
- XD
题目 是否完成 题目分类 简要题解 没有上司的舞会(codevs1380) Y 树形dp dp[u][0]表示不包含此节点,dp[u][1]表示包含,转移方程为 dp[u][0]+=max(dp[v] ...
- C#打开php链接传参然后接收返回值
php代码 一.php <?php header("Content-Type:text/html;charset=UTF-8"); $u=$_POST['zdupdate'] ...
- 开涛spring3(8.1) - 对ORM的支持 之 8.1 概述
8.1 概述 8.1.1 ORM框架 ORM全称对象关系映射(Object/Relation Mapping),指将Java对象状态自动映射到关系数据库中的数据上,从而提供透明化的持久化支持,即把 ...
- 多线程编程-- part 2 线程的生命周期和优先级
线程的创建到消亡的历程: java多线程的5种状态: (1)New(新建) new Thread(run()) 该线程还没开始运行,状态是new,在程序运行前还有一些基础工作要做 (2)runnabl ...
- click和blur事件冲突解决方案
场景:例如做一个模仿百度搜索的搜索框,输入文字下面会有匹配项,当点击下拉项中的值时,就将值添加到搜索框中同时隐藏下拉框,点击其他地方就直接隐藏下拉框,这时所需要的事件分别为 下拉框事件onclick, ...
- kafka 0.8.2 消息消费者 consumer
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- 源代码安装软件-MySQL
一.源码安装 1.经典的源代码安装三步曲: 1.编译前的配置 ./configure 2.编译 make 3.安装 make install 2.源代码软件安装步骤: 1.下载软件包 2.校验软件包 ...
- Javaweb---服务器Tomcat与Eclipse的关联
1.与eclipse的关联 打开eclipse-->window-->preferences 在搜索框输入->server->进行搜索,选择-–>server and R ...
- JavaScript 扫描枪使用(一)
JavaScript 扫描枪应用(一)com.js为主要的代码实现,test.html文件为测试的页面,其中包括了com.js文件中方法的调用.以下为测试成功代码:com.js //com.js /* ...