hdu 3743 Frosh Week(求逆序数方法总结)

Frosh Week

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1785    Accepted Submission(s): 592


Problem Description
During Frosh Week, students play various fun games to get to know each other and compete against other teams. In one such game, all the frosh on a team stand in a line, and are then asked to arrange themselves according to some criterion, such as their height, their birth date, or their student number. This rearrangement of the line must be accomplished only by successively swapping pairs of consecutive students. The team that finishes fastest wins. Thus, in order to win, you would like to minimize the number of swaps required.
 

Input
The first line of input contains one positive integer n, the number of students on the team, which will be no more than one million. The following n lines each contain one integer, the student number of each student on the team. No student number will appear more than once. 
 

Output
Output a line containing the minimum number of swaps required to arrange the students in increasing order by student number. 
 

Sample Input
   
   
   
   
3 3 1 2
 

Sample Output
   
   
   
   
2
 

题意好难懂,其实就是求逆序数,因为存在每次相邻的两个数交换,可以使逆序数减一。

这几天在学树状数组,就用树状数组做了一下,用归并排序也可以。

方法一:树状数组
 
//time 140ms
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
const int maxn=100000+100;
using namespace std;
struct node
{
    int id;
    int v;
}tree[maxn];
int a[maxn];
int low(int k)
{
    return k&(-k);
}
int getsum(int k)
{
    int ans=0;
    while(k>0)
    {
        ans+=a[k];
        k-=low(k);
    }
    return ans;
}
void update(int k,int v)
{
    while(k<maxn)
    {
        a[k]+=v;
        k+=low(k);
    }
}
bool cmp(node a,node b)
{
    return a.v<b.v;
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&tree[i].v);
            tree[i].id=i;
        }
        sort(tree+1,tree+1+n,cmp);
        memset(a,0,sizeof(a));
        long long ans=0;
        for(int i=n;i>=1;i--)
        {
           ans+=getsum(tree[i].id);
           update(tree[i].id,1);
        }
        printf("%I64d\n",ans);
    }
    return 0;
}


方法二: 归并排序

//time 109ms
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int maxn=100100;
const int maxint= 999999999;
int a[maxn];
int lef[maxn];
int righ[maxn];
int n;
long long ans;
void sort(int l,int temp,int r)
{
    int n1=temp-l+1;
    int n2=r-temp;
    for(int i=0;i<n1;i++)
    lef[i]=a[i+l];
    for(int i=0;i<n2;i++)
    righ[i]=a[temp+i+1];
    lef[n1]=righ[n2]=maxint;
    int i=0;
    int j=0;
    for(int k=l;k<=r;k++)
    {
        if(lef[i]<=righ[j])
        {
            a[k]=lef[i];
            i++;
        }
        else
        {
            a[k]=righ[j];
            j++;
            ans+=n1-i;//计算逆序数,在lef数组中有n1-i个比righ[j]大的
        }
    }
    return;

}
void memsort(int l,int r)
{
    int temp=(l+r)/2;
    if(l<r)
    {
    memsort(l,temp);
    memsort(temp+1,r);
    sort(l,temp,r);
    }
    return;
}
int main()
{
    while(~scanf("%d",&n))
    {
        ans=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        memsort(0,n-1);//归并排序
        printf("%I64d\n",ans);
    }
    return 0;
}



你可能感兴趣的:(Algorithm)