1. 程式人生 > >Codeforces Round #522 C. Playing Piano(dp)

Codeforces Round #522 C. Playing Piano(dp)

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;
}