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 树状数组+离散化求逆序对
给出一个序列 相邻的两个数可以进行交换 问最少交换多少次可以让他变成递增序列 每个数都是独一无二的 其实就是问冒泡往后 最多多少次 但是按普通冒泡记录次数一定会超时 冒泡记录次数的本质是每个数的逆序数 ...
随机推荐
- winform Loading效果
做winform项目时,有可能用到异步耗时加载数据啥的,这个时候就需要我们封装一个正在加载的效果.下面是实现思路: 步骤一:把当前form界面使用句柄的方式截图出一个图片,这个图片会在下面用到,使用句 ...
- Android 获取虚拟按键的高度
//获取虚拟按键的高度 public static int getNavigationBarHeight(Context context) { int result = 0; if (hasNavBa ...
- tomcat 登录时用户名和密码问题
在编程的时候我们经常在myeclipes中直接部署web程序,大多数情况下不会登陆tomcat,这样时间长了我们就忘记了tomcat的登陆用户名和密码,下面就说一下怎么找到tomcat的用户名和密码吧 ...
- windows核心编程 DLL技术 【转】
注:本文章转载于网络,源地址为:http://blog.csdn.net/ithzhang/article/details/7051558 本篇文章将介绍DLL显式链接的过程和模块基地址重定位及模块绑 ...
- Json格式化工具 JsonViewer下载
免安装版,分享链接永久有效~! 云盘下载地址: http://cloud.suning.com/cloud-web/share/link.htm?sk=401f784782751055ddc21cdb ...
- 2016.7.12 eclispe使用mybatis generator生成代码时提示project E is not exist
运行mybatis-generator之后,出现错误:project E is not exist 错误原因:使用了项目的绝对路径. http://bbs.csdn.net/topics/3914 ...
- HTML5 Canvas 绘制旋转45度佛教万字
效果如下: 代码如下: <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Cont ...
- java代码实现输出指定以.java结尾的文件的绝对路径
package 输出指定文件绝对路径; import java.io.File; /* * 需求:请大家把"E:\\JAVA语言"文件夹下全部的java结尾的文件的绝对路径给输出在 ...
- FileUpload控件预览图片
HTML代码: <tr> <td class="auto-style1">上传图片:</td> <td> <asp:FileU ...
- Angular+Angular-Ui实现分页(代码更加简单,更加容易懂哦)
前面写了个分页,但是个人认为只能玩玩,实际业务上的话代码太繁杂(笔者想走代码.性能精简化之路[不知道哪天才能成为高手~·YY一下无伤大雅]),逻辑上有点混乱.那么今天我们来看看另外一种只实现分页没有查 ...