POJ2299--树状数组求逆序数
Description
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
题目地址
题目大意
给定一个序列求逆序数,用树状数组解决,注意数据太大需离散化处理
AC代码
#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
using namespace std;
const int M=500010;
int MN;
int e[M];
int k;
struct node
{
    int val;
    int pos;
}a[M];
int lowbit(int x)
{
    return x&(-x);
}
void update(int i,int v)
{
    for(;i<=MN;i+=lowbit(i)){
        e[i]+=v;
    }
}
int getsum(int i)
{
    int res=0;
    for(;i>0;i-=lowbit(i)){
        res+=e[i];
    }
    return res;
}
bool cmp1(node a,node b)
{
    return a.val<b.val;
}
bool cmp2(node a,node b)
{
    return a.pos<b.pos;
}
void init()
{
    sort(a+1,a+k+1,cmp1);
    for(int i=1;i<=k;i++)
        a[i].val=i;
    sort(a+1,a+k+1,cmp2);
    memset(e,0,sizeof(e));
    MN=k;
}
int main()
{
    //freopen("data.in","r",stdin);
    long long  res;
    while(cin>>k&&k!=0){
        res=0;
        for(int i=1;i<=k;i++){
            cin>>a[i].val;
            a[i].pos=i;
        }
        init();
        for(int i=1;i<=k;i++){
            update(a[i].val,1);
            res+=(i-getsum(a[i].val));
        }
        cout<<res<<endl;
    }
}
												
											POJ2299--树状数组求逆序数的更多相关文章
- poj 2299 Ultra-QuickSort(树状数组求逆序数)
		
链接:http://poj.org/problem?id=2299 题意:给出n个数,求将这n个数从小到大排序,求使用快排的需要交换的次数. 分析:由快排的性质很容易发现,只需要求每个数的逆序数累加起 ...
 - hdu 5147 Sequence II (树状数组  求逆序数)
		
题目链接 Sequence II Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
 - SGU180 Inversions(树状数组求逆序数)
		
题目: 思路:先离散化数据然后树状数组搞一下求逆序数. 离散化的方法:https://blog.csdn.net/gokou_ruri/article/details/7723378 自己对用树状数组 ...
 - poj 2299 Ultra-QuickSort(树状数组求逆序数+离散化)
		
题目链接:http://poj.org/problem?id=2299 Description In this problem, you have to analyze a particular so ...
 - HDU 1394 Minimum Inversion Number   (  树状数组求逆序数    )
		
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number ...
 - Codeforces645B【树状数组求逆序数】
		
题意: 给你1-n的序列,然后有k次机会的操作,每一次你可以选择两个数交换. 求一个最大的逆序数. 思路: 感觉就是最后一个和第一个交换,然后往中间逼近,到最终的序列,用树状数组求一下逆序数. #in ...
 - HDU 1394 Minimum Inversion Number(线段树/树状数组求逆序数)
		
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
 - hdu 1394 Minimum Inversion Number (裸树状数组 求逆序数 && 归并排序求逆序数)
		
题目链接 题意: 给一个n个数的序列a1, a2, ..., an ,这些数的范围是0-n-1, 可以把前面m个数移动到后面去,形成新序列:a1, a2, ..., an-1, an (where m ...
 - Codeforces Round #261 (Div. 2)  D. Pashmak and Parmida's problem (树状数组求逆序数  变形)
		
题目链接 题意:给出数组A,定义f(l,r,x)为A[]的下标l到r之间,等于x的元素数.i和j符合f(1,i,a[i])>f(j,n,a[j]),求i和j的种类数. 我们可以用map预处理出 ...
 - poj 2229 Ultra-QuickSort(树状数组求逆序数)
		
题目链接:http://poj.org/problem?id=2299 题目大意:给定n个数,要求这些数构成的逆序对的个数. 可以采用归并排序,也可以使用树状数组 可以把数一个个插入到树状数组中, 每 ...
 
随机推荐
- MTK平台Android项目APK预置方案
			
项目开发中,通常需要向系统中预置一些APK,这里简单介绍一下MTK平台预置APK的方法. 需要预置的apk可以放置在目录:vendor/mediate/${Project}/artifacts/out ...
 - 7.编写Java应用程序。首先,定义一个Print类,它有一个方法void output(int x),如果x的值是1,在控制台打印出大写的英文字母表;如果x的值是2,在 控制台打印出小写的英文字母表。其次,再定义一个主类——TestClass,在主类 的main方法中创建Print类的对象,使用这个对象调用方法output ()来打印出大 小写英文字母表。
			
package com.bao; public class Print1 { int x; void output() { if(x==1) { System.out.println("AB ...
 - 在block函数中规避错误信息 "capturing self strongly in this block is likely to lead to a retain cycle”
			
以形如 _fontValueChangedBlock = ^(){ [self.fontSmallButton addTarget:self action:@selector(btnFontSmall ...
 - 项目中 添加 swift代码 真机调试 错误
			
错误: dyld: Library not loaded: @rpath/libswiftCore.dylib Referenced from: /private/var/mobile/Contain ...
 - python脚本文件删除
			
昨天有需求需要用python脚本删除一个目录下的文件.遇到了点麻烦. 使用的是shutil.rmtree(dir)函数,这个函数可以删除有内容的目录,而shutil.rmdir(dir)只能删除空目录 ...
 - opencv使用convexityDefects计算轮廓凸缺陷
			
引自:http://www.xuebuyuan.com/1684976.html http://blog.csdn.net/lichengyu/article/details/38392473 htt ...
 - codeforce  Gym 101102A Coins (01背包变形)
			
01背包变形,注意dp过程的时候就需要取膜,否则会出错. 代码如下: #include<iostream> #include<cstdio> #include<cstri ...
 - Chapter 1 First Sight——36
			
The door opened again, and the cold wind suddenly gusted through the room, rustling the papers on th ...
 - form异步无刷新提交,提交后可以显示弹出框,否则弹出框会被刷新不见,使用 preventDefault
			
出错点:确认按钮上.加onclick事件.每次点击都会追加给form追加on监听方法.累加on方法,重复提交 suppress_exception:true 阻止异常 (百度推送 jdk) 向下按 p ...
 - pureMVC java版搭建流程
			
转自:http://blog.csdn.net/sutaizi/article/details/6588004 pureMVC 是一个轻量级的框架 它在 flex中非常流行(和cairngorm差不多 ...