hdu 2689 Sort it

/* *********************************************** Author :xryz Email :[email protected] Created Time :4-22 21:15:18 File Name :hdusortit.cpp ************************************************ */

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

int main()
{
    int n,a[1024],i,j,ans,t;
    while(~scanf("%d",&n))
    {
        ans=0;
        memset(a,0,sizeof(a));
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            for(j=0;j<i;j++)
            {
                if(a[j]>a[i]) ans++;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

int c[1024],b[1024];

int lowbit(int x)
{
    return x&-x;
}

int sum(int *p,int x)
{
    int ret=0;
    while(x>0)
    {
        ret+=p[x];
        x-=lowbit(x);
    }
    return ret;
}

void add(int *p,int x,int d)
{
    while(x<1024)
    {
        c[x]+=d;
        x+=lowbit(x);
    }
}
int main()
{
    int i,ans,n,t;
    while(~scanf("%d",&n))
    {
        memset(c,0,sizeof(c));
        //memset(b,0,sizeof(b));
        //memset(num,0,sizeof(num));
        ans=0;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&t);
            add(c,t,1);
            ans+=sum(c,n)-sum(c,t);
        }
        printf("%d\n",ans);
    }
    return 0;
}

你可能感兴趣的:(hdu 2689 Sort it)