日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

Minimum Inversion Number(归并排序)

發布時間:2023/10/11 综合教程 89 老码农
生活随笔 收集整理的這篇文章主要介紹了 Minimum Inversion Number(归并排序) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Minimum Inversion Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14952    Accepted Submission(s): 9140

Problem Description
The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj.

For a given sequence of numbers a1, a2, ..., an, if we move the first m >= 0 numbers to the end of the seqence, we will obtain another sequence. There are totally n such sequences as the following:

a1, a2, ..., an-1, an (where m = 0 - the initial seqence)
a2, a3, ..., an, a1 (where m = 1)
a3, a4, ..., an, a1, a2 (where m = 2)
...
an, a1, a2, ..., an-1 (where m = n-1)

You are asked to write a program to find the minimum inversion number out of the above sequences.

 
Input
The input consists of a number of test cases. Each case consists of two lines: the first line contains a positive integer n (n <= 5000); the next line contains a permutation of the n integers from 0 to n-1.
 
Output
For each case, output the minimum inversion number on a single line.
 
Sample Input
10 1 3 6 9 0 8 5 7 4 2
 
Sample Output
16
 題解;題意就是每次把序列的第一個元素放到最后,求最小逆序數;建設當前值為x則,每次逆序數減小x,增加N-1-x;因為x比x個數大,比N-1-X個數小;
代碼:
 #include<stdio.h>
#include<string.h>
#define MIN(x,y)(x<y?x:y)
const int MAXN=;
int a[MAXN],b[MAXN],anser,c[MAXN];
void mergesort(int l,int r,int mid){
int i=l,j=mid+,k=l;
while(i<=mid&&j<=r){
if(a[i]<=a[j])b[k++]=a[i++];
else{
anser+=j-k;
b[k++]=a[j++];
}
}
while(i<=mid)b[k++]=a[i++];
while(j<=r)b[k++]=a[j++];
for(i=l;i<=r;i++)a[i]=b[i];
}
void merge(int l,int r){
if(l<r){
int mid=(l+r)>>;
merge(l,mid);
merge(mid+,r);
mergesort(l,r,mid);
}
}
int main(){
int N;
while(~scanf("%d",&N)){
for(int i=;i<N;i++)scanf("%d",a+i),c[i]=a[i];
anser=;
merge(,N-);
int temp=anser;
//printf("%d\n",anser);
for(int i=;i<N;i++){
temp=temp+(N--*c[i]);
//printf("%d %d\n",c[i],temp);
anser=MIN(anser,temp);
}
printf("%d\n",anser);
}
return ;
}
//n-a-1-(a) N-2*a+1;

總結

以上是生活随笔為你收集整理的Minimum Inversion Number(归并排序)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。