Inversion (hdu 4911 树状数组 || 归并排序 求逆序对)

Inversion

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2003    Accepted Submission(s): 787


Problem Description
bobo has a sequence a 1,a 2,…,a n. He is allowed to swap two  adjacent numbers for no more than k times.

Find the minimum number of inversions after his swaps.

Note: The number of inversions is the number of pair (i,j) where 1≤i<j≤n and a i>a j.
 

Input
The input consists of several tests. For each tests:

The first line contains 2 integers n,k (1≤n≤10 5,0≤k≤10 9). The second line contains n integers a 1,a 2,…,a n (0≤a i≤10 9).
 

Output
For each tests:

A single integer denotes the minimum number of inversions.
 

Sample Input
  
  
3 1 2 2 1 3 0 2 2 1
 

Sample Output
  
  
1 2
 

Author
Xiaoxu Guo (ftiasch)
 

Source
 

Recommend
We have carefully selected several similar problems for you:   5197  5196  5193  5192  5189 
 


题意:求n个数的逆序对数,可以交换k次相邻的,所以求出原序列的逆序对后减去k即可。

思路:求逆序对有两种方法,归并排序和树状数组。逆序对的几种求法

代码:

//树状数组
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 100005
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

int n,k,len;
int bit[maxn];
int a[maxn],num[maxn];
map<int,int>pos;

int cmp(int a,int b)
{
    return a>b;
}

int Lowbit(int i)
{
    return i&-i;
}

ll sum(int i)
{
    ll s=0;
    while (i>0){
        s+=bit[i];
        i-=Lowbit(i);
    }
    return s;
}

void add(int i,int x)
{
    while (i<maxn)
    {
        bit[i]+=x;
        i+=Lowbit(i);
    }
}

ll solve()
{
    int i,j;
    ll ans=0;
    FRE(i,1,n)
    {
        ans+=sum(pos[num[i]]-1);
        add(pos[num[i]],1);
    }
    ans-=k;
    if (ans<0) ans=0;
    return ans;
}

int main()
{
    int i,j;
    while (~sff(n,k))
    {
        pos.clear();
        mem(bit,0);
        FRE(i,1,n)
        {
            sf(a[i]);
            num[i]=a[i];
        }
        sort(a+1,a+n+1,cmp);
        len=1;
        pos[a[1]]=len;
        FRE(i,2,n)  //map去重,离散化
            if (a[i]!=a[i-1])
                pos[a[i]]=++len;
        pf("len=%d\n",len);
        pf("%I64d\n",solve());
    }
    return 0;
}

//归并排序
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 100005
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

int a[maxn];
int n,k;
__int64 ans;

void Merge(int a[],int l,int mid,int r)
{
    int i,j,k=l;
    int *b=new int[r+1];
    FRE(i,l,r) b[i]=a[i];
    i=l,j=mid+1;
    while (i<=mid&&j<=r)
    {
        if (b[i]<=b[j])
            a[k++]=b[i++];
        else{
            a[k++]=b[j++];
            ans+=(mid-i+1);
        }
    }
    while (i<=mid) a[k++]=b[i++];
    while (j<=r) a[k++]=b[j++];
    delete []b;
}

void Merge_sort(int a[],int l,int r)
{
    if (l==r) return ;
    int mid=(l+r)>>1;
    Merge_sort(a,l,mid);
    Merge_sort(a,mid+1,r);
    Merge(a,l,mid,r);
}

int main()
{
    int i,j;
    while (~sff(n,k))
    {
        FRE(i,1,n)
            sf(a[i]);
        ans=0;
        Merge_sort(a,1,n);
        ans-=k;
        if (ans<0) ans=0;
        pf("%I64d\n",ans);
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值