归并排序 树状数组 求逆序数

Car race game

Description

Bob is a game programming specialist. In his new car race game, there are some racers(n means the amount of racers (1<=n<=100000)) racers star from someplace(xi means Starting point coordinate),and they possible have different speed(V means speed).so it possibly takes place to overtake(include staring the same point ). now he want to calculate the maximal amount of overtaking.

Input

The first line of the input contains an integer n-determining the number of racers.    Next n lines follow, each line contains two integer Xi and Vi.(xi means the ith racer's Starting point coordinate, Vi means the ith racer's speed.0<Xi, Vi<1000000).

Output

For each data set in the input print on a separate line, on the standard output, the integer that represents the maximal amount of overtaking.

Sample Input

2
2 1
2 2
5
2 6
9 4
3 1
4 9
9 1

Sample Output

1
6

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<string>
#include<cmath>
#include<cstring>
#define N 1000010
using namespace std;
int a[N],t[N];
long long cnt;
struct ss
{
    int x;
    int v;
}sss[N];
int cmp(ss a,ss b)
{
    if(a.x!=b.x)return a.x<b.x;
    else return a.v>b.v;
}
void merge_sort(int *a,int *t,int x,int y)
{
    if(y-x>1)
    {
        int m=x+(y-x)/2;
        int p=x,q=m,i=x;
        merge_sort(a,t,x,m);
        merge_sort(a,t,m,y);
        while(p<m||q<y)
        {
            if(q>=y||(p<m&&a[p]<=a[q]))t[i++]=a[p++];
            else
            {
                t[i++]=a[q++];
                cnt+=m-p;
            }
             
        }
        for(i=x;i<y;i++)a[i]=t[i];
    }
}
int main()
{
    int n,i;
    while(~scanf("%d",&n))
    {
        for(i=0;i<n;i++)scanf("%d%d",&sss[i].x,&sss[i].v);
        sort(sss,sss+n,cmp);
        for(i=0;i<n;i++)a[i]=sss[i].v;
        cnt=0;
        merge_sort(a,t,0,n);
        printf("%lld\n",cnt);
 
    }
     
    return 0;
}
 

也可用树状数组写
概念:http://blog.csdn.net/zhoujiachengdhy/article/details/22382719
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define low(x) x&(-x)
#define N 1000010
#define ll long long
struct ss
{
    int x,v;
}p[N];
int n;
ll c[N];
void add(int i,int val)
{
    while(i<=N)
    {
        c[i]+=val;
        i+=low(i);
    }
}
ll getsum(int i)
{
    ll sum=0;
    while(i>0)
    {
        sum+=c[i];
        i-=low(i);
    }
    return sum;
}
bool cmp(ss a,ss b)
{
    if(a.x!=b.x)return a.x<b.x;
    return a.v>b.v;
}
void inti()
{
    memset(c,0,sizeof(c));
    int i;
    for(i=1;i<=n;i++)scanf("%d%d",&p[i].x,&p[i].v);
    sort(p+1,p+n+1,cmp);
}
int main()
{
    int t,i,k=1;
    while(~scanf("%d",&n))
    {
       inti();
       long long ans=0;
       for(i=1;i<=n;i++)
       {
           add(p[i].v+1,1);
           ans+=i-getsum(p[i].v+1);
       }
       printf("%lld\n",ans);
    }
    return 0;
}
注意问题 
1 数据统一都+1
2 数据离散化后WA,不知道为什么


HDU4911 求逆序数,转置不超过k次后的最小逆序数,两个数转置不会影响前后的逆序数,试了好几个例子,感觉只需要用原来的逆序数减去k即可
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<string>
#include<cmath>
#include<cstring>
#define N 1000010
using namespace std;
int a[N],t[N];
long long cnt;
struct ss
{
    int x;
    int v;
}sss[N];
int cmp(ss a,ss b)
{
    if(a.x!=b.x)return a.x<b.x;
    else return a.v>b.v;
}
void merge_sort(int *a,int *t,int x,int y)
{
    if(y-x>1)
    {
        int m=x+(y-x)/2;
        int p=x,q=m,i=x;
        merge_sort(a,t,x,m);
        merge_sort(a,t,m,y);
        while(p<m||q<y)
        {
            if(q>=y||(p<m&&a[p]<=a[q]))t[i++]=a[p++];
            else
            {
                t[i++]=a[q++];
                cnt+=m-p;
            }

        }
        for(i=x;i<y;i++)a[i]=t[i];
    }
}
int main()
{
    int n,i,k;
    while(~scanf("%d%d",&n,&k))
    {
        for(i=0;i<n;i++)scanf("%d",&a[i]);
        cnt=0;
        merge_sort(a,t,0,n);
        if(k<=cnt)printf("%I64d\n",cnt-k);
        else printf("0\n");
    }
    return 0;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
树状数组(Fenwick tree)是一种用于高效解数列前缀和(Prefix Sum)问题的数据结构,而逆序对也是一种经典的问题。 假设有个长度为 n 的数组 a,我们要其中逆序对的数量,即所有 i < j 且 a[i] > a[j] 的 (i,j) 对数。这个问题可以用归并排序的思想解,但是这里我们介绍一种使用树状数组的解法。 思路如下: 1. 将原数组 a 复制一份并排序,得到新数组 b,将 b 中的每个元素在原数组 a 中的下标记录在数组 c 中。 2. 从右往左遍历 a,对于每个元素 a[i],在树状数组中查询前缀和 sum(c[i]-1),即小于 a[i] 的元素个数,将结果累加到逆序对计数器 ans 中。 3. 将 a[i] 在数组 c 中对应的位置在树状数组中更新为 1。 下面是使用 Python 实现的代码: ```python def lowbit(x): return x & -x def update(bit, x, val): n = len(bit) while x <= n: bit[x] += val x += lowbit(x) def query(bit, x): res = 0 while x > 0: res += bit[x] x -= lowbit(x) return res def count_inversions(a): n = len(a) b = sorted(a) c = {v: i+1 for i, v in enumerate(b)} bit = [0] * (n+1) ans = 0 for i in range(n-1, -1, -1): ans += query(bit, c[a[i]]-1) update(bit, c[a[i]], 1) return ans ``` 其中,lowbit 函数是计算 x 的最低位 1 所代表的值;update 函数是树状数组的更新操作;query 函数是树状数组的查询操作;count_inversions 函数是主函数,用于计算逆序对数量。 时间复杂度为 O(nlogn)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值