Educational Codeforces Round 24 B
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.
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个人,进行m次操作,操作规则根据A数组,起始位置为L1 L1+A[L1]得到下一个数字L2
L2+A[L2]得到下一个数字L3这样进行下去,且Li<=n,如果计算超过则必须%n
现在告诉我们L数组,还原出A数组
解法:
1 不存在情况 A数组数组唯一,若出现重复则不存在
A数字每个位置答案唯一,即如果求出A[2]=3,那么A[2]不可以等于其他值,若出现其他答案则不存在
2 填补数字 A[i]<A[i+1] 则该位置为A[i+1]-A[i]
A[i]>=A[i+1] 则该位置n+A[i+1]-A[i]
未填补数字则缺啥补啥就行,中间注意判断存在条件
#include<bits/stdc++.h>
using namespace std;
const long long N = ;
vector<long long> v;
long long A[N];
long long B[N];
map<int,int>Q;
int main(){
long long a,b;
cin >> a >> b;
for(int i=;i<=b;i++){
cin>>A[i];
}
for(int i=;i<=a;i++){
B[i]=-;
}
long long ans=A[];
for(int i=;i<=b;i++){
if(A[i]>ans){
long long temp=A[i]-ans;
if(B[ans]!=-&&B[ans]!=temp){
cout<<"-1";
return ;
}
B[ans]=temp;
}else{
long long num=a+A[i];
if(B[ans]!=-&&B[ans]!=num-ans){
cout<<"-1";
return ;
}
B[ans]=num-ans;
}
ans=A[i];
}
for(int i=;i<=a;i++){
if(Q[B[i]]>||B[i]>a){
cout<<"-1";
return ;
}else if(B[i]!=-){
Q[B[i]]++;
}
}
for(int i=;i<=a;i++){
if(B[i]==-){
long long str=;
while(Q[str]) str++;
Q[str]++;
B[i]=str;
}
}
for(int i=;i<=a;i++){
if(Q[B[i]]>||B[i]>a){
cout<<"-1";
return ;
}else if(B[i]!=-){
Q[B[i]]++;
}
}
for(int i=;i<=a;i++){
cout<<B[i]<<" ";
}
return ;
}
Educational Codeforces Round 24 B的更多相关文章
- Educational Codeforces Round 24 A 水 B stl C 暴力 D stl模拟 E 二分
A. Diplomas and Certificates time limit per test 1 second memory limit per test 256 megabytes input ...
- Educational Codeforces Round 24 CF 818 A-G 补题
6月快要结束了 期末也过去大半了 马上就是大三狗了 取消了小学期后20周的学期真心长, 看着各种北方的学校都放假嗨皮了,我们这个在北回归线的学校,还在忍受酷暑. 过年的时候下定决心要拿块ACM的牌子, ...
- Educational Codeforces Round 24 E
Vova again tries to play some computer card game. The rules of deck creation in this game are simple ...
- codeforces Educational Codeforces Round 24 (A~F)
题目链接:http://codeforces.com/contest/818 A. Diplomas and Certificates 题解:水题 #include <iostream> ...
- Educational Codeforces Round 24
A. Diplomas and Certificates time limit per test 1 second memory limit per test 256 megabytes input ...
- Educational Codeforces Round 24 D
Alice and Bob got very bored during a long car trip so they decided to play a game. From the window ...
- Educational Codeforces Round 24 A
There are n students who have taken part in an olympiad. Now it's time to award the students. Some o ...
- Educational Codeforces Round 24 题解
A: 考你会不会除法 //By SiriusRen #include <bits/stdc++.h> using namespace std; #define int long long ...
- Educational Codeforces Round 32
http://codeforces.com/contest/888 A Local Extrema[水] [题意]:计算极值点个数 [分析]:除了第一个最后一个外,遇到极值点ans++,包括极大和极小 ...
随机推荐
- Android Menu开源项目整合工程
本实例整合了关于Android Menu的优秀开源代码,方便有需要用到Menu开源项目的小伙伴使用. 一.整合的项目有: SlidingMenu:https://github.com/jfeinste ...
- 【iOS系列】-自定义Modar动画
[iOS系列]-自定义Modar动画.md 我们需要做的最终的modar动画的效果是这样的, 就是点击cell,cell发生位移,慢慢的到第二个界面上的.为了做出这样的动画效果,我们需要以下的知识. ...
- 细数AutoLayout以来UIView和UIViewController新增的相关API
本文转载至 http://www.itjhwd.com/autolayout-uiview-uiviewcontroller-api/ 细数AutoLayout以来UIView和UIViewContr ...
- MFC上显示摄像头JPEG图片数据的两种方法
其一是借助opencv,其二是利用流对象. 方法一: CvMat *mat; ,,CV_8UC1); ,,CV_8UC1,JPEGBuf); /*初始化矩阵信息头,这里的JPEGBuf就是JPEG图像 ...
- delphi 7中使用idhttp抓取网页 解决假死现象(使用TIdAntiFreezeControl控件)
在delphi 7中使用idhttp抓取网页,造成窗口无反应的假死状态.通过搜索获得两种方法. 1.写在线程中,但是调用比较麻烦 2.使用delphi 提供的idantifreeze(必须安装indy ...
- react native camera
最近在尝试用react native camera iOS版本很方便就调试通过了,react的试用非常方便 android版本要单独试用fork的 屏蔽了lint的报错后也可以调试通过 参考这篇文章填 ...
- “There's no Qt version assigned to this project for platform ” - visual studio plugin for Qt
1.find menu "Qt VS Tools", select Qt Options 2.add a new Qt version 3. right click the tar ...
- stringBuffer、StringBuilder、排序、Arrays、Jdk1.5新特性(java基础知识十三)
1.StringBuffer类的概述 * A:StringBuffer类概述 * 通过JDK提供的API,查看StringBuffer类的说明 * 1.线程安全的可变字符序列. * 2.可将字符串缓冲 ...
- html5--3.2 input元素(1)
html5--3.2 input元素(1) 学习要点 input元素及其属性 input元素 用来设置表单中的内容项,比如输入内容的文本框,按钮等 不仅可以布置在表单中,也可以在表单之外的元素使用 i ...
- WAS:修改jsp编译器用JDK5.0
问题现象: 今天有现场反映,访问应用的个别页面报错,报错内容如下: 于是先查看其他现场,都是好的:根据报错信息,提示的意思是jsp解析不了. 结合上面2个情况,排除代码问题,应该是现场WAS环境问 ...