1. 程式人生 > >正序數?還是逆序數。洛谷P1428

正序數?還是逆序數。洛谷P1428

//歸併排序求正序數?(逆序)!!!
#include<stdio.h>
#include<string.h>
using namespace std;
#define maxn 1000005
struct node
{
    int value;
    int mark;
}a[maxn];
long long sum;
int ans[maxn];
void merge(int l,int r,int m){
    node temp[maxn];
    printf("%d %d %d....\n",l,r,m);//輸出回溯過程
    int i = l, j = m + 1,k = l;
    while(i<=m&&j<=r){
        if(a[i].value >= a[j].value)
            temp[k++] = a[i++];
        else
        {
            printf("%d %d %d\n",j,a[j].mark,m-i+1);
            ans[a[j].mark] += m-i+1;
            temp[k++] = a[j++];
        }
    }
    while(i<=m)
        temp[k++] = a[i++];
    while(j<=r)
        temp[k++] = a[j++];
    for(i = l;i<=r;i++)
        a[i] = temp[i];
}
void mergesort(int l,int r)
{
    if(l < r)
    {
        int m = (l + r) / 2;
        mergesort(l,m);
        mergesort(m+1,r);
        merge(l,r,m);
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(ans,0,sizeof(ans));
        int n;
        scanf("%d",&n);
        for(int i = 1;i <= n;i++)
        {
            scanf("%d",&a[i].value);
            a[i].mark = i;
        }
        mergesort(1,n);
        for(int i = 1 ; i <= n ; i ++)
            printf("%d ",ans[i]);
    }
    return 0;
}