CF818B-Permutation Game
来源:互联网 发布:网站建设优化及推广 编辑:程序博客网 时间:2024/06/06 18:58
B. Permutation Game
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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 length n. 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 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.
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.
题目大意:自己看吧
解题思路:
注意
判断一下即可。
比赛时自己写的代码(丑。。。)
#include<iostream>#include<cstdio>#include<vector>#include<cmath>#include<stack>using namespace std;typedef long long LL;const int MAXN=105;int L[MAXN];int a[MAXN];bool vis[MAXN];bool mark[MAXN];stack<int> s;int main(){ int n,m; while(cin>>n>>m) { while(!s.empty()) s.pop(); for(int i=1;i<=n;i++) { vis[i]=false; mark[i]=false; a[i]=0; s.push(i); } for(int i=1;i<=m;i++) cin>>L[i]; bool flag=true; int tmp; for(int i=1;i<m;i++) { tmp=(L[i+1]-L[i]+n)%n; if(tmp==0) tmp=n; if(!vis[L[i]]&&!mark[tmp]) { a[L[i]]=tmp; mark[tmp]=true; vis[L[i]]=true; }else if(a[L[i]]==tmp) { a[L[i]]=tmp; }else { flag=false; break; } } if(!flag) cout<<-1<<endl; else { if(vis[1]) cout<<a[1]; else { int t=0; while(!s.empty()) { t=s.top(); s.pop(); if(mark[t]) continue; cout<<t; break; } } for(int i=2;i<=n;i++) { if(vis[i]) cout<<" "<<a[i]; else { int t=0; while(!s.empty()) { t=s.top(); s.pop(); if(mark[t]) continue; cout<<" "<<t; break; } } } cout<<endl; } } return 0;}
赛后参考他人思路写的代码(简洁一些)
#include<iostream>using namespace std;const int MAXN=105;int n,m;int L[MAXN];int a[MAXN];bool vis[MAXN];int main(){ ios::sync_with_stdio(false); while(cin>>n>>m) { for(int i=1;i<=n;i++) { vis[i]=false; a[i]=-1; } for(int i=1;i<=m;i++) cin>>L[i]; for(int i=1;i<m;i++) { int tmp=(L[i+1]-L[i]+n)%n; if(tmp==0) tmp=n; if((!vis[tmp]&&a[L[i]]==-1)||a[L[i]]==tmp) { a[L[i]]=tmp; vis[tmp]=true; }else { cout<<-1<<endl; return 0; } } int p=1; for(int i=1;i<=n;i++) { if(!vis[i]) { while(a[p]!=-1) p++; a[p]=i; } } for(int i=1;i<=n;i++) { cout<<a[i]; if(i!=n) cout<<' '; else cout<<'\n'; } }}
- CF818B-Permutation Game
- Permutation Game
- SPOJ Permutation Game 质数分解
- Codeforces 818B Permutation Game
- Educational Codeforces Round 24 B. Permutation Game
- Educational Codeforces Round 24B Permutation Game 补题
- Permutation
- Permutation
- permutation
- Permutation
- Permutation
- Permutation
- permutation
- Permutation
- permutation
- Permutation
- Permutation
- permutation
- JavaScript本地对象常用方法(二)——字符串对象
- C语言 学生成绩管理系统
- 写给大忙人看的Java核心技术-Solutions提供
- CF818A-Diplomas and Certificates
- 华为OJ——识别有效的IP地址和掩码并进行分类统计
- CF818B-Permutation Game
- 这是数组升序的排序的案例
- ipython
- 利用CSS进行各种图形的绘制
- XXE漏洞笔记
- 郑州-大连:骑行在路上
- Java注解简介
- jsPDF的避坑教程
- python numpy库