Codeforces Round #842 (Div. 2) C. Elemental Decompress

Codeforces Round #842 (Div. 2) C. Elemental Decompress

Two cases produce no answers:

  • One element appears more than twice in a a a
  • After sorting, there is some index that a [ i ] < i a[i]<i a[i]<i ( 1 1 1-indexed).

Consider there is some index that a [ i ] < i a[i]<i a[i]<i, then both p [ i ] < i p[i]<i p[i]<i and q [ i ] < i q[i]<i q[i]<i must satisfy. This is also true for the first i − 1 i−1 i1 index, so the numbers that are smaller than i i i in both p p p and q q q are ( i − 1 ) × 2 + 2 = i ∗ 2 (i−1)×2+2=i∗2 (i1)×2+2=i2. This is a contradiction.
Otherwise, solutions always exist. One method is to constructively attach each element in a a a to p p p or q q q:

  • Traverse from the biggest element to the smallest in a a a, if that number haven’t appeared in p p p then attach it to p p p, otherwise attach it to q q q.
  • Traverse from the biggest element to the smallest in a a a again, if we attached it to p p p, find the biggest number that did not appear in q q q and attach to q q q, vice versa.

A naive solution requires the O ( n 2 ) O(n^2) O(n2) method to solve. We can reduce to O ( n l o g n ) O(nlogn) O(nlogn) by sorting elements in a a a as pairs <element, index>.

Time complexity: O ( n l o g ( n ) ) O(nlog(n)) O(nlog(n))

#include<bits/stdc++.h>
using namespace std;

int main()
{
    auto solve=[&](){
        int n;cin>>n;
        vector<int> a(n+1),st(n+1),p(n+1),q(n+1);
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
            st[a[i]]++;
        }
        if(st[1]>1)
        {
            cout<<"NO"<<endl;
            return ;
        }
        for(int i=1;i<=n;i++)
            if(st[i]>2)
            {
                cout<<"NO"<<endl;
                return ;
            }

        vector<int> pst(n+1),qst(n+1);
        for(int i=1;i<=n;i++)
        {
            if(pst[a[i]]&&qst[a[i]])
            {
                cout<<"NO"<<endl;
                return ;
            }
            if(pst[a[i]])
            {
                q[i]=a[i];
                qst[a[i]]=1;
            }
            else
            {
                p[i]=a[i];
                pst[a[i]]=1;
            }
        }

        multiset<int> m1,m2;
        for(int i=1;i<=n;i++)
        {
            if(!pst[i]) m1.insert(i);
            if(!qst[i]) m2.insert(i);
        }

        for(int i=1;i<=n;i++)
        {
            if(!p[i])
            {
                if(*m1.begin()>q[i])
                {
                    cout<<"NO"<<endl;
                    return ;
                }
                auto pos=m1.upper_bound(q[i]);
                p[i]=*(--pos);
                m1.erase(pos);
            }
            else
            {
                if(*m2.begin()>p[i])
                {
                    cout<<"NO"<<endl;
                    return ;
                }
                auto pos=m2.upper_bound(p[i]);
                q[i]=*(--pos);
                m2.erase(pos);
            }
        }

        cout<<"YES"<<endl;
        for(int i=1;i<=n;i++)
            cout<<p[i]<<" ";
        cout<<endl;
        for(int i=1;i<=n;i++)
            cout<<q[i]<<" ";
        cout<<endl;
    };

    int T;cin>>T;
    while(T--) solve();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wa_Automata

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值