Problem:


ゼッケンの交換 (Swapping Bibs)

  問題

JOI 高校の N 人の生徒が東西に一列に並んでいる．列の西の端から i 番目の生徒が生徒 i である．それぞれの生徒は整数が 1 つ書かれたゼッケンを付けている．最初，生徒 i のゼッケンには整数 Ai が書かれている．

バトンが M 個あり，バトンには 1 から M までの番号が付けられている．k = 1, 2, ..., M に対し，以下の操作を行う．バトン k (2 ≦ k ≦ M) に関する操作は，バトン k - 1 に関する操作が終わってから行う．

先生がバトン k を生徒 1 に渡す．

バトンを受け取った生徒は，以下のルールに従ってバトンを渡す．

ルール：生徒 i がバトン k を受け取ったとする．

 1 ≦ i ≦ N - 1 のとき: 生徒 i のゼッケンの整数を k で割った余りが，生徒 i + 1 のゼッケンの整数を k で割った余りよりも大きいとき，生徒 i と生徒 i + 1 がゼッケンを交換し，生徒 i は生徒 i + 1 にバトンを渡す．そうでないときは，ゼッケンを交換せずに，生徒 i は生徒 i + 1 にバトンを渡す．

 i = N のとき: 生徒 N はバトンを先生に渡す．

先生が生徒 N からバトン k を受け取ったら，バトン k に関する操作は終わりである．

生徒のゼッケンに最初に書かれていた整数とバトンの個数 M が与えられたとき，先生が生徒 N からバトン M を受け取った後の，それぞれの生徒のゼッケンの整数を求めるプログラムを作成せよ．

  入力

入力は 1 + N 行からなる．

1 行目には整数 N, M (1 ≦ N ≦ 100, 1 ≦ M ≦ 100) が空白を区切りとして書かれており，それぞれ生徒の人数とバトンの個数を表す．

続く N 行のうちの i 行目 (1 ≦ i ≦ N) には整数 Ai (1 ≦ Ai ≦ 1000) が書かれており，生徒 i のゼッケンに最初に書かれている整数 Ai を表す．

  出力

出力は N 行からなる．i 行目 (1 ≦ i ≦ N) には，先生が生徒 N からバトン M を受け取った後の，生徒 i のゼッケンの整数を出力せよ．

  入出力例

 
入力例 1

  

6 4
3
2
8
3
1
5

 
出力例 1

   

2
3
1
8
5
3

 
入力例 2

  

10 6
1
2
3
4
5
6
7
8
9
10

 
出力例 2

  

6
1
2
3
10
4
8
7
9
5

入出力例 1 では 6 人の生徒がいる．最初，生徒のゼッケンは順に 3， 2， 8， 3， 1， 5 である．バトンは 4 個ある．

バトン 1 に関する操作が終了した時点での生徒のゼッケンは順に 3， 2， 8， 3， 1， 5 である．

バトン 2 に関する操作が終了した時点での生徒のゼッケンは順に 2， 8， 3， 3， 1， 5 である．

バトン 3 に関する操作が終了した時点での生徒のゼッケンは順に 2， 3， 3， 1， 8， 5 である．

バトン 4 に関する操作が終了した時点での生徒のゼッケンは順に 2， 3， 1， 8， 5， 3 である．

情報オリンピック日本委員会作 『第 15 回日本情報オリンピック JOI 2015/2016 予選競技課題』



Sample input:
6 4
3
2
8
3
1
5


Sample output:
2
3
1
8
5
3

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
d={}
for i in range(n):
    d[i+1] = int(input())
print(2 in d)    
for l in range(m):
    k = l+1
    for j in range(n):
        i = j+1
        if i == n:
            break
        if d[i]%k > d[i+1]%k:
            t = d[i]
            d[i] = d[i+1]
            d[i+1] = t
for i in range(n):
    print(d[i+1])

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
alst = [int(input()) for _ in range(n)]
for k in range(2, m + 1):
  for j in range(n - 1):
    if alst[j] % k > alst[j + 1] % k:
      alst[j], alst[j + 1] = alst[j + 1], alst[j]

print(*alst, sep="\n")

Submission status: Accepted

Solution code:
# coding: utf-8

N, M = list(map(int, input().split(' ')))
A = [int(input()) for i in range(N)]

for k in range(1, M+1):
    i = 1
    while i != N:
        if A[i-1]%k > A[i]%k:
            A[i-1], A[i] = A[i], A[i-1]
        i += 1

for a in A: print(a)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
d={}
for i in range(n):
    d[i+1] = int(input())
for l in range(m):
    k = l+1
    for j in range(n):
        i = j+1
        if i == n:
            break
        if d[i]%k > d[i+1]%k:
            t = d[i]
            d[i] = d[i+1]
            d[i+1] = t
for i in range(n):
    print(d[i+1])

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
a=[int(input()) for _ in range(n)]
for i in range(1,m+1):
    for j in range(n-1):
        if a[j]%i>a[j+1]%i:a[j],a[j+1]=a[j+1],a[j]
for i in a:print(i)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
bibs = [int(input()) for _ in range(N)]
btns = [k+1 for k in range(M)]
for k in btns:
    for i in range(N-1):
        if bibs[i]%k > bibs[i+1]%k:
            bibs[i], bibs[i+1] = bibs[i+1], bibs[i]
print(*bibs, sep='\n')

Submission status: Accepted

Solution code:
n,m=[int(s)for s in input().split(" ")]
bibs=[int(input())for i in range(n)]
for k in range(1,m+1):
    for i in range(n-1):
        if bibs[i]%k>bibs[i+1]%k:(bibs[i],bibs[i+1])=(bibs[i+1],bibs[i])
for bib in bibs:print(bib)

Submission status: Accepted

Solution code:
a = input()
columns = a.split(' ')          #??\??????????????????????????§??????
num_of_student = int(columns[0])   #???????????°
Baton = int(columns[1])            #??????????????°

student_bib = []
for i in range(num_of_student):
    student_bib.append(int(input()))   #???????????????????????\???

for Baton_num in range(Baton + 1):   #??????????????????
    if Baton_num == 0:          #????????????????????????
        continue
    m = 0
    for j in range(num_of_student):     #???????????°?????????????????????
        if num_of_student - 1 == m:
            break
        if (student_bib[m] % Baton_num) > (student_bib[m + 1] % Baton_num):
            student_bib_tmp = student_bib[m]
            student_bib[m] = student_bib[m + 1]
            student_bib[m + 1] = student_bib_tmp  #??????????????????
        m += 1


for student_bib_ in student_bib:
    print(student_bib_)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
s = [0 for _ in range(n + 1)]
for i in range(n):
    s[i + 1] = int(input())
for k in range(2, m + 1):
    for i in range(1, n):
        if s[i] % k > s[i + 1] % k:
            s[i], s[i + 1] = s[i + 1], s[i]
for i in s[1:]:
    print(i)

Submission status: Accepted

Solution code:
# AOJ 0620: Swapping Bibs
# Python3 2018.7.2 bal4u

n, m = map(int, input().split())
a = [int(input()) for i in range(n)]
for k in range(1, m+1):
	for i in range(n-1):
		if a[i]%k > a[i+1]%k: a[i], a[i+1] = a[i+1], a[i]
print(*a, sep='\n')

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
a = [int(input()) for i in range(n)]

for k in range(m):
    for i in range(n-1):
        if a[i] % (k+1) > a[i+1] % (k+1):
            a[i],a[i+1] = a[i+1],a[i]

for i in range(n):
    print(a[i])

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
s=[]
for i in range(n):
    s.append(int(input()))
for i in range(2,m+1):
    for j in range(n-1):
        a=s[j]%i
        b=s[j+1]%i
        if a>b:
            s[j],s[j+1]=s[j+1],s[j]
for i in range(n):
    print(s[i])

Submission status: Accepted

Solution code:
def swappingBibs(a, k):
	for i in range(0, len(a)-1):
		if a[i]%k>a[i+1]%k:
			a[i], a[i+1] = a[i+1], a[i]
	return a


N, M = map(int, input().split())
A = [int(input()) for _ in range(N)]
for m in range(1, M+1):
	A = swappingBibs(A, m)

for j in A: print(j)

Submission status: Accepted

Solution code:
def swap(i,j):
  X=A[i]
  A[i]=A[j]
  A[j]=X

N,M=map(int,input().split())
A=list()
for i in range(N):
  A.append(int(input()))
for i in range(M):
  for j in range(N-1):
    if A[j]%(i+1)>A[j+1]%(i+1):
      swap(j,j+1)
for i in range(N):
  print(A[i])

Submission status: Accepted

Solution code:
#B
N,M = map(int,input().split())
A = [int(input()) for i in range(N)]

for k in range(1,M+1):
    for i in range(N-1):
        if A[i]%k > A[i+1]%k:
            A[i],A[i+1] = A[i+1],A[i]
            
for a in A:
    print(a)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
a = [int(input())for i in range(n)]

for k in range(1, m+1):
    for i in range(1, n):
        if a[i-1]%k>a[i]%k:
            a[i-1], a[i] = a[i], a[i-1]
for i in a: print(i)

Submission status: Accepted

Solution code:
def main():
	n,m = map(int,input().split())
	a = []
	for i in range(n):
		a += [int(input())]
	for i in range(1,m+1):
		for j in range(n-1):
			if a[j] % i > a[j+1] % i:
				a[j], a[j+1] = a[j+1], a[j]
	print("\n".join(map(str,a)))

main()

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
a = []
for i in range(n):
    a.append(int(input()))

for k in range(m):
    for i in range(n-1):
        if a[i] % (k+1) > a[i+1] % (k+1):
            tmp = a[i]
            a[i] = a[i+1]
            a[i+1] = tmp
for i in range(n):
    print(a[i])

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
a=[int(input()) for _ in range(n)]
baton=0 #initial index of baton
for k in range(1,m+1):
    for i in range(n):
        if i==n-1:
            continue
        elif a[i]%k>a[i+1]%k:
            v=a[i+1]
            a[i+1]=a[i]
            a[i]=v
for i in a:
    print(i)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
A = [0] * N
for i in range(N):
    A[i] = int(input())

for i in range(1, M+1):
    if i == 1:
        continue
    else:
        for j in range(N-1):
            if (A[j] % i) > (A[j+1] % i):
                tmp_val = A[j]
                A[j] = A[j+1]
                A[j+1] = tmp_val

for i in range(N):
    print(A[i])

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
a = [int(input()) for i in range(n)]
for k in range(1, m+1):
    for i in range(n-1):
        if a[i]%k > a[i+1]%k: a[i], a[i+1] = a[i+1], a[i]
print(*a, sep='\n')

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
stu=[]
for i in range(n):
    stu.append(int(input()))
for i in range(2,m+1):
    for j in range(n-1):
        if stu[j]%i>stu[j+1]%i:stu[j],stu[j+1]=stu[j+1],stu[j]
for i in stu:print(i)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
student = []
for _ in range(n):
    a = int(input())
    student.append(a)

for k in range(1, m+1):
    for i in range(n-1):
        if student[i] % k > student[i+1] % k:
            student[i], student[i+1] = student[i+1], student[i]

for s in student:
    print(s)

Submission status: Accepted

Solution code:
N,M = map(int, input().split())
zekken = []
for i in range(N):
    zekken.append(int(input()))
def sousa(zekken,k):
    for i in range(N-1):
        if (zekken[i]%k>zekken[i+1]%k):
            zekken[i],zekken[i+1] = zekken[i+1],zekken[i]
    return zekken
for k in range(1,M+1):
    zekken = sousa(zekken,k)
for i in range(N):
    print(zekken[i])

Submission status: Runtime Error

Solution code:
n,k = map(int,input().split())
d={}
for i in range(n):
    d[i+1] = int(input())
for i in range(n):
    j = i+1
    if j == n:
        break
    if d[i]%k > d[i+1]%k:
        t = d[i]
        d[i] = d[i+1]
        d[i+1] = t
for i in range(n):
    print(d[i+1])