【Codeforces Round #522(Div. 2)】Playing Piano(dp || 记忆化搜索)

题目链接

C. Playing Piano

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Little Paul wants to learn how to play piano. He already has a melody he wants to start with. For simplicity he represented this melody as a sequence a1,a2,…,ana1,a2,…,an of key numbers: the more a number is, the closer it is to the right end of the piano keyboard.

Paul is very clever and knows that the essential thing is to properly assign fingers to notes he's going to play. If he chooses an inconvenient fingering, he will then waste a lot of time trying to learn how to play the melody by these fingers and he will probably not succeed.

Let's denote the fingers of hand by numbers from 11 to 55. We call a fingering any sequence b1,…,bnb1,…,bn of fingers numbers. A fingering is convenient if for all 1≤i≤n−11≤i≤n−1 the following holds:

  • if ai<ai+1ai<ai+1 then bi<bi+1bi<bi+1, because otherwise Paul needs to take his hand off the keyboard to play the (i+1)(i+1)-st note;
  • if ai>ai+1ai>ai+1 then bi>bi+1bi>bi+1, because of the same;
  • if ai=ai+1ai=ai+1 then bi≠bi+1bi≠bi+1, because using the same finger twice in a row is dumb. Please note that there is ≠≠, not == between bibiand bi+1bi+1.

Please provide any convenient fingering or find out that there is none.

Input

The first line contains a single integer nn (1≤n≤1051≤n≤105) denoting the number of notes.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤2⋅1051≤ai≤2⋅105) denoting the positions of notes on the keyboard.

Output

If there is no convenient fingering, print −1−1. Otherwise, print nn numbers b1,b2,…,bnb1,b2,…,bn, each from 11 to 55, denoting a convenient fingering, separated by spaces.

Examples

input

Copy

5
1 1 4 2 2

output

Copy

1 4 5 4 5 

input

Copy

7
1 5 7 8 10 3 1

output

Copy

1 2 3 4 5 4 3 

input

Copy

19
3 3 7 9 8 8 8 8 7 7 7 7 5 3 3 3 3 8 8

output

Copy

1 3 4 5 4 5 4 5 4 5 4 5 4 3 5 4 3 5 4 

Note

The third sample test is kinda "Non stop" song by Reflex.

【题意】

给出一个数列a[n],让构造一个满足下列条件的数列b[n]:如果a[i]>a[i-1],那么b[i]>b[i-1],如果a[i]<a[i-1],那么b[i]<b[i-1],如果a[i]==a[i-1],那么b[i]!=b[i-1]。

【解题思路】

dp版本:dp[i][j]表示第i个位置是否可以放第j个数字,如果可以dp[i][j]标记为1,并且用pre数组记录之前一个位置的数字,最后逆序记录数组b即为所求。

记忆化搜索:直接搜就可以了,没什么特别需要注意的。

【代码】

dp版本:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
int dp[maxn][6],a[maxn],pre[maxn][6],b[maxn];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    for(int i=1;i<=5;i++)
        dp[1][i]=1;
    for(int i=1;i<n;i++)
    {
        for(int j=1;j<=5;j++)
        {
            if(!dp[i][j])continue;
            if(a[i]<a[i+1])
            {
                for(int k=j+1;k<=5;k++)
                {
                    dp[i+1][k]=1;
                    pre[i+1][k]=j;
                }
            }
            else if(a[i]>a[i+1])
            {
                for(int k=j-1;k>=0;k--)
                {
                    dp[i+1][k]=1;
                    pre[i+1][k]=j;
                }
            }
            else
            {
                for(int k=1;k<=5;k++)
                {
                    if(k!=j)
                    {
                        dp[i+1][k]=1;
                        pre[i+1][k]=j;
                    }
                }
            }
        }
    }
    int flag=0;
    for(int i=1;i<=5;i++)
    {
        if(dp[n][i]==1)
        {
            int u=i;
            for(int j=n;j>=1;j--)
            {
                b[j]=u;
                u=pre[j][u];
            }
            flag=1;
        }
    }
    if(!flag)printf("-1\n");
    else
    {
        printf("%d",b[1]);
        for(int i=2;i<=n;i++)printf(" %d",b[i]);
        printf("\n");
    }
}

记忆化搜索:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
int a[maxn],b[maxn],vis[maxn][6];
int n;
bool dfs(int pos)
{
    if(pos==n)return true;
    for(int i=1;i<=5;i++)
    {
        if(!vis[pos+1][i])
        {
            if((a[pos]==a[pos+1] && b[pos]!=i) || (a[pos]<a[pos+1] && b[pos]<i)
               || a[pos]>a[pos+1] && b[pos]>i)
            {
                b[pos+1]=i;
                vis[pos+1][i]=1;
                if(dfs(pos+1))return true;
            }
        }
    }
    return false;
}
int main()
{
    int flag=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    for(int i=1;i<=5;i++)
    {
        b[1]=i;
        if(dfs(1))
        {
            flag=1;
            break;
        }
    }
    if(flag)
    {
        printf("%d",b[1]);
        for(int i=2;i<=n;i++)
            printf(" %d",b[i]);
        printf("\n");
    }
    else printf("-1\n");
}

猜你喜欢

转载自blog.csdn.net/qq_39826163/article/details/84562346