POJ 1804

简介: 题目:http://poj.org/problem?id=1804 大意:给你一串数字,排序。求出最少的交换次数  \ 我用归并做的 #include #include using namespace std; int aa[500010],bb[500010]; long lon...

题目:http://poj.org/problem?id=1804

大意:给你一串数字,排序。求出最少的交换次数  \

我用归并做的

#include<iostream>
#include<cstring>
using namespace std;
int aa[500010],bb[500010];
long long  s=0;
void merge(int l,int m,int r)
{
     int i=l,j=m+1,t=0;
     while(i<=m&&j<=r)
     {
          if(aa[i]>aa[j])
          {
               bb[t++]=aa[j++];
               s+=m-i+1;
          }
          else
          {
               bb[t++]=aa[i++];
          }
     }
     while(i<=m)
          bb[t++]=aa[i++];
     while(j<=r)
          bb[t++]=aa[j++];
     for(int i=0;i<t;i++)     //并!不能省,否则归并排序不完整
     {
          aa[l+i]=bb[i];
     }
}
void Msort (int L,int R)   
{
    int cen;
    if(L<R)
    { cen=(L+R)/2;
    Msort(L,cen);
    Msort(cen+1,R);
    merge(L,cen,R);

    }

}
void merge_sort(int *a,int n)
{ Msort(0,n-1);     //做接口;

}
int main()
{
    int n,d=1;
    cin>>n;

    for(int i=0;i<n;i++)
    {   memset(aa,0,sizeof(aa));
        memset(bb,0,sizeof(bb));

       int q;
        cin>>q;

        if(q==0)break;

    for(int j=0;j<q;j++)
    {
        cin>>aa[j];
    }
    merge_sort(aa,q);
cout<<"Scenario #"<<d<<':'<<endl;
    cout<<s<<endl<<endl;
     s=0;d++;

     }
    return 0;

}

 

相关文章
|
人工智能
POJ 3104 Drying
POJ 3104 Drying
|
人工智能 BI
|
并行计算 网络架构
poj-1005-l tanink i need a houseboat
Description Fred Mapper is considering purchasing some land in Louisiana to build his house on. In the process of investigating the land, he learned ...
990 0
|
人工智能
POJ 1936 All in All
Description You have devised a new encryption technique which encodes a message by inserting between its characters randomly generated strings in a clever way.
796 0
POJ 2262 Goldbach&#39;s Conjecture
Problem Description In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the foll...
1014 0
|
机器学习/深度学习
|
机器学习/深度学习