1701D - Permutation Restoration

E. Text Editor

分析:

每个 i i i 都有 b i = ⌊ i a i ⌋ b_i = \left\lfloor \frac{i}{a_i} \right\rfloor bi=aii ,我们可以将其改写如下: a i ⋅ b i ≤ i < a i ⋅ ( b i + 1 ) a_i \cdot b_i \le i < a_i \cdot (b_i+1) aibii<ai(bi+1), or i b i + 1 < a i ≤ i b i \frac{i}{b_i + 1} < a_i \le \frac{i}{b_i} bi+1i<aibii。从这里我们可以看出,每一个 i i i 都有一段数值可以分配给 a i a_i ai 。因此,我们必须将 1 1 1 n n n 中的每个数字与其中的一个值段匹配起来。

因此,我们对左端点排序,然后贪心地考虑,每次选择右端点最小的线段,这样的话其他线段有更多的选择空间。从 1 1 1 枚举到 n n n,当枚举到 i i i 时,将每一个以 i i i 为左端点的线段,加入到一个以右端点排序的堆中,每次选择最小的就行了。

代码:

#include<bits/stdc++.h>
using namespace std;
const int N=5e5+10,M=1e9+10;
typedef long long ll;
typedef pair<int,int> PII;
int T;
vector<PII> g[N];
priority_queue<PII,vector<PII>,greater<PII>> heap;
int ans[N];
void solve()
{
   int n;
   cin>>n;
   int b[N];
   for(int i=1;i<=n;i++) cin>>b[i],g[i].clear();
   for(int i=1;i<=n;i++)
   {
     int l,r;
     if(b[i]==0) l=i+1,r=n;
     else l=i/(b[i]+1) + 1,r=i/b[i];
     g[l].push_back({r,i});
   }
   for(int i=1;i<=n;i++)
   {
    for(auto x : g[i])
       heap.push(x);
    auto t=heap.top();
    heap.pop();
    ans[t.second]=i; 
   }
   for(int i=1;i<=n;i++) cout<<ans[i]<<" \n"[i==n];
   return ;
  }
int main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>T;
    while(T--) 
    solve();
    return 0;
} 

  • 17
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值