Pairwise Sum and Divide

题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1305

统计1和2的数量

1-1是2 1-x(不为1)是1 2-2是1

时间复杂度O(n)

#include<iostream>
using namespace std;

const int maxn=1e5+5;
int a[maxn],n,n1=0,n2=0;

int main()
{
    __int64 sum;
    
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        if(a[i]==1)
            n1++;
        else if(a[i]==2)
            n2++;
    }
    int n11=n1*(n1-1)/2,n1x=n1*(n-n1),n22=n2*(n2-1)/2;
    sum=n11*2+n22+n1x;
    cout<<sum;
}

 

posted @ 2017-09-28 21:23  小九xD  阅读(164)  评论(0编辑  收藏  举报