hdoj 5122 K.Bro Sorting 【求序列中具有至少一对逆序对的 元素个数】

K.Bro Sorting

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)
Total Submission(s): 1043    Accepted Submission(s): 462


Problem Description
Matt’s friend K.Bro is an ACMer.

Yesterday, K.Bro learnt an algorithm: Bubble sort. Bubble sort will compare each pair of adjacent items and swap them if they are in the wrong order. The process repeats until no swap is needed.

Today, K.Bro comes up with a new algorithm and names it K.Bro Sorting.

There are many rounds in K.Bro Sorting. For each round, K.Bro chooses a number, and keeps swapping it with its next number while the next number is less than it. For example, if the sequence is “1 4 3 2 5”, and K.Bro chooses “4”, he will get “1 3 2 4 5” after this round. K.Bro Sorting is similar to Bubble sort, but it’s a randomized algorithm because K.Bro will choose a random number at the beginning of each round. K.Bro wants to know that, for a given sequence, how many rounds are needed to sort this sequence in the best situation. In other words, you should answer the minimal number of rounds needed to sort the sequence into ascending order. To simplify the problem, K.Bro promises that the sequence is a permutation of 1, 2, . . . , N .
 

Input
The first line contains only one integer T (T ≤ 200), which indicates the number of test cases. For each test case, the first line contains an integer N (1 ≤ N ≤ 10 6).

The second line contains N integers a i (1 ≤ a i ≤ N ), denoting the sequence K.Bro gives you.

The sum of N in all test cases would not exceed 3 × 10 6.
 

Output
For each test case, output a single line “Case #x: y”, where x is the case number (starting from 1), y is the minimal number of rounds needed to sort the sequence.
 

Sample Input
   
   
   
   
2 5 5 4 3 2 1 5 5 1 2 3 4
 

Sample Output
   
   
   
   
Case #1: 4 Case #2: 1
Hint
In the second sample, we choose “5” so that after the first round, sequence becomes “1 2 3 4 5”, and the algorithm completes.
 

如序列  1 4 3 2 5:对两个数交换的的前提是两个数不符合升序排列。

若选择元素4,则 4 和 3 交换 ——> 4 和 2交换 ,到这里,已经无法找到与4不符合升序排列的元素。这个过程称为一个循环。

题意:给你一个N个数的序列,让你求出最少经过几次循环才能使序列升序排列。 题目保证序列是1到N的全排列。

思路:就是求序列中至少含有一个逆序对 的元素的个数。我是用树状数组优先插入当前最小值实现的,当然也可以优先插入当前最大值。

自己的思路比较水,还有一重for循环实现的。。。

AC代码:

#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <algorithm>
#define MAXN 1000000+100
#define MAXM 10000000
#define INF 10000000
using namespace std;
struct rec
{
    int val, pos;
};
rec num[MAXN];
bool cmp(rec a, rec b)
{
    return a.val < b.val;
}
bool vis[MAXN];
//vector<int> G[MAXN];//´æ´¢ÄæÐò¶Ô
int c[MAXN << 1];
int N;
int lowbit(int x)
{
    return x&(-x);
}
void update(int x)
{
    while(x <= N)
    {
        c[x] += 1;
        x += lowbit(x);
    }
}
int sum(int x)
{
    int s = 0;
    while(x > 0)
    {
        s += c[x];
        x -= lowbit(x);
    }
    return s;
}
int main()
{
    int t;
    int k = 1;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &N);
        memset(c, 0, sizeof(c));
        for(int i = 1; i <= N; i++)
        {
            scanf("%d", &num[i].val), num[i].pos = i, vis[num[i].val] = false;
        }

        sort(num + 1, num + N + 1, cmp);
        int ans = 0;
        for(int i = 1; i <= N; i++)
        {
            update(num[i].pos);//先插入最小的元素
            int flag = sum(N) - sum(num[i].pos);//判断它后面是否已经有元素先插入
            if(flag)//若有存在逆序对
                ans++;
        }
        printf("Case #%d: ", k++);
        printf("%d\n", ans);
    }
    return 0;
}




你可能感兴趣的:(hdoj 5122 K.Bro Sorting 【求序列中具有至少一对逆序对的 元素个数】)