• POJ


    https://vjudge.net/problem/POJ-2299

    题意

    求对于给定的无序数组,求出经过最少多少次相邻元素的交换之后,可以使数组从小到大有序。

    分析

    很明显是求逆序对的数目,那就要想到归并排序了。在归并过程中计算逆序对。

    #include<iostream>
    #include<cmath>
    #include<cstring>
    #include<queue>
    #include<vector>
    #include<cstdio>
    #include<algorithm>
    #include<map>
    #include<set>
    #define rep(i,e) for(int i=0;i<(e);i++)
    #define rep1(i,e) for(int i=1;i<=(e);i++)
    #define repx(i,x,e) for(int i=(x);i<=(e);i++)
    #define X first
    #define Y second
    #define PB push_back
    #define MP make_pair
    #define mset(var,val) memset(var,val,sizeof(var))
    #define scd(a) scanf("%d",&a)
    #define scdd(a,b) scanf("%d%d",&a,&b)
    #define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
    #define pd(a) printf("%d
    ",a)
    #define scl(a) scanf("%lld",&a)
    #define scll(a,b) scanf("%lld%lld",&a,&b)
    #define sclll(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
    #define IOS ios::sync_with_stdio(false);cin.tie(0)
    
    using namespace std;
    typedef long long ll;
    template <class T>
    void test(T a){cout<<a<<endl;}
    template <class T,class T2>
    void test(T a,T2 b){cout<<a<<" "<<b<<endl;}
    template <class T,class T2,class T3>
    void test(T a,T2 b,T3 c){cout<<a<<" "<<b<<" "<<c<<endl;}
    template <class T>
    inline bool scan_d(T &ret){
        char c;int sgn;
        if(c=getchar(),c==EOF) return 0;
        while(c!='-'&&(c<'0'||c>'9')) c=getchar();
        sgn=(c=='-')?-1:1;
        ret=(c=='-')?0:(c-'0');
        while(c=getchar(),c>='0'&&c<='9') ret = ret*10+(c-'0');
        ret*=sgn;
        return 1;
    }
    //const int N = 1e6+10;
    const int inf = 0x3f3f3f3f;
    const ll INF = 0x3f3f3f3f3f3f3f3fll;
    const ll mod = 1000000000;
    int T;
    
    void testcase(){
        printf("Case %d:",++T);
    }
    
    const int MAXN = 5e5+5 ;
    const int MAXM = 550;
    const double eps = 1e-8;
    const double PI = acos(-1.0);
    ll a[MAXN],tmp[MAXN];
    ll ans;
    int n;
    
    void Merge(int low,int mid,int high){
        int i=low,j=mid+1,k=low;
        while(i<=mid&&j<=high){
            if(a[i]<=a[j]){
                tmp[k++]=a[i++];
            }else{
                ans += j-k;
                tmp[k++] = a[j++];
            }
        }
        while(i<=mid) tmp[k++] = a[i++];
        while(j<=high) tmp[k++] = a[j++];
        for(i=low;i<=high;++i){
            a[i]=tmp[i];
        }
    }
    void mergeSort(int a,int b){
        if(a<b){
            int mid =(a+b)>>1;
            mergeSort(a,mid);
            mergeSort(mid+1,b);
            Merge(a,mid,b);
        }
    }
    
    int main() {
    #ifdef LOCAL
        freopen("in.txt","r",stdin);
    #endif // LOCAL
        while(~scanf("%d",&n)&&n){
            ans=0;
            for(int i=0;i<n;i++) scanf("%lld",&a[i]);
            mergeSort(0,n-1);
            printf("%lld
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    关于全景漫游
    webgl圈中物体
    css3の极限
    reactjs弹幕视频播放
    数值积分I
    显出你的h5逼格
    奇葩のbeforeunload
    面试问题搜集及解析
    TCP拥塞控制(滑动窗口机制)
    如何使CPU占用率为50%
  • 原文地址:https://www.cnblogs.com/fht-litost/p/9246220.html
Copyright © 2020-2023  润新知