Car race game 归并排序求逆序数

Car race game

Time Limit 1000ms

Memory Limit 65536K

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).<="" font="">
							

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
7
5 5
6 10
5 6
3 10
9 10
9 5
2 2
							

sample_output

1
6
7
						
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<queue>
#include<stack>
#include<set>
#include<map>
#define CLEAR(a) memset((a),0,sizeof((a)))

using namespace std;

typedef long long LL;
const double pi = acos(-1.0);
const int maxn=1e5+10;
const int inf=99999999;
const float eps=1e-3;

struct dat
{
    int x,v;
    bool operator< (const dat a) const
    {
        if (v==a.v) return x<a.x;
        else return v<a.v;
    }
} data[maxn];
map<int,bool> mp;
//vector<int> vec;

LL n,ans,cnt;
LL a[maxn],t[maxn];

void init();
void solve();
void outp();
void Merge(LL *a,int x,int y,LL *t)
{
    if(y-x>1)
    {
        int m=x+(y-x)/2;
        int p=x,q=m,i=x;
        Merge(a,x,m,t);
        Merge(a,m,y,t);
        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(int i=x;i<y;i++) {a[i]=t[i];}
    }
}

int main()
{
    while(~scanf("%d",&n))
        {
            init();
            Merge(a,1,n+1,t);
            outp();
        }
    return 0;
}

void solve()
{
}

void init()
{
    ans=0;
    mp.clear();
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&data[i].x,&data[i].v);
        if (mp[data[i].x]>0) ans++;
        mp[data[i].x]=1;
    }
    sort(data+1,data+n+1);
    //vec.clear();
    CLEAR(a);CLEAR(t);
    for(int i=1;i<=n;i++) //vec.push_back(data[i].x);
    {
        a[++a[0]]=data[i].x;
    }
    cnt=0;
}

void outp()
{
    printf("%lld\n",ans+cnt);
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值