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

本文主要介绍了在Frosh Week中遇到的求逆序数问题,详细讲解了各种求解逆序数的方法,帮助读者理解和掌握相关算法。
摘要由CSDN通过智能技术生成

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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值