UESTC - 931 Car race game 逆序数

Car race game

Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
 

Bob is a game programming specialist. In his new car race game, there are some racers( n n means the amount of racers  (1n100000) (1≤n≤100000)) racers star from someplace( xi xi means Starting point coordinate),and they possible have different speed( V 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 n-determining the number of racers. Next  n n lines follow, each line contains two integer  Xi Xi and  Vi Vi.( xi xi means the  ith ith racer's Starting point coordinate,  Vi Vi means the ith racer's speed. 0<Xi,Vi<1000000 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 and output

Sample Input Sample Output
2
2 1
2 2
5
2 6
9 4
3 1
4 9
9 1
7
5 5
6 10
5 6
3 10
9 10
9 5
2 2
1
6
7

#include<string.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
struct data
{
    int pos,speed;
    int P=0;
} y[1000003],x[1000003];
long long a[1000033];
long long c[1000003];
long long  n;
bool cmp(data a,data b)
{
    if(a.pos==b.pos)
        return a.speed>b.speed;
    return a.pos<b.pos;
}
bool cmp1(data a,data b)
{
    if(a.speed==b.speed)
        return a.pos<b.pos;
    return a.speed<b.speed;
}
long long lowbit(long long x)
{
    return x&(-x);
}
void update(long long x)
{
    while(x<=n)
    {
        c[x]+=1;
        x+=lowbit(x);
    }
}
long long getsum(long long x)
{
    long long sum=0;
    while(x>0)
    {
        sum+=c[x];
        x-=lowbit(x);
    }
    return sum;
}
int main()
{
    while(scanf("%d",&n)==1)
    {
        memset(c,0,sizeof(c));
        for(long long i=1; i<=n; i++)
        {
            scanf("%lld%lld",&x[i].pos,&x[i].speed);
        }
        sort(x+1,x+1+n,cmp);
        for(long long i=1;i<=n;i++)
        {
            y[i].pos=i;
            y[i].speed=x[i].speed;
        }
        sort(y+1,y+1+n,cmp1);
        long long res=0;
        for(long long i=1; i<=n; i++)
        {
            a[y[i].pos]=i;
        }
        for(int i=1; i<=n; i++)
        {
            update(a[i]);
            res+=(i-getsum(a[i]));
        }
        printf("%lld\n",res);
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值