题意:一个长度为n的整形序列,是0~n-1的一个排列,这个序列可以滚动(把第一个元素放到尾部),求其在所有可能的滚动序列中,最小的逆序数对(也就是有多少对数在前面的大,在后面的小)。
思路:先求原序列的逆序数对,然后根据这个逆序数对推滚动后的逆序数对,找出最小值。具体方法有以下几种。。
方法1:归并排序,在排序过程中计数。都是sort函数惯的,连个排序都调试了好久。。
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <memory.h>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <ctype.h>
#define INF 10000000
#define ll long long
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define MAXN 100010
using namespace std;
int a[5010];
int b[5010];
int tmp[5010];
ll bisort(int l,int r){
if(l>=r)return 0;
if((l+1)==r){
if(a[l]>a[r]){
swap(a[l],a[r]);return 1;
}
return 0;
}
int mid=(l+r)/2;
int re=bisort(l,mid)+bisort(mid+1,r);
int k1=l,k2=mid+1,k=0;
while(true){
if(k1>mid||(k2<=r&&a[k1]>a[k2]) ){
tmp[k]=a[k2];
k++;k2++;
re+=(mid-k1+1);
}
else{
tmp[k]=a[k1];
k++;k1++;
}
if(k1>mid&&k2>r)break;
}
memcpy(a+l,tmp,k*sizeof(int));
return re;
}
int main() {
int n;
while(cin>>n){
for(int i=1;i<=n;i++)cin>>a[i];
memcpy(b,a,sizeof(a));
ll tmp=bisort(1,n);
ll ans=tmp;
for(int k=1;k<=n;k++){
tmp=tmp-b[k]+n-1-b[k];
ans=min(ans,tmp);
}
cout<<ans<<endl;
}
return 0;
}方法2:线段树
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <memory.h>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <ctype.h>
#define INF 10000000
#define ll long long
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define MAXN 100010
using namespace std;
struct node{
int l,r;
int val;
//int add;
};
node seg[20010];
void build(int x,int l,int r){
seg[x].l=l;
seg[x].r=r;
seg[x].val=0;
int mid=(l+r)/2;
if(l==r)return ;
build(2*x,l,mid);
build(2*x+1,mid+1,r);
}
void update(int x,int n){
seg[x].val++;
int mid=(seg[x].l+seg[x].r)/2;
if(seg[x].l==seg[x].r)return;
if(n<=mid){
update(x*2,n);
}else{
update(x*2+1,n);
}
}
int query(int x,int l,int r){
if(l>r)return 0;
if(seg[x].l==l&&seg[x].r==r){
return seg[x].val;
}
int mid=(seg[x].l+seg[x].r)/2;
if(r<=mid){
return query(2*x,l,r);
}else{
if(l>=mid+1){
return query(2*x+1,l,r);
}else{
return query(2*x,l,mid)+query(2*x+1,mid+1,r);
}
}
}
int a[5010];
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int n;
while(cin>>n){
build(1,0,n);
ll tmp=0;
for(int i=1;i<=n;i++){
cin>>a[i];
tmp+=query(1,a[i]+1,n-1);
update(1,a[i]);
}
ll ans=tmp;
for(int k=1;k<=n;k++){
tmp=tmp-a[k]+n-1-a[k];
ans=min(ans,tmp);
}
cout<<ans<<endl;
}
return 0;
}方法3:树状数组
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <memory.h>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <ctype.h>
#define INF 10000000
#define ll long long
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define MAXN 100010
using namespace std;
int n;
int a[5010];
int c[5010];
inline int lowbit(int n){
return n&(n^(n-1));
}
inline void update(int pos){
while(pos<=n){
c[pos]++;
pos+=lowbit(pos);
}
}
inline int sum(int end){
int re=0;
while(end){
re+=c[end];
end-=lowbit(end);
}
return re;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
while(cin>>n){
memset(c,0,sizeof(c));
ll tmp=0;
for(int i=1;i<=n;i++){
cin>>a[i];
++a[i];
tmp+=(i-1-sum(a[i]-1));
update(a[i]);
}
ll ans=tmp;
for(int k=1;k<=n;k++){
tmp=tmp-2*a[k]+1+n;
ans=min(ans,tmp);
}
cout<<ans<<endl;
}
return 0;
}

本文探讨了在所有可能的滚动序列中寻找最小逆序数对的方法,通过归并排序、线段树和树状数组三种算法实现,并详细解释了每种方法的原理与实现过程。
650

被折叠的 条评论
为什么被折叠?



