Permutation Game

Time limit 1000 ms

Mem limit 262144 kB

n children are standing in a circle and playing a game. Children's numbers in clockwise order form a permutation a1, a2, ..., an of length n. It is an integer sequence such that each integer from 1 to n appears exactly once in it.

The game consists of m steps. On each step the current leader with index i counts out ai people in clockwise order, starting from the next person. The last one to be pointed at by the leader becomes the new leader.

You are given numbers l1, l2, ..., lm — indices of leaders in the beginning of each step. Child with number l1 is the first leader in the game.

Write a program which will restore a possible permutation a1, a2, ..., an. If there are multiple solutions then print any of them. If there is no solution then print -1.

Input

The first line contains two integer numbers nm (1 ≤ n, m ≤ 100).

The second line contains m integer numbers l1, l2, ..., lm (1 ≤ li ≤ n) — indices of leaders in the beginning of each step.

Output

Print such permutation of n numbers a1, a2, ..., an that leaders in the game will be exactly l1, l2, ..., lm if all the rules are followed. If there are multiple solutions print any of them.

If there is no permutation which satisfies all described conditions print -1.

Sample 1

InputcopyOutputcopy
4 5
2 3 1 4 4
3 1 2 4 

Sample 2

InputcopyOutputcopy
3 3
3 1 2
-1

Note

Let's follow leadership in the first example:

  • Child 2 starts.
  • Leadership goes from 2 to 2 + a2 = 3.
  • Leadership goes from 3 to 3 + a3 = 5. As it's greater than 4, it's going in a circle to 1.
  • Leadership goes from 1 to 1 + a1 = 4.
  • Leadership goes from 4 to 4 + a4 = 8. Thus in circle it still remains at 4.
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #define endl "\n"
    typedef long long ll;
    using namespace std;
    
    const int N=1e4+7;
    
    int l[N];
    int c[N];
    int ans[N];
    
    int main(){
    	int n,m;
    	int flag=0;
    	cin>>n>>m;
    	memset(ans,0,sizeof(ans));
    	memset(c,0,sizeof(c));
    	for(int i=1;i<=m;++i){
    		cin>>l[i];
    		if(i>1){
    			if(ans[l[i-1]]!=0&&ans[l[i-1]]!=(l[i]-l[i-1]<=0?l[i]-l[i-1]+n:l[i]-l[i-1])){//对某个位置的操作如果重复但是 移动的距离不等就输出-1
                    flag=1;
                };
    			if(l[i]>l[i-1])ans[l[i-1]]=l[i]-l[i-1];
    			else ans[l[i-1]]=l[i]+n-l[i-1];
    		}
    		c[ans[l[i-1]]]=1;
    	}
    	int t=1;
    	for(int i=1;i<=n;++i){//每个位置的数不能重复没有的话就随便填上不重复的数
    		if(ans[i]==0){
    			while(c[t]){
    				t++;
    			}
    			ans[i]=t;
    			c[t]=1;
    		}
    	}
    	for(int i=1;i<=n;++i){
    		if(c[i]==0){
    			flag=1;
    		}
    	}
    	if(flag)cout<<-1;
    	else{
    		cout<<ans[1];
    		for(int i=2;i<=n;++i)cout<<" "<<ans[i];	
    	}
    	
    		
        return 0;
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值