1079C--Playing Piano

题目

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,…,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 1to 5. We call a fingering any sequence b1,…,bn of fingers numbers. A fingering is convenient if for all 1≤i≤n−1

the following holds:

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

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

Input

The first line contains a single integer n (1≤n≤10^5) denoting the number of notes.

The second line contains n integers a1,a2,…,an(1≤ai≤2⋅10^5) denoting the positions of notes on the keyboard.

Output

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

Examples
input
5
1 1 4 2 2
output
1 4 5 4 5 
input
7
1 5 7 8 10 3 1
output
1 2 3 4 5 4 3 
input
19
3 3 7 9 8 8 8 8 7 7 7 7 5 3 3 3 3 8 8
output
1 3 4 5 4 5 4 5 4 5 4 5 4 3 5 4 3 5 4 
题意:Paul 弹钢琴,已有有一段数字旋律,但是为了演奏的简便,Paul想用简便的指法来弹奏;将手指从1~5编号,指法需要保证:当数字旋律a[i]>a[i+1],则b[i]>b[i+1];当a[i]<a[i+1],则b[i]<b[i+];当a[i]=a[i+1],则b[i+1]为除当前值的其他值。并且保证,一个指法不能连续出现两次。有合适的指法则输出,否则输出-1.
AC–Code
import java.util.Scanner;

public class CF1079C {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] num = new int[n];
		for(int i = 0;i<n;i++)
		{
			num[i] = sc.nextInt();
		}
		sc.close();
		int[] b = new int[n];
		int prev = 0 ;
		b[0] = 2;
		for(int i = 1;i<n;i++)
		{
			if(num[i]==num[i-1])
			{
				b[i]=b[i-1]==2?4:2;
				prev = 0;
			}
			else if(num[i]>num[i-1])
			{
				if(prev<1) 
				{
					b[i-1] = 1;
					if(i>1&&b[i-2]==1)
					{
						b[i-1] = 2;
					}
					b[i] = b[i-1] + 1;
				}
				else {
					b[i] = b[i-1] + 1;
					if(b[i]>5)
					{
						System.out.println(-1);
						return ;
					}
				}
				prev = 1;
			}
			else {
				if(prev >-1)
				{
					b[i-1] = 5;
					if(i>1&&b[i-2]==5)
					{
						b[i-1] = 4;
					}
					b[i] = b[i-1] -1;
				}
				else
				{
					b[i] = b[i-1]-1;
					if(b[i]<1)
					{
						System.out.println(-1);
						return ;
					}
				}
				prev = -1;
			}
		}
		for(int i = 0;i<n;i++)
		{
			System.out.print(b[i]+" ");
		}
		System.out.println();
	}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值