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 ...
随机推荐
- KafKa+Zookeeper+Flume部署脚本
喜欢学习的朋友可以收藏 愿意了解框架技术或者源码的朋友直接加求求(企鹅):2042849237
- js实用方法记录-js动态加载css、js脚本文件
js实用方法记录-动态加载css/js 附送一个加载iframe,h5打开app代码 1. 动态加载js文件到head标签并执行回调 方法调用:dynamicLoadJs('http://www.yi ...
- Java调用IDL方法总结
Java调用IDL方法总结 Java调用IDL程序,需要先在java中加载IDL的java包(javaidlb.jar),该包不需要下载,在IDL的安装目录中可以直接找到(C:\Program Fil ...
- Quartz.net 定时任务之Cron表达式
一.cron表达式简单介绍和下载 1.在上一篇博客"Quartz.net 定时任务之简单任务"中,我简单介绍了quartz的使用,而这篇博客我将介绍cron的具体使用(不足之处望大 ...
- jQuery选择器中的空格问题
前几天就遇到过这样的问题,明明我用的是('tr :even').css('background','#ccc')想改变表格中行的背景色,反复试了还是没改变.还问了度娘还是没找到原因所在(当时问题描述的 ...
- 关于微信小程序拒绝授权后,重新授权并获取用户信息
最近公司做了一些有关微信小程序的项目,涉及到授权获取用户基本信息,但是在拒绝授权之后就不会再出现授权窗口: 看网上也有很多人遇到了同样的问题,所以记录下来我的处理方法,供大家和自己学习和记录: 当调用 ...
- Java web开发中页面跳转小技巧——跳转后新页面在新窗口打开
最近学习Java web,在学习过程中想实现一个需求,就是在jsp页面跳转的时候,希望跳转后的新页面在新窗口中打开, 而不是覆盖原来的页面,这个需求使我困惑了好长时间,后来通过大海捞针似的在网上寻找方 ...
- Python Tkinter学习(1)——第一个Tkinter程序
注:本文可转载,转载请注明出处:http://www.cnblogs.com/collectionne/p/6885066.html.格式修改未完成. Tkinter资料 Python Wiki, T ...
- Java泛型学习
1.泛型的概念 泛型即"参数化类型",就比如我们定义方法的时候,定义一个变量,称为形参,变量值根据传进去的实参的值不同而改变.而泛型的出现,就是为了解决类型也能根据传进去的类型改变 ...
- 一天搞定CSS:初识css--01
1)CSS:层叠样式表(英文全称:Cascading Style Sheets) 是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言. C ...