codevs 1432 总数统计

codevs 1432 总数统计

题目描述 Description

给出n个数,统计两两之和小于k的方案数之和。

输入描述 Input Description

第一行一个数n,表示数字的个数;
第二行到第n + 1行,每行一个不超过2,000,000,000的数k
n + 2行一个数m,表示m个问题;
n + 3行到第n + m + 2行,每行一个数m,询问表示n中两两组合不超过m的组
合的个数;

输出描述 Output Description

输出m行,每行对应一个答案

样例输入 Sample Input

3

1

2

3

2

2

3

样例输出 Sample Output

0

1

数据范围及提示 Data Size & Hint

30%的数据1 ≤ n ≤ 100, 1 ≤ m ≤ 50, k ≤ 2000;
100%的数据 1 ≤ n ≤ 10000, 1 ≤ m ≤ 100, k ≤ 2,000,000,000;

 

 

#include<bits/stdc++.h>
#define MAXN 10001
#define LL long long
using namespace std;
LL a[MAXN],tot,m,n;
LL erfen(LL x)
{
    LL l=0,r=n+1;
    while(r-l>1)
    {
        LL mid=(r+l)/2;
        if(x<a[mid]) r=mid;
        else l=mid;
    }
    return l;
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)  
        cin>>a[i];
    sort(a+1,a+n+1);
    cin>>m;
    while(m--)
    {
        tot=0; LL k;
        cin>>k;
        for(int i=1;a[i]<=k/2;i++)
        {
            LL x=erfen(k-a[i]);
            if(x>i) tot+=(x-i);
        }
        printf("%lld
",tot);
    }
    return 0;
}

 

 

原文地址:https://www.cnblogs.com/dxy1174868024/p/5570462.html