CF2203F.Binary Search with One Swap
省选/NOI-
通过率:0%
时间限制:1.50s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Consider the following algorithm for searching for an integer x in an array a of length n, where the elements are numbered from 1 to n:
- initialize l=1 and r=n;
- if l>r, terminate the algorithm and report that the desired element was not found;
- compute m=⌊2l+r⌋;
- if am=x, terminate the algorithm and report that the desired element was found;
- if am<x, set l=m+1, otherwise set r=m−1;
- go to step 2.
It can be shown that if the array a is sorted in non-decreasing order, then any element of a will be successfully found by this algorithm.
Your task is as follows: for a given number n, consider all pairs (i,j) such that 1≤i<j≤n. For each of these pairs, you need to calculate its beauty — the number of integers x from 1 to n that can be successfully found by the described algorithm if we take the array a=[1,2,3,…,n] and swap the elements ai and aj. After that, for each k from 0 to n, you need to output pk — the number of pairs with beauty k.
考虑以下在长度为 n 的数组 a 中搜索整数 x 的算法,其中数组元素编号从 1 到 n:
- 初始化 l=1 和 r=n;
- 若 l>r,则终止算法,并报告目标元素未找到;
- 计算 m=⌊2l+r⌋;
- 若 am=x,则终止算法,并报告目标元素已找到;
- 若 am<x,则令 l=m+1;否则令 r=m−1;
- 返回第 2 步。
可以证明:若数组 a 按非递减顺序排序,则该算法总能成功找到 a 中的任意元素。
你的任务如下:给定一个正整数 n,考虑所有满足 1≤i<j≤n 的数对 (i,j)。对每个这样的数对,你需要计算其“美观度”(beauty)——即:当取初始数组 a=[1,2,3,…,n] 并交换其中第 i 个与第 j 个元素后,该算法能够成功找到的、属于区间 [1,n] 的整数 x 的个数。随后,对每个 k(从 0 到 n),输出 pk —— 即美观度恰好为 k 的数对的个数。
输入格式
The only line of the input contains one integer n (3≤n≤5⋅106).
输入仅包含一行,其中有一个整数 n(3≤n≤5⋅106)。
输出格式
Print n+1 integers p0,p1,…,pn, where pk is the number of pairs (i,j) with beauty k.
输出 n+1 个整数 p0,p1,…,pn,其中 pk 表示美丽值为 k 的数对 (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=4:
- if we swap a1 and a2, then 1, 3, and 4 can be successfully found;
- if we swap a1 and a3, then 2 and 4 can be successfully found;
- if we swap a2 and a3, then 1, 3, and 4 can be successfully found;
- if we swap a1 and a4, then 2 and 3 can be successfully found;
- if we swap a2 and a4, then 1 and 4 can be successfully found;
- if we swap a3 and a4, then 1, 2, and 4 can be successfully found.
考虑一个 n=4 的例子:
- 若交换 a1 和 a2,则可以成功找到 1、3 和 4;
- 若交换 a1 和 a3,则可以成功找到 2 和 4;
- 若交换 a2 和 a3,则可以成功找到 1、3 和 4;
- 若交换 a1 和 a4,则可以成功找到 2 和 3;
- 若交换 a2 和 a4,则可以成功找到 1 和 4;
- 若交换 a3 和 a4,则可以成功找到 1、2 和 4。
输入解题思路,AI测评打分。不知道怎么写?