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 树状数组+离散化求逆序对
给出一个序列 相邻的两个数可以进行交换 问最少交换多少次可以让他变成递增序列 每个数都是独一无二的 其实就是问冒泡往后 最多多少次 但是按普通冒泡记录次数一定会超时 冒泡记录次数的本质是每个数的逆序数 ...
随机推荐
- 说说GestureDetector.OnGestureListener onScroll函数
public abstract boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) ...
- MFC中 给基于CFormView的单文档添加背景图片
关于基于CFormView的单文档应用程序,添加一个图片背景的方法之一如下: 下面是利用LoadImage实现.(先在程序目录中添加背景图片back.bmp) 1.在view类中添加类成员变量:(为C ...
- 出自 HTML4 规范的可用颜色字符串值列表(常用颜色名称及对应的十六进制值)
据称由于 HTML5 没有修改专属的颜色,HTML4 的颜色都可以在 HTML5 中正确显示. 出自 HTML4 规范的可用颜色字符串值列表如下,此表来源是 http://www.lovean.com ...
- intent传值传对象跳转
intent传值传对象跳转 1.传值 //原activity中存入一个字段 intent = new Intent(From.this, To.class); intent.putExtra(&quo ...
- Odoo11 新功能 : 栏位隐藏
Odoo11增加了一个 新的 modifier-. 先上 前端框架代码 invisible / readonly / required 这几个 是经常在用的, column_invis ...
- OLR
OLR:Oracle Local Registry 环境:11.2.0.3 RAC RHEL6.5 It contains local node specific configuration re ...
- PM2.5
http://baike.baidu.com/view/1423678.htm PM2.5是指大气中直径小于或等于2.5微米的颗粒物,也称为可入肺颗粒物.虽然PM2.5只是地球大气成分中含量很少的组分 ...
- RSA非对称算法实现HTTP密码加密传输
目前一般帐号系统,都是https来传输账户性息,申请一个https证书也不贵.但是网站的其它功能并不需要走https协议,https和http混布比较麻烦,所以决定先实现一个http协议传输RSA非对 ...
- win7查看端口占用
1.查看谁占用了我们的80端口,在windows命令行窗口下执行: netstat -aon|findstr 80 发现80端口被进程号为2596的进程占用.2.查看占用80端口进程的应用程序是什 ...
- 源码维护基本命令diff_patch_quilt
源码维护基本命令 一. diff--生成补丁 diff [命令行选项] 源文件 新文件 -r 递归处理相应目录 -N 包含新文件到patch -u 输出统一格式(unified format),这种格 ...