【OpenJ_Bailian - 2299 】Ultra-QuickSort (归并排序 或 离散化 + 树状数组)
题干:
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
题目大意:
大概题意就是给你n个数,让你求逆序对的个数。
解题报告:
这题是求逆序对的个数,直接上权值树状数组,然后求个逆序数就可以了,这里提供两种思路,一种是枚举每一个数然后看截止当前,他前面出现过多少比他大的(查后缀1的个数)。另一种是看截止当前,他后面有多少比他小的,也就是还有多少比他小的还没出现(查前缀0的个数)。
在此附上对应两种方法的AC代码:
AC代码1:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX = 500005 ;
int a[500005],b[MAX],c[500005];
int n;
int lowbit(int x){return x&-x;}
void update(int x,int val) {while(x<MAX) {c[x] += val;x+=lowbit(x);}
}
int query(int x) {int res = 0;while(x>0) {res += c[x];x-=lowbit(x);}return res;
}
int main()
{while(~scanf("%d",&n)) {if(n==0) break;ll ans = 0;memset(c,0,sizeof c);for(int i = 1; i<=n; i++) scanf("%d",a+i),b[i] = a[i];sort(b+1,b+n+1);int top = unique(b+1,b+n+1) - b - 1;for(int i = 1; i<=n; i++) {int pos = lower_bound(b+1,b+top+1,a[i]) - b;update(pos,1);ans += query(MAX-1) - query(pos);//ans += (pos-1)-query(pos-1);
// printf("yy\n");}printf("%lld\n",ans);}return 0 ;}
总结:
1.注意update和query中while都不能写等号。查询query的时候越界问题,所以需要查询MAX-1;
AC代码2:
//没写n==0的break?
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX = 500005 ;
int a[500005],b[MAX],c[500005];
int n;
int lowbit(int x){return x&-x;}
void update(int x,int val) {while(x<=MAX) {c[x] += val;x+=lowbit(x);}
}
int query(int x) {int res = 0;while(x>0) {res += c[x];x-=lowbit(x);}return res;
}
int main()
{while(~scanf("%d",&n)) {if(n==0) break;ll ans = 0;memset(c,0,sizeof c);for(int i = 1; i<=n; i++) scanf("%d",a+i),b[i] = a[i];sort(b+1,b+n+1);int top = unique(b+1,b+n+1) - b - 1;for(int i = 1; i<=n; i++) {int pos = lower_bound(b+1,b+top+1,a[i]) - b;update(pos,1);ans += (pos-1)-query(pos-1);}printf("%lld\n",ans);}return 0 ;}
【OpenJ_Bailian - 2299 】Ultra-QuickSort (归并排序 或 离散化 + 树状数组)相关推荐
- nyoj 1261 音痴又音痴的LT(离散化+树状数组求K小数)
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=1261 解题思路:比较水的题,用离散化+树状数组求K小数即可,先用一次离线处理. #inc ...
- 离散化+树状数组求逆序数
题目:http://poj.org/problem?id=2299 离散化是一种常用的技巧,有时数据范围太大,可以用来放缩到我们能处理的范围 因为其中需排序的数的范围0--- 999999999:显然 ...
- HDU 4325 离散化+树状数组 或者 不使用树状数组
题意:给出一些花的开放时间段,然后询问某个时间点有几朵花正在开放. 由于ti<1e9,我们需要先将时间离散化,然后将时间点抽象为一个数组中的点,显然,我们需要进行区间更新和单点查询,可以考虑线段 ...
- HDU - 5877 Weak Pair(离散化+树状数组+dfs序)
题目链接:点击查看 题目大意:给定一个n个节点的树,每个节点都有权值,现在定义weak pair(u,v)需要满足的两个条件: u是v的祖先: ; 问给定的树中有多少个weak pair: 题目分析: ...
- CF1621G Weighted Increasing Subsequences(离散化+树状数组优化dp+栈维护后缀最大值+计数)
problem luogu-link solution 显然单独考虑每个 iii 的贡献,即被多少个合法上升子序列包含. 令 x=max{j∣j>i∧aj>ai}x=\max\{j\ | ...
- 逆序数问题,用归并排序而非树状数组求解
逆序数,结合归并排序. 之前一直用树状数组写的,今天发现归并排序也很好写. https://www.nowcoder.com/practice/96bd6684e04a44eb80e6a68efc0e ...
- hdu 6447YJJ's Salesman 离散化+树状数组+DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6447 因为图中点的坐标值过大,达到1e9.然而只有1e5个点.所以先将其离散化.并按照<x.y& ...
- bzoj 3356: [Usaco2004 Jan]禁闭围栏 离散化+树状数组
新博客链接:https://www.everlasting.wang/archives/228
- HDU 4417 Super Mario(莫队 + 树状数组 + 离散化)
Super Mario 思路 区间查找问题,容易想到离线莫队,确实这题就是莫队,接下来我们考虑如何维护区间高度值问题. 既然是离线嘛,我们容易想到离散化和他挂钩,想想这题是否需要离散化,高度的最大值是 ...
最新文章
- Python 的 __name__ 变量,到底是个什么东西?
- Vue项目碰到‘webpack-dev-server’不是内部或外部命令,也不是可运行的程序或批处理文件报错...
- sql删除过程的命令是什么_今日份知识分享:SQL 介绍
- linux查看java进程cpu占用过高
- 体验VS2017的Live Unit Testing
- x = x+1,x+=1,x++那个的执行效率高
- 如何用CSS画一个三角形
- 小红书公司注册老红书商标上热搜 网友:过两年变老了改名吗?
- 「07」回归的诱惑:深入浅出逻辑回归
- caffe的python接口学习(1):生成配置文件
- exeScope软件修改exe或dll文件资源-20150818
- GD32Pack包下载地址
- 51单片机红外遥控小车
- 阅兵方阵 蓝桥杯 第九届JavaA
- 【iOS】 Foundation 数组
- 1096:数字统计(C C++)
- Unity Timeline运行时脚本控制Mute和UnMute(Mute/UnMute a timeline track via scripting)
- Spring-第一篇-快速入门
- [Place 30-876] Port ‘txclk‘ is assigned to PACKAGE_PIN * which can
- 伺服电机旋转变压器光电编码器
热门文章
- [Leedcode][JAVA][第394题][字符串解码][栈][类型转换]
- java加载sql2016_SQl Server 2016 with R.
- python爬虫数据可视化_python 爬虫与数据可视化--python基础知识
- eclipse linux windows 乱码,Ubuntu下Eclipse打开Windows下的工程文件乱码解决办法
- unityscrollview生成大量_superscrollviewforUGUI的一些使用心得
- Spring MVC 生成文件类型响应
- windows 7搭建流媒体服务
- python json.dumps慢_python json.dumps中文乱码问题解决
- mysql卸载完环境变量还有_MySql完全卸载
- ashx连接mysql_对C#中的web访问mysql数据库的一些知识点进行了整理归纳总结