CF2203F.Binary Search with One Swap

省选/NOI-

通过率:0%

时间限制:1.50s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Consider the following algorithm for searching for an integer xx in an array aa of length nn, where the elements are numbered from 11 to nn:

  1. initialize l=1l = 1 and r=nr = n;
  2. if l>rl \gt r, terminate the algorithm and report that the desired element was not found;
  3. compute m=⌊l+r2⌋m = \lfloor \frac{l + r}{2} \rfloor;
  4. if am=xa_m = x, terminate the algorithm and report that the desired element was found;
  5. if am<xa_m \lt x, set l=m+1l = m + 1, otherwise set r=m−1r = m - 1;
  6. go to step 22.

It can be shown that if the array aa is sorted in non-decreasing order, then any element of aa will be successfully found by this algorithm.

Your task is as follows: for a given number nn, consider all pairs (i,j)(i, j) such that 1≤i<j≤n1 \le i \lt j \le n. For each of these pairs, you need to calculate its beauty — the number of integers xx from 11 to nn that can be successfully found by the described algorithm if we take the array a=[1,2,3,…,n]a = [1, 2, 3, \dots, n] and swap the elements aia_i and aja_j. After that, for each kk from 00 to nn, you need to output pkp_k — the number of pairs with beauty kk.

考虑以下在长度为 nn 的数组 aa 中搜索整数 xx 的算法,其中数组元素编号从 11 到 nn:

  1. 初始化 l=1l = 1 和 r=nr = n;
  2. 若 l>rl \gt r,则终止算法,并报告目标元素未找到;
  3. 计算 m=⌊l+r2⌋m = \lfloor \frac{l + r}{2} \rfloor;
  4. 若 am=xa_m = x,则终止算法,并报告目标元素已找到;
  5. 若 am<xa_m \lt x,则令 l=m+1l = m + 1;否则令 r=m−1r = m - 1;
  6. 返回第 22 步。

可以证明:若数组 aa 按非递减顺序排序,则该算法总能成功找到 aa 中的任意元素。

你的任务如下:给定一个正整数 nn,考虑所有满足 1≤i<j≤n1 \le i \lt j \le n 的数对 (i,j)(i, j)。对每个这样的数对,你需要计算其“美观度”(beauty)——即:当取初始数组 a=[1,2,3,…,n]a = [1, 2, 3, \dots, n] 并交换其中第 ii 个与第 jj 个元素后,该算法能够成功找到的、属于区间 [1,n][1, n] 的整数 xx 的个数。随后,对每个 kk(从 00 到 nn),输出 pkp_k —— 即美观度恰好为 kk 的数对的个数。

输入格式

The only line of the input contains one integer nn (3≤n≤5⋅1063 \le n \le 5 \cdot 10^6).

输入仅包含一行,其中有一个整数 nn(3≤n≤5⋅1063 \le n \le 5 \cdot 10^6)。

输出格式

Print n+1n+1 integers p0,p1,…,pnp_0, p_1, \dots, p_n, where pkp_k is the number of pairs (i,j)(i, j) with beauty kk.

输出 n+1n+1 个整数 p0,p1,…,pnp_0, p_1, \dots, p_n,其中 pkp_k 表示美丽值为 kk 的数对 (i,j)(i, j) 的个数。

输入输出样例

  • 输入#1

    4

    输出#1

    0 0 3 3 0
  • 输入#2

    5

    输出#2

    0 0 2 4 4 0
  • 输入#3

    3

    输出#3

    0 1 2 0
  • 输入#4

    13

    输出#4

    0 0 0 0 0 0 1 3 8 9 21 24 12 0

说明/提示

Consider an example where n=4n = 4:

  • if we swap a1a_1 and a2a_2, then 11, 33, and 44 can be successfully found;
  • if we swap a1a_1 and a3a_3, then 22 and 44 can be successfully found;
  • if we swap a2a_2 and a3a_3, then 11, 33, and 44 can be successfully found;
  • if we swap a1a_1 and a4a_4, then 22 and 33 can be successfully found;
  • if we swap a2a_2 and a4a_4, then 11 and 44 can be successfully found;
  • if we swap a3a_3 and a4a_4, then 11, 22, and 44 can be successfully found.

考虑一个 n=4n = 4 的例子:

  • 若交换 a1a_1 和 a2a_2,则可以成功找到 11、33 和 44;
  • 若交换 a1a_1 和 a3a_3,则可以成功找到 22 和 44;
  • 若交换 a2a_2 和 a3a_3,则可以成功找到 11、33 和 44;
  • 若交换 a1a_1 和 a4a_4,则可以成功找到 22 和 33;
  • 若交换 a2a_2 和 a4a_4,则可以成功找到 11 和 44;
  • 若交换 a3a_3 和 a4a_4,则可以成功找到 11、22 和 44。

输入解题思路,AI测评打分。不知道怎么写?

首页