码迷,mamicode.com
首页 > 其他好文 > 详细

Pairwise Sum and Divide

时间:2017-09-28 21:37:32      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:std   log   logs   air   cout   blog   get   pac   cin   

题目: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;
}

 

Pairwise Sum and Divide

标签:std   log   logs   air   cout   blog   get   pac   cin   

原文地址:http://www.cnblogs.com/NDKY9/p/7608661.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!