PAT1 1026 Table Tennis

题目链接
我的github
这题有毒,相同的做法。python总有三个测试点是答案错误,但是C++就可以AC
python:

在这里插入图片描述

import functools
import decimal

decimal.getcontext().rounding = "ROUND_HALF_UP"


class player:
    def __init__(self, arrive, time, isVIP):
        self.arrive = arrive
        self.time = time
        self.isVIP = isVIP
        self.start = -1

    def toString(self):
        a=decimal.Decimal(str(self.start))
        b=decimal.Decimal(str(self.arrive))
        t = decimal.Decimal((a-b)/60)
        rt = decimal.Decimal(str(t)).quantize(decimal.Decimal("0"))
        return '%02d:%02d:%02d %02d:%02d:%02d %.0f' % \
               (self.arrive // 3600, self.arrive % 3600 // 60, self.arrive % 60,
                self.start // 3500, self.start % 3600 // 60, self.start % 60,
                rt)


class table:
    def __init__(self):
        self.end = 8 * 3600
        self.num = 0
        self.isVIP = False

    def toString(self):
        return '%d %d %s' % (self.end, self.num, str(self.isVIP))


players = list()
tables = list()


def playerToTable(playerId, tableId):
    global players, tables
    players[playerId].start = max(players[playerId].arrive, tables[tableId].end)
    tables[tableId].end = players[playerId].start + players[playerId].time
    tables[tableId].num += 1


def findVip(VIPId):
    VIPId += 1
    while VIPId < len(players) and not players[VIPId].isVIP:
        VIPId += 1
    return VIPId


def cmpArrive(a, b):
    return -1 if a.arrive < b.arrive else (1 if a.arrive != b.arrive else 0)


def cmpStart(a, b):
    return -1 if a.start < b.start else (1 if a.start != b.start else 0)


def solve():
    global players, tables
    n = eval(input())
    for i in range(n):
        t, time, isVIP = input().split()
        hour, minute, second = t.split(':')
        time = int(time)
        time = min(time * 60, 2 * 3600)
        isVIP = int(isVIP)
        arrive = int(hour) * 3600 + int(minute) * 60 + int(second)
        if arrive >= 21 * 3600:
            continue
        tmpPlayer = player(arrive, time, True if isVIP == 1 else False)
        tmpPlayer.start = 21 * 3600
        players.append(tmpPlayer)
    k, m = map(int, input().split())
    for i in range(k + 1):
        tables.append(table())
    for i in range(m):
        VIPTable = eval(input())
        tables[VIPTable].isVIP = True
    players.sort(key=functools.cmp_to_key(cmpArrive))
    mark, VIPId = 0, -1
    VIPId = findVip(VIPId)
    while mark < len(players):
        index, minEnd = -1, 999999999
        for i in range(1, k + 1):
            if tables[i].end < minEnd:
                minEnd = tables[i].end
                index = i
        if tables[index].end >= 21 * 3600:
            break
        if players[mark].isVIP and mark < VIPId:
            mark += 1
            continue
        if tables[index].isVIP:
            if players[mark].isVIP:
                playerToTable(mark, index)
                if VIPId == mark:
                    VIPId = findVip(VIPId)
                mark += 1
            else:
                if VIPId < len(players) and players[VIPId].arrive <= tables[index].end:
                    playerToTable(VIPId, index)
                    VIPId = findVip(VIPId)
                else:
                    playerToTable(mark, index)
                    mark += 1
        else:
            if not players[mark].isVIP:
                playerToTable(mark, index)
                mark += 1
            else:
                VIPIndex, minVIPEnd = -1, 999999999
                for i in range(1, k + 1):
                    if tables[i].isVIP and tables[i].end < minVIPEnd:
                        minVIPEnd = tables[i].end
                        VIPIndex = i
                if VIPIndex != -1 and players[mark].arrive >= tables[VIPIndex].end:
                    playerToTable(mark, VIPIndex)
                    if VIPId == mark:
                        VIPId = findVip(VIPId)
                    mark += 1
                else:
                    playerToTable(mark, index)
                    if VIPId == mark:
                        VIPId = findVip(VIPId)
                    mark += 1
    players.sort(key=functools.cmp_to_key(cmpStart))
    for i in players:
        if i.start >= 21 * 3600:
            continue
        print(i.toString())
    for i in range(1, k + 1):
        print("%d" % tables[i].num, end=('\n' if i == k else ' '))


if __name__ == "__main__":
    solve()

c++:
直接看大神的吧:传送门

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值