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