【kickstart round F 2020】前两题 python 题解

第一题:
一些人排队取钱,每次可取金额有限,所需金额超过单次可取金额则需重新排队,问出队顺序
暴力遍历test2 会TLE;
按取金额次数排序,得到的即是出队顺序,复杂度就是O(nlogn),可过test2

from collections import deque
import math
T=int(input())
for tt in range(T):
    N,X=[int(s) for s in input().split()]
    withdraw=[int(s) for s in input().split()]
    stack=[]
    for i in range(N):
        withdraw[i]=math.ceil(withdraw[i]/X)
    for i in range(N):
        stack.append([i,withdraw[i]])
    stack.sort(key=lambda x:x[1])
    order=[i[0] for i in stack]
    
    print('Case #{}: '.format(tt+1),end='')
    for i in range(N):
        if i!=N-1:
            print(order[i]+1,end=' ')  
        else:
            print(order[i]+1)

第二题:
每个机器人有指定单次工作时长(达到时长后需返修),但只有特定时间允许机器人工作,求需要deploy机器人多少次

import math
T=int(input())
for tt in range(T):
    N,K=[int(s) for s in input().split()]
    robot=[]
    for nn in range(N):
        s,e=[int(s) for s in input().split()]
        robot.append([s,e])
    robot.sort(key=lambda x:x[0])
    ans=0
    res=0
    for p in robot:
        if p[1]<=res:
            continue
        if p[0]>=res:
            h=math.ceil((p[1]-p[0])/K)
            ans+=h
            res=p[0]+K*h
        else:
            h=math.ceil((p[1]-res)/K)
            ans+=h
            res+=K*h
    print('Case #{}: {}'.format(tt+1,ans)) 
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页