题目链接:1305
先排序,然后二分查找1和2的个数--
k为1的个数--p为2的个数
s=(k-1)*k/2*2+(n-k)*k+(p-1)*p/2
1-1,,,,,,1-i(i大于1),,,,2-2
其余的都为0--复杂度(logn)
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define LL long long
LL shu[100100];
int main()
{
int n;scanf("%d",&n);
for (int i=0;i<n;i++)
scanf("%lld",&shu[i]);
sort(shu,shu+n);
LL s=0;
LL k=upper_bound(shu,shu+n,1)-lower_bound(shu,shu+n,1);
if (k)
{
s=(n-k)*k+2*((k-1)*k/2);
}
LL kk=upper_bound(shu,shu+n,2)-lower_bound(shu,shu+n,2);
if (kk)
{
s=s+(kk-1)*kk/2;
}
printf("%lld\n",s);
return 0;
}