cf1701 D. Permutation Restoration
作者:互联网
题意:
构造长度为 n 的排列,要求 \(\lfloor \frac {i}{a_i}\rfloor = b_i\)
保证答案存在
思路:
\(a_i\) 的取值范围是 \(b_ia_i\le i < (b_i+1)a_i\implies \frac {i}{b_i+1}<a_i\le \frac i{b_i}\)。(如果懒得推这个的话也可以二分)
先考虑取值范围的左端点最小的那些 \(a_i\),设它们的左端点为 \(l\),给它们中右端点最小的安排在 \(l\),那么其它点的左端点都 \(+1\)
const signed N = 5 + 5e5;
int n, b[N], a[N];
PII L[N];
void sol() {
cin >> n; for(int i = 1; i <= n; i++)
cin >> b[i], L[i] = {i/(b[i]+1) + 1, i}; //{l,id}
sort(L + 1, L + 1 + n); //按左端点排序
priority_queue<PII,vector<PII>,greater<PII>> R; //{r,id}
for(int i = 1, j = 1; i <= n; i++) {
while(j <= n && L[j].first == i) {
int id = L[j].second;
R.push({b[id] ? id/b[id] : n, id});
j++;
}
a[R.top().second] = i;
R.pop();
}
for(int i = 1; i <= n; i++) cout << a[i] << " \n"[i==n];
}
标签:frac,int,取值,最小,Restoration,端点,Permutation,cf1701,id 来源: https://www.cnblogs.com/wushansinger/p/16486094.html