HDU 2689 Sort it(树状数组求逆序数)

Sort it

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3418    Accepted Submission(s): 2478

Problem Description

You want to processe a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. Then how many times it need.

For example, 1 2 3 5 4, we only need one operation : swap 5 and 4.

Input

The input consists of a number of test cases. Each case consists of two lines: the first line contains a positive integer n (n <= 1000); the next line contains a permutation of the n integers from 1 to n.

Output

For each case, output the minimum times need to sort it in ascending order on a single line.

Sample Input

3
1 2 3
4
4 3 2 1 

Sample Output

0
6

Author

WhereIsHeroFrom

Source

ZJFC 2009-3 Programming Contest

题意,用冒泡排序的方法把n个数排成按升序排列需要几步。

点击打开链接

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

using namespace std;

const int N = 1001;

int n;
int num[N];
int c[N];
struct node{
    int x;
    int id;
}q[N] ;

bool cmp(node a,node b){
    return a.x < b.x;
}

int lowbit(int x){
    return x&(-x);
}

int getsum(int x){
    int s = 0;
    while(x>0){
        s += c[x];
        x -= lowbit(x);
    }
    return s;
}

void add(int x,int y){
    while(x<=n){
        c[x] += y;
        x += lowbit(x);
    }
}

int main()
{
    while(scanf("%d",&n)!=EOF){
        memset(c,0,sizeof(c));
        memset(num,0,sizeof(num));
        for(int i=1;i<=n;i++){
            scanf("%d",&q[i].x);
            q[i].id = i;
        }
        sort(q+1,q+1+n,cmp);
        for(int i=1;i<=n;i++){
            num[q[i].id] = i;
        }
        int sum = 0;
        for(int i=1;i<=n;i++){
            add(num[i],1);
            sum += getsum(n) - getsum(num[i]);
        }
        printf("%d\n",sum);
    }
    return 0;
}

版权声明:本文为博主原创文章,如有特殊需要请与博主联系 QQ : 793977586。

时间: 2024-08-19 01:51:35

HDU 2689 Sort it(树状数组求逆序数)的相关文章

hdu 5147 Sequence II (树状数组 求逆序数)

题目链接 Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 331    Accepted Submission(s): 151 Problem Description Long long ago, there is a sequence A with length n. All numbers in this se

HDU 2689 Sort it (树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2689 Sort it Problem Description You want to processe a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. Then how many times it 

HDU 1394 Minimum Inversion Number (树状数组求逆序数)

Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13942    Accepted Submission(s): 8514 Problem Description The inversion number of a given number sequence a1, a2, ..., a

hdu 1394 Minimum Inversion Number (裸树状数组 求逆序数)

题目链接 题意: 给一个n个数的序列a1, a2, ..., an ,这些数的范围是0-n-1, 可以把前面m个数移动到后面去,形成新序列:a1, a2, ..., an-1, an (where m = 0 - the initial seqence)a2, a3, ..., an, a1 (where m = 1)a3, a4, ..., an, a1, a2 (where m = 2)...an, a1, a2, ..., an-1 (where m = n-1)求这些序列中,逆序数最少的

线段树或树状数组求逆序数(附例题)

学习了博主:MyZee   , shengweison 的文章 线段树或树状数组求逆序数 假设给你一个序列 6 1 2 7 3 4 8 5,  首先我们先手算逆序数, 设逆序数为 N; 6的前面没有比他大的数 N +=0 1的前面有一个比他大的数 N+=1 2的前面有一个比他大的数 N+=1 7的前面没有比他大的数 N+=0 ... 最后得到 N = 0 + 1 + 1 + 0 + 2 + 2 + 0 + 3 = 9 其实我们可用用线段树,或者树状数组模拟这个过程. 又因为线段树和树状数组的效率

ZOJ-2386 Ultra-QuickSort 【树状数组求逆序数+离散化】

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 seque

树状数组求逆序数

poj 2299 树状数组求逆序数题目链接:http://poj.org/problem?id=2299 1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <algorithm> 5 #include <vector> 6 #include <queue> 7 #include <stack> 8 #include <

poj2299 Ultra-QuickSort 树状数组求逆序数

poj2299 Ultra-QuickSort   树状数组求逆序数 Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 49587   Accepted: 18153 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequenc

poj 2299 树状数组求逆序数+离散化

http://poj.org/problem?id=2299 最初做离散化的时候没太确定但是写完发现对的---因为后缀数组学的时候,,这种思维习惯了吧 1.初始化as[i]=i:对as数组按照num[]的大小间接排序 2.bs[as[i]]=i:现在bs数组就是num[]数组的离散化后的结果 3.注意,树状数组中lowbit(i)  i是不可以为0的,0&(-0)=0,死循环... #include <cstdio> #include <cstring> #include