1 second
256 megabytes
standard input
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.
The first line contains two integer numbers n, m (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.
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.
4 5
2 3 1 4 4
3 1 2 4
3 3
3 1 2
-1
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的更多相关文章
-
贪心 CodeForces 137B Permutation
题目传送门 /* 贪心:因为可以任意修改,所以答案是没有出现过的数字的个数 */ #include <cstdio> #include <algorithm> #include ...
-
codeforces B. Permutation 解题报告
题目链接:http://codeforces.com/problemset/problem/359/B 题目意思:给定n和k的值,需要构造一条长度为2n(每个元素取值范围只能是[1,2n])且元素各不 ...
-
Codeforces 1158C Permutation recovery
https://codeforces.com/contest/1158/problem/C 题目 已知 $p_1, p_2, \dots, p_n$ 是 $1$ 到 $n$ 的一个排列. 给出关于这个 ...
-
Codeforces - 1033C - Permutation Game - 简单dp - 简单数论
https://codeforces.com/problemset/problem/1033/C 一开始觉得自己的答案会TLE,但是吸取徐州赛区的经验去莽了一发. 其实因为下面这个公式是 $O(nlo ...
-
Codeforces 1295E. Permutation Separation (线段树)
https://codeforces.com/contest/1295/problem/E 建一颗线段树,叶子结点是花费从1到i所需要花费的前缀和,表示前i个元素全部移动到右边的花费,再维护区间最小值 ...
-
Codeforces 1159E Permutation recovery(构造+拓扑)
这道题其实只要解决了什么时候输出 -1 ,那么此题的构造方法也就解决了.首先我们可以观察这组 3 3 4 和 3 4 4 ,可以算出第二组是不成立的,在观察一组 2 3 4 5 和 3 2 4 5 ...
-
Codeforces 1295E Permutation Separation
题目链接 link Solution 暴力一眼就可以看出来,枚举分界点,然后左右两边统计答案即可,但复杂度是我们无法接受的 然后我们看我们可以优化哪一部分 \(1^0\) 枚举:这部分没有办法优化 \ ...
-
Codeforces 817+818(A~C)
(点击题目即可查看原题) 817A Treasure Hunt 题意:给出起点和终点,每次移动只能从 (a,b)移动至(a+x,b+y) , (a+x,b-y) , (a-x,b+y) , (a-x, ...
-
codeforces285C
Building Permutation CodeForces - 285C Permutation p is an ordered set of integers p1, p2, ..., p ...
随机推荐
-
安卓开发:效果图中标注的像素尺寸如何转换为安卓的dp尺寸?
我们的UI基于1920x1080分辨率给的尺寸标注,但是在安卓开发中大家一般都使用dp.sp来标注界面尺寸,所以需要一个dp与sp的转换公式. 一开始参考的的这篇文章:关于Android开发中px.d ...
-
SPOJ ONEZERO(搜索)
搜索的好题,,,, 摘自题解: 题意: 给一个数n,求n 的最小的倍数,满足它的10进制 表示中每一位不是0就是1. 思路: 用f(x)表示被n整除取模后的最小数,那么从0开始,每次往后添0或者1,如 ...
-
Javascript继承机制的设计
写软工作业时各种蛋疼:主要在于Javascript没有“子类”“父类”“接口”的概念,只能使用prototype来实现,看了下面一篇文章,感觉写得很不错~ http://www.ruanyifeng. ...
-
上传头像,界面无跳转,php+js
上传头像,界面无跳转的方式很多,我用的是加个iframe那种.下面直接上代码. html: //route 为后端接口//upload/avatar 为上传的头像的保存地址//imgurl=/uplo ...
-
(Problem 5)Smallest multiple
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any rema ...
-
运维之Linux基础知识(三)
运维之Linux基础知识(三) 1. 查看文本 cat tac more less head tail 1.1 cat 连接并显示文件 cat -n:在显示的时候,将每一行编号 -E:显示结束符$ - ...
-
Redis配置主从复制
Redis配置主从复制 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.操作环境介绍 1>.操作系统环境 [root@node101.yinzhengjie.org.cn ...
-
关于js特效轮播图练习
[出现问题] js轮播图,图片未正常轮播. [解决方法] 通过对代码的检查,发现是以下三个原因造成的错误. 1.js代码问题 js代码使用alert(test);,测试修改完毕后,发现依然没有解决错误 ...
-
myeclipse 8.5破解方法
转自: http://blog.sina.com.cn/s/blog_70600f7201018pib.html , 记录下来存档 Step: 1.建立一个任意名称的Java Project 2.在 ...
-
vue中axios调用接口和用node.js跨域
<script>const API_PROXY = 'https://bird.ioliu.cn/v1/?url='import axios from 'axios'export defa ...