Codeforces 818B Permutation Game
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 ...
随机推荐
- Java集合框架类
java集合框架类图 Collection接口(List.Set.Queue.Stack):
- ConcurrentHashMap源码解析
转自:http://www.iteye.com/topic/344876 ConcurrentHashMap是Java 5中支持高并发.高吞吐量的线程安全HashMap实现. 实现原理 锁分离 (Lo ...
- Window7 下 WNMP 环境搭建
WNMP 指"Windows 下的 Nginx.MariaDB/MySQL 和 PHP 环境". 下载 Nginx:http://nginx.org/en/download.htm ...
- 读《Java并发编程的艺术》(二)
上篇博客开始,我们接触了一些有关Java多线程的基本概念.这篇博客开始,我们就正式的进入了Java多线程的实战演练了.实战演练不仅仅是贴代码,也会涉及到相关概念和术语的讲解. 线程的状态 程的状态分为 ...
- Java泛型的应用——T extends Comparable<? super T>
在观察Java源码的时候,发现了这么一个写法T extends Comparable<? super T>.不禁纳闷为什么要这么写呢?有什么好处吗,extends和super在这里的作用着 ...
- DropDownList如何绑定DataTable,如何绑定DataSet
dpDnUpMenu是我定义的DropDownList控件 如果直接使用下面的方式,则会出现如下错误 dpDnUpMenu.DataSource = menu_tbBll.GetPID() dpDnU ...
- jmeter IP欺骗功能实现
使用过loadrunner的同学,应该都了解有个IP欺骗功能,jmeter遇到类似需求怎样实现呢? 环境:windows7,jdk1.8,jmeter3.1 使用IP欺骗功能前提是本地有多个可用IP, ...
- grub 学习之路
现在,是grub2的天下了呀,虽然网上关于grub2的资料不少,但很多都是就一个方面讨论的,跟着这些教程配置虽然也能够成功,但总是迷迷糊糊,不知这grub2背后到底是怎么实现的.所以决定花时间深入了解 ...
- WPF自动更新程序
WPF AutoUpdater 描述: WPF+MVVM实现的自动更新程序 支持更新包文件验证(比较文件MD5码) 支持区分x86与x64程序的更新 支持更新程序的版本号 支持执行更新策略 截图: 使 ...
- Grid search in the tidyverse
@drsimonj here to share a tidyverse method of grid search for optimizing a model's hyperparameters. ...