首先看一下题目
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. Java7中的ForkJoin并发框架初探(中)——JDK中实现简要分析

    原文发表于 2013 年 8 月 28 日 由 三石 根据前文描述的Doug Lea的理论基础,在JDK1.7中已经给出了Fork Join的实现.在Java SE 7的API中,多了ForkJoin ...

  2. 最近一些朋友问我,临近快毕业了专业不对口,想转行看到IT行业就业前景不错,但是编程语言众多不了解,不知道哪门语言能够快速入门掌握,短期能让我找到工作

    我做互联网前端后台开发也有四年多了,一路走过来,累并快乐着.快乐比艰辛更多,源自我的兴趣驱动.初中的一个偶然的机会我接触到了计算机,从那个时候就喜欢上开始经常到网吧上网.那个时候我对计算机领域的认识是 ...

  3. DropDownList如何绑定DataTable,如何绑定DataSet

    dpDnUpMenu是我定义的DropDownList控件 如果直接使用下面的方式,则会出现如下错误 dpDnUpMenu.DataSource = menu_tbBll.GetPID() dpDnU ...

  4. 【转】典型的JavaScript面试题

    问题1: 作用域(Scope) (function() { "use strict"; var a = b = 5; })(); console.log(b); 控制台(conso ...

  5. OpenCV探索之路(十五):角点检测

    角点检测是计算机视觉系统中用来获取图像特征的一种方法.我们都常说,这幅图像很有特点,但是一问他到底有哪些特点,或者这幅图有哪些特征可以让你一下子就识别出该物体,你可能就说不出来了.其实说图像的特征,你 ...

  6. Lambda语言篇 —— lambda, 方法引用, 目标类型和默认方法

    本文介绍了Java SE 8中新引入的lambda语言特性以及这些特性背后的设计思想.这些特性包括: lambda表达式(又被成为"闭包"或"匿名方法") 方法 ...

  7. eclipse hibernate导出数据库实体类

    打开eclipse->help->Eclipse Marketplace->查找hibernate->安装如下插件 只要安装其中一个,hibernate tool即可: 安装完 ...

  8. iOS安全攻防之越狱设备检测

    iOS 越狱(iOS Jailbreaking),是用于获取苹果公司便携装置操作系统iOS最高权限的一种技术手段,用户使用这种技术及软件可以获取到 iOS 的最高权限,甚至可能可以进一步解开运营商对手 ...

  9. Vue.js高仿饿了么WebApp

    介绍 学习Vue.js也有一阵子了,为了加深对Vue的理解及运用,做了一个小项目.这是一个高仿饿了么外卖WebApp,现已完成商品预览.商品详情.商家预览.添加购物.查看评论等功能. 部分截图 项目预 ...

  10. 入职这一段时间的总结,Don't Repeat Yourself.

    1.第一次接触到大型软件系统的开发,现在我们使用的是 python + flask +vue.js ,数据库:postgresql 2. 不要在自己不懂的情况下复制代码,每次分析一段代码的时候,就跟以 ...