跳到正文

题解

题解:CF1725D Deducing Sortability

Evaniscentia约 7 分钟
原文
文章信息与版本

文章信息

发布
2025年5月26日
收录
2026年4月8日
更新
2026年11月3日
篇幅
约 7 分钟
题目
CF1725D

版本

  1. v2当前
  2. v1

题目传送门。

本题解提供英文版,位于示例代码之后。

English version of this editorial is provided after the sample code.

思路

考虑求解第一个问题,不妨令 Ai,jA_{i,j} 表示数 ii 经过 jj 次操作后得到的数,将其暴力展开即可得到 Ai,j=2j(i−j)A_{i,j}=2^j(i-j),问题转换成了在 AA 中选出 nn 个互不相同的数使得它们的 ii 和最小。

因此我们贪心的从小到大枚举 ii,考虑什么数是没在之前选的数中出现的,Ai,j=2j(i−j)=2j−1(2i−j−1−(j−1))=A2i−j−1,j−1A_{i,j}=2^j(i-j)=2^{j-1}(2i-j-1-(j-1))=A_{2i-j-1,j-1},根据这个,我们能很轻松的得到当 i−ji-j 为奇数的时候,这个数一定是不重复的,证明可参考唯一分解定理。

也就是说,我们每一行都会至少取出一半的数,因此总枚举的 ii 是 O(n)\mathcal O(\sqrt n) 量级的,这样就可以求出最小和了,现在我们要求的就是第 kk 个数,接下来的操作就很妙了。

首先通过打表发现选出来的数一定构成若干个等差数列,第 ii 个等差数列的公差是 2i−12^{i-1}。

考虑证明这个东西,对于第 00 列,假设我们选出最大的数为 kk,那么它在 kk 行,而 [1,k−1][1,k-1] 行中一定会出现 1,2,⋯ ,k−11,2,\cdots,k-1 这些数,接下来删去第一列,将剩下的数除以 22,我们会发现,这个子矩阵依旧满足这个条件,不断进行下去,最终会形成若干个等差数列,第 ii 个等差数列的公差为 2i−12^{i-1},不过这些等差数列会产生交集,去除掉这些交集即可,仍然是等差数列,故成立。

根据以上过程,我们可以求出每个等差数列的项数,由于等差数列的个数是 O(n)\mathcal O(\sqrt n) 级别的,因此我们直接预处理,询问二分即可。

时间复杂度 O(n+qlog⁡n)\mathcal O(\sqrt n +q\log n)。

C++
#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int Maxn=1e5+7;
int n,Q;
ll ans;

int ps[Maxn],w[Maxn];

int main(){
    scanf("%d%d",&n,&Q);
    int len=1;
    while(1){
        if(n<=(len+1)/2) break;
        n-=(len+1)/2;
        ans+=(len+1)/2*len; len++;
    }
    ans+=n*len;
    printf("%lld\n",ans);
    for(int i=1;i<=len;i++){
        w[i]=len-i+(i>=len-(n-1)*2);
        ps[i]=ps[i-1]+w[i]-w[i-1]/2;
    }
    while(Q--){
        int x;
        scanf("%d",&x);
        int p=lower_bound(ps+1,ps+len+1,x)-ps-1,t=x-(ps[p]-w[p]/2);
        while(!(t&1)) p++,t/=2;
        printf("%d\n",p+t);
    }

    system("pause");
    return 0;
}

Considering solving the first problem,let Ai,jA_{i,j} denote the number obtained from ii after jj operations.By expanding it,we get Ai,j=2j(i−j)A_{i,j}=2^j(i-j).The problem then transforms into selecting nn distinct numbers from AA such that the sum of their ii values is minimized.

Therefore,we greedily enumerate ii in increasing number and consider which numbers have not appeared among the previously selected numbers.Ai,j=2j(i−j)=2j−1(2i−j−1−(j−1))=A2i−j−1,j−1A_{i,j}=2^j(i-j)=2^{j-1}(2i-j-1-(j-1))=A_{2i-j-1,j-1}.Based on this,we can easily observe that when i−ji-j is odd, this number must be unique. The proof can be referenced from the unique factorization theorem.

In other words, we select at least half of the numbers in each row, so the total number of ii values we need to enumerate is on the order of O(n)\mathcal O(\sqrt n).This allows us to compute the minimum sum efficiently. Now, the problem reduces to finding the kk-th number in this selection, and the subsequent operations are quite ingenious.

First, by examining small cases, we observe that the selected numbers must form several arithmetic sequences, where the ii-th arithmetic sequence has a common difference of 2i−12^{i-1}.

Let’s proceed to prove this observation.

For the 00-th column, suppose the largest number we select is kk. This number lies in the kk-th row, while the rows [1,k−1][1,k-1] must contain all the numbers 1,2,…,k−11,2,\dots,k-1.

Next, we remove the first column and divide the remaining numbers by 2. We observe that the resulting submatrix still satisfies the same property. By repeating this process, we eventually obtain several arithmetic sequences, where the ii-th sequence has a common difference of 2i−12^{i-1}.

However, these arithmetic sequences may overlap. After removing these overlapping terms, the remaining sequences still form arithmetic progressions. Thus, the claim holds.

Based on the above process, we can determine the number of terms in each arithmetic sequence. Since the number of arithmetic sequences is of order O(n)\mathcal O(\sqrt{n}), we can directly preprocess them and answer queries using binary search.

The time complexity is O(n+qlog⁡n)\mathcal O(\sqrt n+q\log n).

原文
存档评论3 条抓取于