sgu 180. Inversions (树状数组+离散化,第一道需要改模板的题目,好题)

1、http://blog.csdn.net/sdjzping/article/details/20381665

2、题目大意:

有n个数字,定义i<j并且a[i]>a[j]这样的一对数为逆序对,求这个数中有多少逆序对,

3、题目分析

看着题目很简单,但是n=65537,两重for循环找必超时,所以想到了用树状数组,但是交了好几遍都错了,runtime error,原因就是数组中的数字可以到达10^9,数组肯定要超内存,此题需要用到离散化,但是数字之间可能有重复,这一点没考虑到,有wrong 了好几遍,不过终于是改对了,。

4、题目:

180. Inversions
time limit per test: 0.25 sec.
memory limit per test: 4096 KB
input: standard
output: standard




 

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 test(s)

Input

 

 
5 2 3 1 5 4
 

Output

 

 
3
 

5、AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 65540
#define ll long long
int n;
int c[N];
struct node
{
    int id;
    int v;
}a[N];
node b[N];
int cmp(node a,node b)
{
    return a.v<b.v;
}
int cmp1(node a,node b)
{
    return a.id<b.id;
}

int lowbit(int i)
{
    return i&(-i);
}
void update(int x,int v)
{
    for(int i=x;i<=n;i+=lowbit(i))
    {
        c[i]+=v;
    }
}
ll getsum(int x)
{
    ll sum=0;
    //此处注意需要计算的是比自己小的,所以不包含自己
    for(int i=x-1;i>=1;i-=lowbit(i))
    {
        sum+=c[i];
    }
    return sum;
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        memset(c,0,sizeof(c));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i].v);
            a[i].id=i;
        }
        sort(a+1,a+n+1,cmp);
        a[0].v=-1;
        int k=1;
        //注意离散化的时候考虑相等的情况
        for(int i=1;i<=n;i++)
        {
            if(a[i].v!=a[i-1].v)
            {
                b[i].id=a[i].id;
                b[i].v=k++;
            }
            else
            {
                b[i].id=a[i].id;
                b[i].v=b[i-1].v;
            }
        }
        sort(b+1,b+n+1,cmp1);
        ll sum=0;
        for(int i=n;i>=1;i--)
        {
            sum+=getsum(b[i].v);
            update(b[i].v,1);
        }
        printf("%lld\n",sum);
    }
    return 0;
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值