Problem:


There are n processes in a queue. Each process has namei and timei.  The round-robin scheduling handles the processes in order. A round-robin scheduler gives each process a quantum (a time slot) and interrupts the process if it is not completed by then. The process is resumed and moved to the end of the queue, then the scheduler handles the next process in the queue.

For example, we have the following queue with the quantum of 100ms.

A(150) - B(80) - C(200) - D(200)

First, process A is handled for 100ms, then the process is moved to the end of the queue with the remaining time (50ms).

B(80) - C(200) - D(200) - A(50)

Next, process B is handled for 80ms. The process is completed with the time stamp of 180ms and removed from the queue.

C(200) - D(200) - A(50)

Your task is to write a program which simulates the round-robin scheduling. 

Input

n q

name1 time1

name2 time2

...

namen timen

In the first line the number of processes n and the quantum q are given separated by a single space.

In the following n lines, names and times for the n processes are given. namei and timei are separated by a single space.

Output

For each process, prints its name and the time the process finished in order.

Constraints

1 ≤ n ≤ 100000

1 ≤ q ≤ 1000

1 ≤ timei ≤ 50000

1 ≤ length of namei ≤ 10

1 ≤ Sum of timei ≤ 1000000

Sample Input 1

5 100
p1 150
p2 80
p3 200
p4 350
p5 20

Sample Output 1

p2 180
p5 400
p1 450
p3 550
p4 800

Notes

Template in C



Sample input:
5 100
p1 150
p2 80
p3 200
p4 350
p5 20


Sample output:
p2 180
p5 400
p1 450
p3 550
p4 800

Submission status: Accepted

Solution code:
# coding: UTF-8
from collections import deque
queue = deque()
ftime = deque()
n,quantum = map(int,input().split())
for i in range(n):
  name,time = input().split()
  time = int(time)
  queue.append((name,time))

t = 0
while queue:
 name,time = queue.popleft()
 if time <= quantum:
   t += time
   ftime.append(name + " " + str(t))
 else:
   t += quantum
   queue.append((name,time-quantum))

while ftime:
  print(ftime.popleft())

Submission status: Accepted

Solution code:
import collections
calc_time = 0
hoge = collections.deque()
num, q = [int(x) for x in input().split()]
for _ in range(num):
    name, t = input().split()
    hoge.append([name, int(t)])
while hoge:
    name, t = hoge.popleft()
    if t - q > 0:
        hoge.append([name, t-q])
        calc_time += q
    elif t == q:
        calc_time += q
        print ('{} {}'.format(name, calc_time))
    elif t - q < 0:
        calc_time += t
        print ('{} {}'.format(name, calc_time))

Submission status: Accepted

Solution code:
import sys
data = []
for line in sys.stdin:
	data.append(line.split())

quontum = int(data[0][1])
targetData = data[1:]
answerData = []
totalQuontum = 0

while targetData :
	target = targetData.pop(0)

	target[1] =  int(target[1]) - quontum

	if target[1]<= 0:
		totalQuontum += quontum + int(target[1])
		target[1] = totalQuontum
		answerData.append(target)
	else :
		targetData.append(target)
		totalQuontum += quontum

for answer in answerData:
	print(str(answer[0]) + ' ' + str(answer[1]))

Submission status: Accepted

Solution code:
from collections import deque

n,q = map(int,input().split())
qu = deque([])
time = 0
for i in range(n):
    a,b = input().split()
    qu.append([a,int(b)])

while len(qu) > 0:
    c = qu.popleft()
    if c[1] <= q:
        time += c[1]
        print(c[0],time)
    else:
        time += q
        qu.append([c[0],c[1]-q])

Submission status: Accepted

Solution code:
import sys
import collections
s=sys.stdin.readlines()
n,q=map(int,s[0].split())
d=collections.deque(e.split()for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 v=int(v)
 if v>q:
  v-=q
  t+=q
  d.append([k,v])
 else:
  t+=v
  print(k,t)

Submission status: Accepted

Solution code:
import sys
from collections import deque
s=sys.stdin.readlines()
n,q=map(int,s[0].split())
d=deque(e.split()for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 v=int(v)
 if v>q:
  v-=q
  t+=q
  d.append([k,v])
 else:
  t+=v
  print(k,t)

Submission status: Accepted

Solution code:
import sys
from collections import deque
s=sys.stdin.readlines()
_,q=map(int,s[0].split())
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 if v>q:
  v-=q
  t+=q
  d.append([k,v])
 else:
  t+=v
  print(k,t)

Submission status: Accepted

Solution code:
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 if v>q:
  v-=q
  t+=q
  d.append([k,v])
 else:
  t+=v
  print(k,t)

Submission status: Accepted

Solution code:
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 if v>q:v-=q;t+=q;d.append([k,v])
 else:t+=v;print(k,t)

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys

n, quantum = map(int, sys.stdin.readline().split())

proc_q = list()
for i in range(n):
    p_name, length = sys.stdin.readline().split()
    proc_q.append([p_name, int(length)])

elapse = 0
while proc_q:
    process = proc_q.pop(0)  # retrieve 1st element
    p_time = min(quantum, process[1])
    elapse += p_time
    if p_time == process[1]:
        print(process[0], elapse)
    else:
        proc_q.append([process[0], process[1]-quantum])

Submission status: Accepted

Solution code:
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
d=deque(list(e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft();v=int(v)
 if v>q:v-=q;t+=q;d.append([k,v])
 else:t+=v;print(k,t)

Submission status: Accepted

Solution code:
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t,a=0,[]
while d:
 k,v=d.popleft()
 if v>q:v-=q;t+=q;d.append([k,v])
 else:t+=v;a+=[f'{k} {t}']
print(*a,sep='\n')

Submission status: Accepted

Solution code:
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t,a=0,[]
while d:
 k,v=d.popleft()
 if v>q:v-=q;t+=q;d.append([k,v])
 else:t+=v;a+=[f'{k} {t}']
print('\n'.join(a))

Submission status: Accepted

Solution code:
n, q = [int(i) for i in input().split()]

name = []
time = []

for i in range(n):
    x, y = [i for i in input().split()]
    y = int(y)
    name.append(x)
    time.append(y)

from collections import deque
time = deque(time)
name = deque(name)

count = 0
time_out = []
name_out = []

while len(time) > 0:
    a = time.popleft()
    b = name.popleft()
    if a > q:
        a -= q
        time.append(a)
        name.append(b)
        count += q
    else:
        count += a
        time_out.append(count)
        name_out.append(b)

for i, j in zip(name_out, time_out):
    print(i, j)

Submission status: Accepted

Solution code:
import sys
from collections import deque
def m():
 s=sys.stdin.readlines()
 q=int(s[0].split()[1])
 f=lambda x,y:(x,int(y))
 d=deque(f(*e.split())for e in s[1:])
 t,a=0,[]
 while d:
  k,v=d.popleft()
  if v>q:v-=q;t+=q;d.append([k,v])
  else:t+=v;a+=[f'{k} {t}']
 print('\n'.join(a))
if'__main__'==__name__:m()

Submission status: Accepted

Solution code:
n, q = map(int, input().split(' '))
L = []
i = 0
while i < n:
    name, time = map(str, input().split(' '))
    L.append([name, int(time)])
    i += 1
t = 0
fin = []
while len(L) != 0:
    if L[0][1] > q:
        t += q
        name = L[0][0]
        time = L[0][1] - q
        L.append([name, time])
        L.pop(0)
    else:
        t += L[0][1]
        name = L[0][0]
        time = t
        fin.append([name, time])
        L.pop(0)
ans = ''
for f in fin:
    ans += f[0] + ' ' + str(f[1]) + '\n'
print(ans[:-1])

Submission status: Accepted

Solution code:
import queue
q = queue.Queue()
exetime = 0

n, quantum = map(int, input().split())
for i in range(n):
    q.put(tuple(input().split())) # tupleで渡す

while not q.empty():
    process = q.get()
    #print(process)
    time = int(process[1])
    if time > quantum:
        exetime += quantum
        q.put((process[0], time - quantum))
    else:
        exetime += time
        print(process[0], exetime)

Submission status: Accepted

Solution code:
n,q = map(int,input().split())
A = []
B = []
C = []
t = 0

for i in range(n):
    x,y = input().split()
    A.append([x,int(y)])

while len(A)>0:
    f = A[0][1]
    if f<=q:
        t += f
        B.append(A[0][0])
        C.append(t)
    else:
        t += q
        A[0][1] -= q
        A.append(A[0])
    A.pop(0)

for j in range(len(B)):
    print(B[j],C[j])

Submission status: Accepted

Solution code:
import sys
#import numpy as np
import copy

fline = input().rstrip().split(' ')
N, Q = int(fline[0]), int(fline[1])

list = []
for i in range(N):
    tlist = []
    line = input().split(' ')
    tlist = [line[0], line[1]]
    #print(tlist)
    list.append(tlist)

#print(list)
current = 0
while len(list) != 0:
    if int(list[0][1]) <= Q:
        current += int(list[0][1])
        print(list[0][0] + " " + str(current))
        del list[0]
    else:
        list[0][1] = str((int)(list[0][1])-Q)
        list.append(list[0])
        del list[0]
        current += Q
    #print(list)

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
l = []
for i in range(n):
  t = []
  for j in input().split():
    t += [j]
  t[1] = int(t[1])
  l += [t]

c = 0
while len(l) > 0:
  if l[0][1] > q:
    l[0][1] -= q
    l += [l[0]]
    l.pop(0)
    c += q
  else:
    c += l[0][1]
    print(l[0][0], c)
    l.pop(0)

Submission status: Accepted

Solution code:
# ALDS1_3_B
# coding: utf-8

from collections import deque

n, q = [int(i) for i in raw_input().split()]
d = deque()
for i in range(n):
    name, time = raw_input().split()
    time = int(time)
    d.append([name, time])

total = 0
while len(d) > 0:
    p = d.popleft()
    if p[1] <= q: # 終わり
        total += p[1]
        print "{} {}".format(p[0], total)
    else:
        total += q
        p[1] -= q
        d.append(p)

Submission status: Accepted

Solution code:
def parse(l):
    name, time = l.split()
    a = [name, int(time)]
    return a


n, quantum = map(int, input().split())
queue = []
for i in range(n):
    queue.append(parse(input()))
head = 0


def enqueue(x):
    global queue
    queue.append(x)


def dequeue():
    global head
    head += 1


total_time, count = 0, 0
while count < n:
    y = queue[head][1] - quantum
    if y <= 0:
        print(str(queue[head][0]) + " " + str(total_time + queue[head][1]))
        dequeue()
        total_time += y
        count += 1
    else:
        queue[head][1] -= quantum
        enqueue(queue[head])
        dequeue()

    total_time += quantum

Submission status: Accepted

Solution code:
from collections import deque


def run_process(processes, slot):
    """Simulate the round-robin scheduler.
    Returns a list of (process, elapsed_time).

    >>> ps = [('p1', 150), ('p2', 80), ('p3', 200), ('p4', 350), ('p5', 20)]
    >>> run_process(ps, 100)
    [('p2', 180), ('p5', 400), ('p1', 450), ('p3', 550), ('p4', 800)]
    """
    ps = deque(processes)
    tot = 0
    finish = []

    while len(ps) > 0:
        p, t = ps.popleft()
        dt = min(t, slot)
        if dt == t:
            finish.append((p, tot+dt))
        else:
            ps.append((p, t-dt))

        tot += dt

    return finish


def run():
    count, slot = [int(i) for i in input().split()]
    ps = []

    for _ in range(count):
        p, t = input().split()
        ps.append((p, int(t)))

    for p, t in run_process(ps, slot):
        print(p, t)


if __name__ == '__main__':
    run()

Submission status: Accepted

Solution code:
n, q = [int(e) for e in input().split()]
A = []
res = []
for i in range(n):
    l = input().split()
    A.append((l[0], int(l[1])))

idx = 0
tot_t = 0
while A:
    dif = A[0][1] - q
    if dif <= 0:
        tot_t += A[0][1]
        res.append((A[0][0], tot_t))
        A.pop(0)
    else:
        tmp = (A[0][0], dif)
        A.pop(0)
        A.append(tmp)
        tot_t += q

for e in res:
    print(e[0], e[1])

Submission status: Accepted

Solution code:
n, q = [int(a) for a in input().split()]
total_time = 0
queue = []
for i in range(n):
    queue.append([a for a in input().split()])

while len(queue) > 0:
    name = queue[0][0]
    time = int(queue[0][1])
    if time <= q:
        total_time += time
        print(name + " " + str(total_time))
        del queue[0]
    else:
        total_time += q
        time -= q
        queue.append([name, time])
        del queue[0]

Submission status: Accepted

Solution code:
n,q = list(map(int, input().split()))
p = [0]*n
t = [0]*n

for i in range(n):
    p[i],t[i] = list(input().split())

i = 0
sum_t = 0
while 1:
    if i==len(t):
        break
    tmp_t = int(t[i])-q
    if tmp_t<=0:
        sum_t += int(t[i])
        print(p[i],sum_t)
    else:
        sum_t += q
        p.append(p[i])
        t.append(tmp_t)        
    i+=1

Submission status: Accepted

Solution code:
n,q=map(int,input().split())
Q=[]
sum=0
for i in range(n):
    tmp=input().split()
    tmp[1]=int(tmp[1])
    Q.append(tmp[0])
    Q.append(tmp[1])

loop=0
while loop==0:
    for i in range(len(Q)//2):
        tmp=[Q[0],Q[1]]
        if tmp[1]>q:
            sum+=q
            Q.append(tmp[0])
            Q.append(tmp[1]-q)
        else:
            sum+=tmp[1]
            print(tmp[0],sum)
        Q.pop(0)
        Q.pop(0)
    if len(Q)==0:
        break

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
l = []
for i in range(n):
  t = []
  for j in input().split():
    t += [j]
  t[1] = int(t[1])
  l += [t]
 
c = 0
while len(l) > 0:
  if l[0][1] > q:
    l[0][1] -= q
    l += [l[0]]
    l.pop(0)
    c += q
  else:
    c += l[0][1]
    print(l[0][0], c)
    l.pop(0)

Submission status: Accepted

Solution code:
n, q = map(int,input().split())
l = []
for i in range(n):
  t=[]
  for j in input().split():
    t+=[j]
  t[1]=int(t[1])
  l+=[t]
 
c=0
while len(l)>0:
  if l[0][1]>q:
    l[0][1]-=q
    l+=[l[0]]
    l.pop(0)
    c+=q
  else:
    c+=l[0][1]
    print(l[0][0],c)
    l.pop(0)

Submission status: Accepted

Solution code:
sum=0
s=0
A=input().split()
n=(int)(A[0])
t=(int)(A[1])
x=[0 for i in range(2*n)]
for i in range(n):
    B=input().split()
    k=2*i
    x[k]=(B[0])
    x[k+1]=(int)(B[1])
while len(x)>0:
    s=x[1]-t
    if s>0: 
        sum+=t
        x.append(x[0])
        x.append(s)
        del x[0]
        del x[0]
    if s<=0:
        sum+=x[1]
        print( x[0],sum)
        del x[0]
        del x[0]

Submission status: Accepted

Solution code:
#! python3
# queue.py

from collections import deque

n, q = [int(x) for x in input().split(' ')]

queue = deque([])
for i in range(n):
    inp = input().split(' ')
    queue.append([inp[0], int(inp[1])])

elapsed_time = 0
while True:
    proc = queue.popleft()
    if proc[1] - q <= 0:
        elapsed_time += proc[1]
        print(proc[0], elapsed_time)
    else:
        elapsed_time += q
        queue.append([proc[0], proc[1] - q])
    if len(queue) == 0:
        break

Submission status: Accepted

Solution code:
n, p = map(int, input().split())
ps = []
for i in range(n):
    ps.append(input().split())
    ps[i][1] = int(ps[i][1])
t = 0
while len(ps) > 0:
    if ps[0][1] > p:
        ps.append([ps[0][0], ps[0][1] - p])
        t += p
        ps.pop(0)
    else:
        t += ps[0][1]
        print("{0} {1}".format(ps[0][0], t))
        ps.pop(0)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 29 14:42:43 2018

@author: maezawa
"""

def print_array(g):
    ans = str(g[0])
    if len(g) > 1:
        for i in range(1,len(g)):
            ans += ' '+str(g[i]) 
    print(ans)

name=[]
time=[]
current_time = 0

n, q = list(map(int, input().split()))
for i in range(n):
    s = input().split()
    name.append(s[0])
    time.append(int(s[1]))
# =============================================================================
# print(n,q)
# print(name)
# print(time)
# =============================================================================
finished = []
fin_time = []
i=0
remain = n
while True:
    if time[i] <= q:
        current_time += time.pop(i)
        finished.append(name.pop(i))
        fin_time.append(current_time)      
        remain -= 1
        if remain == 0:
            break
        i = i%remain
    else:
        time[i] -= q
        current_time += q
        i = (i+1)%remain

for i in range(n):
    print("{} {}".format(finished[i], fin_time[i]))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 29 18:04:22 2018
ALDS1_3b_r リングバッファによる実装
@author: maezawa
"""

def fifo_enque(data):
    global tail
    global fifo
    fifo[tail] = data
    tail = (tail+1)%fifo_size
    
def fifo_deque():
    global head
    global fifo
    data = fifo[head]
    head = (head+1)%fifo_size
    return data

fifo_size = 100000
fifo = [0 for _ in range(fifo_size)]
head = 0
tail = 0

n, q = list(map(int, input().split()))
for i in range(n):
    s = input().split()
    data = [s[0], int(s[1])]
    fifo_enque(data)
    
current_time = 0
finished = []
fin_time = []

while True:
    data = fifo_deque()
    if data[1] > q:
        current_time += q
        data[1] -= q
        fifo_enque(data)
    else:
        current_time += data[1]
        finished.append(data[0])
        fin_time.append(current_time)
    if head == tail:
        break

for i in range(n):
    print("{} {}".format(finished[i], fin_time[i]))

Submission status: Accepted

Solution code:
a=input().split()
a=[int(i) for i in a]
b=[]
cnt=0
time=0
for i in range(a[0]):
  b.append(input().split())
  cnt+=int(b[i][1])
i=0
while cnt!=time:
  if int(b[i][1])<=a[1]:
    time+=int(b[i][1])
    print(b[i][0],time)
    b.pop(0)
  else: 
    b[i][1]=int(b[i][1])-a[1]
    time+=a[1]
    b.append(b.pop(i))

Submission status: Accepted

Solution code:
s=input()
d=s.split()
n=int(d[0])
q=int(d[1])
end_t=0
processes=[]
for i in range(n):
    processes.append([])
    s=input()
    d=s.split()
    processes[i].append(d[0])
    processes[i].append(int(d[1]))

cnt=0
i=0
while True:
    if processes[0][1]<=q:
       end_t+=processes[0][1]
       print(processes[0][0],end_t)
       processes.pop(0)
       cnt+=1
       if cnt==n:
           break
   
    else:
       processes[0][1]-=q
       end_t+=q
       processes.append(processes[0])
       processes.pop(0)
       i+=1

Submission status: Accepted

Solution code:
a = input().split()
n = int(a[0])
q = int(a[1])

N = [] #プロセス名
T1 = [] #必要時間


for i in range(n):
    a = input().split()
    N.append(a[0])
    T1.append(int(a[1]))

t = 0 #経過時間
f = 0 #処理対象
l = len(N)-1 #最後尾

while len(N) > 0:
    if T1[f] <= q: #処理が完了する
        t += T1[f]
        if f == len(N)-1:
            print(N.pop(f), t)
            del T1[f]
            f = 0
        elif f == 0:
            print(N.pop(f), t)
            del T1[f]
            l -= 1
        else:
            print(N.pop(f), t)
            del T1[f]
        
        if len(N) == 0: break

    else: #処理が完了しない
        t += q
        T1[f] -= q

        i = N[f]
        j = T1[f]

        if f+1 == len(N): f = 0
        else: f += 1
        if l+1 == len(N): l = 0
        else: l += 1
        
        N[l] = i
        T1[l] = j

Submission status: Accepted

Solution code:
n,q = map(int, input().split())
name = []
time = []
for i in range(n):
    x = input().split()
    name.append(x[0])
    time.append(int(x[1]))

t = 0
n_end = []
t_end = []
while(len(time) > 0):
    if time[0] <= q:
        t += time[0]
        n_end.append(name.pop(0))
        time.pop(0)
        t_end.append(t)
    else :
        t += q
        time[0] -= q
        a = time.pop(0)
        b = name.pop(0)
        time.append(a)
        name.append(b)

for i in range(len(n_end)):
    print(n_end[i],t_end[i])

Submission status: Accepted

Solution code:
#ALDS_3_B 16D8103010K Ko Okasaki

from collections import deque

n,q=map(int,input().split())
que=deque()
for i in range(n):
   name,time=input().split()
   time=int(time)
   que.append([name,time])

t=0

while len(que)>0:
    que_t=que.popleft()
    if que_t[1]>q:
        que_t[1]-=q
        t+=q
        que.append(que_t)
    else:
        t+=que_t[1]
        print(que_t[0],t)

Submission status: Accepted

Solution code:
n,q = map(int,input().split())
process = []
for i in range(n):
    name,time = input().split()
    process.append([name,int(time)])
elap = 0
out = []
while len(process) > 0:
    name,time = process.pop(0)
    temp_q = q
    while True:
        if time <= 0:
            out.append([name,elap])
            break
        if temp_q <= 0:
            process.append([name,time])
            break
        time -= 1
        temp_q -= 1
        elap += 1
for name,time in out:
    print(name,time)

Submission status: Accepted

Solution code:
from collections import deque as D

n, q = map(int, input().split())
dq = D()
for i in range(n):
    p, t = map(str, input().split())
    dq.append([p, int(t)])
total = 0


while dq:
    pt = dq.popleft()
    if pt[1]<=q:
        total += pt[1]
        print(pt[0], total)
    else:
        total += q
        dq.append([pt[0],pt[1]-q])

Submission status: Accepted

Solution code:
sum=0
s=0
A=input().split()
n=(int)(A[0])
t=(int)(A[1])
x=[0 for i in range(2*n)]
for i in range(n):
    B=input().split()
    k=2*i
    x[k]=(B[0])
    x[k+1]=(int)(B[1])
while len(x)>0:
    s=x[1]-t
    if s>0: 
        sum+=t
        x.append(x[0])
        x.append(s)
        del x[0]
        del x[0]
    if s<=0:
        sum+=x[1]
        print( x[0],sum)
        del x[0]
        del x[0]

Submission status: Accepted

Solution code:
a=input().split()
n=(int)(a[0])
q=(int)(a[1])
b=[0 for i in range(2*n)]
cnt=0
tmp=0
for i in range(n):
    B=input().split()
    b[2*i]=B[0]
    b[2*i+1]=int(B[1])
while len(b)>0:
    tmp=b[1]-q
    if tmp>0:
        b.append(b[0])
        b.append(tmp)
        del b[0]
        del b[0]
        cnt=cnt+q
    else:
        cnt=cnt+b[1]
        print(b[0],cnt)
        del b[0]
        del b[0]

Submission status: Accepted

Solution code:
from queue import Queue

n,q =map(int,input().split()) ##n:プロセス数、q:クオンタム

qu=Queue()  ##意味を調べる

for i in range(n):
    name,t=input().split()
    qu.put((name,int(t))) ##Queueのput?
    

cnt=0

while qu.qsize()>0: ##qsize?
    name,t=qu.get() 
    cnt+=min(q,t)  ##処理時間と残り時間の小さい方をたす
    if q< t:
        qu.put((name,t-q))  ##残り時間から処理時間を引いて、後ろに回す
    else:
        print(name,cnt)

Submission status: Accepted

Solution code:
n,q=list(map(int,input().split()))
name=[]
time=[]
sum=0
for i in range(n):

	na,ti=input().split()
	name.append(na)
	time.append(int(ti))
	
	
while len(name)>0:
	na=name.pop(0)
	ti=time.pop(0)
	
	if ti>q:
		sum+=q
		name.append(na)
		time.append(ti-q)
		
	else:
		sum+=ti
		print(na, sum)

Submission status: Accepted

Solution code:
class Queue():
    def __init__(self, size=10):
        self.queue = [None] * size
        self.head = self.tail = 0
        self.MAX = size
        self.num_items = 0

    def is_empty(self):
        return self.num_items == 0

    def is_full(self):
        return self.num_items == self.MAX

    def enqueue(self, x):
        if self.is_full():
            raise IndexError

        self.queue[self.tail] = x
        self.num_items += 1
        if self.tail+1 == self.MAX:
            self.tail = 0
        else:
            self.tail += 1

    def dequeue(self):
        if self.is_empty():
            raise IndexError

        x = self.queue[self.head]
        self.num_items -= 1
        if self.head+1 == self.MAX:
            self.head = 0
        else:
            self.head += 1

        return x


class Process:
    def __init__(self, name, time):
        self.name = name
        self.time = time

    def print_time(self):
        return self.time

    def print_name(self):
        return self.name


def main():
    n, q = map(int, input().split())
    qq = Queue(n)

    t = 0
    for i in range(n):
        name, time = input().split()
        p = Process(name, int(time))
        qq.enqueue(p)

    while not qq.is_empty():
        p = qq.dequeue()

        if p.time - q <= 0:
            t += p.time
            print("{} {}".format(p.name, t))
        else:
            p.time -= q
            t += q
            qq.enqueue(p)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from queue import deque

def main():
    n, q = tuple(map(int, input().split(' ')))

    task_q = deque()
    for idx in range(n):
        l = input().split(' ')
        task_q.append((l[0], int(l[1])))

    tot_time = 0
    while len(task_q) != 0:

        task = task_q.popleft()
        if task[1] > q:
            task_q.append((task[0], task[1] - q))
            tot_time += q

        else:
            tot_time += task[1]
            print(task[0], tot_time)


main()

Submission status: Accepted

Solution code:
class Data:
    def __init__(self):
        self.name = ""
        self.time = -1
    def printdata(self,sum):
        print(self.name,sum)
    def datain(self,name,time):
        self.name = name
        self.time = int(time)

def enqueue(Q,x):
    global tail
    Q[tail] = x
    if(tail+1 == n+1):
        tail = 0
    else:
        tail += 1

def dequeue():
    global head
    if(head+1 == n+1):
        head = 0
    else:
        head += 1

n,q = map(int,input().split())
A = [Data()]
head = 0
tail = n
sum = 0

for i in range(n):
    d = list(map(str,input().split()))
    A.append(Data())
    A[i].datain(d[0],d[1])
A.append(Data()) #要素数1追加

while(head != tail):
    if(A[head].time - q > 0):
        A[head].time -= q
        enqueue(A,A[head])
        dequeue()
        sum += q
    else:
        sum += A[head].time
        A[head].printdata(sum)
        dequeue()

Submission status: Accepted

Solution code:

sum=0
s=0
A=input().split()
n=(int)(A[0])
m=(int)(A[1])
x=[0 for i in range(2*n)]
for i in range(n):
    B=input().split()
    k=2*i
    x[k]=(B[0])
    x[k+1]=(int)(B[1])
while len(x)>0:
    s=x[1]-m
    if s>0:
        sum+=m
        x.append(x[0])
        x.append(s)
        del x[0]
        del x[0]
    if s<=0:
        sum+=x[1]
        print( x[0],sum)
        del x[0]
        del x[0]

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
input_list = []
for i in range(n):
    name, time = input().split()
    input_list.append([str(name), int(time)])
queue_head = 0
now_time = 0
while queue_head != len(input_list):
    if q < input_list[queue_head][1]:
        input_list.append([input_list[queue_head][0], input_list[queue_head][1] - q])
        queue_head += 1
        now_time += q
    else:
        now_time += input_list[queue_head][1]
        print(input_list[queue_head][0], now_time)
        queue_head += 1

Submission status: Accepted

Solution code:
n,q = map(int, input().split())
a = []
t = 0
for i in range(n):
    a.append(input().split())
 
while len(a) != 0:
    proc =  a.pop(0)
    if int(proc[1]) <= q:
        t += int(proc[1])
        print(proc[0],t)
    else:
        t += q
        proc[1] = int(proc[1]) - q
        a.append(proc)

Submission status: Accepted

Solution code:
n,q = map(int, input().split())
a = []
t = 0
for i in range(n):
    a.append(input().split())
 
while len(a) != 0:
    A =  a.pop(0)
    if int(A[1]) <= q:
        t += int(A[1])
        print(A[0],t)
    else:
        t += q
        A[1] = int(A[1]) - q
        a.append(A)

Submission status: Accepted

Solution code:
a,b=map(int,input().split())
list=[[0 for i in range(2)] for j in range(a)]
for i in range(a):
    list[i]=input().split()
n=0
s=0
while n!=a:
    if int(list[n][1])>b:
        list[n][1]=int(list[n][1])-b
        list.append(list[n])
        list.pop(n)
        s+=b
    else:
        print(list[n][0],s+int(list[n][1]))
        s+=int(list[n][1])
        n+=1

Submission status: Accepted

Solution code:
#16D8101014F 久留米 竜之介 Kurume Ryunosuke Python
All = 0
q = []
Queue = []
tmp,time = map(int,input().split())
for i in range(tmp):
    p,zi =input().split()
    q.append([p,int(zi)])
while len(q) > 0:
    if q[0][1]<= time:
        All+=q[0][1]
        v=q.pop(0)
        Queue.append([v[0],All])
    else:
        All+=time
        q[0][1]-=time
        last=q.pop(0)
        q.append(last)
for i in range(len(Queue)):
    print("{0} {1}".format(Queue[i][0],Queue[i][1]))

Submission status: Accepted

Solution code:
n,q=map(int, input().split())
que=[]
t=0
for i in range(n):
    que.append(input().split())
 
while len(que)!=0:
    proc=que.pop(0)
    if int(proc[1]) <= q:
        t += int(proc[1])
        print(proc[0],t)
    else:
        t += q
        proc[1]=int(proc[1]) - q
        que.append(proc)

Submission status: Accepted

Solution code:
n,q = map(int,raw_input().split(" "))
task = [raw_input().split(" ") for i in range(n)]
c= 0
while task:
    name,time = task.pop(0)
    e = int(time) - q
    if e > 0:
        c+= q
        task.append([name,e])
    else:
        c+= int(time)
        print name+" "+str(c)

Submission status: Accepted

Solution code:
from collections import namedtuple

Task = namedtuple('Task', 'name time')

class Queue:
    def __init__(self, n):
        self.n = n
        self._l = [None for _ in range(self.n + 1)]
        self._head = 0
        self._tail = 0

    def enqueue(self, x):
        self._l[self._tail] = x
        self._tail += 1
        if self._tail > self.n:
            self._tail -= self.n

    def dequeue(self):
        if self.isEmpty():
            raise IndexError("pop from empty queue")
        else:
            e = self._l[self._head]
            self._l[self._head] = None
            self._head += 1
            if self._head > self.n:
                self._head -= self.n
            return e

    def isEmpty(self):
        return self._head == self._tail

    def isFull(self):
        return self._tail == self.n

if __name__ == '__main__':
    n, q = map(int, input().split())
    queue = Queue(n+1)
    for _ in range(n):
        name, time = input().split()
        time = int(time)
        queue.enqueue(Task(name=name, time=time))

    now = 0
    while not queue.isEmpty():
        task = queue.dequeue()
        t = task.time
        if t <= q:
            now += t
            print(task.name, now)
        else:
            now += q
            queue.enqueue(Task(task.name, t - q))

Submission status: Accepted

Solution code:
from collections import namedtuple

Task = namedtuple('Task', 'name time')

class Queue:
    def __init__(self, n):
        self.n = n
        self._l = [None for _ in range(self.n + 1)]
        self._head = 0
        self._tail = 0

    def enqueue(self, x):
        self._l[self._tail] = x
        self._tail += 1
        if self._tail > self.n:
            self._tail = 0

    def dequeue(self):
        if self.isEmpty():
            raise IndexError("pop from empty queue")
        else:
            e = self._l[self._head]
            self._l[self._head] = None
            self._head += 1
            if self._head > self.n:
                self._head = 0
            return e

    def isEmpty(self):
        return self._head == self._tail

    def isFull(self):
        return self._tail == self.n

if __name__ == '__main__':
    n, q = map(int, input().split())
    queue = Queue(n)
    for _ in range(n):
        name, time = input().split()
        time = int(time)
        queue.enqueue(Task(name=name, time=time))

    now = 0
    while not queue.isEmpty():
        task = queue.dequeue()
        t = task.time
        if t <= q:
            now += t
            print(task.name, now)
        else:
            now += q
            queue.enqueue(Task(task.name, t - q))