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

本文介绍了一种算法,用于解决赛车游戏中计算最大超车次数的问题。通过输入赛车的起始位置和速度,该算法能高效地计算出可能发生的最大超车次数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.youkuaiyun.com/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;
}






内容概要:本文从关键概念、核心技巧、应用场景、代码案例分析及未来发展趋势五个维度探讨了Python编程语言的进阶之路。关键概念涵盖装饰器、生成器、上下文管理器、元类和异步编程,这些概念有助于开发者突破基础认知的核心壁垒。核心技巧方面,介绍了内存优化、性能加速、代码复用和异步处理的方法,例如使用生成器处理大数据流、numba库加速计算密集型任务等。应用场景展示了Python在大数据处理、Web开发、人工智能和自动化运维等多个领域的广泛运用,特别是在FastAPI框架中构建异步API服务的实战案例,详细分析了装饰器日志记录、异步数据库查询和性能优化技巧。最后展望了Python的未来发展趋势,包括异步编程的普及、类型提示的强化、AI框架的深度整合以及多语言协同。 适合人群:已经掌握Python基础语法,希望进一步提升编程技能的开发者,特别是有意向从事数据科学、Web开发或AI相关工作的技术人员。 使用场景及目标:①掌握Python进阶概念和技术,如装饰器、生成器、异步编程等,提升代码质量和效率;②学习如何在实际项目中应用这些技术,如通过FastAPI构建高效的异步API服务;③了解Python在未来编程领域的潜在发展方向,为职业规划提供参考。 阅读建议:本文不仅提供了理论知识,还包含了丰富的实战案例,建议读者在学习过程中结合实际项目进行练习,特别是尝试构建自己的异步API服务,并通过调试代码加深理解。同时关注Python社区的发展动态,及时掌握最新的技术和工具。
内容概要:本文档《Rust系统编程实战》详细介绍了Rust在系统编程领域的应用,强调了其内存安全、零成本抽象和高性能的特点。文档分为三个主要部分:核心实战方向、典型项目案例和技术关键点。在核心实战方向中,重点讲解了unsafe编程、FFI(外部函数接口)和底层API调用,涉及操作系统组件开发、网络编程、设备驱动开发、系统工具开发和嵌入式开发等多个领域,并列出了每个方向所需的技术栈和前置知识。典型项目案例部分以Linux字符设备驱动为例,详细描述了从环境搭建到核心代码实现的具体步骤,包括使用bindgen生成Linux内核API的Rust绑定,定义设备结构体,以及实现驱动核心函数。 适合人群:对系统编程有兴趣并有一定编程基础的开发者,尤其是那些希望深入了解操作系统底层机制、网络协议栈或嵌入式系统的工程师。 使用场景及目标:①掌握Rust在不同系统编程场景下的应用,如操作系统组件开发、网络编程、设备驱动开发等;②通过实际项目(如Linux字符设备驱动)的学习,理解Rust与操作系统内核的交互逻辑;③提高对unsafe编程、FFI和底层API调用的理解和运用能力。 阅读建议:由于文档内容较为深入且涉及多个复杂概念,建议读者在学习过程中结合实际操作进行练习,特别是在尝试实现Linux字符设备驱动时,务必按照文档提供的步骤逐步进行,并多加调试和测试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值