码迷,mamicode.com
首页 > 其他好文 > 详细

归并求逆序数 && 线段树求逆序数

时间:2014-09-26 21:51:58      阅读:320      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   os   ar   java   

Brainman

Time Limit: 1000 MS Memory Limit: 30000 KB

64-bit integer IO format: %I64d , %I64u   Java class name: Main

[Submit] [Status] [Discuss]

Description

Background Raymond Babbitt drives his brother Charlie mad. Recently Raymond counted 246 toothpicks spilled all over the floor in an instant just by glancing at them. And he can even count Poker cards. Charlie would love to be able to do cool things like that, too. He wants to beat his brother in a similar task.
Problem Here‘s what Charlie thinks of. Imagine you get a sequence of N numbers. The goal is to move the numbers around so that at the end the sequence is ordered. The only operation allowed is to swap two adjacent numbers. Let us try an example:
Start with: 2 8 0 3 swap  (2 8) 8 2 0 3 swap  (2 0) 8 0 2 3 swap  (2 3) 8 0 3 2 swap  (8 0) 0 8 3 2 swap  (8 3) 0 3 8 2 swap  (8 2) 0 3 2 8 swap  (3 2) 0 2 3 8 swap  (3 8) 0 2 8 3 swap  (8 3) 0 2 3 8
So the sequence (2 8 0 3) can be sorted with nine swaps of adjacent numbers. However, it is even possible to sort it with three such swaps:
Start with: 2 8 0 3 swap  (8 0) 2 0 8 3 swap  (2 0) 0 2 8 3 swap  (8 3) 0 2 3 8
The question is: What is the minimum number of swaps of adjacent numbers to sort a given sequence?Since Charlie does not have Raymond‘s mental capabilities, he decides to cheat. Here is where you come into play. He asks you to write a computer program for him that answers the question. Rest assured he will pay a very good prize for it.

Input

The first line contains the number of scenarios. For every scenario, you are given a line containing first the length N (1 <= N <= 1000) of the sequence,followed by the N elements of the sequence (each element is an integer in [-1000000, 1000000]). All numbers in this line are separated by single blanks.

Output

Start the output for every scenario with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the minimal number of swaps of adjacent numbers that are necessary to sort the given sequence. Terminate the output for the scenario with a blank line.

Sample Input

4
4 2 8 0 3
10 0 1 2 3 4 5 6 7 8 9
6 -42 23 6 28 -100 65537
5 0 0 0 0 0

Sample Output

Scenario #1:
3

Scenario #2:
0

Scenario #3:
5

Scenario #4:
0
#include<iostream>
#include<cstdio>
#include<stdlib.h>
#define N 1000010
using namespace std;
long long ans;
int a[N];

void merge(int s1,int e1,int s2,int e2)
{
    int p1,p2,p;
    int* temp = new int[e2-s1+5];
    p=0;p1=s1;p2=s2;
    while(p1<=e1&&p2<=e2)
    {
        if(a[p1]<=a[p2])
        {
            temp[p++]=a[p1++];
            continue;
        }
        else
        {
            temp[p++]=a[p2++];
            ans+=e1-p1+1;   //关键所在
            continue;
        }
    }
    while(p1<=e1) temp[p++]=a[p1++];
    while(p2<=e2) temp[p++]=a[p2++];
    int i;
    for(i=s1;i<=e2;i++) a[i]=temp[i-s1];
    delete temp;
}

void merge_sort(int s,int e)
{
    int m;
    if(s<e)
    {
        m=(s+e)/2;
        merge_sort(s,m);
        merge_sort(m+1,e);
        merge(s,m,m+1,e);
    }
}

int main()
{
    int test;
    int num;
    scanf("%d",&test);
    for(num=1;num<=test;num++)
    {
        int n,i;
        ans=0;
        scanf("%d",&n);
        for(i=0;i<n;i++)
            scanf("%d",&a[i]);
        merge_sort(0,n-1);
        printf("Scenario #%d:\n",num);
        printf("%lld\n",ans);
        if(num!=test)
        printf("\n");
    }
}

 

#include<iostream>
#include<cstring>
#include <stdio.h>
#include<algorithm>
using namespace std;
int n;
int static c[500003];
int a[500003];
const int maxn = 500003;
struct Node
{
    int value,pos;
    bool operator<(const Node a)const
    {
        return value<a.value;
    }
} s[500003];
int lowbit(int x)
{
    return x&(-x);
}
void insert(int pos,int x)
{
    while(pos<=maxn)
    {
        c[pos]+=x;
        pos+=lowbit(pos);
    }
}
int sum(int x)
{
    int s=0;
    while(x>0)
    {
        s+=c[x];
        x-=lowbit(x);
    }
    return s;
}
int main()
{
    int t,num;
    cin>>t;
    for(num=1; num<=t; num++)
    {
        scanf("%d",&n);
        memset(c,0,sizeof(c));
        memset(a,0,sizeof(a));
        for(int i=0; i<n; i++)
        {
            scanf("%d",&s[i].value);
            s[i].pos=i+1;
        }
        sort(s,s+n);
        int id=1;
        a[s[0].pos]=1;
        for(int i=1; i<n; i++) //离散化
        {
            if(s[i].value!=s[i-1].value)
            {
                a[s[i].pos]=++id;
            }
            else
            {
                a[s[i].pos]=id;
            }
        }
        long long int ans=0;
        for(int i=1; i<=n; i++) //一个个插入求逆序对
        {
            insert(a[i],1);
            ans+=(i-sum(a[i]));
        }
        printf("Scenario #%d:\n",num);
        printf("%lld\n",ans);
        if(num!=t)
            printf("\n");
    }
    return 0;
}

树状数组时间空间上都没有归并好~~~~~~~

归并求逆序数 && 线段树求逆序数

标签:des   style   blog   http   color   io   os   ar   java   

原文地址:http://www.cnblogs.com/zhangying/p/3995496.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!