poj 2299 树状数组求逆序对数+离散化
Time Limit: 7000MS | Memory Limit: 65536K | |
Total Submissions: 54883 | Accepted: 20184 |
Description

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
Output
Sample Input
5
9
1
0
5
4
3
1
2
3
0
Sample Output
6
0
Source
/******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<algorithm>
#include<queue>
#include<cmath>
#define ll __int64
#define PI acos(-1.0)
#define mod 1000000007
using namespace std;
int n;
struct node
{
int v;
int pos;
}N[];
int a[];
int tree[];
bool cmp(struct node aa,struct node bb)
{
return aa.v<bb.v;
}
int lowbit(int t) {return t&(-t);}
void add(int i,int v){
for(;i<=n;i+=lowbit(i))
tree[i]+=v;
}
int sum(int i){//sum 就是统计之前有多少个小于i的数
int ans=;
for(;i>;i-=lowbit(i))
ans+=tree[i];
return ans;
}
int main()
{
while(~scanf("%d",&n)){
if(n==)
break;
for(int i=;i<=n;i++)
{
scanf("%d",&N[i].v);
N[i].pos=i;
}
sort(N+,N++n,cmp);
for(int i=;i<=n;i++)//离散化
a[N[i].pos]=i;
for(int i=;i<=n;i++)//初始化
tree[i]=;
ll ans=;
for(int i=;i<=n;i++)
{
add(a[i],);//注意修改值为1
ans+=(i-sum(a[i]));
}
printf("%I64d\n",ans);
}
return ;
}
poj 2299 树状数组求逆序对数+离散化的更多相关文章
- Ultra-QuickSort POJ - 2299 树状数组求逆序对
In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a seque ...
- POJ 2299树状数组求逆序对
求逆序对最常用的方法就是树状数组了,确实,树状数组是非常优秀的一种算法.在做POJ2299时,接触到了这个算法,理解起来还是有一定难度的,那么下面我就总结一下思路: 首先:因为题目中a[i]可以到99 ...
- poj 2299 树状数组求逆序数+离散化
http://poj.org/problem?id=2299 最初做离散化的时候没太确定可是写完发现对的---由于后缀数组学的时候,,这样的思维习惯了吧 1.初始化as[i]=i:对as数组依照num ...
- POJ2299Ultra-QuickSort(归并排序 + 树状数组求逆序对)
树状数组求逆序对 转载http://www.cnblogs.com/shenshuyang/archive/2012/07/14/2591859.html 转载: 树状数组,具体的说是 离散化+树 ...
- [NOIP2013提高&洛谷P1966]火柴排队 题解(树状数组求逆序对)
[NOIP2013提高&洛谷P1966]火柴排队 Description 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度. 现在将每盒中的火柴各自排成一列, 同一列火柴的高度互不相 ...
- [NOI导刊2010提高&洛谷P1774]最接近神的人 题解(树状数组求逆序对)
[NOI导刊2010提高&洛谷P1774]最接近神的人 Description 破解了符文之语,小FF开启了通往地下的道路.当他走到最底层时,发现正前方有一扇巨石门,门上雕刻着一幅古代人进行某 ...
- 【bzoj2789】[Poi2012]Letters 树状数组求逆序对
题目描述 给出两个长度相同且由大写英文字母组成的字符串A.B,保证A和B中每种字母出现的次数相同. 现在每次可以交换A中相邻两个字符,求最少需要交换多少次可以使得A变成B. 输入 第一行一个正整数n ...
- “浪潮杯”第九届山东省ACM大学生程序设计竞赛(重现赛)E.sequence(树状数组求逆序对(划掉))
传送门 E.sequence •题意 定义序列 p 中的 "good",只要 i 之前存在 pj < pi,那么,pi就是 "good": 求删除一个数, ...
- 2021.12.10 P5041 [HAOI2009]求回文串(树状数组求逆序对)
2021.12.10 P5041 [HAOI2009]求回文串(树状数组求逆序对) https://www.luogu.com.cn/problem/P5041 题意: 给一个字符串 \(S\) ,每 ...
随机推荐
- ZOJ 3644 Kitty's Game dfs,记忆化搜索,map映射 难度:2
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4834 从点1出发,假设现在在i,点数为sta,则下一步的点数必然不能是sta的 ...
- 二模 (4) day1
第一题: 题目描述: 有一个无穷序列如下:110100100010000100000…请你找出这个无穷序列中指定位置上的数字 解题过程: 1.考虑到1的数目比0少的多,就从1的位置的规律开始分析.前几 ...
- 根据图片Uri获得图片文件
2013-12-17 1. 根据联系人图片Uri获得图片文件并将它显示在ImageView上, 代码如下: Uri uri = Uri.parse("content://com.androi ...
- [C/C++]C++标准中的名词
1.qualified-id.nested-name-specifier: [example: struct A { struct B { void F(); }; }; A is an unqual ...
- Lib New
gacutil /if E:\ThirdParty\RockyLib\Rocky\bin\Release\Rocky.dll gacutil /u Rocky partial class GmailF ...
- UIView 翻转动画
[_mapView removeFromSuperview]; [self addSubview:_tableView]; //应将self.view设置为翻转对象 [UIView transitio ...
- 捕获异常的两种方式Exception
1.抛出异常:让调用此方法的代码去管 public static void GetFile() throws Exception{} package com.throwable; import jav ...
- poj1651 区间dp
//Accepted 200 KB 0 ms //dp区间 //dp[i][j]=min(dp[i][k]+dp[k][j]+a[i]*a[k]*a[j]) i<k<j #include ...
- DirectX 总结和DirectX 9.0 学习笔记
转自:http://www.cnblogs.com/graphics/archive/2009/11/25/1583682.html DirectX 总结 DDS DirectXDraw Surfac ...
- js jquery 判断函数是否存在($.isFunction函数的使用)
var fun = "testFun"; // 函数的名称 try{ 3 if($.isFunction(fun)){ } } $.alert(fun +'不是函数!'); } 注 ...