Educational Codeforces Round 24 B

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.

Examples

input

4 52 3 1 4 4

output

3 1 2 4 

input

3 33 1 2

output

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

题意:n个人,进行m次操作,操作规则根据A数组,起始位置为L1 L1+A[L1]得到下一个数字L2

L2+A[L2]得到下一个数字L3这样进行下去,且Li<=n,如果计算超过则必须%n

现在告诉我们L数组,还原出A数组

解法:

1 不存在情况 A数组数组唯一,若出现重复则不存在

      A数字每个位置答案唯一,即如果求出A[2]=3,那么A[2]不可以等于其他值,若出现其他答案则不存在

2 填补数字 A[i]<A[i+1] 则该位置为A[i+1]-A[i]

     A[i]>=A[i+1] 则该位置n+A[i+1]-A[i]

未填补数字则缺啥补啥就行,中间注意判断存在条件

#include<bits/stdc++.h>
using namespace std;
const long long N = 10010;
vector<long long> v;
long long A[N];
long long B[N];
map<int,int>Q;
int main(){
    long long a,b;
    cin >> a >> b;
    for(int i=1;i<=b;i++){
        cin>>A[i];
    }
    for(int i=1;i<=a;i++){
        B[i]=-1;
    }
    long long ans=A[1];
    for(int i=2;i<=b;i++){
        if(A[i]>ans){
            long long temp=A[i]-ans;
            if(B[ans]!=-1&&B[ans]!=temp){
                cout<<"-1";
                return 0;
            }
            B[ans]=temp;
        }else{
            long long num=a+A[i];
            if(B[ans]!=-1&&B[ans]!=num-ans){
                cout<<"-1";
                return 0;
            }
            B[ans]=num-ans;
        }
        ans=A[i];
    }
    for(int i=1;i<=a;i++){
        if(Q[B[i]]>1||B[i]>a){
            cout<<"-1";
            return 0;
        }else if(B[i]!=-1){
            Q[B[i]]++;
        }
    }
    for(int i=1;i<=a;i++){
        if(B[i]==-1){
            long long str=1;
            while(Q[str]) str++;
            Q[str]++;
            B[i]=str;
        }
    }
    for(int i=1;i<=a;i++){
        if(Q[B[i]]>1||B[i]>a){
            cout<<"-1";
            return 0;
        }else if(B[i]!=-1){
            Q[B[i]]++;
        }
    }
    for(int i=1;i<=a;i++){
        cout<<B[i]<<" ";
    }
    return 0;
}
时间: 2024-12-20 01:19:38

Educational Codeforces Round 24 B的相关文章

Educational Codeforces Round 24 F. Level Generation(三分)

题目链接:Educational Codeforces Round 24 F. Level Generation 题意: 给你n个点,让你构造ans条边,使得这ans条边中至少有一半是桥. 让你求ans的最大值. 题解: 首先我们将每一个点按顺序连起来,那么可以构成n-1个桥. 然后我们可以把其中的x个点拿出来连边,这些边都不是桥. x个点最多能连x*(x-1)条边,然后剩下的n-x个点连的边将会构成桥. 然后就可以构造一个函数关系,详见check函数,然后三分一下就行了. 1 #include

Educational Codeforces Round 24 E

Vova again tries to play some computer card game. The rules of deck creation in this game are simple. Vova is given an existing deck of n cards and a magic number k. The order of the cards in the deck is fixed. Each card has a number written on it; n

Educational Codeforces Round 24 D

Alice and Bob got very bored during a long car trip so they decided to play a game. From the window they can see cars of different colors running past them. Cars are going one after another. The game rules are like this. Firstly Alice chooses some co

Educational Codeforces Round 24 CF 818 A-G 补题

6月快要结束了 期末也过去大半了 马上就是大三狗了 取消了小学期后20周的学期真心长, 看着各种北方的学校都放假嗨皮了,我们这个在北回归线的学校,还在忍受酷暑. 过年的时候下定决心要拿块ACM的牌子,一直坚持刷题,这一个学期刷了200道吧,感觉还是小有收获.特别是Ural和Codeforces上的题,质量很高. 然后4月的校赛,5月的省赛,发挥的也一般,不过也没有太失常. 希望暑假的选拔赛能碰到有趣的队友 蛤蛤. 这两天各种考试,实在是太忙,看了一下edu24的题目,不是很容易,做了一道水题,以

Educational Codeforces Round 24 A

There are n students who have taken part in an olympiad. Now it's time to award the students. Some of them will receive diplomas, some wiil get certificates, and others won't receive anything. Students with diplomas and certificates are called winner

Educational Codeforces Round 24

陷入了一种每场比赛打完都不想改题的虚无状态,不能这样,改题改题改题. 昨晚只写了三道题意即题解的题…感觉意识模糊,看了看是unrated就睡了 CF已经连续三场unrated了qwq,我一共就没打过几场 A. Diplomas and Certificates 题意:拿到certificate的人数将会是拿到diploma人数的k倍,但拿到他们的总人数不能超过n/2 把n/2向下取底分成k+1分 #include<iostream> #include<cstdio> #includ

Educational Codeforces Round 21 G. Anthem of Berland(dp+kmp)

题目链接:Educational Codeforces Round 21 G. Anthem of Berland 题意: 给你两个字符串,第一个字符串包含问号,问号可以变成任意字符串. 问你第一个字符串最多包含多少个第二个字符串. 题解: 考虑dp[i][j],表示当前考虑到第一个串的第i位,已经匹配到第二个字符串的第j位. 这样的话复杂度为26*n*m*O(fail). fail可以用kmp进行预处理,将26个字母全部处理出来,这样复杂度就变成了26*n*m. 状态转移看代码(就是一个kmp

Educational Codeforces Round 26 D. Round Subset(dp)

题目链接:Educational Codeforces Round 26 D. Round Subset 题意: 给你n个数,让你选其中的k个数,使得这k个数的乘积的末尾的0的个数最大. 题解: 显然,末尾乘积0的个数和因子2和因子5的个数有关. 然后考虑dp[i][j]表示选i个数,当前因子5的个数为j时,能得到因子2最多的为多少. 那么对于每个数,记录一下因子2和5的个数,做一些01背包就行了. 1 #include<bits/stdc++.h> 2 #define mst(a,b) me

Educational Codeforces Round 23 F. MEX Queries(线段树)

题目链接:Educational Codeforces Round 23 F. MEX Queries 题意: 一共有n个操作. 1.  将[l,r]区间的数标记为1. 2.  将[l,r]区间的数标记为0. 3.  将[l,r]区间取反. 对每个操作,输出标记为0的最小正整数. 题解: hash后,用线段树xjb标记一下就行了. 1 #include<bits/stdc++.h> 2 #define ls l,m,rt<<1 3 #define rs m+1,r,rt<&l