• 【洛谷 1908】逆序对


    题目描述

    猫猫TOM和小老鼠JERRY最近又较量上了,但是毕竟都是成年人,他们已经不喜欢再玩那种你追我赶的游戏,现在他们喜欢玩统计。最近,TOM老猫查阅到一个人类称之为“逆序对”的东西,这东西是这样定义的:对于给定的一段正整数序列,逆序对就是序列中ai>aj且i<j的有序对。知道这概念后,他们就比赛谁先算出给定的一段正整数序列中逆序对的数目。
    Update:数据已加强。

    输入格式

    第一行,一个数n,表示序列中有n个数。

    第二行n个数,表示给定的序列。序列中每个数字不超过10^9109

    输出格式

    给定序列中逆序对的数目。

    输入输出样例

    输入 #1
    6
    5 4 2 6 3 1
    
    输出 #1
    11

    说明/提示

    对于25%的数据,n leq 2500n2500

    对于50%的数据,n leq 4 imes 10^4n4×104。

    对于所有数据,n leq 5 imes 10^5n5×105

    请使用较快的输入输出

    应该不会n方过50万吧 by chen_zhe

    题解:临时学(背)了个归并排序……希望考试能用的上嗯。

    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    typedef long long ll;
    using namespace std;
    const int N=1000002;
    int n,a[N],r[N];
    ll ans=0;
    void masort(int s,int t){
        if(s==t) return ;
        int m=(s+t)>>1;
        masort(s,m); masort(m+1,t);
        int i=s,k=s,j=m+1;
        while(i<=m && j<=t){
            if(a[i]<=a[j]) r[k++]=a[i++];
            else { r[k++]=a[j++]; ans+=(m-i+1); }
        } 
        while(i<=m) r[k++]=a[i++];
        while(j<=t) r[k++]=a[j++];
        for(int i=s;i<=t;i++) a[i]=r[i];
    }
    
    int main(){
        freopen("1908.in","r",stdin);
        freopen("1908.out","w",stdout);
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        masort(1,n);
        cout<<ans<<endl;
        return 0;
    }
  • 相关阅读:
    Python第三方库SnowNLP(Simplified Chinese Text Processing)快速入门与进阶
    Python第三方库SnowNLP(Simplified Chinese Text Processing)快速入门与进阶
    用python玩微信(聊天机器人,好友信息统计)
    BIOS与UEFI
    MBR&/BOOT&GRUB
    Hard Disk Driver(GPT)
    Hard Disk Drive(MBR)
    反Secure Boot垄断:兼谈如何在Windows 8电脑上安装Linux
    硬盘分区基本知识
    计算机启动过程
  • 原文地址:https://www.cnblogs.com/wuhu-JJJ/p/11865103.html
Copyright © 2020-2023  润新知