CodeForces ——1079C. Playing Piano(记忆化搜索 / DP )

8 篇文章 0 订阅

CodeForces ——1079C. Playing Piano

DFS版本:
记忆化存一下DFS过程中出现过的状态。因为只要出现回溯,就证明该深度下的所有方案都是无效解。 存一下这些无效的方案直接跳过~~

/*
* @Author: Achan
* @Date:   2018-11-20 19:29:26
* @Last Modified by:   Achan
* @Last Modified time: 2018-11-20 21:05:32
*/
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+2;

void read(int &x)
{
    x=0;int f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    x*=f;return;
}

int n;
std::vector<int> a(maxn);  
std::vector<int> pr(maxn); //ans
bool used[maxn][6];   //记忆数组


inline OK(int idx, int cur) 
{
	if(a[idx-1] > a[idx]) return pr[idx-1] > cur;
	if(a[idx-1] < a[idx]) return pr[idx-1] < cur;
	return  pr[idx-1] != cur;    
}

bool DFS(int idx) 
{
	if(idx >= n ) return true; 
	for(int i = 1; i <= 5 ; i++)
	{
		if(used[idx][i]) continue;  //记忆化剪枝:回溯后再次访问禁止!原因:回溯前的pr状态只能全部改变.访问原来的必定失败的used状态无意义_(:з」∠)_
		if(OK(idx ,i))
		{ 
			pr[idx] = i; 
			used[idx][i] = 1;  
			if(DFS(idx+1)) { return true; }  
		} 	  
	}
	return false;  
}

int main(void)
{
    read(n);
    for(int i = 0; i< n ;i++ ) read(a[i]);
    int flag = 0; 
    for(int i = 1 ; i <= 5 ;i++)
    {
    	pr[0] = i;
    	if( DFS(1) ) { flag = 1; break; }  
    }
    if(!flag) return  !printf("-1\n");
    for(int i = 0; i < n; i++) printf("%d ",pr[i]);   
}

DP Version:
一句话:dp[i][j]表示是在满足i-1能弹 并且 当前手指j与前面一个指头 满足 三条件 的状态 (注意:这里需要所有能成功弹的指头状态,所以1<= j <=5)

/*
* @Author: Achan
* @Date:   2018-11-20 21:16:50
* @Last Modified by:   Achan
* @Last Modified time: 2018-11-20 21:59:51
*/
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+2;
void read(int &x)
{
    x=0;int f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    x*=f;return;
}
inline bool OK(int pre, int cur, int pc, int cc)
{
	if(pre <  cur) return pc < cc;
	if(pre > cur ) return pc > cc;
	return pc!=cc; 
}
bool dp[maxn][6];
int  last[maxn][6]; //存答案,因为只能通过上一个成功态推下一个,选用last邻接表
int main(void)
{
	int n;
	read(n);
	std::vector<int> a(n);
	for(int i = 0; i<n; i++)  read(a[i]); 
	for(int i = 1; i<=5; i++) dp[0][i] = 1; //第一步任意1~5可行

	for(int i = 1; i<n; i++)
	{
		bool flag = 0; 
		for(int j = 1; j<=5; j++)
		for(int k = 1; k<=5; k++)
		{
			if(OK(a[i-1],a[i],j,k) && dp[i-1][j])
			{
				dp[i][k] = 1;
				last[i][k] = j;	//链式邻接表表   
				flag = 1; 
			}
		}
		if(!flag) return !printf("-1\n"); 
	}
	int cur;
	for(int i = 1; i<=5; i++) if(dp[n-1][i]) cur = i ;   //随便乱找个能ok的当cur啦 
	std::vector<int> ans; //调整输出顺序 
	for(int i = n-1; i>=0; i--)
	{
		ans.push_back(cur);
		cur = last[i][cur]; 
	}
	for (int i = n-1; i >= 0; i--)  printf("%d ",ans[i]);   
}

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

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 1
to 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 ai<ai+1

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

. 

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

The first line contains a single integer n
(1≤n≤105

) denoting the number of notes.

The second line contains n
integers a1,a2,…,an (1≤ai≤2⋅105

) 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
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.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值