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

Dog Card is a card game. In the game, there are a total of 2n cards in the deck, each card has a value, and the values of these 2n cards form a permutation of 1 ~ 2n. There is a skill that works as follows: 1. Draw a card from the top of the deck. 2. If the deck is empty, then skip to step 3, otherwise you guess whether the card on the top of the deck has a higher value than your last drawn card and draw a card from the top of the deck. If your guess is correct, then repeat this step, otherwise skip to step 3. 3. End this process. Nana enjoys playing this game, although she may not be skilled at it. Therefore, her guessing strategy when using this skill is simple: if the value of the last drawn card is less than or equal to n, then she guesses that the next oard's valve is higher, ther wse, she guedses thet the next card's vaue s lomler she wârns tb dmokt tor anfafrhlm decks of cards (Obviously, there are (2n)! cases), how many cards she can draw in total if she uses the skill only once in each case. Since this number can be very large,please provide the answer modulo a given value.Dog Card is a card game. In the game, there are a total of 2n cards in the deck, each card has a value, and the values of these 2n cards form a permutation of 1 ~ 2n. There is a skill that works as follows: 1. Draw a card from the top of the deck. 2. If the deck is empty, then skip to step 3, otherwise you guess whether the card on the top of the deck has a higher value than your last drawn card and draw a card from the top of the deck. If your guess is correct, then repeat this step, otherwise skip to step 3. 3. End this process. Nana enjoys playing this game, although she may not be skilled at it. Therefore, her guessing strategy when using this skill is simple: if the value of the last drawn card is less than or equal to n, then she guesses that the next oard's valve is higher, ther wse, she guedses thet the next card's vaue s lomler she wârns tb dmokt tor anfafrhlm decks of cards (Obviously, there are (2n)! cases), how many cards she can draw in total if she uses the skill only once in each case. Since this number can be very large,please provide the answer modulo a given value.给出c++代码及中文解释
07-25
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值