#11661. 珅泽教育CSP-J第一轮模拟考第十六套 第 24 题

珅泽教育CSP-J第一轮模拟考第十六套 第 24 题

二、阅读程序(判断题每题1分,选择题每题3分,共计40分;判断题正确填 T,错误填 F)

第2题

int solve(int n, int d, int x[])
{
    std::sort(x, x + n);
    int pair = 0;
    int j = 0;
    for (int i = 0; i < n; ++i)
    {
        while (j < n and x[j] - x[i] <= d)
        {
            j++;
        }
        pair += j - i - 1;
    }
    return pair;
}

不考虑 std::sort() 的运行时间,剩余程序的时间复杂度为( )。

{{ select(1) }}

  • Θ(n)\Theta(n)
  • Θ(nlogn)\Theta(n\log n)
  • Θ(n2)\Theta(n^2)
  • Θ(nlogd)\Theta(n\log d)