zoukankan      html  css  js  c++  java
  • XidianOJ 1024 简单逆序对

    题目描述

    逆序对问题对于大家来说已经是非常熟悉的问题了,就是求i<j时,a[i] > a[j]的组数。现在请你求出一串数字中的逆序对的个数,需要注意的是,这些数字均在[0,9]之内。

    输入

    第一行输入T,表示有T组测试数据
    对于每组数据,首先输入n,代表有n个数(0<n<=10^6)
    接下来输入n个数,每个数都在[0,9]之内

    输出

    输出逆序对的个数,且对10^9+7取模

    --正文

    使用的是归并排序求逆序对,其实还有很多种

    PS: 打10^9的时候老是不小心多打个0少打个0,WA了半天,诶

    #include <stdio.h>
    
    int a[1000001];
    int tempa[1000001];
    long long total = 0;
    void merge(int left,int mid,int right){
        int endl = mid - 1,endr = right;
        int L = left,R = mid;
        int Tmp = left;
        int NumElements = right - left + 1; 
        while (L<= endl && R <= endr) {
            if (a[L] <= a[R]) {
                tempa[Tmp++] = a[L++];
            }
            else {
                total += endl - L + 1;
                tempa[Tmp++] = a[R++];
            }
        } 
        while (L<=endl){
            tempa[Tmp++] = a[L++];
        }
        while (R<=endr){
            tempa[Tmp++] = a[R++];
        }
        int i;
        for (i=0;i<=NumElements;i++,endr--){
            a[endr] = tempa[endr];
        }
    }
    
    void mergesort(int left,int right){
        int mid = (left + right) / 2;
        if (left < right){
            mergesort(left,mid);
            mergesort(mid+1,right);
            merge(left,mid+1,right);
        }
    } 
    
    int main() {
        int time,T;
        scanf("%d",&T);
        for (time=1;time<=T;time++){
            int n;
            scanf("%d",&n);
            int i;
            for (i=1;i<=n;i++){
                scanf("%d",&a[i]);
            }
            total = 0;
            mergesort(1,n); 
            printf("%lld
    ",total % 1000000007);
        }
        
        return 0;
    }
  • 相关阅读:
    字符串提取数字/汉字/英文字母
    CHARINDEX,PATINDEX,STUFF函数
    raiserror的用法
    数据库备份与还原(通过命令)
    查询某个字段属于哪些表
    设备驱动基础1:设备模型之总线,驱动,设备
    设备模型之kobject,kset及其关系
    模拟电路创新设计
    cdev、udev
    PCB阻抗调节
  • 原文地址:https://www.cnblogs.com/ToTOrz/p/6064084.html
Copyright © 2011-2022 走看看