Codeforces Round #522 (Div. 2) C Playing Piano(DFS)

本文介绍了一种用于钢琴旋律智能指法分配的算法,确保演奏者能以最便利的方式演奏旋律,避免不必要的手部移动,提高学习效率。算法通过深度优先搜索策略,根据音符序列自动分配最佳指法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

 

题目大意:

给n个数 a[i] ,要求根据给出的a[i]求b[i]   (5>=b[i]>=1)

要求:

1、a[i]<a[i+1]时b[i]<b[i+1]

2、a[i]>a[i+1]时b[i]>b[i+1]

3、a[i] == a[i+1] 时 b[i]!=b[i+1]

给出a[i] 求 b[i]

dfs暴力就ok了

ac代码:

#include<bits/stdc++.h>
using namespace std;
//#define mp make_pair
#define CL(a, b) memset(a, b, sizeof(a))
#define debug(x) cout<<"debug"<<x<<"\n"
#define sc scanf
#define pr printf
#define INF 0x3f3f3f3f
const int N = 1e5+10;
int b[N];
int a[N];
int vis[N][6];
int n;
int dfs(int x)
{
    if(x == n)
    {
        return 1;
    }
    else
    {
        for(int i=1; i<=5; i++)
        {
            if(!vis[x+1][i])
            {
                if((a[x+1]>a[x] && i>b[x]) || (a[x+1]<a[x] && i<b[x]) || (a[x+1] == a[x] && b[x]!=i))
                {
                    b[x+1] = i;
                    vis[x+1][i] = 1;
                    if(dfs(x+1))
                    {
                        return 1;
                    }
                }
            }
        }
    }
    return 0;
}
int main()
{
    while(~scanf("%d",&n))
    {
        memset(vis,0,sizeof(vis));
        for(int i=1; i<=n; i++)
        {
            sc("%d",&a[i]);
        }
        int flag = 0;
        for(int i=1; i<=5; i++)
        {
            b[1] = i;
            if(dfs(1))
            {
                flag = 1;
                break;
            }
        }
        if(flag == 1)
        {
            for(int i=1; i<=n; i++)
            {
                if(i == 1)
                {
                    printf("%d",b[i]);
                }
                else
                printf(" %d",b[i]);
            }
            printf("\n");
        }
        else
        {
            printf("-1\n");
        }
    }
    return 0;
}

参考博客:https://blog.youkuaiyun.com/qq_41021816/article/details/84328518

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值