Codeforces Round #550 (Div. 3)C. Two Shuffled Sequences

<center> C. Two Shuffled Sequences </center> <center> time limit per test2 seconds </center> <center> memory limit per test256 megabytes </center> <center> inputstandard input <center> <center> outputstandard output </center> Two integer sequences existed initially — one of them was strictly increasing, and the other one — strictly decreasing. </center> </center>

Strictly increasing sequence is a sequence of integers [x1<x2<⋯<xk]. And strictly decreasing sequence is a sequence of integers [y1>y2>⋯>yl]. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.

They were merged into one sequence a. After that sequence a got shuffled. For example, some of the possible resulting sequences a for an increasing sequence [1,3,4] and a decreasing sequence [10,4,2] are sequences [1,2,3,4,4,10] or [4,2,1,10,4,3].

This shuffled sequence a is given in the input.

Your task is to find any two suitable initial sequences. One of them should be strictly increasing and the other one — strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.

If there is a contradiction in the input and it is impossible to split the given sequence a to increasing and decreasing sequences, print “NO”.

Input
The first line of the input contains one integer n (1≤n≤2⋅105) — the number of elements in a.

The second line of the input contains n integers a1,a2,…,an (0≤ai≤2⋅105), where ai is the i-th element of a.

Output
If there is a contradiction in the input and it is impossible to split the given sequence a to increasing and decreasing sequences, print “NO” in the first line.

Otherwise print “YES” in the first line and any two suitable sequences. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.

In the second line print ni — the number of elements in the strictly increasing sequence. ni can be zero, in this case the increasing sequence is empty.

In the third line print ni integers inc1,inc2,…,incni in the increasing order of its values (inc1<inc2<⋯<incni) — the strictly increasing sequence itself. You can keep this line empty if ni=0 (or just print the empty line).

In the fourth line print nd — the number of elements in the strictly decreasing sequence. nd can be zero, in this case the decreasing sequence is empty.

In the fifth line print nd integers dec1,dec2,…,decnd in the decreasing order of its values (dec1>dec2>⋯>decnd) — the strictly decreasing sequence itself. You can keep this line empty if nd=0 (or just print the empty line).

ni+nd should be equal to n and the union of printed sequences should be a permutation of the given sequence (in case of “YES” answer).

Examples
input
7
7 2 7 3 3 1 4
output
YES
2
3 7
5
7 4 3 2 1
input
5
4 3 1 5 3
output
YES
1
3
4
5 4 3 1
input
5
1 1 2 1 2
output
NO
input
5
0 1 2 3 4
output
YES
0

5
4 3 2 1 0
题意:给定一个序列,将它拆成两部分,一个试严格递增,一个是严格递减。不能发拆成功就是输出“No”。(一个数字的序列和空虚了符号递增和递减要求)
思路:正向跑一遍,逆向一遍
参考代码:

#include <bits/stdc++.h>
#define N 200005
#define ll long long int
#define inf 0x3f3f3f3f
using namespace std;
ll A[N],B[N],C[N];
bool vis[N];
int main()
{
    ll  n;
    cin>>n;
    for(ll i=1; i<=n; i++)
        cin>>A[i];
    sort(A+1,A+n+1);
    B[0]=-1;
    C[0]=inf;
    ll sum1=0;
    for(ll i=1; i<=n; i++)
    {
        if(B[sum1]<A[i]&&!vis[i])
        {
            B[++sum1]=A[i];
            vis[i]=1;
        }
    }
    ll sum2=0;
    for(ll i=n; i>=1; i--)
    {
        if(C[sum2]>A[i]&&!vis[i])
        {
            C[++sum2]=A[i];
            vis[i]=1;
        }
    }
    if(sum1+sum2==n)
    {
        cout<<"YES\n";
        cout<<sum2<<endl;
        for(ll i=sum2; i>=1; i--)
            if(i==sum2)
                cout<<C[i];
            else
                cout<<" "<<C[i];
        cout<<endl;
        cout<<sum1<<endl;
        for(ll i=sum1; i>=1; i--)
            if(i==sum1)
                cout<<B[i];
            else
                cout<<" "<<B[i];
        cout<<endl;
    }
    else
        cout<<"NO\n";
    return 0;
}
全部评论

相关推荐

2025-12-12 19:01
南京航空航天大学 C++
秋招没咋投,准备&nbsp;wxg&nbsp;转正之后摆烂了。结果不堪字节&nbsp;HR&nbsp;的骚扰还是面了一下字节。之前想去字节的时候怎么面都挂。现在想着随便面一下结果三面技术面都意外顺利还有加面。十月中旬字节发了意向,wxg&nbsp;转正结果无响应。十月底字节拉了保温群,wxg&nbsp;口头通过,系统显示考核中。十一月初和字节&nbsp;ld&nbsp;交流之后得知&nbsp;base&nbsp;居然能选海外,甚至能小&nbsp;wlb&nbsp;一下,wxg&nbsp;无响应无人联系。十一月中旬把字节&nbsp;base&nbsp;转到了海外,wxg&nbsp;流程灰了,一问超时忘处理了,过两天又变考核中了。十一月下旬字节换了海外&nbsp;HR&nbsp;对接,问了期望薪资,wxg&nbsp;考核终于显示通过,无&nbsp;HR&nbsp;保温,无其他保温。十一月底给字节报了个天价,想吓吓他们,同时告诉微信字节要开了,微信无响应。同样十一月底字节&nbsp;HR&nbsp;告诉我确实给不到那么高,但是能拿期权补上,问能不能接受。微信无响应。同样十一月底字节&nbsp;HR&nbsp;告知了具体方案,符合预期。&nbsp;微信无响应。十二月上旬催&nbsp;wxg&nbsp;不开我就盲拒了,wxg&nbsp;HR&nbsp;火急火燎的打电话问情况,问期望。我给了一个不算夸张的总包数字,因为今年市场在涨,过了三天还不联系我,我再催,约时间下午打电话,非得在我给出的数字上压下去几万,微信又不差这点,为什么不能满足我,让我没有拒绝的理由呢?一番纠结抗争,求稳还是追求挑战,最终选择接受迎接新的挑战,因为堂吉诃德永远不会停下脚步!回想起来,在&nbsp;wxg&nbsp;谈薪的阶段,我认为并没有给予我一定的重视,即使&nbsp;HR&nbsp;表示我在实习期间的表现和之前的面评都很靠前。也没有感觉到想要争取我,虽然我表示拒了&nbsp;offer&nbsp;之后要给我加面委定&nbsp;t6&nbsp;再涨,但我三个月没面试让我面面委那就是白给,还是算了。有缘再见了我亲爱的&nbsp;wxg,再见了曾经的梦中情厂,再见亲爱的&nbsp;mt,再见亲爱的朋友们。也再见,北京的一切。我想润了。秋招结束,卸载牛客,下一个三年,下一个五年,下一个十年后再来看看。
面试中的大熊猫爱吃薯...:我嫉妒得狗眼通红
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务