Codeforces-1144G-Two Merged Sequences(动态规划/贪心)

原题链接:http://codeforces.com/contest/1144/problem/G

题目原文:

Two Merged Sequences

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Two integer sequences existed initially, one of them was strictly increasing, and another one — strictly decreasing.

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

Elements of increasing sequence were inserted between elements of the decreasing one (and, possibly, before its first element and after its last element) without changing the order. For example, sequences [1,3,4][1,3,4] and [10,4,2][10,4,2] can produce the following resulting sequences: [10,1,3,4,2,4][10,1,3,4,2,4], [1,3,4,10,4,2][1,3,4,10,4,2]. The following sequence cannot be the result of these insertions: [1,10,4,4,3,2][1,10,4,4,3,2] because the order of elements in the increasing sequence was changed.

Let the obtained sequence be aa. This sequence aa is given in the input. Your task is to find any two suitable initial sequences. One of them should be strictly increasing, and another 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 aa into one increasing sequence and one decreasing sequence, print "NO".

Input

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

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤2⋅1050≤ai≤2⋅105), where aiai is the ii-th element of aa.

Output

If there is a contradiction in the input and it is impossible to split the given sequence aa into one increasing sequence and one decreasing sequence, print "NO" in the first line.

Otherwise print "YES" in the first line. In the second line, print a sequence of nn integers res1,res2,…,resnres1,res2,…,resn, where resiresi should be either 00 or 11 for each ii from 11 to nn. The ii-th element of this sequence should be 00 if the ii-th element of aa belongs to the increasing sequence, and 11 otherwise. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.

Examples

input

Copy

9
5 1 3 6 8 2 9 0 10

output

Copy

YES
1 0 0 0 0 1 0 1 0 

input

Copy

5
1 2 4 0 2

output

Copy

NO

 

题目大意:

        给一个序列,现在给这个序列分组,一组严格递增,一组严格递减,序列中的元素在原序列中的相对位置不变。

解题思路:

        乍一看,有点像LIS最长上升子序列,但后来发现错了,测试样例:13:{9 8 0 7 6 1 5 4 2 3 2 3 1} 过不了。 代码提交后竟然能过到第15个测试样例。

        现在来看看正确的解题思路。

        方法一:贪心思想来做,先设两个数列,一个递增列asc,一个递减列desc,遍历a[i],当a[i] > asc[end]则满足加入asc的条件,当a[i] < desc[end]则满足加入desc的条件。当只满足一个条件时,毋庸置疑,加入即可。当都不满足时,则不成立输出NO。当两个条件都满足时,就需要思考一下了。当a[i + 1] > a[i],那么a[i]加入asc更优,因为对a[i + 1]没有影响,也就是说如果a[i]加入desc那么a[i + 1]就肯定不能加入desc了,如果a[i]加入asc那么a[i + 1]肯定可以加入asc,也有可能加入desc,这样就消除了a[i]对a[i + 1]的判断的影响;当a[i + 1] < a[i],a[i]加入desc更优,原因同理;a[i + 1] == a[i],则分别加入一个即可。

        方法二:动态规划,状态转移方程:dp[i][0]表示处理完前i个,第i个是递增序列序列里的元素,递减序列的最大值。dp[i][1]表示处理完前i个,第i个是递减序列序列里的元素,递增序列的最小值。

贪心:

#include <cstdio>
using namespace std;

const int MIN = 1 << 31;
const int MAX = (1 << 31) - 1;
const int N = int(2e5 + 5);

int g_arr[N];
int g_asc[N], la;
int g_desc[N], ld;

int main()
{
    int n, i;
    int mask[N];
    bool flag1, flag2;
    while (scanf("%d", &n) != EOF)
    {
        g_asc[0] = MIN;
        g_desc[0] = MAX;
        la = 1;
        ld = 1;
        for (i = 0; i < n; i++)
        {
            scanf("%d", &g_arr[i]);
        }
        for (i = 0; i < n; i++)
        {
            flag1 = g_arr[i] > g_asc[la - 1];
            flag2 = g_arr[i] < g_desc[ld - 1];
            if (flag1 && flag2)
            {
                if (i + 1 < n)
                {
                    if (g_arr[i] < g_arr[i + 1]) g_asc[la++] = g_arr[i], mask[i] = 0;
                    else g_desc[ld++] = g_arr[i], mask[i] = 1;
                }
                else 
                {
                    g_asc[la++] = g_arr[i], mask[i] = 0;
                }
            }
            else if (flag1)
            {
                g_asc[la++] = g_arr[i], mask[i] = 0;
            }
            else if (flag2)
            {
                g_desc[ld++] = g_arr[i], mask[i] = 1;
            }
            else
            {
                break;
            }
        }
        if (i >= n)
        {
            printf("YES\n");
            for (i = 0; i < n; i++)
            {
                printf("%d ", mask[i]);
            }
            printf("\n");
        }
        else
        {
            printf("NO\n");
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值