首先看一下题目
B. Permutation Game
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

n children are standing in a circle and playing a game. Children's numbers in clockwise order form a permutation a1, a2, ..., an of lengthn. 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 5
2 3 1 4 4
output
3 1 2 4 
input
3 3
3 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个孩子围成一个圈,他们每个人分别有一个index和序号,index是从1-n,序号是从a1到an,并且恰好满足它们是1-n的一个全排列。

游戏分成m步,从第1步开始数。第i步的时候,当前的leader有一个index,我们记做k,他需要从他下一个人开始数出ak个人,然后再下一个人成为新的leader。我们需要给出m个数字,从l1到lm。对于第一个leader,我们需要找出某个ai,使得l1=ai,此时第i个孩子是第一个leader。

我们的目标是给出m个数字,使得所有的规则都成立。

---------------------------------------------------分割线------------------------------------------------------------------------------------------------------------------------------------------------------------------

之前的理解有点扯。。。我在晚上终于理解了。。

题目给定了leader index序列,要我们求a[n]序列,显然由leader index 的变化,我们是可以轻易求出a[leader index]的。。。但是这题目的描述确实不行,也确实有很多人吐槽题意。。。

最后贴一下代码

#include <iostream>

using namespace std;

const int maxn = ;

int used[maxn];
int l[maxn];
int a[maxn]; int main() {
int n, m;
cin >> n >> m;
for(int i = ; i < m; i++) {
cin >> l[i];
}
for(int i = ; i < m; i++) {
int delta = l[i] - l[i-];
if(delta <= ) {
delta = n + delta;
}
if(l[i-] != used[delta] && used[delta] != ) {
cout << "-1" << endl;
return ;
}
a[l[i-]] = delta;
used[delta] = l[i-];
}
if(a[] == ) {
for(int i = ; i < maxn; i++) {
if(!used[i]) {
a[] = i;
used[i] = true;
break;
}
}
}
cout << a[];
for(int i = ; i <= n; i++) {
if(a[i] == ) {
for(int j = ; j < maxn; j++) {
if(!used[j]) {
a[i] = j;
used[j] = true;
break;
}
}
}
cout << " " << a[i];
}
cout << endl;
return ;
}

我今天又看了一下cf,发现自己居然WA了。不过确实是因为代码写错了。。有一些情况没考虑到。如下是正确代码

#include <iostream>

using namespace std;

const int maxn = ;

int used[maxn];
int l[maxn];
int a[maxn]; int main() {
int n, m;
cin >> n >> m;
for(int i = ; i < m; i++) {
cin >> l[i];
}
for(int i = ; i < m; i++) {
int delta = l[i] - l[i-];
if(delta <= ) {
delta = n + delta;
}
if(a[l[i-]] == ) {
if(used[delta]) {
cout << "-1" << endl;
return ;
}
a[l[i-]] = delta;
used[delta] = a[l[i-]];
continue;
}
if(delta != a[l[i-]]) {
cout << "-1" << endl;
return ;
} } for(int i = ; i <= n; i++) {
if(!a[i]) {
for(int j = ; j <= n; j++) {
if(!used[j]) {
used[j] = true;
a[i] = j;
break;
}
}
}
}
cout << a[];
for(int i = ; i <= n; i++) {
cout << " " << a[i];
}
cout << endl;
return ;
}

Codeforces 818B Permutation Game的更多相关文章

  1. 贪心 CodeForces 137B Permutation

    题目传送门 /* 贪心:因为可以任意修改,所以答案是没有出现过的数字的个数 */ #include <cstdio> #include <algorithm> #include ...

  2. codeforces B. Permutation 解题报告

    题目链接:http://codeforces.com/problemset/problem/359/B 题目意思:给定n和k的值,需要构造一条长度为2n(每个元素取值范围只能是[1,2n])且元素各不 ...

  3. Codeforces 1158C Permutation recovery

    https://codeforces.com/contest/1158/problem/C 题目 已知 $p_1, p_2, \dots, p_n$ 是 $1$ 到 $n$ 的一个排列. 给出关于这个 ...

  4. Codeforces - 1033C - Permutation Game - 简单dp - 简单数论

    https://codeforces.com/problemset/problem/1033/C 一开始觉得自己的答案会TLE,但是吸取徐州赛区的经验去莽了一发. 其实因为下面这个公式是 $O(nlo ...

  5. Codeforces 1295E. Permutation Separation (线段树)

    https://codeforces.com/contest/1295/problem/E 建一颗线段树,叶子结点是花费从1到i所需要花费的前缀和,表示前i个元素全部移动到右边的花费,再维护区间最小值 ...

  6. Codeforces 1159E Permutation recovery(构造+拓扑)

    这道题其实只要解决了什么时候输出 -1 ,那么此题的构造方法也就解决了.首先我们可以观察这组 3 3 4 和 3 4 4 ,可以算出第二组是不成立的,在观察一组 2 3 4 5 和  3 2 4 5 ...

  7. Codeforces 1295E Permutation Separation

    题目链接 link Solution 暴力一眼就可以看出来,枚举分界点,然后左右两边统计答案即可,但复杂度是我们无法接受的 然后我们看我们可以优化哪一部分 \(1^0\) 枚举:这部分没有办法优化 \ ...

  8. Codeforces 817+818(A~C)

    (点击题目即可查看原题) 817A Treasure Hunt 题意:给出起点和终点,每次移动只能从 (a,b)移动至(a+x,b+y) , (a+x,b-y) , (a-x,b+y) , (a-x, ...

  9. codeforces285C

    Building Permutation CodeForces - 285C Permutation p is an ordered set of integers p1,  p2,  ...,  p ...

随机推荐

  1. 区别client、offset、scroll系列以及event的几个距离属性

    element元素结点属性 一. offset系列 1.offsetWidth 和offsetHeight element.offsetWidth是一个只读属性,它包括了: css width + p ...

  2. 基于本地文件系统的LocalDB

    零.前言 之前写一些小工具的时候,需要用到数据存储方面的技术,但是用数据库又觉得太大了,本地文件存储txt文件存储又不是很规范,于是乎想到了去编写一个简单的基于本地文件系统的数据存储库,暂且叫它loc ...

  3. web 直播&即时聊天------阿里云、融云

    随着直播越来越火,所在公司也打算制作自己的直播,所以去了解了这方面,使用后发现还是有些问题需要记录的. 经过分析,制作直播应该是分为两块来做,即直播与实时评论.这里先去制作实时评论,等直播ok后,也会 ...

  4. .Net程序员学用Oracle系列(10):系统函数(中)

    1.四大转换函数 1.1.TO_CHAR 1.2.TO_NUMBER 1.3.TO_DATE 1.4.CAST 2.两大近似值函数 2.1.ROUND 2.2.TRUNC 3.正则函数 3.1.正则函 ...

  5. TCP协议随笔

    传输控制协议TCP是面向连接.保证高可靠性(数据无丢失.数据无失序.数据无错误.数据无重复到达)传输层协议.TCP/IP结构对应OSITCP/IP                           ...

  6. eclipse内存溢出设置

    1. arguments中的内容添加红色部分:   -Dcatalina.base="E:\workspace\.metadata\.plugins   \org.eclipse.wst.s ...

  7. 每天一道Java题[10]

    题目 阐述创建线程最常用的两种方法及其对比. 解答 方法一:继承Thread类实现 步骤: 创建Thread类的子类,如MyThread. 重写Thread类的run()方法. 实例化MyThread ...

  8. 一步一步实现android studio代码上传到github。

    本文只注重代码上传能成功就好,不解释什么是git什么事github,git有什么优势. 1,先创建一个android应用, 第二步:创建github账户 和 安装git.网上的文章多如牛毛.唯一要说的 ...

  9. [转]tomcat部署(1)

      阅读目录 1 目录结构 2 部署 3 发布 4 测试 本篇参考Tomcat官方文档:<First Webapp>翻译,并结合自己的开发经验介绍关于tomcat部署以及发布的相关内容. ...

  10. 最全面的Java字节byte操作,处理Java基本数据的转换及进制转换操作工具,流媒体及java底层开发项目常用工具类

    前言:用于处理Java基本数据的转换及进制转换操作工具 一.实现功能 1.int与byte互转 2.int与byte[]互转 3.short与byte互转 4.short与byte[]互转 5.16位 ...