POJ - 2299 Ultra-QuickSort 【树状数组+离散化】
题目链接
http://poj.org/problem?id=2299
题意
给出一个序列 求出 这个序列要排成有序序列 至少要经过多少次交换
思路
求逆序对的过程
但是因为数据范围比较大 到 999999999
但是 给的 n 的数量又比较少 所以 离散化一下就可以了
比如 给出的
9 1 0 5 4
原始ID 0 1 2 3 4
排序后 0 1 4 5 9
原始ID 2 1 4 3 0
然后就可以发现 求 9 1 0 5 4 的 所有逆序对个数 实际和 求 2 1 4 3 0
的逆序对个数 是一样的
然后 我们就可以将数据范围缩小到 50000
就可以用数组保存了
因为 sum 求得的是 之前比当前数字小的数字的个数 那么
逆序对个数就是 i - sum(i)
然后套用树状数组就可以了
参考
https://www.cnblogs.com/George1994/p/7710886.html
有一个坑点是 要用long long
AC代码
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>
#define CLR(a) memset(a, 0, sizeof(a))
#define pb push_back
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss;
const double PI = acos(-1.0);
const double E = exp(1.0);
const double eps = 1e-30;
const int INF = 0x3f3f3f3f;
const int maxn = 5e5 + 5;
const int MOD = 1e9 + 7;
int a[maxn];
int sum[maxn];
struct node
{
int v, ord;
}q[maxn];
bool comp(node x, node y)
{
return x.v < y.v;
}
int lowbit(int x)
{
return x & (-x);
}
int Sum(int n)
{
int ans = 0;
while (n > 0)
{
ans += a[n];
n -= lowbit(n);
}
return ans;
}
void add(int x)
{
while (x <= maxn)
{
a[x]++;
x += lowbit(x);
}
}
int main()
{
int n;
while (scanf("%d", &n) && n)
{
CLR(a, 0);
CLR(q, 0);
CLR(sum, 0);
for (int i = 0; i < n; i++)
{
scanf("%d", &q[i].v);
q[i].ord = i;
}
sort(q, q + n, comp);
ll ans = 0;
for (int i = 0; i < n; i++)
{
ans += (i) - Sum(++q[i].ord);
add(q[i].ord);
}
cout << ans << endl;
}
}
POJ - 2299 Ultra-QuickSort 【树状数组+离散化】的更多相关文章
- POJ 2299 Ultra-QuickSort(树状数组+离散化)
http://poj.org/problem?id=2299 题意:给出一组数,求逆序对. 思路: 这道题可以用树状数组解决,但是在此之前,需要对数据进行一下预处理. 这道题目的数据可以大到999,9 ...
- poj 2299 Ultra-QuickSort(树状数组求逆序数+离散化)
题目链接:http://poj.org/problem?id=2299 Description In this problem, you have to analyze a particular so ...
- poj 2299 Ultra-QuickSort(树状数组求逆序数)
链接:http://poj.org/problem?id=2299 题意:给出n个数,求将这n个数从小到大排序,求使用快排的需要交换的次数. 分析:由快排的性质很容易发现,只需要求每个数的逆序数累加起 ...
- poj 2299 Ultra-QuickSort(树状数组)
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 67681 Accepted: 25345 ...
- POJ 2299 Ultra-QuickSort【树状数组 ,逆序数】
题意:给出一组数,然后求它的逆序数 先把这组数离散化,大概就是编上号的意思--- 然后利用树状数组求出每个数前面有多少个数比它小,再通过这个数的位置,就可以求出前面有多少个数比它大了 这一篇讲得很详细 ...
- 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 树状数组+离散化求逆序对
给出一个序列 相邻的两个数可以进行交换 问最少交换多少次可以让他变成递增序列 每个数都是独一无二的 其实就是问冒泡往后 最多多少次 但是按普通冒泡记录次数一定会超时 冒泡记录次数的本质是每个数的逆序数 ...
随机推荐
- 爬虫:网页里元素的xpath结构,scrapy不一定就找的到
这种情况原因是html界面关联的js文件可能会动态修改DOM结构,这样浏览器完成了动态修改DOM,在 浏览器上看到的DOM结构,就和后台抓到的DOM结构不通 举例:新浪微博发的微博,在浏览器通过fir ...
- EasyMvc入门教程-高级控件说明(15)方位布局控件
现在很多管理后台都流行全屏切割的布局,大体结构如下图所示: 大家注意到没,整个布局是五个部分组成:“东西南北中”,EasyMvc对应的实现的代码为: @(Html.Q().Layout().TextC ...
- 细微之处见功夫!这5点让Wish3D Earth与众不同
产品的体验是全方位的,任何一点,都可能决定成败.细微之处见功夫,5个细节,告诉你Wish3D Earth为什么与众不同. 中科图新最新发布的Wish3D Earth,是基于WebGL技术的网页版三维地 ...
- 第十二题 Merge Sorted Array
Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume th ...
- JAVA_Could not find property [struts.actionMapping]怎么办
你的项目中不包含log4j.jar这个文件,包含进去即可
- codeforces 204(Div.1 A) Little Elephant and Interval(贪心)
题意: 有一种个位数与最高位数字相等的数字,求在l,r的范围内,这样的数字的个数. 思路: 找下规律就知道当当n>10的时候除去个位以后的答案等于n/10,然后考虑第一个数字是否小于最后一个.小 ...
- 利用 LeakCanary 来检查 Android 内存泄漏
前言 你被概率性的 OOM 困扰么?有时候,OOM 像幽灵一样,挥之不去,可真想把它揪出来时,又捉之不着.或许,是时候用 LeakCanary 来诊断一下了.它是一个用来检查 Android 下内存泄 ...
- linux 查找并操作
find -depth 1 -name 'aa*' | xargs tar -cvf aa.tar 这个命令将为查找当前目录下的所有已aa开头的文件,然后将所有结果"执行打包",打 ...
- Codeforces Round #243 (Div. 2)——Sereja and Table
看这个问题之前,能够先看看这个论文<一类算法复合的方法>,说白了就是分类讨论,可是这个思想非常重要 题目链接 题意: 首先给出联通块的定义:对于相邻(上下和左右)的同样的数字视为一个联通块 ...
- template.js文档
参见GitHub:https://github.com/yanhaijing/template.js/ template.js简介: template.js 一款javascript模板引擎,简单,好 ...