首页 > 代码库 > Educational Codeforces Round 24 B
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 = 10010; 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=1;i<=b;i++){ cin>>A[i]; } for(int i=1;i<=a;i++){ B[i]=-1; } long long ans=A[1]; for(int i=2;i<=b;i++){ if(A[i]>ans){ long long temp=A[i]-ans; if(B[ans]!=-1&&B[ans]!=temp){ cout<<"-1"; return 0; } B[ans]=temp; }else{ long long num=a+A[i]; if(B[ans]!=-1&&B[ans]!=num-ans){ cout<<"-1"; return 0; } B[ans]=num-ans; } ans=A[i]; } for(int i=1;i<=a;i++){ if(Q[B[i]]>1||B[i]>a){ cout<<"-1"; return 0; }else if(B[i]!=-1){ Q[B[i]]++; } } for(int i=1;i<=a;i++){ if(B[i]==-1){ long long str=1; while(Q[str]) str++; Q[str]++; B[i]=str; } } for(int i=1;i<=a;i++){ if(Q[B[i]]>1||B[i]>a){ cout<<"-1"; return 0; }else if(B[i]!=-1){ Q[B[i]]++; } } for(int i=1;i<=a;i++){ cout<<B[i]<<" "; } return 0; }
Educational Codeforces Round 24 B