题解:CF1725D Deducing Sortability
Evaniscentia · · 题解
题目传送门。
本题解提供英文版,位于示例代码之后。
English version of this editorial is provided after the sample code.
思路
考虑求解第一个问题,不妨令
因此我们贪心的从小到大枚举
也就是说,我们每一行都会至少取出一半的数,因此总枚举的
首先通过打表发现选出来的数一定构成若干个等差数列,第
考虑证明这个东西,对于第
根据以上过程,我们可以求出每个等差数列的项数,由于等差数列的个数是
时间复杂度
#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
Therefore,we greedily enumerate
In other words, we select at least half of the numbers in each row, so the total number of
First, by examining small cases, we observe that the selected numbers must form several arithmetic sequences, where the
Let's proceed to prove this observation.
For the
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
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
The time complexity is