马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
 本帖最后由 勿语静候 于 2014-10-8 22:04 编辑  
 
题目大意: 给出长度为n的序列,每次只能交换相邻的两个元素,问至少要交换几次才使得该序列为递增序列。  
 
Input 59 1 0 5 4 
31 2 3 
Output 
6 
0- </blockquote></div></div><p></p><div><div class="blockcode"><blockquote>#include<iostream>
 
 - using namespace std;
 
 - #define MAXSIZE 500000
 
  
- long long t;
 
 - void merging(int* listL , int listL_size , int* listR , int listR_size)
 
 - {
 
 -         int i , j , k;
 
 -         i = j = k =0;
 
 -         int temp[MAXSIZE];
 
 -         
 
 -         while( i < listL_size && j < listR_size)
 
 -         {
 
 -                 if( listL[i] > listR[j])
 
 -                 {
 
 -                         temp[k++] = listR[j++];
 
 -                         t+=listL_size - i;
 
 -                 }
 
 -                 else
 
 -                 {
 
 -                         temp[k++] = listL[i++];
 
 -                 }
 
 -         }
 
 -         
 
 -         while(i < listL_size)
 
 -         {
 
 -                 temp[k++] = listL[i++];
 
 -         }
 
 -         while(j < listR_size)
 
 -         {
 
 -                 temp[k++] = listR[j++];
 
 -         }
 
 -         
 
 -         for(int m = 0; m < (listL_size + listR_size);m++)
 
 -         {
 
 -                 listL[m] = temp[m];
 
 -         }
 
 - }
 
  
- void mergeSort(int* k , int n)
 
 - {
 
 -         if(n > 1)
 
 -         {
 
 -                 int *listL = k;
 
 -                 int listL_size = n / 2;
 
 -                 int *listR = k + n / 2;
 
 -                 int listR_size = n - listL_size;
 
 -                 
 
 -                 mergeSort(listL , listL_size);
 
 -                 mergeSort(listR , listR_size);
 
 -                 
 
 -                 merging(listL , listL_size , listR, listR_size);
 
 -         }
 
 - }
 
  
- int main()
 
 - {
 
 -         int n;
 
 -         while(cin >> n)
 
 -         {
 
 -                 t = 0;
 
 -                 int* a = new int[n+1];
 
 -                 
 
 -                 for(int i = 0 ; i < n ; i++)
 
 -                 {
 
 -                         cin >> a[i];
 
 -                 }
 
 -                 
 
 -                 mergeSort(a,n);
 
 -                 
 
 -                 cout << t << endl;
 
 -                 
 
 -                 delete a;
 
 -         }
 
  
-         return 0;
 
 - }
 
 
  复制代码 
 
 
 
 
 
 
 
 
 
 
 |