husj oj

F - Inversions
Time Limit:500MS     Memory Limit:4096KB     64bit IO Format:%I64d & %I64u

Description

There are N integers (1<=N<=65537) A1, A2,.. AN (0<=Ai<=10^9). You need to find amount of such pairs (i, j) that 1<=i<j<=N and A[i]>A[j].

Input

The first line of the input contains the number N. The second line contains N numbers A1...AN.

Output

Write amount of such pairs.

Sample Input


Input
 
   
5 2 3 1 5 4
Output
 
   
3


仍然是逆序数。不知道哪个oj上的

要用__int64,不能用long long

离散化的时候要注意相同的值

还有注意从后往前插,查询比其小的数字的个数

不需要多组数据。(tle n次)

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxn=65540;

int a[maxn];
int c[maxn];

struct Item
{
    int val;
    int pos;
}item[maxn];

bool cmp(Item a,Item b)
{
    return a.val<b.val;
}

int n;

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

void update(int x,__int64 val)
{
    for(int i=x;i<n;i+=Lowbit(i))
        a[i]+=val;
}

__int64 query(int x)
{
    __int64 ans=0;
    for(int i=x;i>0;i-=Lowbit(i))
       ans+=a[i];
    return ans;
}

int main()
{
   __int64 ans=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
            scanf("%d",&item[i].val);
            item[i].pos=i;
    }
    sort(item+1,item+n+1,cmp);

    c[item[1].pos]=1;
    for(int i=2;i<=n;i++){
        if(item[i].val!=item[i-1].val)
            c[item[i].pos]=i;
        else
            c[item[i].pos]=c[item[i-1].pos];
    } 
    for(int i=n;i>=1;i--){
        update(c[i],1);
        ans+=query(c[i]-1);
    }
    printf("%I64d\n",ans);
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值