CF818B.Permutation Game
普及/提高-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
n children are standing in a circle and playing a game. Children's numbers in clockwise order form a permutation _a_1, _a_2, ..., a__n 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 a__i 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 _l_1, _l_2, ..., l__m — indices of leaders in the beginning of each step. Child with number _l_1 is the first leader in the game.
Write a program which will restore a possible permutation _a_1, _a_2, ..., a__n. If there are multiple solutions then print any of them. If there is no solution then print -1.
有 n 个孩子围成一个圆圈进行游戏。孩子们按顺时针方向编号,构成一个长度为 n 的排列 a1,a2,…,an。这是一个整数序列,其中 1 到 n 中的每个整数恰好出现一次。
游戏共进行 m 轮。在每一轮中,当前领导者(其位置索引为 i)从下一个人开始,沿顺时针方向数出 ai 个人;被数到的第 ai 个人成为新的领导者。
现给出 l1,l2,…,lm —— 每轮开始时领导者的位置索引。编号为 l1 的孩子是游戏的第一位领导者。
请编写一个程序,还原出一个可能的排列 a1,a2,…,an。若存在多个解,输出任意一个即可;若无解,则输出 −1。
输入格式
The first line contains two integer numbers n, m (1 ≤ n, m ≤ 100).
The second line contains m integer numbers _l_1, _l_2, ..., l__m (1 ≤ l__i ≤ n) — indices of leaders in the beginning of each step.
第一行包含两个整数 n、m(1 ≤ n, m ≤ 100)。
第二行包含 m 个整数 l1, l2, ..., lm(1 ≤ li ≤ n)—— 每一步开始时领导者们的下标。
输出格式
Print such permutation of n numbers _a_1, _a_2, ..., a__n that leaders in the game will be exactly _l_1, _l_2, ..., l__m 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.
输出一个由 n 个数 a1,a2,…,an 构成的排列,使得在遵循所有规则的前提下,游戏中的“领导者”(leaders)恰好为 l1,l2,…,lm。若存在多个满足条件的解,输出任意一个即可。
若不存在满足上述所有条件的排列,则输出 −1。
输入输出样例
输入#1
4 5 2 3 1 4 4
输出#1
3 1 2 4
输入#2
3 3 3 1 2
输出#2
-1
说明/提示
Let's follow leadership in the first example:
- Child 2 starts.
- Leadership goes from 2 to 2 + _a_2 = 3.
- Leadership goes from 3 to 3 + _a_3 = 5. As it's greater than 4, it's going in a circle to 1.
- Leadership goes from 1 to 1 + _a_1 = 4.
- Leadership goes from 4 to 4 + _a_4 = 8. Thus in circle it still remains at 4.
我们来跟随第一个示例中的领导权传递过程:
- 儿童 2 首先开始。
- 领导权从 2 传递到 2+a2=3。
- 领导权从 3 传递到 3+a3=5;由于该值大于 4,因此在环中绕回至 1。
- 领导权从 1 传递到 1+a1=4。
- 领导权从 4 传递到 4+a4=8;因此在环中它仍停留在 4。
输入解题思路,AI测评打分。不知道怎么写?