哈理工 1400 汽车比赛

汽车比赛

Time Limit: 1000 MS

Memory Limit: 65536 K

Total Submit: 388(95 users)

Total Accepted: 146(80 users)

Rating:

Special Judge: No

Description

XianGe非常喜欢赛车比赛尤其是像达喀尔拉力赛,这种的比赛规模很大,涉及到很多国家的车队的许多车手参赛。XianGe也梦想着自己能举办一个这样大规模的比赛,XianGe幻想着有许多人参赛,那是人山人海啊,不过XianGe只允许最多100000人参加比赛。

这么大规模的比赛应该有技术统计,在XianGe的比赛中所有车辆的起始点可能不同,速度当然也会有差异。XianGe想知道比赛中会出现多少次超车(如果两辆车起点相同速度不同也算发生一次超车)。


 

Input

本题有多组测试数据,第一行一个整数n,代表参赛人数,接下来n行,每行输入两个数据,车辆起始位置X i 和速度 V i(0<Xi,Vi<1000000)

Output

输出比赛中超车的次数,每组输出占一行

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

Author

杨和禹

 

 

 

这个是我们理工的题目。树状数组第二次做,独立做出来了 ,不过走了点儿弯路,一开始 是对位置排序 发现这样会导致很复杂,然而 如果直接对速度先排好顺序的话 ,那就会方便好多。注意到如果,位置相同,速度要递增排。

对了

#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
using namespace std;

const int M=100000+2;
int cnt[M];

struct A
{
    int s,v;
} per[M*10];

bool cmp(A a,A b)
{
    if(a.s==b.s)
        return a.v<b.v;
    return a.s>b.s;
}

int lowbit(int x)
{
    return x&(-x);
}
long long  sum(int x)
{
    long long  s=0;

    while(x>0)
    {
        s+=cnt[x];
        x-=lowbit(x);
    }
    return s;
}

void add(int x,int val)
{
    while(x<=M)
    {
        cnt[x]+=val;
        x+=lowbit(x);
    }
}

int main()
{
    int N;
    while(cin>>N)
    {
        int x,y;
        memset(cnt,0,sizeof(cnt));
        for(int i=0; i<N; i++)
        {
            cin>>x>>y;
            per[i].s=x;
            per[i].v=y;

        }
        sort(per,per+N,cmp);
        long long  result=0;
        for(int j=0; j<N; j++)
        {
            add(per[j].v,1);
            result+=(sum(per[j].v-1));

//cout<<"sum="<<sum(per[j].v-1)<<endl<<"result="<<result<<endl;
        }
        printf("%lld\n",result);

    }
    return 0;
}
/**
4
7 3
2 3
4 6
4 6
*/


 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值