1 条题解

  • 0
    @ 2021-6-15 10:09:34

    C++ :

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cstdlib>
    #define rep(i,a,n) for(int i=a;i<=n;++i)
    #define mod 99999997
    #define N 100010
    using namespace std;
    int n,a[N],b[N],c[N],d[N],ans;
    bool cmp(const int &x,const int &y){return a[x]<a[y];}
    bool cmp2(const int &x,const int &y){return b[x]<b[y];}
    void merge(int l,int r)
    {
        if(l==r)return;int mid=((l+r)>>1);merge(l,mid);merge(mid+1,r);
        int i=l,j=mid+1,tot=l;
        while(i<=mid&&j<=r)
        {
            if(a[i]<a[j])b[tot++]=a[i++];
            else b[tot++]=a[j++],ans=(ans+mid-i+1)%mod;
        }
        while(i<=mid)b[tot++]=a[i++];
        while(j<=r)b[tot++]=a[j++];
        rep(i,l,r)a[i]=b[i];
    }
    int main()
    {
        scanf("%d",&n);
    	rep(i,1,n)scanf("%d",&a[i]);
    	rep(i,1,n)scanf("%d",&b[i]);
        rep(i,1,n)c[i]=d[i]=i;
        sort(c+1,c+1+n,cmp);
    	sort(d+1,d+1+n,cmp2);
        rep(i,1,n)a[d[i]]=c[i];
        merge(1,n);
        printf("%d\n",ans);
        return 0;
    }
    
    

    Pascal :

    type typ=array[1..100000]of longint;
    var a,b,k:typ;i,n,sum,s:longint;
    procedure sort(l,r:longint;var a,k:typ);
    var i,j,x,t:longint;
    begin
     i:=l;j:=r;x:=a[(l+r)div 2];
     repeat
      while a[i]<x do inc(i);
      while a[j]>x do dec(j);
      if i<=j then
       begin
        t:=a[i];a[i]:=a[j];a[j]:=t;
        t:=k[i];k[i]:=k[j];k[j]:=t;
        inc(i);dec(j)
       end
     until i>j;
     if i<r then sort(i,r,a,k);
     if l<j then sort(l,j,a,k)
    end;
    function guibing(l,r:longint;var a:typ):longint;
    var i,j,m,n:longint;
    begin
     guibing:=0;
     m:=(l+r)div 2;n:=r-l+1;
     if l<m then guibing(l,m,a);
     if m<r then guibing(m+1,r,a);
     s:=1;i:=l;j:=m+1;
     repeat
      if (i<=m)and((a[i]<=a[j])or(j>r)) then
       begin
        k[s]:=a[i];
        inc(i)
       end else
       begin
        k[s]:=a[j];
        inc(j);
        inc(sum,m-i+1)
       end;
      inc(s)
     until s>n;
     for i:=l to r do
      a[i]:=k[i-l+1];
     sum:=sum mod 99999997
    end;
    begin
     readln(n);
     for i:=1 to n do
      begin
       read(a[i]);
       k[i]:=i
      end;
     readln;
     sort(1,n,a,k);
     for i:=1 to n do
      begin
       read(b[i]);
       a[i]:=k[i];
       k[i]:=i
      end;
     readln;
     sort(1,n,b,k);
     for i:=1 to n do
      b[k[i]]:=a[i];
     sum:=0;
     guibing(1,n,b);
     writeln(sum)
    end.
    
    • 1

    信息

    ID
    316
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者