Algorithm


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,,an�1,�2,…,�� 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,,bn�1,…,�� of fingers numbers. A fingering is convenient if for all 1in11≤�≤�−1 the following holds:

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

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

Input

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

The second line contains n integers a1,a2,,an�1,�2,…,�� (1ai21051≤��≤2⋅105) denoting the positions of notes on the keyboard.

Output

If there is no convenient fingering, print 1−1. Otherwise, print n numbers b1,b2,,bn�1,�2,…,��, 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.

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <bits/stdc++.h>

using namespace std;

int const N = 1e5 + 1;
int n, a[N], dp[N][6];
vector<int> sol;

int rec(int idx, int prv) {
  if(idx == n) {
    for(int i = 0; i < sol.size(); ++i)
      printf("%s%d", i == 0 ? "" : " ", sol[i]);
    puts("");
    exit(0);
  }

  int &ret = dp[idx][prv];
  if(ret != -1)
    return ret;
  ret = 0;

  if(a[idx] == a[idx - 1]) {
    for(int i = 1; i <= 5; ++i)
      if(i != prv) {
        sol.push_back(i);
        rec(idx + 1, i);
        sol.pop_back();
      }
  }

  if(a[idx] > a[idx - 1]) {
    for(int i = prv + 1; i <= 5; ++i) {
      sol.push_back(i);
      rec(idx + 1, i);
      sol.pop_back();
    }
  }

  if(a[idx] < a[idx - 1]) {
    for(int i = prv - 1; i >= 1; --i) {
      sol.push_back(i);
      rec(idx + 1, i);
      sol.pop_back();
    }
  }

  return ret;
}

int main() {
  scanf("%d", &n);
  for(int i = 0; i < n; ++i)
    scanf("%d", a + i);

  memset(dp, -1, sizeof dp);
  for(int i = 1; i <= 5; ++i) {
    sol.push_back(i);
    if(rec(1, i))
      return 0;
    sol.pop_back();
  }
  puts("-1");

  return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
5
1 1 4 2 2

Output

x
+
cmd
1 4 5 4 5
Advertisements

Demonstration


Codeforces Solution-C. Playing Piano-Solution in C, C++, Java, Python,Playing Piano,Codeforces Solution

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+