fork download
  1. #include <stdio.h>
  2.  
  3. #define MAX_N 1000
  4.  
  5. int A[MAX_N], B[MAX_N], pos[MAX_N + 1];
  6.  
  7. int main(){
  8. int N, M;
  9. if (scanf("%d %d", &N, &M) != 2) return 0;
  10.  
  11. // 初期化:1..N 並べ,pos[x]=そのインデックス
  12. for (int i = 0; i < N; i++) {
  13. A[i] = i + 1;
  14. pos[i+1] = i;
  15. }
  16.  
  17. // M 回の操作(値 q の要素を回転)
  18. for (int k = 0; k < M; k++) {
  19. int q;
  20. scanf("%d", &q);
  21. int idx = pos[q];
  22. int t = 0;
  23.  
  24. // Y 部分:idx+1..N-1
  25. for (int i = idx + 1; i < N; i++) {
  26. B[t] = A[i];
  27. pos[A[i]] = t++;
  28. }
  29.  
  30. // 真ん中の q
  31. B[t] = A[idx];
  32. pos[A[idx]]= t++;
  33.  
  34. // X 部分:0..idx-1
  35. for (int i = 0; i < idx; i++) {
  36. B[t] = A[i];
  37. pos[A[i]] = t++;
  38. }
  39.  
  40. // B→A へコピー
  41. for (int i = 0; i < N; i++) {
  42. A[i] = B[i];
  43. }
  44. }
  45.  
  46.  
  47. for (int i = 0; i < N; i++) {
  48. printf("%d", A[i]);
  49. }
  50. putchar('\n');
  51. return 0;
  52. }
Success #stdin #stdout 0s 5320KB
stdin
8 3
2 4 8
stdout
21438567