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数组让你构建b数组,b[i] >= 1 && b[i] <= 5,还要满足下列条件:
思路:
dp[i][j]代表第i个数取j是否可行,如果可行dp[i][j] == 1,反之为0。我们跑一边只是得到了dp数组,具体的每个数取几是不清楚的,所以我们还需要一个数组记录一下过程pre[i][j]代表第i个数为j的时候前一个数的值。最后再用pre数组倒着输出就行了。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1e5 + 100;
int a[maxn], b[maxn];
int dp[maxn][6], pre[maxn][6];
int main()
{
//freopen("in.txt", "r", stdin);
int n;
cin >> n;
for(int i = 1; i <= n; ++ i) {
scanf("%d", &a[i]);
}
for(int i = 1; i <= 5; ++ i) { //dp数组初始化
dp[1][i] = 1;
}
for(int i = 2; i <= n; ++ i) {
for(int j = 1; j <= 5; ++ j) { //开始由dp[i-1][j]推dp[i][j]
if(dp[i - 1][j] == 0) continue;
if(a[i] > a[i - 1]) { //第一种情况
for(int k = j + 1; k <= 5; ++ k) { //从 j+1 开始跑
dp[i][k] = 1; //标记为
pre[i][k] = j; //记录路径
}
}
else if(a[i] < a[i - 1]) {
for(int k = j - 1; k >= 1; -- k) { //倒着跑
dp[i][k] = 1;
pre[i][k] = j;
}
}
else if(a[i] == a[i - 1]) {
for(int k = 1; k <= 5; ++ k) {
if(k != j) {
dp[i][k] = 1;
pre[i][k] = j;
}
}
}
}
}
int t = -1;
for(int i = 1; i <= 5; ++ i) { //检验一下是否可以得到b数组
if(dp[n][i]) {
t = i;
}
}
if(t == -1) {
cout << t << endl;
}
else {
int top = 0;
for(int i = n; i >= 1; -- i) { //回溯路径
b[top++] = t;
t = pre[i][t];
}
for(int i = top - 1; i >= 0; i --) { //倒序输出
if(i == 0) {
printf("%d\n", b[i]);
}
else {
printf("%d ", b[i]);
}
}
}
return 0;
}