SPOJ Problem:Inversion Count

逆序对裸题。可以用树状数组做,但树状数组是以数据的大小为下标,时间复杂度为O(n log n)(n=max(a[i])),总体来说还是归并好一些。

#include<cstdio>
#include<cstring>
int a[200005],b[200005],n,t;
int i;
long long ans;
void count(int l,int r){
    int m,i,j,k;
    if (l==r)return;
    m=(l+r)>>1;
    count(l,m);count(m+1,r);
    i=l;j=m+1;k=l-1;
    while(i<=m&&j<=r){
        while(a[i]<=a[j]&&i<=m){
            b[++k]=a[i++];
            ans+=(j-m-1);
        }
        while(a[i]>a[j]&&j<=r){
            b[++k]=a[j++];
        }
    }
    while(i<=m){
        b[++k]=a[i++];
        ans+=(j-m-1);
    }
    for (i=l;i<j;i++)a[i]=b[i];
}
int main(){
    scanf("%d",&t);
    while(t--){
        ans=0;
        scanf("%d",&n);
        for (i=1;i<=n;i++)
            scanf("%d",&a[i]);
        count(1,n);
        printf("%lld\n",ans);
    }
}
时间: 2024-10-11 14:12:42

SPOJ Problem:Inversion Count的相关文章

SPOJ Problem 302:Count on Cantor

题目不复述了,自己看吧.http://www.spoj.com/problems/CANTON/一如既往的暴力.. #include<cstdio> int n,i,j,s,ans; int main(){ scanf("%d",&n); while(n--){ scanf("%d",&s); printf("TERM %d IS ",s); for (i=1;s>i;i++){ s-=i; } if (i&am

【微软2014实习生及秋令营技术类职位在线测试】题目3 : Reduce inversion count

时间限制:10000ms 单点时限:1000ms 内存限制:256MB Description Find a pair in an integer array that swapping them would maximally decrease the inversion count of the array. If such a pair exists, return the new inversion count; otherwise returns the original invers

素数筛法--SPOJ Problem 2 Prime Generator

质数(prime number)又称素数,除了1和它本身外,不能整除以其他自然数,换句话说就是该数除了1和它本身以外不再有其他的因数:否则称为合数.最小的质数是2. 要判断一个整数N是不是质数很简单,看它是否能被2到sqrt(N)之间的整数整除即可. def isPrime(n): if n%2==0: return False for i in xrange(3,int(math.sqrt(n)+1),2): if n%i==0: return False return True 不过要找出1

SPOJ Problem 7974:What&#39;s Next

求数列下一项,啥都不说了,贡献了N多WA... #include<cstdio> int main(){ int a,b,c; while(scanf("%d%d%d",&a,&b,&c)&&(a||b||c)){ if (b*2==c+a&&b!=a) printf("AP %d\n",2*c-b); else printf("GP %d\n",c*c/b); } return

hdu 6434 ( Problem I. Count )

Problem I. Count Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 418    Accepted Submission(s): 216 Problem Description Multiple query, for each n, you need to getn i-1∑ ∑ [gcd(i + j, i - j) =

[Daily Coding Problem 68] Count Pairs of attacking bishop pairs

This problem was asked by Google. On our special chessboard, two bishops attack each other if they share the same diagonal. This includes bishops that have another bishop located between them, i.e. bishops can attack through pieces. You are given N b

SPOJ Problem 2: Prime Generator

嗯..在SPOJ上刷的第二题. 一开始不知道哪错了,后来发现i出现了两遍.. 因为m<10^9,所以用素数筛筛32000以内的数,开一个4000的数组存储就行.然后再从n开始用素数筛,总之效率还行. 代码如下: //0.01s 3.2M #include<cstdio> #include<cstring> #include<cmath> int n,i,j,t,m,k,tot; int a[100005],b[4000]; int prime[40000]; in

SPOJ Problem 9948:Will it ever stop

如题..http://www.spoj.com/problems/WILLITST/ #include<cstdio> long long n; int main(){ scanf("%lld",&n); while(n>1){ if (n==3||n==6){printf("NIE\n");return 0;} if (n&1)n=(n+1)/2*3;else n>>=1; } printf("TAK\n&q

SPOJ Problem:A Game with Numbers

突然就看到SPOJ升级了,让我好不适应.. 这一一道博弈论的题目,可以先前面暴力一下,易得小于十的为必胜态,十的时候必败,然后11到19又必胜,而且发现只要各位为零且这个数不为零就必败. 再依次验证上百,上千的数.. #include<cstdio> #include<cstring> int n; int main(){ scanf("%d",&n); if (n%10==0&&n!=0)printf("2\n");