zoukankan      html  css  js  c++  java
  • NEFU 700 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
    							

    ------------

    将speed离散化。

    按坐标从大到小,speed从小到大排序。

    用树状数组求出比速度spd[i]后面比spd[i]小的数。和即为答案。

    ------------

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    
    int maxn=100000;
    int n;
    
    struct CAR{
        int x;
        long long spd;
        int y;
    }a[111111];
    
    struct LS{
        int x;
        long long y;
    }lss[111111];
    
    bool cmp1(LS a,LS b)
    {
        return a.y<b.y;
    }
    
    bool cmp2(CAR a,CAR b)
    {
        if (a.x==b.x)
        {
            return a.y<b.y;
        }
        return a.x>b.x;
    }
    
    int tree[111111];
    
    int lowbit(int x)
    {
        return x&(-x);
    }
    
    void update(int x,int val)
    {
        for (int i=x;i<=maxn;i+=lowbit(i))
        {
            tree[i]+=val;
        }
    }
    
    int query(int x)
    {
        int ret=0;
        for (int i=x;i>0;i-=lowbit(i))
        {
            ret+=tree[i];
        }
        return ret;
    }
    
    int main()
    {
        while (~scanf("%d",&n))
        {
            memset(a,0,sizeof(a));
            memset(lss,0,sizeof(lss));
            memset(tree,0,sizeof(tree));
            for (int i=1;i<=n;i++)
            {
                scanf("%d%I64d",&a[i].x,&a[i].spd);
                lss[i].x=i;
                lss[i].y=a[i].spd;
            }
    
            //ÀëÉ¢
            sort(lss+1,lss+n+1,cmp1);
            int cnt=0;
            int last=-65535;
            for (int i=1;i<=n;i++)
            {
                if (lss[i].y!=last)
                {
                    last=lss[i].y;
                    cnt++;
                    a[lss[i].x].y=cnt;
                }
                else
                {
                    a[lss[i].x].y=cnt;
                }
            }
    
            maxn=0;
            for (int i=1;i<=n;i++) maxn=max(maxn,a[i].y);
            maxn++;
    
            sort(a+1,a+n+1,cmp2);
    
            long long ans=0;
            for (int i=1;i<=n;i++)
            {
                ans+=query(a[i].y-1);
                update(a[i].y,1);
            }
            printf("%lld\n",ans);
        }
        return 0;
    }
    


  • 相关阅读:
    em
    How to locate a path?
    云图说 | 揭秘云硬盘高可靠性的秘密,速来围观
    【华为云技术分享】开发团队中的任务没人领取,你头疼吗?
    介绍一种更方便的代理池实现方案
    4行Python代码生成图像验证码
    【华为云技术分享】机器学习(02)——学习资料链接
    【华为云技术分享】华为开发者大会HDC.Cloud带你探索强化学习三大挑战及落地实践
    【华为云技术分享】【一统江湖的大前端(8)】matter.js 经典物理
    华为开发者大会HDC.Cloud硬核技术解读:知识图谱构建流程及方法
  • 原文地址:https://www.cnblogs.com/cyendra/p/3226383.html
Copyright © 2011-2022 走看看